-
Notifications
You must be signed in to change notification settings - Fork 0
/
MisskeyDestroyerWatchPlugin-BlankConfig.is
452 lines (401 loc) · 15.8 KB
/
MisskeyDestroyerWatchPlugin-BlankConfig.is
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/// @ 0.13.3
### {
name: "通知からリアクション等を一覧で表示するプラグイン",
version: "0.3.0",
author: "はとぽぷ",
description: "直近最大300件前後の通知のリアクションとリノートを取得",
permissions: ["read:notifications"],
config: {
notificationLimit: {
type: "number",
label: "1回のリクエストで取得する通知の最大数",
description: "一度のAPIリクエストで取得できる通知の数。APIの制限により、最大値は100までです。",
default: 100
},
requestMaxCount: {
type: "number",
label: "APIリクエストの最大試行回数",
description: "APIに対するリクエストの最大試行回数。この回数に達した時点で、または結果が0の場合に処理が終了します。",
default: 4
},
usernameSuffixText: {
type: "string",
label: "ユーザー名の語尾につけるテキスト",
default: ""
},
reactionIsEnable: {
type: "boolean",
label: "リアクションを取得する",
default: true
},
renoteIsEnable: {
type: "boolean",
label: "リノートを取得する",
description: "引用リノートは含みません。",
default: true
},
linkIsEnable: {
type: "boolean",
label: "リアクションやリノートに元ノートのリンクを付与",
default: true
},
reactionReverse: {
type: "boolean",
label: "リアクションの表示順を反転",
default: true
},
dialogReactionVisible: {
type: "boolean",
label: "ダイアログのリアクション詳細表示",
default: true
},
dialogReactionCountVisible: {
type: "boolean",
label: "ダイアログのリアクション数表示",
default: true
},
dialogDestroyerLimit: {
type: "number",
label: "ダイアログの最大ユーザー数",
default: 20
},
dialogReactionMin: {
type: "number",
label: "ダイアログに表示する最低リアクション数",
default: 1
},
dialogHeaderText: {
type: "string",
label: "ダイアログのヘッダーテキスト",
default: ""
},
dialogUserPrefixText: {
type: "string",
label: "ダイアログのユーザー接頭辞テキスト",
default: ""
},
dialogReactionSeparator: {
type: "string",
label: "ダイアログのReaction間セパレータ",
default: ""
},
resultReactionVisible: {
type: "boolean",
label: "投稿用テキストのリアクション詳細表示",
default: true
},
resultReactionCountVisible: {
type: "boolean",
label: "投稿用テキストのリアクション数表示",
default: true
},
resultDestroyerLimit: {
type: "number",
label: "投稿用テキストの最大ユーザー数",
default: 1
},
resultReactionMin: {
type: "number",
label: "投稿用テキストに表示する最低リアクション数",
default: 1
},
resultHeaderText: {
type: "string",
label: "投稿用テキストのヘッダーテキスト",
default: ""
},
resultUserSuffixText: {
type: "string",
label: "投稿用テキストのユーザー末尾テキスト",
default: ""
},
resultReactionSeparator: {
type: "string",
label: "投稿用テキストでのReaction間セパレータ",
default: ""
},
resultFooterTagText: {
type: "string",
label: "投稿用テキストのフッターに追加するタグテキスト",
default: "#MisskeyDestroyerWatch"
},
resultTextMaxLength: {
type: "number",
label: "投稿用テキストの最大長",
description: "投稿用テキストで表示する最大文字数を設定します。",
default: 2800
},
resultReactionMaxLength: {
type: "number",
label: "投稿用リアクション一覧の最大長",
description: "投稿用テキストのユーザーのリアクション一覧の最大文字数を設定します。",
default: 2500
},
resultOverflowText: {
type: "string",
label: "長文超過時の省略テキスト",
description: "表示するテキストが設定された最大長を超えた場合に表示されるテキストを設定します。",
default: "<small><small>...(一部省略)</small></small>"
},
excludedReactions: {
type: "string",
label: "除外するリアクション",
description: "表示しないリアクションのワードをカンマ区切りで入力します。例:':ohayoo:,:oyasumisskey:'",
default: ""
}
}
}
// SeverNameの取得
let SERVER_NAME = Mk:url().split('/')[2]
// configに値がない場合デフォルト値を取得
@getConfigValue(configValue, defaultValue) {
if (configValue != null) {
configValue
} else {
defaultValue
}
}
// APIからの通知を取得
@getNotifications(limit, requestMaxCount) {
let resultList = []
var result = Mk:api("i/notifications", {limit: limit})
each (let res result){
resultList.push(res)
}
var until_id = result[result.len - 1].id
var count = 1
loop {
if count == requestMaxCount break
var payload = {limit: limit, untilId: until_id}
result = Mk:api("i/notifications", payload)
if (result.len == 0) break
each (let res result){
resultList.push(res)
}
until_id = result[result.len - 1].id
count = count + 1
}
resultList
}
// ユーザーとリアクションを集計
@aggregateUserReactions(notifications, config) {
// ユーザーとリアクションを保存するためのオブジェクト
let userReactionCount = {}
let userIdList = []
each (let notif notifications) {
// リアクションを取得
if (config.reactionIsEnable == true && notif.type == 'reaction') {
let userId = notif.user.id
let userName = notif.user.name
let noteId = notif.note.id
var reaction = `{notif.reaction}`
// userのリストの追加
if (!userIdList.incl(userId)) {
userIdList.push(userId)
}
// userReactionCountにuserIdがなければ初期化
if (Obj:has(userReactionCount, userId) == false) {
Obj:set(userReactionCount, userId, { user: notif.user, reactions: [] })
}
//リアクションを取得
if (config.linkIsEnable == true) {
reaction = `?[{notif.reaction}](https://{SERVER_NAME}/notes/{noteId})`
}
let userReactions = Obj:get(Obj:get(userReactionCount, userId), 'reactions')
// リアクションをユーザーに追加
userReactions.push(reaction)
}
// リノートを取得
if (config.renoteIsEnable == true && notif.type == 'renote') {
let userId = notif.user.id
let userName = notif.user.name
let noteId = notif.note.id
var reaction = `:rn:`
// userのリストの追加
if (!userIdList.incl(userId)) {
userIdList.push(userId)
}
// userReactionCountにuserIdがなければ初期化
if (Obj:has(userReactionCount, userId) == false) {
Obj:set(userReactionCount, userId, { user: notif.user, reactions: [] })
}
//リノートを取得
if (config.linkIsEnable == true) {
reaction = `?[:rn:](https://{SERVER_NAME}/notes/{noteId})`
}
let userReactions = Obj:get(Obj:get(userReactionCount, userId), 'reactions')
// リアクションをユーザーに追加
userReactions.push(reaction)
}
}
// countListに各リアクション数の追加
let countList = []
each (let userId userIdList){
let userReactions = Obj:get(Obj:get(userReactionCount, userId), 'reactions')
countList.push(userReactions.len)
}
let resultObj = {}
Obj:set(resultObj, "userReactionCount", userReactionCount)
Obj:set(resultObj, "userIdList", userIdList)
Obj:set(resultObj, "countList", countList)
resultObj
}
// indexに基づいてソート
@sortByIndices(sortedIndices, targetList) {
let sortedList = []
each (let i sortedIndices) {
sortedList.push(targetList[i])
}
sortedList
}
// ソート処理
@sortUsersByReaction(userIdList, countList) {
let sortedIndices = []
for (let i, countList.len) {
sortedIndices.push(i)
}
sortedIndices.sort(@(a, b) {
return countList[b] - countList[a]
})
let sortedUserIdList = sortByIndices(sortedIndices, userIdList)
let sortedCountList = sortByIndices(sortedIndices, countList)
let sortedObj = {}
Obj:set(sortedObj, "sortedUserIdList", sortedUserIdList)
Obj:set(sortedObj, "sortedCountList", sortedCountList)
sortedObj
}
// 半角/全角の空白を除去
@removeWhitespace(originalText) {
var newText = originalText.replace(" ","")
newText = newText.replace(" ", "")
return newText
}
// 除外するリアクションを除去
@filterReactions(userReactions, excludedReactions, config) {
let filteredReactions = []
each (let reaction userReactions) {
var exclude = false
if (excludedReactions.join("").len > 0) {
each (let exReaction excludedReactions) {
let exReactionNew = removeWhitespace(exReaction)
if exReactionNew.len == 0 break
if ((reaction).incl(exReactionNew)) {
exclude = true
break
}
}
}
if (!exclude) {
var filteredReactionsText = filteredReactions.join(config.resultReactionSeparator)
if (config.resultReactionMaxLength > filteredReactionsText.len) {
filteredReactions.push(reaction)
}
else {
filteredReactions.push(config.resultOverflowText)
break
}
}
}
return filteredReactions
}
// ダイアログと投稿用テキストの作成
@createUserTextLists(sortedUserIdList, userReactionCount, overrideResultText, config) {
// -----------------------------------------------------------------
var resultText = "通知破壊などありません"
// 注記。
var dialogCautionText = "<center><small>※このプラグインは直近最大300件前後の通知を取得するものです。ユーザーのページへの遷移はユーザー名右クリックで新しいタブを開くなどで飛んでください。</small></center>"
// -----------------------------------------------------------------
// 除外するリアクションを配列に変換
let excludedReactionsArray = config.excludedReactions.split(",")
// ダイアログと投稿用テキストの作成
let dialogTextList = []
let resultTextList = []
var destroyerCount = 0
dialogTextList.push(config.dialogHeaderText)
dialogTextList.push(dialogCautionText)
resultTextList.push(config.resultHeaderText)
each (let userId sortedUserIdList){
let user = Obj:get(Obj:get(userReactionCount, userId), 'user')
let userReactions = Obj:get(Obj:get(userReactionCount, userId), 'reactions')
let userHost = user.host
if (config.reactionReverse == true) {
userReactions.reverse()
}
var dialogReactionCountText = userReactions.len
if (config.dialogReactionCountVisible == false) {
dialogReactionCountText = ""
}
var resultReactionCountText = userReactions.len
if (config.resultReactionCountVisible == false) {
resultReactionCountText = ""
}
if (config.dialogDestroyerLimit > destroyerCount && userReactions.len >= config.dialogReactionMin) {
let dialogUserReactions = userReactions.join(`{config.dialogReactionSeparator}`)
if (config.dialogReactionVisible == false) {
dialogUserReactions = ""
}
if (userHost == null) {
dialogTextList.push(`{config.dialogUserPrefixText}@{user.username}{config.usernameSuffixText}: {dialogReactionCountText} {dialogUserReactions}`)
} else {
dialogTextList.push(`{config.dialogUserPrefixText}@{user.username}@{userHost}{config.usernameSuffixText}: {dialogReactionCountText} {dialogUserReactions}`)
}
}
if (config.resultDestroyerLimit > destroyerCount && userReactions.len >= config.dialogReactionMin) {
// リアクションから除外するリアクションを除去
let filteredUserReactions = filterReactions(userReactions, excludedReactionsArray, config)
let resultUserReactions = filteredUserReactions.join(`{config.resultReactionSeparator}`)
if (config.resultReactionVisible == false) {
resultUserReactions = ""
}
if (userHost == null) {
resultTextList.push(`@{user.username}{config.usernameSuffixText}: {resultReactionCountText} {config.resultUserSuffixText}{Str:lf}{resultUserReactions}{Str:lf}`)
} else {
resultTextList.push(`@{user.username}@{userHost}{config.usernameSuffixText}: {resultReactionCountText} {config.resultUserSuffixText}{Str:lf}{resultUserReactions}{Str:lf}`)
}
}
destroyerCount = destroyerCount + 1
}
// 結合
var resultTextMerged = resultTextList.join(Str:lf)
if (resultTextMerged.len > config.resultTextMaxLength) {
resultTextMerged = resultTextMerged.slice(0, config.resultTextMaxLength)
resultTextMerged = `{resultTextMerged}{config.resultOverflowText}`
}
// 最終的なダイアログテキスト
let dialogText = dialogTextList.join(Str:lf)
if (overrideResultText == true) {
// タグ付与
resultText = `{resultTextMerged}{Str:lf}{config.resultFooterTagText}`
} else {
resultText = overrideResultText
}
let resultTextObj = {}
Obj:set(resultTextObj, "dialogText", dialogText)
Obj:set(resultTextObj, "resultText", resultText)
resultTextObj
}
// メイン処理
@misskeyDestroyerWatch(overrideResultText) {
// 設定を取得
let config = Plugin:config
// 通知の取得数です。APIの制限により現在は最大100です。
var notificationLimit = getConfigValue(config.notificationLimit, 100)
// Misskey APIから通知の取得
let notifications = getNotifications(notificationLimit, config.requestMaxCount)
// ユーザーとリアクションを保存するためのオブジェクト
let userReactionObj = aggregateUserReactions(notifications, config)
let userReactionCount = Obj:get(userReactionObj, "userReactionCount")
let userIdList = Obj:get(userReactionObj, "userIdList")
let countList = Obj:get(userReactionObj, "countList")
// リアクション数順にソートしたオブジェクト
let sortedObj = sortUsersByReaction(userIdList, countList)
let sortedUserIdList = Obj:get(sortedObj, "sortedUserIdList")
// ダイアログと投稿用テキストの作成
let resultTextObj = createUserTextLists(sortedUserIdList, userReactionCount, overrideResultText, config)
let dialogText = Obj:get(resultTextObj, "dialogText")
let resultText = Obj:get(resultTextObj, "resultText")
// ダイアログに表示
Mk:dialog(null dialogText)
resultText}
Plugin:register_post_form_action(`[投稿]直近の通知を取得!` @(f u) {u("text" misskeyDestroyerWatch(true))})
Plugin:register_post_form_action(`[閲覧]直近の通知を取得!` @(f u) {u("text" misskeyDestroyerWatch(f.text))})