]>
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' |
f4796856 | 23 | import { ActorImageModel } from '../../models/account/actor-image' |
50d6de9c C |
24 | import { ActorModel } from '../../models/activitypub/actor' |
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 | ||
c5911fd3 C |
135 | function buildActorInstance (type: ActivityPubActorType, url: string, preferredUsername: string, uuid?: string) { |
136 | return new ActorModel({ | |
137 | type, | |
138 | url, | |
139 | preferredUsername, | |
140 | uuid, | |
141 | publicKey: null, | |
142 | privateKey: null, | |
143 | followersCount: 0, | |
144 | followingCount: 0, | |
145 | inboxUrl: url + '/inbox', | |
146 | outboxUrl: url + '/outbox', | |
6dd9de95 | 147 | sharedInboxUrl: WEBSERVER.URL + '/inbox', |
c5911fd3 C |
148 | followersUrl: url + '/followers', |
149 | followingUrl: url + '/following' | |
1ca9f7c3 | 150 | }) as MActor |
c5911fd3 C |
151 | } |
152 | ||
a5625b41 C |
153 | async function updateActorInstance (actorInstance: ActorModel, attributes: ActivityPubActor) { |
154 | const followersCount = await fetchActorTotalItems(attributes.followers) | |
155 | const followingCount = await fetchActorTotalItems(attributes.following) | |
156 | ||
57cfff78 C |
157 | actorInstance.type = attributes.type |
158 | actorInstance.preferredUsername = attributes.preferredUsername | |
159 | actorInstance.url = attributes.id | |
160 | actorInstance.publicKey = attributes.publicKey.publicKeyPem | |
161 | actorInstance.followersCount = followersCount | |
162 | actorInstance.followingCount = followingCount | |
163 | actorInstance.inboxUrl = attributes.inbox | |
164 | actorInstance.outboxUrl = attributes.outbox | |
57cfff78 C |
165 | actorInstance.followersUrl = attributes.followers |
166 | actorInstance.followingUrl = attributes.following | |
47581df0 | 167 | |
a66c2e32 C |
168 | if (attributes.published) actorInstance.remoteCreatedAt = new Date(attributes.published) |
169 | ||
faa9d434 | 170 | if (attributes.endpoints?.sharedInbox) { |
47581df0 C |
171 | actorInstance.sharedInboxUrl = attributes.endpoints.sharedInbox |
172 | } | |
a5625b41 C |
173 | } |
174 | ||
84531547 C |
175 | type ImageInfo = { |
176 | name: string | |
177 | fileUrl: string | |
178 | height: number | |
179 | width: number | |
180 | onDisk?: boolean | |
181 | } | |
213e30ef C |
182 | async function updateActorImageInstance (actor: MActorImages, type: ActorImageType, imageInfo: ImageInfo | null, t: Transaction) { |
183 | const oldImageModel = type === ActorImageType.AVATAR | |
2cb03dc1 C |
184 | ? actor.Avatar |
185 | : actor.Banner | |
186 | ||
187 | if (oldImageModel) { | |
1c5fbed2 | 188 | // Don't update the avatar if the file URL did not change |
213e30ef | 189 | if (imageInfo?.fileUrl && oldImageModel.fileUrl === imageInfo.fileUrl) return actor |
a5625b41 | 190 | |
66fb2aa3 | 191 | try { |
2cb03dc1 | 192 | await oldImageModel.destroy({ transaction: t }) |
213e30ef C |
193 | |
194 | setActorImage(actor, type, null) | |
66fb2aa3 | 195 | } catch (err) { |
2cb03dc1 | 196 | logger.error('Cannot remove old actor image of actor %s.', actor.url, { err }) |
66fb2aa3 | 197 | } |
a5625b41 C |
198 | } |
199 | ||
213e30ef C |
200 | if (imageInfo) { |
201 | const imageModel = await ActorImageModel.create({ | |
202 | filename: imageInfo.name, | |
203 | onDisk: imageInfo.onDisk ?? false, | |
204 | fileUrl: imageInfo.fileUrl, | |
84531547 C |
205 | height: imageInfo.height, |
206 | width: imageInfo.width, | |
207 | type | |
213e30ef | 208 | }, { transaction: t }) |
66fb2aa3 | 209 | |
213e30ef | 210 | setActorImage(actor, type, imageModel) |
2cb03dc1 | 211 | } |
66fb2aa3 | 212 | |
557b13ae | 213 | return actor |
a5625b41 C |
214 | } |
215 | ||
2cb03dc1 | 216 | async function deleteActorImageInstance (actor: MActorImages, type: ActorImageType, t: Transaction) { |
1ea7da81 | 217 | try { |
2cb03dc1 C |
218 | if (type === ActorImageType.AVATAR) { |
219 | await actor.Avatar.destroy({ transaction: t }) | |
220 | ||
221 | actor.avatarId = null | |
222 | actor.Avatar = null | |
223 | } else { | |
224 | await actor.Banner.destroy({ transaction: t }) | |
225 | ||
226 | actor.bannerId = null | |
227 | actor.Banner = null | |
228 | } | |
1ea7da81 | 229 | } catch (err) { |
2cb03dc1 | 230 | logger.error('Cannot remove old image of actor %s.', actor.url, { err }) |
1ea7da81 RK |
231 | } |
232 | ||
1ea7da81 RK |
233 | return actor |
234 | } | |
235 | ||
265ba139 | 236 | async function fetchActorTotalItems (url: string) { |
265ba139 | 237 | try { |
db4b15f2 C |
238 | const { body } = await doJSONRequest<ActivityPubOrderedCollection<unknown>>(url, { activityPub: true }) |
239 | ||
240 | return body.totalItems || 0 | |
265ba139 | 241 | } catch (err) { |
d5b7d911 | 242 | logger.warn('Cannot fetch remote actor count %s.', url, { err }) |
7006bc63 | 243 | return 0 |
265ba139 | 244 | } |
265ba139 C |
245 | } |
246 | ||
2cb03dc1 | 247 | function getImageInfoIfExists (actorJSON: ActivityPubActor, type: ActorImageType) { |
c6de3a85 | 248 | const mimetypes = MIMETYPES.IMAGE |
2cb03dc1 C |
249 | const icon = type === ActorImageType.AVATAR |
250 | ? actorJSON.icon | |
251 | : actorJSON.image | |
c6de3a85 C |
252 | |
253 | if (!icon || icon.type !== 'Image' || !isActivityPubUrlValid(icon.url)) return undefined | |
265ba139 | 254 | |
5224f713 C |
255 | let extension: string |
256 | ||
257 | if (icon.mediaType) { | |
258 | extension = mimetypes.MIMETYPE_EXT[icon.mediaType] | |
259 | } else { | |
260 | const tmp = extname(icon.url) | |
261 | ||
262 | if (mimetypes.EXT_MIMETYPE[tmp] !== undefined) extension = tmp | |
263 | } | |
c6de3a85 C |
264 | |
265 | if (!extension) return undefined | |
266 | ||
267 | return { | |
268 | name: uuidv4() + extension, | |
2cb03dc1 | 269 | fileUrl: icon.url, |
84531547 C |
270 | height: icon.height, |
271 | width: icon.width, | |
2cb03dc1 | 272 | type |
c6de3a85 | 273 | } |
265ba139 C |
274 | } |
275 | ||
5224c394 | 276 | async function addFetchOutboxJob (actor: Pick<ActorModel, 'id' | 'outboxUrl'>) { |
16f29007 C |
277 | // Don't fetch ourselves |
278 | const serverActor = await getServerActor() | |
279 | if (serverActor.id === actor.id) { | |
280 | logger.error('Cannot fetch our own outbox!') | |
281 | return undefined | |
282 | } | |
283 | ||
284 | const payload = { | |
f6eebcb3 C |
285 | uri: actor.outboxUrl, |
286 | type: 'activity' as 'activity' | |
16f29007 C |
287 | } |
288 | ||
289 | return JobQueue.Instance.createJob({ type: 'activitypub-http-fetcher', payload }) | |
290 | } | |
291 | ||
453e83ea C |
292 | async function refreshActorIfNeeded <T extends MActorFull | MActorAccountChannelId> ( |
293 | actorArg: T, | |
744d0eca | 294 | fetchedType: ActorFetchByUrlType |
453e83ea | 295 | ): Promise<{ actor: T | MActorFull, refreshed: boolean }> { |
744d0eca C |
296 | if (!actorArg.isOutdated()) return { actor: actorArg, refreshed: false } |
297 | ||
298 | // We need more attributes | |
453e83ea C |
299 | const actor = fetchedType === 'all' |
300 | ? actorArg as MActorFull | |
301 | : await ActorModel.loadByUrlAndPopulateAccountAndChannel(actorArg.url) | |
744d0eca C |
302 | |
303 | try { | |
699b059e C |
304 | let actorUrl: string |
305 | try { | |
306 | actorUrl = await getUrlFromWebfinger(actor.preferredUsername + '@' + actor.getHost()) | |
307 | } catch (err) { | |
308 | logger.warn('Cannot get actor URL from webfinger, keeping the old one.', err) | |
309 | actorUrl = actor.url | |
310 | } | |
311 | ||
b5c36108 | 312 | const { result } = await fetchRemoteActor(actorUrl) |
744d0eca C |
313 | |
314 | if (result === undefined) { | |
315 | logger.warn('Cannot fetch remote actor in refresh actor.') | |
316 | return { actor, refreshed: false } | |
317 | } | |
318 | ||
319 | return sequelizeTypescript.transaction(async t => { | |
320 | updateInstanceWithAnother(actor, result.actor) | |
321 | ||
213e30ef C |
322 | await updateActorImageInstance(actor, ActorImageType.AVATAR, result.avatar, t) |
323 | await updateActorImageInstance(actor, ActorImageType.BANNER, result.banner, t) | |
744d0eca C |
324 | |
325 | // Force update | |
326 | actor.setDataValue('updatedAt', new Date()) | |
327 | await actor.save({ transaction: t }) | |
328 | ||
329 | if (actor.Account) { | |
6b9c966f C |
330 | actor.Account.name = result.name |
331 | actor.Account.description = result.summary | |
744d0eca C |
332 | |
333 | await actor.Account.save({ transaction: t }) | |
334 | } else if (actor.VideoChannel) { | |
6b9c966f C |
335 | actor.VideoChannel.name = result.name |
336 | actor.VideoChannel.description = result.summary | |
337 | actor.VideoChannel.support = result.support | |
744d0eca C |
338 | |
339 | await actor.VideoChannel.save({ transaction: t }) | |
340 | } | |
341 | ||
342 | return { refreshed: true, actor } | |
343 | }) | |
344 | } catch (err) { | |
b5c36108 C |
345 | if ((err as PeerTubeRequestError).statusCode === HttpStatusCode.NOT_FOUND_404) { |
346 | logger.info('Deleting actor %s because there is a 404 in refresh actor.', actor.url) | |
347 | actor.Account | |
348 | ? await actor.Account.destroy() | |
349 | : await actor.VideoChannel.destroy() | |
350 | ||
351 | return { actor: undefined, refreshed: false } | |
352 | } | |
353 | ||
4ee7a4c9 | 354 | logger.warn('Cannot refresh actor %s.', actor.url, { err }) |
744d0eca C |
355 | return { actor, refreshed: false } |
356 | } | |
357 | } | |
358 | ||
c5911fd3 C |
359 | export { |
360 | getOrCreateActorAndServerAndModel, | |
361 | buildActorInstance, | |
8795d6f2 | 362 | generateAndSaveActorKeys, |
265ba139 | 363 | fetchActorTotalItems, |
2cb03dc1 | 364 | getImageInfoIfExists, |
a5625b41 | 365 | updateActorInstance, |
2cb03dc1 | 366 | deleteActorImageInstance, |
744d0eca | 367 | refreshActorIfNeeded, |
2cb03dc1 | 368 | updateActorImageInstance, |
16f29007 | 369 | addFetchOutboxJob |
c5911fd3 C |
370 | } |
371 | ||
372 | // --------------------------------------------------------------------------- | |
373 | ||
213e30ef C |
374 | function setActorImage (actorModel: MActorImages, type: ActorImageType, imageModel: MActorImage) { |
375 | const id = imageModel | |
376 | ? imageModel.id | |
377 | : null | |
378 | ||
379 | if (type === ActorImageType.AVATAR) { | |
380 | actorModel.avatarId = id | |
381 | actorModel.Avatar = imageModel | |
382 | } else { | |
383 | actorModel.bannerId = id | |
384 | actorModel.Banner = imageModel | |
385 | } | |
386 | ||
387 | return actorModel | |
388 | } | |
389 | ||
50d6de9c C |
390 | function saveActorAndServerAndModelIfNotExist ( |
391 | result: FetchRemoteActorResult, | |
453e83ea | 392 | ownerActor?: MActorFullActor, |
50d6de9c | 393 | t?: Transaction |
453e83ea | 394 | ): Bluebird<MActorFullActor> | Promise<MActorFullActor> { |
a1587156 | 395 | const actor = result.actor |
50d6de9c C |
396 | |
397 | if (t !== undefined) return save(t) | |
398 | ||
399 | return sequelizeTypescript.transaction(t => save(t)) | |
400 | ||
401 | async function save (t: Transaction) { | |
a1587156 | 402 | const actorHost = new URL(actor.url).host |
50d6de9c C |
403 | |
404 | const serverOptions = { | |
405 | where: { | |
406 | host: actorHost | |
407 | }, | |
408 | defaults: { | |
409 | host: actorHost | |
410 | }, | |
411 | transaction: t | |
412 | } | |
413 | const [ server ] = await ServerModel.findOrCreate(serverOptions) | |
414 | ||
415 | // Save our new account in database | |
557b13ae | 416 | actor.serverId = server.id |
50d6de9c | 417 | |
c5911fd3 | 418 | // Avatar? |
557b13ae | 419 | if (result.avatar) { |
f4796856 | 420 | const avatar = await ActorImageModel.create({ |
557b13ae C |
421 | filename: result.avatar.name, |
422 | fileUrl: result.avatar.fileUrl, | |
84531547 C |
423 | width: result.avatar.width, |
424 | height: result.avatar.height, | |
2cb03dc1 C |
425 | onDisk: false, |
426 | type: ActorImageType.AVATAR | |
c5911fd3 | 427 | }, { transaction: t }) |
557b13ae C |
428 | |
429 | actor.avatarId = avatar.id | |
c5911fd3 C |
430 | } |
431 | ||
2cb03dc1 C |
432 | // Banner? |
433 | if (result.banner) { | |
434 | const banner = await ActorImageModel.create({ | |
435 | filename: result.banner.name, | |
436 | fileUrl: result.banner.fileUrl, | |
84531547 C |
437 | width: result.banner.width, |
438 | height: result.banner.height, | |
2cb03dc1 C |
439 | onDisk: false, |
440 | type: ActorImageType.BANNER | |
441 | }, { transaction: t }) | |
442 | ||
443 | actor.bannerId = banner.id | |
444 | } | |
445 | ||
50d6de9c C |
446 | // Force the actor creation, sometimes Sequelize skips the save() when it thinks the instance already exists |
447 | // (which could be false in a retried query) | |
5147a6d9 | 448 | const [ actorCreated, created ] = await ActorModel.findOrCreate<MActorFullActor>({ |
2c897999 C |
449 | defaults: actor.toJSON(), |
450 | where: { | |
5147a6d9 C |
451 | [Op.or]: [ |
452 | { | |
453 | url: actor.url | |
454 | }, | |
455 | { | |
456 | serverId: actor.serverId, | |
457 | preferredUsername: actor.preferredUsername | |
458 | } | |
459 | ] | |
2c897999 C |
460 | }, |
461 | transaction: t | |
462 | }) | |
50d6de9c | 463 | |
5147a6d9 C |
464 | // Try to fix non HTTPS accounts of remote instances that fixed their URL afterwards |
465 | if (created !== true && actorCreated.url !== actor.url) { | |
466 | // Only fix http://example.com/account/djidane to https://example.com/account/djidane | |
e26dc0cd | 467 | if (actorCreated.url.replace(/^http:\/\//, '') !== actor.url.replace(/^https:\/\//, '')) { |
5147a6d9 C |
468 | throw new Error(`Actor from DB with URL ${actorCreated.url} does not correspond to actor ${actor.url}`) |
469 | } | |
470 | ||
471 | actorCreated.url = actor.url | |
472 | await actorCreated.save({ transaction: t }) | |
473 | } | |
474 | ||
50d6de9c | 475 | if (actorCreated.type === 'Person' || actorCreated.type === 'Application') { |
0283eaac | 476 | actorCreated.Account = await saveAccount(actorCreated, result, t) as MAccountDefault |
50d6de9c C |
477 | actorCreated.Account.Actor = actorCreated |
478 | } else if (actorCreated.type === 'Group') { // Video channel | |
0283eaac C |
479 | const channel = await saveVideoChannel(actorCreated, result, ownerActor, t) |
480 | actorCreated.VideoChannel = Object.assign(channel, { Actor: actorCreated, Account: ownerActor.Account }) | |
50d6de9c C |
481 | } |
482 | ||
883993c8 C |
483 | actorCreated.Server = server |
484 | ||
50d6de9c C |
485 | return actorCreated |
486 | } | |
487 | } | |
488 | ||
84531547 C |
489 | type ImageResult = { |
490 | name: string | |
491 | fileUrl: string | |
492 | height: number | |
493 | width: number | |
494 | } | |
495 | ||
50d6de9c | 496 | type FetchRemoteActorResult = { |
453e83ea | 497 | actor: MActor |
e12a0092 | 498 | name: string |
50d6de9c | 499 | summary: string |
2422c46b | 500 | support?: string |
418d092a | 501 | playlists?: string |
84531547 C |
502 | avatar?: ImageResult |
503 | banner?: ImageResult | |
50d6de9c C |
504 | attributedTo: ActivityPubAttributedTo[] |
505 | } | |
f5b0af50 | 506 | async function fetchRemoteActor (actorUrl: string): Promise<{ statusCode?: number, result: FetchRemoteActorResult }> { |
50d6de9c C |
507 | logger.info('Fetching remote actor %s.', actorUrl) |
508 | ||
db4b15f2 | 509 | const requestResult = await doJSONRequest<ActivityPubActor>(actorUrl, { activityPub: true }) |
4c280004 | 510 | const actorJSON = requestResult.body |
9977c128 C |
511 | |
512 | if (sanitizeAndCheckActorObject(actorJSON) === false) { | |
b4593cd7 | 513 | logger.debug('Remote actor JSON is not valid.', { actorJSON }) |
db4b15f2 | 514 | return { result: undefined, statusCode: requestResult.statusCode } |
50d6de9c C |
515 | } |
516 | ||
5c6d985f | 517 | if (checkUrlsSameHost(actorJSON.id, actorUrl) !== true) { |
9f79ade6 | 518 | logger.warn('Actor url %s has not the same host than its AP id %s', actorUrl, actorJSON.id) |
db4b15f2 | 519 | return { result: undefined, statusCode: requestResult.statusCode } |
5c6d985f C |
520 | } |
521 | ||
50d6de9c C |
522 | const followersCount = await fetchActorTotalItems(actorJSON.followers) |
523 | const followingCount = await fetchActorTotalItems(actorJSON.following) | |
524 | ||
525 | const actor = new ActorModel({ | |
526 | type: actorJSON.type, | |
e12a0092 C |
527 | preferredUsername: actorJSON.preferredUsername, |
528 | url: actorJSON.id, | |
50d6de9c C |
529 | publicKey: actorJSON.publicKey.publicKeyPem, |
530 | privateKey: null, | |
531 | followersCount: followersCount, | |
532 | followingCount: followingCount, | |
533 | inboxUrl: actorJSON.inbox, | |
534 | outboxUrl: actorJSON.outbox, | |
50d6de9c | 535 | followersUrl: actorJSON.followers, |
47581df0 C |
536 | followingUrl: actorJSON.following, |
537 | ||
faa9d434 | 538 | sharedInboxUrl: actorJSON.endpoints?.sharedInbox |
47581df0 | 539 | ? actorJSON.endpoints.sharedInbox |
a82ddfad | 540 | : null |
50d6de9c C |
541 | }) |
542 | ||
2cb03dc1 C |
543 | const avatarInfo = getImageInfoIfExists(actorJSON, ActorImageType.AVATAR) |
544 | const bannerInfo = getImageInfoIfExists(actorJSON, ActorImageType.BANNER) | |
c5911fd3 | 545 | |
e12a0092 | 546 | const name = actorJSON.name || actorJSON.preferredUsername |
50d6de9c | 547 | return { |
db4b15f2 | 548 | statusCode: requestResult.statusCode, |
f5b0af50 C |
549 | result: { |
550 | actor, | |
551 | name, | |
557b13ae | 552 | avatar: avatarInfo, |
2cb03dc1 | 553 | banner: bannerInfo, |
f5b0af50 C |
554 | summary: actorJSON.summary, |
555 | support: actorJSON.support, | |
418d092a | 556 | playlists: actorJSON.playlists, |
f5b0af50 C |
557 | attributedTo: actorJSON.attributedTo |
558 | } | |
50d6de9c C |
559 | } |
560 | } | |
561 | ||
453e83ea | 562 | async function saveAccount (actor: MActorId, result: FetchRemoteActorResult, t: Transaction) { |
2c897999 C |
563 | const [ accountCreated ] = await AccountModel.findOrCreate({ |
564 | defaults: { | |
565 | name: result.name, | |
2422c46b | 566 | description: result.summary, |
2c897999 C |
567 | actorId: actor.id |
568 | }, | |
569 | where: { | |
570 | actorId: actor.id | |
571 | }, | |
572 | transaction: t | |
50d6de9c C |
573 | }) |
574 | ||
453e83ea | 575 | return accountCreated as MAccount |
50d6de9c C |
576 | } |
577 | ||
453e83ea | 578 | async function saveVideoChannel (actor: MActorId, result: FetchRemoteActorResult, ownerActor: MActorAccountId, t: Transaction) { |
2c897999 C |
579 | const [ videoChannelCreated ] = await VideoChannelModel.findOrCreate({ |
580 | defaults: { | |
581 | name: result.name, | |
582 | description: result.summary, | |
2422c46b | 583 | support: result.support, |
2c897999 C |
584 | actorId: actor.id, |
585 | accountId: ownerActor.Account.id | |
586 | }, | |
587 | where: { | |
588 | actorId: actor.id | |
589 | }, | |
590 | transaction: t | |
50d6de9c C |
591 | }) |
592 | ||
453e83ea | 593 | return videoChannelCreated as MChannel |
50d6de9c | 594 | } |