]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-create.ts
Add shares forward and collection on videos/video channels
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-create.ts
1 import { ActivityCreate, VideoChannelObject } from '../../../../shared'
2 import { DislikeObject } from '../../../../shared/models/activitypub/objects/dislike-object'
3 import { VideoAbuseObject } from '../../../../shared/models/activitypub/objects/video-abuse-object'
4 import { ViewObject } from '../../../../shared/models/activitypub/objects/view-object'
5 import { logger, retryTransactionWrapper } from '../../../helpers'
6 import { database as db } from '../../../initializers'
7 import { AccountInstance } from '../../../models/account/account-interface'
8 import { getOrCreateAccountAndServer } from '../account'
9 import { forwardActivity } from '../send/misc'
10 import { getVideoChannelActivityPubUrl } from '../url'
11 import { addVideoChannelShares, videoChannelActivityObjectToDBAttributes } from './misc'
12
13 async function processCreateActivity (activity: ActivityCreate) {
14 const activityObject = activity.object
15 const activityType = activityObject.type
16 const account = await getOrCreateAccountAndServer(activity.actor)
17
18 if (activityType === 'View') {
19 return processCreateView(account, activity)
20 } else if (activityType === 'Dislike') {
21 return processCreateDislike(account, activity)
22 } else if (activityType === 'VideoChannel') {
23 return processCreateVideoChannel(account, activityObject as VideoChannelObject)
24 } else if (activityType === 'Flag') {
25 return processCreateVideoAbuse(account, activityObject as VideoAbuseObject)
26 }
27
28 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
29 return Promise.resolve(undefined)
30 }
31
32 // ---------------------------------------------------------------------------
33
34 export {
35 processCreateActivity
36 }
37
38 // ---------------------------------------------------------------------------
39
40 async function processCreateDislike (byAccount: AccountInstance, activity: ActivityCreate) {
41 const options = {
42 arguments: [ byAccount, activity ],
43 errorMessage: 'Cannot dislike the video with many retries.'
44 }
45
46 return retryTransactionWrapper(createVideoDislike, options)
47 }
48
49 function createVideoDislike (byAccount: AccountInstance, activity: ActivityCreate) {
50 const dislike = activity.object as DislikeObject
51
52 return db.sequelize.transaction(async t => {
53 const video = await db.Video.loadByUrlAndPopulateAccount(dislike.object, t)
54 if (!video) throw new Error('Unknown video ' + dislike.object)
55
56 const rate = {
57 type: 'dislike' as 'dislike',
58 videoId: video.id,
59 accountId: byAccount.id
60 }
61 const [ , created ] = await db.AccountVideoRate.findOrCreate({
62 where: rate,
63 defaults: rate,
64 transaction: t
65 })
66 await video.increment('dislikes', { transaction: t })
67
68 if (video.isOwned() && created === true) {
69 // Don't resend the activity to the sender
70 const exceptions = [ byAccount ]
71 await forwardActivity(activity, t, exceptions)
72 }
73 })
74 }
75
76 async function processCreateView (byAccount: AccountInstance, activity: ActivityCreate) {
77 const view = activity.object as ViewObject
78
79 const video = await db.Video.loadByUrlAndPopulateAccount(view.object)
80
81 if (!video) throw new Error('Unknown video ' + view.object)
82
83 const account = await db.Account.loadByUrl(view.actor)
84 if (!account) throw new Error('Unknown account ' + view.actor)
85
86 await video.increment('views')
87
88 if (video.isOwned()) {
89 // Don't resend the activity to the sender
90 const exceptions = [ byAccount ]
91 await forwardActivity(activity, undefined, exceptions)
92 }
93 }
94
95 async function processCreateVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) {
96 const options = {
97 arguments: [ account, videoChannelToCreateData ],
98 errorMessage: 'Cannot insert the remote video channel with many retries.'
99 }
100
101 const videoChannel = await retryTransactionWrapper(addRemoteVideoChannel, options)
102
103 if (videoChannelToCreateData.shares && Array.isArray(videoChannelToCreateData.shares.orderedItems)) {
104 await addVideoChannelShares(videoChannel, videoChannelToCreateData.shares.orderedItems)
105 }
106
107 return videoChannel
108 }
109
110 function addRemoteVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) {
111 logger.debug('Adding remote video channel "%s".', videoChannelToCreateData.uuid)
112
113 return db.sequelize.transaction(async t => {
114 let videoChannel = await db.VideoChannel.loadByUUIDOrUrl(videoChannelToCreateData.uuid, videoChannelToCreateData.id, t)
115 if (videoChannel) return videoChannel
116
117 const videoChannelData = videoChannelActivityObjectToDBAttributes(videoChannelToCreateData, account)
118 videoChannel = db.VideoChannel.build(videoChannelData)
119 videoChannel.url = getVideoChannelActivityPubUrl(videoChannel)
120
121 videoChannel = await videoChannel.save({ transaction: t })
122 logger.info('Remote video channel with uuid %s inserted.', videoChannelToCreateData.uuid)
123
124 return videoChannel
125 })
126 }
127
128 function processCreateVideoAbuse (account: AccountInstance, videoAbuseToCreateData: VideoAbuseObject) {
129 const options = {
130 arguments: [ account, videoAbuseToCreateData ],
131 errorMessage: 'Cannot insert the remote video abuse with many retries.'
132 }
133
134 return retryTransactionWrapper(addRemoteVideoAbuse, options)
135 }
136
137 function addRemoteVideoAbuse (account: AccountInstance, videoAbuseToCreateData: VideoAbuseObject) {
138 logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object)
139
140 return db.sequelize.transaction(async t => {
141 const video = await db.Video.loadByUrlAndPopulateAccount(videoAbuseToCreateData.object, t)
142 if (!video) {
143 logger.warn('Unknown video %s for remote video abuse.', videoAbuseToCreateData.object)
144 return undefined
145 }
146
147 const videoAbuseData = {
148 reporterAccountId: account.id,
149 reason: videoAbuseToCreateData.content,
150 videoId: video.id
151 }
152
153 await db.VideoAbuse.create(videoAbuseData)
154
155 logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object)
156 })
157 }