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