diff options
Diffstat (limited to 'server')
32 files changed, 753 insertions, 685 deletions
diff --git a/server/controllers/api/search.ts b/server/controllers/api/search.ts index 0cb5674c2..ef0f4285d 100644 --- a/server/controllers/api/search.ts +++ b/server/controllers/api/search.ts | |||
@@ -15,7 +15,7 @@ import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../helpers/expr | |||
15 | import { logger } from '../../helpers/logger' | 15 | import { logger } from '../../helpers/logger' |
16 | import { getFormattedObjects } from '../../helpers/utils' | 16 | import { getFormattedObjects } from '../../helpers/utils' |
17 | import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger' | 17 | import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger' |
18 | import { getOrCreateActorAndServerAndModel } from '../../lib/activitypub/actor' | 18 | import { getOrCreateAPActor } from '../../lib/activitypub/actors' |
19 | import { | 19 | import { |
20 | asyncMiddleware, | 20 | asyncMiddleware, |
21 | commonVideosFiltersValidator, | 21 | commonVideosFiltersValidator, |
@@ -145,7 +145,7 @@ async function searchVideoChannelURI (search: string, isWebfingerSearch: boolean | |||
145 | 145 | ||
146 | if (isUserAbleToSearchRemoteURI(res)) { | 146 | if (isUserAbleToSearchRemoteURI(res)) { |
147 | try { | 147 | try { |
148 | const actor = await getOrCreateActorAndServerAndModel(uri, 'all', true, true) | 148 | const actor = await getOrCreateAPActor(uri, 'all', true, true) |
149 | videoChannel = actor.VideoChannel | 149 | videoChannel = actor.VideoChannel |
150 | } catch (err) { | 150 | } catch (err) { |
151 | logger.info('Cannot search remote video channel %s.', uri, { err }) | 151 | logger.info('Cannot search remote video channel %s.', uri, { err }) |
diff --git a/server/controllers/api/users/me.ts b/server/controllers/api/users/me.ts index 810e4295e..1f2b2f9dd 100644 --- a/server/controllers/api/users/me.ts +++ b/server/controllers/api/users/me.ts | |||
@@ -11,7 +11,7 @@ import { CONFIG } from '../../../initializers/config' | |||
11 | import { MIMETYPES } from '../../../initializers/constants' | 11 | import { MIMETYPES } from '../../../initializers/constants' |
12 | import { sequelizeTypescript } from '../../../initializers/database' | 12 | import { sequelizeTypescript } from '../../../initializers/database' |
13 | import { sendUpdateActor } from '../../../lib/activitypub/send' | 13 | import { sendUpdateActor } from '../../../lib/activitypub/send' |
14 | import { deleteLocalActorImageFile, updateLocalActorImageFile } from '../../../lib/actor-image' | 14 | import { deleteLocalActorImageFile, updateLocalActorImageFile } from '../../../lib/local-actor' |
15 | import { getOriginalVideoFileTotalDailyFromUser, getOriginalVideoFileTotalFromUser, sendVerifyUserEmail } from '../../../lib/user' | 15 | import { getOriginalVideoFileTotalDailyFromUser, getOriginalVideoFileTotalFromUser, sendVerifyUserEmail } from '../../../lib/user' |
16 | import { | 16 | import { |
17 | asyncMiddleware, | 17 | asyncMiddleware, |
diff --git a/server/controllers/api/video-channel.ts b/server/controllers/api/video-channel.ts index 34207ea8a..03aa918d3 100644 --- a/server/controllers/api/video-channel.ts +++ b/server/controllers/api/video-channel.ts | |||
@@ -13,8 +13,8 @@ import { CONFIG } from '../../initializers/config' | |||
13 | import { MIMETYPES } from '../../initializers/constants' | 13 | import { MIMETYPES } from '../../initializers/constants' |
14 | import { sequelizeTypescript } from '../../initializers/database' | 14 | import { sequelizeTypescript } from '../../initializers/database' |
15 | import { sendUpdateActor } from '../../lib/activitypub/send' | 15 | import { sendUpdateActor } from '../../lib/activitypub/send' |
16 | import { deleteLocalActorImageFile, updateLocalActorImageFile } from '../../lib/actor-image' | ||
17 | import { JobQueue } from '../../lib/job-queue' | 16 | import { JobQueue } from '../../lib/job-queue' |
17 | import { deleteLocalActorImageFile, updateLocalActorImageFile } from '../../lib/local-actor' | ||
18 | import { createLocalVideoChannel, federateAllVideosOfChannel } from '../../lib/video-channel' | 18 | import { createLocalVideoChannel, federateAllVideosOfChannel } from '../../lib/video-channel' |
19 | import { | 19 | import { |
20 | asyncMiddleware, | 20 | asyncMiddleware, |
diff --git a/server/controllers/lazy-static.ts b/server/controllers/lazy-static.ts index 9f260cef0..27b1b7160 100644 --- a/server/controllers/lazy-static.ts +++ b/server/controllers/lazy-static.ts | |||
@@ -4,8 +4,8 @@ import { VideosTorrentCache } from '@server/lib/files-cache/videos-torrent-cache | |||
4 | import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes' | 4 | import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes' |
5 | import { logger } from '../helpers/logger' | 5 | import { logger } from '../helpers/logger' |
6 | import { LAZY_STATIC_PATHS, STATIC_MAX_AGE } from '../initializers/constants' | 6 | import { LAZY_STATIC_PATHS, STATIC_MAX_AGE } from '../initializers/constants' |
7 | import { actorImagePathUnsafeCache, pushActorImageProcessInQueue } from '../lib/actor-image' | ||
8 | import { VideosCaptionCache, VideosPreviewCache } from '../lib/files-cache' | 7 | import { VideosCaptionCache, VideosPreviewCache } from '../lib/files-cache' |
8 | import { actorImagePathUnsafeCache, pushActorImageProcessInQueue } from '../lib/local-actor' | ||
9 | import { asyncMiddleware } from '../middlewares' | 9 | import { asyncMiddleware } from '../middlewares' |
10 | import { ActorImageModel } from '../models/actor/actor-image' | 10 | import { ActorImageModel } from '../models/actor/actor-image' |
11 | 11 | ||
diff --git a/server/lib/activitypub/actor.ts b/server/lib/activitypub/actor.ts deleted file mode 100644 index 1bcee7ef9..000000000 --- a/server/lib/activitypub/actor.ts +++ /dev/null | |||
@@ -1,593 +0,0 @@ | |||
1 | import * as Bluebird from 'bluebird' | ||
2 | import { extname } from 'path' | ||
3 | import { Op, Transaction } from 'sequelize' | ||
4 | import { URL } from 'url' | ||
5 | import { v4 as uuidv4 } from 'uuid' | ||
6 | import { getServerActor } from '@server/models/application/application' | ||
7 | import { ActorImageType } from '@shared/models' | ||
8 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
9 | import { ActivityPubActor, ActivityPubActorType, ActivityPubOrderedCollection } from '../../../shared/models/activitypub' | ||
10 | import { ActivityPubAttributedTo } from '../../../shared/models/activitypub/objects' | ||
11 | import { checkUrlsSameHost, getAPId } from '../../helpers/activitypub' | ||
12 | import { ActorFetchByUrlType, fetchActorByUrl } from '../../helpers/actor' | ||
13 | import { sanitizeAndCheckActorObject } from '../../helpers/custom-validators/activitypub/actor' | ||
14 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' | ||
15 | import { retryTransactionWrapper, updateInstanceWithAnother } from '../../helpers/database-utils' | ||
16 | import { logger } from '../../helpers/logger' | ||
17 | import { createPrivateAndPublicKeys } from '../../helpers/peertube-crypto' | ||
18 | import { doJSONRequest, PeerTubeRequestError } from '../../helpers/requests' | ||
19 | import { getUrlFromWebfinger } from '../../helpers/webfinger' | ||
20 | import { MIMETYPES, WEBSERVER } from '../../initializers/constants' | ||
21 | import { sequelizeTypescript } from '../../initializers/database' | ||
22 | import { AccountModel } from '../../models/account/account' | ||
23 | import { ActorModel } from '../../models/actor/actor' | ||
24 | import { ActorImageModel } from '../../models/actor/actor-image' | ||
25 | import { ServerModel } from '../../models/server/server' | ||
26 | import { VideoChannelModel } from '../../models/video/video-channel' | ||
27 | import { | ||
28 | MAccount, | ||
29 | MAccountDefault, | ||
30 | MActor, | ||
31 | MActorAccountChannelId, | ||
32 | MActorAccountChannelIdActor, | ||
33 | MActorAccountId, | ||
34 | MActorFull, | ||
35 | MActorFullActor, | ||
36 | MActorId, | ||
37 | MActorImage, | ||
38 | MActorImages, | ||
39 | MChannel | ||
40 | } from '../../types/models' | ||
41 | import { JobQueue } from '../job-queue' | ||
42 | |||
43 | // Set account keys, this could be long so process after the account creation and do not block the client | ||
44 | async 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() | ||
51 | } | ||
52 | |||
53 | function getOrCreateActorAndServerAndModel ( | ||
54 | activityActor: string | ActivityPubActor, | ||
55 | fetchType: 'all', | ||
56 | recurseIfNeeded?: boolean, | ||
57 | updateCollections?: boolean | ||
58 | ): Promise<MActorFullActor> | ||
59 | |||
60 | function getOrCreateActorAndServerAndModel ( | ||
61 | activityActor: string | ActivityPubActor, | ||
62 | fetchType?: 'association-ids', | ||
63 | recurseIfNeeded?: boolean, | ||
64 | updateCollections?: boolean | ||
65 | ): Promise<MActorAccountChannelId> | ||
66 | |||
67 | async function getOrCreateActorAndServerAndModel ( | ||
68 | activityActor: string | ActivityPubActor, | ||
69 | fetchType: ActorFetchByUrlType = 'association-ids', | ||
70 | recurseIfNeeded = true, | ||
71 | updateCollections = false | ||
72 | ): Promise<MActorFullActor | MActorAccountChannelId> { | ||
73 | const actorUrl = getAPId(activityActor) | ||
74 | let created = false | ||
75 | let accountPlaylistsUrl: string | ||
76 | |||
77 | let actor = await fetchActorByUrl(actorUrl, fetchType) | ||
78 | // Orphan actor (not associated to an account of channel) so recreate it | ||
79 | if (actor && (!actor.Account && !actor.VideoChannel)) { | ||
80 | await actor.destroy() | ||
81 | actor = null | ||
82 | } | ||
83 | |||
84 | // We don't have this actor in our database, fetch it on remote | ||
85 | if (!actor) { | ||
86 | const { result } = await fetchRemoteActor(actorUrl) | ||
87 | if (result === undefined) throw new Error('Cannot fetch remote actor ' + actorUrl) | ||
88 | |||
89 | // Create the attributed to actor | ||
90 | // In PeerTube a video channel is owned by an account | ||
91 | let ownerActor: MActorFullActor | ||
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 | |||
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 | |||
100 | try { | ||
101 | // Don't recurse another time | ||
102 | const recurseIfNeeded = false | ||
103 | ownerActor = await getOrCreateActorAndServerAndModel(accountAttributedTo.id, 'all', recurseIfNeeded) | ||
104 | } catch (err) { | ||
105 | logger.error('Cannot get or create account attributed to video channel ' + actorUrl) | ||
106 | throw new Error(err) | ||
107 | } | ||
108 | } | ||
109 | |||
110 | actor = await retryTransactionWrapper(saveActorAndServerAndModelIfNotExist, result, ownerActor) | ||
111 | created = true | ||
112 | accountPlaylistsUrl = result.playlists | ||
113 | } | ||
114 | |||
115 | if (actor.Account) (actor as MActorAccountChannelIdActor).Account.Actor = actor | ||
116 | if (actor.VideoChannel) (actor as MActorAccountChannelIdActor).VideoChannel.Actor = actor | ||
117 | |||
118 | const { actor: actorRefreshed, refreshed } = await retryTransactionWrapper(refreshActorIfNeeded, actor, fetchType) | ||
119 | if (!actorRefreshed) throw new Error('Actor ' + actor.url + ' does not exist anymore.') | ||
120 | |||
121 | if ((created === true || refreshed === true) && updateCollections === true) { | ||
122 | const payload = { uri: actor.outboxUrl, type: 'activity' as 'activity' } | ||
123 | await JobQueue.Instance.createJobWithPromise({ type: 'activitypub-http-fetcher', payload }) | ||
124 | } | ||
125 | |||
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' } | ||
129 | await JobQueue.Instance.createJobWithPromise({ type: 'activitypub-http-fetcher', payload }) | ||
130 | } | ||
131 | |||
132 | return actorRefreshed | ||
133 | } | ||
134 | |||
135 | function buildActorInstance (type: ActivityPubActorType, url: string, preferredUsername: string) { | ||
136 | return new ActorModel({ | ||
137 | type, | ||
138 | url, | ||
139 | preferredUsername, | ||
140 | publicKey: null, | ||
141 | privateKey: null, | ||
142 | followersCount: 0, | ||
143 | followingCount: 0, | ||
144 | inboxUrl: url + '/inbox', | ||
145 | outboxUrl: url + '/outbox', | ||
146 | sharedInboxUrl: WEBSERVER.URL + '/inbox', | ||
147 | followersUrl: url + '/followers', | ||
148 | followingUrl: url + '/following' | ||
149 | }) as MActor | ||
150 | } | ||
151 | |||
152 | async function updateActorInstance (actorInstance: ActorModel, attributes: ActivityPubActor) { | ||
153 | const followersCount = await fetchActorTotalItems(attributes.followers) | ||
154 | const followingCount = await fetchActorTotalItems(attributes.following) | ||
155 | |||
156 | actorInstance.type = attributes.type | ||
157 | actorInstance.preferredUsername = attributes.preferredUsername | ||
158 | actorInstance.url = attributes.id | ||
159 | actorInstance.publicKey = attributes.publicKey.publicKeyPem | ||
160 | actorInstance.followersCount = followersCount | ||
161 | actorInstance.followingCount = followingCount | ||
162 | actorInstance.inboxUrl = attributes.inbox | ||
163 | actorInstance.outboxUrl = attributes.outbox | ||
164 | actorInstance.followersUrl = attributes.followers | ||
165 | actorInstance.followingUrl = attributes.following | ||
166 | |||
167 | if (attributes.published) actorInstance.remoteCreatedAt = new Date(attributes.published) | ||
168 | |||
169 | if (attributes.endpoints?.sharedInbox) { | ||
170 | actorInstance.sharedInboxUrl = attributes.endpoints.sharedInbox | ||
171 | } | ||
172 | } | ||
173 | |||
174 | type ImageInfo = { | ||
175 | name: string | ||
176 | fileUrl: string | ||
177 | height: number | ||
178 | width: number | ||
179 | onDisk?: boolean | ||
180 | } | ||
181 | async function updateActorImageInstance (actor: MActorImages, type: ActorImageType, imageInfo: ImageInfo | null, t: Transaction) { | ||
182 | const oldImageModel = type === ActorImageType.AVATAR | ||
183 | ? actor.Avatar | ||
184 | : actor.Banner | ||
185 | |||
186 | if (oldImageModel) { | ||
187 | // Don't update the avatar if the file URL did not change | ||
188 | if (imageInfo?.fileUrl && oldImageModel.fileUrl === imageInfo.fileUrl) return actor | ||
189 | |||
190 | try { | ||
191 | await oldImageModel.destroy({ transaction: t }) | ||
192 | |||
193 | setActorImage(actor, type, null) | ||
194 | } catch (err) { | ||
195 | logger.error('Cannot remove old actor image of actor %s.', actor.url, { err }) | ||
196 | } | ||
197 | } | ||
198 | |||
199 | if (imageInfo) { | ||
200 | const imageModel = await ActorImageModel.create({ | ||
201 | filename: imageInfo.name, | ||
202 | onDisk: imageInfo.onDisk ?? false, | ||
203 | fileUrl: imageInfo.fileUrl, | ||
204 | height: imageInfo.height, | ||
205 | width: imageInfo.width, | ||
206 | type | ||
207 | }, { transaction: t }) | ||
208 | |||
209 | setActorImage(actor, type, imageModel) | ||
210 | } | ||
211 | |||
212 | return actor | ||
213 | } | ||
214 | |||
215 | async function deleteActorImageInstance (actor: MActorImages, type: ActorImageType, t: Transaction) { | ||
216 | try { | ||
217 | if (type === ActorImageType.AVATAR) { | ||
218 | await actor.Avatar.destroy({ transaction: t }) | ||
219 | |||
220 | actor.avatarId = null | ||
221 | actor.Avatar = null | ||
222 | } else { | ||
223 | await actor.Banner.destroy({ transaction: t }) | ||
224 | |||
225 | actor.bannerId = null | ||
226 | actor.Banner = null | ||
227 | } | ||
228 | } catch (err) { | ||
229 | logger.error('Cannot remove old image of actor %s.', actor.url, { err }) | ||
230 | } | ||
231 | |||
232 | return actor | ||
233 | } | ||
234 | |||
235 | async function fetchActorTotalItems (url: string) { | ||
236 | try { | ||
237 | const { body } = await doJSONRequest<ActivityPubOrderedCollection<unknown>>(url, { activityPub: true }) | ||
238 | |||
239 | return body.totalItems || 0 | ||
240 | } catch (err) { | ||
241 | logger.warn('Cannot fetch remote actor count %s.', url, { err }) | ||
242 | return 0 | ||
243 | } | ||
244 | } | ||
245 | |||
246 | function getImageInfoIfExists (actorJSON: ActivityPubActor, type: ActorImageType) { | ||
247 | const mimetypes = MIMETYPES.IMAGE | ||
248 | const icon = type === ActorImageType.AVATAR | ||
249 | ? actorJSON.icon | ||
250 | : actorJSON.image | ||
251 | |||
252 | if (!icon || icon.type !== 'Image' || !isActivityPubUrlValid(icon.url)) return undefined | ||
253 | |||
254 | let extension: string | ||
255 | |||
256 | if (icon.mediaType) { | ||
257 | extension = mimetypes.MIMETYPE_EXT[icon.mediaType] | ||
258 | } else { | ||
259 | const tmp = extname(icon.url) | ||
260 | |||
261 | if (mimetypes.EXT_MIMETYPE[tmp] !== undefined) extension = tmp | ||
262 | } | ||
263 | |||
264 | if (!extension) return undefined | ||
265 | |||
266 | return { | ||
267 | name: uuidv4() + extension, | ||
268 | fileUrl: icon.url, | ||
269 | height: icon.height, | ||
270 | width: icon.width, | ||
271 | type | ||
272 | } | ||
273 | } | ||
274 | |||
275 | async function addFetchOutboxJob (actor: Pick<ActorModel, 'id' | 'outboxUrl'>) { | ||
276 | // Don't fetch ourselves | ||
277 | const serverActor = await getServerActor() | ||
278 | if (serverActor.id === actor.id) { | ||
279 | logger.error('Cannot fetch our own outbox!') | ||
280 | return undefined | ||
281 | } | ||
282 | |||
283 | const payload = { | ||
284 | uri: actor.outboxUrl, | ||
285 | type: 'activity' as 'activity' | ||
286 | } | ||
287 | |||
288 | return JobQueue.Instance.createJob({ type: 'activitypub-http-fetcher', payload }) | ||
289 | } | ||
290 | |||
291 | async function refreshActorIfNeeded <T extends MActorFull | MActorAccountChannelId> ( | ||
292 | actorArg: T, | ||
293 | fetchedType: ActorFetchByUrlType | ||
294 | ): Promise<{ actor: T | MActorFull, refreshed: boolean }> { | ||
295 | if (!actorArg.isOutdated()) return { actor: actorArg, refreshed: false } | ||
296 | |||
297 | // We need more attributes | ||
298 | const actor = fetchedType === 'all' | ||
299 | ? actorArg as MActorFull | ||
300 | : await ActorModel.loadByUrlAndPopulateAccountAndChannel(actorArg.url) | ||
301 | |||
302 | try { | ||
303 | let actorUrl: string | ||
304 | try { | ||
305 | actorUrl = await getUrlFromWebfinger(actor.preferredUsername + '@' + actor.getHost()) | ||
306 | } catch (err) { | ||
307 | logger.warn('Cannot get actor URL from webfinger, keeping the old one.', err) | ||
308 | actorUrl = actor.url | ||
309 | } | ||
310 | |||
311 | const { result } = await fetchRemoteActor(actorUrl) | ||
312 | |||
313 | if (result === undefined) { | ||
314 | logger.warn('Cannot fetch remote actor in refresh actor.') | ||
315 | return { actor, refreshed: false } | ||
316 | } | ||
317 | |||
318 | return sequelizeTypescript.transaction(async t => { | ||
319 | updateInstanceWithAnother(actor, result.actor) | ||
320 | |||
321 | await updateActorImageInstance(actor, ActorImageType.AVATAR, result.avatar, t) | ||
322 | await updateActorImageInstance(actor, ActorImageType.BANNER, result.banner, t) | ||
323 | |||
324 | // Force update | ||
325 | actor.setDataValue('updatedAt', new Date()) | ||
326 | await actor.save({ transaction: t }) | ||
327 | |||
328 | if (actor.Account) { | ||
329 | actor.Account.name = result.name | ||
330 | actor.Account.description = result.summary | ||
331 | |||
332 | await actor.Account.save({ transaction: t }) | ||
333 | } else if (actor.VideoChannel) { | ||
334 | actor.VideoChannel.name = result.name | ||
335 | actor.VideoChannel.description = result.summary | ||
336 | actor.VideoChannel.support = result.support | ||
337 | |||
338 | await actor.VideoChannel.save({ transaction: t }) | ||
339 | } | ||
340 | |||
341 | return { refreshed: true, actor } | ||
342 | }) | ||
343 | } catch (err) { | ||
344 | if ((err as PeerTubeRequestError).statusCode === HttpStatusCode.NOT_FOUND_404) { | ||
345 | logger.info('Deleting actor %s because there is a 404 in refresh actor.', actor.url) | ||
346 | actor.Account | ||
347 | ? await actor.Account.destroy() | ||
348 | : await actor.VideoChannel.destroy() | ||
349 | |||
350 | return { actor: undefined, refreshed: false } | ||
351 | } | ||
352 | |||
353 | logger.warn('Cannot refresh actor %s.', actor.url, { err }) | ||
354 | return { actor, refreshed: false } | ||
355 | } | ||
356 | } | ||
357 | |||
358 | export { | ||
359 | getOrCreateActorAndServerAndModel, | ||
360 | buildActorInstance, | ||
361 | generateAndSaveActorKeys, | ||
362 | fetchActorTotalItems, | ||
363 | getImageInfoIfExists, | ||
364 | updateActorInstance, | ||
365 | deleteActorImageInstance, | ||
366 | refreshActorIfNeeded, | ||
367 | updateActorImageInstance, | ||
368 | addFetchOutboxJob | ||
369 | } | ||
370 | |||
371 | // --------------------------------------------------------------------------- | ||
372 | |||
373 | function setActorImage (actorModel: MActorImages, type: ActorImageType, imageModel: MActorImage) { | ||
374 | const id = imageModel | ||
375 | ? imageModel.id | ||
376 | : null | ||
377 | |||
378 | if (type === ActorImageType.AVATAR) { | ||
379 | actorModel.avatarId = id | ||
380 | actorModel.Avatar = imageModel | ||
381 | } else { | ||
382 | actorModel.bannerId = id | ||
383 | actorModel.Banner = imageModel | ||
384 | } | ||
385 | |||
386 | return actorModel | ||
387 | } | ||
388 | |||
389 | function saveActorAndServerAndModelIfNotExist ( | ||
390 | result: FetchRemoteActorResult, | ||
391 | ownerActor?: MActorFullActor, | ||
392 | t?: Transaction | ||
393 | ): Bluebird<MActorFullActor> | Promise<MActorFullActor> { | ||
394 | const actor = result.actor | ||
395 | |||
396 | if (t !== undefined) return save(t) | ||
397 | |||
398 | return sequelizeTypescript.transaction(t => save(t)) | ||
399 | |||
400 | async function save (t: Transaction) { | ||
401 | const actorHost = new URL(actor.url).host | ||
402 | |||
403 | const serverOptions = { | ||
404 | where: { | ||
405 | host: actorHost | ||
406 | }, | ||
407 | defaults: { | ||
408 | host: actorHost | ||
409 | }, | ||
410 | transaction: t | ||
411 | } | ||
412 | const [ server ] = await ServerModel.findOrCreate(serverOptions) | ||
413 | |||
414 | // Save our new account in database | ||
415 | actor.serverId = server.id | ||
416 | |||
417 | // Avatar? | ||
418 | if (result.avatar) { | ||
419 | const avatar = await ActorImageModel.create({ | ||
420 | filename: result.avatar.name, | ||
421 | fileUrl: result.avatar.fileUrl, | ||
422 | width: result.avatar.width, | ||
423 | height: result.avatar.height, | ||
424 | onDisk: false, | ||
425 | type: ActorImageType.AVATAR | ||
426 | }, { transaction: t }) | ||
427 | |||
428 | actor.avatarId = avatar.id | ||
429 | } | ||
430 | |||
431 | // Banner? | ||
432 | if (result.banner) { | ||
433 | const banner = await ActorImageModel.create({ | ||
434 | filename: result.banner.name, | ||
435 | fileUrl: result.banner.fileUrl, | ||
436 | width: result.banner.width, | ||
437 | height: result.banner.height, | ||
438 | onDisk: false, | ||
439 | type: ActorImageType.BANNER | ||
440 | }, { transaction: t }) | ||
441 | |||
442 | actor.bannerId = banner.id | ||
443 | } | ||
444 | |||
445 | // Force the actor creation, sometimes Sequelize skips the save() when it thinks the instance already exists | ||
446 | // (which could be false in a retried query) | ||
447 | const [ actorCreated, created ] = await ActorModel.findOrCreate<MActorFullActor>({ | ||
448 | defaults: actor.toJSON(), | ||
449 | where: { | ||
450 | [Op.or]: [ | ||
451 | { | ||
452 | url: actor.url | ||
453 | }, | ||
454 | { | ||
455 | serverId: actor.serverId, | ||
456 | preferredUsername: actor.preferredUsername | ||
457 | } | ||
458 | ] | ||
459 | }, | ||
460 | transaction: t | ||
461 | }) | ||
462 | |||
463 | // Try to fix non HTTPS accounts of remote instances that fixed their URL afterwards | ||
464 | if (created !== true && actorCreated.url !== actor.url) { | ||
465 | // Only fix http://example.com/account/djidane to https://example.com/account/djidane | ||
466 | if (actorCreated.url.replace(/^http:\/\//, '') !== actor.url.replace(/^https:\/\//, '')) { | ||
467 | throw new Error(`Actor from DB with URL ${actorCreated.url} does not correspond to actor ${actor.url}`) | ||
468 | } | ||
469 | |||
470 | actorCreated.url = actor.url | ||
471 | await actorCreated.save({ transaction: t }) | ||
472 | } | ||
473 | |||
474 | if (actorCreated.type === 'Person' || actorCreated.type === 'Application') { | ||
475 | actorCreated.Account = await saveAccount(actorCreated, result, t) as MAccountDefault | ||
476 | actorCreated.Account.Actor = actorCreated | ||
477 | } else if (actorCreated.type === 'Group') { // Video channel | ||
478 | const channel = await saveVideoChannel(actorCreated, result, ownerActor, t) | ||
479 | actorCreated.VideoChannel = Object.assign(channel, { Actor: actorCreated, Account: ownerActor.Account }) | ||
480 | } | ||
481 | |||
482 | actorCreated.Server = server | ||
483 | |||
484 | return actorCreated | ||
485 | } | ||
486 | } | ||
487 | |||
488 | type ImageResult = { | ||
489 | name: string | ||
490 | fileUrl: string | ||
491 | height: number | ||
492 | width: number | ||
493 | } | ||
494 | |||
495 | type FetchRemoteActorResult = { | ||
496 | actor: MActor | ||
497 | name: string | ||
498 | summary: string | ||
499 | support?: string | ||
500 | playlists?: string | ||
501 | avatar?: ImageResult | ||
502 | banner?: ImageResult | ||
503 | attributedTo: ActivityPubAttributedTo[] | ||
504 | } | ||
505 | async function fetchRemoteActor (actorUrl: string): Promise<{ statusCode?: number, result: FetchRemoteActorResult }> { | ||
506 | logger.info('Fetching remote actor %s.', actorUrl) | ||
507 | |||
508 | const requestResult = await doJSONRequest<ActivityPubActor>(actorUrl, { activityPub: true }) | ||
509 | const actorJSON = requestResult.body | ||
510 | |||
511 | if (sanitizeAndCheckActorObject(actorJSON) === false) { | ||
512 | logger.debug('Remote actor JSON is not valid.', { actorJSON }) | ||
513 | return { result: undefined, statusCode: requestResult.statusCode } | ||
514 | } | ||
515 | |||
516 | if (checkUrlsSameHost(actorJSON.id, actorUrl) !== true) { | ||
517 | logger.warn('Actor url %s has not the same host than its AP id %s', actorUrl, actorJSON.id) | ||
518 | return { result: undefined, statusCode: requestResult.statusCode } | ||
519 | } | ||
520 | |||
521 | const followersCount = await fetchActorTotalItems(actorJSON.followers) | ||
522 | const followingCount = await fetchActorTotalItems(actorJSON.following) | ||
523 | |||
524 | const actor = new ActorModel({ | ||
525 | type: actorJSON.type, | ||
526 | preferredUsername: actorJSON.preferredUsername, | ||
527 | url: actorJSON.id, | ||
528 | publicKey: actorJSON.publicKey.publicKeyPem, | ||
529 | privateKey: null, | ||
530 | followersCount: followersCount, | ||
531 | followingCount: followingCount, | ||
532 | inboxUrl: actorJSON.inbox, | ||
533 | outboxUrl: actorJSON.outbox, | ||
534 | followersUrl: actorJSON.followers, | ||
535 | followingUrl: actorJSON.following, | ||
536 | |||
537 | sharedInboxUrl: actorJSON.endpoints?.sharedInbox | ||
538 | ? actorJSON.endpoints.sharedInbox | ||
539 | : null | ||
540 | }) | ||
541 | |||
542 | const avatarInfo = getImageInfoIfExists(actorJSON, ActorImageType.AVATAR) | ||
543 | const bannerInfo = getImageInfoIfExists(actorJSON, ActorImageType.BANNER) | ||
544 | |||
545 | const name = actorJSON.name || actorJSON.preferredUsername | ||
546 | return { | ||
547 | statusCode: requestResult.statusCode, | ||
548 | result: { | ||
549 | actor, | ||
550 | name, | ||
551 | avatar: avatarInfo, | ||
552 | banner: bannerInfo, | ||
553 | summary: actorJSON.summary, | ||
554 | support: actorJSON.support, | ||
555 | playlists: actorJSON.playlists, | ||
556 | attributedTo: actorJSON.attributedTo | ||
557 | } | ||
558 | } | ||
559 | } | ||
560 | |||
561 | async function saveAccount (actor: MActorId, result: FetchRemoteActorResult, t: Transaction) { | ||
562 | const [ accountCreated ] = await AccountModel.findOrCreate({ | ||
563 | defaults: { | ||
564 | name: result.name, | ||
565 | description: result.summary, | ||
566 | actorId: actor.id | ||
567 | }, | ||
568 | where: { | ||
569 | actorId: actor.id | ||
570 | }, | ||
571 | transaction: t | ||
572 | }) | ||
573 | |||
574 | return accountCreated as MAccount | ||
575 | } | ||
576 | |||
577 | async function saveVideoChannel (actor: MActorId, result: FetchRemoteActorResult, ownerActor: MActorAccountId, t: Transaction) { | ||
578 | const [ videoChannelCreated ] = await VideoChannelModel.findOrCreate({ | ||
579 | defaults: { | ||
580 | name: result.name, | ||
581 | description: result.summary, | ||
582 | support: result.support, | ||
583 | actorId: actor.id, | ||
584 | accountId: ownerActor.Account.id | ||
585 | }, | ||
586 | where: { | ||
587 | actorId: actor.id | ||
588 | }, | ||
589 | transaction: t | ||
590 | }) | ||
591 | |||
592 | return videoChannelCreated as MChannel | ||
593 | } | ||
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 | } | ||
diff --git a/server/lib/activitypub/outbox.ts b/server/lib/activitypub/outbox.ts new file mode 100644 index 000000000..ecdc33a77 --- /dev/null +++ b/server/lib/activitypub/outbox.ts | |||
@@ -0,0 +1,24 @@ | |||
1 | import { logger } from '@server/helpers/logger' | ||
2 | import { ActorModel } from '@server/models/actor/actor' | ||
3 | import { getServerActor } from '@server/models/application/application' | ||
4 | import { JobQueue } from '../job-queue' | ||
5 | |||
6 | async function addFetchOutboxJob (actor: Pick<ActorModel, 'id' | 'outboxUrl'>) { | ||
7 | // Don't fetch ourselves | ||
8 | const serverActor = await getServerActor() | ||
9 | if (serverActor.id === actor.id) { | ||
10 | logger.error('Cannot fetch our own outbox!') | ||
11 | return undefined | ||
12 | } | ||
13 | |||
14 | const payload = { | ||
15 | uri: actor.outboxUrl, | ||
16 | type: 'activity' as 'activity' | ||
17 | } | ||
18 | |||
19 | return JobQueue.Instance.createJob({ type: 'activitypub-http-fetcher', payload }) | ||
20 | } | ||
21 | |||
22 | export { | ||
23 | addFetchOutboxJob | ||
24 | } | ||
diff --git a/server/lib/activitypub/playlists/create-update.ts b/server/lib/activitypub/playlists/create-update.ts index 886b1f288..fcfcc41a2 100644 --- a/server/lib/activitypub/playlists/create-update.ts +++ b/server/lib/activitypub/playlists/create-update.ts | |||
@@ -9,7 +9,7 @@ import { FilteredModelAttributes } from '@server/types' | |||
9 | import { MAccountDefault, MAccountId, MVideoPlaylist, MVideoPlaylistFull } from '@server/types/models' | 9 | import { MAccountDefault, MAccountId, MVideoPlaylist, MVideoPlaylistFull } from '@server/types/models' |
10 | import { AttributesOnly } from '@shared/core-utils' | 10 | import { AttributesOnly } from '@shared/core-utils' |
11 | import { PlaylistObject } from '@shared/models' | 11 | import { PlaylistObject } from '@shared/models' |
12 | import { getOrCreateActorAndServerAndModel } from '../actor' | 12 | import { getOrCreateAPActor } from '../actors' |
13 | import { crawlCollectionPage } from '../crawl' | 13 | import { crawlCollectionPage } from '../crawl' |
14 | import { getOrCreateAPVideo } from '../videos' | 14 | import { getOrCreateAPVideo } from '../videos' |
15 | import { | 15 | import { |
@@ -75,7 +75,7 @@ export { | |||
75 | async function setVideoChannelIfNeeded (playlistObject: PlaylistObject, playlistAttributes: AttributesOnly<VideoPlaylistModel>) { | 75 | async function setVideoChannelIfNeeded (playlistObject: PlaylistObject, playlistAttributes: AttributesOnly<VideoPlaylistModel>) { |
76 | if (!isArray(playlistObject.attributedTo) || playlistObject.attributedTo.length !== 1) return | 76 | if (!isArray(playlistObject.attributedTo) || playlistObject.attributedTo.length !== 1) return |
77 | 77 | ||
78 | const actor = await getOrCreateActorAndServerAndModel(playlistObject.attributedTo[0]) | 78 | const actor = await getOrCreateAPActor(playlistObject.attributedTo[0]) |
79 | 79 | ||
80 | if (!actor.VideoChannel) { | 80 | if (!actor.VideoChannel) { |
81 | logger.warn('Playlist "attributedTo" %s is not a video channel.', playlistObject.id, { playlistObject, ...lTags(playlistObject.id) }) | 81 | logger.warn('Playlist "attributedTo" %s is not a video channel.', playlistObject.id, { playlistObject, ...lTags(playlistObject.id) }) |
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] |
diff --git a/server/lib/activitypub/share.ts b/server/lib/activitypub/share.ts index 327955dd2..1ff01a175 100644 --- a/server/lib/activitypub/share.ts +++ b/server/lib/activitypub/share.ts | |||
@@ -7,7 +7,7 @@ import { doJSONRequest } from '../../helpers/requests' | |||
7 | import { CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants' | 7 | import { CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants' |
8 | import { VideoShareModel } from '../../models/video/video-share' | 8 | import { VideoShareModel } from '../../models/video/video-share' |
9 | import { MChannelActorLight, MVideo, MVideoAccountLight, MVideoId } from '../../types/models/video' | 9 | import { MChannelActorLight, MVideo, MVideoAccountLight, MVideoId } from '../../types/models/video' |
10 | import { getOrCreateActorAndServerAndModel } from './actor' | 10 | import { getOrCreateAPActor } from './actors' |
11 | import { sendUndoAnnounce, sendVideoAnnounce } from './send' | 11 | import { sendUndoAnnounce, sendVideoAnnounce } from './send' |
12 | import { getLocalVideoAnnounceActivityPubUrl } from './url' | 12 | import { getLocalVideoAnnounceActivityPubUrl } from './url' |
13 | 13 | ||
@@ -64,7 +64,7 @@ async function addVideoShare (shareUrl: string, video: MVideoId) { | |||
64 | throw new Error(`Actor url ${actorUrl} has not the same host than the share url ${shareUrl}`) | 64 | throw new Error(`Actor url ${actorUrl} has not the same host than the share url ${shareUrl}`) |
65 | } | 65 | } |
66 | 66 | ||
67 | const actor = await getOrCreateActorAndServerAndModel(actorUrl) | 67 | const actor = await getOrCreateAPActor(actorUrl) |
68 | 68 | ||
69 | const entry = { | 69 | const entry = { |
70 | actorId: actor.id, | 70 | actorId: actor.id, |
diff --git a/server/lib/activitypub/video-comments.ts b/server/lib/activitypub/video-comments.ts index 760da719d..6b7f9504f 100644 --- a/server/lib/activitypub/video-comments.ts +++ b/server/lib/activitypub/video-comments.ts | |||
@@ -6,7 +6,7 @@ import { doJSONRequest } from '../../helpers/requests' | |||
6 | import { ACTIVITY_PUB, CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants' | 6 | import { ACTIVITY_PUB, CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants' |
7 | import { VideoCommentModel } from '../../models/video/video-comment' | 7 | import { VideoCommentModel } from '../../models/video/video-comment' |
8 | import { MCommentOwner, MCommentOwnerVideo, MVideoAccountLightBlacklistAllFiles } from '../../types/models/video' | 8 | import { MCommentOwner, MCommentOwnerVideo, MVideoAccountLightBlacklistAllFiles } from '../../types/models/video' |
9 | import { getOrCreateActorAndServerAndModel } from './actor' | 9 | import { getOrCreateAPActor } from './actors' |
10 | import { getOrCreateAPVideo } from './videos' | 10 | import { getOrCreateAPVideo } from './videos' |
11 | 11 | ||
12 | type ResolveThreadParams = { | 12 | type ResolveThreadParams = { |
@@ -147,7 +147,7 @@ async function resolveRemoteParentComment (params: ResolveThreadParams) { | |||
147 | } | 147 | } |
148 | 148 | ||
149 | const actor = actorUrl | 149 | const actor = actorUrl |
150 | ? await getOrCreateActorAndServerAndModel(actorUrl, 'all') | 150 | ? await getOrCreateAPActor(actorUrl, 'all') |
151 | : null | 151 | : null |
152 | 152 | ||
153 | const comment = new VideoCommentModel({ | 153 | const comment = new VideoCommentModel({ |
diff --git a/server/lib/activitypub/video-rates.ts b/server/lib/activitypub/video-rates.ts index 091f4ec23..0eec806f9 100644 --- a/server/lib/activitypub/video-rates.ts +++ b/server/lib/activitypub/video-rates.ts | |||
@@ -7,7 +7,7 @@ import { logger } from '../../helpers/logger' | |||
7 | import { CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants' | 7 | import { CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants' |
8 | import { AccountVideoRateModel } from '../../models/account/account-video-rate' | 8 | import { AccountVideoRateModel } from '../../models/account/account-video-rate' |
9 | import { MAccountActor, MActorUrl, MVideo, MVideoAccountLight, MVideoId } from '../../types/models' | 9 | import { MAccountActor, MActorUrl, MVideo, MVideoAccountLight, MVideoId } from '../../types/models' |
10 | import { getOrCreateActorAndServerAndModel } from './actor' | 10 | import { getOrCreateAPActor } from './actors' |
11 | import { sendLike, sendUndoDislike, sendUndoLike } from './send' | 11 | import { sendLike, sendUndoDislike, sendUndoLike } from './send' |
12 | import { sendDislike } from './send/send-dislike' | 12 | import { sendDislike } from './send/send-dislike' |
13 | import { getVideoDislikeActivityPubUrlByLocalActor, getVideoLikeActivityPubUrlByLocalActor } from './url' | 13 | import { getVideoDislikeActivityPubUrlByLocalActor, getVideoLikeActivityPubUrlByLocalActor } from './url' |
@@ -74,7 +74,7 @@ async function createRate (rateUrl: string, video: MVideo, rate: VideoRateType) | |||
74 | throw new Error(`Rate url ${rateUrl} host is different from the AP object id ${body.id}`) | 74 | throw new Error(`Rate url ${rateUrl} host is different from the AP object id ${body.id}`) |
75 | } | 75 | } |
76 | 76 | ||
77 | const actor = await getOrCreateActorAndServerAndModel(actorUrl) | 77 | const actor = await getOrCreateAPActor(actorUrl) |
78 | 78 | ||
79 | const entry = { | 79 | const entry = { |
80 | videoId: video.id, | 80 | videoId: video.id, |
diff --git a/server/lib/activitypub/videos/shared/abstract-builder.ts b/server/lib/activitypub/videos/shared/abstract-builder.ts index 953710f6c..f8e4d6aa3 100644 --- a/server/lib/activitypub/videos/shared/abstract-builder.ts +++ b/server/lib/activitypub/videos/shared/abstract-builder.ts | |||
@@ -10,7 +10,7 @@ import { VideoLiveModel } from '@server/models/video/video-live' | |||
10 | import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist' | 10 | import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist' |
11 | import { MStreamingPlaylistFilesVideo, MThumbnail, MVideoCaption, MVideoFile, MVideoFullLight, MVideoThumbnail } from '@server/types/models' | 11 | import { MStreamingPlaylistFilesVideo, MThumbnail, MVideoCaption, MVideoFile, MVideoFullLight, MVideoThumbnail } from '@server/types/models' |
12 | import { ActivityTagObject, ThumbnailType, VideoObject, VideoStreamingPlaylistType } from '@shared/models' | 12 | import { ActivityTagObject, ThumbnailType, VideoObject, VideoStreamingPlaylistType } from '@shared/models' |
13 | import { getOrCreateActorAndServerAndModel } from '../../actor' | 13 | import { getOrCreateAPActor } from '../../actors' |
14 | import { | 14 | import { |
15 | getCaptionAttributesFromObject, | 15 | getCaptionAttributesFromObject, |
16 | getFileAttributesFromUrl, | 16 | getFileAttributesFromUrl, |
@@ -34,7 +34,7 @@ export abstract class APVideoAbstractBuilder { | |||
34 | throw new Error(`Video channel url ${channel.id} does not have the same host than video object id ${this.videoObject.id}`) | 34 | throw new Error(`Video channel url ${channel.id} does not have the same host than video object id ${this.videoObject.id}`) |
35 | } | 35 | } |
36 | 36 | ||
37 | return getOrCreateActorAndServerAndModel(channel.id, 'all') | 37 | return getOrCreateAPActor(channel.id, 'all') |
38 | } | 38 | } |
39 | 39 | ||
40 | protected tryToGenerateThumbnail (video: MVideoThumbnail): Promise<MThumbnail> { | 40 | protected tryToGenerateThumbnail (video: MVideoThumbnail): Promise<MThumbnail> { |
diff --git a/server/lib/activitypub/videos/updater.ts b/server/lib/activitypub/videos/updater.ts index 9e1c74969..6745e2efd 100644 --- a/server/lib/activitypub/videos/updater.ts +++ b/server/lib/activitypub/videos/updater.ts | |||
@@ -126,7 +126,7 @@ export class APVideoUpdater extends APVideoAbstractBuilder { | |||
126 | this.video.views = videoData.views | 126 | this.video.views = videoData.views |
127 | this.video.isLive = videoData.isLive | 127 | this.video.isLive = videoData.isLive |
128 | 128 | ||
129 | // Ensures we update the updated video attribute | 129 | // Ensures we update the updatedAt attribute, even if main attributes did not change |
130 | this.video.changed('updatedAt', true) | 130 | this.video.changed('updatedAt', true) |
131 | 131 | ||
132 | return this.video.save({ transaction }) as Promise<MVideoFullLight> | 132 | return this.video.save({ transaction }) as Promise<MVideoFullLight> |
diff --git a/server/lib/job-queue/handlers/activitypub-follow.ts b/server/lib/job-queue/handlers/activitypub-follow.ts index ec8df8969..76b6fcaae 100644 --- a/server/lib/job-queue/handlers/activitypub-follow.ts +++ b/server/lib/job-queue/handlers/activitypub-follow.ts | |||
@@ -10,7 +10,7 @@ import { sequelizeTypescript } from '../../../initializers/database' | |||
10 | import { ActorModel } from '../../../models/actor/actor' | 10 | import { ActorModel } from '../../../models/actor/actor' |
11 | import { ActorFollowModel } from '../../../models/actor/actor-follow' | 11 | import { ActorFollowModel } from '../../../models/actor/actor-follow' |
12 | import { MActor, MActorFollowActors, MActorFull } from '../../../types/models' | 12 | import { MActor, MActorFollowActors, MActorFull } from '../../../types/models' |
13 | import { getOrCreateActorAndServerAndModel } from '../../activitypub/actor' | 13 | import { getOrCreateAPActor } from '../../activitypub/actors' |
14 | import { sendFollow } from '../../activitypub/send' | 14 | import { sendFollow } from '../../activitypub/send' |
15 | import { Notifier } from '../../notifier' | 15 | import { Notifier } from '../../notifier' |
16 | 16 | ||
@@ -26,7 +26,7 @@ async function processActivityPubFollow (job: Bull.Job) { | |||
26 | } else { | 26 | } else { |
27 | const sanitizedHost = sanitizeHost(host, REMOTE_SCHEME.HTTP) | 27 | const sanitizedHost = sanitizeHost(host, REMOTE_SCHEME.HTTP) |
28 | const actorUrl = await loadActorUrlOrGetFromWebfinger(payload.name + '@' + sanitizedHost) | 28 | const actorUrl = await loadActorUrlOrGetFromWebfinger(payload.name + '@' + sanitizedHost) |
29 | targetActor = await getOrCreateActorAndServerAndModel(actorUrl, 'all') | 29 | targetActor = await getOrCreateAPActor(actorUrl, 'all') |
30 | } | 30 | } |
31 | 31 | ||
32 | if (payload.assertIsChannel && !targetActor.VideoChannel) { | 32 | if (payload.assertIsChannel && !targetActor.VideoChannel) { |
diff --git a/server/lib/job-queue/handlers/activitypub-refresher.ts b/server/lib/job-queue/handlers/activitypub-refresher.ts index 10e6895da..29483f310 100644 --- a/server/lib/job-queue/handlers/activitypub-refresher.ts +++ b/server/lib/job-queue/handlers/activitypub-refresher.ts | |||
@@ -6,7 +6,7 @@ import { logger } from '../../../helpers/logger' | |||
6 | import { fetchVideoByUrl } from '../../../helpers/video' | 6 | import { fetchVideoByUrl } from '../../../helpers/video' |
7 | import { ActorModel } from '../../../models/actor/actor' | 7 | import { ActorModel } from '../../../models/actor/actor' |
8 | import { VideoPlaylistModel } from '../../../models/video/video-playlist' | 8 | import { VideoPlaylistModel } from '../../../models/video/video-playlist' |
9 | import { refreshActorIfNeeded } from '../../activitypub/actor' | 9 | import { refreshActorIfNeeded } from '../../activitypub/actors' |
10 | 10 | ||
11 | async function refreshAPObject (job: Bull.Job) { | 11 | async function refreshAPObject (job: Bull.Job) { |
12 | const payload = job.data as RefreshPayload | 12 | const payload = job.data as RefreshPayload |
diff --git a/server/lib/job-queue/handlers/actor-keys.ts b/server/lib/job-queue/handlers/actor-keys.ts index 3eef565d0..60ac61afd 100644 --- a/server/lib/job-queue/handlers/actor-keys.ts +++ b/server/lib/job-queue/handlers/actor-keys.ts | |||
@@ -1,5 +1,5 @@ | |||
1 | import * as Bull from 'bull' | 1 | import * as Bull from 'bull' |
2 | import { generateAndSaveActorKeys } from '@server/lib/activitypub/actor' | 2 | import { generateAndSaveActorKeys } from '@server/lib/activitypub/actors' |
3 | import { ActorModel } from '@server/models/actor/actor' | 3 | import { ActorModel } from '@server/models/actor/actor' |
4 | import { ActorKeysPayload } from '@shared/models' | 4 | import { ActorKeysPayload } from '@shared/models' |
5 | import { logger } from '../../../helpers/logger' | 5 | import { logger } from '../../../helpers/logger' |
diff --git a/server/lib/actor-image.ts b/server/lib/local-actor.ts index f271f0b5b..55e77dd04 100644 --- a/server/lib/actor-image.ts +++ b/server/lib/local-actor.ts | |||
@@ -3,17 +3,35 @@ import { queue } from 'async' | |||
3 | import * as LRUCache from 'lru-cache' | 3 | import * as LRUCache from 'lru-cache' |
4 | import { extname, join } from 'path' | 4 | import { extname, join } from 'path' |
5 | import { v4 as uuidv4 } from 'uuid' | 5 | import { v4 as uuidv4 } from 'uuid' |
6 | import { ActorImageType } from '@shared/models' | 6 | import { ActorModel } from '@server/models/actor/actor' |
7 | import { ActivityPubActorType, ActorImageType } from '@shared/models' | ||
7 | import { retryTransactionWrapper } from '../helpers/database-utils' | 8 | import { retryTransactionWrapper } from '../helpers/database-utils' |
8 | import { processImage } from '../helpers/image-utils' | 9 | import { processImage } from '../helpers/image-utils' |
9 | import { downloadImage } from '../helpers/requests' | 10 | import { downloadImage } from '../helpers/requests' |
10 | import { CONFIG } from '../initializers/config' | 11 | import { CONFIG } from '../initializers/config' |
11 | import { ACTOR_IMAGES_SIZE, LRU_CACHE, QUEUE_CONCURRENCY } from '../initializers/constants' | 12 | import { ACTOR_IMAGES_SIZE, LRU_CACHE, QUEUE_CONCURRENCY, WEBSERVER } from '../initializers/constants' |
12 | import { sequelizeTypescript } from '../initializers/database' | 13 | import { sequelizeTypescript } from '../initializers/database' |
13 | import { MAccountDefault, MChannelDefault } from '../types/models' | 14 | import { MAccountDefault, MActor, MChannelDefault } from '../types/models' |
14 | import { deleteActorImageInstance, updateActorImageInstance } from './activitypub/actor' | 15 | import { deleteActorImageInstance, updateActorImageInstance } from './activitypub/actors' |
15 | import { sendUpdateActor } from './activitypub/send' | 16 | import { sendUpdateActor } from './activitypub/send' |
16 | 17 | ||
18 | function buildActorInstance (type: ActivityPubActorType, url: string, preferredUsername: string) { | ||
19 | return new ActorModel({ | ||
20 | type, | ||
21 | url, | ||
22 | preferredUsername, | ||
23 | publicKey: null, | ||
24 | privateKey: null, | ||
25 | followersCount: 0, | ||
26 | followingCount: 0, | ||
27 | inboxUrl: url + '/inbox', | ||
28 | outboxUrl: url + '/outbox', | ||
29 | sharedInboxUrl: WEBSERVER.URL + '/inbox', | ||
30 | followersUrl: url + '/followers', | ||
31 | followingUrl: url + '/following' | ||
32 | }) as MActor | ||
33 | } | ||
34 | |||
17 | async function updateLocalActorImageFile ( | 35 | async function updateLocalActorImageFile ( |
18 | accountOrChannel: MAccountDefault | MChannelDefault, | 36 | accountOrChannel: MAccountDefault | MChannelDefault, |
19 | imagePhysicalFile: Express.Multer.File, | 37 | imagePhysicalFile: Express.Multer.File, |
@@ -93,5 +111,6 @@ export { | |||
93 | actorImagePathUnsafeCache, | 111 | actorImagePathUnsafeCache, |
94 | updateLocalActorImageFile, | 112 | updateLocalActorImageFile, |
95 | deleteLocalActorImageFile, | 113 | deleteLocalActorImageFile, |
96 | pushActorImageProcessInQueue | 114 | pushActorImageProcessInQueue, |
115 | buildActorInstance | ||
97 | } | 116 | } |
diff --git a/server/lib/user.ts b/server/lib/user.ts index 8a6fcebc7..a2163abb1 100644 --- a/server/lib/user.ts +++ b/server/lib/user.ts | |||
@@ -11,10 +11,11 @@ import { ActorModel } from '../models/actor/actor' | |||
11 | import { UserNotificationSettingModel } from '../models/user/user-notification-setting' | 11 | import { UserNotificationSettingModel } from '../models/user/user-notification-setting' |
12 | import { MAccountDefault, MChannelActor } from '../types/models' | 12 | import { MAccountDefault, MChannelActor } from '../types/models' |
13 | import { MUser, MUserDefault, MUserId } from '../types/models/user' | 13 | import { MUser, MUserDefault, MUserId } from '../types/models/user' |
14 | import { buildActorInstance, generateAndSaveActorKeys } from './activitypub/actor' | 14 | import { generateAndSaveActorKeys } from './activitypub/actors' |
15 | import { getLocalAccountActivityPubUrl } from './activitypub/url' | 15 | import { getLocalAccountActivityPubUrl } from './activitypub/url' |
16 | import { Emailer } from './emailer' | 16 | import { Emailer } from './emailer' |
17 | import { LiveManager } from './live-manager' | 17 | import { LiveManager } from './live-manager' |
18 | import { buildActorInstance } from './local-actor' | ||
18 | import { Redis } from './redis' | 19 | import { Redis } from './redis' |
19 | import { createLocalVideoChannel } from './video-channel' | 20 | import { createLocalVideoChannel } from './video-channel' |
20 | import { createWatchLaterPlaylist } from './video-playlist' | 21 | import { createWatchLaterPlaylist } from './video-playlist' |
diff --git a/server/lib/video-channel.ts b/server/lib/video-channel.ts index d57e832fe..2fd63a8c4 100644 --- a/server/lib/video-channel.ts +++ b/server/lib/video-channel.ts | |||
@@ -3,9 +3,9 @@ import { VideoChannelCreate } from '../../shared/models' | |||
3 | import { VideoModel } from '../models/video/video' | 3 | import { VideoModel } from '../models/video/video' |
4 | import { VideoChannelModel } from '../models/video/video-channel' | 4 | import { VideoChannelModel } from '../models/video/video-channel' |
5 | import { MAccountId, MChannelId } from '../types/models' | 5 | import { MAccountId, MChannelId } from '../types/models' |
6 | import { buildActorInstance } from './activitypub/actor' | ||
7 | import { getLocalVideoChannelActivityPubUrl } from './activitypub/url' | 6 | import { getLocalVideoChannelActivityPubUrl } from './activitypub/url' |
8 | import { federateVideoIfNeeded } from './activitypub/videos' | 7 | import { federateVideoIfNeeded } from './activitypub/videos' |
8 | import { buildActorInstance } from './local-actor' | ||
9 | 9 | ||
10 | async function createLocalVideoChannel (videoChannelInfo: VideoChannelCreate, account: MAccountId, t: Sequelize.Transaction) { | 10 | async function createLocalVideoChannel (videoChannelInfo: VideoChannelCreate, account: MAccountId, t: Sequelize.Transaction) { |
11 | const url = getLocalVideoChannelActivityPubUrl(videoChannelInfo.name) | 11 | const url = getLocalVideoChannelActivityPubUrl(videoChannelInfo.name) |
diff --git a/server/middlewares/activitypub.ts b/server/middlewares/activitypub.ts index 6cd23f230..a1fdfafcf 100644 --- a/server/middlewares/activitypub.ts +++ b/server/middlewares/activitypub.ts | |||
@@ -3,7 +3,7 @@ import { ActivityDelete, ActivityPubSignature } from '../../shared' | |||
3 | import { logger } from '../helpers/logger' | 3 | import { logger } from '../helpers/logger' |
4 | import { isHTTPSignatureVerified, isJsonLDSignatureVerified, parseHTTPSignature } from '../helpers/peertube-crypto' | 4 | import { isHTTPSignatureVerified, isJsonLDSignatureVerified, parseHTTPSignature } from '../helpers/peertube-crypto' |
5 | import { ACCEPT_HEADERS, ACTIVITY_PUB, HTTP_SIGNATURE } from '../initializers/constants' | 5 | import { ACCEPT_HEADERS, ACTIVITY_PUB, HTTP_SIGNATURE } from '../initializers/constants' |
6 | import { getOrCreateActorAndServerAndModel } from '../lib/activitypub/actor' | 6 | import { getOrCreateAPActor } from '../lib/activitypub/actors' |
7 | import { loadActorUrlOrGetFromWebfinger } from '../helpers/webfinger' | 7 | import { loadActorUrlOrGetFromWebfinger } from '../helpers/webfinger' |
8 | import { isActorDeleteActivityValid } from '@server/helpers/custom-validators/activitypub/actor' | 8 | import { isActorDeleteActivityValid } from '@server/helpers/custom-validators/activitypub/actor' |
9 | import { getAPId } from '@server/helpers/activitypub' | 9 | import { getAPId } from '@server/helpers/activitypub' |
@@ -100,7 +100,7 @@ async function checkHttpSignature (req: Request, res: Response) { | |||
100 | actorUrl = await loadActorUrlOrGetFromWebfinger(actorUrl.replace(/^acct:/, '')) | 100 | actorUrl = await loadActorUrlOrGetFromWebfinger(actorUrl.replace(/^acct:/, '')) |
101 | } | 101 | } |
102 | 102 | ||
103 | const actor = await getOrCreateActorAndServerAndModel(actorUrl) | 103 | const actor = await getOrCreateAPActor(actorUrl) |
104 | 104 | ||
105 | const verified = isHTTPSignatureVerified(parsed, actor) | 105 | const verified = isHTTPSignatureVerified(parsed, actor) |
106 | if (verified !== true) { | 106 | if (verified !== true) { |
@@ -135,7 +135,7 @@ async function checkJsonLDSignature (req: Request, res: Response) { | |||
135 | 135 | ||
136 | logger.debug('Checking JsonLD signature of actor %s...', creator) | 136 | logger.debug('Checking JsonLD signature of actor %s...', creator) |
137 | 137 | ||
138 | const actor = await getOrCreateActorAndServerAndModel(creator) | 138 | const actor = await getOrCreateAPActor(creator) |
139 | const verified = await isJsonLDSignatureVerified(actor, req.body) | 139 | const verified = await isJsonLDSignatureVerified(actor, req.body) |
140 | 140 | ||
141 | if (verified !== true) { | 141 | if (verified !== true) { |