-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathadd-holiday-flair.js
96 lines (84 loc) · 2.65 KB
/
add-holiday-flair.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
import I18n from "I18n";
import { withPluginApi } from "discourse/lib/plugin-api";
import { cancel } from "@ember/runloop";
import getURL from "discourse-common/lib/get-url";
import { emojiUnescape } from "discourse/lib/text";
import discourseLater from "discourse-common/lib/later";
function applyFlairOnMention(element, username) {
if (!element) {
return;
}
const href = getURL(`/u/${username.toLowerCase()}`);
const mentions = element.querySelectorAll(`a.mention[href="${href}"]`);
mentions.forEach((mention) => {
if (!mention.querySelector(".on-holiday")) {
mention.insertAdjacentHTML(
"beforeend",
emojiUnescape(":desert_island:", { class: "on-holiday" })
);
}
mention.classList.add("on-holiday");
});
}
export default {
name: "add-holiday-flair",
initialize() {
withPluginApi("0.10.1", (api) => {
const usernames = api.container.lookup("service:site").users_on_holiday;
if (usernames && usernames.length > 0) {
api.addUsernameSelectorDecorator((username) => {
if (usernames.includes(username)) {
return `<span class="on-holiday">${emojiUnescape(
":desert_island:",
{ class: "on-holiday" }
)}</span>`;
}
});
}
});
withPluginApi("0.8", (api) => {
const usernames = api.container.lookup("service:site").users_on_holiday;
if (usernames?.length > 0) {
let flairHandler;
api.cleanupStream(() => cancel(flairHandler));
if (api.decorateChatMessage) {
api.decorateChatMessage((message) => {
usernames.forEach((username) =>
applyFlairOnMention(message, username)
);
});
}
api.decorateCookedElement(
(element, helper) => {
if (helper) {
// decorating a post
usernames.forEach((username) =>
applyFlairOnMention(element, username)
);
} else {
// decorating preview
cancel(flairHandler);
flairHandler = discourseLater(
() =>
usernames.forEach((username) =>
applyFlairOnMention(element, username)
),
1000
);
}
},
{ id: "discourse-calendar-holiday-flair" }
);
api.addPosterIcon((cfs) => {
if (cfs.on_holiday) {
return {
emoji: "desert_island",
className: "holiday",
title: I18n.t("discourse_calendar.on_holiday"),
};
}
});
}
});
},
};