-import { AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
+import {
+ AllowNull,
+ BelongsTo,
+ Column,
+ CreatedAt,
+ Default,
+ ForeignKey,
+ IFindOptions,
+ Is,
+ Model,
+ Scopes,
+ Table,
+ UpdatedAt
+} from 'sequelize-typescript'
import { UserNotification, UserNotificationType } from '../../../shared'
import { getSort, throwIfNotValid } from '../utils'
import { isBooleanValid } from '../../helpers/custom-validators/misc'
import { AccountModel } from './account'
import { VideoAbuseModel } from '../video/video-abuse'
import { VideoBlacklistModel } from '../video/video-blacklist'
+import { VideoImportModel } from '../video/video-import'
enum ScopeNames {
WITH_ALL = 'WITH_ALL'
}
+function buildVideoInclude (required: boolean) {
+ return {
+ attributes: [ 'id', 'uuid', 'name' ],
+ 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) ]
}
]
}
})
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<UserNotificationModel> = {
offset: start,
limit: count,
order: getSort(sort),
}
}
+ if (unread !== undefined) query.where['read'] = !unread
+
return UserNotificationModel.scope(ScopeNames.WITH_ALL)
.findAndCountAll(query)
.then(({ rows, count }) => {
}
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 {
type: this.type,
read: this.read,
video,
+ videoImport,
comment,
videoAbuse,
videoBlacklist,
updatedAt: this.updatedAt.toISOString()
}
}
+
+ private formatVideo (video: VideoModel) {
+ return {
+ id: video.id,
+ uuid: video.uuid,
+ name: video.name
+ }
+ }
}