diff options
author | Chocobozzz <me@florianbigard.com> | 2019-01-15 11:14:12 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-01-15 14:45:09 +0100 |
commit | 848f499def54db2dd36437ef0dfb74dd5041c23b (patch) | |
tree | 13e6fcd30e3ce5306d5999fc91561af54d9fd20e /server/lib/activitypub/process/process-create.ts | |
parent | 44b9c0ba31c4a97e3d874f33226ad935c3a90dd5 (diff) | |
download | PeerTube-848f499def54db2dd36437ef0dfb74dd5041c23b.tar.gz PeerTube-848f499def54db2dd36437ef0dfb74dd5041c23b.tar.zst PeerTube-848f499def54db2dd36437ef0dfb74dd5041c23b.zip |
Prepare Dislike/Flag/View fixes
For now we Create these activities, but we should just send them
directly.
This fix handles correctly direct Dislikes/Flags/Views, we'll implement
the sending correctly these activities in the next peertube version
Diffstat (limited to 'server/lib/activitypub/process/process-create.ts')
-rw-r--r-- | server/lib/activitypub/process/process-create.ts | 118 |
1 files changed, 26 insertions, 92 deletions
diff --git a/server/lib/activitypub/process/process-create.ts b/server/lib/activitypub/process/process-create.ts index 2e04ee843..5f4d793a5 100644 --- a/server/lib/activitypub/process/process-create.ts +++ b/server/lib/activitypub/process/process-create.ts | |||
@@ -1,36 +1,44 @@ | |||
1 | import { ActivityCreate, CacheFileObject, VideoAbuseState, VideoTorrentObject } from '../../../../shared' | 1 | import { ActivityCreate, CacheFileObject, VideoTorrentObject } from '../../../../shared' |
2 | import { DislikeObject, VideoAbuseObject, ViewObject } from '../../../../shared/models/activitypub/objects' | ||
3 | import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object' | 2 | import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object' |
4 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | 3 | import { retryTransactionWrapper } from '../../../helpers/database-utils' |
5 | import { logger } from '../../../helpers/logger' | 4 | import { logger } from '../../../helpers/logger' |
6 | import { sequelizeTypescript } from '../../../initializers' | 5 | import { sequelizeTypescript } from '../../../initializers' |
7 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' | ||
8 | import { ActorModel } from '../../../models/activitypub/actor' | 6 | import { ActorModel } from '../../../models/activitypub/actor' |
9 | import { VideoAbuseModel } from '../../../models/video/video-abuse' | ||
10 | import { addVideoComment, resolveThread } from '../video-comments' | 7 | import { addVideoComment, resolveThread } from '../video-comments' |
11 | import { getOrCreateVideoAndAccountAndChannel } from '../videos' | 8 | import { getOrCreateVideoAndAccountAndChannel } from '../videos' |
12 | import { forwardVideoRelatedActivity } from '../send/utils' | 9 | import { forwardVideoRelatedActivity } from '../send/utils' |
13 | import { Redis } from '../../redis' | ||
14 | import { createOrUpdateCacheFile } from '../cache-file' | 10 | import { createOrUpdateCacheFile } from '../cache-file' |
15 | import { getVideoDislikeActivityPubUrl } from '../url' | ||
16 | import { Notifier } from '../../notifier' | 11 | import { Notifier } from '../../notifier' |
12 | import { processViewActivity } from './process-view' | ||
13 | import { processDislikeActivity } from './process-dislike' | ||
14 | import { processFlagActivity } from './process-flag' | ||
17 | 15 | ||
18 | async function processCreateActivity (activity: ActivityCreate, byActor: ActorModel) { | 16 | async function processCreateActivity (activity: ActivityCreate, byActor: ActorModel) { |
19 | const activityObject = activity.object | 17 | const activityObject = activity.object |
20 | const activityType = activityObject.type | 18 | const activityType = activityObject.type |
21 | 19 | ||
22 | if (activityType === 'View') { | 20 | if (activityType === 'View') { |
23 | return processCreateView(byActor, activity) | 21 | return processViewActivity(activity, byActor) |
24 | } else if (activityType === 'Dislike') { | 22 | } |
25 | return retryTransactionWrapper(processCreateDislike, byActor, activity) | 23 | |
26 | } else if (activityType === 'Video') { | 24 | if (activityType === 'Dislike') { |
25 | return retryTransactionWrapper(processDislikeActivity, activity, byActor) | ||
26 | } | ||
27 | |||
28 | if (activityType === 'Flag') { | ||
29 | return retryTransactionWrapper(processFlagActivity, activity, byActor) | ||
30 | } | ||
31 | |||
32 | if (activityType === 'Video') { | ||
27 | return processCreateVideo(activity) | 33 | return processCreateVideo(activity) |
28 | } else if (activityType === 'Flag') { | 34 | } |
29 | return retryTransactionWrapper(processCreateVideoAbuse, byActor, activityObject as VideoAbuseObject) | 35 | |
30 | } else if (activityType === 'Note') { | 36 | if (activityType === 'Note') { |
31 | return retryTransactionWrapper(processCreateVideoComment, byActor, activity) | 37 | return retryTransactionWrapper(processCreateVideoComment, activity, byActor) |
32 | } else if (activityType === 'CacheFile') { | 38 | } |
33 | return retryTransactionWrapper(processCacheFile, byActor, activity) | 39 | |
40 | if (activityType === 'CacheFile') { | ||
41 | return retryTransactionWrapper(processCacheFile, activity, byActor) | ||
34 | } | 42 | } |
35 | 43 | ||
36 | logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id }) | 44 | logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id }) |
@@ -55,56 +63,7 @@ async function processCreateVideo (activity: ActivityCreate) { | |||
55 | return video | 63 | return video |
56 | } | 64 | } |
57 | 65 | ||
58 | async function processCreateDislike (byActor: ActorModel, activity: ActivityCreate) { | 66 | async function processCacheFile (activity: ActivityCreate, byActor: ActorModel) { |
59 | const dislike = activity.object as DislikeObject | ||
60 | const byAccount = byActor.Account | ||
61 | |||
62 | if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url) | ||
63 | |||
64 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: dislike.object }) | ||
65 | |||
66 | return sequelizeTypescript.transaction(async t => { | ||
67 | const rate = { | ||
68 | type: 'dislike' as 'dislike', | ||
69 | videoId: video.id, | ||
70 | accountId: byAccount.id | ||
71 | } | ||
72 | |||
73 | const [ , created ] = await AccountVideoRateModel.findOrCreate({ | ||
74 | where: rate, | ||
75 | defaults: Object.assign({}, rate, { url: getVideoDislikeActivityPubUrl(byActor, video) }), | ||
76 | transaction: t | ||
77 | }) | ||
78 | if (created === true) await video.increment('dislikes', { transaction: t }) | ||
79 | |||
80 | if (video.isOwned() && created === true) { | ||
81 | // Don't resend the activity to the sender | ||
82 | const exceptions = [ byActor ] | ||
83 | |||
84 | await forwardVideoRelatedActivity(activity, t, exceptions, video) | ||
85 | } | ||
86 | }) | ||
87 | } | ||
88 | |||
89 | async function processCreateView (byActor: ActorModel, activity: ActivityCreate) { | ||
90 | const view = activity.object as ViewObject | ||
91 | |||
92 | const options = { | ||
93 | videoObject: view.object, | ||
94 | fetchType: 'only-video' as 'only-video' | ||
95 | } | ||
96 | const { video } = await getOrCreateVideoAndAccountAndChannel(options) | ||
97 | |||
98 | await Redis.Instance.addVideoView(video.id) | ||
99 | |||
100 | if (video.isOwned()) { | ||
101 | // Don't resend the activity to the sender | ||
102 | const exceptions = [ byActor ] | ||
103 | await forwardVideoRelatedActivity(activity, undefined, exceptions, video) | ||
104 | } | ||
105 | } | ||
106 | |||
107 | async function processCacheFile (byActor: ActorModel, activity: ActivityCreate) { | ||
108 | const cacheFile = activity.object as CacheFileObject | 67 | const cacheFile = activity.object as CacheFileObject |
109 | 68 | ||
110 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object }) | 69 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object }) |
@@ -120,32 +79,7 @@ async function processCacheFile (byActor: ActorModel, activity: ActivityCreate) | |||
120 | } | 79 | } |
121 | } | 80 | } |
122 | 81 | ||
123 | async function processCreateVideoAbuse (byActor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) { | 82 | async function processCreateVideoComment (activity: ActivityCreate, byActor: ActorModel) { |
124 | logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object) | ||
125 | |||
126 | const account = byActor.Account | ||
127 | if (!account) throw new Error('Cannot create dislike with the non account actor ' + byActor.url) | ||
128 | |||
129 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoAbuseToCreateData.object }) | ||
130 | |||
131 | return sequelizeTypescript.transaction(async t => { | ||
132 | const videoAbuseData = { | ||
133 | reporterAccountId: account.id, | ||
134 | reason: videoAbuseToCreateData.content, | ||
135 | videoId: video.id, | ||
136 | state: VideoAbuseState.PENDING | ||
137 | } | ||
138 | |||
139 | const videoAbuseInstance = await VideoAbuseModel.create(videoAbuseData, { transaction: t }) | ||
140 | videoAbuseInstance.Video = video | ||
141 | |||
142 | Notifier.Instance.notifyOnNewVideoAbuse(videoAbuseInstance) | ||
143 | |||
144 | logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object) | ||
145 | }) | ||
146 | } | ||
147 | |||
148 | async function processCreateVideoComment (byActor: ActorModel, activity: ActivityCreate) { | ||
149 | const commentObject = activity.object as VideoCommentObject | 83 | const commentObject = activity.object as VideoCommentObject |
150 | const byAccount = byActor.Account | 84 | const byAccount = byActor.Account |
151 | 85 | ||