Move models to typescript-sequelize
[oweals/peertube.git] / server / lib / activitypub / videos.ts
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'
9 import {
10   sendCreateDislikeToOrigin,
11   sendCreateDislikeToVideoFollowers,
12   sendLikeToOrigin,
13   sendLikeToVideoFollowers,
14   sendUndoDislikeToOrigin,
15   sendUndoDislikeToVideoFollowers,
16   sendUndoLikeToOrigin,
17   sendUndoLikeToVideoFollowers
18 } from './send'
19
20 function fetchRemoteVideoPreview (video: VideoModel) {
21   // FIXME: use url
22   const host = video.VideoChannel.Account.Server.host
23   const path = join(STATIC_PATHS.PREVIEWS, video.getPreviewName())
24
25   return request.get(REMOTE_SCHEME.HTTP + '://' + host + path)
26 }
27
28 async function fetchRemoteVideoDescription (video: VideoModel) {
29   // FIXME: use url
30   const host = video.VideoChannel.Account.Server.host
31   const path = video.getDescriptionPath()
32   const options = {
33     uri: REMOTE_SCHEME.HTTP + '://' + host + path,
34     json: true
35   }
36
37   const { body } = await doRequest(options)
38   return body.description ? body.description : ''
39 }
40
41 function generateThumbnailFromUrl (video: VideoModel, icon: ActivityIconObject) {
42   const thumbnailName = video.getThumbnailName()
43   const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, thumbnailName)
44
45   const options = {
46     method: 'GET',
47     uri: icon.url
48   }
49   return doRequestAndSaveToFile(options, thumbnailPath)
50 }
51
52 async function sendVideoRateChangeToFollowers (
53     account: AccountModel,
54   video: VideoModel,
55   likes: number,
56   dislikes: number,
57   t: Transaction
58 ) {
59   // Keep the order: first we undo and then we create
60
61   // Undo Like
62   if (likes < 0) await sendUndoLikeToVideoFollowers(account, video, t)
63   // Undo Dislike
64   if (dislikes < 0) await sendUndoDislikeToVideoFollowers(account, video, t)
65
66   // Like
67   if (likes > 0) await sendLikeToVideoFollowers(account, video, t)
68   // Dislike
69   if (dislikes > 0) await sendCreateDislikeToVideoFollowers(account, video, t)
70 }
71
72 async function sendVideoRateChangeToOrigin (
73     account: AccountModel,
74   video: VideoModel,
75   likes: number,
76   dislikes: number,
77   t: Transaction
78 ) {
79   // Keep the order: first we undo and then we create
80
81   // Undo Like
82   if (likes < 0) await sendUndoLikeToOrigin(account, video, t)
83   // Undo Dislike
84   if (dislikes < 0) await sendUndoDislikeToOrigin(account, video, t)
85
86   // Like
87   if (likes > 0) await sendLikeToOrigin(account, video, t)
88   // Dislike
89   if (dislikes > 0) await sendCreateDislikeToOrigin(account, video, t)
90 }
91
92 export {
93   fetchRemoteVideoPreview,
94   fetchRemoteVideoDescription,
95   generateThumbnailFromUrl,
96   sendVideoRateChangeToFollowers,
97   sendVideoRateChangeToOrigin
98 }