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