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/requests'
6 import { CONFIG, REMOTE_SCHEME, STATIC_PATHS } from '../../initializers/constants'
7 import { AccountInstance } from '../../models/account/account-interface'
8 import { VideoInstance } from '../../models/video/video-interface'
9 import { sendLikeToOrigin } from './index'
10 import { sendCreateDislikeToOrigin, sendCreateDislikeToVideoFollowers } from './send/send-create'
11 import { sendLikeToVideoFollowers } from './send/send-like'
13 sendUndoDislikeToOrigin,
14 sendUndoDislikeToVideoFollowers,
16 sendUndoLikeToVideoFollowers
17 } from './send/send-undo'
19 function fetchRemoteVideoPreview (video: VideoInstance) {
21 const host = video.VideoChannel.Account.Server.host
22 const path = join(STATIC_PATHS.PREVIEWS, video.getPreviewName())
24 return request.get(REMOTE_SCHEME.HTTP + '://' + host + path)
27 async function fetchRemoteVideoDescription (video: VideoInstance) {
29 const host = video.VideoChannel.Account.Server.host
30 const path = video.getDescriptionPath()
32 uri: REMOTE_SCHEME.HTTP + '://' + host + path,
36 const { body } = await doRequest(options)
37 return body.description ? body.description : ''
40 function generateThumbnailFromUrl (video: VideoInstance, icon: ActivityIconObject) {
41 const thumbnailName = video.getThumbnailName()
42 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, thumbnailName)
48 return doRequestAndSaveToFile(options, thumbnailPath)
51 async function sendVideoRateChangeToFollowers (
52 account: AccountInstance,
58 // Keep the order: first we undo and then we create
61 if (likes < 0) await sendUndoLikeToVideoFollowers(account, video, t)
63 if (dislikes < 0) await sendUndoDislikeToVideoFollowers(account, video, t)
66 if (likes > 0) await sendLikeToVideoFollowers(account, video, t)
68 if (dislikes > 0) await sendCreateDislikeToVideoFollowers(account, video, t)
71 async function sendVideoRateChangeToOrigin (
72 account: AccountInstance,
78 // Keep the order: first we undo and then we create
81 if (likes < 0) await sendUndoLikeToOrigin(account, video, t)
83 if (dislikes < 0) await sendUndoDislikeToOrigin(account, video, t)
86 if (likes > 0) await sendLikeToOrigin(account, video, t)
88 if (dislikes > 0) await sendCreateDislikeToOrigin(account, video, t)
92 fetchRemoteVideoPreview,
93 fetchRemoteVideoDescription,
94 generateThumbnailFromUrl,
95 sendVideoRateChangeToFollowers,
96 sendVideoRateChangeToOrigin