From 8a19bee1a1ee39f973bb37429e4f73c3f2873cdb Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 17 Aug 2018 15:45:42 +0200 Subject: Add ability to set a name to a channel --- server/models/video/video-channel.ts | 56 +++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 14 deletions(-) (limited to 'server/models/video') diff --git a/server/models/video/video-channel.ts b/server/models/video/video-channel.ts index 0273fab13..9f80e0b8d 100644 --- a/server/models/video/video-channel.ts +++ b/server/models/video/video-channel.ts @@ -29,6 +29,7 @@ import { getSort, throwIfNotValid } from '../utils' import { VideoModel } from './video' import { CONSTRAINTS_FIELDS } from '../../initializers' import { AvatarModel } from '../avatar/avatar' +import { ServerModel } from '../server/server' enum ScopeNames { WITH_ACCOUNT = 'WITH_ACCOUNT', @@ -206,7 +207,7 @@ export class VideoChannelModel extends Model { } static loadByIdAndAccount (id: number, accountId: number) { - const options = { + const query = { where: { id, accountId @@ -215,7 +216,7 @@ export class VideoChannelModel extends Model { return VideoChannelModel .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ]) - .findOne(options) + .findOne(query) } static loadAndPopulateAccount (id: number) { @@ -225,7 +226,7 @@ export class VideoChannelModel extends Model { } static loadByUUIDAndPopulateAccount (uuid: string) { - const options = { + const query = { include: [ { model: ActorModel, @@ -239,36 +240,63 @@ export class VideoChannelModel extends Model { return VideoChannelModel .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ]) - .findOne(options) + .findOne(query) } - static loadAndPopulateAccountAndVideos (id: number) { - const options = { + static loadLocalByNameAndPopulateAccount (name: string) { + const query = { include: [ - VideoModel + { + model: ActorModel, + required: true, + where: { + preferredUsername: name, + serverId: null + } + } ] } return VideoChannelModel - .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEOS ]) - .findById(id, options) + .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ]) + .findOne(query) } - static loadLocalByName (name: string) { + static loadByNameAndHostAndPopulateAccount (name: string, host: string) { const query = { include: [ { model: ActorModel, required: true, where: { - preferredUsername: name, - serverId: null - } + preferredUsername: name + }, + include: [ + { + model: ServerModel, + required: true, + where: { host } + } + ] } ] } - return VideoChannelModel.findOne(query) + return VideoChannelModel + .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ]) + .findOne(query) + } + + static loadAndPopulateAccountAndVideos (id: number) { + const options = { + include: [ + VideoModel + ] + } + + return VideoChannelModel + .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEOS ]) + .findById(id, options) } toFormattedJSON (): VideoChannel { -- cgit v1.2.3