]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/videos.ts
Fix preview 404
[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 4import { ActivityIconObject } from '../../../shared/index'
3fd3ab2d
C
5import { doRequest, doRequestAndSaveToFile } from '../../helpers'
6import { CONFIG, REMOTE_SCHEME, STATIC_PATHS } from '../../initializers'
7import { AccountModel } from '../../models/account/account'
8import { VideoModel } from '../../models/video/video'
0032ebe9 9import {
3fd3ab2d
C
10 sendCreateDislikeToOrigin,
11 sendCreateDislikeToVideoFollowers,
12 sendLikeToOrigin,
13 sendLikeToVideoFollowers,
0032ebe9
C
14 sendUndoDislikeToOrigin,
15 sendUndoDislikeToVideoFollowers,
16 sendUndoLikeToOrigin,
17 sendUndoLikeToVideoFollowers
3fd3ab2d 18} from './send'
892211e8 19
d50acfab 20function fetchRemoteVideoPreview (video: VideoModel, reject: Function) {
892211e8 21 // FIXME: use url
50d6de9c 22 const host = video.VideoChannel.Account.Actor.Server.host
892211e8
C
23 const path = join(STATIC_PATHS.PREVIEWS, video.getPreviewName())
24
f40bbe31
C
25 return request.get(REMOTE_SCHEME.HTTP + '://' + host + path, err => {
26 if (err) reject(err)
27 })
892211e8
C
28}
29
3fd3ab2d 30async function fetchRemoteVideoDescription (video: VideoModel) {
892211e8 31 // FIXME: use url
50d6de9c 32 const host = video.VideoChannel.Account.Actor.Server.host
892211e8
C
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
3fd3ab2d 43function generateThumbnailFromUrl (video: VideoModel, icon: ActivityIconObject) {
892211e8
C
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
f00984c0 54async function sendVideoRateChangeToFollowers (
50d6de9c 55 account: AccountModel,
3fd3ab2d 56 video: VideoModel,
f00984c0
C
57 likes: number,
58 dislikes: number,
59 t: Transaction
60) {
50d6de9c
C
61 const actor = account.Actor
62
f00984c0 63 // Keep the order: first we undo and then we create
0032ebe9
C
64
65 // Undo Like
50d6de9c 66 if (likes < 0) await sendUndoLikeToVideoFollowers(actor, video, t)
0032ebe9 67 // Undo Dislike
50d6de9c 68 if (dislikes < 0) await sendUndoDislikeToVideoFollowers(actor, video, t)
0032ebe9 69
f00984c0 70 // Like
50d6de9c 71 if (likes > 0) await sendLikeToVideoFollowers(actor, video, t)
f00984c0 72 // Dislike
50d6de9c 73 if (dislikes > 0) await sendCreateDislikeToVideoFollowers(actor, video, t)
0032ebe9
C
74}
75
f00984c0 76async function sendVideoRateChangeToOrigin (
50d6de9c 77 account: AccountModel,
3fd3ab2d 78 video: VideoModel,
f00984c0
C
79 likes: number,
80 dislikes: number,
81 t: Transaction
82) {
50d6de9c
C
83 const actor = account.Actor
84
f00984c0 85 // Keep the order: first we undo and then we create
0032ebe9
C
86
87 // Undo Like
50d6de9c 88 if (likes < 0) await sendUndoLikeToOrigin(actor, video, t)
0032ebe9 89 // Undo Dislike
50d6de9c 90 if (dislikes < 0) await sendUndoDislikeToOrigin(actor, video, t)
0032ebe9 91
f00984c0 92 // Like
50d6de9c 93 if (likes > 0) await sendLikeToOrigin(actor, video, t)
f00984c0 94 // Dislike
50d6de9c 95 if (dislikes > 0) await sendCreateDislikeToOrigin(actor, video, t)
0032ebe9
C
96}
97
892211e8
C
98export {
99 fetchRemoteVideoPreview,
100 fetchRemoteVideoDescription,
0032ebe9
C
101 generateThumbnailFromUrl,
102 sendVideoRateChangeToFollowers,
103 sendVideoRateChangeToOrigin
892211e8 104}