aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/process/process-announce.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-11-27 14:44:51 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-11-27 19:40:53 +0100
commit4e50b6a1c9a3eb261e04ede73241648e6edf21d6 (patch)
treee1c6c121d561ffc1cf2996daec03a1e7f27f0a25 /server/lib/activitypub/process/process-announce.ts
parent74bb2cb8348d6794ed3a0e2ec94c8c9abdde82cf (diff)
downloadPeerTube-4e50b6a1c9a3eb261e04ede73241648e6edf21d6.tar.gz
PeerTube-4e50b6a1c9a3eb261e04ede73241648e6edf21d6.tar.zst
PeerTube-4e50b6a1c9a3eb261e04ede73241648e6edf21d6.zip
Add shares forward and collection on videos/video channels
Diffstat (limited to 'server/lib/activitypub/process/process-announce.ts')
-rw-r--r--server/lib/activitypub/process/process-announce.ts100
1 files changed, 82 insertions, 18 deletions
diff --git a/server/lib/activitypub/process/process-announce.ts b/server/lib/activitypub/process/process-announce.ts
index d8532d3a1..2aa9ad5c7 100644
--- a/server/lib/activitypub/process/process-announce.ts
+++ b/server/lib/activitypub/process/process-announce.ts
@@ -1,34 +1,23 @@
1import { ActivityAnnounce } from '../../../../shared/models/activitypub/activity' 1import { ActivityAdd, ActivityAnnounce, ActivityCreate } from '../../../../shared/models/activitypub/activity'
2import { retryTransactionWrapper } from '../../../helpers/database-utils'
2import { logger } from '../../../helpers/logger' 3import { logger } from '../../../helpers/logger'
3import { database as db } from '../../../initializers/index' 4import { database as db } from '../../../initializers/index'
5import { AccountInstance } from '../../../models/account/account-interface'
4import { VideoInstance } from '../../../models/index' 6import { VideoInstance } from '../../../models/index'
5import { VideoChannelInstance } from '../../../models/video/video-channel-interface' 7import { VideoChannelInstance } from '../../../models/video/video-channel-interface'
8import { getOrCreateAccountAndServer } from '../account'
9import { forwardActivity } from '../send/misc'
6import { processAddActivity } from './process-add' 10import { processAddActivity } from './process-add'
7import { processCreateActivity } from './process-create' 11import { processCreateActivity } from './process-create'
8import { getOrCreateAccountAndServer } from '../account'
9 12
10async function processAnnounceActivity (activity: ActivityAnnounce) { 13async function processAnnounceActivity (activity: ActivityAnnounce) {
11 const announcedActivity = activity.object 14 const announcedActivity = activity.object
12 const accountAnnouncer = await getOrCreateAccountAndServer(activity.actor) 15 const accountAnnouncer = await getOrCreateAccountAndServer(activity.actor)
13 16
14 if (announcedActivity.type === 'Create' && announcedActivity.object.type === 'VideoChannel') { 17 if (announcedActivity.type === 'Create' && announcedActivity.object.type === 'VideoChannel') {
15 // Add share entry 18 return processVideoChannelShare(accountAnnouncer, activity)
16 const videoChannel: VideoChannelInstance = await processCreateActivity(announcedActivity)
17 await db.VideoChannelShare.create({
18 accountId: accountAnnouncer.id,
19 videoChannelId: videoChannel.id
20 })
21
22 return undefined
23 } else if (announcedActivity.type === 'Add' && announcedActivity.object.type === 'Video') { 19 } else if (announcedActivity.type === 'Add' && announcedActivity.object.type === 'Video') {
24 // Add share entry 20 return processVideoShare(accountAnnouncer, activity)
25 const video: VideoInstance = await processAddActivity(announcedActivity)
26 await db.VideoShare.create({
27 accountId: accountAnnouncer.id,
28 videoId: video.id
29 })
30
31 return undefined
32 } 21 }
33 22
34 logger.warn( 23 logger.warn(
@@ -44,3 +33,78 @@ async function processAnnounceActivity (activity: ActivityAnnounce) {
44export { 33export {
45 processAnnounceActivity 34 processAnnounceActivity
46} 35}
36
37// ---------------------------------------------------------------------------
38
39function processVideoChannelShare (accountAnnouncer: AccountInstance, activity: ActivityAnnounce) {
40 const options = {
41 arguments: [ accountAnnouncer, activity ],
42 errorMessage: 'Cannot share the video channel with many retries.'
43 }
44
45 return retryTransactionWrapper(shareVideoChannel, options)
46}
47
48async function shareVideoChannel (accountAnnouncer: AccountInstance, activity: ActivityAnnounce) {
49 const announcedActivity = activity.object as ActivityCreate
50
51 return db.sequelize.transaction(async t => {
52 // Add share entry
53 const videoChannel: VideoChannelInstance = await processCreateActivity(announcedActivity)
54 const share = {
55 accountId: accountAnnouncer.id,
56 videoChannelId: videoChannel.id
57 }
58
59 const [ , created ] = await db.VideoChannelShare.findOrCreate({
60 where: share,
61 defaults: share,
62 transaction: t
63 })
64
65 if (videoChannel.isOwned() && created === true) {
66 // Don't resend the activity to the sender
67 const exceptions = [ accountAnnouncer ]
68 await forwardActivity(activity, t, exceptions)
69 }
70
71 return undefined
72 })
73}
74
75function processVideoShare (accountAnnouncer: AccountInstance, activity: ActivityAnnounce) {
76 const options = {
77 arguments: [ accountAnnouncer, activity ],
78 errorMessage: 'Cannot share the video with many retries.'
79 }
80
81 return retryTransactionWrapper(shareVideo, options)
82}
83
84function shareVideo (accountAnnouncer: AccountInstance, activity: ActivityAnnounce) {
85 const announcedActivity = activity.object as ActivityAdd
86
87 return db.sequelize.transaction(async t => {
88 // Add share entry
89 const video: VideoInstance = await processAddActivity(announcedActivity)
90
91 const share = {
92 accountId: accountAnnouncer.id,
93 videoId: video.id
94 }
95
96 const [ , created ] = await db.VideoShare.findOrCreate({
97 where: share,
98 defaults: share,
99 transaction: t
100 })
101
102 if (video.isOwned() && created === true) {
103 // Don't resend the activity to the sender
104 const exceptions = [ accountAnnouncer ]
105 await forwardActivity(activity, t, exceptions)
106 }
107
108 return undefined
109 })
110}