]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-announce.ts
Move models to typescript-sequelize
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-announce.ts
1 import { ActivityAdd, ActivityAnnounce, ActivityCreate } from '../../../../shared/models/activitypub'
2 import { logger, retryTransactionWrapper } from '../../../helpers'
3 import { sequelizeTypescript } from '../../../initializers'
4 import { AccountModel } from '../../../models/account/account'
5 import { VideoModel } from '../../../models/video/video'
6 import { VideoChannelModel } from '../../../models/video/video-channel'
7 import { VideoChannelShareModel } from '../../../models/video/video-channel-share'
8 import { VideoShareModel } from '../../../models/video/video-share'
9 import { getOrCreateAccountAndServer } from '../account'
10 import { forwardActivity } from '../send/misc'
11 import { processAddActivity } from './process-add'
12 import { processCreateActivity } from './process-create'
13
14 async function processAnnounceActivity (activity: ActivityAnnounce) {
15 const announcedActivity = activity.object
16 const accountAnnouncer = await getOrCreateAccountAndServer(activity.actor)
17
18 if (announcedActivity.type === 'Create' && announcedActivity.object.type === 'VideoChannel') {
19 return processVideoChannelShare(accountAnnouncer, activity)
20 } else if (announcedActivity.type === 'Add' && announcedActivity.object.type === 'Video') {
21 return processVideoShare(accountAnnouncer, activity)
22 }
23
24 logger.warn(
25 'Unknown activity object type %s -> %s when announcing activity.', announcedActivity.type, announcedActivity.object.type,
26 { activity: activity.id }
27 )
28
29 return undefined
30 }
31
32 // ---------------------------------------------------------------------------
33
34 export {
35 processAnnounceActivity
36 }
37
38 // ---------------------------------------------------------------------------
39
40 function processVideoChannelShare (accountAnnouncer: AccountModel, activity: ActivityAnnounce) {
41 const options = {
42 arguments: [ accountAnnouncer, activity ],
43 errorMessage: 'Cannot share the video channel with many retries.'
44 }
45
46 return retryTransactionWrapper(shareVideoChannel, options)
47 }
48
49 async function shareVideoChannel (accountAnnouncer: AccountModel, activity: ActivityAnnounce) {
50 const announcedActivity = activity.object as ActivityCreate
51
52 return sequelizeTypescript.transaction(async t => {
53 // Add share entry
54 const videoChannel: VideoChannelModel = await processCreateActivity(announcedActivity)
55 const share = {
56 accountId: accountAnnouncer.id,
57 videoChannelId: videoChannel.id
58 }
59
60 const [ , created ] = await VideoChannelShareModel.findOrCreate({
61 where: share,
62 defaults: share,
63 transaction: t
64 })
65
66 if (videoChannel.isOwned() && created === true) {
67 // Don't resend the activity to the sender
68 const exceptions = [ accountAnnouncer ]
69 await forwardActivity(activity, t, exceptions)
70 }
71
72 return undefined
73 })
74 }
75
76 function processVideoShare (accountAnnouncer: AccountModel, activity: ActivityAnnounce) {
77 const options = {
78 arguments: [ accountAnnouncer, activity ],
79 errorMessage: 'Cannot share the video with many retries.'
80 }
81
82 return retryTransactionWrapper(shareVideo, options)
83 }
84
85 function shareVideo (accountAnnouncer: AccountModel, activity: ActivityAnnounce) {
86 const announcedActivity = activity.object as ActivityAdd
87
88 return sequelizeTypescript.transaction(async t => {
89 // Add share entry
90 const video: VideoModel = await processAddActivity(announcedActivity)
91
92 const share = {
93 accountId: accountAnnouncer.id,
94 videoId: video.id
95 }
96
97 const [ , created ] = await VideoShareModel.findOrCreate({
98 where: share,
99 defaults: share,
100 transaction: t
101 })
102
103 if (video.isOwned() && created === true) {
104 // Don't resend the activity to the sender
105 const exceptions = [ accountAnnouncer ]
106 await forwardActivity(activity, t, exceptions)
107 }
108
109 return undefined
110 })
111 }