File size: 3,680 Bytes
ae01f49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/////////////////////////////////////////////////////////////
//
// pgAdmin 4 - PostgreSQL Tools
//
// Copyright (C) 2013 - 2024, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
//////////////////////////////////////////////////////////////

import _ from 'lodash';

import pgAdmin from 'sources/pgadmin';
import pgWindow from 'sources/window';
import {getEpoch} from 'sources/utils';

const pgBrowser = pgAdmin.Browser = pgAdmin.Browser || {};
const MIN_ACTIVITY_TIME_UNIT = 1000; /* in seconds */
/*
 * User UI activity related functions.
 */
_.extend(pgBrowser, {
  inactivity_timeout_at: null,
  logging_activity: false,
  inactivity_timeout_daemon_running: false,

  is_pgadmin_timedout: function() {
    return !pgWindow.pgAdmin;
  },

  is_inactivity_timeout: function() {
    return pgWindow.pgAdmin.Browser.inactivity_timeout_at < this.get_epoch_now();
  },

  get_epoch_now: function(){
    return getEpoch();
  },

  log_activity: function() {
    if(!this.logging_activity) {
      this.logging_activity = true;
      this.inactivity_timeout_at = this.get_epoch_now() + pgAdmin.user_inactivity_timeout;

      /* No need to log events till next MIN_ACTIVITY_TIME_UNIT second as the
       * value of inactivity_timeout_at won't change
       */
      setTimeout(()=>{
        this.logging_activity = false;
      }, MIN_ACTIVITY_TIME_UNIT);
    }
  },

  /* Call this to register element for acitivity monitoring
   * Generally, document is passed to cover all.
   */
  register_to_activity_listener: function(target, timeout_callback) {
    let inactivity_events = ['mousemove', 'mousedown', 'keydown'];
    let self = this;
    inactivity_events.forEach((event)=>{
      /* Bind events in the event capture phase, the bubble phase might stop propagation */
      let eventHandler = function() {
        if(self.is_pgadmin_timedout()) {
          /* If the main page has logged out then remove the listener and call the timeout callback */
          inactivity_events.forEach((events)=>{
            target.removeEventListener(events, eventHandler, true);
          });
          timeout_callback();
        } else {
          pgWindow.pgAdmin.Browser.log_activity();
        }
      };

      target.addEventListener(event, eventHandler, true);
    });
  },

  /*
   * This function can be used by tools like sqleditor where
   * poll call is as good as user activity. Decorate such functions
   * with this to consider them as events. Note that, this is controlled
   * by override_user_inactivity_timeout.
   */
  override_activity_event_decorator: function(input_func) {
    return function() {
      /* Log only if override_user_inactivity_timeout true */
      if(pgAdmin.override_user_inactivity_timeout) {
        pgWindow.pgAdmin.Browser.log_activity();
      }
      return input_func.apply(this, arguments);
    };
  },

  logout_inactivity_user: function() {
    if (!_.isUndefined(pgBrowser.utils) &&
        !_.isUndefined(pgBrowser.utils.logout_url)) {
      window.location.href = pgBrowser.utils.logout_url;
    }
  },

  /* The daemon will track and logout when timeout occurs */
  start_inactivity_timeout_daemon: function() {
    let self = this;
    if(pgAdmin.user_inactivity_timeout > 0 && !self.inactivity_timeout_daemon_running) {
      let timeout_daemon_id = setInterval(()=>{
        self.inactivity_timeout_daemon_running = true;
        if(self.is_inactivity_timeout()) {
          clearInterval(timeout_daemon_id);
          self.inactivity_timeout_daemon_running = false;
          self.logout_inactivity_user();
        }
      }, MIN_ACTIVITY_TIME_UNIT);
    }
  },
});

export {pgBrowser};