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