From 29128b2f5ce00093ad81b4b72daae0e3444fd5a8 Mon Sep 17 00:00:00 2001 From: Rigel Kent Date: Thu, 2 Jan 2020 13:07:18 +0100 Subject: Add miniature quick actions to add video to Watch later playlist --- server/controllers/api/users/me.ts | 2 +- server/models/account/user.ts | 31 ++++++++++++++++++++++++++----- server/tests/api/users/users.ts | 6 ++++-- 3 files changed, 31 insertions(+), 8 deletions(-) (limited to 'server') 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) // We did not load channels in res.locals.user const user = await UserModel.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username) - return res.json(user.toFormattedJSON()) + return res.json(user.toFormattedJSON({ me: true })) } 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 { Table, UpdatedAt } from 'sequelize-typescript' -import { hasUserRight, USER_ROLE_LABELS, UserRight, VideoPrivacy } from '../../../shared' +import { hasUserRight, USER_ROLE_LABELS, UserRight, VideoPrivacy, MyUser } from '../../../shared' import { User, UserRole } from '../../../shared/models/users' import { isNoInstanceConfigWarningModal, @@ -45,6 +45,7 @@ import { comparePassword, cryptPassword } from '../../helpers/peertube-crypto' import { OAuthTokenModel } from '../oauth/oauth-token' import { getSort, throwIfNotValid } from '../utils' import { VideoChannelModel } from '../video/video-channel' +import { VideoPlaylistModel } from '../video/video-playlist' import { AccountModel } from './account' import { NSFWPolicyType } from '../../../shared/models/videos/nsfw-policy.type' import { values } from 'lodash' @@ -68,7 +69,8 @@ import { } from '@server/typings/models' enum ScopeNames { - WITH_VIDEO_CHANNEL = 'WITH_VIDEO_CHANNEL' + WITH_VIDEO_CHANNEL = 'WITH_VIDEO_CHANNEL', + WITH_SPECIAL_PLAYLISTS = 'WITH_SPECIAL_PLAYLISTS' } @DefaultScope(() => ({ @@ -96,6 +98,16 @@ enum ScopeNames { required: true } ] + }, + [ScopeNames.WITH_SPECIAL_PLAYLISTS]: { + attributes: { + include: [ + [ + literal('(select array(select "id" from "videoPlaylist" where "ownerAccountId" in (select id from public.account where "userId" = "UserModel"."id") and name LIKE \'Watch later\'))'), + 'specialPlaylists' + ] + ] + } } })) @Table({ @@ -431,7 +443,10 @@ export class UserModel extends Model { } } - return UserModel.scope(ScopeNames.WITH_VIDEO_CHANNEL).findOne(query) + return UserModel.scope([ + ScopeNames.WITH_VIDEO_CHANNEL, + ScopeNames.WITH_SPECIAL_PLAYLISTS + ]).findOne(query) } static loadByEmail (email: string): Bluebird { @@ -610,11 +625,11 @@ export class UserModel extends Model { return comparePassword(password, this.password) } - toFormattedJSON (this: MUserFormattable, parameters: { withAdminFlags?: boolean } = {}): User { + toFormattedJSON (this: MUserFormattable, parameters: { withAdminFlags?: boolean, me?: boolean } = {}): User | MyUser { const videoQuotaUsed = this.get('videoQuotaUsed') const videoQuotaUsedDaily = this.get('videoQuotaUsedDaily') - const json: User = { + const json: User | MyUser = { id: this.id, username: this.username, email: this.email, @@ -675,6 +690,12 @@ export class UserModel extends Model { }) } + if (parameters.me) { + Object.assign(json, { + specialPlaylists: (this.get('specialPlaylists') as Array).map(p => ({ id: p })) + }) + } + return json } 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 @@ import * as chai from 'chai' import 'mocha' -import { User, UserRole, Video } from '../../../../shared/index' +import { User, UserRole, Video, MyUser } from '../../../../shared/index' import { blockUser, cleanupTests, @@ -251,7 +251,7 @@ describe('Test users', function () { it('Should be able to get user information', async function () { const res1 = await getMyUserInformation(server.url, accessTokenUser) - const userMe: User = res1.body + const userMe: User & MyUser = res1.body const res2 = await getUserInformation(server.url, server.accessToken, userMe.id) const userGet: User = res2.body @@ -269,6 +269,8 @@ describe('Test users', function () { expect(userMe.adminFlags).to.be.undefined expect(userGet.adminFlags).to.equal(UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST) + + expect(userMe.specialPlaylists).to.have.lengthOf(1) }) }) -- cgit v1.2.3