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