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
|
import { APError, fetchOpts } from "@spacebar/ap";
import {
Channel,
Config,
DmChannelDTO,
Member,
Message,
Snowflake,
User,
UserSettings,
WebfingerResponse,
} from "@spacebar/util";
import { APNote, APPerson, AnyAPObject } from "activitypub-types";
import fetch from "node-fetch";
import { ProxyAgent } from "proxy-agent";
import TurndownService from "turndown";
const hasAPContext = (data: object) => {
if (!("@context" in data)) return false;
const context = data["@context"];
const activitystreams = "https://www.w3.org/ns/activitystreams";
if (Array.isArray(context))
return context.find((x) => x == activitystreams);
return context == activitystreams;
};
export const resolveAPObject = async <T>(data: string | T): Promise<T> => {
// we were already given an AP object
if (typeof data != "string") return data;
const agent = new ProxyAgent();
const ret = await fetch(data, {
...fetchOpts,
agent,
});
const json = await ret.json();
if (!hasAPContext(json)) throw new APError("Object is not APObject");
return json;
};
export const resolveWebfinger = async (
lookup: string,
): Promise<AnyAPObject> => {
let domain: string, user: string;
if (lookup.includes("@")) {
// lookup a @handle
if (lookup[0] == "@") lookup = lookup.slice(1);
[domain, user] = lookup.split("@");
} else {
// lookup was a URL ( hopefully )
const url = new URL(lookup);
domain = url.hostname;
user = url.pathname.split("/").reverse()[0];
}
const agent = new ProxyAgent();
const wellknown = (await fetch(
`https://${domain}/.well-known/webfinger?resource=${lookup}`,
{
agent,
...fetchOpts,
},
).then((x) => x.json())) as WebfingerResponse;
const link = wellknown.links.find((x) => x.rel == "self");
if (!link) throw new APError(".well-known did not contain rel=self link");
return await resolveAPObject<AnyAPObject>(link.href);
};
export const messageFromAP = async (data: APNote): Promise<Message> => {
if (!data.id) throw new APError("Message must have ID");
if (data.type != "Note") throw new APError("Message must be Note");
if (!data.attributedTo)
throw new APError("Message must have author (attributedTo)");
const attrib = await resolveAPObject(
Array.isArray(data.attributedTo)
? data.attributedTo[0] // hmm
: data.attributedTo,
);
if (!APObjectIsPerson(attrib))
throw new APError("Message attributedTo must be Person");
const user = await userFromAP(attrib);
const to = Array.isArray(data.to)
? data.to.filter((x) =>
typeof x == "string"
? x.includes("channel") || x.includes("user")
: false,
)[0]
: data.to;
if (!to || typeof to != "string")
throw new APError("Message not deliverable");
// TODO: use a regex
let channel: Channel | DmChannelDTO;
const to_id = to.split("/").reverse()[0];
if (to.includes("user")) {
// this is a DM channel
const toUser = await User.findOneOrFail({ where: { id: to_id } });
// Channel.createDMCHannel does a .save() so the author must be present
await user.save();
// const cache = await Channel.findOne({ where: { recipients: []}})
channel = await Channel.createDMChannel(
[toUser.id, user.id],
toUser.id,
);
} else {
channel = await Channel.findOneOrFail({
where: { id: to_id },
relations: { guild: true },
});
}
const member =
channel instanceof Channel
? await Member.findOneOrFail({
where: { id: user.id, guild_id: channel.guild!.id },
})
: undefined;
return Message.create({
id: Snowflake.generate(),
federatedId: data.id,
content: new TurndownService().turndown(data.content),
timestamp: data.published,
author: user,
guild: channel instanceof Channel ? channel.guild : undefined,
member,
channel_id: channel.id,
type: 0,
sticker_items: [],
attachments: [],
embeds: [],
reactions: [],
mentions: [],
mention_roles: [],
mention_channels: [],
});
};
export const APObjectIsPerson = (object: AnyAPObject): object is APPerson => {
return object.type == "Person";
};
export const userFromAP = async (data: APPerson): Promise<User> => {
if (!data.id) throw new APError("User must have ID");
const url = new URL(data.id);
const email = `${url.pathname.split("/").reverse()[0]}@${url.hostname}`;
// don't like this
// the caching should probably be done elsewhere
// this function should only be for converting AP to SB (ideally)
const cache = await User.findOne({
where: { federatedId: url.toString() },
});
if (cache) return cache;
return User.create({
federatedId: url.toString(),
username: data.preferredUsername,
discriminator: url.hostname,
bio: new TurndownService().turndown(data.summary),
email,
data: {
hash: "#",
valid_tokens_since: new Date(),
},
extended_settings: "{}",
settings: UserSettings.create(),
publicKey: "",
privateKey: "",
premium: false,
premium_since: Config.get().defaults.user.premium
? new Date()
: undefined,
rights: Config.get().register.defaultRights,
premium_type: Config.get().defaults.user.premiumType ?? 0,
verified: Config.get().defaults.user.verified ?? true,
created_at: new Date(),
});
};
|