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