diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/controllers/api/users/me.ts | 2 | ||||
-rw-r--r-- | server/models/account/user.ts | 31 | ||||
-rw-r--r-- | server/tests/api/users/users.ts | 6 |
3 files changed, 31 insertions, 8 deletions
diff --git a/server/controllers/api/users/me.ts b/server/controllers/api/users/me.ts index b1f29f252..2f3efe6aa 100644 --- a/server/controllers/api/users/me.ts +++ b/server/controllers/api/users/me.ts | |||
@@ -128,7 +128,7 @@ async function getUserInformation (req: express.Request, res: express.Response) | |||
128 | // We did not load channels in res.locals.user | 128 | // We did not load channels in res.locals.user |
129 | const user = await UserModel.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username) | 129 | const user = await UserModel.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username) |
130 | 130 | ||
131 | return res.json(user.toFormattedJSON()) | 131 | return res.json(user.toFormattedJSON({ me: true })) |
132 | } | 132 | } |
133 | 133 | ||
134 | async function getUserVideoQuotaUsed (req: express.Request, res: express.Response) { | 134 | async function getUserVideoQuotaUsed (req: express.Request, res: express.Response) { |
diff --git a/server/models/account/user.ts b/server/models/account/user.ts index 3a339b5c3..8bd41de22 100644 --- a/server/models/account/user.ts +++ b/server/models/account/user.ts | |||
@@ -19,7 +19,7 @@ import { | |||
19 | Table, | 19 | Table, |
20 | UpdatedAt | 20 | UpdatedAt |
21 | } from 'sequelize-typescript' | 21 | } from 'sequelize-typescript' |
22 | import { hasUserRight, USER_ROLE_LABELS, UserRight, VideoPrivacy } from '../../../shared' | 22 | import { hasUserRight, USER_ROLE_LABELS, UserRight, VideoPrivacy, MyUser } from '../../../shared' |
23 | import { User, UserRole } from '../../../shared/models/users' | 23 | import { User, UserRole } from '../../../shared/models/users' |
24 | import { | 24 | import { |
25 | isNoInstanceConfigWarningModal, | 25 | isNoInstanceConfigWarningModal, |
@@ -45,6 +45,7 @@ import { comparePassword, cryptPassword } from '../../helpers/peertube-crypto' | |||
45 | import { OAuthTokenModel } from '../oauth/oauth-token' | 45 | import { OAuthTokenModel } from '../oauth/oauth-token' |
46 | import { getSort, throwIfNotValid } from '../utils' | 46 | import { getSort, throwIfNotValid } from '../utils' |
47 | import { VideoChannelModel } from '../video/video-channel' | 47 | import { VideoChannelModel } from '../video/video-channel' |
48 | import { VideoPlaylistModel } from '../video/video-playlist' | ||
48 | import { AccountModel } from './account' | 49 | import { AccountModel } from './account' |
49 | import { NSFWPolicyType } from '../../../shared/models/videos/nsfw-policy.type' | 50 | import { NSFWPolicyType } from '../../../shared/models/videos/nsfw-policy.type' |
50 | import { values } from 'lodash' | 51 | import { values } from 'lodash' |
@@ -68,7 +69,8 @@ import { | |||
68 | } from '@server/typings/models' | 69 | } from '@server/typings/models' |
69 | 70 | ||
70 | enum ScopeNames { | 71 | enum ScopeNames { |
71 | WITH_VIDEO_CHANNEL = 'WITH_VIDEO_CHANNEL' | 72 | WITH_VIDEO_CHANNEL = 'WITH_VIDEO_CHANNEL', |
73 | WITH_SPECIAL_PLAYLISTS = 'WITH_SPECIAL_PLAYLISTS' | ||
72 | } | 74 | } |
73 | 75 | ||
74 | @DefaultScope(() => ({ | 76 | @DefaultScope(() => ({ |
@@ -96,6 +98,16 @@ enum ScopeNames { | |||
96 | required: true | 98 | required: true |
97 | } | 99 | } |
98 | ] | 100 | ] |
101 | }, | ||
102 | [ScopeNames.WITH_SPECIAL_PLAYLISTS]: { | ||
103 | attributes: { | ||
104 | include: [ | ||
105 | [ | ||
106 | literal('(select array(select "id" from "videoPlaylist" where "ownerAccountId" in (select id from public.account where "userId" = "UserModel"."id") and name LIKE \'Watch later\'))'), | ||
107 | 'specialPlaylists' | ||
108 | ] | ||
109 | ] | ||
110 | } | ||
99 | } | 111 | } |
100 | })) | 112 | })) |
101 | @Table({ | 113 | @Table({ |
@@ -431,7 +443,10 @@ export class UserModel extends Model<UserModel> { | |||
431 | } | 443 | } |
432 | } | 444 | } |
433 | 445 | ||
434 | return UserModel.scope(ScopeNames.WITH_VIDEO_CHANNEL).findOne(query) | 446 | return UserModel.scope([ |
447 | ScopeNames.WITH_VIDEO_CHANNEL, | ||
448 | ScopeNames.WITH_SPECIAL_PLAYLISTS | ||
449 | ]).findOne(query) | ||
435 | } | 450 | } |
436 | 451 | ||
437 | static loadByEmail (email: string): Bluebird<MUserDefault> { | 452 | static loadByEmail (email: string): Bluebird<MUserDefault> { |
@@ -610,11 +625,11 @@ export class UserModel extends Model<UserModel> { | |||
610 | return comparePassword(password, this.password) | 625 | return comparePassword(password, this.password) |
611 | } | 626 | } |
612 | 627 | ||
613 | toFormattedJSON (this: MUserFormattable, parameters: { withAdminFlags?: boolean } = {}): User { | 628 | toFormattedJSON (this: MUserFormattable, parameters: { withAdminFlags?: boolean, me?: boolean } = {}): User | MyUser { |
614 | const videoQuotaUsed = this.get('videoQuotaUsed') | 629 | const videoQuotaUsed = this.get('videoQuotaUsed') |
615 | const videoQuotaUsedDaily = this.get('videoQuotaUsedDaily') | 630 | const videoQuotaUsedDaily = this.get('videoQuotaUsedDaily') |
616 | 631 | ||
617 | const json: User = { | 632 | const json: User | MyUser = { |
618 | id: this.id, | 633 | id: this.id, |
619 | username: this.username, | 634 | username: this.username, |
620 | email: this.email, | 635 | email: this.email, |
@@ -675,6 +690,12 @@ export class UserModel extends Model<UserModel> { | |||
675 | }) | 690 | }) |
676 | } | 691 | } |
677 | 692 | ||
693 | if (parameters.me) { | ||
694 | Object.assign(json, { | ||
695 | specialPlaylists: (this.get('specialPlaylists') as Array<number>).map(p => ({ id: p })) | ||
696 | }) | ||
697 | } | ||
698 | |||
678 | return json | 699 | return json |
679 | } | 700 | } |
680 | 701 | ||
diff --git a/server/tests/api/users/users.ts b/server/tests/api/users/users.ts index 07b7fc747..3c3ee3ed7 100644 --- a/server/tests/api/users/users.ts +++ b/server/tests/api/users/users.ts | |||
@@ -2,7 +2,7 @@ | |||
2 | 2 | ||
3 | import * as chai from 'chai' | 3 | import * as chai from 'chai' |
4 | import 'mocha' | 4 | import 'mocha' |
5 | import { User, UserRole, Video } from '../../../../shared/index' | 5 | import { User, UserRole, Video, MyUser } from '../../../../shared/index' |
6 | import { | 6 | import { |
7 | blockUser, | 7 | blockUser, |
8 | cleanupTests, | 8 | cleanupTests, |
@@ -251,7 +251,7 @@ describe('Test users', function () { | |||
251 | 251 | ||
252 | it('Should be able to get user information', async function () { | 252 | it('Should be able to get user information', async function () { |
253 | const res1 = await getMyUserInformation(server.url, accessTokenUser) | 253 | const res1 = await getMyUserInformation(server.url, accessTokenUser) |
254 | const userMe: User = res1.body | 254 | const userMe: User & MyUser = res1.body |
255 | 255 | ||
256 | const res2 = await getUserInformation(server.url, server.accessToken, userMe.id) | 256 | const res2 = await getUserInformation(server.url, server.accessToken, userMe.id) |
257 | const userGet: User = res2.body | 257 | const userGet: User = res2.body |
@@ -269,6 +269,8 @@ describe('Test users', function () { | |||
269 | 269 | ||
270 | expect(userMe.adminFlags).to.be.undefined | 270 | expect(userMe.adminFlags).to.be.undefined |
271 | expect(userGet.adminFlags).to.equal(UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST) | 271 | expect(userGet.adminFlags).to.equal(UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST) |
272 | |||
273 | expect(userMe.specialPlaylists).to.have.lengthOf(1) | ||
272 | }) | 274 | }) |
273 | }) | 275 | }) |
274 | 276 | ||