]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-create.ts
Basic video redundancy implementation
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-create.ts
1 import { ActivityCreate, CacheFileObject, VideoAbuseState, VideoTorrentObject } from '../../../../shared'
2 import { DislikeObject, VideoAbuseObject, ViewObject } from '../../../../shared/models/activitypub/objects'
3 import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object'
4 import { retryTransactionWrapper } from '../../../helpers/database-utils'
5 import { logger } from '../../../helpers/logger'
6 import { sequelizeTypescript } from '../../../initializers'
7 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
8 import { ActorModel } from '../../../models/activitypub/actor'
9 import { VideoAbuseModel } from '../../../models/video/video-abuse'
10 import { getOrCreateActorAndServerAndModel } from '../actor'
11 import { addVideoComment, resolveThread } from '../video-comments'
12 import { getOrCreateVideoAndAccountAndChannel } from '../videos'
13 import { forwardActivity, forwardVideoRelatedActivity } from '../send/utils'
14 import { Redis } from '../../redis'
15 import { createCacheFile } from '../cache-file'
16
17 async function processCreateActivity (activity: ActivityCreate) {
18 const activityObject = activity.object
19 const activityType = activityObject.type
20 const actor = await getOrCreateActorAndServerAndModel(activity.actor)
21
22 if (activityType === 'View') {
23 return processCreateView(actor, activity)
24 } else if (activityType === 'Dislike') {
25 return retryTransactionWrapper(processCreateDislike, actor, activity)
26 } else if (activityType === 'Video') {
27 return processCreateVideo(activity)
28 } else if (activityType === 'Flag') {
29 return retryTransactionWrapper(processCreateVideoAbuse, actor, activityObject as VideoAbuseObject)
30 } else if (activityType === 'Note') {
31 return retryTransactionWrapper(processCreateVideoComment, actor, activity)
32 } else if (activityType === 'CacheFile') {
33 return retryTransactionWrapper(processCacheFile, actor, activity)
34 }
35
36 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
37 return Promise.resolve(undefined)
38 }
39
40 // ---------------------------------------------------------------------------
41
42 export {
43 processCreateActivity
44 }
45
46 // ---------------------------------------------------------------------------
47
48 async function processCreateVideo (activity: ActivityCreate) {
49 const videoToCreateData = activity.object as VideoTorrentObject
50
51 const { video } = await getOrCreateVideoAndAccountAndChannel(videoToCreateData)
52
53 return video
54 }
55
56 async function processCreateDislike (byActor: ActorModel, activity: ActivityCreate) {
57 const dislike = activity.object as DislikeObject
58 const byAccount = byActor.Account
59
60 if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url)
61
62 const { video } = await getOrCreateVideoAndAccountAndChannel(dislike.object)
63
64 return sequelizeTypescript.transaction(async t => {
65 const rate = {
66 type: 'dislike' as 'dislike',
67 videoId: video.id,
68 accountId: byAccount.id
69 }
70 const [ , created ] = await AccountVideoRateModel.findOrCreate({
71 where: rate,
72 defaults: rate,
73 transaction: t
74 })
75 if (created === true) await video.increment('dislikes', { transaction: t })
76
77 if (video.isOwned() && created === true) {
78 // Don't resend the activity to the sender
79 const exceptions = [ byActor ]
80
81 await forwardVideoRelatedActivity(activity, t, exceptions, video)
82 }
83 })
84 }
85
86 async function processCreateView (byActor: ActorModel, activity: ActivityCreate) {
87 const view = activity.object as ViewObject
88
89 const { video } = await getOrCreateVideoAndAccountAndChannel(view.object)
90
91 const actor = await ActorModel.loadByUrl(view.actor)
92 if (!actor) throw new Error('Unknown actor ' + view.actor)
93
94 await Redis.Instance.addVideoView(video.id)
95
96 if (video.isOwned()) {
97 // Don't resend the activity to the sender
98 const exceptions = [ byActor ]
99 await forwardActivity(activity, undefined, exceptions)
100 }
101 }
102
103 async function processCacheFile (byActor: ActorModel, activity: ActivityCreate) {
104 const cacheFile = activity.object as CacheFileObject
105
106 const { video } = await getOrCreateVideoAndAccountAndChannel(cacheFile.object)
107
108 await createCacheFile(cacheFile, video, byActor)
109
110 if (video.isOwned()) {
111 // Don't resend the activity to the sender
112 const exceptions = [ byActor ]
113 await forwardActivity(activity, undefined, exceptions)
114 }
115 }
116
117 async function processCreateVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) {
118 logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object)
119
120 const account = actor.Account
121 if (!account) throw new Error('Cannot create dislike with the non account actor ' + actor.url)
122
123 const { video } = await getOrCreateVideoAndAccountAndChannel(videoAbuseToCreateData.object)
124
125 return sequelizeTypescript.transaction(async t => {
126 const videoAbuseData = {
127 reporterAccountId: account.id,
128 reason: videoAbuseToCreateData.content,
129 videoId: video.id,
130 state: VideoAbuseState.PENDING
131 }
132
133 await VideoAbuseModel.create(videoAbuseData, { transaction: t })
134
135 logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object)
136 })
137 }
138
139 async function processCreateVideoComment (byActor: ActorModel, activity: ActivityCreate) {
140 const commentObject = activity.object as VideoCommentObject
141 const byAccount = byActor.Account
142
143 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
144
145 const { video } = await resolveThread(commentObject.inReplyTo)
146
147 const { created } = await addVideoComment(video, commentObject.id)
148
149 if (video.isOwned() && created === true) {
150 // Don't resend the activity to the sender
151 const exceptions = [ byActor ]
152
153 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
154 }
155 }