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