diff options
author | Chocobozzz <me@florianbigard.com> | 2021-06-03 16:02:29 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-06-03 16:40:32 +0200 |
commit | 136d7efde798d3dc0ec0dd18aac674365f7d162e (patch) | |
tree | 3a0e2a7a5d04dedf0d8ffda99c2787cecb838891 /server/lib/activitypub/process | |
parent | 49af5ac8c2653cb0ef23479c9d3256c5b724d49d (diff) | |
download | PeerTube-136d7efde798d3dc0ec0dd18aac674365f7d162e.tar.gz PeerTube-136d7efde798d3dc0ec0dd18aac674365f7d162e.tar.zst PeerTube-136d7efde798d3dc0ec0dd18aac674365f7d162e.zip |
Refactor AP actors
Diffstat (limited to 'server/lib/activitypub/process')
-rw-r--r-- | server/lib/activitypub/process/process-accept.ts | 2 | ||||
-rw-r--r-- | server/lib/activitypub/process/process-update.ts | 62 | ||||
-rw-r--r-- | server/lib/activitypub/process/process.ts | 14 |
3 files changed, 16 insertions, 62 deletions
diff --git a/server/lib/activitypub/process/process-accept.ts b/server/lib/activitypub/process/process-accept.ts index 8ad470cf4..077b01eda 100644 --- a/server/lib/activitypub/process/process-accept.ts +++ b/server/lib/activitypub/process/process-accept.ts | |||
@@ -2,7 +2,7 @@ import { ActivityAccept } from '../../../../shared/models/activitypub' | |||
2 | import { ActorFollowModel } from '../../../models/actor/actor-follow' | 2 | import { ActorFollowModel } from '../../../models/actor/actor-follow' |
3 | import { APProcessorOptions } from '../../../types/activitypub-processor.model' | 3 | import { APProcessorOptions } from '../../../types/activitypub-processor.model' |
4 | import { MActorDefault, MActorSignature } from '../../../types/models' | 4 | import { MActorDefault, MActorSignature } from '../../../types/models' |
5 | import { addFetchOutboxJob } from '../actor' | 5 | import { addFetchOutboxJob } from '../outbox' |
6 | 6 | ||
7 | async function processAcceptActivity (options: APProcessorOptions<ActivityAccept>) { | 7 | async function processAcceptActivity (options: APProcessorOptions<ActivityAccept>) { |
8 | const { byActor: targetActor, inboxActor } = options | 8 | const { byActor: targetActor, inboxActor } = options |
diff --git a/server/lib/activitypub/process/process-update.ts b/server/lib/activitypub/process/process-update.ts index d2b63c901..aa80d5d09 100644 --- a/server/lib/activitypub/process/process-update.ts +++ b/server/lib/activitypub/process/process-update.ts | |||
@@ -1,19 +1,16 @@ | |||
1 | import { isRedundancyAccepted } from '@server/lib/redundancy' | 1 | import { isRedundancyAccepted } from '@server/lib/redundancy' |
2 | import { ActorImageType } from '@shared/models' | ||
3 | import { ActivityUpdate, CacheFileObject, VideoObject } from '../../../../shared/models/activitypub' | 2 | import { ActivityUpdate, CacheFileObject, VideoObject } from '../../../../shared/models/activitypub' |
4 | import { ActivityPubActor } from '../../../../shared/models/activitypub/activitypub-actor' | 3 | import { ActivityPubActor } from '../../../../shared/models/activitypub/activitypub-actor' |
5 | import { PlaylistObject } from '../../../../shared/models/activitypub/objects/playlist-object' | 4 | import { PlaylistObject } from '../../../../shared/models/activitypub/objects/playlist-object' |
6 | import { isCacheFileObjectValid } from '../../../helpers/custom-validators/activitypub/cache-file' | 5 | import { isCacheFileObjectValid } from '../../../helpers/custom-validators/activitypub/cache-file' |
7 | import { sanitizeAndCheckVideoTorrentObject } from '../../../helpers/custom-validators/activitypub/videos' | 6 | import { sanitizeAndCheckVideoTorrentObject } from '../../../helpers/custom-validators/activitypub/videos' |
8 | import { resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers/database-utils' | 7 | import { retryTransactionWrapper } from '../../../helpers/database-utils' |
9 | import { logger } from '../../../helpers/logger' | 8 | import { logger } from '../../../helpers/logger' |
10 | import { sequelizeTypescript } from '../../../initializers/database' | 9 | import { sequelizeTypescript } from '../../../initializers/database' |
11 | import { AccountModel } from '../../../models/account/account' | ||
12 | import { ActorModel } from '../../../models/actor/actor' | 10 | import { ActorModel } from '../../../models/actor/actor' |
13 | import { VideoChannelModel } from '../../../models/video/video-channel' | ||
14 | import { APProcessorOptions } from '../../../types/activitypub-processor.model' | 11 | import { APProcessorOptions } from '../../../types/activitypub-processor.model' |
15 | import { MActorSignature } from '../../../types/models' | 12 | import { MActorFull, MActorSignature } from '../../../types/models' |
16 | import { getImageInfoIfExists, updateActorImageInstance, updateActorInstance } from '../actor' | 13 | import { APActorUpdater } from '../actors/updater' |
17 | import { createOrUpdateCacheFile } from '../cache-file' | 14 | import { createOrUpdateCacheFile } from '../cache-file' |
18 | import { createOrUpdateVideoPlaylist } from '../playlists' | 15 | import { createOrUpdateVideoPlaylist } from '../playlists' |
19 | import { forwardVideoRelatedActivity } from '../send/utils' | 16 | import { forwardVideoRelatedActivity } from '../send/utils' |
@@ -99,56 +96,13 @@ async function processUpdateCacheFile (byActor: MActorSignature, activity: Activ | |||
99 | } | 96 | } |
100 | } | 97 | } |
101 | 98 | ||
102 | async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) { | 99 | async function processUpdateActor (actor: MActorFull, activity: ActivityUpdate) { |
103 | const actorAttributesToUpdate = activity.object as ActivityPubActor | 100 | const actorObject = activity.object as ActivityPubActor |
104 | 101 | ||
105 | logger.debug('Updating remote account "%s".', actorAttributesToUpdate.url) | 102 | logger.debug('Updating remote account "%s".', actorObject.url) |
106 | let accountOrChannelInstance: AccountModel | VideoChannelModel | ||
107 | let actorFieldsSave: object | ||
108 | let accountOrChannelFieldsSave: object | ||
109 | 103 | ||
110 | // Fetch icon? | 104 | const updater = new APActorUpdater(actorObject, actor) |
111 | const avatarInfo = getImageInfoIfExists(actorAttributesToUpdate, ActorImageType.AVATAR) | 105 | return updater.update() |
112 | const bannerInfo = getImageInfoIfExists(actorAttributesToUpdate, ActorImageType.BANNER) | ||
113 | |||
114 | try { | ||
115 | await sequelizeTypescript.transaction(async t => { | ||
116 | actorFieldsSave = actor.toJSON() | ||
117 | |||
118 | if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel | ||
119 | else accountOrChannelInstance = actor.Account | ||
120 | |||
121 | accountOrChannelFieldsSave = accountOrChannelInstance.toJSON() | ||
122 | |||
123 | await updateActorInstance(actor, actorAttributesToUpdate) | ||
124 | |||
125 | await updateActorImageInstance(actor, ActorImageType.AVATAR, avatarInfo, t) | ||
126 | await updateActorImageInstance(actor, ActorImageType.BANNER, bannerInfo, t) | ||
127 | |||
128 | await actor.save({ transaction: t }) | ||
129 | |||
130 | accountOrChannelInstance.name = actorAttributesToUpdate.name || actorAttributesToUpdate.preferredUsername | ||
131 | accountOrChannelInstance.description = actorAttributesToUpdate.summary | ||
132 | |||
133 | if (accountOrChannelInstance instanceof VideoChannelModel) accountOrChannelInstance.support = actorAttributesToUpdate.support | ||
134 | |||
135 | await accountOrChannelInstance.save({ transaction: t }) | ||
136 | }) | ||
137 | |||
138 | logger.info('Remote account %s updated', actorAttributesToUpdate.url) | ||
139 | } catch (err) { | ||
140 | if (actor !== undefined && actorFieldsSave !== undefined) { | ||
141 | resetSequelizeInstance(actor, actorFieldsSave) | ||
142 | } | ||
143 | |||
144 | if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) { | ||
145 | resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave) | ||
146 | } | ||
147 | |||
148 | // This is just a debug because we will retry the insert | ||
149 | logger.debug('Cannot update the remote account.', { err }) | ||
150 | throw err | ||
151 | } | ||
152 | } | 106 | } |
153 | 107 | ||
154 | async function processUpdatePlaylist (byActor: MActorSignature, activity: ActivityUpdate) { | 108 | async function processUpdatePlaylist (byActor: MActorSignature, activity: ActivityUpdate) { |
diff --git a/server/lib/activitypub/process/process.ts b/server/lib/activitypub/process/process.ts index 5cef75665..02a23d098 100644 --- a/server/lib/activitypub/process/process.ts +++ b/server/lib/activitypub/process/process.ts | |||
@@ -1,22 +1,22 @@ | |||
1 | import { StatsManager } from '@server/lib/stat-manager' | ||
1 | import { Activity, ActivityType } from '../../../../shared/models/activitypub' | 2 | import { Activity, ActivityType } from '../../../../shared/models/activitypub' |
2 | import { checkUrlsSameHost, getAPId } from '../../../helpers/activitypub' | 3 | import { checkUrlsSameHost, getAPId } from '../../../helpers/activitypub' |
3 | import { logger } from '../../../helpers/logger' | 4 | import { logger } from '../../../helpers/logger' |
5 | import { APProcessorOptions } from '../../../types/activitypub-processor.model' | ||
6 | import { MActorDefault, MActorSignature } from '../../../types/models' | ||
7 | import { getOrCreateAPActor } from '../actors' | ||
4 | import { processAcceptActivity } from './process-accept' | 8 | import { processAcceptActivity } from './process-accept' |
5 | import { processAnnounceActivity } from './process-announce' | 9 | import { processAnnounceActivity } from './process-announce' |
6 | import { processCreateActivity } from './process-create' | 10 | import { processCreateActivity } from './process-create' |
7 | import { processDeleteActivity } from './process-delete' | 11 | import { processDeleteActivity } from './process-delete' |
12 | import { processDislikeActivity } from './process-dislike' | ||
13 | import { processFlagActivity } from './process-flag' | ||
8 | import { processFollowActivity } from './process-follow' | 14 | import { processFollowActivity } from './process-follow' |
9 | import { processLikeActivity } from './process-like' | 15 | import { processLikeActivity } from './process-like' |
10 | import { processRejectActivity } from './process-reject' | 16 | import { processRejectActivity } from './process-reject' |
11 | import { processUndoActivity } from './process-undo' | 17 | import { processUndoActivity } from './process-undo' |
12 | import { processUpdateActivity } from './process-update' | 18 | import { processUpdateActivity } from './process-update' |
13 | import { getOrCreateActorAndServerAndModel } from '../actor' | ||
14 | import { processDislikeActivity } from './process-dislike' | ||
15 | import { processFlagActivity } from './process-flag' | ||
16 | import { processViewActivity } from './process-view' | 19 | import { processViewActivity } from './process-view' |
17 | import { APProcessorOptions } from '../../../types/activitypub-processor.model' | ||
18 | import { MActorDefault, MActorSignature } from '../../../types/models' | ||
19 | import { StatsManager } from '@server/lib/stat-manager' | ||
20 | 20 | ||
21 | const processActivity: { [ P in ActivityType ]: (options: APProcessorOptions<Activity>) => Promise<any> } = { | 21 | const processActivity: { [ P in ActivityType ]: (options: APProcessorOptions<Activity>) => Promise<any> } = { |
22 | Create: processCreateActivity, | 22 | Create: processCreateActivity, |
@@ -65,7 +65,7 @@ async function processActivities ( | |||
65 | continue | 65 | continue |
66 | } | 66 | } |
67 | 67 | ||
68 | const byActor = signatureActor || actorsCache[actorUrl] || await getOrCreateActorAndServerAndModel(actorUrl) | 68 | const byActor = signatureActor || actorsCache[actorUrl] || await getOrCreateAPActor(actorUrl) |
69 | actorsCache[actorUrl] = byActor | 69 | actorsCache[actorUrl] = byActor |
70 | 70 | ||
71 | const activityProcessor = processActivity[activity.type] | 71 | const activityProcessor = processActivity[activity.type] |