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