From dc13348070d808d0ba3feb56a435b835c2e7e791 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 2 Jan 2019 16:37:43 +0100 Subject: Add import finished and video published notifs --- server/models/account/account-blocklist.ts | 15 +++ server/models/account/user-notification-setting.ts | 22 ++- server/models/account/user-notification.ts | 150 +++++++++++++-------- server/models/account/user.ts | 24 ++++ 4 files changed, 151 insertions(+), 60 deletions(-) (limited to 'server/models/account') diff --git a/server/models/account/account-blocklist.ts b/server/models/account/account-blocklist.ts index fa2819235..54ac290c4 100644 --- a/server/models/account/account-blocklist.ts +++ b/server/models/account/account-blocklist.ts @@ -72,6 +72,21 @@ export class AccountBlocklistModel extends Model { }) BlockedAccount: AccountModel + static isAccountMutedBy (accountId: number, targetAccountId: number) { + const query = { + attributes: [ 'id' ], + where: { + accountId, + targetAccountId + }, + raw: true + } + + return AccountBlocklistModel.unscoped() + .findOne(query) + .then(a => !!a) + } + static loadByAccountAndTarget (accountId: number, targetAccountId: number) { const query = { where: { diff --git a/server/models/account/user-notification-setting.ts b/server/models/account/user-notification-setting.ts index bc24b1e33..6470defa7 100644 --- a/server/models/account/user-notification-setting.ts +++ b/server/models/account/user-notification-setting.ts @@ -65,6 +65,24 @@ export class UserNotificationSettingModel extends Model throwIfNotValid(value, isUserNotificationSettingValid, 'myVideoPublished') + ) + @Column + myVideoPublished: UserNotificationSettingValue + + @AllowNull(false) + @Default(null) + @Is( + 'UserNotificationSettingMyVideoImportFinished', + value => throwIfNotValid(value, isUserNotificationSettingValid, 'myVideoImportFinished') + ) + @Column + myVideoImportFinished: UserNotificationSettingValue + @ForeignKey(() => UserModel) @Column userId: number @@ -94,7 +112,9 @@ export class UserNotificationSettingModel extends Model VideoModel.unscoped(), + required + } +} + +function buildChannelInclude () { + return { + required: true, + attributes: [ 'id', 'name' ], + model: () => VideoChannelModel.unscoped() + } +} + +function buildAccountInclude () { + return { + required: true, + attributes: [ 'id', 'name' ], + model: () => AccountModel.unscoped() + } +} + @Scopes({ [ScopeNames.WITH_ALL]: { include: [ + Object.assign(buildVideoInclude(false), { + include: [ buildChannelInclude() ] + }), { - attributes: [ 'id', 'uuid', 'name' ], - model: () => VideoModel.unscoped(), - required: false, - include: [ - { - required: true, - attributes: [ 'id', 'name' ], - model: () => VideoChannelModel.unscoped() - } - ] - }, - { - attributes: [ 'id' ], + attributes: [ 'id', 'originCommentId' ], model: () => VideoCommentModel.unscoped(), required: false, include: [ - { - required: true, - attributes: [ 'id', 'name' ], - model: () => AccountModel.unscoped() - }, - { - required: true, - attributes: [ 'id', 'uuid', 'name' ], - model: () => VideoModel.unscoped() - } + buildAccountInclude(), + buildVideoInclude(true) ] }, { attributes: [ 'id' ], model: () => VideoAbuseModel.unscoped(), required: false, - include: [ - { - required: true, - attributes: [ 'id', 'uuid', 'name' ], - model: () => VideoModel.unscoped() - } - ] + include: [ buildVideoInclude(true) ] }, { attributes: [ 'id' ], model: () => VideoBlacklistModel.unscoped(), required: false, - include: [ - { - required: true, - attributes: [ 'id', 'uuid', 'name' ], - model: () => VideoModel.unscoped() - } - ] + include: [ buildVideoInclude(true) ] + }, + { + attributes: [ 'id', 'magnetUri', 'targetUrl', 'torrentName' ], + model: () => VideoImportModel.unscoped(), + required: false, + include: [ buildVideoInclude(false) ] } ] } @@ -166,8 +181,20 @@ export class UserNotificationModel extends Model { }) VideoBlacklist: VideoBlacklistModel - static listForApi (userId: number, start: number, count: number, sort: string) { - const query = { + @ForeignKey(() => VideoImportModel) + @Column + videoImportId: number + + @BelongsTo(() => VideoImportModel, { + foreignKey: { + allowNull: true + }, + onDelete: 'cascade' + }) + VideoImport: VideoImportModel + + static listForApi (userId: number, start: number, count: number, sort: string, unread?: boolean) { + const query: IFindOptions = { offset: start, limit: count, order: getSort(sort), @@ -176,6 +203,8 @@ export class UserNotificationModel extends Model { } } + if (unread !== undefined) query.where['read'] = !unread + return UserNotificationModel.scope(ScopeNames.WITH_ALL) .findAndCountAll(query) .then(({ rows, count }) => { @@ -200,45 +229,39 @@ export class UserNotificationModel extends Model { } toFormattedJSON (): UserNotification { - const video = this.Video ? { - id: this.Video.id, - uuid: this.Video.uuid, - name: this.Video.name, + const video = this.Video ? Object.assign(this.formatVideo(this.Video), { channel: { id: this.Video.VideoChannel.id, displayName: this.Video.VideoChannel.getDisplayName() } + }) : undefined + + const videoImport = this.VideoImport ? { + id: this.VideoImport.id, + video: this.VideoImport.Video ? this.formatVideo(this.VideoImport.Video) : undefined, + torrentName: this.VideoImport.torrentName, + magnetUri: this.VideoImport.magnetUri, + targetUrl: this.VideoImport.targetUrl } : undefined const comment = this.Comment ? { id: this.Comment.id, + threadId: this.Comment.getThreadId(), account: { id: this.Comment.Account.id, displayName: this.Comment.Account.getDisplayName() }, - video: { - id: this.Comment.Video.id, - uuid: this.Comment.Video.uuid, - name: this.Comment.Video.name - } + video: this.formatVideo(this.Comment.Video) } : undefined const videoAbuse = this.VideoAbuse ? { id: this.VideoAbuse.id, - video: { - id: this.VideoAbuse.Video.id, - uuid: this.VideoAbuse.Video.uuid, - name: this.VideoAbuse.Video.name - } + video: this.formatVideo(this.VideoAbuse.Video) } : undefined const videoBlacklist = this.VideoBlacklist ? { id: this.VideoBlacklist.id, - video: { - id: this.VideoBlacklist.Video.id, - uuid: this.VideoBlacklist.Video.uuid, - name: this.VideoBlacklist.Video.name - } + video: this.formatVideo(this.VideoBlacklist.Video) } : undefined return { @@ -246,6 +269,7 @@ export class UserNotificationModel extends Model { type: this.type, read: this.read, video, + videoImport, comment, videoAbuse, videoBlacklist, @@ -253,4 +277,12 @@ export class UserNotificationModel extends Model { updatedAt: this.updatedAt.toISOString() } } + + private formatVideo (video: VideoModel) { + return { + id: video.id, + uuid: video.uuid, + name: video.name + } + } } diff --git a/server/models/account/user.ts b/server/models/account/user.ts index 55ec14d05..33f56f641 100644 --- a/server/models/account/user.ts +++ b/server/models/account/user.ts @@ -48,6 +48,7 @@ import { UserNotificationSettingModel } from './user-notification-setting' import { VideoModel } from '../video/video' import { ActorModel } from '../activitypub/actor' import { ActorFollowModel } from '../activitypub/actor-follow' +import { VideoImportModel } from '../video/video-import' enum ScopeNames { WITH_VIDEO_CHANNEL = 'WITH_VIDEO_CHANNEL' @@ -186,6 +187,12 @@ export class UserModel extends Model { }) NotificationSetting: UserNotificationSettingModel + @HasMany(() => VideoImportModel, { + foreignKey: 'userId', + onDelete: 'cascade' + }) + VideoImports: VideoImportModel[] + @HasMany(() => OAuthTokenModel, { foreignKey: 'userId', onDelete: 'cascade' @@ -400,6 +407,23 @@ export class UserModel extends Model { return UserModel.findOne(query) } + static loadByVideoImportId (videoImportId: number) { + const query = { + include: [ + { + required: true, + attributes: [ 'id' ], + model: VideoImportModel.unscoped(), + where: { + id: videoImportId + } + } + ] + } + + return UserModel.findOne(query) + } + static getOriginalVideoFileTotalFromUser (user: UserModel) { // Don't use sequelize because we need to use a sub query const query = UserModel.generateUserQuotaBaseSQL() -- cgit v1.2.3