]>
Commit | Line | Data |
---|---|---|
50d6de9c | 1 | import * as Bluebird from 'bluebird' |
db4b15f2 | 2 | import { extname } from 'path' |
5147a6d9 | 3 | import { Op, Transaction } from 'sequelize' |
a1587156 | 4 | import { URL } from 'url' |
bdd428a6 | 5 | import { v4 as uuidv4 } from 'uuid' |
db4b15f2 | 6 | import { getServerActor } from '@server/models/application/application' |
2cb03dc1 | 7 | import { ActorImageType } from '@shared/models' |
db4b15f2 | 8 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' |
bdd428a6 | 9 | import { ActivityPubActor, ActivityPubActorType, ActivityPubOrderedCollection } from '../../../shared/models/activitypub' |
50d6de9c | 10 | import { ActivityPubAttributedTo } from '../../../shared/models/activitypub/objects' |
848f499d | 11 | import { checkUrlsSameHost, getAPId } from '../../helpers/activitypub' |
db4b15f2 | 12 | import { ActorFetchByUrlType, fetchActorByUrl } from '../../helpers/actor' |
9977c128 | 13 | import { sanitizeAndCheckActorObject } from '../../helpers/custom-validators/activitypub/actor' |
c5911fd3 | 14 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' |
a5625b41 | 15 | import { retryTransactionWrapper, updateInstanceWithAnother } from '../../helpers/database-utils' |
da854ddd C |
16 | import { logger } from '../../helpers/logger' |
17 | import { createPrivateAndPublicKeys } from '../../helpers/peertube-crypto' | |
b5c36108 | 18 | import { doJSONRequest, PeerTubeRequestError } from '../../helpers/requests' |
a5625b41 | 19 | import { getUrlFromWebfinger } from '../../helpers/webfinger' |
557b13ae | 20 | import { MIMETYPES, WEBSERVER } from '../../initializers/constants' |
db4b15f2 | 21 | import { sequelizeTypescript } from '../../initializers/database' |
50d6de9c | 22 | import { AccountModel } from '../../models/account/account' |
7d9ba5c0 C |
23 | import { ActorModel } from '../../models/actor/actor' |
24 | import { ActorImageModel } from '../../models/actor/actor-image' | |
50d6de9c C |
25 | import { ServerModel } from '../../models/server/server' |
26 | import { VideoChannelModel } from '../../models/video/video-channel' | |
453e83ea C |
27 | import { |
28 | MAccount, | |
0283eaac | 29 | MAccountDefault, |
453e83ea C |
30 | MActor, |
31 | MActorAccountChannelId, | |
0283eaac | 32 | MActorAccountChannelIdActor, |
453e83ea | 33 | MActorAccountId, |
453e83ea | 34 | MActorFull, |
0283eaac | 35 | MActorFullActor, |
453e83ea | 36 | MActorId, |
213e30ef | 37 | MActorImage, |
2cb03dc1 | 38 | MActorImages, |
a1587156 | 39 | MChannel |
26d6bf65 | 40 | } from '../../types/models' |
db4b15f2 | 41 | import { JobQueue } from '../job-queue' |
50d6de9c | 42 | |
e12a0092 | 43 | // Set account keys, this could be long so process after the account creation and do not block the client |
8795d6f2 C |
44 | async function generateAndSaveActorKeys <T extends MActor> (actor: T) { |
45 | const { publicKey, privateKey } = await createPrivateAndPublicKeys() | |
46 | ||
47 | actor.publicKey = publicKey | |
48 | actor.privateKey = privateKey | |
49 | ||
50 | return actor.save() | |
50d6de9c C |
51 | } |
52 | ||
453e83ea C |
53 | function getOrCreateActorAndServerAndModel ( |
54 | activityActor: string | ActivityPubActor, | |
55 | fetchType: 'all', | |
56 | recurseIfNeeded?: boolean, | |
57 | updateCollections?: boolean | |
58 | ): Promise<MActorFullActor> | |
59 | ||
60 | function getOrCreateActorAndServerAndModel ( | |
61 | activityActor: string | ActivityPubActor, | |
62 | fetchType?: 'association-ids', | |
63 | recurseIfNeeded?: boolean, | |
64 | updateCollections?: boolean | |
65 | ): Promise<MActorAccountChannelId> | |
66 | ||
687d638c C |
67 | async function getOrCreateActorAndServerAndModel ( |
68 | activityActor: string | ActivityPubActor, | |
453e83ea | 69 | fetchType: ActorFetchByUrlType = 'association-ids', |
687d638c C |
70 | recurseIfNeeded = true, |
71 | updateCollections = false | |
453e83ea | 72 | ): Promise<MActorFullActor | MActorAccountChannelId> { |
848f499d | 73 | const actorUrl = getAPId(activityActor) |
687d638c | 74 | let created = false |
418d092a | 75 | let accountPlaylistsUrl: string |
6be84cbc | 76 | |
e587e0ec | 77 | let actor = await fetchActorByUrl(actorUrl, fetchType) |
25e4d6ee | 78 | // Orphan actor (not associated to an account of channel) so recreate it |
6104adc3 | 79 | if (actor && (!actor.Account && !actor.VideoChannel)) { |
25e4d6ee C |
80 | await actor.destroy() |
81 | actor = null | |
82 | } | |
50d6de9c C |
83 | |
84 | // We don't have this actor in our database, fetch it on remote | |
85 | if (!actor) { | |
f5b0af50 | 86 | const { result } = await fetchRemoteActor(actorUrl) |
601527d7 | 87 | if (result === undefined) throw new Error('Cannot fetch remote actor ' + actorUrl) |
50d6de9c C |
88 | |
89 | // Create the attributed to actor | |
90 | // In PeerTube a video channel is owned by an account | |
453e83ea | 91 | let ownerActor: MActorFullActor |
50d6de9c C |
92 | if (recurseIfNeeded === true && result.actor.type === 'Group') { |
93 | const accountAttributedTo = result.attributedTo.find(a => a.type === 'Person') | |
94 | if (!accountAttributedTo) throw new Error('Cannot find account attributed to video channel ' + actor.url) | |
95 | ||
5c6d985f C |
96 | if (checkUrlsSameHost(accountAttributedTo.id, actorUrl) !== true) { |
97 | throw new Error(`Account attributed to ${accountAttributedTo.id} does not have the same host than actor url ${actorUrl}`) | |
98 | } | |
99 | ||
50d6de9c | 100 | try { |
5c6d985f | 101 | // Don't recurse another time |
418d092a C |
102 | const recurseIfNeeded = false |
103 | ownerActor = await getOrCreateActorAndServerAndModel(accountAttributedTo.id, 'all', recurseIfNeeded) | |
50d6de9c | 104 | } catch (err) { |
471251fa | 105 | logger.error('Cannot get or create account attributed to video channel ' + actorUrl) |
50d6de9c C |
106 | throw new Error(err) |
107 | } | |
108 | } | |
109 | ||
90d4bb81 | 110 | actor = await retryTransactionWrapper(saveActorAndServerAndModelIfNotExist, result, ownerActor) |
687d638c | 111 | created = true |
418d092a | 112 | accountPlaylistsUrl = result.playlists |
50d6de9c C |
113 | } |
114 | ||
453e83ea C |
115 | if (actor.Account) (actor as MActorAccountChannelIdActor).Account.Actor = actor |
116 | if (actor.VideoChannel) (actor as MActorAccountChannelIdActor).VideoChannel.Actor = actor | |
d9bdd007 | 117 | |
e587e0ec | 118 | const { actor: actorRefreshed, refreshed } = await retryTransactionWrapper(refreshActorIfNeeded, actor, fetchType) |
652c6416 | 119 | if (!actorRefreshed) throw new Error('Actor ' + actor.url + ' does not exist anymore.') |
f5b0af50 | 120 | |
687d638c C |
121 | if ((created === true || refreshed === true) && updateCollections === true) { |
122 | const payload = { uri: actor.outboxUrl, type: 'activity' as 'activity' } | |
a1587156 | 123 | await JobQueue.Instance.createJobWithPromise({ type: 'activitypub-http-fetcher', payload }) |
687d638c C |
124 | } |
125 | ||
418d092a C |
126 | // We created a new account: fetch the playlists |
127 | if (created === true && actor.Account && accountPlaylistsUrl) { | |
128 | const payload = { uri: accountPlaylistsUrl, accountId: actor.Account.id, type: 'account-playlists' as 'account-playlists' } | |
a1587156 | 129 | await JobQueue.Instance.createJobWithPromise({ type: 'activitypub-http-fetcher', payload }) |
418d092a C |
130 | } |
131 | ||
687d638c | 132 | return actorRefreshed |
50d6de9c C |
133 | } |
134 | ||
16c016e8 | 135 | function buildActorInstance (type: ActivityPubActorType, url: string, preferredUsername: string) { |
c5911fd3 C |
136 | return new ActorModel({ |
137 | type, | |
138 | url, | |
139 | preferredUsername, | |
c5911fd3 C |
140 | publicKey: null, |
141 | privateKey: null, | |
142 | followersCount: 0, | |
143 | followingCount: 0, | |
144 | inboxUrl: url + '/inbox', | |
145 | outboxUrl: url + '/outbox', | |
6dd9de95 | 146 | sharedInboxUrl: WEBSERVER.URL + '/inbox', |
c5911fd3 C |
147 | followersUrl: url + '/followers', |
148 | followingUrl: url + '/following' | |
1ca9f7c3 | 149 | }) as MActor |
c5911fd3 C |
150 | } |
151 | ||
a5625b41 C |
152 | async function updateActorInstance (actorInstance: ActorModel, attributes: ActivityPubActor) { |
153 | const followersCount = await fetchActorTotalItems(attributes.followers) | |
154 | const followingCount = await fetchActorTotalItems(attributes.following) | |
155 | ||
57cfff78 C |
156 | actorInstance.type = attributes.type |
157 | actorInstance.preferredUsername = attributes.preferredUsername | |
158 | actorInstance.url = attributes.id | |
159 | actorInstance.publicKey = attributes.publicKey.publicKeyPem | |
160 | actorInstance.followersCount = followersCount | |
161 | actorInstance.followingCount = followingCount | |
162 | actorInstance.inboxUrl = attributes.inbox | |
163 | actorInstance.outboxUrl = attributes.outbox | |
57cfff78 C |
164 | actorInstance.followersUrl = attributes.followers |
165 | actorInstance.followingUrl = attributes.following | |
47581df0 | 166 | |
a66c2e32 C |
167 | if (attributes.published) actorInstance.remoteCreatedAt = new Date(attributes.published) |
168 | ||
faa9d434 | 169 | if (attributes.endpoints?.sharedInbox) { |
47581df0 C |
170 | actorInstance.sharedInboxUrl = attributes.endpoints.sharedInbox |
171 | } | |
a5625b41 C |
172 | } |
173 | ||
84531547 C |
174 | type ImageInfo = { |
175 | name: string | |
176 | fileUrl: string | |
177 | height: number | |
178 | width: number | |
179 | onDisk?: boolean | |
180 | } | |
213e30ef C |
181 | async function updateActorImageInstance (actor: MActorImages, type: ActorImageType, imageInfo: ImageInfo | null, t: Transaction) { |
182 | const oldImageModel = type === ActorImageType.AVATAR | |
2cb03dc1 C |
183 | ? actor.Avatar |
184 | : actor.Banner | |
185 | ||
186 | if (oldImageModel) { | |
1c5fbed2 | 187 | // Don't update the avatar if the file URL did not change |
213e30ef | 188 | if (imageInfo?.fileUrl && oldImageModel.fileUrl === imageInfo.fileUrl) return actor |
a5625b41 | 189 | |
66fb2aa3 | 190 | try { |
2cb03dc1 | 191 | await oldImageModel.destroy({ transaction: t }) |
213e30ef C |
192 | |
193 | setActorImage(actor, type, null) | |
66fb2aa3 | 194 | } catch (err) { |
2cb03dc1 | 195 | logger.error('Cannot remove old actor image of actor %s.', actor.url, { err }) |
66fb2aa3 | 196 | } |
a5625b41 C |
197 | } |
198 | ||
213e30ef C |
199 | if (imageInfo) { |
200 | const imageModel = await ActorImageModel.create({ | |
201 | filename: imageInfo.name, | |
202 | onDisk: imageInfo.onDisk ?? false, | |
203 | fileUrl: imageInfo.fileUrl, | |
84531547 C |
204 | height: imageInfo.height, |
205 | width: imageInfo.width, | |
206 | type | |
213e30ef | 207 | }, { transaction: t }) |
66fb2aa3 | 208 | |
213e30ef | 209 | setActorImage(actor, type, imageModel) |
2cb03dc1 | 210 | } |
66fb2aa3 | 211 | |
557b13ae | 212 | return actor |
a5625b41 C |
213 | } |
214 | ||
2cb03dc1 | 215 | async function deleteActorImageInstance (actor: MActorImages, type: ActorImageType, t: Transaction) { |
1ea7da81 | 216 | try { |
2cb03dc1 C |
217 | if (type === ActorImageType.AVATAR) { |
218 | await actor.Avatar.destroy({ transaction: t }) | |
219 | ||
220 | actor.avatarId = null | |
221 | actor.Avatar = null | |
222 | } else { | |
223 | await actor.Banner.destroy({ transaction: t }) | |
224 | ||
225 | actor.bannerId = null | |
226 | actor.Banner = null | |
227 | } | |
1ea7da81 | 228 | } catch (err) { |
2cb03dc1 | 229 | logger.error('Cannot remove old image of actor %s.', actor.url, { err }) |
1ea7da81 RK |
230 | } |
231 | ||
1ea7da81 RK |
232 | return actor |
233 | } | |
234 | ||
265ba139 | 235 | async function fetchActorTotalItems (url: string) { |
265ba139 | 236 | try { |
db4b15f2 C |
237 | const { body } = await doJSONRequest<ActivityPubOrderedCollection<unknown>>(url, { activityPub: true }) |
238 | ||
239 | return body.totalItems || 0 | |
265ba139 | 240 | } catch (err) { |
d5b7d911 | 241 | logger.warn('Cannot fetch remote actor count %s.', url, { err }) |
7006bc63 | 242 | return 0 |
265ba139 | 243 | } |
265ba139 C |
244 | } |
245 | ||
2cb03dc1 | 246 | function getImageInfoIfExists (actorJSON: ActivityPubActor, type: ActorImageType) { |
c6de3a85 | 247 | const mimetypes = MIMETYPES.IMAGE |
2cb03dc1 C |
248 | const icon = type === ActorImageType.AVATAR |
249 | ? actorJSON.icon | |
250 | : actorJSON.image | |
c6de3a85 C |
251 | |
252 | if (!icon || icon.type !== 'Image' || !isActivityPubUrlValid(icon.url)) return undefined | |
265ba139 | 253 | |
5224f713 C |
254 | let extension: string |
255 | ||
256 | if (icon.mediaType) { | |
257 | extension = mimetypes.MIMETYPE_EXT[icon.mediaType] | |
258 | } else { | |
259 | const tmp = extname(icon.url) | |
260 | ||
261 | if (mimetypes.EXT_MIMETYPE[tmp] !== undefined) extension = tmp | |
262 | } | |
c6de3a85 C |
263 | |
264 | if (!extension) return undefined | |
265 | ||
266 | return { | |
267 | name: uuidv4() + extension, | |
2cb03dc1 | 268 | fileUrl: icon.url, |
84531547 C |
269 | height: icon.height, |
270 | width: icon.width, | |
2cb03dc1 | 271 | type |
c6de3a85 | 272 | } |
265ba139 C |
273 | } |
274 | ||
5224c394 | 275 | async function addFetchOutboxJob (actor: Pick<ActorModel, 'id' | 'outboxUrl'>) { |
16f29007 C |
276 | // Don't fetch ourselves |
277 | const serverActor = await getServerActor() | |
278 | if (serverActor.id === actor.id) { | |
279 | logger.error('Cannot fetch our own outbox!') | |
280 | return undefined | |
281 | } | |
282 | ||
283 | const payload = { | |
f6eebcb3 C |
284 | uri: actor.outboxUrl, |
285 | type: 'activity' as 'activity' | |
16f29007 C |
286 | } |
287 | ||
288 | return JobQueue.Instance.createJob({ type: 'activitypub-http-fetcher', payload }) | |
289 | } | |
290 | ||
453e83ea C |
291 | async function refreshActorIfNeeded <T extends MActorFull | MActorAccountChannelId> ( |
292 | actorArg: T, | |
744d0eca | 293 | fetchedType: ActorFetchByUrlType |
453e83ea | 294 | ): Promise<{ actor: T | MActorFull, refreshed: boolean }> { |
744d0eca C |
295 | if (!actorArg.isOutdated()) return { actor: actorArg, refreshed: false } |
296 | ||
297 | // We need more attributes | |
453e83ea C |
298 | const actor = fetchedType === 'all' |
299 | ? actorArg as MActorFull | |
300 | : await ActorModel.loadByUrlAndPopulateAccountAndChannel(actorArg.url) | |
744d0eca C |
301 | |
302 | try { | |
699b059e C |
303 | let actorUrl: string |
304 | try { | |
305 | actorUrl = await getUrlFromWebfinger(actor.preferredUsername + '@' + actor.getHost()) | |
306 | } catch (err) { | |
307 | logger.warn('Cannot get actor URL from webfinger, keeping the old one.', err) | |
308 | actorUrl = actor.url | |
309 | } | |
310 | ||
b5c36108 | 311 | const { result } = await fetchRemoteActor(actorUrl) |
744d0eca C |
312 | |
313 | if (result === undefined) { | |
314 | logger.warn('Cannot fetch remote actor in refresh actor.') | |
315 | return { actor, refreshed: false } | |
316 | } | |
317 | ||
318 | return sequelizeTypescript.transaction(async t => { | |
319 | updateInstanceWithAnother(actor, result.actor) | |
320 | ||
213e30ef C |
321 | await updateActorImageInstance(actor, ActorImageType.AVATAR, result.avatar, t) |
322 | await updateActorImageInstance(actor, ActorImageType.BANNER, result.banner, t) | |
744d0eca C |
323 | |
324 | // Force update | |
325 | actor.setDataValue('updatedAt', new Date()) | |
326 | await actor.save({ transaction: t }) | |
327 | ||
328 | if (actor.Account) { | |
6b9c966f C |
329 | actor.Account.name = result.name |
330 | actor.Account.description = result.summary | |
744d0eca C |
331 | |
332 | await actor.Account.save({ transaction: t }) | |
333 | } else if (actor.VideoChannel) { | |
6b9c966f C |
334 | actor.VideoChannel.name = result.name |
335 | actor.VideoChannel.description = result.summary | |
336 | actor.VideoChannel.support = result.support | |
744d0eca C |
337 | |
338 | await actor.VideoChannel.save({ transaction: t }) | |
339 | } | |
340 | ||
341 | return { refreshed: true, actor } | |
342 | }) | |
343 | } catch (err) { | |
b5c36108 C |
344 | if ((err as PeerTubeRequestError).statusCode === HttpStatusCode.NOT_FOUND_404) { |
345 | logger.info('Deleting actor %s because there is a 404 in refresh actor.', actor.url) | |
346 | actor.Account | |
347 | ? await actor.Account.destroy() | |
348 | : await actor.VideoChannel.destroy() | |
349 | ||
350 | return { actor: undefined, refreshed: false } | |
351 | } | |
352 | ||
4ee7a4c9 | 353 | logger.warn('Cannot refresh actor %s.', actor.url, { err }) |
744d0eca C |
354 | return { actor, refreshed: false } |
355 | } | |
356 | } | |
357 | ||
c5911fd3 C |
358 | export { |
359 | getOrCreateActorAndServerAndModel, | |
360 | buildActorInstance, | |
8795d6f2 | 361 | generateAndSaveActorKeys, |
265ba139 | 362 | fetchActorTotalItems, |
2cb03dc1 | 363 | getImageInfoIfExists, |
a5625b41 | 364 | updateActorInstance, |
2cb03dc1 | 365 | deleteActorImageInstance, |
744d0eca | 366 | refreshActorIfNeeded, |
2cb03dc1 | 367 | updateActorImageInstance, |
16f29007 | 368 | addFetchOutboxJob |
c5911fd3 C |
369 | } |
370 | ||
371 | // --------------------------------------------------------------------------- | |
372 | ||
213e30ef C |
373 | function setActorImage (actorModel: MActorImages, type: ActorImageType, imageModel: MActorImage) { |
374 | const id = imageModel | |
375 | ? imageModel.id | |
376 | : null | |
377 | ||
378 | if (type === ActorImageType.AVATAR) { | |
379 | actorModel.avatarId = id | |
380 | actorModel.Avatar = imageModel | |
381 | } else { | |
382 | actorModel.bannerId = id | |
383 | actorModel.Banner = imageModel | |
384 | } | |
385 | ||
386 | return actorModel | |
387 | } | |
388 | ||
50d6de9c C |
389 | function saveActorAndServerAndModelIfNotExist ( |
390 | result: FetchRemoteActorResult, | |
453e83ea | 391 | ownerActor?: MActorFullActor, |
50d6de9c | 392 | t?: Transaction |
453e83ea | 393 | ): Bluebird<MActorFullActor> | Promise<MActorFullActor> { |
a1587156 | 394 | const actor = result.actor |
50d6de9c C |
395 | |
396 | if (t !== undefined) return save(t) | |
397 | ||
398 | return sequelizeTypescript.transaction(t => save(t)) | |
399 | ||
400 | async function save (t: Transaction) { | |
a1587156 | 401 | const actorHost = new URL(actor.url).host |
50d6de9c C |
402 | |
403 | const serverOptions = { | |
404 | where: { | |
405 | host: actorHost | |
406 | }, | |
407 | defaults: { | |
408 | host: actorHost | |
409 | }, | |
410 | transaction: t | |
411 | } | |
412 | const [ server ] = await ServerModel.findOrCreate(serverOptions) | |
413 | ||
414 | // Save our new account in database | |
557b13ae | 415 | actor.serverId = server.id |
50d6de9c | 416 | |
c5911fd3 | 417 | // Avatar? |
557b13ae | 418 | if (result.avatar) { |
f4796856 | 419 | const avatar = await ActorImageModel.create({ |
557b13ae C |
420 | filename: result.avatar.name, |
421 | fileUrl: result.avatar.fileUrl, | |
84531547 C |
422 | width: result.avatar.width, |
423 | height: result.avatar.height, | |
2cb03dc1 C |
424 | onDisk: false, |
425 | type: ActorImageType.AVATAR | |
c5911fd3 | 426 | }, { transaction: t }) |
557b13ae C |
427 | |
428 | actor.avatarId = avatar.id | |
c5911fd3 C |
429 | } |
430 | ||
2cb03dc1 C |
431 | // Banner? |
432 | if (result.banner) { | |
433 | const banner = await ActorImageModel.create({ | |
434 | filename: result.banner.name, | |
435 | fileUrl: result.banner.fileUrl, | |
84531547 C |
436 | width: result.banner.width, |
437 | height: result.banner.height, | |
2cb03dc1 C |
438 | onDisk: false, |
439 | type: ActorImageType.BANNER | |
440 | }, { transaction: t }) | |
441 | ||
442 | actor.bannerId = banner.id | |
443 | } | |
444 | ||
50d6de9c C |
445 | // Force the actor creation, sometimes Sequelize skips the save() when it thinks the instance already exists |
446 | // (which could be false in a retried query) | |
5147a6d9 | 447 | const [ actorCreated, created ] = await ActorModel.findOrCreate<MActorFullActor>({ |
2c897999 C |
448 | defaults: actor.toJSON(), |
449 | where: { | |
5147a6d9 C |
450 | [Op.or]: [ |
451 | { | |
452 | url: actor.url | |
453 | }, | |
454 | { | |
455 | serverId: actor.serverId, | |
456 | preferredUsername: actor.preferredUsername | |
457 | } | |
458 | ] | |
2c897999 C |
459 | }, |
460 | transaction: t | |
461 | }) | |
50d6de9c | 462 | |
5147a6d9 C |
463 | // Try to fix non HTTPS accounts of remote instances that fixed their URL afterwards |
464 | if (created !== true && actorCreated.url !== actor.url) { | |
465 | // Only fix http://example.com/account/djidane to https://example.com/account/djidane | |
e26dc0cd | 466 | if (actorCreated.url.replace(/^http:\/\//, '') !== actor.url.replace(/^https:\/\//, '')) { |
5147a6d9 C |
467 | throw new Error(`Actor from DB with URL ${actorCreated.url} does not correspond to actor ${actor.url}`) |
468 | } | |
469 | ||
470 | actorCreated.url = actor.url | |
471 | await actorCreated.save({ transaction: t }) | |
472 | } | |
473 | ||
50d6de9c | 474 | if (actorCreated.type === 'Person' || actorCreated.type === 'Application') { |
0283eaac | 475 | actorCreated.Account = await saveAccount(actorCreated, result, t) as MAccountDefault |
50d6de9c C |
476 | actorCreated.Account.Actor = actorCreated |
477 | } else if (actorCreated.type === 'Group') { // Video channel | |
0283eaac C |
478 | const channel = await saveVideoChannel(actorCreated, result, ownerActor, t) |
479 | actorCreated.VideoChannel = Object.assign(channel, { Actor: actorCreated, Account: ownerActor.Account }) | |
50d6de9c C |
480 | } |
481 | ||
883993c8 C |
482 | actorCreated.Server = server |
483 | ||
50d6de9c C |
484 | return actorCreated |
485 | } | |
486 | } | |
487 | ||
84531547 C |
488 | type ImageResult = { |
489 | name: string | |
490 | fileUrl: string | |
491 | height: number | |
492 | width: number | |
493 | } | |
494 | ||
50d6de9c | 495 | type FetchRemoteActorResult = { |
453e83ea | 496 | actor: MActor |
e12a0092 | 497 | name: string |
50d6de9c | 498 | summary: string |
2422c46b | 499 | support?: string |
418d092a | 500 | playlists?: string |
84531547 C |
501 | avatar?: ImageResult |
502 | banner?: ImageResult | |
50d6de9c C |
503 | attributedTo: ActivityPubAttributedTo[] |
504 | } | |
f5b0af50 | 505 | async function fetchRemoteActor (actorUrl: string): Promise<{ statusCode?: number, result: FetchRemoteActorResult }> { |
50d6de9c C |
506 | logger.info('Fetching remote actor %s.', actorUrl) |
507 | ||
db4b15f2 | 508 | const requestResult = await doJSONRequest<ActivityPubActor>(actorUrl, { activityPub: true }) |
4c280004 | 509 | const actorJSON = requestResult.body |
9977c128 C |
510 | |
511 | if (sanitizeAndCheckActorObject(actorJSON) === false) { | |
b4593cd7 | 512 | logger.debug('Remote actor JSON is not valid.', { actorJSON }) |
db4b15f2 | 513 | return { result: undefined, statusCode: requestResult.statusCode } |
50d6de9c C |
514 | } |
515 | ||
5c6d985f | 516 | if (checkUrlsSameHost(actorJSON.id, actorUrl) !== true) { |
9f79ade6 | 517 | logger.warn('Actor url %s has not the same host than its AP id %s', actorUrl, actorJSON.id) |
db4b15f2 | 518 | return { result: undefined, statusCode: requestResult.statusCode } |
5c6d985f C |
519 | } |
520 | ||
50d6de9c C |
521 | const followersCount = await fetchActorTotalItems(actorJSON.followers) |
522 | const followingCount = await fetchActorTotalItems(actorJSON.following) | |
523 | ||
524 | const actor = new ActorModel({ | |
525 | type: actorJSON.type, | |
e12a0092 C |
526 | preferredUsername: actorJSON.preferredUsername, |
527 | url: actorJSON.id, | |
50d6de9c C |
528 | publicKey: actorJSON.publicKey.publicKeyPem, |
529 | privateKey: null, | |
530 | followersCount: followersCount, | |
531 | followingCount: followingCount, | |
532 | inboxUrl: actorJSON.inbox, | |
533 | outboxUrl: actorJSON.outbox, | |
50d6de9c | 534 | followersUrl: actorJSON.followers, |
47581df0 C |
535 | followingUrl: actorJSON.following, |
536 | ||
faa9d434 | 537 | sharedInboxUrl: actorJSON.endpoints?.sharedInbox |
47581df0 | 538 | ? actorJSON.endpoints.sharedInbox |
a82ddfad | 539 | : null |
50d6de9c C |
540 | }) |
541 | ||
2cb03dc1 C |
542 | const avatarInfo = getImageInfoIfExists(actorJSON, ActorImageType.AVATAR) |
543 | const bannerInfo = getImageInfoIfExists(actorJSON, ActorImageType.BANNER) | |
c5911fd3 | 544 | |
e12a0092 | 545 | const name = actorJSON.name || actorJSON.preferredUsername |
50d6de9c | 546 | return { |
db4b15f2 | 547 | statusCode: requestResult.statusCode, |
f5b0af50 C |
548 | result: { |
549 | actor, | |
550 | name, | |
557b13ae | 551 | avatar: avatarInfo, |
2cb03dc1 | 552 | banner: bannerInfo, |
f5b0af50 C |
553 | summary: actorJSON.summary, |
554 | support: actorJSON.support, | |
418d092a | 555 | playlists: actorJSON.playlists, |
f5b0af50 C |
556 | attributedTo: actorJSON.attributedTo |
557 | } | |
50d6de9c C |
558 | } |
559 | } | |
560 | ||
453e83ea | 561 | async function saveAccount (actor: MActorId, result: FetchRemoteActorResult, t: Transaction) { |
2c897999 C |
562 | const [ accountCreated ] = await AccountModel.findOrCreate({ |
563 | defaults: { | |
564 | name: result.name, | |
2422c46b | 565 | description: result.summary, |
2c897999 C |
566 | actorId: actor.id |
567 | }, | |
568 | where: { | |
569 | actorId: actor.id | |
570 | }, | |
571 | transaction: t | |
50d6de9c C |
572 | }) |
573 | ||
453e83ea | 574 | return accountCreated as MAccount |
50d6de9c C |
575 | } |
576 | ||
453e83ea | 577 | async function saveVideoChannel (actor: MActorId, result: FetchRemoteActorResult, ownerActor: MActorAccountId, t: Transaction) { |
2c897999 C |
578 | const [ videoChannelCreated ] = await VideoChannelModel.findOrCreate({ |
579 | defaults: { | |
580 | name: result.name, | |
581 | description: result.summary, | |
2422c46b | 582 | support: result.support, |
2c897999 C |
583 | actorId: actor.id, |
584 | accountId: ownerActor.Account.id | |
585 | }, | |
586 | where: { | |
587 | actorId: actor.id | |
588 | }, | |
589 | transaction: t | |
50d6de9c C |
590 | }) |
591 | ||
453e83ea | 592 | return videoChannelCreated as MChannel |
50d6de9c | 593 | } |