]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/videos.ts
Fix bad to/cc when undo dislike
[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 async function sendVideoRateChangeToFollowers (
52 account: AccountInstance,
53 video: VideoInstance,
54 likes: number,
55 dislikes: number,
56 t: Transaction
57 ) {
58 // Keep the order: first we undo and then we create
59
60 // Undo Like
61 if (likes < 0) await sendUndoLikeToVideoFollowers(account, video, t)
62 // Undo Dislike
63 if (dislikes < 0) await sendUndoDislikeToVideoFollowers(account, video, t)
64
65 // Like
66 if (likes > 0) await sendLikeToVideoFollowers(account, video, t)
67 // Dislike
68 if (dislikes > 0) await sendCreateDislikeToVideoFollowers(account, video, t)
69 }
70
71 async function sendVideoRateChangeToOrigin (
72 account: AccountInstance,
73 video: VideoInstance,
74 likes: number,
75 dislikes: number,
76 t: Transaction
77 ) {
78 // Keep the order: first we undo and then we create
79
80 // Undo Like
81 if (likes < 0) await sendUndoLikeToOrigin(account, video, t)
82 // Undo Dislike
83 if (dislikes < 0) await sendUndoDislikeToOrigin(account, video, t)
84
85 // Like
86 if (likes > 0) await sendLikeToOrigin(account, video, t)
87 // Dislike
88 if (dislikes > 0) await sendCreateDislikeToOrigin(account, video, t)
89 }
90
91 export {
92 fetchRemoteVideoPreview,
93 fetchRemoteVideoDescription,
94 generateThumbnailFromUrl,
95 sendVideoRateChangeToFollowers,
96 sendVideoRateChangeToOrigin
97 }