]>
Commit | Line | Data |
---|---|---|
1 | import * as Bluebird from 'bluebird' | |
2 | import { VideoTorrentObject } from '../../../../shared' | |
3 | import { ActivityAdd } from '../../../../shared/models/activitypub/activity' | |
4 | import { VideoRateType } from '../../../../shared/models/videos/video-rate.type' | |
5 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | |
6 | import { logger } from '../../../helpers/logger' | |
7 | import { database as db } from '../../../initializers' | |
8 | import { AccountInstance } from '../../../models/account/account-interface' | |
9 | import { VideoChannelInstance } from '../../../models/video/video-channel-interface' | |
10 | import { VideoInstance } from '../../../models/video/video-interface' | |
11 | import { getOrCreateAccountAndServer } from '../account' | |
12 | import { getOrCreateVideoChannel } from '../video-channels' | |
13 | import { generateThumbnailFromUrl } from '../videos' | |
14 | import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc' | |
15 | ||
16 | async function processAddActivity (activity: ActivityAdd) { | |
17 | const activityObject = activity.object | |
18 | const activityType = activityObject.type | |
19 | const account = await getOrCreateAccountAndServer(activity.actor) | |
20 | ||
21 | if (activityType === 'Video') { | |
22 | const videoChannelUrl = activity.target | |
23 | const videoChannel = await getOrCreateVideoChannel(account, videoChannelUrl) | |
24 | ||
25 | return processAddVideo(account, activity, videoChannel, activityObject) | |
26 | } | |
27 | ||
28 | logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id }) | |
29 | return Promise.resolve(undefined) | |
30 | } | |
31 | ||
32 | // --------------------------------------------------------------------------- | |
33 | ||
34 | export { | |
35 | processAddActivity | |
36 | } | |
37 | ||
38 | // --------------------------------------------------------------------------- | |
39 | ||
40 | async function processAddVideo ( | |
41 | account: AccountInstance, | |
42 | activity: ActivityAdd, | |
43 | videoChannel: VideoChannelInstance, | |
44 | videoToCreateData: VideoTorrentObject | |
45 | ) { | |
46 | const options = { | |
47 | arguments: [ account, activity, videoChannel, videoToCreateData ], | |
48 | errorMessage: 'Cannot insert the remote video with many retries.' | |
49 | } | |
50 | ||
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 | ||
62 | return video | |
63 | } | |
64 | ||
65 | function addRemoteVideo (account: AccountInstance, | |
66 | activity: ActivityAdd, | |
67 | videoChannel: VideoChannelInstance, | |
68 | videoToCreateData: VideoTorrentObject) { | |
69 | logger.debug('Adding remote video %s.', videoToCreateData.id) | |
70 | ||
71 | return db.sequelize.transaction(async t => { | |
72 | const sequelizeOptions = { | |
73 | transaction: t | |
74 | } | |
75 | ||
76 | if (videoChannel.Account.id !== account.id) throw new Error('Video channel is not owned by this account.') | |
77 | ||
78 | const videoFromDatabase = await db.Video.loadByUUIDOrURL(videoToCreateData.uuid, videoToCreateData.id, t) | |
79 | if (videoFromDatabase) return videoFromDatabase | |
80 | ||
81 | const videoData = await videoActivityObjectToDBAttributes(videoChannel, videoToCreateData, activity.to, activity.cc) | |
82 | const video = db.Video.build(videoData) | |
83 | ||
84 | // Don't block on request | |
85 | generateThumbnailFromUrl(video, videoToCreateData.icon) | |
86 | .catch(err => logger.warn('Cannot generate thumbnail of %s.', videoToCreateData.id, err)) | |
87 | ||
88 | const videoCreated = await video.save(sequelizeOptions) | |
89 | ||
90 | const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoCreated, videoToCreateData) | |
91 | if (videoFileAttributes.length === 0) { | |
92 | throw new Error('Cannot find valid files for video %s ' + videoToCreateData.url) | |
93 | } | |
94 | ||
95 | const tasks: Bluebird<any>[] = videoFileAttributes.map(f => db.VideoFile.create(f, { transaction: t })) | |
96 | await Promise.all(tasks) | |
97 | ||
98 | const tags = videoToCreateData.tag.map(t => t.name) | |
99 | const tagInstances = await db.Tag.findOrCreateTags(tags, t) | |
100 | await videoCreated.setTags(tagInstances, sequelizeOptions) | |
101 | ||
102 | logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid) | |
103 | ||
104 | return videoCreated | |
105 | }) | |
106 | } | |
107 | ||
108 | async function createRates (accountUrls: string[], video: VideoInstance, rate: VideoRateType) { | |
109 | let rateCounts = 0 | |
110 | const tasks: Bluebird<any>[] = [] | |
111 | ||
112 | for (const accountUrl of accountUrls) { | |
113 | const account = await getOrCreateAccountAndServer(accountUrl) | |
114 | const p = db.AccountVideoRate | |
115 | .create({ | |
116 | videoId: video.id, | |
117 | accountId: account.id, | |
118 | type: rate | |
119 | }) | |
120 | .then(() => rateCounts += 1) | |
121 | ||
122 | tasks.push(p) | |
123 | } | |
124 | ||
125 | await Promise.all(tasks) | |
126 | ||
127 | logger.info('Adding %d %s to video %s.', rateCounts, rate, video.uuid) | |
128 | ||
129 | // This is "likes" and "dislikes" | |
130 | await video.increment(rate + 's', { by: rateCounts }) | |
131 | ||
132 | return | |
133 | } |