]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/videos.ts
Fix conflict rate serializations
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / videos.ts
CommitLineData
892211e8
C
1import { join } from 'path'
2import * as request from 'request'
0032ebe9 3import { Transaction } from 'sequelize'
892211e8
C
4import { ActivityIconObject } from '../../../shared/index'
5import { doRequest, doRequestAndSaveToFile } from '../../helpers/requests'
6import { CONFIG, REMOTE_SCHEME, STATIC_PATHS } from '../../initializers/constants'
0032ebe9 7import { AccountInstance } from '../../models/account/account-interface'
892211e8 8import { VideoInstance } from '../../models/video/video-interface'
0032ebe9
C
9import { sendLikeToOrigin } from './index'
10import { sendCreateDislikeToOrigin, sendCreateDislikeToVideoFollowers } from './send/send-create'
11import { sendLikeToVideoFollowers } from './send/send-like'
12import {
13 sendUndoDislikeToOrigin,
14 sendUndoDislikeToVideoFollowers,
15 sendUndoLikeToOrigin,
16 sendUndoLikeToVideoFollowers
17} from './send/send-undo'
892211e8
C
18
19function 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
27async 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
40function 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
f00984c0
C
51async 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
0032ebe9
C
59
60 // Undo Like
f00984c0 61 if (likes < 0) await sendUndoLikeToVideoFollowers(account, video, t)
0032ebe9 62 // Undo Dislike
f00984c0 63 if (dislikes < 0) await sendUndoDislikeToVideoFollowers(account, video, t)
0032ebe9 64
f00984c0
C
65 // Like
66 if (likes > 0) await sendLikeToVideoFollowers(account, video, t)
67 // Dislike
68 if (dislikes > 0) await sendCreateDislikeToVideoFollowers(account, video, t)
0032ebe9
C
69}
70
f00984c0
C
71async 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
0032ebe9
C
79
80 // Undo Like
f00984c0 81 if (likes < 0) await sendUndoLikeToOrigin(account, video, t)
0032ebe9 82 // Undo Dislike
f00984c0 83 if (dislikes < 0) await sendUndoDislikeToOrigin(account, video, t)
0032ebe9 84
f00984c0
C
85 // Like
86 if (likes > 0) await sendLikeToOrigin(account, video, t)
87 // Dislike
88 if (dislikes > 0) await sendCreateDislikeToOrigin(account, video, t)
0032ebe9
C
89}
90
892211e8
C
91export {
92 fetchRemoteVideoPreview,
93 fetchRemoteVideoDescription,
0032ebe9
C
94 generateThumbnailFromUrl,
95 sendVideoRateChangeToFollowers,
96 sendVideoRateChangeToOrigin
892211e8 97}