-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathactivitypub.js
More file actions
123 lines (106 loc) · 3.1 KB
/
Copy pathactivitypub.js
File metadata and controls
123 lines (106 loc) · 3.1 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
'use strict';
const nconf = nodebb.require('nconf');
const posts = nodebb.require('./src/posts');
const activitypub = nodebb.require('./src/activitypub');
const helpers = require('./helpers');
const Ap = module.exports;
/**
* Build an EmojiReact (FEP-c0e0) or Like-with-content activity
* by reusing the core mocks.activities.like as a base.
*/
async function buildActivity(pid, uid, reaction, type) {
const base = await activitypub.mocks.activities.like(pid, uid);
const cleanName = reaction.replace(/^:+|:+$/g, '');
if (type === 'EmojiReact') {
const content = `:${cleanName}:`;
const overrides = {
id: base.id.replace('like', 'emojireact'),
type: 'EmojiReact',
content,
};
// For custom emoji (shortcode:hostname), attach Emoji tag
const idx = reaction.indexOf(':');
if (idx !== -1) {
const shortcode = reaction.slice(0, idx);
const hostname = reaction.slice(idx + 1);
const metadata = await activitypub.emoji.getEmoji(shortcode, hostname);
if (metadata) {
overrides.tag = [{
type: 'Emoji',
mediaType: metadata.mediaType || null,
name: `:${shortcode}:`,
icon: {
url: metadata.remoteUrl,
},
}];
}
}
return { ...base, ...overrides };
}
// Like with content
return {
...base,
content: `:${cleanName}:`,
};
}
/**
* Build an Undo activity wrapping the given reaction activity.
*/
async function buildUndoActivity(pid, uid, reaction, innerType) {
const inner = await buildActivity(pid, uid, reaction, innerType);
return {
id: `${nconf.get('url')}/uid/${uid}#activity/undo:emojireact/${encodeURIComponent(pid)}/${Date.now()}`,
type: 'Undo',
actor: `${nconf.get('url')}/uid/${uid}`,
object: {
actor: inner.actor,
id: inner.id,
type: inner.type,
object: inner.object,
...(inner.content ? { content: inner.content } : {}),
...(inner.tag ? { tag: inner.tag } : {}),
},
};
}
/**
* Send a reaction activity to the remote post author and announce via FEPs.
*/
async function send(pid, uid, reaction, innerType) {
const isRemotePost = activitypub.helpers.isUri(pid);
const activity = await buildActivity(pid, uid, reaction, innerType);
if (!isRemotePost) {
// Local post: only announce via FEPs (1b12 relay)
await activitypub.feps.announce(pid, activity);
return;
}
// Remote post: send to author + announce
const author = await posts.getPostField(pid, 'uid');
if (!activitypub.helpers.isUri(author)) {
// Post author is local — nothing to send
await activitypub.feps.announce(pid, activity);
return;
}
await Promise.all([
activitypub.send('uid', uid, [author], activity),
activitypub.feps.announce(pid, activity),
]);
}
/**
* Send an Undo activity for a reaction.
*/
async function sendUndo(pid, uid, reaction, innerType) {
if (!activitypub.helpers.isUri(pid)) {
return;
}
const author = await posts.getPostField(pid, 'uid');
if (!activitypub.helpers.isUri(author)) {
return;
}
const undo = await buildUndoActivity(pid, uid, reaction, innerType);
await Promise.all([
activitypub.send('uid', uid, [author], undo),
activitypub.feps.announce(pid, undo),
]);
}
Ap.send = send;
Ap.sendUndo = sendUndo;