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