forked from nick-denry/Chimeverse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.js
192 lines (153 loc) · 5.69 KB
/
renderer.js
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// All of the Node.js APIs are available in this process.
const angular = require('angular')
var angApp = angular.module('app', [])
angApp.factory('SettingsServise', () => {
const keytar = require('keytar')
const settings = require('electron-settings')
let settingsService = {}
settingsService.getCredentials = () => {
let credentials = {}
credentials.login = settings.get('login')
let promise = new Promise((resolve, reject) => {
if (credentials.login) {
credentials.bosh = settings.get('bosh')
credentials.xmppService = credentials.login.split('@').pop()
let password = keytar.getPassword(credentials.xmppService, credentials.login)
password.then((result) => {
credentials.password = result
resolve(credentials)
})
}
else {
reject(Error('No login stored'))
}
})
return promise
}
settingsService.addCredentials = (bosh, login, password) => {
let xmppService = login.split('@').pop()
settings.set('bosh', bosh)
settings.set('login', login)
keytar.setPassword(xmppService, login, password)
}
settingsService.removeCredentials = (login) => {
let xmppService = login.split('@').pop()
passwordDelete = keytar.deletePassword(xmppService, login)
let promise = new Promise((resolve, reject) => {
passwordDelete.then((result) => {
settings.delete('login')
settings.delete('bosh')
resolve()
}, (error) => {
reject(error)
})
})
return promise
}
return settingsService
})
angApp.factory('SystemService', () => {
const remote = require('electron').remote
let systemService = {}
systemService.playAudio = () => {
var audio = new Audio(__dirname + '/sounds/graceful.ogg')
audio.play()
}
systemService.showEnvelope = () => {
remote.require('./main').trayService.showEnvelope()
}
systemService.hideEnvelope = () => {
remote.require('./main').trayService.hideEnvelope()
}
systemService.reloadWindow = () => {
remote.getCurrentWindow().reload()
}
return systemService
})
angApp.factory('ChimeVerseService', (SettingsServise, SystemService) => {
let chimeverseService = {}
chimeverseService.settings = SettingsServise
chimeverseService.system = SystemService
chimeverseService._notifyMessage = (data) => {
if (data.message.attributes.sender === 'me') {
chimeverseService.system.hideEnvelope()
return ;
}
if (data.message.attributes.chat_state === 'active') {
chimeverseService.system.playAudio()
chimeverseService.system.showEnvelope()
}
}
chimeverseService.logout = () => {
let credentials = SettingsServise.getCredentials()
credentials.then((result) => {
let remove = chimeverseService.settings.removeCredentials(result.login)
remove.then(() => {
chimeverseService.system.reloadWindow()
})
})
}
chimeverseService.addChimeVersePluign = () => {
converse.plugins.add('chimeVerse', {
initialize: (event) => {
var _converse = event.properties._converse
Promise.all([
_converse.api.waitUntil('rosterContactsFetched'),
_converse.api.waitUntil('chatBoxesFetched')
]).then(() => {
_converse.api.listen.on('logout', () => {
chimeverseService.logout()
})
_converse.api.listen.on('messageAdded', (data) => {
chimeverseService._notifyMessage(data)
})
_converse.api.listen.on('chatBoxFocused', () => {
chimeverseService.system.hideEnvelope()
})
})
}
})
}
chimeverseService.initConverse = (bosh, login, password) => {
chimeverseService.addChimeVersePluign()
let lang = navigator.language
converse.initialize({
bosh_service_url: bosh,
view_mode: 'fullscreen',
jid: login + '/chimeverse',
password: password,
auto_login: true,
whitelisted_plugins: ['chimeVerse'],
i18n: lang,
priority: 50,
// debug: true,
auto_reconnect: true
})
}
return chimeverseService
})
angApp.controller('AppController', function ($scope, ChimeVerseService) {
const { ipcRenderer } = require('electron');
$scope.showLoginForm = false
ipcRenderer.on('force-logout-event', () => {
ChimeVerseService.logout()
ipcRenderer.getCurrentWindow().reload()
})
$scope.addAccountAction = () => {
ChimeVerseService.settings.addCredentials($scope.bosh, $scope.login, $scope.password)
$scope.showLoginForm = false
ChimeVerseService.initConverse($scope.bosh, $scope.login, $scope.password)
}
$scope.getCredentialsAndLogin = () => {
let credentials = ChimeVerseService.settings.getCredentials()
credentials.then((result) => {
ChimeVerseService.initConverse(result.bosh, result.login, result.password)
}, (error) => {
$scope.showLoginForm = true
$scope.$apply()
})
}
$scope.getCredentialsAndLogin()
})