Merge branch 'release/v1.0.0' into develop
[oweals/peertube.git] / server / lib / activitypub / process / process-update.ts
1 import { ActivityUpdate, CacheFileObject, 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, updateActorAvatarInstance, updateActorInstance } from '../actor'
10 import { getOrCreateVideoAndAccountAndChannel, getOrCreateVideoChannelFromVideoObject, updateVideoFromAP } from '../videos'
11 import { sanitizeAndCheckVideoTorrentObject } from '../../../helpers/custom-validators/activitypub/videos'
12 import { isCacheFileObjectValid } from '../../../helpers/custom-validators/activitypub/cache-file'
13 import { createOrUpdateCacheFile } from '../cache-file'
14 import { forwardVideoRelatedActivity } from '../send/utils'
15
16 async function processUpdateActivity (activity: ActivityUpdate, byActor: ActorModel) {
17   const objectType = activity.object.type
18
19   if (objectType === 'Video') {
20     return retryTransactionWrapper(processUpdateVideo, byActor, activity)
21   }
22
23   if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') {
24     // We need more attributes
25     const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
26     return retryTransactionWrapper(processUpdateActor, byActorFull, activity)
27   }
28
29   if (objectType === 'CacheFile') {
30     // We need more attributes
31     const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
32     return retryTransactionWrapper(processUpdateCacheFile, byActorFull, activity)
33   }
34
35   return undefined
36 }
37
38 // ---------------------------------------------------------------------------
39
40 export {
41   processUpdateActivity
42 }
43
44 // ---------------------------------------------------------------------------
45
46 async function processUpdateVideo (actor: ActorModel, activity: ActivityUpdate) {
47   const videoObject = activity.object as VideoTorrentObject
48
49   if (sanitizeAndCheckVideoTorrentObject(videoObject) === false) {
50     logger.debug('Video sent by update is not valid.', { videoObject })
51     return undefined
52   }
53
54   const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoObject.id })
55   const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject)
56
57   const updateOptions = {
58     video,
59     videoObject,
60     account: actor.Account,
61     channel: channelActor.VideoChannel,
62     updateViews: true,
63     overrideTo: activity.to
64   }
65   return updateVideoFromAP(updateOptions)
66 }
67
68 async function processUpdateCacheFile (byActor: ActorModel, activity: ActivityUpdate) {
69   const cacheFileObject = activity.object as CacheFileObject
70
71   if (!isCacheFileObjectValid(cacheFileObject)) {
72     logger.debug('Cache file object sent by update is not valid.', { cacheFileObject })
73     return undefined
74   }
75
76   const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object })
77
78   await sequelizeTypescript.transaction(async t => {
79     await createOrUpdateCacheFile(cacheFileObject, video, byActor, t)
80   })
81
82   if (video.isOwned()) {
83     // Don't resend the activity to the sender
84     const exceptions = [ byActor ]
85
86     await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
87   }
88 }
89
90 async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) {
91   const actorAttributesToUpdate = activity.object as ActivityPubActor
92
93   logger.debug('Updating remote account "%s".', actorAttributesToUpdate.uuid)
94   let accountOrChannelInstance: AccountModel | VideoChannelModel
95   let actorFieldsSave: object
96   let accountOrChannelFieldsSave: object
97
98   // Fetch icon?
99   const avatarName = await fetchAvatarIfExists(actorAttributesToUpdate)
100
101   try {
102     await sequelizeTypescript.transaction(async t => {
103       actorFieldsSave = actor.toJSON()
104
105       if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel
106       else accountOrChannelInstance = actor.Account
107
108       accountOrChannelFieldsSave = accountOrChannelInstance.toJSON()
109
110       await updateActorInstance(actor, actorAttributesToUpdate)
111
112       if (avatarName !== undefined) {
113         await updateActorAvatarInstance(actor, avatarName, t)
114       }
115
116       await actor.save({ transaction: t })
117
118       accountOrChannelInstance.set('name', actorAttributesToUpdate.name || actorAttributesToUpdate.preferredUsername)
119       accountOrChannelInstance.set('description', actorAttributesToUpdate.summary)
120       accountOrChannelInstance.set('support', actorAttributesToUpdate.support)
121       await accountOrChannelInstance.save({ transaction: t })
122     })
123
124     logger.info('Remote account with uuid %s updated', actorAttributesToUpdate.uuid)
125   } catch (err) {
126     if (actor !== undefined && actorFieldsSave !== undefined) {
127       resetSequelizeInstance(actor, actorFieldsSave)
128     }
129
130     if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) {
131       resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave)
132     }
133
134     // This is just a debug because we will retry the insert
135     logger.debug('Cannot update the remote account.', { err })
136     throw err
137   }
138 }