aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/videos.ts
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/videos.ts
parentd6e99e5322209a692cc3d870ddb5dcedbda69f2a (diff)
downloadPeerTube-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.ts53
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 @@
1import * as Bluebird from 'bluebird'
1import * as magnetUtil from 'magnet-uri' 2import * as magnetUtil from 'magnet-uri'
2import { join } from 'path' 3import { join } from 'path'
3import * as request from 'request' 4import * as request from 'request'
4import { ActivityIconObject } from '../../../shared/index' 5import { ActivityIconObject } from '../../../shared/index'
5import { VideoTorrentObject } from '../../../shared/models/activitypub/objects' 6import { VideoTorrentObject } from '../../../shared/models/activitypub/objects'
6import { VideoPrivacy } from '../../../shared/models/videos' 7import { VideoPrivacy, VideoRateType } from '../../../shared/models/videos'
7import { isVideoTorrentObjectValid } from '../../helpers/custom-validators/activitypub/videos' 8import { isVideoTorrentObjectValid } from '../../helpers/custom-validators/activitypub/videos'
8import { isVideoFileInfoHashValid } from '../../helpers/custom-validators/videos' 9import { isVideoFileInfoHashValid } from '../../helpers/custom-validators/videos'
9import { retryTransactionWrapper } from '../../helpers/database-utils' 10import { retryTransactionWrapper } from '../../helpers/database-utils'
10import { logger } from '../../helpers/logger' 11import { logger } from '../../helpers/logger'
11import { doRequest, doRequestAndSaveToFile } from '../../helpers/requests' 12import { doRequest, doRequestAndSaveToFile } from '../../helpers/requests'
12import { ACTIVITY_PUB, CONFIG, REMOTE_SCHEME, sequelizeTypescript, STATIC_PATHS, VIDEO_MIMETYPE_EXT } from '../../initializers' 13import { ACTIVITY_PUB, CONFIG, REMOTE_SCHEME, sequelizeTypescript, STATIC_PATHS, VIDEO_MIMETYPE_EXT } from '../../initializers'
14import { AccountVideoRateModel } from '../../models/account/account-video-rate'
13import { ActorModel } from '../../models/activitypub/actor' 15import { ActorModel } from '../../models/activitypub/actor'
14import { TagModel } from '../../models/video/tag' 16import { TagModel } from '../../models/video/tag'
15import { VideoModel } from '../../models/video/video' 17import { VideoModel } from '../../models/video/video'
@@ -17,6 +19,7 @@ import { VideoChannelModel } from '../../models/video/video-channel'
17import { VideoFileModel } from '../../models/video/video-file' 19import { VideoFileModel } from '../../models/video/video-file'
18import { VideoShareModel } from '../../models/video/video-share' 20import { VideoShareModel } from '../../models/video/video-share'
19import { getOrCreateActorAndServerAndModel } from './actor' 21import { getOrCreateActorAndServerAndModel } from './actor'
22import { addVideoComments } from './video-comments'
20 23
21function fetchRemoteVideoPreview (video: VideoModel, reject: Function) { 24function 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
240async 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
216async function addVideoShares (instance: VideoModel, shareUrls: string[]) { 267async function addVideoShares (instance: VideoModel, shareUrls: string[]) {
217 for (const shareUrl of shareUrls) { 268 for (const shareUrl of shareUrls) {
218 // Fetch url 269 // Fetch url