PeerTube/server/core/models/user/user-notification-setting.ts

250 lines
7.0 KiB
TypeScript
Raw Normal View History

import { type UserNotificationSetting, type UserNotificationSettingValueType } from '@peertube/peertube-models'
import { TokensCache } from '@server/lib/auth/tokens-cache.js'
import { MNotificationSettingFormattable } from '@server/types/models/index.js'
2018-12-26 10:36:24 +01:00
import {
AfterDestroy,
AfterUpdate,
AllowNull,
BelongsTo,
Column,
CreatedAt,
Default,
ForeignKey,
2024-02-22 10:12:04 +01:00
Is, Table,
2018-12-26 10:36:24 +01:00
UpdatedAt
} from 'sequelize-typescript'
import { isUserNotificationSettingValid } from '../../helpers/custom-validators/user-notifications.js'
2024-02-22 10:12:04 +01:00
import { SequelizeModel, throwIfNotValid } from '../shared/index.js'
import { UserModel } from './user.js'
2018-12-26 10:36:24 +01:00
@Table({
tableName: 'userNotificationSetting',
indexes: [
{
fields: [ 'userId' ],
unique: true
}
]
})
2024-02-22 10:12:04 +01:00
export class UserNotificationSettingModel extends SequelizeModel<UserNotificationSettingModel> {
2018-12-26 10:36:24 +01:00
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingNewVideoFromSubscription',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'newVideoFromSubscription')
)
@Column
newVideoFromSubscription: UserNotificationSettingValueType
2018-12-26 10:36:24 +01:00
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingNewCommentOnMyVideo',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'newCommentOnMyVideo')
)
@Column
newCommentOnMyVideo: UserNotificationSettingValueType
2018-12-26 10:36:24 +01:00
@AllowNull(false)
@Default(null)
@Is(
2020-07-07 14:34:16 +02:00
'UserNotificationSettingAbuseAsModerator',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'abuseAsModerator')
2018-12-26 10:36:24 +01:00
)
@Column
abuseAsModerator: UserNotificationSettingValueType
2018-12-26 10:36:24 +01:00
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingVideoAutoBlacklistAsModerator',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'videoAutoBlacklistAsModerator')
)
@Column
videoAutoBlacklistAsModerator: UserNotificationSettingValueType
2018-12-26 10:36:24 +01:00
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingBlacklistOnMyVideo',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'blacklistOnMyVideo')
)
@Column
blacklistOnMyVideo: UserNotificationSettingValueType
2018-12-26 10:36:24 +01:00
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingMyVideoPublished',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'myVideoPublished')
)
@Column
myVideoPublished: UserNotificationSettingValueType
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingMyVideoImportFinished',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'myVideoImportFinished')
)
@Column
myVideoImportFinished: UserNotificationSettingValueType
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingNewUserRegistration',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'newUserRegistration')
)
@Column
newUserRegistration: UserNotificationSettingValueType
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingNewInstanceFollower',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'newInstanceFollower')
)
@Column
newInstanceFollower: UserNotificationSettingValueType
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingNewInstanceFollower',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'autoInstanceFollowing')
)
@Column
autoInstanceFollowing: UserNotificationSettingValueType
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingNewFollow',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'newFollow')
)
@Column
newFollow: UserNotificationSettingValueType
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingCommentMention',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'commentMention')
)
@Column
commentMention: UserNotificationSettingValueType
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingAbuseStateChange',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'abuseStateChange')
)
@Column
abuseStateChange: UserNotificationSettingValueType
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingAbuseNewMessage',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'abuseNewMessage')
)
@Column
abuseNewMessage: UserNotificationSettingValueType
2021-03-11 16:54:52 +01:00
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingNewPeerTubeVersion',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'newPeerTubeVersion')
)
@Column
newPeerTubeVersion: UserNotificationSettingValueType
2021-03-11 16:54:52 +01:00
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingNewPeerPluginVersion',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'newPluginVersion')
)
@Column
newPluginVersion: UserNotificationSettingValueType
2021-03-11 16:54:52 +01:00
@AllowNull(false)
@Default(null)
@Is(
2022-03-22 16:58:49 +01:00
'UserNotificationSettingMyVideoStudioEditionFinished',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'myVideoStudioEditionFinished')
)
@Column
myVideoStudioEditionFinished: UserNotificationSettingValueType
2024-06-13 09:23:12 +02:00
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingTranscriptionGeneratedForOwner',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'myVideoTranscriptionGenerated')
)
@Column
myVideoTranscriptionGenerated: UserNotificationSettingValueType
2018-12-26 10:36:24 +01:00
@ForeignKey(() => UserModel)
@Column
userId: number
@BelongsTo(() => UserModel, {
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
User: Awaited<UserModel>
2018-12-26 10:36:24 +01:00
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@AfterUpdate
@AfterDestroy
static removeTokenCache (instance: UserNotificationSettingModel) {
return TokensCache.Instance.clearCacheByUserId(instance.userId)
2018-12-26 10:36:24 +01:00
}
2024-02-12 10:47:52 +01:00
static updateUserSettings (settings: UserNotificationSetting, userId: number) {
const query = {
where: {
userId
}
}
return UserNotificationSettingModel.update(settings, query)
}
2019-08-20 19:05:31 +02:00
toFormattedJSON (this: MNotificationSettingFormattable): UserNotificationSetting {
2018-12-26 10:36:24 +01:00
return {
newCommentOnMyVideo: this.newCommentOnMyVideo,
newVideoFromSubscription: this.newVideoFromSubscription,
2020-07-07 14:34:16 +02:00
abuseAsModerator: this.abuseAsModerator,
videoAutoBlacklistAsModerator: this.videoAutoBlacklistAsModerator,
blacklistOnMyVideo: this.blacklistOnMyVideo,
myVideoPublished: this.myVideoPublished,
myVideoImportFinished: this.myVideoImportFinished,
newUserRegistration: this.newUserRegistration,
commentMention: this.commentMention,
newFollow: this.newFollow,
newInstanceFollower: this.newInstanceFollower,
autoInstanceFollowing: this.autoInstanceFollowing,
abuseNewMessage: this.abuseNewMessage,
2021-03-11 16:54:52 +01:00
abuseStateChange: this.abuseStateChange,
newPeerTubeVersion: this.newPeerTubeVersion,
2022-03-22 16:58:49 +01:00
myVideoStudioEditionFinished: this.myVideoStudioEditionFinished,
2024-06-13 09:23:12 +02:00
myVideoTranscriptionGenerated: this.myVideoTranscriptionGenerated,
2021-03-11 16:54:52 +01:00
newPluginVersion: this.newPluginVersion
2018-12-26 10:36:24 +01:00
}
}
}