diff options
author | Chocobozzz <me@florianbigard.com> | 2017-12-12 17:53:50 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2017-12-13 16:50:33 +0100 |
commit | 3fd3ab2d34d512b160a5e6084d7609be7b4f4452 (patch) | |
tree | e5ca358287fca6ecacce83defcf23af1e8e9f419 /server/lib/activitypub/process | |
parent | c893d4514e6ecbf282c7985fe5f82b8acd8a1137 (diff) | |
download | PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.tar.gz PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.tar.zst PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.zip |
Move models to typescript-sequelize
Diffstat (limited to 'server/lib/activitypub/process')
-rw-r--r-- | server/lib/activitypub/process/misc.ts | 38 | ||||
-rw-r--r-- | server/lib/activitypub/process/process-accept.ts | 14 | ||||
-rw-r--r-- | server/lib/activitypub/process/process-add.ts | 42 | ||||
-rw-r--r-- | server/lib/activitypub/process/process-announce.ts | 35 | ||||
-rw-r--r-- | server/lib/activitypub/process/process-create.ts | 48 | ||||
-rw-r--r-- | server/lib/activitypub/process/process-delete.ts | 35 | ||||
-rw-r--r-- | server/lib/activitypub/process/process-follow.ts | 22 | ||||
-rw-r--r-- | server/lib/activitypub/process/process-like.ts | 20 | ||||
-rw-r--r-- | server/lib/activitypub/process/process-undo.ts | 37 | ||||
-rw-r--r-- | server/lib/activitypub/process/process-update.ts | 39 | ||||
-rw-r--r-- | server/lib/activitypub/process/process.ts | 10 |
11 files changed, 174 insertions, 166 deletions
diff --git a/server/lib/activitypub/process/misc.ts b/server/lib/activitypub/process/misc.ts index 0baa22c26..a775c858a 100644 --- a/server/lib/activitypub/process/misc.ts +++ b/server/lib/activitypub/process/misc.ts | |||
@@ -1,18 +1,18 @@ | |||
1 | import * as magnetUtil from 'magnet-uri' | 1 | import * as magnetUtil from 'magnet-uri' |
2 | import { VideoTorrentObject } from '../../../../shared' | 2 | import { VideoTorrentObject } from '../../../../shared' |
3 | import { VideoChannelObject } from '../../../../shared/models/activitypub/objects/video-channel-object' | 3 | import { VideoChannelObject } from '../../../../shared/models/activitypub/objects' |
4 | import { VideoPrivacy } from '../../../../shared/models/videos/video-privacy.enum' | 4 | import { VideoPrivacy } from '../../../../shared/models/videos' |
5 | import { doRequest } from '../../../helpers' | ||
5 | import { isVideoFileInfoHashValid } from '../../../helpers/custom-validators/videos' | 6 | import { isVideoFileInfoHashValid } from '../../../helpers/custom-validators/videos' |
6 | import { doRequest } from '../../../helpers/requests' | 7 | import { ACTIVITY_PUB, VIDEO_MIMETYPE_EXT } from '../../../initializers' |
7 | import { database as db } from '../../../initializers' | 8 | import { AccountModel } from '../../../models/account/account' |
8 | import { ACTIVITY_PUB, VIDEO_MIMETYPE_EXT } from '../../../initializers/constants' | 9 | import { VideoModel } from '../../../models/video/video' |
9 | import { AccountInstance } from '../../../models/account/account-interface' | 10 | import { VideoChannelModel } from '../../../models/video/video-channel' |
10 | import { VideoChannelInstance } from '../../../models/video/video-channel-interface' | 11 | import { VideoChannelShareModel } from '../../../models/video/video-channel-share' |
11 | import { VideoFileAttributes } from '../../../models/video/video-file-interface' | 12 | import { VideoShareModel } from '../../../models/video/video-share' |
12 | import { VideoAttributes, VideoInstance } from '../../../models/video/video-interface' | ||
13 | import { getOrCreateAccountAndServer } from '../account' | 13 | import { getOrCreateAccountAndServer } from '../account' |
14 | 14 | ||
15 | function videoChannelActivityObjectToDBAttributes (videoChannelObject: VideoChannelObject, account: AccountInstance) { | 15 | function videoChannelActivityObjectToDBAttributes (videoChannelObject: VideoChannelObject, account: AccountModel) { |
16 | return { | 16 | return { |
17 | name: videoChannelObject.name, | 17 | name: videoChannelObject.name, |
18 | description: videoChannelObject.content, | 18 | description: videoChannelObject.content, |
@@ -26,7 +26,7 @@ function videoChannelActivityObjectToDBAttributes (videoChannelObject: VideoChan | |||
26 | } | 26 | } |
27 | 27 | ||
28 | async function videoActivityObjectToDBAttributes ( | 28 | async function videoActivityObjectToDBAttributes ( |
29 | videoChannel: VideoChannelInstance, | 29 | videoChannel: VideoChannelModel, |
30 | videoObject: VideoTorrentObject, | 30 | videoObject: VideoTorrentObject, |
31 | to: string[] = [], | 31 | to: string[] = [], |
32 | cc: string[] = [] | 32 | cc: string[] = [] |
@@ -56,7 +56,7 @@ async function videoActivityObjectToDBAttributes ( | |||
56 | description = videoObject.content | 56 | description = videoObject.content |
57 | } | 57 | } |
58 | 58 | ||
59 | const videoData: VideoAttributes = { | 59 | return { |
60 | name: videoObject.name, | 60 | name: videoObject.name, |
61 | uuid: videoObject.uuid, | 61 | uuid: videoObject.uuid, |
62 | url: videoObject.id, | 62 | url: videoObject.id, |
@@ -76,11 +76,9 @@ async function videoActivityObjectToDBAttributes ( | |||
76 | remote: true, | 76 | remote: true, |
77 | privacy | 77 | privacy |
78 | } | 78 | } |
79 | |||
80 | return videoData | ||
81 | } | 79 | } |
82 | 80 | ||
83 | function videoFileActivityUrlToDBAttributes (videoCreated: VideoInstance, videoObject: VideoTorrentObject) { | 81 | function videoFileActivityUrlToDBAttributes (videoCreated: VideoModel, videoObject: VideoTorrentObject) { |
84 | const mimeTypes = Object.keys(VIDEO_MIMETYPE_EXT) | 82 | const mimeTypes = Object.keys(VIDEO_MIMETYPE_EXT) |
85 | const fileUrls = videoObject.url.filter(u => { | 83 | const fileUrls = videoObject.url.filter(u => { |
86 | return mimeTypes.indexOf(u.mimeType) !== -1 && u.mimeType.startsWith('video/') | 84 | return mimeTypes.indexOf(u.mimeType) !== -1 && u.mimeType.startsWith('video/') |
@@ -90,7 +88,7 @@ function videoFileActivityUrlToDBAttributes (videoCreated: VideoInstance, videoO | |||
90 | throw new Error('Cannot find video files for ' + videoCreated.url) | 88 | throw new Error('Cannot find video files for ' + videoCreated.url) |
91 | } | 89 | } |
92 | 90 | ||
93 | const attributes: VideoFileAttributes[] = [] | 91 | const attributes = [] |
94 | for (const fileUrl of fileUrls) { | 92 | for (const fileUrl of fileUrls) { |
95 | // Fetch associated magnet uri | 93 | // Fetch associated magnet uri |
96 | const magnet = videoObject.url.find(u => { | 94 | const magnet = videoObject.url.find(u => { |
@@ -115,7 +113,7 @@ function videoFileActivityUrlToDBAttributes (videoCreated: VideoInstance, videoO | |||
115 | return attributes | 113 | return attributes |
116 | } | 114 | } |
117 | 115 | ||
118 | async function addVideoShares (instance: VideoInstance, shares: string[]) { | 116 | async function addVideoShares (instance: VideoModel, shares: string[]) { |
119 | for (const share of shares) { | 117 | for (const share of shares) { |
120 | // Fetch url | 118 | // Fetch url |
121 | const json = await doRequest({ | 119 | const json = await doRequest({ |
@@ -132,14 +130,14 @@ async function addVideoShares (instance: VideoInstance, shares: string[]) { | |||
132 | videoId: instance.id | 130 | videoId: instance.id |
133 | } | 131 | } |
134 | 132 | ||
135 | await db.VideoShare.findOrCreate({ | 133 | await VideoShareModel.findOrCreate({ |
136 | where: entry, | 134 | where: entry, |
137 | defaults: entry | 135 | defaults: entry |
138 | }) | 136 | }) |
139 | } | 137 | } |
140 | } | 138 | } |
141 | 139 | ||
142 | async function addVideoChannelShares (instance: VideoChannelInstance, shares: string[]) { | 140 | async function addVideoChannelShares (instance: VideoChannelModel, shares: string[]) { |
143 | for (const share of shares) { | 141 | for (const share of shares) { |
144 | // Fetch url | 142 | // Fetch url |
145 | const json = await doRequest({ | 143 | const json = await doRequest({ |
@@ -156,7 +154,7 @@ async function addVideoChannelShares (instance: VideoChannelInstance, shares: st | |||
156 | videoChannelId: instance.id | 154 | videoChannelId: instance.id |
157 | } | 155 | } |
158 | 156 | ||
159 | await db.VideoChannelShare.findOrCreate({ | 157 | await VideoChannelShareModel.findOrCreate({ |
160 | where: entry, | 158 | where: entry, |
161 | defaults: entry | 159 | defaults: entry |
162 | }) | 160 | }) |
diff --git a/server/lib/activitypub/process/process-accept.ts b/server/lib/activitypub/process/process-accept.ts index 73c6cb279..5b321f771 100644 --- a/server/lib/activitypub/process/process-accept.ts +++ b/server/lib/activitypub/process/process-accept.ts | |||
@@ -1,12 +1,12 @@ | |||
1 | import { ActivityAccept } from '../../../../shared/models/activitypub/activity' | 1 | import { ActivityAccept } from '../../../../shared/models/activitypub' |
2 | import { database as db } from '../../../initializers' | 2 | import { AccountModel } from '../../../models/account/account' |
3 | import { AccountInstance } from '../../../models/account/account-interface' | 3 | import { AccountFollowModel } from '../../../models/account/account-follow' |
4 | import { addFetchOutboxJob } from '../fetch' | 4 | import { addFetchOutboxJob } from '../fetch' |
5 | 5 | ||
6 | async function processAcceptActivity (activity: ActivityAccept, inboxAccount?: AccountInstance) { | 6 | async function processAcceptActivity (activity: ActivityAccept, inboxAccount?: AccountModel) { |
7 | if (inboxAccount === undefined) throw new Error('Need to accept on explicit inbox.') | 7 | if (inboxAccount === undefined) throw new Error('Need to accept on explicit inbox.') |
8 | 8 | ||
9 | const targetAccount = await db.Account.loadByUrl(activity.actor) | 9 | const targetAccount = await AccountModel.loadByUrl(activity.actor) |
10 | 10 | ||
11 | return processAccept(inboxAccount, targetAccount) | 11 | return processAccept(inboxAccount, targetAccount) |
12 | } | 12 | } |
@@ -19,8 +19,8 @@ export { | |||
19 | 19 | ||
20 | // --------------------------------------------------------------------------- | 20 | // --------------------------------------------------------------------------- |
21 | 21 | ||
22 | async function processAccept (account: AccountInstance, targetAccount: AccountInstance) { | 22 | async function processAccept (account: AccountModel, targetAccount: AccountModel) { |
23 | const follow = await db.AccountFollow.loadByAccountAndTarget(account.id, targetAccount.id) | 23 | const follow = await AccountFollowModel.loadByAccountAndTarget(account.id, targetAccount.id) |
24 | if (!follow) throw new Error('Cannot find associated follow.') | 24 | if (!follow) throw new Error('Cannot find associated follow.') |
25 | 25 | ||
26 | follow.set('state', 'accepted') | 26 | follow.set('state', 'accepted') |
diff --git a/server/lib/activitypub/process/process-add.ts b/server/lib/activitypub/process/process-add.ts index e6bf63eb2..550593eab 100644 --- a/server/lib/activitypub/process/process-add.ts +++ b/server/lib/activitypub/process/process-add.ts | |||
@@ -1,13 +1,15 @@ | |||
1 | import * as Bluebird from 'bluebird' | 1 | import * as Bluebird from 'bluebird' |
2 | import { VideoTorrentObject } from '../../../../shared' | 2 | import { VideoTorrentObject } from '../../../../shared' |
3 | import { ActivityAdd } from '../../../../shared/models/activitypub/activity' | 3 | import { ActivityAdd } from '../../../../shared/models/activitypub' |
4 | import { VideoRateType } from '../../../../shared/models/videos/video-rate.type' | 4 | import { VideoRateType } from '../../../../shared/models/videos' |
5 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | 5 | import { logger, retryTransactionWrapper } from '../../../helpers' |
6 | import { logger } from '../../../helpers/logger' | 6 | import { sequelizeTypescript } from '../../../initializers' |
7 | import { database as db } from '../../../initializers' | 7 | import { AccountModel } from '../../../models/account/account' |
8 | import { AccountInstance } from '../../../models/account/account-interface' | 8 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' |
9 | import { VideoChannelInstance } from '../../../models/video/video-channel-interface' | 9 | import { TagModel } from '../../../models/video/tag' |
10 | import { VideoInstance } from '../../../models/video/video-interface' | 10 | import { VideoModel } from '../../../models/video/video' |
11 | import { VideoChannelModel } from '../../../models/video/video-channel' | ||
12 | import { VideoFileModel } from '../../../models/video/video-file' | ||
11 | import { getOrCreateAccountAndServer } from '../account' | 13 | import { getOrCreateAccountAndServer } from '../account' |
12 | import { getOrCreateVideoChannel } from '../video-channels' | 14 | import { getOrCreateVideoChannel } from '../video-channels' |
13 | import { generateThumbnailFromUrl } from '../videos' | 15 | import { generateThumbnailFromUrl } from '../videos' |
@@ -37,9 +39,9 @@ export { | |||
37 | 39 | ||
38 | // --------------------------------------------------------------------------- | 40 | // --------------------------------------------------------------------------- |
39 | 41 | ||
40 | async function processAddVideo (account: AccountInstance, | 42 | async function processAddVideo (account: AccountModel, |
41 | activity: ActivityAdd, | 43 | activity: ActivityAdd, |
42 | videoChannel: VideoChannelInstance, | 44 | videoChannel: VideoChannelModel, |
43 | videoToCreateData: VideoTorrentObject) { | 45 | videoToCreateData: VideoTorrentObject) { |
44 | const options = { | 46 | const options = { |
45 | arguments: [ account, activity, videoChannel, videoToCreateData ], | 47 | arguments: [ account, activity, videoChannel, videoToCreateData ], |
@@ -64,24 +66,24 @@ async function processAddVideo (account: AccountInstance, | |||
64 | return video | 66 | return video |
65 | } | 67 | } |
66 | 68 | ||
67 | function addRemoteVideo (account: AccountInstance, | 69 | function addRemoteVideo (account: AccountModel, |
68 | activity: ActivityAdd, | 70 | activity: ActivityAdd, |
69 | videoChannel: VideoChannelInstance, | 71 | videoChannel: VideoChannelModel, |
70 | videoToCreateData: VideoTorrentObject) { | 72 | videoToCreateData: VideoTorrentObject) { |
71 | logger.debug('Adding remote video %s.', videoToCreateData.id) | 73 | logger.debug('Adding remote video %s.', videoToCreateData.id) |
72 | 74 | ||
73 | return db.sequelize.transaction(async t => { | 75 | return sequelizeTypescript.transaction(async t => { |
74 | const sequelizeOptions = { | 76 | const sequelizeOptions = { |
75 | transaction: t | 77 | transaction: t |
76 | } | 78 | } |
77 | 79 | ||
78 | if (videoChannel.Account.id !== account.id) throw new Error('Video channel is not owned by this account.') | 80 | if (videoChannel.Account.id !== account.id) throw new Error('Video channel is not owned by this account.') |
79 | 81 | ||
80 | const videoFromDatabase = await db.Video.loadByUUIDOrURL(videoToCreateData.uuid, videoToCreateData.id, t) | 82 | const videoFromDatabase = await VideoModel.loadByUUIDOrURL(videoToCreateData.uuid, videoToCreateData.id, t) |
81 | if (videoFromDatabase) return videoFromDatabase | 83 | if (videoFromDatabase) return videoFromDatabase |
82 | 84 | ||
83 | const videoData = await videoActivityObjectToDBAttributes(videoChannel, videoToCreateData, activity.to, activity.cc) | 85 | const videoData = await videoActivityObjectToDBAttributes(videoChannel, videoToCreateData, activity.to, activity.cc) |
84 | const video = db.Video.build(videoData) | 86 | const video = VideoModel.build(videoData) |
85 | 87 | ||
86 | // Don't block on request | 88 | // Don't block on request |
87 | generateThumbnailFromUrl(video, videoToCreateData.icon) | 89 | generateThumbnailFromUrl(video, videoToCreateData.icon) |
@@ -94,12 +96,12 @@ function addRemoteVideo (account: AccountInstance, | |||
94 | throw new Error('Cannot find valid files for video %s ' + videoToCreateData.url) | 96 | throw new Error('Cannot find valid files for video %s ' + videoToCreateData.url) |
95 | } | 97 | } |
96 | 98 | ||
97 | const tasks: Bluebird<any>[] = videoFileAttributes.map(f => db.VideoFile.create(f, { transaction: t })) | 99 | const tasks: Bluebird<any>[] = videoFileAttributes.map(f => VideoFileModel.create(f, { transaction: t })) |
98 | await Promise.all(tasks) | 100 | await Promise.all(tasks) |
99 | 101 | ||
100 | const tags = videoToCreateData.tag.map(t => t.name) | 102 | const tags = videoToCreateData.tag.map(t => t.name) |
101 | const tagInstances = await db.Tag.findOrCreateTags(tags, t) | 103 | const tagInstances = await TagModel.findOrCreateTags(tags, t) |
102 | await videoCreated.setTags(tagInstances, sequelizeOptions) | 104 | await videoCreated.$set('Tags', tagInstances, sequelizeOptions) |
103 | 105 | ||
104 | logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid) | 106 | logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid) |
105 | 107 | ||
@@ -107,13 +109,13 @@ function addRemoteVideo (account: AccountInstance, | |||
107 | }) | 109 | }) |
108 | } | 110 | } |
109 | 111 | ||
110 | async function createRates (accountUrls: string[], video: VideoInstance, rate: VideoRateType) { | 112 | async function createRates (accountUrls: string[], video: VideoModel, rate: VideoRateType) { |
111 | let rateCounts = 0 | 113 | let rateCounts = 0 |
112 | const tasks: Bluebird<any>[] = [] | 114 | const tasks: Bluebird<any>[] = [] |
113 | 115 | ||
114 | for (const accountUrl of accountUrls) { | 116 | for (const accountUrl of accountUrls) { |
115 | const account = await getOrCreateAccountAndServer(accountUrl) | 117 | const account = await getOrCreateAccountAndServer(accountUrl) |
116 | const p = db.AccountVideoRate | 118 | const p = AccountVideoRateModel |
117 | .create({ | 119 | .create({ |
118 | videoId: video.id, | 120 | videoId: video.id, |
119 | accountId: account.id, | 121 | accountId: account.id, |
diff --git a/server/lib/activitypub/process/process-announce.ts b/server/lib/activitypub/process/process-announce.ts index 2aa9ad5c7..ff2c6d708 100644 --- a/server/lib/activitypub/process/process-announce.ts +++ b/server/lib/activitypub/process/process-announce.ts | |||
@@ -1,10 +1,11 @@ | |||
1 | import { ActivityAdd, ActivityAnnounce, ActivityCreate } from '../../../../shared/models/activitypub/activity' | 1 | import { ActivityAdd, ActivityAnnounce, ActivityCreate } from '../../../../shared/models/activitypub' |
2 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | 2 | import { logger, retryTransactionWrapper } from '../../../helpers' |
3 | import { logger } from '../../../helpers/logger' | 3 | import { sequelizeTypescript } from '../../../initializers' |
4 | import { database as db } from '../../../initializers/index' | 4 | import { AccountModel } from '../../../models/account/account' |
5 | import { AccountInstance } from '../../../models/account/account-interface' | 5 | import { VideoModel } from '../../../models/video/video' |
6 | import { VideoInstance } from '../../../models/index' | 6 | import { VideoChannelModel } from '../../../models/video/video-channel' |
7 | import { VideoChannelInstance } from '../../../models/video/video-channel-interface' | 7 | import { VideoChannelShareModel } from '../../../models/video/video-channel-share' |
8 | import { VideoShareModel } from '../../../models/video/video-share' | ||
8 | import { getOrCreateAccountAndServer } from '../account' | 9 | import { getOrCreateAccountAndServer } from '../account' |
9 | import { forwardActivity } from '../send/misc' | 10 | import { forwardActivity } from '../send/misc' |
10 | import { processAddActivity } from './process-add' | 11 | import { processAddActivity } from './process-add' |
@@ -36,7 +37,7 @@ export { | |||
36 | 37 | ||
37 | // --------------------------------------------------------------------------- | 38 | // --------------------------------------------------------------------------- |
38 | 39 | ||
39 | function processVideoChannelShare (accountAnnouncer: AccountInstance, activity: ActivityAnnounce) { | 40 | function processVideoChannelShare (accountAnnouncer: AccountModel, activity: ActivityAnnounce) { |
40 | const options = { | 41 | const options = { |
41 | arguments: [ accountAnnouncer, activity ], | 42 | arguments: [ accountAnnouncer, activity ], |
42 | errorMessage: 'Cannot share the video channel with many retries.' | 43 | errorMessage: 'Cannot share the video channel with many retries.' |
@@ -45,18 +46,18 @@ function processVideoChannelShare (accountAnnouncer: AccountInstance, activity: | |||
45 | return retryTransactionWrapper(shareVideoChannel, options) | 46 | return retryTransactionWrapper(shareVideoChannel, options) |
46 | } | 47 | } |
47 | 48 | ||
48 | async function shareVideoChannel (accountAnnouncer: AccountInstance, activity: ActivityAnnounce) { | 49 | async function shareVideoChannel (accountAnnouncer: AccountModel, activity: ActivityAnnounce) { |
49 | const announcedActivity = activity.object as ActivityCreate | 50 | const announcedActivity = activity.object as ActivityCreate |
50 | 51 | ||
51 | return db.sequelize.transaction(async t => { | 52 | return sequelizeTypescript.transaction(async t => { |
52 | // Add share entry | 53 | // Add share entry |
53 | const videoChannel: VideoChannelInstance = await processCreateActivity(announcedActivity) | 54 | const videoChannel: VideoChannelModel = await processCreateActivity(announcedActivity) |
54 | const share = { | 55 | const share = { |
55 | accountId: accountAnnouncer.id, | 56 | accountId: accountAnnouncer.id, |
56 | videoChannelId: videoChannel.id | 57 | videoChannelId: videoChannel.id |
57 | } | 58 | } |
58 | 59 | ||
59 | const [ , created ] = await db.VideoChannelShare.findOrCreate({ | 60 | const [ , created ] = await VideoChannelShareModel.findOrCreate({ |
60 | where: share, | 61 | where: share, |
61 | defaults: share, | 62 | defaults: share, |
62 | transaction: t | 63 | transaction: t |
@@ -72,7 +73,7 @@ async function shareVideoChannel (accountAnnouncer: AccountInstance, activity: A | |||
72 | }) | 73 | }) |
73 | } | 74 | } |
74 | 75 | ||
75 | function processVideoShare (accountAnnouncer: AccountInstance, activity: ActivityAnnounce) { | 76 | function processVideoShare (accountAnnouncer: AccountModel, activity: ActivityAnnounce) { |
76 | const options = { | 77 | const options = { |
77 | arguments: [ accountAnnouncer, activity ], | 78 | arguments: [ accountAnnouncer, activity ], |
78 | errorMessage: 'Cannot share the video with many retries.' | 79 | errorMessage: 'Cannot share the video with many retries.' |
@@ -81,19 +82,19 @@ function processVideoShare (accountAnnouncer: AccountInstance, activity: Activit | |||
81 | return retryTransactionWrapper(shareVideo, options) | 82 | return retryTransactionWrapper(shareVideo, options) |
82 | } | 83 | } |
83 | 84 | ||
84 | function shareVideo (accountAnnouncer: AccountInstance, activity: ActivityAnnounce) { | 85 | function shareVideo (accountAnnouncer: AccountModel, activity: ActivityAnnounce) { |
85 | const announcedActivity = activity.object as ActivityAdd | 86 | const announcedActivity = activity.object as ActivityAdd |
86 | 87 | ||
87 | return db.sequelize.transaction(async t => { | 88 | return sequelizeTypescript.transaction(async t => { |
88 | // Add share entry | 89 | // Add share entry |
89 | const video: VideoInstance = await processAddActivity(announcedActivity) | 90 | const video: VideoModel = await processAddActivity(announcedActivity) |
90 | 91 | ||
91 | const share = { | 92 | const share = { |
92 | accountId: accountAnnouncer.id, | 93 | accountId: accountAnnouncer.id, |
93 | videoId: video.id | 94 | videoId: video.id |
94 | } | 95 | } |
95 | 96 | ||
96 | const [ , created ] = await db.VideoShare.findOrCreate({ | 97 | const [ , created ] = await VideoShareModel.findOrCreate({ |
97 | where: share, | 98 | where: share, |
98 | defaults: share, | 99 | defaults: share, |
99 | transaction: t | 100 | transaction: t |
diff --git a/server/lib/activitypub/process/process-create.ts b/server/lib/activitypub/process/process-create.ts index 4740dc432..c1eb2a8ab 100644 --- a/server/lib/activitypub/process/process-create.ts +++ b/server/lib/activitypub/process/process-create.ts | |||
@@ -1,10 +1,12 @@ | |||
1 | import { ActivityCreate, VideoChannelObject } from '../../../../shared' | 1 | import { ActivityCreate, VideoChannelObject } from '../../../../shared' |
2 | import { DislikeObject } from '../../../../shared/models/activitypub/objects/dislike-object' | 2 | import { DislikeObject, VideoAbuseObject, ViewObject } from '../../../../shared/models/activitypub/objects' |
3 | import { VideoAbuseObject } from '../../../../shared/models/activitypub/objects/video-abuse-object' | ||
4 | import { ViewObject } from '../../../../shared/models/activitypub/objects/view-object' | ||
5 | import { logger, retryTransactionWrapper } from '../../../helpers' | 3 | import { logger, retryTransactionWrapper } from '../../../helpers' |
6 | import { database as db } from '../../../initializers' | 4 | import { sequelizeTypescript } from '../../../initializers' |
7 | import { AccountInstance } from '../../../models/account/account-interface' | 5 | import { AccountModel } from '../../../models/account/account' |
6 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' | ||
7 | import { VideoModel } from '../../../models/video/video' | ||
8 | import { VideoAbuseModel } from '../../../models/video/video-abuse' | ||
9 | import { VideoChannelModel } from '../../../models/video/video-channel' | ||
8 | import { getOrCreateAccountAndServer } from '../account' | 10 | import { getOrCreateAccountAndServer } from '../account' |
9 | import { forwardActivity } from '../send/misc' | 11 | import { forwardActivity } from '../send/misc' |
10 | import { getVideoChannelActivityPubUrl } from '../url' | 12 | import { getVideoChannelActivityPubUrl } from '../url' |
@@ -37,7 +39,7 @@ export { | |||
37 | 39 | ||
38 | // --------------------------------------------------------------------------- | 40 | // --------------------------------------------------------------------------- |
39 | 41 | ||
40 | async function processCreateDislike (byAccount: AccountInstance, activity: ActivityCreate) { | 42 | async function processCreateDislike (byAccount: AccountModel, activity: ActivityCreate) { |
41 | const options = { | 43 | const options = { |
42 | arguments: [ byAccount, activity ], | 44 | arguments: [ byAccount, activity ], |
43 | errorMessage: 'Cannot dislike the video with many retries.' | 45 | errorMessage: 'Cannot dislike the video with many retries.' |
@@ -46,11 +48,11 @@ async function processCreateDislike (byAccount: AccountInstance, activity: Activ | |||
46 | return retryTransactionWrapper(createVideoDislike, options) | 48 | return retryTransactionWrapper(createVideoDislike, options) |
47 | } | 49 | } |
48 | 50 | ||
49 | function createVideoDislike (byAccount: AccountInstance, activity: ActivityCreate) { | 51 | function createVideoDislike (byAccount: AccountModel, activity: ActivityCreate) { |
50 | const dislike = activity.object as DislikeObject | 52 | const dislike = activity.object as DislikeObject |
51 | 53 | ||
52 | return db.sequelize.transaction(async t => { | 54 | return sequelizeTypescript.transaction(async t => { |
53 | const video = await db.Video.loadByUrlAndPopulateAccount(dislike.object, t) | 55 | const video = await VideoModel.loadByUrlAndPopulateAccount(dislike.object, t) |
54 | if (!video) throw new Error('Unknown video ' + dislike.object) | 56 | if (!video) throw new Error('Unknown video ' + dislike.object) |
55 | 57 | ||
56 | const rate = { | 58 | const rate = { |
@@ -58,7 +60,7 @@ function createVideoDislike (byAccount: AccountInstance, activity: ActivityCreat | |||
58 | videoId: video.id, | 60 | videoId: video.id, |
59 | accountId: byAccount.id | 61 | accountId: byAccount.id |
60 | } | 62 | } |
61 | const [ , created ] = await db.AccountVideoRate.findOrCreate({ | 63 | const [ , created ] = await AccountVideoRateModel.findOrCreate({ |
62 | where: rate, | 64 | where: rate, |
63 | defaults: rate, | 65 | defaults: rate, |
64 | transaction: t | 66 | transaction: t |
@@ -73,14 +75,14 @@ function createVideoDislike (byAccount: AccountInstance, activity: ActivityCreat | |||
73 | }) | 75 | }) |
74 | } | 76 | } |
75 | 77 | ||
76 | async function processCreateView (byAccount: AccountInstance, activity: ActivityCreate) { | 78 | async function processCreateView (byAccount: AccountModel, activity: ActivityCreate) { |
77 | const view = activity.object as ViewObject | 79 | const view = activity.object as ViewObject |
78 | 80 | ||
79 | const video = await db.Video.loadByUrlAndPopulateAccount(view.object) | 81 | const video = await VideoModel.loadByUrlAndPopulateAccount(view.object) |
80 | 82 | ||
81 | if (!video) throw new Error('Unknown video ' + view.object) | 83 | if (!video) throw new Error('Unknown video ' + view.object) |
82 | 84 | ||
83 | const account = await db.Account.loadByUrl(view.actor) | 85 | const account = await AccountModel.loadByUrl(view.actor) |
84 | if (!account) throw new Error('Unknown account ' + view.actor) | 86 | if (!account) throw new Error('Unknown account ' + view.actor) |
85 | 87 | ||
86 | await video.increment('views') | 88 | await video.increment('views') |
@@ -92,7 +94,7 @@ async function processCreateView (byAccount: AccountInstance, activity: Activity | |||
92 | } | 94 | } |
93 | } | 95 | } |
94 | 96 | ||
95 | async function processCreateVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) { | 97 | async function processCreateVideoChannel (account: AccountModel, videoChannelToCreateData: VideoChannelObject) { |
96 | const options = { | 98 | const options = { |
97 | arguments: [ account, videoChannelToCreateData ], | 99 | arguments: [ account, videoChannelToCreateData ], |
98 | errorMessage: 'Cannot insert the remote video channel with many retries.' | 100 | errorMessage: 'Cannot insert the remote video channel with many retries.' |
@@ -107,15 +109,15 @@ async function processCreateVideoChannel (account: AccountInstance, videoChannel | |||
107 | return videoChannel | 109 | return videoChannel |
108 | } | 110 | } |
109 | 111 | ||
110 | function addRemoteVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) { | 112 | function addRemoteVideoChannel (account: AccountModel, videoChannelToCreateData: VideoChannelObject) { |
111 | logger.debug('Adding remote video channel "%s".', videoChannelToCreateData.uuid) | 113 | logger.debug('Adding remote video channel "%s".', videoChannelToCreateData.uuid) |
112 | 114 | ||
113 | return db.sequelize.transaction(async t => { | 115 | return sequelizeTypescript.transaction(async t => { |
114 | let videoChannel = await db.VideoChannel.loadByUUIDOrUrl(videoChannelToCreateData.uuid, videoChannelToCreateData.id, t) | 116 | let videoChannel = await VideoChannelModel.loadByUUIDOrUrl(videoChannelToCreateData.uuid, videoChannelToCreateData.id, t) |
115 | if (videoChannel) return videoChannel | 117 | if (videoChannel) return videoChannel |
116 | 118 | ||
117 | const videoChannelData = videoChannelActivityObjectToDBAttributes(videoChannelToCreateData, account) | 119 | const videoChannelData = videoChannelActivityObjectToDBAttributes(videoChannelToCreateData, account) |
118 | videoChannel = db.VideoChannel.build(videoChannelData) | 120 | videoChannel = new VideoChannelModel(videoChannelData) |
119 | videoChannel.url = getVideoChannelActivityPubUrl(videoChannel) | 121 | videoChannel.url = getVideoChannelActivityPubUrl(videoChannel) |
120 | 122 | ||
121 | videoChannel = await videoChannel.save({ transaction: t }) | 123 | videoChannel = await videoChannel.save({ transaction: t }) |
@@ -125,7 +127,7 @@ function addRemoteVideoChannel (account: AccountInstance, videoChannelToCreateDa | |||
125 | }) | 127 | }) |
126 | } | 128 | } |
127 | 129 | ||
128 | function processCreateVideoAbuse (account: AccountInstance, videoAbuseToCreateData: VideoAbuseObject) { | 130 | function processCreateVideoAbuse (account: AccountModel, videoAbuseToCreateData: VideoAbuseObject) { |
129 | const options = { | 131 | const options = { |
130 | arguments: [ account, videoAbuseToCreateData ], | 132 | arguments: [ account, videoAbuseToCreateData ], |
131 | errorMessage: 'Cannot insert the remote video abuse with many retries.' | 133 | errorMessage: 'Cannot insert the remote video abuse with many retries.' |
@@ -134,11 +136,11 @@ function processCreateVideoAbuse (account: AccountInstance, videoAbuseToCreateDa | |||
134 | return retryTransactionWrapper(addRemoteVideoAbuse, options) | 136 | return retryTransactionWrapper(addRemoteVideoAbuse, options) |
135 | } | 137 | } |
136 | 138 | ||
137 | function addRemoteVideoAbuse (account: AccountInstance, videoAbuseToCreateData: VideoAbuseObject) { | 139 | function addRemoteVideoAbuse (account: AccountModel, videoAbuseToCreateData: VideoAbuseObject) { |
138 | logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object) | 140 | logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object) |
139 | 141 | ||
140 | return db.sequelize.transaction(async t => { | 142 | return sequelizeTypescript.transaction(async t => { |
141 | const video = await db.Video.loadByUrlAndPopulateAccount(videoAbuseToCreateData.object, t) | 143 | const video = await VideoModel.loadByUrlAndPopulateAccount(videoAbuseToCreateData.object, t) |
142 | if (!video) { | 144 | if (!video) { |
143 | logger.warn('Unknown video %s for remote video abuse.', videoAbuseToCreateData.object) | 145 | logger.warn('Unknown video %s for remote video abuse.', videoAbuseToCreateData.object) |
144 | return undefined | 146 | return undefined |
@@ -150,7 +152,7 @@ function addRemoteVideoAbuse (account: AccountInstance, videoAbuseToCreateData: | |||
150 | videoId: video.id | 152 | videoId: video.id |
151 | } | 153 | } |
152 | 154 | ||
153 | await db.VideoAbuse.create(videoAbuseData) | 155 | await VideoAbuseModel.create(videoAbuseData) |
154 | 156 | ||
155 | logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object) | 157 | logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object) |
156 | }) | 158 | }) |
diff --git a/server/lib/activitypub/process/process-delete.ts b/server/lib/activitypub/process/process-delete.ts index 41cdc236d..8f280d37f 100644 --- a/server/lib/activitypub/process/process-delete.ts +++ b/server/lib/activitypub/process/process-delete.ts | |||
@@ -1,10 +1,9 @@ | |||
1 | import { ActivityDelete } from '../../../../shared/models/activitypub/activity' | 1 | import { ActivityDelete } from '../../../../shared/models/activitypub' |
2 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | 2 | import { logger, retryTransactionWrapper } from '../../../helpers' |
3 | import { logger } from '../../../helpers/logger' | 3 | import { sequelizeTypescript } from '../../../initializers' |
4 | import { database as db } from '../../../initializers' | 4 | import { AccountModel } from '../../../models/account/account' |
5 | import { AccountInstance } from '../../../models/account/account-interface' | 5 | import { VideoModel } from '../../../models/video/video' |
6 | import { VideoChannelInstance } from '../../../models/video/video-channel-interface' | 6 | import { VideoChannelModel } from '../../../models/video/video-channel' |
7 | import { VideoInstance } from '../../../models/video/video-interface' | ||
8 | import { getOrCreateAccountAndServer } from '../account' | 7 | import { getOrCreateAccountAndServer } from '../account' |
9 | 8 | ||
10 | async function processDeleteActivity (activity: ActivityDelete) { | 9 | async function processDeleteActivity (activity: ActivityDelete) { |
@@ -15,14 +14,14 @@ async function processDeleteActivity (activity: ActivityDelete) { | |||
15 | } | 14 | } |
16 | 15 | ||
17 | { | 16 | { |
18 | let videoObject = await db.Video.loadByUrlAndPopulateAccount(activity.id) | 17 | let videoObject = await VideoModel.loadByUrlAndPopulateAccount(activity.id) |
19 | if (videoObject !== undefined) { | 18 | if (videoObject !== undefined) { |
20 | return processDeleteVideo(account, videoObject) | 19 | return processDeleteVideo(account, videoObject) |
21 | } | 20 | } |
22 | } | 21 | } |
23 | 22 | ||
24 | { | 23 | { |
25 | let videoChannelObject = await db.VideoChannel.loadByUrl(activity.id) | 24 | let videoChannelObject = await VideoChannelModel.loadByUrl(activity.id) |
26 | if (videoChannelObject !== undefined) { | 25 | if (videoChannelObject !== undefined) { |
27 | return processDeleteVideoChannel(account, videoChannelObject) | 26 | return processDeleteVideoChannel(account, videoChannelObject) |
28 | } | 27 | } |
@@ -39,7 +38,7 @@ export { | |||
39 | 38 | ||
40 | // --------------------------------------------------------------------------- | 39 | // --------------------------------------------------------------------------- |
41 | 40 | ||
42 | async function processDeleteVideo (account: AccountInstance, videoToDelete: VideoInstance) { | 41 | async function processDeleteVideo (account: AccountModel, videoToDelete: VideoModel) { |
43 | const options = { | 42 | const options = { |
44 | arguments: [ account, videoToDelete ], | 43 | arguments: [ account, videoToDelete ], |
45 | errorMessage: 'Cannot remove the remote video with many retries.' | 44 | errorMessage: 'Cannot remove the remote video with many retries.' |
@@ -48,10 +47,10 @@ async function processDeleteVideo (account: AccountInstance, videoToDelete: Vide | |||
48 | await retryTransactionWrapper(deleteRemoteVideo, options) | 47 | await retryTransactionWrapper(deleteRemoteVideo, options) |
49 | } | 48 | } |
50 | 49 | ||
51 | async function deleteRemoteVideo (account: AccountInstance, videoToDelete: VideoInstance) { | 50 | async function deleteRemoteVideo (account: AccountModel, videoToDelete: VideoModel) { |
52 | logger.debug('Removing remote video "%s".', videoToDelete.uuid) | 51 | logger.debug('Removing remote video "%s".', videoToDelete.uuid) |
53 | 52 | ||
54 | await db.sequelize.transaction(async t => { | 53 | await sequelizeTypescript.transaction(async t => { |
55 | if (videoToDelete.VideoChannel.Account.id !== account.id) { | 54 | if (videoToDelete.VideoChannel.Account.id !== account.id) { |
56 | throw new Error('Account ' + account.url + ' does not own video channel ' + videoToDelete.VideoChannel.url) | 55 | throw new Error('Account ' + account.url + ' does not own video channel ' + videoToDelete.VideoChannel.url) |
57 | } | 56 | } |
@@ -62,7 +61,7 @@ async function deleteRemoteVideo (account: AccountInstance, videoToDelete: Video | |||
62 | logger.info('Remote video with uuid %s removed.', videoToDelete.uuid) | 61 | logger.info('Remote video with uuid %s removed.', videoToDelete.uuid) |
63 | } | 62 | } |
64 | 63 | ||
65 | async function processDeleteVideoChannel (account: AccountInstance, videoChannelToRemove: VideoChannelInstance) { | 64 | async function processDeleteVideoChannel (account: AccountModel, videoChannelToRemove: VideoChannelModel) { |
66 | const options = { | 65 | const options = { |
67 | arguments: [ account, videoChannelToRemove ], | 66 | arguments: [ account, videoChannelToRemove ], |
68 | errorMessage: 'Cannot remove the remote video channel with many retries.' | 67 | errorMessage: 'Cannot remove the remote video channel with many retries.' |
@@ -71,10 +70,10 @@ async function processDeleteVideoChannel (account: AccountInstance, videoChannel | |||
71 | await retryTransactionWrapper(deleteRemoteVideoChannel, options) | 70 | await retryTransactionWrapper(deleteRemoteVideoChannel, options) |
72 | } | 71 | } |
73 | 72 | ||
74 | async function deleteRemoteVideoChannel (account: AccountInstance, videoChannelToRemove: VideoChannelInstance) { | 73 | async function deleteRemoteVideoChannel (account: AccountModel, videoChannelToRemove: VideoChannelModel) { |
75 | logger.debug('Removing remote video channel "%s".', videoChannelToRemove.uuid) | 74 | logger.debug('Removing remote video channel "%s".', videoChannelToRemove.uuid) |
76 | 75 | ||
77 | await db.sequelize.transaction(async t => { | 76 | await sequelizeTypescript.transaction(async t => { |
78 | if (videoChannelToRemove.Account.id !== account.id) { | 77 | if (videoChannelToRemove.Account.id !== account.id) { |
79 | throw new Error('Account ' + account.url + ' does not own video channel ' + videoChannelToRemove.url) | 78 | throw new Error('Account ' + account.url + ' does not own video channel ' + videoChannelToRemove.url) |
80 | } | 79 | } |
@@ -85,7 +84,7 @@ async function deleteRemoteVideoChannel (account: AccountInstance, videoChannelT | |||
85 | logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.uuid) | 84 | logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.uuid) |
86 | } | 85 | } |
87 | 86 | ||
88 | async function processDeleteAccount (accountToRemove: AccountInstance) { | 87 | async function processDeleteAccount (accountToRemove: AccountModel) { |
89 | const options = { | 88 | const options = { |
90 | arguments: [ accountToRemove ], | 89 | arguments: [ accountToRemove ], |
91 | errorMessage: 'Cannot remove the remote account with many retries.' | 90 | errorMessage: 'Cannot remove the remote account with many retries.' |
@@ -94,10 +93,10 @@ async function processDeleteAccount (accountToRemove: AccountInstance) { | |||
94 | await retryTransactionWrapper(deleteRemoteAccount, options) | 93 | await retryTransactionWrapper(deleteRemoteAccount, options) |
95 | } | 94 | } |
96 | 95 | ||
97 | async function deleteRemoteAccount (accountToRemove: AccountInstance) { | 96 | async function deleteRemoteAccount (accountToRemove: AccountModel) { |
98 | logger.debug('Removing remote account "%s".', accountToRemove.uuid) | 97 | logger.debug('Removing remote account "%s".', accountToRemove.uuid) |
99 | 98 | ||
100 | await db.sequelize.transaction(async t => { | 99 | await sequelizeTypescript.transaction(async t => { |
101 | await accountToRemove.destroy({ transaction: t }) | 100 | await accountToRemove.destroy({ transaction: t }) |
102 | }) | 101 | }) |
103 | 102 | ||
diff --git a/server/lib/activitypub/process/process-follow.ts b/server/lib/activitypub/process/process-follow.ts index 320dc1138..ccaee43a6 100644 --- a/server/lib/activitypub/process/process-follow.ts +++ b/server/lib/activitypub/process/process-follow.ts | |||
@@ -1,10 +1,10 @@ | |||
1 | import { ActivityFollow } from '../../../../shared/models/activitypub/activity' | 1 | import { ActivityFollow } from '../../../../shared/models/activitypub' |
2 | import { retryTransactionWrapper } from '../../../helpers' | 2 | import { logger, retryTransactionWrapper } from '../../../helpers' |
3 | import { database as db } from '../../../initializers' | 3 | import { sequelizeTypescript } from '../../../initializers' |
4 | import { AccountInstance } from '../../../models/account/account-interface' | 4 | import { AccountModel } from '../../../models/account/account' |
5 | import { logger } from '../../../helpers/logger' | 5 | import { AccountFollowModel } from '../../../models/account/account-follow' |
6 | import { sendAccept } from '../send/send-accept' | ||
7 | import { getOrCreateAccountAndServer } from '../account' | 6 | import { getOrCreateAccountAndServer } from '../account' |
7 | import { sendAccept } from '../send' | ||
8 | 8 | ||
9 | async function processFollowActivity (activity: ActivityFollow) { | 9 | async function processFollowActivity (activity: ActivityFollow) { |
10 | const activityObject = activity.object | 10 | const activityObject = activity.object |
@@ -21,7 +21,7 @@ export { | |||
21 | 21 | ||
22 | // --------------------------------------------------------------------------- | 22 | // --------------------------------------------------------------------------- |
23 | 23 | ||
24 | function processFollow (account: AccountInstance, targetAccountURL: string) { | 24 | function processFollow (account: AccountModel, targetAccountURL: string) { |
25 | const options = { | 25 | const options = { |
26 | arguments: [ account, targetAccountURL ], | 26 | arguments: [ account, targetAccountURL ], |
27 | errorMessage: 'Cannot follow with many retries.' | 27 | errorMessage: 'Cannot follow with many retries.' |
@@ -30,14 +30,14 @@ function processFollow (account: AccountInstance, targetAccountURL: string) { | |||
30 | return retryTransactionWrapper(follow, options) | 30 | return retryTransactionWrapper(follow, options) |
31 | } | 31 | } |
32 | 32 | ||
33 | async function follow (account: AccountInstance, targetAccountURL: string) { | 33 | async function follow (account: AccountModel, targetAccountURL: string) { |
34 | await db.sequelize.transaction(async t => { | 34 | await sequelizeTypescript.transaction(async t => { |
35 | const targetAccount = await db.Account.loadByUrl(targetAccountURL, t) | 35 | const targetAccount = await AccountModel.loadByUrl(targetAccountURL, t) |
36 | 36 | ||
37 | if (!targetAccount) throw new Error('Unknown account') | 37 | if (!targetAccount) throw new Error('Unknown account') |
38 | if (targetAccount.isOwned() === false) throw new Error('This is not a local account.') | 38 | if (targetAccount.isOwned() === false) throw new Error('This is not a local account.') |
39 | 39 | ||
40 | const [ accountFollow ] = await db.AccountFollow.findOrCreate({ | 40 | const [ accountFollow ] = await AccountFollowModel.findOrCreate({ |
41 | where: { | 41 | where: { |
42 | accountId: account.id, | 42 | accountId: account.id, |
43 | targetAccountId: targetAccount.id | 43 | targetAccountId: targetAccount.id |
diff --git a/server/lib/activitypub/process/process-like.ts b/server/lib/activitypub/process/process-like.ts index 5f2ffe7ea..a6e391f1e 100644 --- a/server/lib/activitypub/process/process-like.ts +++ b/server/lib/activitypub/process/process-like.ts | |||
@@ -1,7 +1,9 @@ | |||
1 | import { ActivityLike } from '../../../../shared/models/activitypub/activity' | 1 | import { ActivityLike } from '../../../../shared/models/activitypub' |
2 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | 2 | import { retryTransactionWrapper } from '../../../helpers' |
3 | import { database as db } from '../../../initializers' | 3 | import { sequelizeTypescript } from '../../../initializers' |
4 | import { AccountInstance } from '../../../models/account/account-interface' | 4 | import { AccountModel } from '../../../models/account/account' |
5 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' | ||
6 | import { VideoModel } from '../../../models/video/video' | ||
5 | import { getOrCreateAccountAndServer } from '../account' | 7 | import { getOrCreateAccountAndServer } from '../account' |
6 | import { forwardActivity } from '../send/misc' | 8 | import { forwardActivity } from '../send/misc' |
7 | 9 | ||
@@ -19,7 +21,7 @@ export { | |||
19 | 21 | ||
20 | // --------------------------------------------------------------------------- | 22 | // --------------------------------------------------------------------------- |
21 | 23 | ||
22 | async function processLikeVideo (byAccount: AccountInstance, activity: ActivityLike) { | 24 | async function processLikeVideo (byAccount: AccountModel, activity: ActivityLike) { |
23 | const options = { | 25 | const options = { |
24 | arguments: [ byAccount, activity ], | 26 | arguments: [ byAccount, activity ], |
25 | errorMessage: 'Cannot like the video with many retries.' | 27 | errorMessage: 'Cannot like the video with many retries.' |
@@ -28,11 +30,11 @@ async function processLikeVideo (byAccount: AccountInstance, activity: ActivityL | |||
28 | return retryTransactionWrapper(createVideoLike, options) | 30 | return retryTransactionWrapper(createVideoLike, options) |
29 | } | 31 | } |
30 | 32 | ||
31 | function createVideoLike (byAccount: AccountInstance, activity: ActivityLike) { | 33 | function createVideoLike (byAccount: AccountModel, activity: ActivityLike) { |
32 | const videoUrl = activity.object | 34 | const videoUrl = activity.object |
33 | 35 | ||
34 | return db.sequelize.transaction(async t => { | 36 | return sequelizeTypescript.transaction(async t => { |
35 | const video = await db.Video.loadByUrlAndPopulateAccount(videoUrl) | 37 | const video = await VideoModel.loadByUrlAndPopulateAccount(videoUrl) |
36 | 38 | ||
37 | if (!video) throw new Error('Unknown video ' + videoUrl) | 39 | if (!video) throw new Error('Unknown video ' + videoUrl) |
38 | 40 | ||
@@ -41,7 +43,7 @@ function createVideoLike (byAccount: AccountInstance, activity: ActivityLike) { | |||
41 | videoId: video.id, | 43 | videoId: video.id, |
42 | accountId: byAccount.id | 44 | accountId: byAccount.id |
43 | } | 45 | } |
44 | const [ , created ] = await db.AccountVideoRate.findOrCreate({ | 46 | const [ , created ] = await AccountVideoRateModel.findOrCreate({ |
45 | where: rate, | 47 | where: rate, |
46 | defaults: rate, | 48 | defaults: rate, |
47 | transaction: t | 49 | transaction: t |
diff --git a/server/lib/activitypub/process/process-undo.ts b/server/lib/activitypub/process/process-undo.ts index cc221045f..efa63122b 100644 --- a/server/lib/activitypub/process/process-undo.ts +++ b/server/lib/activitypub/process/process-undo.ts | |||
@@ -1,8 +1,11 @@ | |||
1 | import { ActivityFollow, ActivityLike, ActivityUndo } from '../../../../shared/models/activitypub/activity' | 1 | import { ActivityFollow, ActivityLike, ActivityUndo } from '../../../../shared/models/activitypub' |
2 | import { DislikeObject } from '../../../../shared/models/activitypub/objects/dislike-object' | 2 | import { DislikeObject } from '../../../../shared/models/activitypub/objects' |
3 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | 3 | import { logger, retryTransactionWrapper } from '../../../helpers' |
4 | import { logger } from '../../../helpers/logger' | 4 | import { sequelizeTypescript } from '../../../initializers' |
5 | import { database as db } from '../../../initializers' | 5 | import { AccountModel } from '../../../models/account/account' |
6 | import { AccountFollowModel } from '../../../models/account/account-follow' | ||
7 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' | ||
8 | import { VideoModel } from '../../../models/video/video' | ||
6 | import { forwardActivity } from '../send/misc' | 9 | import { forwardActivity } from '../send/misc' |
7 | 10 | ||
8 | async function processUndoActivity (activity: ActivityUndo) { | 11 | async function processUndoActivity (activity: ActivityUndo) { |
@@ -41,14 +44,14 @@ function processUndoLike (actor: string, activity: ActivityUndo) { | |||
41 | function undoLike (actor: string, activity: ActivityUndo) { | 44 | function undoLike (actor: string, activity: ActivityUndo) { |
42 | const likeActivity = activity.object as ActivityLike | 45 | const likeActivity = activity.object as ActivityLike |
43 | 46 | ||
44 | return db.sequelize.transaction(async t => { | 47 | return sequelizeTypescript.transaction(async t => { |
45 | const byAccount = await db.Account.loadByUrl(actor, t) | 48 | const byAccount = await AccountModel.loadByUrl(actor, t) |
46 | if (!byAccount) throw new Error('Unknown account ' + actor) | 49 | if (!byAccount) throw new Error('Unknown account ' + actor) |
47 | 50 | ||
48 | const video = await db.Video.loadByUrlAndPopulateAccount(likeActivity.object, t) | 51 | const video = await VideoModel.loadByUrlAndPopulateAccount(likeActivity.object, t) |
49 | if (!video) throw new Error('Unknown video ' + likeActivity.actor) | 52 | if (!video) throw new Error('Unknown video ' + likeActivity.actor) |
50 | 53 | ||
51 | const rate = await db.AccountVideoRate.load(byAccount.id, video.id, t) | 54 | const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t) |
52 | if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`) | 55 | if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`) |
53 | 56 | ||
54 | await rate.destroy({ transaction: t }) | 57 | await rate.destroy({ transaction: t }) |
@@ -74,14 +77,14 @@ function processUndoDislike (actor: string, activity: ActivityUndo) { | |||
74 | function undoDislike (actor: string, activity: ActivityUndo) { | 77 | function undoDislike (actor: string, activity: ActivityUndo) { |
75 | const dislike = activity.object.object as DislikeObject | 78 | const dislike = activity.object.object as DislikeObject |
76 | 79 | ||
77 | return db.sequelize.transaction(async t => { | 80 | return sequelizeTypescript.transaction(async t => { |
78 | const byAccount = await db.Account.loadByUrl(actor, t) | 81 | const byAccount = await AccountModel.loadByUrl(actor, t) |
79 | if (!byAccount) throw new Error('Unknown account ' + actor) | 82 | if (!byAccount) throw new Error('Unknown account ' + actor) |
80 | 83 | ||
81 | const video = await db.Video.loadByUrlAndPopulateAccount(dislike.object, t) | 84 | const video = await VideoModel.loadByUrlAndPopulateAccount(dislike.object, t) |
82 | if (!video) throw new Error('Unknown video ' + dislike.actor) | 85 | if (!video) throw new Error('Unknown video ' + dislike.actor) |
83 | 86 | ||
84 | const rate = await db.AccountVideoRate.load(byAccount.id, video.id, t) | 87 | const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t) |
85 | if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`) | 88 | if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`) |
86 | 89 | ||
87 | await rate.destroy({ transaction: t }) | 90 | await rate.destroy({ transaction: t }) |
@@ -105,10 +108,10 @@ function processUndoFollow (actor: string, followActivity: ActivityFollow) { | |||
105 | } | 108 | } |
106 | 109 | ||
107 | function undoFollow (actor: string, followActivity: ActivityFollow) { | 110 | function undoFollow (actor: string, followActivity: ActivityFollow) { |
108 | return db.sequelize.transaction(async t => { | 111 | return sequelizeTypescript.transaction(async t => { |
109 | const follower = await db.Account.loadByUrl(actor, t) | 112 | const follower = await AccountModel.loadByUrl(actor, t) |
110 | const following = await db.Account.loadByUrl(followActivity.object, t) | 113 | const following = await AccountModel.loadByUrl(followActivity.object, t) |
111 | const accountFollow = await db.AccountFollow.loadByAccountAndTarget(follower.id, following.id, t) | 114 | const accountFollow = await AccountFollowModel.loadByAccountAndTarget(follower.id, following.id, t) |
112 | 115 | ||
113 | if (!accountFollow) throw new Error(`'Unknown account follow ${follower.id} -> ${following.id}.`) | 116 | if (!accountFollow) throw new Error(`'Unknown account follow ${follower.id} -> ${following.id}.`) |
114 | 117 | ||
diff --git a/server/lib/activitypub/process/process-update.ts b/server/lib/activitypub/process/process-update.ts index 11c6de8f5..771021f0c 100644 --- a/server/lib/activitypub/process/process-update.ts +++ b/server/lib/activitypub/process/process-update.ts | |||
@@ -1,12 +1,13 @@ | |||
1 | import * as Bluebird from 'bluebird' | 1 | import * as Bluebird from 'bluebird' |
2 | import { VideoChannelObject, VideoTorrentObject } from '../../../../shared' | 2 | import { VideoChannelObject, VideoTorrentObject } from '../../../../shared' |
3 | import { ActivityUpdate } from '../../../../shared/models/activitypub/activity' | 3 | import { ActivityUpdate } from '../../../../shared/models/activitypub' |
4 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | 4 | import { logger, resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers' |
5 | import { logger } from '../../../helpers/logger' | 5 | import { sequelizeTypescript } from '../../../initializers' |
6 | import { resetSequelizeInstance } from '../../../helpers/utils' | 6 | import { AccountModel } from '../../../models/account/account' |
7 | import { database as db } from '../../../initializers' | 7 | import { TagModel } from '../../../models/video/tag' |
8 | import { AccountInstance } from '../../../models/account/account-interface' | 8 | import { VideoModel } from '../../../models/video/video' |
9 | import { VideoInstance } from '../../../models/video/video-interface' | 9 | import { VideoChannelModel } from '../../../models/video/video-channel' |
10 | import { VideoFileModel } from '../../../models/video/video-file' | ||
10 | import { getOrCreateAccountAndServer } from '../account' | 11 | import { getOrCreateAccountAndServer } from '../account' |
11 | import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc' | 12 | import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc' |
12 | 13 | ||
@@ -30,7 +31,7 @@ export { | |||
30 | 31 | ||
31 | // --------------------------------------------------------------------------- | 32 | // --------------------------------------------------------------------------- |
32 | 33 | ||
33 | function processUpdateVideo (account: AccountInstance, video: VideoTorrentObject) { | 34 | function processUpdateVideo (account: AccountModel, video: VideoTorrentObject) { |
34 | const options = { | 35 | const options = { |
35 | arguments: [ account, video ], | 36 | arguments: [ account, video ], |
36 | errorMessage: 'Cannot update the remote video with many retries' | 37 | errorMessage: 'Cannot update the remote video with many retries' |
@@ -39,18 +40,18 @@ function processUpdateVideo (account: AccountInstance, video: VideoTorrentObject | |||
39 | return retryTransactionWrapper(updateRemoteVideo, options) | 40 | return retryTransactionWrapper(updateRemoteVideo, options) |
40 | } | 41 | } |
41 | 42 | ||
42 | async function updateRemoteVideo (account: AccountInstance, videoAttributesToUpdate: VideoTorrentObject) { | 43 | async function updateRemoteVideo (account: AccountModel, videoAttributesToUpdate: VideoTorrentObject) { |
43 | logger.debug('Updating remote video "%s".', videoAttributesToUpdate.uuid) | 44 | logger.debug('Updating remote video "%s".', videoAttributesToUpdate.uuid) |
44 | let videoInstance: VideoInstance | 45 | let videoInstance: VideoModel |
45 | let videoFieldsSave: object | 46 | let videoFieldsSave: object |
46 | 47 | ||
47 | try { | 48 | try { |
48 | await db.sequelize.transaction(async t => { | 49 | await sequelizeTypescript.transaction(async t => { |
49 | const sequelizeOptions = { | 50 | const sequelizeOptions = { |
50 | transaction: t | 51 | transaction: t |
51 | } | 52 | } |
52 | 53 | ||
53 | const videoInstance = await db.Video.loadByUrlAndPopulateAccount(videoAttributesToUpdate.id, t) | 54 | const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(videoAttributesToUpdate.id, t) |
54 | if (!videoInstance) throw new Error('Video ' + videoAttributesToUpdate.id + ' not found.') | 55 | if (!videoInstance) throw new Error('Video ' + videoAttributesToUpdate.id + ' not found.') |
55 | 56 | ||
56 | if (videoInstance.VideoChannel.Account.id !== account.id) { | 57 | if (videoInstance.VideoChannel.Account.id !== account.id) { |
@@ -81,12 +82,12 @@ async function updateRemoteVideo (account: AccountInstance, videoAttributesToUpd | |||
81 | await Promise.all(videoFileDestroyTasks) | 82 | await Promise.all(videoFileDestroyTasks) |
82 | 83 | ||
83 | const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoInstance, videoAttributesToUpdate) | 84 | const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoInstance, videoAttributesToUpdate) |
84 | const tasks: Bluebird<any>[] = videoFileAttributes.map(f => db.VideoFile.create(f)) | 85 | const tasks: Bluebird<any>[] = videoFileAttributes.map(f => VideoFileModel.create(f)) |
85 | await Promise.all(tasks) | 86 | await Promise.all(tasks) |
86 | 87 | ||
87 | const tags = videoAttributesToUpdate.tag.map(t => t.name) | 88 | const tags = videoAttributesToUpdate.tag.map(t => t.name) |
88 | const tagInstances = await db.Tag.findOrCreateTags(tags, t) | 89 | const tagInstances = await TagModel.findOrCreateTags(tags, t) |
89 | await videoInstance.setTags(tagInstances, sequelizeOptions) | 90 | await videoInstance.$set('Tags', tagInstances, sequelizeOptions) |
90 | }) | 91 | }) |
91 | 92 | ||
92 | logger.info('Remote video with uuid %s updated', videoAttributesToUpdate.uuid) | 93 | logger.info('Remote video with uuid %s updated', videoAttributesToUpdate.uuid) |
@@ -101,7 +102,7 @@ async function updateRemoteVideo (account: AccountInstance, videoAttributesToUpd | |||
101 | } | 102 | } |
102 | } | 103 | } |
103 | 104 | ||
104 | async function processUpdateVideoChannel (account: AccountInstance, videoChannel: VideoChannelObject) { | 105 | async function processUpdateVideoChannel (account: AccountModel, videoChannel: VideoChannelObject) { |
105 | const options = { | 106 | const options = { |
106 | arguments: [ account, videoChannel ], | 107 | arguments: [ account, videoChannel ], |
107 | errorMessage: 'Cannot update the remote video channel with many retries.' | 108 | errorMessage: 'Cannot update the remote video channel with many retries.' |
@@ -110,13 +111,13 @@ async function processUpdateVideoChannel (account: AccountInstance, videoChannel | |||
110 | await retryTransactionWrapper(updateRemoteVideoChannel, options) | 111 | await retryTransactionWrapper(updateRemoteVideoChannel, options) |
111 | } | 112 | } |
112 | 113 | ||
113 | async function updateRemoteVideoChannel (account: AccountInstance, videoChannel: VideoChannelObject) { | 114 | async function updateRemoteVideoChannel (account: AccountModel, videoChannel: VideoChannelObject) { |
114 | logger.debug('Updating remote video channel "%s".', videoChannel.uuid) | 115 | logger.debug('Updating remote video channel "%s".', videoChannel.uuid) |
115 | 116 | ||
116 | await db.sequelize.transaction(async t => { | 117 | await sequelizeTypescript.transaction(async t => { |
117 | const sequelizeOptions = { transaction: t } | 118 | const sequelizeOptions = { transaction: t } |
118 | 119 | ||
119 | const videoChannelInstance = await db.VideoChannel.loadByUrl(videoChannel.id) | 120 | const videoChannelInstance = await VideoChannelModel.loadByUrl(videoChannel.id) |
120 | if (!videoChannelInstance) throw new Error('Video ' + videoChannel.id + ' not found.') | 121 | if (!videoChannelInstance) throw new Error('Video ' + videoChannel.id + ' not found.') |
121 | 122 | ||
122 | if (videoChannelInstance.Account.id !== account.id) { | 123 | if (videoChannelInstance.Account.id !== account.id) { |
diff --git a/server/lib/activitypub/process/process.ts b/server/lib/activitypub/process/process.ts index 54981c289..bfbf8053c 100644 --- a/server/lib/activitypub/process/process.ts +++ b/server/lib/activitypub/process/process.ts | |||
@@ -1,6 +1,6 @@ | |||
1 | import { Activity, ActivityType } from '../../../../shared/models/activitypub/activity' | 1 | import { Activity, ActivityType } from '../../../../shared/models/activitypub' |
2 | import { logger } from '../../../helpers/logger' | 2 | import { logger } from '../../../helpers' |
3 | import { AccountInstance } from '../../../models/account/account-interface' | 3 | import { AccountModel } from '../../../models/account/account' |
4 | import { processAcceptActivity } from './process-accept' | 4 | import { processAcceptActivity } from './process-accept' |
5 | import { processAddActivity } from './process-add' | 5 | import { processAddActivity } from './process-add' |
6 | import { processAnnounceActivity } from './process-announce' | 6 | import { processAnnounceActivity } from './process-announce' |
@@ -11,7 +11,7 @@ import { processLikeActivity } from './process-like' | |||
11 | import { processUndoActivity } from './process-undo' | 11 | import { processUndoActivity } from './process-undo' |
12 | import { processUpdateActivity } from './process-update' | 12 | import { processUpdateActivity } from './process-update' |
13 | 13 | ||
14 | const processActivity: { [ P in ActivityType ]: (activity: Activity, inboxAccount?: AccountInstance) => Promise<any> } = { | 14 | const processActivity: { [ P in ActivityType ]: (activity: Activity, inboxAccount?: AccountModel) => Promise<any> } = { |
15 | Create: processCreateActivity, | 15 | Create: processCreateActivity, |
16 | Add: processAddActivity, | 16 | Add: processAddActivity, |
17 | Update: processUpdateActivity, | 17 | Update: processUpdateActivity, |
@@ -23,7 +23,7 @@ const processActivity: { [ P in ActivityType ]: (activity: Activity, inboxAccoun | |||
23 | Like: processLikeActivity | 23 | Like: processLikeActivity |
24 | } | 24 | } |
25 | 25 | ||
26 | async function processActivities (activities: Activity[], signatureAccount?: AccountInstance, inboxAccount?: AccountInstance) { | 26 | async function processActivities (activities: Activity[], signatureAccount?: AccountModel, inboxAccount?: AccountModel) { |
27 | for (const activity of activities) { | 27 | for (const activity of activities) { |
28 | // When we fetch remote data, we don't have signature | 28 | // When we fetch remote data, we don't have signature |
29 | if (signatureAccount && activity.actor !== signatureAccount.url) { | 29 | if (signatureAccount && activity.actor !== signatureAccount.url) { |