diff options
Diffstat (limited to 'server/lib/activitypub/actors')
-rw-r--r-- | server/lib/activitypub/actors/get.ts | 119 | ||||
-rw-r--r-- | server/lib/activitypub/actors/image.ts | 94 | ||||
-rw-r--r-- | server/lib/activitypub/actors/index.ts | 5 | ||||
-rw-r--r-- | server/lib/activitypub/actors/keys.ts | 16 | ||||
-rw-r--r-- | server/lib/activitypub/actors/refresh.ts | 63 | ||||
-rw-r--r-- | server/lib/activitypub/actors/shared/creator.ts | 149 | ||||
-rw-r--r-- | server/lib/activitypub/actors/shared/index.ts | 3 | ||||
-rw-r--r-- | server/lib/activitypub/actors/shared/object-to-model-attributes.ts | 70 | ||||
-rw-r--r-- | server/lib/activitypub/actors/shared/url-to-object.ts | 54 | ||||
-rw-r--r-- | server/lib/activitypub/actors/updater.ts | 90 |
10 files changed, 663 insertions, 0 deletions
diff --git a/server/lib/activitypub/actors/get.ts b/server/lib/activitypub/actors/get.ts new file mode 100644 index 000000000..0d5bea789 --- /dev/null +++ b/server/lib/activitypub/actors/get.ts | |||
@@ -0,0 +1,119 @@ | |||
1 | |||
2 | import { checkUrlsSameHost, getAPId } from '@server/helpers/activitypub' | ||
3 | import { ActorFetchByUrlType, fetchActorByUrl } from '@server/helpers/actor' | ||
4 | import { retryTransactionWrapper } from '@server/helpers/database-utils' | ||
5 | import { logger } from '@server/helpers/logger' | ||
6 | import { JobQueue } from '@server/lib/job-queue' | ||
7 | import { MActor, MActorAccountChannelId, MActorAccountChannelIdActor, MActorAccountId, MActorFullActor } from '@server/types/models' | ||
8 | import { ActivityPubActor } from '@shared/models' | ||
9 | import { refreshActorIfNeeded } from './refresh' | ||
10 | import { APActorCreator, fetchRemoteActor } from './shared' | ||
11 | |||
12 | function getOrCreateAPActor ( | ||
13 | activityActor: string | ActivityPubActor, | ||
14 | fetchType: 'all', | ||
15 | recurseIfNeeded?: boolean, | ||
16 | updateCollections?: boolean | ||
17 | ): Promise<MActorFullActor> | ||
18 | |||
19 | function getOrCreateAPActor ( | ||
20 | activityActor: string | ActivityPubActor, | ||
21 | fetchType?: 'association-ids', | ||
22 | recurseIfNeeded?: boolean, | ||
23 | updateCollections?: boolean | ||
24 | ): Promise<MActorAccountChannelId> | ||
25 | |||
26 | async function getOrCreateAPActor ( | ||
27 | activityActor: string | ActivityPubActor, | ||
28 | fetchType: ActorFetchByUrlType = 'association-ids', | ||
29 | recurseIfNeeded = true, | ||
30 | updateCollections = false | ||
31 | ): Promise<MActorFullActor | MActorAccountChannelId> { | ||
32 | const actorUrl = getAPId(activityActor) | ||
33 | let actor = await loadActorFromDB(actorUrl, fetchType) | ||
34 | |||
35 | let created = false | ||
36 | let accountPlaylistsUrl: string | ||
37 | |||
38 | // We don't have this actor in our database, fetch it on remote | ||
39 | if (!actor) { | ||
40 | const { actorObject } = await fetchRemoteActor(actorUrl) | ||
41 | if (actorObject === undefined) throw new Error('Cannot fetch remote actor ' + actorUrl) | ||
42 | |||
43 | // Create the attributed to actor | ||
44 | // In PeerTube a video channel is owned by an account | ||
45 | let ownerActor: MActorFullActor | ||
46 | if (recurseIfNeeded === true && actorObject.type === 'Group') { | ||
47 | ownerActor = await getOrCreateAPOwner(actorObject, actorUrl) | ||
48 | } | ||
49 | |||
50 | const creator = new APActorCreator(actorObject, ownerActor) | ||
51 | actor = await retryTransactionWrapper(creator.create.bind(creator)) | ||
52 | created = true | ||
53 | accountPlaylistsUrl = actorObject.playlists | ||
54 | } | ||
55 | |||
56 | if (actor.Account) (actor as MActorAccountChannelIdActor).Account.Actor = actor | ||
57 | if (actor.VideoChannel) (actor as MActorAccountChannelIdActor).VideoChannel.Actor = actor | ||
58 | |||
59 | const { actor: actorRefreshed, refreshed } = await retryTransactionWrapper(refreshActorIfNeeded, actor, fetchType) | ||
60 | if (!actorRefreshed) throw new Error('Actor ' + actor.url + ' does not exist anymore.') | ||
61 | |||
62 | await scheduleOutboxFetchIfNeeded(actor, created, refreshed, updateCollections) | ||
63 | await schedulePlaylistFetchIfNeeded(actor, created, accountPlaylistsUrl) | ||
64 | |||
65 | return actorRefreshed | ||
66 | } | ||
67 | |||
68 | // --------------------------------------------------------------------------- | ||
69 | |||
70 | export { | ||
71 | getOrCreateAPActor | ||
72 | } | ||
73 | |||
74 | // --------------------------------------------------------------------------- | ||
75 | |||
76 | async function loadActorFromDB (actorUrl: string, fetchType: ActorFetchByUrlType) { | ||
77 | let actor = await fetchActorByUrl(actorUrl, fetchType) | ||
78 | |||
79 | // Orphan actor (not associated to an account of channel) so recreate it | ||
80 | if (actor && (!actor.Account && !actor.VideoChannel)) { | ||
81 | await actor.destroy() | ||
82 | actor = null | ||
83 | } | ||
84 | |||
85 | return actor | ||
86 | } | ||
87 | |||
88 | function getOrCreateAPOwner (actorObject: ActivityPubActor, actorUrl: string) { | ||
89 | const accountAttributedTo = actorObject.attributedTo.find(a => a.type === 'Person') | ||
90 | if (!accountAttributedTo) throw new Error('Cannot find account attributed to video channel ' + actorUrl) | ||
91 | |||
92 | if (checkUrlsSameHost(accountAttributedTo.id, actorUrl) !== true) { | ||
93 | throw new Error(`Account attributed to ${accountAttributedTo.id} does not have the same host than actor url ${actorUrl}`) | ||
94 | } | ||
95 | |||
96 | try { | ||
97 | // Don't recurse another time | ||
98 | const recurseIfNeeded = false | ||
99 | return getOrCreateAPActor(accountAttributedTo.id, 'all', recurseIfNeeded) | ||
100 | } catch (err) { | ||
101 | logger.error('Cannot get or create account attributed to video channel ' + actorUrl) | ||
102 | throw new Error(err) | ||
103 | } | ||
104 | } | ||
105 | |||
106 | async function scheduleOutboxFetchIfNeeded (actor: MActor, created: boolean, refreshed: boolean, updateCollections: boolean) { | ||
107 | if ((created === true || refreshed === true) && updateCollections === true) { | ||
108 | const payload = { uri: actor.outboxUrl, type: 'activity' as 'activity' } | ||
109 | await JobQueue.Instance.createJobWithPromise({ type: 'activitypub-http-fetcher', payload }) | ||
110 | } | ||
111 | } | ||
112 | |||
113 | async function schedulePlaylistFetchIfNeeded (actor: MActorAccountId, created: boolean, accountPlaylistsUrl: string) { | ||
114 | // We created a new account: fetch the playlists | ||
115 | if (created === true && actor.Account && accountPlaylistsUrl) { | ||
116 | const payload = { uri: accountPlaylistsUrl, accountId: actor.Account.id, type: 'account-playlists' as 'account-playlists' } | ||
117 | await JobQueue.Instance.createJobWithPromise({ type: 'activitypub-http-fetcher', payload }) | ||
118 | } | ||
119 | } | ||
diff --git a/server/lib/activitypub/actors/image.ts b/server/lib/activitypub/actors/image.ts new file mode 100644 index 000000000..443ad0a63 --- /dev/null +++ b/server/lib/activitypub/actors/image.ts | |||
@@ -0,0 +1,94 @@ | |||
1 | import { Transaction } from 'sequelize/types' | ||
2 | import { logger } from '@server/helpers/logger' | ||
3 | import { ActorImageModel } from '@server/models/actor/actor-image' | ||
4 | import { MActorImage, MActorImages } from '@server/types/models' | ||
5 | import { ActorImageType } from '@shared/models' | ||
6 | |||
7 | type ImageInfo = { | ||
8 | name: string | ||
9 | fileUrl: string | ||
10 | height: number | ||
11 | width: number | ||
12 | onDisk?: boolean | ||
13 | } | ||
14 | |||
15 | async function updateActorImageInstance (actor: MActorImages, type: ActorImageType, imageInfo: ImageInfo | null, t: Transaction) { | ||
16 | const oldImageModel = type === ActorImageType.AVATAR | ||
17 | ? actor.Avatar | ||
18 | : actor.Banner | ||
19 | |||
20 | if (oldImageModel) { | ||
21 | // Don't update the avatar if the file URL did not change | ||
22 | if (imageInfo?.fileUrl && oldImageModel.fileUrl === imageInfo.fileUrl) return actor | ||
23 | |||
24 | try { | ||
25 | await oldImageModel.destroy({ transaction: t }) | ||
26 | |||
27 | setActorImage(actor, type, null) | ||
28 | } catch (err) { | ||
29 | logger.error('Cannot remove old actor image of actor %s.', actor.url, { err }) | ||
30 | } | ||
31 | } | ||
32 | |||
33 | if (imageInfo) { | ||
34 | const imageModel = await ActorImageModel.create({ | ||
35 | filename: imageInfo.name, | ||
36 | onDisk: imageInfo.onDisk ?? false, | ||
37 | fileUrl: imageInfo.fileUrl, | ||
38 | height: imageInfo.height, | ||
39 | width: imageInfo.width, | ||
40 | type | ||
41 | }, { transaction: t }) | ||
42 | |||
43 | setActorImage(actor, type, imageModel) | ||
44 | } | ||
45 | |||
46 | return actor | ||
47 | } | ||
48 | |||
49 | async function deleteActorImageInstance (actor: MActorImages, type: ActorImageType, t: Transaction) { | ||
50 | try { | ||
51 | if (type === ActorImageType.AVATAR) { | ||
52 | await actor.Avatar.destroy({ transaction: t }) | ||
53 | |||
54 | actor.avatarId = null | ||
55 | actor.Avatar = null | ||
56 | } else { | ||
57 | await actor.Banner.destroy({ transaction: t }) | ||
58 | |||
59 | actor.bannerId = null | ||
60 | actor.Banner = null | ||
61 | } | ||
62 | } catch (err) { | ||
63 | logger.error('Cannot remove old image of actor %s.', actor.url, { err }) | ||
64 | } | ||
65 | |||
66 | return actor | ||
67 | } | ||
68 | |||
69 | // --------------------------------------------------------------------------- | ||
70 | |||
71 | export { | ||
72 | ImageInfo, | ||
73 | |||
74 | updateActorImageInstance, | ||
75 | deleteActorImageInstance | ||
76 | } | ||
77 | |||
78 | // --------------------------------------------------------------------------- | ||
79 | |||
80 | function setActorImage (actorModel: MActorImages, type: ActorImageType, imageModel: MActorImage) { | ||
81 | const id = imageModel | ||
82 | ? imageModel.id | ||
83 | : null | ||
84 | |||
85 | if (type === ActorImageType.AVATAR) { | ||
86 | actorModel.avatarId = id | ||
87 | actorModel.Avatar = imageModel | ||
88 | } else { | ||
89 | actorModel.bannerId = id | ||
90 | actorModel.Banner = imageModel | ||
91 | } | ||
92 | |||
93 | return actorModel | ||
94 | } | ||
diff --git a/server/lib/activitypub/actors/index.ts b/server/lib/activitypub/actors/index.ts new file mode 100644 index 000000000..a54da6798 --- /dev/null +++ b/server/lib/activitypub/actors/index.ts | |||
@@ -0,0 +1,5 @@ | |||
1 | export * from './get' | ||
2 | export * from './image' | ||
3 | export * from './keys' | ||
4 | export * from './refresh' | ||
5 | export * from './updater' | ||
diff --git a/server/lib/activitypub/actors/keys.ts b/server/lib/activitypub/actors/keys.ts new file mode 100644 index 000000000..c3d18abd8 --- /dev/null +++ b/server/lib/activitypub/actors/keys.ts | |||
@@ -0,0 +1,16 @@ | |||
1 | import { createPrivateAndPublicKeys } from '@server/helpers/peertube-crypto' | ||
2 | import { MActor } from '@server/types/models' | ||
3 | |||
4 | // Set account keys, this could be long so process after the account creation and do not block the client | ||
5 | async function generateAndSaveActorKeys <T extends MActor> (actor: T) { | ||
6 | const { publicKey, privateKey } = await createPrivateAndPublicKeys() | ||
7 | |||
8 | actor.publicKey = publicKey | ||
9 | actor.privateKey = privateKey | ||
10 | |||
11 | return actor.save() | ||
12 | } | ||
13 | |||
14 | export { | ||
15 | generateAndSaveActorKeys | ||
16 | } | ||
diff --git a/server/lib/activitypub/actors/refresh.ts b/server/lib/activitypub/actors/refresh.ts new file mode 100644 index 000000000..ff3b249d0 --- /dev/null +++ b/server/lib/activitypub/actors/refresh.ts | |||
@@ -0,0 +1,63 @@ | |||
1 | import { ActorFetchByUrlType } from '@server/helpers/actor' | ||
2 | import { logger } from '@server/helpers/logger' | ||
3 | import { PeerTubeRequestError } from '@server/helpers/requests' | ||
4 | import { getUrlFromWebfinger } from '@server/helpers/webfinger' | ||
5 | import { ActorModel } from '@server/models/actor/actor' | ||
6 | import { MActorAccountChannelId, MActorFull } from '@server/types/models' | ||
7 | import { HttpStatusCode } from '@shared/core-utils' | ||
8 | import { fetchRemoteActor } from './shared' | ||
9 | import { APActorUpdater } from './updater' | ||
10 | |||
11 | async function refreshActorIfNeeded <T extends MActorFull | MActorAccountChannelId> ( | ||
12 | actorArg: T, | ||
13 | fetchedType: ActorFetchByUrlType | ||
14 | ): Promise<{ actor: T | MActorFull, refreshed: boolean }> { | ||
15 | if (!actorArg.isOutdated()) return { actor: actorArg, refreshed: false } | ||
16 | |||
17 | // We need more attributes | ||
18 | const actor = fetchedType === 'all' | ||
19 | ? actorArg as MActorFull | ||
20 | : await ActorModel.loadByUrlAndPopulateAccountAndChannel(actorArg.url) | ||
21 | |||
22 | try { | ||
23 | const actorUrl = await getActorUrl(actor) | ||
24 | const { actorObject } = await fetchRemoteActor(actorUrl) | ||
25 | |||
26 | if (actorObject === undefined) { | ||
27 | logger.warn('Cannot fetch remote actor in refresh actor.') | ||
28 | return { actor, refreshed: false } | ||
29 | } | ||
30 | |||
31 | const updater = new APActorUpdater(actorObject, actor) | ||
32 | await updater.update() | ||
33 | |||
34 | return { refreshed: true, actor } | ||
35 | } catch (err) { | ||
36 | if ((err as PeerTubeRequestError).statusCode === HttpStatusCode.NOT_FOUND_404) { | ||
37 | logger.info('Deleting actor %s because there is a 404 in refresh actor.', actor.url) | ||
38 | |||
39 | actor.Account | ||
40 | ? await actor.Account.destroy() | ||
41 | : await actor.VideoChannel.destroy() | ||
42 | |||
43 | return { actor: undefined, refreshed: false } | ||
44 | } | ||
45 | |||
46 | logger.warn('Cannot refresh actor %s.', actor.url, { err }) | ||
47 | return { actor, refreshed: false } | ||
48 | } | ||
49 | } | ||
50 | |||
51 | export { | ||
52 | refreshActorIfNeeded | ||
53 | } | ||
54 | |||
55 | // --------------------------------------------------------------------------- | ||
56 | |||
57 | function getActorUrl (actor: MActorFull) { | ||
58 | return getUrlFromWebfinger(actor.preferredUsername + '@' + actor.getHost()) | ||
59 | .catch(err => { | ||
60 | logger.warn('Cannot get actor URL from webfinger, keeping the old one.', err) | ||
61 | return actor.url | ||
62 | }) | ||
63 | } | ||
diff --git a/server/lib/activitypub/actors/shared/creator.ts b/server/lib/activitypub/actors/shared/creator.ts new file mode 100644 index 000000000..999aed97d --- /dev/null +++ b/server/lib/activitypub/actors/shared/creator.ts | |||
@@ -0,0 +1,149 @@ | |||
1 | import { Op, Transaction } from 'sequelize' | ||
2 | import { sequelizeTypescript } from '@server/initializers/database' | ||
3 | import { AccountModel } from '@server/models/account/account' | ||
4 | import { ActorModel } from '@server/models/actor/actor' | ||
5 | import { ServerModel } from '@server/models/server/server' | ||
6 | import { VideoChannelModel } from '@server/models/video/video-channel' | ||
7 | import { MAccount, MAccountDefault, MActor, MActorFullActor, MActorId, MActorImages, MChannel, MServer } from '@server/types/models' | ||
8 | import { ActivityPubActor, ActorImageType } from '@shared/models' | ||
9 | import { updateActorImageInstance } from '../image' | ||
10 | import { getActorAttributesFromObject, getActorDisplayNameFromObject, getImageInfoFromObject } from './object-to-model-attributes' | ||
11 | import { fetchActorFollowsCount } from './url-to-object' | ||
12 | |||
13 | export class APActorCreator { | ||
14 | |||
15 | constructor ( | ||
16 | private readonly actorObject: ActivityPubActor, | ||
17 | private readonly ownerActor?: MActorFullActor | ||
18 | ) { | ||
19 | |||
20 | } | ||
21 | |||
22 | async create (): Promise<MActorFullActor> { | ||
23 | const { followersCount, followingCount } = await fetchActorFollowsCount(this.actorObject) | ||
24 | |||
25 | const actorInstance = new ActorModel(getActorAttributesFromObject(this.actorObject, followersCount, followingCount)) | ||
26 | |||
27 | return sequelizeTypescript.transaction(async t => { | ||
28 | const server = await this.setServer(actorInstance, t) | ||
29 | |||
30 | await this.setImageIfNeeded(actorInstance, ActorImageType.AVATAR, t) | ||
31 | await this.setImageIfNeeded(actorInstance, ActorImageType.BANNER, t) | ||
32 | |||
33 | const { actorCreated, created } = await this.saveActor(actorInstance, t) | ||
34 | |||
35 | await this.tryToFixActorUrlIfNeeded(actorCreated, actorInstance, created, t) | ||
36 | |||
37 | if (actorCreated.type === 'Person' || actorCreated.type === 'Application') { // Account or PeerTube instance | ||
38 | actorCreated.Account = await this.saveAccount(actorCreated, t) as MAccountDefault | ||
39 | actorCreated.Account.Actor = actorCreated | ||
40 | } | ||
41 | |||
42 | if (actorCreated.type === 'Group') { // Video channel | ||
43 | const channel = await this.saveVideoChannel(actorCreated, t) | ||
44 | actorCreated.VideoChannel = Object.assign(channel, { Actor: actorCreated, Account: this.ownerActor.Account }) | ||
45 | } | ||
46 | |||
47 | actorCreated.Server = server | ||
48 | |||
49 | return actorCreated | ||
50 | }) | ||
51 | } | ||
52 | |||
53 | private async setServer (actor: MActor, t: Transaction) { | ||
54 | const actorHost = new URL(actor.url).host | ||
55 | |||
56 | const serverOptions = { | ||
57 | where: { | ||
58 | host: actorHost | ||
59 | }, | ||
60 | defaults: { | ||
61 | host: actorHost | ||
62 | }, | ||
63 | transaction: t | ||
64 | } | ||
65 | const [ server ] = await ServerModel.findOrCreate(serverOptions) | ||
66 | |||
67 | // Save our new account in database | ||
68 | actor.serverId = server.id | ||
69 | |||
70 | return server as MServer | ||
71 | } | ||
72 | |||
73 | private async setImageIfNeeded (actor: MActor, type: ActorImageType, t: Transaction) { | ||
74 | const imageInfo = getImageInfoFromObject(this.actorObject, type) | ||
75 | if (!imageInfo) return | ||
76 | |||
77 | return updateActorImageInstance(actor as MActorImages, type, imageInfo, t) | ||
78 | } | ||
79 | |||
80 | private async saveActor (actor: MActor, t: Transaction) { | ||
81 | // Force the actor creation using findOrCreate() instead of save() | ||
82 | // Sometimes Sequelize skips the save() when it thinks the instance already exists | ||
83 | // (which could be false in a retried query) | ||
84 | const [ actorCreated, created ] = await ActorModel.findOrCreate<MActorFullActor>({ | ||
85 | defaults: actor.toJSON(), | ||
86 | where: { | ||
87 | [Op.or]: [ | ||
88 | { | ||
89 | url: actor.url | ||
90 | }, | ||
91 | { | ||
92 | serverId: actor.serverId, | ||
93 | preferredUsername: actor.preferredUsername | ||
94 | } | ||
95 | ] | ||
96 | }, | ||
97 | transaction: t | ||
98 | }) | ||
99 | |||
100 | return { actorCreated, created } | ||
101 | } | ||
102 | |||
103 | private async tryToFixActorUrlIfNeeded (actorCreated: MActor, newActor: MActor, created: boolean, t: Transaction) { | ||
104 | // Try to fix non HTTPS accounts of remote instances that fixed their URL afterwards | ||
105 | if (created !== true && actorCreated.url !== newActor.url) { | ||
106 | // Only fix http://example.com/account/djidane to https://example.com/account/djidane | ||
107 | if (actorCreated.url.replace(/^http:\/\//, '') !== newActor.url.replace(/^https:\/\//, '')) { | ||
108 | throw new Error(`Actor from DB with URL ${actorCreated.url} does not correspond to actor ${newActor.url}`) | ||
109 | } | ||
110 | |||
111 | actorCreated.url = newActor.url | ||
112 | await actorCreated.save({ transaction: t }) | ||
113 | } | ||
114 | } | ||
115 | |||
116 | private async saveAccount (actor: MActorId, t: Transaction) { | ||
117 | const [ accountCreated ] = await AccountModel.findOrCreate({ | ||
118 | defaults: { | ||
119 | name: getActorDisplayNameFromObject(this.actorObject), | ||
120 | description: this.actorObject.summary, | ||
121 | actorId: actor.id | ||
122 | }, | ||
123 | where: { | ||
124 | actorId: actor.id | ||
125 | }, | ||
126 | transaction: t | ||
127 | }) | ||
128 | |||
129 | return accountCreated as MAccount | ||
130 | } | ||
131 | |||
132 | private async saveVideoChannel (actor: MActorId, t: Transaction) { | ||
133 | const [ videoChannelCreated ] = await VideoChannelModel.findOrCreate({ | ||
134 | defaults: { | ||
135 | name: getActorDisplayNameFromObject(this.actorObject), | ||
136 | description: this.actorObject.summary, | ||
137 | support: this.actorObject.support, | ||
138 | actorId: actor.id, | ||
139 | accountId: this.ownerActor.Account.id | ||
140 | }, | ||
141 | where: { | ||
142 | actorId: actor.id | ||
143 | }, | ||
144 | transaction: t | ||
145 | }) | ||
146 | |||
147 | return videoChannelCreated as MChannel | ||
148 | } | ||
149 | } | ||
diff --git a/server/lib/activitypub/actors/shared/index.ts b/server/lib/activitypub/actors/shared/index.ts new file mode 100644 index 000000000..a2ff468cf --- /dev/null +++ b/server/lib/activitypub/actors/shared/index.ts | |||
@@ -0,0 +1,3 @@ | |||
1 | export * from './creator' | ||
2 | export * from './url-to-object' | ||
3 | export * from './object-to-model-attributes' | ||
diff --git a/server/lib/activitypub/actors/shared/object-to-model-attributes.ts b/server/lib/activitypub/actors/shared/object-to-model-attributes.ts new file mode 100644 index 000000000..66b22c952 --- /dev/null +++ b/server/lib/activitypub/actors/shared/object-to-model-attributes.ts | |||
@@ -0,0 +1,70 @@ | |||
1 | import { extname } from 'path' | ||
2 | import { v4 as uuidv4 } from 'uuid' | ||
3 | import { isActivityPubUrlValid } from '@server/helpers/custom-validators/activitypub/misc' | ||
4 | import { MIMETYPES } from '@server/initializers/constants' | ||
5 | import { ActorModel } from '@server/models/actor/actor' | ||
6 | import { FilteredModelAttributes } from '@server/types' | ||
7 | import { ActivityPubActor, ActorImageType } from '@shared/models' | ||
8 | |||
9 | function getActorAttributesFromObject ( | ||
10 | actorObject: ActivityPubActor, | ||
11 | followersCount: number, | ||
12 | followingCount: number | ||
13 | ): FilteredModelAttributes<ActorModel> { | ||
14 | return { | ||
15 | type: actorObject.type, | ||
16 | preferredUsername: actorObject.preferredUsername, | ||
17 | url: actorObject.id, | ||
18 | publicKey: actorObject.publicKey.publicKeyPem, | ||
19 | privateKey: null, | ||
20 | followersCount, | ||
21 | followingCount, | ||
22 | inboxUrl: actorObject.inbox, | ||
23 | outboxUrl: actorObject.outbox, | ||
24 | followersUrl: actorObject.followers, | ||
25 | followingUrl: actorObject.following, | ||
26 | |||
27 | sharedInboxUrl: actorObject.endpoints?.sharedInbox | ||
28 | ? actorObject.endpoints.sharedInbox | ||
29 | : null | ||
30 | } | ||
31 | } | ||
32 | |||
33 | function getImageInfoFromObject (actorObject: ActivityPubActor, type: ActorImageType) { | ||
34 | const mimetypes = MIMETYPES.IMAGE | ||
35 | const icon = type === ActorImageType.AVATAR | ||
36 | ? actorObject.icon | ||
37 | : actorObject.image | ||
38 | |||
39 | if (!icon || icon.type !== 'Image' || !isActivityPubUrlValid(icon.url)) return undefined | ||
40 | |||
41 | let extension: string | ||
42 | |||
43 | if (icon.mediaType) { | ||
44 | extension = mimetypes.MIMETYPE_EXT[icon.mediaType] | ||
45 | } else { | ||
46 | const tmp = extname(icon.url) | ||
47 | |||
48 | if (mimetypes.EXT_MIMETYPE[tmp] !== undefined) extension = tmp | ||
49 | } | ||
50 | |||
51 | if (!extension) return undefined | ||
52 | |||
53 | return { | ||
54 | name: uuidv4() + extension, | ||
55 | fileUrl: icon.url, | ||
56 | height: icon.height, | ||
57 | width: icon.width, | ||
58 | type | ||
59 | } | ||
60 | } | ||
61 | |||
62 | function getActorDisplayNameFromObject (actorObject: ActivityPubActor) { | ||
63 | return actorObject.name || actorObject.preferredUsername | ||
64 | } | ||
65 | |||
66 | export { | ||
67 | getActorAttributesFromObject, | ||
68 | getImageInfoFromObject, | ||
69 | getActorDisplayNameFromObject | ||
70 | } | ||
diff --git a/server/lib/activitypub/actors/shared/url-to-object.ts b/server/lib/activitypub/actors/shared/url-to-object.ts new file mode 100644 index 000000000..f4f16b044 --- /dev/null +++ b/server/lib/activitypub/actors/shared/url-to-object.ts | |||
@@ -0,0 +1,54 @@ | |||
1 | |||
2 | import { checkUrlsSameHost } from '@server/helpers/activitypub' | ||
3 | import { sanitizeAndCheckActorObject } from '@server/helpers/custom-validators/activitypub/actor' | ||
4 | import { logger } from '@server/helpers/logger' | ||
5 | import { doJSONRequest } from '@server/helpers/requests' | ||
6 | import { ActivityPubActor, ActivityPubOrderedCollection } from '@shared/models' | ||
7 | |||
8 | async function fetchRemoteActor (actorUrl: string): Promise<{ statusCode: number, actorObject: ActivityPubActor }> { | ||
9 | logger.info('Fetching remote actor %s.', actorUrl) | ||
10 | |||
11 | const { body, statusCode } = await doJSONRequest<ActivityPubActor>(actorUrl, { activityPub: true }) | ||
12 | |||
13 | if (sanitizeAndCheckActorObject(body) === false) { | ||
14 | logger.debug('Remote actor JSON is not valid.', { actorJSON: body }) | ||
15 | return { actorObject: undefined, statusCode: statusCode } | ||
16 | } | ||
17 | |||
18 | if (checkUrlsSameHost(body.id, actorUrl) !== true) { | ||
19 | logger.warn('Actor url %s has not the same host than its AP id %s', actorUrl, body.id) | ||
20 | return { actorObject: undefined, statusCode: statusCode } | ||
21 | } | ||
22 | |||
23 | return { | ||
24 | statusCode, | ||
25 | |||
26 | actorObject: body | ||
27 | } | ||
28 | } | ||
29 | |||
30 | async function fetchActorFollowsCount (actorObject: ActivityPubActor) { | ||
31 | const followersCount = await fetchActorTotalItems(actorObject.followers) | ||
32 | const followingCount = await fetchActorTotalItems(actorObject.following) | ||
33 | |||
34 | return { followersCount, followingCount } | ||
35 | } | ||
36 | |||
37 | // --------------------------------------------------------------------------- | ||
38 | export { | ||
39 | fetchActorFollowsCount, | ||
40 | fetchRemoteActor | ||
41 | } | ||
42 | |||
43 | // --------------------------------------------------------------------------- | ||
44 | |||
45 | async function fetchActorTotalItems (url: string) { | ||
46 | try { | ||
47 | const { body } = await doJSONRequest<ActivityPubOrderedCollection<unknown>>(url, { activityPub: true }) | ||
48 | |||
49 | return body.totalItems || 0 | ||
50 | } catch (err) { | ||
51 | logger.warn('Cannot fetch remote actor count %s.', url, { err }) | ||
52 | return 0 | ||
53 | } | ||
54 | } | ||
diff --git a/server/lib/activitypub/actors/updater.ts b/server/lib/activitypub/actors/updater.ts new file mode 100644 index 000000000..471688f11 --- /dev/null +++ b/server/lib/activitypub/actors/updater.ts | |||
@@ -0,0 +1,90 @@ | |||
1 | import { resetSequelizeInstance } from '@server/helpers/database-utils' | ||
2 | import { logger } from '@server/helpers/logger' | ||
3 | import { sequelizeTypescript } from '@server/initializers/database' | ||
4 | import { VideoChannelModel } from '@server/models/video/video-channel' | ||
5 | import { MAccount, MActor, MActorFull, MChannel } from '@server/types/models' | ||
6 | import { ActivityPubActor, ActorImageType } from '@shared/models' | ||
7 | import { updateActorImageInstance } from './image' | ||
8 | import { fetchActorFollowsCount } from './shared' | ||
9 | import { getImageInfoFromObject } from './shared/object-to-model-attributes' | ||
10 | |||
11 | export class APActorUpdater { | ||
12 | |||
13 | private accountOrChannel: MAccount | MChannel | ||
14 | |||
15 | private readonly actorFieldsSave: object | ||
16 | private readonly accountOrChannelFieldsSave: object | ||
17 | |||
18 | constructor ( | ||
19 | private readonly actorObject: ActivityPubActor, | ||
20 | private readonly actor: MActorFull | ||
21 | ) { | ||
22 | this.actorFieldsSave = this.actor.toJSON() | ||
23 | |||
24 | if (this.actorObject.type === 'Group') this.accountOrChannel = this.actor.VideoChannel | ||
25 | else this.accountOrChannel = this.actor.Account | ||
26 | |||
27 | this.accountOrChannelFieldsSave = this.accountOrChannel.toJSON() | ||
28 | } | ||
29 | |||
30 | async update () { | ||
31 | const avatarInfo = getImageInfoFromObject(this.actorObject, ActorImageType.AVATAR) | ||
32 | const bannerInfo = getImageInfoFromObject(this.actorObject, ActorImageType.BANNER) | ||
33 | |||
34 | try { | ||
35 | await sequelizeTypescript.transaction(async t => { | ||
36 | await this.updateActorInstance(this.actor, this.actorObject) | ||
37 | |||
38 | await updateActorImageInstance(this.actor, ActorImageType.AVATAR, avatarInfo, t) | ||
39 | await updateActorImageInstance(this.actor, ActorImageType.BANNER, bannerInfo, t) | ||
40 | |||
41 | await this.actor.save({ transaction: t }) | ||
42 | |||
43 | this.accountOrChannel.name = this.actorObject.name || this.actorObject.preferredUsername | ||
44 | this.accountOrChannel.description = this.actorObject.summary | ||
45 | |||
46 | if (this.accountOrChannel instanceof VideoChannelModel) this.accountOrChannel.support = this.actorObject.support | ||
47 | |||
48 | await this.accountOrChannel.save({ transaction: t }) | ||
49 | }) | ||
50 | |||
51 | logger.info('Remote account %s updated', this.actorObject.url) | ||
52 | } catch (err) { | ||
53 | if (this.actor !== undefined && this.actorFieldsSave !== undefined) { | ||
54 | resetSequelizeInstance(this.actor, this.actorFieldsSave) | ||
55 | } | ||
56 | |||
57 | if (this.accountOrChannel !== undefined && this.accountOrChannelFieldsSave !== undefined) { | ||
58 | resetSequelizeInstance(this.accountOrChannel, this.accountOrChannelFieldsSave) | ||
59 | } | ||
60 | |||
61 | // This is just a debug because we will retry the insert | ||
62 | logger.debug('Cannot update the remote account.', { err }) | ||
63 | throw err | ||
64 | } | ||
65 | } | ||
66 | |||
67 | private async updateActorInstance (actorInstance: MActor, actorObject: ActivityPubActor) { | ||
68 | const { followersCount, followingCount } = await fetchActorFollowsCount(actorObject) | ||
69 | |||
70 | actorInstance.type = actorObject.type | ||
71 | actorInstance.preferredUsername = actorObject.preferredUsername | ||
72 | actorInstance.url = actorObject.id | ||
73 | actorInstance.publicKey = actorObject.publicKey.publicKeyPem | ||
74 | actorInstance.followersCount = followersCount | ||
75 | actorInstance.followingCount = followingCount | ||
76 | actorInstance.inboxUrl = actorObject.inbox | ||
77 | actorInstance.outboxUrl = actorObject.outbox | ||
78 | actorInstance.followersUrl = actorObject.followers | ||
79 | actorInstance.followingUrl = actorObject.following | ||
80 | |||
81 | if (actorObject.published) actorInstance.remoteCreatedAt = new Date(actorObject.published) | ||
82 | |||
83 | if (actorObject.endpoints?.sharedInbox) { | ||
84 | actorInstance.sharedInboxUrl = actorObject.endpoints.sharedInbox | ||
85 | } | ||
86 | |||
87 | // Force actor update | ||
88 | actorInstance.changed('updatedAt', true) | ||
89 | } | ||
90 | } | ||