diff options
author | Chocobozzz <me@florianbigard.com> | 2018-01-26 12:02:18 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-01-26 13:43:34 +0100 |
commit | 7acee6f18aac99e359360fc4f2362d5405135a79 (patch) | |
tree | a8eceaba9a01b913fcfca32f17f26b4f588a633e /server/lib/activitypub/videos.ts | |
parent | d6e99e5322209a692cc3d870ddb5dcedbda69f2a (diff) | |
download | PeerTube-7acee6f18aac99e359360fc4f2362d5405135a79.tar.gz PeerTube-7acee6f18aac99e359360fc4f2362d5405135a79.tar.zst PeerTube-7acee6f18aac99e359360fc4f2362d5405135a79.zip |
Fix announce activities
Diffstat (limited to 'server/lib/activitypub/videos.ts')
-rw-r--r-- | server/lib/activitypub/videos.ts | 53 |
1 files changed, 52 insertions, 1 deletions
diff --git a/server/lib/activitypub/videos.ts b/server/lib/activitypub/videos.ts index 50e7e5cde..7d535bb0a 100644 --- a/server/lib/activitypub/videos.ts +++ b/server/lib/activitypub/videos.ts | |||
@@ -1,15 +1,17 @@ | |||
1 | import * as Bluebird from 'bluebird' | ||
1 | import * as magnetUtil from 'magnet-uri' | 2 | import * as magnetUtil from 'magnet-uri' |
2 | import { join } from 'path' | 3 | import { join } from 'path' |
3 | import * as request from 'request' | 4 | import * as request from 'request' |
4 | import { ActivityIconObject } from '../../../shared/index' | 5 | import { ActivityIconObject } from '../../../shared/index' |
5 | import { VideoTorrentObject } from '../../../shared/models/activitypub/objects' | 6 | import { VideoTorrentObject } from '../../../shared/models/activitypub/objects' |
6 | import { VideoPrivacy } from '../../../shared/models/videos' | 7 | import { VideoPrivacy, VideoRateType } from '../../../shared/models/videos' |
7 | import { isVideoTorrentObjectValid } from '../../helpers/custom-validators/activitypub/videos' | 8 | import { isVideoTorrentObjectValid } from '../../helpers/custom-validators/activitypub/videos' |
8 | import { isVideoFileInfoHashValid } from '../../helpers/custom-validators/videos' | 9 | import { isVideoFileInfoHashValid } from '../../helpers/custom-validators/videos' |
9 | import { retryTransactionWrapper } from '../../helpers/database-utils' | 10 | import { retryTransactionWrapper } from '../../helpers/database-utils' |
10 | import { logger } from '../../helpers/logger' | 11 | import { logger } from '../../helpers/logger' |
11 | import { doRequest, doRequestAndSaveToFile } from '../../helpers/requests' | 12 | import { doRequest, doRequestAndSaveToFile } from '../../helpers/requests' |
12 | import { ACTIVITY_PUB, CONFIG, REMOTE_SCHEME, sequelizeTypescript, STATIC_PATHS, VIDEO_MIMETYPE_EXT } from '../../initializers' | 13 | import { ACTIVITY_PUB, CONFIG, REMOTE_SCHEME, sequelizeTypescript, STATIC_PATHS, VIDEO_MIMETYPE_EXT } from '../../initializers' |
14 | import { AccountVideoRateModel } from '../../models/account/account-video-rate' | ||
13 | import { ActorModel } from '../../models/activitypub/actor' | 15 | import { ActorModel } from '../../models/activitypub/actor' |
14 | import { TagModel } from '../../models/video/tag' | 16 | import { TagModel } from '../../models/video/tag' |
15 | import { VideoModel } from '../../models/video/video' | 17 | import { VideoModel } from '../../models/video/video' |
@@ -17,6 +19,7 @@ import { VideoChannelModel } from '../../models/video/video-channel' | |||
17 | import { VideoFileModel } from '../../models/video/video-file' | 19 | import { VideoFileModel } from '../../models/video/video-file' |
18 | import { VideoShareModel } from '../../models/video/video-share' | 20 | import { VideoShareModel } from '../../models/video/video-share' |
19 | import { getOrCreateActorAndServerAndModel } from './actor' | 21 | import { getOrCreateActorAndServerAndModel } from './actor' |
22 | import { addVideoComments } from './video-comments' | ||
20 | 23 | ||
21 | function fetchRemoteVideoPreview (video: VideoModel, reject: Function) { | 24 | function fetchRemoteVideoPreview (video: VideoModel, reject: Function) { |
22 | const host = video.VideoChannel.Account.Actor.Server.host | 25 | const host = video.VideoChannel.Account.Actor.Server.host |
@@ -210,9 +213,57 @@ async function getOrCreateAccountAndVideoAndChannel (videoObject: VideoTorrentOb | |||
210 | 213 | ||
211 | const video = await retryTransactionWrapper(getOrCreateVideo, options) | 214 | const video = await retryTransactionWrapper(getOrCreateVideo, options) |
212 | 215 | ||
216 | // Process outside the transaction because we could fetch remote data | ||
217 | if (videoObject.likes && Array.isArray(videoObject.likes.orderedItems)) { | ||
218 | logger.info('Adding likes of video %s.', video.uuid) | ||
219 | await createRates(videoObject.likes.orderedItems, video, 'like') | ||
220 | } | ||
221 | |||
222 | if (videoObject.dislikes && Array.isArray(videoObject.dislikes.orderedItems)) { | ||
223 | logger.info('Adding dislikes of video %s.', video.uuid) | ||
224 | await createRates(videoObject.dislikes.orderedItems, video, 'dislike') | ||
225 | } | ||
226 | |||
227 | if (videoObject.shares && Array.isArray(videoObject.shares.orderedItems)) { | ||
228 | logger.info('Adding shares of video %s.', video.uuid) | ||
229 | await addVideoShares(video, videoObject.shares.orderedItems) | ||
230 | } | ||
231 | |||
232 | if (videoObject.comments && Array.isArray(videoObject.comments.orderedItems)) { | ||
233 | logger.info('Adding comments of video %s.', video.uuid) | ||
234 | await addVideoComments(video, videoObject.comments.orderedItems) | ||
235 | } | ||
236 | |||
213 | return { actor, channelActor, video } | 237 | return { actor, channelActor, video } |
214 | } | 238 | } |
215 | 239 | ||
240 | async function createRates (actorUrls: string[], video: VideoModel, rate: VideoRateType) { | ||
241 | let rateCounts = 0 | ||
242 | const tasks: Bluebird<number>[] = [] | ||
243 | |||
244 | for (const actorUrl of actorUrls) { | ||
245 | const actor = await getOrCreateActorAndServerAndModel(actorUrl) | ||
246 | const p = AccountVideoRateModel | ||
247 | .create({ | ||
248 | videoId: video.id, | ||
249 | accountId: actor.Account.id, | ||
250 | type: rate | ||
251 | }) | ||
252 | .then(() => rateCounts += 1) | ||
253 | |||
254 | tasks.push(p) | ||
255 | } | ||
256 | |||
257 | await Promise.all(tasks) | ||
258 | |||
259 | logger.info('Adding %d %s to video %s.', rateCounts, rate, video.uuid) | ||
260 | |||
261 | // This is "likes" and "dislikes" | ||
262 | await video.increment(rate + 's', { by: rateCounts }) | ||
263 | |||
264 | return | ||
265 | } | ||
266 | |||
216 | async function addVideoShares (instance: VideoModel, shareUrls: string[]) { | 267 | async function addVideoShares (instance: VideoModel, shareUrls: string[]) { |
217 | for (const shareUrl of shareUrls) { | 268 | for (const shareUrl of shareUrls) { |
218 | // Fetch url | 269 | // Fetch url |