-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
415 lines (380 loc) · 18.3 KB
/
index.js
File metadata and controls
415 lines (380 loc) · 18.3 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
const _ = require("lodash")
const cache = require('cache')
const hotkey = require('hotkey')
const net = require("net")
const pref = require("pref")
const i18n = require("i18n")
const http = require('http')
const pb = require('pasteboard')
const {getPostId, getUnreadFeeds, getFeedsWithRead, getMatrixData, getHomepageData} = require('./sspai.js')
const {getUpdateFrequency, getFetchArticleNum, getMenuBarStyleName, isDebugMode, isUnreadNotifyOpen, getDebugHotkey, getExpertMode, debug} = require('./tool.js')
function updateData() {
debug(__("update data starting"), true)
//PREF stuff
const LIMIT = getFetchArticleNum()
const IS_UNREAD_NOTIFY_OPEN = isUnreadNotifyOpen()
debug(`[Read PREF] 更新文章数:${LIMIT}`)
debug(`[Read PREF] 未读消息提醒:${IS_UNREAD_NOTIFY_OPEN}`)
//component init render
here.miniWindow.set({ title: __("Fetching…")})
here.miniWindow.reload()
debug("[COMPONENT] miniWindow reloaded. finish title setting")
// menu bar component display
const stylePath = "./menubar/" + getMenuBarStyleName();
debug("menubar style path: " + stylePath);
here.menuBar.set({
icon: stylePath,
})
here.menuBar.reload()
debug("[COMPONENT] miniWindow reloaded. finish menubar icon setting")
//use Promise.all to get all the tab data
Promise.all([getMatrixData(), getHomepageData()])
.then( (results) => {
// console.log(results)
let matrixData = results[0]
// console.log(matrixData.items)
let homepageData = results[1]
if (matrixData == null && homepageData == null) {
console.log("all tabs data get null");
here.miniWindow.set({ title: "Fetching Failed..." })
return
}
debug(`fetchedAPIDataRawSize: matrix-${matrixData.items.length} homepage-${homepageData.items.length}`)
//basic check
if ((matrixData.items.length + homepageData.items.length) <= 0) {
here.miniWindow.set({ title: __("No item found.") })
return
}
//filter homepageData in matrixData
homepageData.items = _.differenceWith(homepageData.items, matrixData.items, (s, t) => {
return s.link == t.link
})
debug(`filter Homepage duplicate data. remain:${homepageData.items.length}`)
if (matrixData.items.length > LIMIT) {
matrixData.items = matrixData.items.slice(0, LIMIT)
}
if (homepageData.items.length > LIMIT) {
homepageData.items = homepageData.items.slice(0, LIMIT)
}
debug(`toRenderSize. matrix:${matrixData.items.length} homepage:${homepageData.items.length}`)
const cachedPostIds = JSON.parse(cache.get('readIds') || '[]');
const checkUnreadFeedsNum = getUnreadFeeds(
(getShowExpertSpecificSwitch('index-channel', true)
? _.concat(matrixData.items, homepageData.items)
: matrixData.items),
cachedPostIds).length
debug("unread total: " + checkUnreadFeedsNum)
//unread notify
if (checkUnreadFeedsNum > 0 && IS_UNREAD_NOTIFY_OPEN) {
//when in debug mode, system notifications will be conflicted
//delay the unread notification for seconds later
_.delay((unreadNum) => {
here.systemNotification("【少数派有新的文章更新啦】", `未读数 ${checkUnreadFeedsNum}`)
}, isDebugMode() ? 5000 : 1000);
}
// render component
let renderComponent = () => {
let readIds = JSON.parse(cache.get('readIds') || '[]');
debug(`cachedIDs:${JSON.stringify(readIds)}`)
//TOP Feed set......
const unreadFeeds = getUnreadFeeds(
(getShowExpertSpecificSwitch('index-channel', true)
? _.concat(matrixData.items, homepageData.items)
: matrixData.items),
readIds)
let topFeed = _.head(unreadFeeds)
debug(`topFeed: ${topFeed != undefined ? topFeed.title : ""}`)
here.miniWindow.set({
onClick: () => {
//console.log("topfeed is" + JSON.stringify(topFeed))
if (topFeed != undefined && topFeed.link != undefined) {
let postId = getPostId(topFeed.link);
console.log("get post id:" + postId)
//filter cached postId
if (_.indexOf(readIds, postId) == -1) {
debug(`[click] push postId:${postId} to cache`, false, true);
readIds.push(postId);
cache.set("readIds", readIds);
} else {
debug(`cacheExists:${postId} skip`);
}
if (!isDebugMode()) {
here.openURL(topFeed.link);
here.miniWindow.hide()
}
}
},
title: topFeed == undefined ? '暂无最新文章' : `${isDebugMode() ? "🐞" : ""}${topFeed.title}`,
detail: "少数派文章更新",
accessory: {
badge: unreadFeeds.length + ""
}
})
here.miniWindow.reload()
debug("[COMPONENT] miniWindow reloaded.")
//support multi tab for different channels
let matrixKey = __("Matrix")
let homepageKey = __("Homepage")
const tabRawData = {
[matrixKey]: getRenderedData(matrixData.items, readIds),
...(getShowExpertSpecificSwitch('index-channel', true))
&& {[homepageKey]: getRenderedData(homepageData.items, readIds)},
}
// console.log(tabRawData)
const tabData = _.map(tabRawData, (val, key) => {
// console.log("val: " + val + "key:" + key)
return {
"title": key,
"data": formatTabData(val, readIds)
}
})
//expert mode tab
if (getExpertMode()) {
let expertModeConfig = JSON.parse(cache.get('expert') || '{}')
console.log("avatar switch: " + getShowExpertSpecificSwitch('avatar', true))
console.log("read switch: " + getShowExpertSpecificSwitch('read', false))
console.log("index-channel switch: " + getShowExpertSpecificSwitch('index-channel', true))
let expertModeTab = {
"title": "⚙️高级设置🤫",
"data": [
{
title: "=================展示配置================="
},
{
title: "是否展示作者头像",
accessory: new here.SwitchAccessory({
id: "accessory-avatar",
isOn: getShowExpertSpecificSwitch('avatar', true),
onValueChange: (isOn) => {
console.log(`accessory-avatar isOn: ${isOn}`);
expertModeConfig['avatar'] = isOn ? true : false
// console.log(expertModeConfig)
cache.set('expert', expertModeConfig)
console.log(cache.get('expert'))
here.popover.update(`#accessory-avatar.isOn`, isOn)
here.hudNotification(`avatar switch is ${isOn ? "On" : "Off"}.`);
}
}),
},
{
title: "是否展示已读文章",
accessory: new here.SwitchAccessory({
id: "accessory-read",
isOn: getShowExpertSpecificSwitch('read', false),
onValueChange: (isOn) => {
console.log(`read isOn: ${isOn}`);
expertModeConfig['read'] = isOn ? true : false
cache.set('expert', expertModeConfig)
console.log(cache.get('expert'))
here.popover.update(`#accessory-read.isOn`, isOn)
here.hudNotification(`read post switch is ${isOn ? "On" : "Off"}.`);
}
}),
},
{
title: "是否展示[首页]频道",
accessory: new here.SwitchAccessory({
id: "accessory-index-channel",
isOn: getShowExpertSpecificSwitch('index-channel', true),
onValueChange: (isOn) => {
console.log(`index channel isOn: ${isOn}`);
expertModeConfig['index-channel'] = isOn ? true : false
cache.set('expert', expertModeConfig)
console.log(cache.get('expert'))
here.popover.update(`#accessory-index-channel.isOn`, isOn)
here.hudNotification(`index channel switch is ${isOn ? "On" : "Off"}.`);
}
}),
},
]
}
if (isDebugMode()) {
let debugLineData = [
{
title: "=================DEBUG================="
},
{
title: "一键清除缓存",
accessory: new here.MainTextAccessory({
title: "谨慎使用",
}),
onClick: () => {
cache.removeAll()
here.hudNotification("Cache info cleared.");
//reset debug mode setting
cache.set('debug-hotkey-switch', 1)
}
},
{
title: "一键复制缓存信息",
onClick: () => {
pb.setText(JSON.stringify(cache.all()));
here.hudNotification("Cache info copied.");
}
},
{
title: "一键清除已读",
onClick: () => {
cache.remove("readIds")
here.hudNotification("Read cache cleared.");
}
},
]
let mergeData = expertModeTab.data
mergeData.push(...debugLineData)
}
tabData.push(expertModeTab)
}
// console.log("tabData:" + JSON.stringify(tabData))
here.popover = new here.TabPopover(tabData);
here.popover.reload()
debug("[COMPONENT] popover reloaded.")
// menu bar component display
// const stylePath = "./menubar/" + getMenuBarStyleName();
// debug("menubar style path: " + stylePath);
here.menuBar.set({
title: ` ${unreadFeeds.length}`,
icon: stylePath,
})
// here.menuBar.setIcon(stylePath);
here.menuBar.reload()
debug("[COMPONENT] menuBar reloaded.")
//dock component display
here.dock.set({
title: `${unreadFeeds.length}`,
detail: "少数派更新"
})
here.dock.reload()
debug("[COMPONENT] dock reloaded.")
}
debug("Render component start...", true)
renderComponent()
//rerender component display, partial render is not supported for now
here.popover.on('close', () => {
debug("onPopOverDisappear", true)
debug("Rerender component start")
// here.popover.reload()
renderComponent()
})
})
.catch( (error) => {
console.error(`promise all error: ${JSON.stringify(error)}`)
//TODO interrupt retry ,api not supported
here.miniWindow.set({ title: "Fetching Failed..." })
})
}
function getShowExpertSpecificSwitch(switchName, notExistsDefaultValue) {
const expertModeConfig = JSON.parse(cache.get('expert') || '{}')
if (_.isEmpty(expertModeConfig) || expertModeConfig[switchName] == undefined) return notExistsDefaultValue
return expertModeConfig[switchName]
}
function getRenderedData(feeds, readIds) {
const readSwitch = getShowExpertSpecificSwitch('read', false)
if (readSwitch) {
return getFeedsWithRead(feeds, readIds)
}
return getUnreadFeeds(feeds, readIds)
}
function formatTabData(rawUnreadFeeds, readIds) {
// console.log(rawUnreadFeeds)
if (_.isEmpty(rawUnreadFeeds)) {
return [{title: "Nothing New here."}]
}
return _.map(rawUnreadFeeds, (item, index) => {
// console.log("item detail" + JSON.stringify(item))
// console.log("item avatar: " + item.avatar)
return {
title: isDebugMode()
? `${index + 1}. ${item.title} PID:${getPostId(item.link)}`
: `${index + 1}. ${item.title}`,
accessory: {
title: "",
...(getShowExpertSpecificSwitch('avatar', true)) && {imageURL: `${item.avatar}`},
...(getShowExpertSpecificSwitch('avatar', true)) && {imageCornerRadius: 4}
},
onClick: () => {
if (item.link != undefined) {
let postId = getPostId(item.link);
//filter cached postId
if (_.indexOf(readIds, postId) == -1) {
debug(`[click] push postId:${postId} to cache`, false, true);
readIds.push(postId);
// debug(JSON.stringify(readIds));
cache.set("readIds", readIds);
} else {
debug(`cacheExists:${postId} skip`);
}
if (!isDebugMode()) {
here.openURL(item.link);
}
}
},
};
})
}
function initDebugHotKey() {
//ensure debug switch was initialized closed on every onLoad
// cache.set('debug-hotkey-switch', 0)
let hotkeySetting = getDebugHotkey();
if (hotkeySetting == "") return
debug(`[Read PREF] Hotkey: ${hotkeySetting}`)
if (!hotkey.assignable(hotkeySetting.split("+"))) {
here.systemNotification(`【🐞DEBUG热键{${hotkeySetting}} 已绑定其他快捷键】`, "请重新设定或者清空绑定")
return
}
let bindResult = hotkey.bind(hotkeySetting.split("+"), () => {
//debug mode only available in expert mode
const identifier = here.pluginIdentifier()
if (!getExpertMode()) {
here.systemNotification("【🐞DEBUG模式开启失败】", `必须开启 ${identifier} 的高级模式后才可以使用`)
return false
}
debug('|DEBUG_MODE CHANGED|', false, true)
debug(`Before: ${cache.get('debug-hotkey-switch')}`)
//Toggle Debug hotkey, implement use a simple cache switch
const debugSwitch = cache.get('debug-hotkey-switch')
if (debugSwitch != undefined && _.toSafeInteger(debugSwitch) == 1) {
here.systemNotification("【🐞DEBUG模式】", `当前 ${identifier} 已关闭 DEBUG 模式`)
cache.set('debug-hotkey-switch', 0)
debug('After: 0')
} else {
here.systemNotification("【🐞DEBUG模式】", `当前 ${identifier} 处于 DEBUG 模式
1. 在高级设置下面会有 Debug 菜单
2. 帖子标题增加 POST_ID 方便追溯
`)
// cache.removeAll()
//ensure debug switch exists
cache.set('debug-hotkey-switch', 1)
debug('After: 1')
}
//rerender
debug('Rerender component start')
updateData()
})
debug(`Debug hotkey bindResult: ${bindResult}`)
}
/**
* onLoad will be called in below scenes
* - restart here
* - save plugin pref
* - reload plugin in Debug Console
*/
here.on("load", () => {
debug("========== Plugin Debug Info Start ==========", false, true)
debug("↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓", false, true)
//main flow
debug(`init debug feature`, true)
initDebugHotKey();
debug(`[CURRENT_DEBUG_MODE] ${isDebugMode()}`)
updateData()
setInterval(updateData, getUpdateFrequency() * 3600 * 1000);
})
let type = net.effectiveType;
net.onChange((currentType) => {
debug("Connection type changed from " + type + " to " + currentType, true);
type = currentType;
if (net.isReachable()) {
console.log(__("net work recovered. execute updating"))
updateData()
}
})