Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Bootstrap.ts « ts - github.com/nextcloud/jsxc.nextcloud.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 353aa3b0abc26be5cc23f8800fdeeedb4e8ea365 (plain)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { DEPENDENCIES } from './CONST';
import Settings from './Settings';
import injectChatIcon from './ChatIconInjector';
import { addChatSubmitButton } from './ChatSubmitButtonInjector';
import { IJID } from '@jsxc/jsxc/src/JID.interface';
import defaultAvatar from './DefaultAvatar';
import Storage from './Storage';
import { armXMPPSchemaHandler } from './XMPPSchemaHandler';

export default class Bootstrap {
   private jsxc;

   public check() {
      this.checkDependencies();
      this.checkFrame();
      this.checkSpecialPage();
   }

   private checkDependencies() {
      for (let dependency of DEPENDENCIES) {
         if (typeof (<any> window)[dependency] === 'undefined') {
            throw new Error(`Dependency "${dependency}" is missing.`);
         }
      }
   }

   private checkFrame() {
      if (window.parent && window !== window.parent) {
         throw new Error(`Abort, because we are running inside a frame.`);
      }
   }

   private checkSpecialPage() {
      if (/^(\/index.php)?\/s\//.test(window.location.pathname)) {
         throw new Error(`Abort, because we dont want to start chat on public shares.`);
      }

      if (OC.generateUrl('login/flow') === window.location.pathname) {
         throw new Error(`Abort, because chat is not needed on flow login.`);
      }

      if (OC.generateUrl('login/challenge/totp') === window.location.pathname) {
         throw new Error('Abort, because chat is not needed on totp page.');
      }
   }

   public start() {
      if (typeof OJSXC_CONFIG === 'undefined') {
         setTimeout(this.start.bind(this), 100);

         return;
      }

      JSXC.jQuery(document).on('ajaxSend', function(_elm, xhr, settings) {
         if (settings.crossDomain === false) {
            xhr.setRequestHeader('requesttoken', OC.requestToken);
            xhr.setRequestHeader('OCS-APIREQUEST', 'true');
         }
      });

      if (OJSXC_CONFIG.serverType !== 'external' && OJSXC_CONFIG.serverType !== 'managed') {
         return;
      }

      this.initJSXC();

      if (OC.generateUrl('/login') === window.location.pathname) {
         this.addWatcher();
         this.addAlternativeLogin();
      }

      injectChatIcon(this.jsxc.toggleRoster);
      armXMPPSchemaHandler(this.jsxc.executeXMPPUri);
   }

   private initJSXC() {
      this.jsxc = new JSXC({
         appName: 'Nextcloud',
         rosterVisibility: OJSXC_CONFIG.startMinimized ? 'hidden' : 'shown',
         loadConnectionOptions: Settings.loadConnection,
         loadOptions: Settings.load,
         onOptionChange: Settings.onOptionChange,
         avatarPlaceholder: (element: JQuery, name: string, color: string, jid: IJID) => {
            defaultAvatar(element, name, jid);
         },
         onUserRequestsToGoOnline: this.onUserRequestsToGoOnline.bind(this),
         RTCPeerConfig: {
            url: OC.generateUrl('apps/ojsxc/settings/iceServers')
         },
         automaticallyRestoreAccounts: window.location.pathname.indexOf(OC.generateUrl('/login')) !== 0,
      });

      //For debugging
      (<any> window).ojsxc = {
         jsxc: this.jsxc,
      };

      if (this.jsxc.numberOfCachedAccounts === 0) {
         if (OC.getCurrentUser().uid) {
            this.jsxc.start();
         }
      }
   }

   private addWatcher() {
      let formElement = $('#body-login form[name="login"]');
      let usernameElement = $('#user');
      let passwordElement = $('#password');

      if (formElement.length && usernameElement.length && passwordElement.length) {
         this.jsxc.watchForm(formElement, usernameElement, passwordElement);
      }

      let logoutElement = $('[data-id="logout"] a');

      if (logoutElement.length) {
         this.jsxc.watchLogoutClick(logoutElement);
      }
   }

   private addAlternativeLogin() {
      let formElement = $('#body-login form[name="login"]');

      addChatSubmitButton(formElement, this.jsxc.translate);
   }

   private async onUserRequestsToGoOnline() {
      try {
         let settings = await Settings.loadConnection(undefined, undefined);

         if (!Storage.get().getItem('serverIsOmniscient') && OJSXC_CONFIG.serverType !== 'internal') {
            this.jsxc.showLoginBox();

            return;
         }

         if (!settings) {
            throw new Error('No settings provided');
         }

         let xmpp = settings.xmpp;
         let jid = xmpp.node + '@' + xmpp.domain;

         if (xmpp.resource) {
            jid +=  '/' + xmpp.resource;
         }

         this.jsxc.start(xmpp.url, jid, xmpp.password || '');
      } catch (err) {
         console.log('Error during log in', err);

         this.jsxc.showLoginBox();
      }
   }
}