]>
Commit | Line | Data |
---|---|---|
892211e8 C |
1 | import { join } from 'path' |
2 | import * as request from 'request' | |
0032ebe9 | 3 | import { Transaction } from 'sequelize' |
892211e8 | 4 | import { ActivityIconObject } from '../../../shared/index' |
3fd3ab2d C |
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' | |
0032ebe9 | 9 | import { |
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 | |
3fd3ab2d | 20 | function fetchRemoteVideoPreview (video: VideoModel) { |
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 | ||
25 | return request.get(REMOTE_SCHEME.HTTP + '://' + host + path) | |
26 | } | |
27 | ||
3fd3ab2d | 28 | async function fetchRemoteVideoDescription (video: VideoModel) { |
892211e8 | 29 | // FIXME: use url |
50d6de9c | 30 | const host = video.VideoChannel.Account.Actor.Server.host |
892211e8 C |
31 | const path = video.getDescriptionPath() |
32 | const options = { | |
33 | uri: REMOTE_SCHEME.HTTP + '://' + host + path, | |
34 | json: true | |
35 | } | |
36 | ||
37 | const { body } = await doRequest(options) | |
38 | return body.description ? body.description : '' | |
39 | } | |
40 | ||
3fd3ab2d | 41 | function generateThumbnailFromUrl (video: VideoModel, icon: ActivityIconObject) { |
892211e8 C |
42 | const thumbnailName = video.getThumbnailName() |
43 | const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, thumbnailName) | |
44 | ||
45 | const options = { | |
46 | method: 'GET', | |
47 | uri: icon.url | |
48 | } | |
49 | return doRequestAndSaveToFile(options, thumbnailPath) | |
50 | } | |
51 | ||
f00984c0 | 52 | async function sendVideoRateChangeToFollowers ( |
50d6de9c | 53 | account: AccountModel, |
3fd3ab2d | 54 | video: VideoModel, |
f00984c0 C |
55 | likes: number, |
56 | dislikes: number, | |
57 | t: Transaction | |
58 | ) { | |
50d6de9c C |
59 | const actor = account.Actor |
60 | ||
f00984c0 | 61 | // Keep the order: first we undo and then we create |
0032ebe9 C |
62 | |
63 | // Undo Like | |
50d6de9c | 64 | if (likes < 0) await sendUndoLikeToVideoFollowers(actor, video, t) |
0032ebe9 | 65 | // Undo Dislike |
50d6de9c | 66 | if (dislikes < 0) await sendUndoDislikeToVideoFollowers(actor, video, t) |
0032ebe9 | 67 | |
f00984c0 | 68 | // Like |
50d6de9c | 69 | if (likes > 0) await sendLikeToVideoFollowers(actor, video, t) |
f00984c0 | 70 | // Dislike |
50d6de9c | 71 | if (dislikes > 0) await sendCreateDislikeToVideoFollowers(actor, video, t) |
0032ebe9 C |
72 | } |
73 | ||
f00984c0 | 74 | async function sendVideoRateChangeToOrigin ( |
50d6de9c | 75 | account: AccountModel, |
3fd3ab2d | 76 | video: VideoModel, |
f00984c0 C |
77 | likes: number, |
78 | dislikes: number, | |
79 | t: Transaction | |
80 | ) { | |
50d6de9c C |
81 | const actor = account.Actor |
82 | ||
f00984c0 | 83 | // Keep the order: first we undo and then we create |
0032ebe9 C |
84 | |
85 | // Undo Like | |
50d6de9c | 86 | if (likes < 0) await sendUndoLikeToOrigin(actor, video, t) |
0032ebe9 | 87 | // Undo Dislike |
50d6de9c | 88 | if (dislikes < 0) await sendUndoDislikeToOrigin(actor, video, t) |
0032ebe9 | 89 | |
f00984c0 | 90 | // Like |
50d6de9c | 91 | if (likes > 0) await sendLikeToOrigin(actor, video, t) |
f00984c0 | 92 | // Dislike |
50d6de9c | 93 | if (dislikes > 0) await sendCreateDislikeToOrigin(actor, video, t) |
0032ebe9 C |
94 | } |
95 | ||
892211e8 C |
96 | export { |
97 | fetchRemoteVideoPreview, | |
98 | fetchRemoteVideoDescription, | |
0032ebe9 C |
99 | generateThumbnailFromUrl, |
100 | sendVideoRateChangeToFollowers, | |
101 | sendVideoRateChangeToOrigin | |
892211e8 | 102 | } |