diff options
-rw-r--r-- | server/controllers/api/users/me.ts | 22 | ||||
-rw-r--r-- | server/models/video/video.ts | 14 | ||||
-rw-r--r-- | server/tests/fixtures/peertube-plugin-test/main.js | 10 | ||||
-rw-r--r-- | server/tests/plugins/filter-hooks.ts | 15 | ||||
-rw-r--r-- | shared/models/plugins/server-hook.model.ts | 4 |
5 files changed, 52 insertions, 13 deletions
diff --git a/server/controllers/api/users/me.ts b/server/controllers/api/users/me.ts index 009cf42b7..c4e8d8130 100644 --- a/server/controllers/api/users/me.ts +++ b/server/controllers/api/users/me.ts | |||
@@ -1,7 +1,8 @@ | |||
1 | import 'multer' | 1 | import 'multer' |
2 | import * as express from 'express' | 2 | import * as express from 'express' |
3 | import { auditLoggerFactory, getAuditIdFromRes, UserAuditView } from '@server/helpers/audit-logger' | 3 | import { auditLoggerFactory, getAuditIdFromRes, UserAuditView } from '@server/helpers/audit-logger' |
4 | import { UserUpdateMe, UserVideoRate as FormattedUserVideoRate, VideoSortField } from '../../../../shared' | 4 | import { Hooks } from '@server/lib/plugins/hooks' |
5 | import { UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../../shared' | ||
5 | import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' | 6 | import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' |
6 | import { UserVideoQuota } from '../../../../shared/models/users/user-video-quota.model' | 7 | import { UserVideoQuota } from '../../../../shared/models/users/user-video-quota.model' |
7 | import { createReqFiles } from '../../../helpers/express-utils' | 8 | import { createReqFiles } from '../../../helpers/express-utils' |
@@ -104,12 +105,19 @@ export { | |||
104 | 105 | ||
105 | async function getUserVideos (req: express.Request, res: express.Response) { | 106 | async function getUserVideos (req: express.Request, res: express.Response) { |
106 | const user = res.locals.oauth.token.User | 107 | const user = res.locals.oauth.token.User |
107 | const resultList = await VideoModel.listUserVideosForApi( | 108 | |
108 | user.Account.id, | 109 | const apiOptions = await Hooks.wrapObject({ |
109 | req.query.start as number, | 110 | accountId: user.Account.id, |
110 | req.query.count as number, | 111 | start: req.query.start, |
111 | req.query.sort as VideoSortField, | 112 | count: req.query.count, |
112 | req.query.search as string | 113 | sort: req.query.sort, |
114 | search: req.query.search | ||
115 | }, 'filter:api.user.me.videos.list.params') | ||
116 | |||
117 | const resultList = await Hooks.wrapPromiseFun( | ||
118 | VideoModel.listUserVideosForApi, | ||
119 | apiOptions, | ||
120 | 'filter:api.user.me.videos.list.result' | ||
113 | ) | 121 | ) |
114 | 122 | ||
115 | const additionalAttributes = { | 123 | const additionalAttributes = { |
diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 5027e980d..3db6549ae 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts | |||
@@ -1004,13 +1004,15 @@ export class VideoModel extends Model { | |||
1004 | return result.map(v => v.id) | 1004 | return result.map(v => v.id) |
1005 | } | 1005 | } |
1006 | 1006 | ||
1007 | static listUserVideosForApi ( | 1007 | static listUserVideosForApi (options: { |
1008 | accountId: number, | 1008 | accountId: number |
1009 | start: number, | 1009 | start: number |
1010 | count: number, | 1010 | count: number |
1011 | sort: string, | 1011 | sort: string |
1012 | search?: string | 1012 | search?: string |
1013 | ) { | 1013 | }) { |
1014 | const { accountId, start, count, sort, search } = options | ||
1015 | |||
1014 | function buildBaseQuery (): FindOptions { | 1016 | function buildBaseQuery (): FindOptions { |
1015 | let baseQuery = { | 1017 | let baseQuery = { |
1016 | offset: start, | 1018 | offset: start, |
diff --git a/server/tests/fixtures/peertube-plugin-test/main.js b/server/tests/fixtures/peertube-plugin-test/main.js index e5a9f8df5..305d92002 100644 --- a/server/tests/fixtures/peertube-plugin-test/main.js +++ b/server/tests/fixtures/peertube-plugin-test/main.js | |||
@@ -60,6 +60,16 @@ async function register ({ registerHook, registerSetting, settingsManager, stora | |||
60 | }) | 60 | }) |
61 | 61 | ||
62 | registerHook({ | 62 | registerHook({ |
63 | target: 'filter:api.user.me.videos.list.params', | ||
64 | handler: obj => addToCount(obj, 4) | ||
65 | }) | ||
66 | |||
67 | registerHook({ | ||
68 | target: 'filter:api.user.me.videos.list.result', | ||
69 | handler: obj => addToTotal(obj, 4) | ||
70 | }) | ||
71 | |||
72 | registerHook({ | ||
63 | target: 'filter:api.video.get.result', | 73 | target: 'filter:api.video.get.result', |
64 | handler: video => { | 74 | handler: video => { |
65 | video.name += ' <3' | 75 | video.name += ' <3' |
diff --git a/server/tests/plugins/filter-hooks.ts b/server/tests/plugins/filter-hooks.ts index cdde6cd30..d88170201 100644 --- a/server/tests/plugins/filter-hooks.ts +++ b/server/tests/plugins/filter-hooks.ts | |||
@@ -10,6 +10,7 @@ import { | |||
10 | doubleFollow, | 10 | doubleFollow, |
11 | getAccountVideos, | 11 | getAccountVideos, |
12 | getConfig, | 12 | getConfig, |
13 | getMyVideos, | ||
13 | getPluginTestPath, | 14 | getPluginTestPath, |
14 | getVideo, | 15 | getVideo, |
15 | getVideoChannelVideos, | 16 | getVideoChannelVideos, |
@@ -121,6 +122,20 @@ describe('Test plugin filter hooks', function () { | |||
121 | expect(res.body.total).to.equal(13) | 122 | expect(res.body.total).to.equal(13) |
122 | }) | 123 | }) |
123 | 124 | ||
125 | it('Should run filter:api.user.me.videos.list.params', async function () { | ||
126 | const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 2) | ||
127 | |||
128 | // 1 plugin do +4 to the count parameter | ||
129 | expect(res.body.data).to.have.lengthOf(6) | ||
130 | }) | ||
131 | |||
132 | it('Should run filter:api.user.me.videos.list.result', async function () { | ||
133 | const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 2) | ||
134 | |||
135 | // Plugin do +4 to the total result | ||
136 | expect(res.body.total).to.equal(14) | ||
137 | }) | ||
138 | |||
124 | it('Should run filter:api.video.get.result', async function () { | 139 | it('Should run filter:api.video.get.result', async function () { |
125 | const res = await getVideo(servers[0].url, videoUUID) | 140 | const res = await getVideo(servers[0].url, videoUUID) |
126 | 141 | ||
diff --git a/shared/models/plugins/server-hook.model.ts b/shared/models/plugins/server-hook.model.ts index c82abadd7..082b4b591 100644 --- a/shared/models/plugins/server-hook.model.ts +++ b/shared/models/plugins/server-hook.model.ts | |||
@@ -14,6 +14,10 @@ export const serverFilterHookObject = { | |||
14 | 'filter:api.video-channels.videos.list.params': true, | 14 | 'filter:api.video-channels.videos.list.params': true, |
15 | 'filter:api.video-channels.videos.list.result': true, | 15 | 'filter:api.video-channels.videos.list.result': true, |
16 | 16 | ||
17 | // Filter params/result used to list my user videos for the REST API | ||
18 | 'filter:api.user.me.videos.list.params': true, | ||
19 | 'filter:api.user.me.videos.list.result': true, | ||
20 | |||
17 | // Filter the result of the get function | 21 | // Filter the result of the get function |
18 | // Used to get detailed video information (video watch page for example) | 22 | // Used to get detailed video information (video watch page for example) |
19 | 'filter:api.video.get.result': true, | 23 | 'filter:api.video.get.result': true, |