-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.is
358 lines (299 loc) · 10.6 KB
/
plugin.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
/// @ 0.12.4
### {
name: "Pronouns in name V3.3.9"
version: "3.3.9"
author: "@ChaosKitsune@woem.men"
description: "This will try to put the users pronouns in their name on any given post"
permissions: ["read:account", "write:notifications"]
config: {
experimentalDescMethod: {
type: 'boolean'
label: 'Use experimental algorithm'
description: 'This could put some random stuff in their name, but also will allow for more acurate pronouns'
default: true
},
debugExperimentalDescMethod: {
type: 'boolean'
label: 'Debug experimental algorithm'
description: 'Prints the data related to the setting above'
default: false
},
checkFields: {
type: 'boolean'
label: 'Check Fields'
description: 'If its crashing try disabling this'
default: true
},
notifyForUpdates: {
type: 'boolean'
label: 'Notify you when there is an update'
description: 'Turning this on will attempt to notify you once when there is an update for this plugin'
default: true
},
debug: {
type: 'boolean'
label: 'Debug'
description: 'If it fails to find find pronouns it will put (unknown) at the end of the name'
default: false
}
}
}
let version = 12
let versionPost = "https://woem.men/notes/9t8r1dy4eemc1u2l"
let updateIcon = "https://woem.men/files/92af615a-4a8b-4fa0-bc61-25aa5b7f6f54"
//write:notifications
@send_notification(message, header){
Mk:api("notifications/create", {
body: message,
header: header,
icon: updateIcon
})
}
//read:account
@get_post_by_url(posturl){
let post = Mk:api("ap/show", {
uri: posturl
})
if Obj:has(post, "type") {
return post
}
return null
}
@check_for_update(){
let post = get_post_by_url(versionPost)
if post != null {
let text = post.object.text
if Json:parsable(text) {
let latest_version = Json:parse(post.object.text).pronounPluginVersion
if latest_version > version && Mk:load("updateRequested") != latest_version {
send_notification("Please install the latest version of the plugin here /~https://github.com/ChaoticLeah/pronoun-plugin", "Pronoun Plugin Update")
Mk:save("updateRequested", latest_version)
}
}
}
}
// Pronoun Plugin Stuff
@is_pronoun(pronoun, lowerDesc){
return lowerDesc.index_of([pronoun, "/"].join()) != -1 || lowerDesc.index_of(["/", pronoun].join()) != -1
}
@contains_any_pronouns(pronouns, lowerDesc){
for let i, pronouns.len {
let found_pronoun = lowerDesc.incl(pronouns[i])
if found_pronoun {
return true
}
}
return false
}
//Make sure that a "word" isnt like 10/10 rather than she/her
@is_word_fraction(word){
return contains_any_pronouns(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"], word)
}
@is_word_mfm(word){
return contains_any_pronouns(["<", ">"], word)
}
@trim_mfm(word){
var trimmed = word.split("]")[0].split("[")
return trimmed[trimmed.len - 1]
}
@trim_parrens(word){
var trimmed = word.split(")")[0].split("(")
return trimmed[trimmed.len - 1]
}
@split_by_space_and_nl(desc) {
var arr = desc.split(" ")
var new_arr = []
for let i, arr.len {
let split_by_nl = arr[i].split(Str:lf)
for let j, split_by_nl.len {
new_arr.push(split_by_nl[j])
}
}
return new_arr
}
//The new function to replace check_desc
@find_pronouns_in_desc(desc){
let words_in_desc = split_by_space_and_nl(desc)
//desc.split(" ")
if Plugin:config.debugExperimentalDescMethod {
print(words_in_desc)
}
for let i, words_in_desc.len {
let word = trim_parrens(trim_mfm(words_in_desc[i])).replace(",", "")
//Make sure there is a / and its not w/ (Cuz thats short for with)
let is_potential_pronoun = word.incl("/") && (!word.incl("w/")) && (!word.incl("and/or")) && (!word.incl("or/and")) && (!word.incl(".")) && (!word.incl("http")) && (word.index_of("/") > 1 || word.len == 1)
if is_potential_pronoun {
let is_fraction = is_word_fraction(word)
let is_word_mfm = is_word_mfm(word)
if (!is_fraction) && (!is_word_mfm) {
// That might mean someone spaced it out like she / her vs she/her
if (word.len == 1 && i != 0 && i != words_in_desc.len) {
return [trim_mfm(words_in_desc[i - 1]), word, trim_mfm(words_in_desc[i + 1])].join("").replace(",", "")
}
return word.trim()
}
}
}
return ""
}
@check_desc(desc){
let lowerDesc = desc.lower()
if is_pronoun("she", lowerDesc) || is_pronoun("her", lowerDesc) {
return "She/Her"
}
if is_pronoun("he", lowerDesc) || is_pronoun("him", lowerDesc) {
return "He/Him"
}
if is_pronoun("they", lowerDesc) || is_pronoun("them", lowerDesc) {
return "They/Them"
}
if is_pronoun("it", lowerDesc) || is_pronoun("its", lowerDesc) {
return "It/Its"
}
return ""
}
@get_has_headmate(post) {
return post.incl("<small> - posted by ")
}
@headmate_post_name(post) {
var noteHeadmateCharStartPos = post.index_of("<small> - posted by ") + ("<small> - posted by ".len)
var rtn = post.slice(noteHeadmateCharStartPos, post.len - ("</small>".len))
//A little fix for a problem that *sometimes* happens
return (((rtn.split("<")[0])).split(Str:lf)[0]).trim()
}
@get_desc_starting_from(desc, name) {
var descHeadmateMentionStart = desc.index_of(name)
return desc.slice(descHeadmateMentionStart, desc.len)
}
@get_pronouns_from_desc(description) {
if Plugin:config.experimentalDescMethod {
return find_pronouns_in_desc(description)
} else {
return check_desc(description)
}
}
@replace_last(str, key, replacer) {
let split_by_key = str.split(key)
split_by_key.pop()
let start_of_word_to_replace = split_by_key.join(key).len
let end_of_word_to_replace = start_of_word_to_replace + key.len
return [str.slice(0, start_of_word_to_replace), replacer, str.slice(end_of_word_to_replace , str.len) ].join("")
}
@check_note(note) {
if note.reply != null {
note.reply = check_note(note.reply)
}
if note.user.name == null {
note.user.name = ""
}
var fields = []
var notePostedBy = ""
var currentlyFronting = ""
var description = if note.user.description != null {
note.user.description
} else {
""
}
let data = {
pronouns: "",
pluralPostPronouns: null,
}
let hasHeadmate = if note.text != null {
get_has_headmate(note.text)
} else {
false
}
if Plugin:config.checkFields {
let userData = Mk:api("users/show", {username: note.user.username, host: note.user.host})
fields = userData.fields
// Misskey cant grab the description from the note sometimes so if it can it grabs it here
if userData.description != null && !hasHeadmate {
note.user.description = userData.description
description = note.user.description
}
}
if hasHeadmate {
currentlyFronting = note.user.name.slice(note.user.name.index_of("(") + 1, note.user.name.index_of(")"))
notePostedBy = headmate_post_name(note.text)
let current_fronter_desc = get_desc_starting_from(note.user.description, currentlyFronting)
description = current_fronter_desc
let note_posted_by_desc = get_desc_starting_from(note.user.description, notePostedBy)
if Plugin:config.experimentalDescMethod {
data.pluralPostPronouns = find_pronouns_in_desc(note_posted_by_desc)
} else {
data.pluralPostPronouns = check_desc(note_posted_by_desc)
}
if Plugin:config.debug {
if data.pluralPostPronouns == null {
data.pluralPostPronouns = "Unknown"
}
}
}
var descPronoun = get_pronouns_from_desc(description)
// If nothing comes up for that headmate then just go with the general search
if hasHeadmate {
if data.pluralPostPronouns == null {
description = note.user.description
descPronoun = get_pronouns_from_desc(description)
} else {
note.text = replace_last(note.text, notePostedBy, [notePostedBy, " (", data.pluralPostPronouns, ")"].join(""))
}
}
data.pronouns = descPronoun
// If there are headmates then dont use the fields since right now we dont support them (unless no pronouns are found already in the desc)
if fields.len > 0 && ((!hasHeadmate) || descPronoun.len == 0 || descPronoun.len > 15) {
for let i, fields.len {
if fields[i].name.lower().incl("pronoun") && !fields[i].value.incl("depends") {
if !Plugin:config.experimentalDescMethod {
// Overwite the pronouns found in the desc, and prefer ones set in the fields
if !fields[i].value.incl("http") {
data.pronouns = fields[i].value
}
} else {
var found_pronouns = find_pronouns_in_desc(fields[i].value)
if found_pronouns.len != 0 {
data.pronouns = found_pronouns
if data.pronouns.len == 0 {
data.pronouns = find_pronouns_in_desc(fields[i].name)
}
}
}
break
}
//If using the experimental method also apply it to the fields incase they didnt name it pronoun(s)
if Plugin:config.experimentalDescMethod && data.pronouns.len == 0 {
data.pronouns = find_pronouns_in_desc(fields[i].value)
if data.pronouns.len == 0 {
data.pronouns = find_pronouns_in_desc(fields[i].name)
}
}
}
}
if Plugin:config.debug {
if data.pronouns.len == 0 {
data.pronouns = "Unknown"
}
}
//If there are no pronouns then just return it as-is
if data.pronouns.len == 0 {
return note
}
var name = note.user.name
if name == "" {
name = note.user.username
}
let ltr_char = ""
note.user.name = [name, ltr_char, " (", data.pronouns, ")"].join("")
return note
}
Plugin:register_note_view_interruptor(@(note) {
note = check_note(note)
if note.renote != null {
note.renote = check_note(note.renote)
}
return note
})
print("Pronouns In Name Successfully Installed")
if Plugin:config.notifyForUpdates {
check_for_update()
}