-
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathindex.js
More file actions
313 lines (257 loc) · 10.8 KB
/
index.js
File metadata and controls
313 lines (257 loc) · 10.8 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
import { keyToCss } from '../../utils/css_map.js';
import { dom } from '../../utils/dom.js';
import { inject } from '../../utils/inject.js';
import { buildStyle, displayInlineFlexUnlessDisabledAttr, notificationSelector } from '../../utils/interface.js';
import { registerReplyMeatballItem, unregisterReplyMeatballItem } from '../../utils/meatballs.js';
import { showErrorModal } from '../../utils/modals.js';
import { pageModifications } from '../../utils/mutations.js';
import { notify } from '../../utils/notifications.js';
import { getPreferences } from '../../utils/preferences.js';
import { timelineObject } from '../../utils/react_props.js';
import { buildSvg } from '../../utils/remixicon.js';
import { apiFetch, navigate } from '../../utils/tumblr_helpers.js';
import { userBlogNames, userBlogs } from '../../utils/user.js';
const storageKey = 'quote_replies.draftLocation';
const buttonClass = 'xkit-quote-replies';
const meatballButtonId = 'quote-replies';
const dropdownButtonClass = 'xkit-quote-replies-dropdown';
// Remove outdated elements when loading module
$(`.${buttonClass}`).remove();
export const styleElement = buildStyle(`
button.xkit-quote-replies {
position: relative;
align-self: center;
transform: translateY(-2px);
align-items: center;
margin: 0 6px;
cursor: pointer;
}
button.xkit-quote-replies svg {
width: 21.5px;
height: 21.5px;
fill: rgb(var(--blue));
transition: all .25s ease-out .4s;
}
button.xkit-quote-replies:disabled svg {
fill: rgba(var(--black), 0.65);
transition-property: none;
}
button.xkit-quote-replies-dropdown {
align-self: flex-start;
margin: 10px 0 0;
}
@media (hover: hover) {
button.xkit-quote-replies svg {
opacity: 0;
transform: scale(0);
}
${notificationSelector}:is(:hover, :focus-within) button.xkit-quote-replies svg {
opacity: 1;
transform: scale(1);
}
}
`);
const originalPostTagStorageKey = 'quick_tags.preferences.originalPostTag';
const activitySelector = `:is(${keyToCss('notification')} > ${keyToCss('activity')}, ${keyToCss('activityContent')})`;
const dropdownSelector = '[role="tabpanel"] *';
let originalPostTag;
let tagReplyingBlog;
let newTab;
const processNotifications = notifications => notifications.forEach(async notification => {
const [notificationProps, tumblelogName] = await Promise.all([
inject('/main_world/get_notification_props.js', [], notification),
inject('/main_world/get_tumblelogname_prop.js', [], notification),
]);
if (!['reply', 'reply_to_comment', 'note_mention'].includes(notificationProps.type === 'generic' ? notificationProps.subtype : notificationProps.type)) return;
if (notificationProps.community) return;
if (notificationProps.actions?.tap?.href && new URL(notificationProps.actions.tap.href).pathname.startsWith('/communities/')) return;
const activityElement = notification.querySelector(activitySelector);
if (!activityElement) return;
activityElement.after(dom(
'button',
{
class: `${buttonClass} ${notification.matches(dropdownSelector) ? dropdownButtonClass : ''}`,
[displayInlineFlexUnlessDisabledAttr]: '',
title: 'Quote this reply',
},
{
click () {
this.disabled = true;
quoteActivityReply(tumblelogName, notificationProps)
.catch(showErrorModal)
.finally(() => { this.disabled = false; });
},
},
[buildSvg('ri-chat-quote-line')],
));
});
const createGenericActivityReplyPost = async (notificationProps) => {
const {
subtype: type,
timestamp,
title: { textContent: titleContent },
body: { content: [bodyDescriptionContent, bodyQuoteContent] },
actions,
} = notificationProps;
const summaryFormatting = bodyDescriptionContent.formatting?.find(({ type }) => type === 'semantic_color');
try {
const [, targetTumblelogName, targetPostId] =
/^\/@?([a-z0-9-]{1,32})\/([0-9]{1,20})(\/|$)/.exec(new URL(actions.tap.href).pathname);
const targetPostSummary = summaryFormatting
? bodyDescriptionContent.text.slice(summaryFormatting.start + 1, summaryFormatting.end - 1)
: bodyDescriptionContent.text;
return await createActivityReplyPost({ type, timestamp, targetPostId, targetTumblelogName, targetPostSummary });
} catch (exception) {
console.error(exception);
console.debug('[XKit] Falling back to generic quote content due to fetch/parse failure');
}
const replyingBlog = titleContent.formatting.find(({ type }) => type === 'mention').blog;
const content = [
{
type: 'text',
text: `@${replyingBlog.name}`,
formatting: [
{ start: 0, end: replyingBlog.name.length + 1, type: 'mention', blog: { uuid: replyingBlog.uuid } }],
},
{
type: 'text',
text: bodyDescriptionContent.text,
formatting: summaryFormatting
? [{ start: summaryFormatting.start, end: summaryFormatting.end, type: 'link', url: actions.tap.href }]
: [],
},
bodyQuoteContent,
{ type: 'text', text: '\u200B' },
];
const tags = [
...originalPostTag ? [originalPostTag] : [],
...tagReplyingBlog ? [replyingBlog.name] : [],
].join(',');
return { content, tags };
};
const createActivityReplyPost = async ({ type, timestamp, targetPostId, targetTumblelogName, targetPostSummary }) => {
const { response } = await apiFetch(
`/v2/blog/${targetTumblelogName}/post/${targetPostId}/notes/timeline`,
{ queryParams: { mode: 'replies', before_timestamp: `${timestamp + 1}000000` } },
);
const reply = response?.timeline?.elements?.[0];
if (!reply) throw new Error('No replies found on target post.');
if ([Math.floor(reply.timestamp), Math.round(reply.timestamp)].includes(timestamp) === false) {
throw new Error('Reply not found.');
}
const { content: replyContent, blog: { name: replyingBlogName, uuid: replyingBlogUuid } } = reply;
const targetPostUrl = `https://${targetTumblelogName}.tumblr.com/post/${targetPostId}`;
return createReplyPost({ type, replyingBlogName, replyingBlogUuid, targetPostSummary, targetPostUrl, replyContent });
};
const createReplyPost = ({ type, replyingBlogName, replyingBlogUuid, targetPostSummary, targetPostUrl, replyContent }) => {
const verbiage = {
reply: 'replied to your post',
reply_to_comment: 'replied to you in a post',
note_mention: 'mentioned you on a post',
}[type];
const text = `@${replyingBlogName} ${verbiage} \u201C${targetPostSummary.replace(/\n/g, ' ')}\u201D:`;
const formatting = [
{ start: 0, end: replyingBlogName.length + 1, type: 'mention', blog: { uuid: replyingBlogUuid } },
{ start: text.indexOf('\u201C'), end: text.length - 1, type: 'link', url: targetPostUrl },
];
const content = [
{ type: 'text', text, formatting },
Object.assign(replyContent[0], { subtype: 'indented' }),
{ type: 'text', text: '\u200B' },
];
const tags = [
...originalPostTag ? [originalPostTag] : [],
...tagReplyingBlog ? [replyingBlogName] : [],
].join(',');
return { content, tags };
};
const quoteActivityReply = async (tumblelogName, notificationProps) => {
const replyPost = notificationProps.type === 'generic'
? await createGenericActivityReplyPost(notificationProps)
: await createActivityReplyPost(notificationProps);
openQuoteReplyDraft(tumblelogName, replyPost);
};
const determineNoteReplyType = ({ noteProps, parentNoteProps }) => {
if (userBlogNames.includes(noteProps.note.blogName)) return false;
if (noteProps.communityId) return false;
if (parentNoteProps && userBlogNames.includes(parentNoteProps.note.blogName)) {
return {
type: 'reply_to_comment',
targetBlogName: parentNoteProps.note.blogName,
};
}
if (userBlogNames.includes(noteProps.blog.name)) {
return {
type: 'reply',
targetBlogName: noteProps.blog.name,
};
}
for (const { formatting = [] } of noteProps.note.content) {
for (const { type, blog } of formatting) {
if (type === 'mention' && userBlogNames.includes(blog.name)) {
return {
type: 'note_mention',
targetBlogName: blog.name,
};
}
}
}
return false;
};
const quoteNoteReply = async ({ currentTarget }) => {
const {
noteProps: {
note: { blogName: replyingBlogName, content: replyContent },
},
} = currentTarget.__notePropsData;
const { type, targetBlogName } = determineNoteReplyType(currentTarget.__notePropsData);
const { summary: targetPostSummary, postUrl: targetPostUrl } = await timelineObject(currentTarget.closest(keyToCss('meatballMenu')));
const replyingBlogUuid = await apiFetch(`/v2/blog/${replyingBlogName}/info?fields[blogs]=uuid`)
.then(({ response: { blog: { uuid } } }) => uuid);
const replyPost = createReplyPost({ type, replyingBlogName, replyingBlogUuid, targetPostSummary, targetPostUrl, replyContent });
openQuoteReplyDraft(targetBlogName, replyPost);
};
const openQuoteReplyDraft = async (tumblelogName, replyPost) => {
const uuid = userBlogs.find(({ name }) => name === tumblelogName).uuid;
const { response: { id: responseId, displayText } } = await apiFetch(`/v2/blog/${uuid}/posts`, { method: 'POST', body: { state: 'draft', ...replyPost } });
const currentDraftLocation = `/edit/${tumblelogName}/${responseId}`;
if (newTab) {
await browser.storage.local.set({ [storageKey]: currentDraftLocation });
const openedTab = window.open(`/blog/${tumblelogName}/drafts`);
if (openedTab === null) {
browser.storage.local.remove(storageKey);
notify(displayText);
}
} else {
navigate(currentDraftLocation);
}
};
export const main = async function () {
({ [originalPostTagStorageKey]: originalPostTag } = await browser.storage.local.get(originalPostTagStorageKey));
({ tagReplyingBlog, newTab } = await getPreferences('quote_replies'));
pageModifications.register(notificationSelector, processNotifications);
registerReplyMeatballItem({
id: meatballButtonId,
label: 'Quote this reply',
notePropsFilter: notePropsData => Boolean(determineNoteReplyType(notePropsData)),
onclick: event => quoteNoteReply(event).catch(showErrorModal),
});
const { [storageKey]: draftLocation } = await browser.storage.local.get(storageKey);
browser.storage.local.remove(storageKey);
if (newTab && draftLocation !== undefined && /^\/blog\/.+\/drafts/.test(location.pathname)) {
navigate(draftLocation);
}
};
export const clean = async function () {
pageModifications.unregister(processNotifications);
unregisterReplyMeatballItem(meatballButtonId);
$(`.${buttonClass}`).remove();
};
export const onStorageChanged = async function (changes) {
if (Object.keys(changes).includes(originalPostTagStorageKey)) {
({ [originalPostTagStorageKey]: { newValue: originalPostTag } } = changes);
}
if (Object.keys(changes).some(key => key.startsWith('quote_replies.preferences'))) {
({ tagReplyingBlog, newTab } = await getPreferences('quote_replies'));
}
};