aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/process
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-01-26 12:02:18 +0100
committerChocobozzz <me@florianbigard.com>2018-01-26 13:43:34 +0100
commit7acee6f18aac99e359360fc4f2362d5405135a79 (patch)
treea8eceaba9a01b913fcfca32f17f26b4f588a633e /server/lib/activitypub/process
parentd6e99e5322209a692cc3d870ddb5dcedbda69f2a (diff)
downloadPeerTube-7acee6f18aac99e359360fc4f2362d5405135a79.tar.gz
PeerTube-7acee6f18aac99e359360fc4f2362d5405135a79.tar.zst
PeerTube-7acee6f18aac99e359360fc4f2362d5405135a79.zip
Fix announce activities
Diffstat (limited to 'server/lib/activitypub/process')
-rw-r--r--server/lib/activitypub/process/process-announce.ts26
-rw-r--r--server/lib/activitypub/process/process-create.ts55
2 files changed, 6 insertions, 75 deletions
diff --git a/server/lib/activitypub/process/process-announce.ts b/server/lib/activitypub/process/process-announce.ts
index bf7d7879d..7dafc0593 100644
--- a/server/lib/activitypub/process/process-announce.ts
+++ b/server/lib/activitypub/process/process-announce.ts
@@ -1,6 +1,5 @@
1import { ActivityAnnounce } from '../../../../shared/models/activitypub' 1import { ActivityAnnounce } from '../../../../shared/models/activitypub'
2import { retryTransactionWrapper } from '../../../helpers/database-utils' 2import { retryTransactionWrapper } from '../../../helpers/database-utils'
3import { logger } from '../../../helpers/logger'
4import { sequelizeTypescript } from '../../../initializers' 3import { sequelizeTypescript } from '../../../initializers'
5import { ActorModel } from '../../../models/activitypub/actor' 4import { ActorModel } from '../../../models/activitypub/actor'
6import { VideoModel } from '../../../models/video/video' 5import { VideoModel } from '../../../models/video/video'
@@ -8,24 +7,11 @@ import { VideoShareModel } from '../../../models/video/video-share'
8import { getOrCreateActorAndServerAndModel } from '../actor' 7import { getOrCreateActorAndServerAndModel } from '../actor'
9import { forwardActivity } from '../send/misc' 8import { forwardActivity } from '../send/misc'
10import { getOrCreateAccountAndVideoAndChannel } from '../videos' 9import { getOrCreateAccountAndVideoAndChannel } from '../videos'
11import { processCreateActivity } from './process-create'
12 10
13async function processAnnounceActivity (activity: ActivityAnnounce) { 11async function processAnnounceActivity (activity: ActivityAnnounce) {
14 const announcedActivity = activity.object
15 const actorAnnouncer = await getOrCreateActorAndServerAndModel(activity.actor) 12 const actorAnnouncer = await getOrCreateActorAndServerAndModel(activity.actor)
16 13
17 if (typeof announcedActivity === 'string') { 14 return processVideoShare(actorAnnouncer, activity)
18 return processVideoShare(actorAnnouncer, activity)
19 } else if (announcedActivity.type === 'Create' && announcedActivity.object.type === 'Video') {
20 return processVideoShare(actorAnnouncer, activity)
21 }
22
23 logger.warn(
24 'Unknown activity object type %s -> %s when announcing activity.', announcedActivity.type, announcedActivity.object.type,
25 { activity: activity.id }
26 )
27
28 return undefined
29} 15}
30 16
31// --------------------------------------------------------------------------- 17// ---------------------------------------------------------------------------
@@ -46,15 +32,11 @@ function processVideoShare (actorAnnouncer: ActorModel, activity: ActivityAnnoun
46} 32}
47 33
48async function shareVideo (actorAnnouncer: ActorModel, activity: ActivityAnnounce) { 34async function shareVideo (actorAnnouncer: ActorModel, activity: ActivityAnnounce) {
49 const announced = activity.object 35 const objectUri = typeof activity.object === 'string' ? activity.object : activity.object.id
50 let video: VideoModel 36 let video: VideoModel
51 37
52 if (typeof announced === 'string') { 38 const res = await getOrCreateAccountAndVideoAndChannel(objectUri)
53 const res = await getOrCreateAccountAndVideoAndChannel(announced) 39 video = res.video
54 video = res.video
55 } else {
56 video = await processCreateActivity(announced)
57 }
58 40
59 return sequelizeTypescript.transaction(async t => { 41 return sequelizeTypescript.transaction(async t => {
60 // Add share entry 42 // Add share entry
diff --git a/server/lib/activitypub/process/process-create.ts b/server/lib/activitypub/process/process-create.ts
index eff796c1d..ee180b765 100644
--- a/server/lib/activitypub/process/process-create.ts
+++ b/server/lib/activitypub/process/process-create.ts
@@ -1,20 +1,17 @@
1import * as Bluebird from 'bluebird'
2import { ActivityCreate, VideoTorrentObject } from '../../../../shared' 1import { ActivityCreate, VideoTorrentObject } from '../../../../shared'
3import { DislikeObject, VideoAbuseObject, ViewObject } from '../../../../shared/models/activitypub/objects' 2import { DislikeObject, VideoAbuseObject, ViewObject } from '../../../../shared/models/activitypub/objects'
4import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object' 3import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object'
5import { VideoRateType } from '../../../../shared/models/videos'
6import { retryTransactionWrapper } from '../../../helpers/database-utils' 4import { retryTransactionWrapper } from '../../../helpers/database-utils'
7import { logger } from '../../../helpers/logger' 5import { logger } from '../../../helpers/logger'
8import { sequelizeTypescript } from '../../../initializers' 6import { sequelizeTypescript } from '../../../initializers'
9import { AccountVideoRateModel } from '../../../models/account/account-video-rate' 7import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
10import { ActorModel } from '../../../models/activitypub/actor' 8import { ActorModel } from '../../../models/activitypub/actor'
11import { VideoModel } from '../../../models/video/video'
12import { VideoAbuseModel } from '../../../models/video/video-abuse' 9import { VideoAbuseModel } from '../../../models/video/video-abuse'
13import { VideoCommentModel } from '../../../models/video/video-comment' 10import { VideoCommentModel } from '../../../models/video/video-comment'
14import { getOrCreateActorAndServerAndModel } from '../actor' 11import { getOrCreateActorAndServerAndModel } from '../actor'
15import { forwardActivity, getActorsInvolvedInVideo } from '../send/misc' 12import { forwardActivity, getActorsInvolvedInVideo } from '../send/misc'
16import { addVideoComments, resolveThread } from '../video-comments' 13import { resolveThread } from '../video-comments'
17import { addVideoShares, getOrCreateAccountAndVideoAndChannel } from '../videos' 14import { getOrCreateAccountAndVideoAndChannel } from '../videos'
18 15
19async function processCreateActivity (activity: ActivityCreate) { 16async function processCreateActivity (activity: ActivityCreate) {
20 const activityObject = activity.object 17 const activityObject = activity.object
@@ -53,57 +50,9 @@ async function processCreateVideo (
53 50
54 const { video } = await getOrCreateAccountAndVideoAndChannel(videoToCreateData, actor) 51 const { video } = await getOrCreateAccountAndVideoAndChannel(videoToCreateData, actor)
55 52
56 // Process outside the transaction because we could fetch remote data
57 if (videoToCreateData.likes && Array.isArray(videoToCreateData.likes.orderedItems)) {
58 logger.info('Adding likes of video %s.', video.uuid)
59 await createRates(videoToCreateData.likes.orderedItems, video, 'like')
60 }
61
62 if (videoToCreateData.dislikes && Array.isArray(videoToCreateData.dislikes.orderedItems)) {
63 logger.info('Adding dislikes of video %s.', video.uuid)
64 await createRates(videoToCreateData.dislikes.orderedItems, video, 'dislike')
65 }
66
67 if (videoToCreateData.shares && Array.isArray(videoToCreateData.shares.orderedItems)) {
68 logger.info('Adding shares of video %s.', video.uuid)
69 await addVideoShares(video, videoToCreateData.shares.orderedItems)
70 }
71
72 if (videoToCreateData.comments && Array.isArray(videoToCreateData.comments.orderedItems)) {
73 logger.info('Adding comments of video %s.', video.uuid)
74 await addVideoComments(video, videoToCreateData.comments.orderedItems)
75 }
76
77 return video 53 return video
78} 54}
79 55
80async function createRates (actorUrls: string[], video: VideoModel, rate: VideoRateType) {
81 let rateCounts = 0
82 const tasks: Bluebird<number>[] = []
83
84 for (const actorUrl of actorUrls) {
85 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
86 const p = AccountVideoRateModel
87 .create({
88 videoId: video.id,
89 accountId: actor.Account.id,
90 type: rate
91 })
92 .then(() => rateCounts += 1)
93
94 tasks.push(p)
95 }
96
97 await Promise.all(tasks)
98
99 logger.info('Adding %d %s to video %s.', rateCounts, rate, video.uuid)
100
101 // This is "likes" and "dislikes"
102 await video.increment(rate + 's', { by: rateCounts })
103
104 return
105}
106
107async function processCreateDislike (byActor: ActorModel, activity: ActivityCreate) { 56async function processCreateDislike (byActor: ActorModel, activity: ActivityCreate) {
108 const options = { 57 const options = {
109 arguments: [ byActor, activity ], 58 arguments: [ byActor, activity ],