]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-update.ts
Add ability to search video channels
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-update.ts
1 import { ActivityUpdate, VideoTorrentObject } from '../../../../shared/models/activitypub'
2 import { ActivityPubActor } from '../../../../shared/models/activitypub/activitypub-actor'
3 import { resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers/database-utils'
4 import { logger } from '../../../helpers/logger'
5 import { sequelizeTypescript } from '../../../initializers'
6 import { AccountModel } from '../../../models/account/account'
7 import { ActorModel } from '../../../models/activitypub/actor'
8 import { VideoChannelModel } from '../../../models/video/video-channel'
9 import { fetchAvatarIfExists, getOrCreateActorAndServerAndModel, updateActorAvatarInstance, updateActorInstance } from '../actor'
10 import { getOrCreateVideoAndAccountAndChannel, getOrCreateVideoChannelFromVideoObject, updateVideoFromAP } from '../videos'
11 import { sanitizeAndCheckVideoTorrentObject } from '../../../helpers/custom-validators/activitypub/videos'
12
13 async function processUpdateActivity (activity: ActivityUpdate) {
14 const actor = await getOrCreateActorAndServerAndModel(activity.actor)
15 const objectType = activity.object.type
16
17 if (objectType === 'Video') {
18 return retryTransactionWrapper(processUpdateVideo, actor, activity)
19 } else if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') {
20 return retryTransactionWrapper(processUpdateActor, actor, activity)
21 }
22
23 return undefined
24 }
25
26 // ---------------------------------------------------------------------------
27
28 export {
29 processUpdateActivity
30 }
31
32 // ---------------------------------------------------------------------------
33
34 async function processUpdateVideo (actor: ActorModel, activity: ActivityUpdate) {
35 const videoObject = activity.object as VideoTorrentObject
36
37 if (sanitizeAndCheckVideoTorrentObject(videoObject) === false) {
38 logger.debug('Video sent by update is not valid.', { videoObject })
39 return undefined
40 }
41
42 const { video } = await getOrCreateVideoAndAccountAndChannel(videoObject.id)
43 const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject)
44
45 return updateVideoFromAP(video, videoObject, actor, channelActor, activity.to)
46 }
47
48 async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) {
49 const actorAttributesToUpdate = activity.object as ActivityPubActor
50
51 logger.debug('Updating remote account "%s".', actorAttributesToUpdate.uuid)
52 let accountOrChannelInstance: AccountModel | VideoChannelModel
53 let actorFieldsSave: object
54 let accountOrChannelFieldsSave: object
55
56 // Fetch icon?
57 const avatarName = await fetchAvatarIfExists(actorAttributesToUpdate)
58
59 try {
60 await sequelizeTypescript.transaction(async t => {
61 actorFieldsSave = actor.toJSON()
62
63 if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel
64 else accountOrChannelInstance = actor.Account
65
66 accountOrChannelFieldsSave = accountOrChannelInstance.toJSON()
67
68 await updateActorInstance(actor, actorAttributesToUpdate)
69
70 if (avatarName !== undefined) {
71 await updateActorAvatarInstance(actor, avatarName, t)
72 }
73
74 await actor.save({ transaction: t })
75
76 accountOrChannelInstance.set('name', actorAttributesToUpdate.name || actorAttributesToUpdate.preferredUsername)
77 accountOrChannelInstance.set('description', actorAttributesToUpdate.summary)
78 accountOrChannelInstance.set('support', actorAttributesToUpdate.support)
79 await accountOrChannelInstance.save({ transaction: t })
80 })
81
82 logger.info('Remote account with uuid %s updated', actorAttributesToUpdate.uuid)
83 } catch (err) {
84 if (actor !== undefined && actorFieldsSave !== undefined) {
85 resetSequelizeInstance(actor, actorFieldsSave)
86 }
87
88 if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) {
89 resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave)
90 }
91
92 // This is just a debug because we will retry the insert
93 logger.debug('Cannot update the remote account.', { err })
94 throw err
95 }
96 }