]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-update.ts
Merge branch 'release/1.4.0' into develop
[github/Chocobozzz/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 { getAvatarInfoIfExists, 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 import { PlaylistObject } from '../../../../shared/models/activitypub/objects/playlist-object'
16 import { createOrUpdateVideoPlaylist } from '../playlist'
17 import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
18 import { MActorSignature, MAccountIdActor } from '../../../typings/models'
19
20 async function processUpdateActivity (options: APProcessorOptions<ActivityUpdate>) {
21 const { activity, byActor } = options
22
23 const objectType = activity.object.type
24
25 if (objectType === 'Video') {
26 return retryTransactionWrapper(processUpdateVideo, byActor, activity)
27 }
28
29 if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') {
30 // We need more attributes
31 const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
32 return retryTransactionWrapper(processUpdateActor, byActorFull, activity)
33 }
34
35 if (objectType === 'CacheFile') {
36 // We need more attributes
37 const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
38 return retryTransactionWrapper(processUpdateCacheFile, byActorFull, activity)
39 }
40
41 if (objectType === 'Playlist') {
42 return retryTransactionWrapper(processUpdatePlaylist, byActor, activity)
43 }
44
45 return undefined
46 }
47
48 // ---------------------------------------------------------------------------
49
50 export {
51 processUpdateActivity
52 }
53
54 // ---------------------------------------------------------------------------
55
56 async function processUpdateVideo (actor: MActorSignature, activity: ActivityUpdate) {
57 const videoObject = activity.object as VideoTorrentObject
58
59 if (sanitizeAndCheckVideoTorrentObject(videoObject) === false) {
60 logger.debug('Video sent by update is not valid.', { videoObject })
61 return undefined
62 }
63
64 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoObject.id, allowRefresh: false, fetchType: 'all' })
65 const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject)
66
67 const account = actor.Account as MAccountIdActor
68 account.Actor = actor
69
70 const updateOptions = {
71 video,
72 videoObject,
73 account,
74 channel: channelActor.VideoChannel,
75 overrideTo: activity.to
76 }
77 return updateVideoFromAP(updateOptions)
78 }
79
80 async function processUpdateCacheFile (byActor: MActorSignature, activity: ActivityUpdate) {
81 const cacheFileObject = activity.object as CacheFileObject
82
83 if (!isCacheFileObjectValid(cacheFileObject)) {
84 logger.debug('Cache file object sent by update is not valid.', { cacheFileObject })
85 return undefined
86 }
87
88 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object })
89
90 await sequelizeTypescript.transaction(async t => {
91 await createOrUpdateCacheFile(cacheFileObject, video, byActor, t)
92 })
93
94 if (video.isOwned()) {
95 // Don't resend the activity to the sender
96 const exceptions = [ byActor ]
97
98 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
99 }
100 }
101
102 async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) {
103 const actorAttributesToUpdate = activity.object as ActivityPubActor
104
105 logger.debug('Updating remote account "%s".', actorAttributesToUpdate.url)
106 let accountOrChannelInstance: AccountModel | VideoChannelModel
107 let actorFieldsSave: object
108 let accountOrChannelFieldsSave: object
109
110 // Fetch icon?
111 const avatarInfo = await getAvatarInfoIfExists(actorAttributesToUpdate)
112
113 try {
114 await sequelizeTypescript.transaction(async t => {
115 actorFieldsSave = actor.toJSON()
116
117 if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel
118 else accountOrChannelInstance = actor.Account
119
120 accountOrChannelFieldsSave = accountOrChannelInstance.toJSON()
121
122 await updateActorInstance(actor, actorAttributesToUpdate)
123
124 if (avatarInfo !== undefined) {
125 const avatarOptions = Object.assign({}, avatarInfo, { onDisk: false })
126
127 await updateActorAvatarInstance(actor, avatarOptions, t)
128 }
129
130 await actor.save({ transaction: t })
131
132 accountOrChannelInstance.name = actorAttributesToUpdate.name || actorAttributesToUpdate.preferredUsername
133 accountOrChannelInstance.description = actorAttributesToUpdate.summary
134
135 if (accountOrChannelInstance instanceof VideoChannelModel) accountOrChannelInstance.support = actorAttributesToUpdate.support
136
137 await accountOrChannelInstance.save({ transaction: t })
138 })
139
140 logger.info('Remote account %s updated', actorAttributesToUpdate.url)
141 } catch (err) {
142 if (actor !== undefined && actorFieldsSave !== undefined) {
143 resetSequelizeInstance(actor, actorFieldsSave)
144 }
145
146 if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) {
147 resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave)
148 }
149
150 // This is just a debug because we will retry the insert
151 logger.debug('Cannot update the remote account.', { err })
152 throw err
153 }
154 }
155
156 async function processUpdatePlaylist (byActor: MActorSignature, activity: ActivityUpdate) {
157 const playlistObject = activity.object as PlaylistObject
158 const byAccount = byActor.Account
159
160 if (!byAccount) throw new Error('Cannot update video playlist with the non account actor ' + byActor.url)
161
162 await createOrUpdateVideoPlaylist(playlistObject, byAccount, activity.to)
163 }