forked from nakajmg/SlackStatusChanger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
256 lines (231 loc) · 6.73 KB
/
main.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//const electron = require('electron')
const {BrowserWindow, ipcMain, Menu, app} = require('electron')
const axios = require('axios')
const types = require('./store/types')
const APIError = require('./APIError')
const {keys, each, debounce} = require('lodash')
const Promise = require('bluebird')
function apiErrorReport({name, message}) {
console.error(name, message)
}
const menubar = require('menubar')({
preloadWindow: true,
transparent: true,
frame: false,
width: 280,
icon: `${__dirname}/icon.png`,
})
menubar.app.once('ready', () => {
const menuTemplate = require('./menuTemplate')
const menu = Menu.buildFromTemplate(menuTemplate)
Menu.setApplicationMenu(menu)
})
let preference = null
let signinWindow = null
menubar.on('ready', () => {
let menuWindow = menubar.window.webContents
menubar.window.on('close', () => {
app.quit()
})
const {powerMonitor} = require('electron')
powerMonitor.on('suspend', () => {
menuWindow.send(types.SUSPENDED)
})
powerMonitor.on('resume', () => {
menuWindow.send(types.RESUMED)
})
/**
* for debug
*/
//menubar.showWindow()
//menuWindow.openDevTools()
/**
* @param {string} apiToken
*/
function syncStatus ({apiToken}){
const url = 'https://slack.com/api/users.profile.get'
return axios({
url,
method: 'POST',
params: {
token: apiToken,
}
})
.then(res => res.data)
.then(body => {
if (body.error) throw new APIError(body)
console.log('sync success')
menuWindow.send(types.SET_CURRENT_STATUS, body.profile)
})
}
function getInfo({apiToken}) {
return Promise.all([
axios({
url : 'https://slack.com/api/team.info',
method: 'POST',
params: {
token: apiToken
}
})
.then(res => res.data)
.then(res => {
const name = res.team.name
const icon = res.team.icon.image_original
return {name, icon}
}),
axios({
url: 'https://slack.com/api/users.profile.get',
method: 'POST',
params: {
token: apiToken
}
})
.then(res => res.data)
.then(res => {
const name = res.profile.display_name
const icon = res.profile.image_original
return {name, icon}
})
])
}
/**
* @param {string} apiToken
* tokenをセットしてsyncStatusを呼ぶ役
*/
function verifyToken({apiToken}) {
// tokenのverifyを行う
// verifyが成功したらINITIALIZE_SUCCEESSかなにか飛ばしてINITIALIZEを完了する
// verify失敗したらpreferenceのtoken開いてエラー通知 エラーの詳細はURLパラメータで送る
return syncStatus({apiToken})
.then(() => {
verifySuccess({apiToken})
})
.catch(error => {
openPreference({
preferenceName: 'token',
error: `${error.name},${error.message}`,
})
console.log(error.name, error.message)
})
}
/**
* @param {string} apiToken
* tokenの検証が成功したら各windowに通知
*/
function verifySuccess({apiToken}) {
menuWindow.send(types.TOKEN_VERIFIED, {apiToken})
if (preference) {
preference.webContents.send(types.TOKEN_VERIFIED, {apiToken})
}
}
/**
* @param {string} apiToken
* @param {string} status_emoji
* @param {string} status_text
* 絵文字とテキストをAPIに投げる
*/
function setCurrentStatus({apiToken, status_emoji, status_text}) {
const url = 'https://slack.com/api/users.profile.set'
const options = {
url,
method: 'POST',
params: {
token: apiToken,
profile: {
status_emoji,
status_text,
}
}
}
return axios(options)
.then(res => res.data)
.then(body => {
console.log(body.ok, body.profile.status_emoji, body.profile.status_text)
menuWindow.send(types.SET_CURRENT_STATUS, body.profile)
})
.catch(apiErrorReport)
}
/**
* @param {string} preferenceName - メニューの名前
* @param {error} error - カンマ区切りでnameとmessage
* 設定画面開く
*/
function openPreference({preferenceName, error}) {
// 設定画面が開かれてたら既存の設定画面にフォーカス
if (preference) {
// 指定された設定項目を開く
preference.webContents.send(types.CHANGE_PREFERENCE_MENU, {preferenceName, error})
return preference.show()
}
preference = new BrowserWindow({
width: 480,
height: 400,
titleBarStyle: 'hidden',
fullscreenable: false,
maximizable: false,
show: false,
})
const listeners = {
[types.UPDATE_TOKEN](e, {apiToken}) {
verifyToken({apiToken})
if (signinWindow) {
signinWindow.close()
signinWindow = null
}
},
[types.READY_TO_SHOW]() {
preference.show()
}
}
each(listeners, (cb, channel) => ipcMain.on(channel, cb))
const errorParam = error ? `&error=${error}` : ''
preference.loadURL(`file://${__dirname}/preference.html?name=${preferenceName}${errorParam}`)
preference.on('close', () => {
menuWindow.send(types.CLOSE_PREFERENCE)
preference = null
ipcMain.removeAllListeners(keys(listeners))
})
}
const dsyncStatus = debounce(syncStatus, 100)
const dverifyToken = debounce(verifyToken, 100)
const listeners = {
[types.SYNC_STATUS](e, {apiToken}) { dsyncStatus({apiToken}) },
[types.INITIALIZE_STATUS](e, {apiToken}) { dverifyToken({apiToken}) },
[types.SET_CURRENT_STATUS](e, {apiToken, status_emoji, status_text}) {
setCurrentStatus({apiToken, status_emoji, status_text})
},
[types.OPEN_PREFERENCE](e, params = {}) { openPreference(params) },
[types.UPDATE_PREFERENCE](e, payload) { menuWindow.send(types.UPDATE_PREFERENCE, payload) },
[types.RESTART_APP]() {
if (preference) preference.close()
menuWindow.reload()
},
[types.EXIT_APP]() {
if (preference) preference.close()
menubar.window.close()
},
[types.OPEN_SIGNIN]() {
signinWindow = new BrowserWindow({
width: 600,
height: 680,
show: true,
})
signinWindow.loadURL(`file://${__dirname}/signin.html`)
},
[types.GOT_TOKEN](e, {apiToken}) {
if (signinWindow) {
signinWindow.close()
}
getInfo({apiToken})
.then(([team, user]) => {
preference.webContents.send(types.SET_INFO, {team, user})
menuWindow.send(types.UPDATE_PREFERENCE, {team, user})
verifySuccess({apiToken})
})
.catch(err => {
console.log(err)
})
}
}
each(listeners, (cb, channel) => ipcMain.on(channel, cb))
})