1 import { join } from 'path'
2 import * as request from 'request'
3 import { Transaction } from 'sequelize'
4 import { ActivityIconObject } from '../../../shared/index'
5 import { doRequest, doRequestAndSaveToFile } from '../../helpers'
6 import { CONFIG, REMOTE_SCHEME, STATIC_PATHS } from '../../initializers'
7 import { AccountModel } from '../../models/account/account'
8 import { VideoModel } from '../../models/video/video'
10 sendCreateDislikeToOrigin,
11 sendCreateDislikeToVideoFollowers,
13 sendLikeToVideoFollowers,
14 sendUndoDislikeToOrigin,
15 sendUndoDislikeToVideoFollowers,
17 sendUndoLikeToVideoFollowers
20 function fetchRemoteVideoPreview (video: VideoModel, reject: Function) {
22 const host = video.VideoChannel.Account.Actor.Server.host
23 const path = join(STATIC_PATHS.PREVIEWS, video.getPreviewName())
25 return request.get(REMOTE_SCHEME.HTTP + '://' + host + path, err => reject(err))
28 async function fetchRemoteVideoDescription (video: VideoModel) {
30 const host = video.VideoChannel.Account.Actor.Server.host
31 const path = video.getDescriptionPath()
33 uri: REMOTE_SCHEME.HTTP + '://' + host + path,
37 const { body } = await doRequest(options)
38 return body.description ? body.description : ''
41 function generateThumbnailFromUrl (video: VideoModel, icon: ActivityIconObject) {
42 const thumbnailName = video.getThumbnailName()
43 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, thumbnailName)
49 return doRequestAndSaveToFile(options, thumbnailPath)
52 async function sendVideoRateChangeToFollowers (
53 account: AccountModel,
59 const actor = account.Actor
61 // Keep the order: first we undo and then we create
64 if (likes < 0) await sendUndoLikeToVideoFollowers(actor, video, t)
66 if (dislikes < 0) await sendUndoDislikeToVideoFollowers(actor, video, t)
69 if (likes > 0) await sendLikeToVideoFollowers(actor, video, t)
71 if (dislikes > 0) await sendCreateDislikeToVideoFollowers(actor, video, t)
74 async function sendVideoRateChangeToOrigin (
75 account: AccountModel,
81 const actor = account.Actor
83 // Keep the order: first we undo and then we create
86 if (likes < 0) await sendUndoLikeToOrigin(actor, video, t)
88 if (dislikes < 0) await sendUndoDislikeToOrigin(actor, video, t)
91 if (likes > 0) await sendLikeToOrigin(actor, video, t)
93 if (dislikes > 0) await sendCreateDislikeToOrigin(actor, video, t)
97 fetchRemoteVideoPreview,
98 fetchRemoteVideoDescription,
99 generateThumbnailFromUrl,
100 sendVideoRateChangeToFollowers,
101 sendVideoRateChangeToOrigin