]>
Commit | Line | Data |
---|---|---|
d7d5611c | 1 | import * as Bluebird from 'bluebird' |
54141398 | 2 | import { VideoTorrentObject } from '../../../../shared' |
3fd3ab2d C |
3 | import { ActivityAdd } from '../../../../shared/models/activitypub' |
4 | import { VideoRateType } from '../../../../shared/models/videos' | |
5 | import { logger, retryTransactionWrapper } from '../../../helpers' | |
6 | import { sequelizeTypescript } from '../../../initializers' | |
7 | import { AccountModel } from '../../../models/account/account' | |
8 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' | |
9 | import { TagModel } from '../../../models/video/tag' | |
10 | import { VideoModel } from '../../../models/video/video' | |
11 | import { VideoChannelModel } from '../../../models/video/video-channel' | |
12 | import { VideoFileModel } from '../../../models/video/video-file' | |
0f91ae62 | 13 | import { getOrCreateAccountAndServer } from '../account' |
892211e8 C |
14 | import { getOrCreateVideoChannel } from '../video-channels' |
15 | import { generateThumbnailFromUrl } from '../videos' | |
4e50b6a1 | 16 | import { addVideoShares, videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc' |
0d0e8dd0 C |
17 | |
18 | async function processAddActivity (activity: ActivityAdd) { | |
19 | const activityObject = activity.object | |
20 | const activityType = activityObject.type | |
0f91ae62 | 21 | const account = await getOrCreateAccountAndServer(activity.actor) |
0d0e8dd0 C |
22 | |
23 | if (activityType === 'Video') { | |
20494f12 C |
24 | const videoChannelUrl = activity.target |
25 | const videoChannel = await getOrCreateVideoChannel(account, videoChannelUrl) | |
26 | ||
79d5caf9 | 27 | return processAddVideo(account, activity, videoChannel, activityObject) |
0d0e8dd0 C |
28 | } |
29 | ||
30 | logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id }) | |
31 | return Promise.resolve(undefined) | |
32 | } | |
33 | ||
34 | // --------------------------------------------------------------------------- | |
35 | ||
36 | export { | |
37 | processAddActivity | |
38 | } | |
39 | ||
40 | // --------------------------------------------------------------------------- | |
41 | ||
3fd3ab2d | 42 | async function processAddVideo (account: AccountModel, |
4e50b6a1 | 43 | activity: ActivityAdd, |
3fd3ab2d | 44 | videoChannel: VideoChannelModel, |
4e50b6a1 | 45 | videoToCreateData: VideoTorrentObject) { |
0d0e8dd0 | 46 | const options = { |
16b90975 | 47 | arguments: [ account, activity, videoChannel, videoToCreateData ], |
0d0e8dd0 C |
48 | errorMessage: 'Cannot insert the remote video with many retries.' |
49 | } | |
50 | ||
16b90975 C |
51 | const video = await retryTransactionWrapper(addRemoteVideo, options) |
52 | ||
53 | // Process outside the transaction because we could fetch remote data | |
54 | if (videoToCreateData.likes && Array.isArray(videoToCreateData.likes.orderedItems)) { | |
55 | await createRates(videoToCreateData.likes.orderedItems, video, 'like') | |
56 | } | |
57 | ||
58 | if (videoToCreateData.dislikes && Array.isArray(videoToCreateData.dislikes.orderedItems)) { | |
59 | await createRates(videoToCreateData.dislikes.orderedItems, video, 'dislike') | |
60 | } | |
61 | ||
4e50b6a1 C |
62 | if (videoToCreateData.shares && Array.isArray(videoToCreateData.shares.orderedItems)) { |
63 | await addVideoShares(video, videoToCreateData.shares.orderedItems) | |
64 | } | |
65 | ||
16b90975 | 66 | return video |
0d0e8dd0 C |
67 | } |
68 | ||
3fd3ab2d | 69 | function addRemoteVideo (account: AccountModel, |
892211e8 | 70 | activity: ActivityAdd, |
3fd3ab2d | 71 | videoChannel: VideoChannelModel, |
892211e8 | 72 | videoToCreateData: VideoTorrentObject) { |
c46edbc2 | 73 | logger.debug('Adding remote video %s.', videoToCreateData.id) |
0d0e8dd0 | 74 | |
3fd3ab2d | 75 | return sequelizeTypescript.transaction(async t => { |
0d0e8dd0 C |
76 | const sequelizeOptions = { |
77 | transaction: t | |
78 | } | |
79 | ||
0d0e8dd0 C |
80 | if (videoChannel.Account.id !== account.id) throw new Error('Video channel is not owned by this account.') |
81 | ||
3fd3ab2d | 82 | const videoFromDatabase = await VideoModel.loadByUUIDOrURL(videoToCreateData.uuid, videoToCreateData.id, t) |
63c93323 | 83 | if (videoFromDatabase) return videoFromDatabase |
d7d5611c | 84 | |
9a27cdc2 | 85 | const videoData = await videoActivityObjectToDBAttributes(videoChannel, videoToCreateData, activity.to, activity.cc) |
3fd3ab2d | 86 | const video = VideoModel.build(videoData) |
0d0e8dd0 C |
87 | |
88 | // Don't block on request | |
89 | generateThumbnailFromUrl(video, videoToCreateData.icon) | |
350e31d6 | 90 | .catch(err => logger.warn('Cannot generate thumbnail of %s.', videoToCreateData.id, err)) |
0d0e8dd0 C |
91 | |
92 | const videoCreated = await video.save(sequelizeOptions) | |
93 | ||
79d5caf9 | 94 | const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoCreated, videoToCreateData) |
20494f12 C |
95 | if (videoFileAttributes.length === 0) { |
96 | throw new Error('Cannot find valid files for video %s ' + videoToCreateData.url) | |
97 | } | |
0d0e8dd0 | 98 | |
3fd3ab2d | 99 | const tasks: Bluebird<any>[] = videoFileAttributes.map(f => VideoFileModel.create(f, { transaction: t })) |
0d0e8dd0 C |
100 | await Promise.all(tasks) |
101 | ||
102 | const tags = videoToCreateData.tag.map(t => t.name) | |
3fd3ab2d C |
103 | const tagInstances = await TagModel.findOrCreateTags(tags, t) |
104 | await videoCreated.$set('Tags', tagInstances, sequelizeOptions) | |
d8465018 C |
105 | |
106 | logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid) | |
107 | ||
108 | return videoCreated | |
0d0e8dd0 | 109 | }) |
0d0e8dd0 | 110 | } |
16b90975 | 111 | |
3fd3ab2d | 112 | async function createRates (accountUrls: string[], video: VideoModel, rate: VideoRateType) { |
16b90975 C |
113 | let rateCounts = 0 |
114 | const tasks: Bluebird<any>[] = [] | |
115 | ||
116 | for (const accountUrl of accountUrls) { | |
117 | const account = await getOrCreateAccountAndServer(accountUrl) | |
3fd3ab2d | 118 | const p = AccountVideoRateModel |
16b90975 C |
119 | .create({ |
120 | videoId: video.id, | |
121 | accountId: account.id, | |
122 | type: rate | |
123 | }) | |
124 | .then(() => rateCounts += 1) | |
125 | ||
126 | tasks.push(p) | |
127 | } | |
128 | ||
129 | await Promise.all(tasks) | |
130 | ||
131 | logger.info('Adding %d %s to video %s.', rateCounts, rate, video.uuid) | |
132 | ||
133 | // This is "likes" and "dislikes" | |
134 | await video.increment(rate + 's', { by: rateCounts }) | |
135 | ||
136 | return | |
137 | } |