user-notification-setting.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import {
  2. AfterDestroy,
  3. AfterUpdate,
  4. AllowNull,
  5. BelongsTo,
  6. Column,
  7. CreatedAt,
  8. Default,
  9. ForeignKey,
  10. Is,
  11. Model,
  12. Table,
  13. UpdatedAt
  14. } from 'sequelize-typescript'
  15. import { throwIfNotValid } from '../utils'
  16. import { UserModel } from './user'
  17. import { isUserNotificationSettingValid } from '../../helpers/custom-validators/user-notifications'
  18. import { UserNotificationSetting, UserNotificationSettingValue } from '../../../shared/models/users/user-notification-setting.model'
  19. import { clearCacheByUserId } from '../../lib/oauth-model'
  20. @Table({
  21. tableName: 'userNotificationSetting',
  22. indexes: [
  23. {
  24. fields: [ 'userId' ],
  25. unique: true
  26. }
  27. ]
  28. })
  29. export class UserNotificationSettingModel extends Model<UserNotificationSettingModel> {
  30. @AllowNull(false)
  31. @Default(null)
  32. @Is(
  33. 'UserNotificationSettingNewVideoFromSubscription',
  34. value => throwIfNotValid(value, isUserNotificationSettingValid, 'newVideoFromSubscription')
  35. )
  36. @Column
  37. newVideoFromSubscription: UserNotificationSettingValue
  38. @AllowNull(false)
  39. @Default(null)
  40. @Is(
  41. 'UserNotificationSettingNewCommentOnMyVideo',
  42. value => throwIfNotValid(value, isUserNotificationSettingValid, 'newCommentOnMyVideo')
  43. )
  44. @Column
  45. newCommentOnMyVideo: UserNotificationSettingValue
  46. @AllowNull(false)
  47. @Default(null)
  48. @Is(
  49. 'UserNotificationSettingVideoAbuseAsModerator',
  50. value => throwIfNotValid(value, isUserNotificationSettingValid, 'videoAbuseAsModerator')
  51. )
  52. @Column
  53. videoAbuseAsModerator: UserNotificationSettingValue
  54. @AllowNull(false)
  55. @Default(null)
  56. @Is(
  57. 'UserNotificationSettingVideoAutoBlacklistAsModerator',
  58. value => throwIfNotValid(value, isUserNotificationSettingValid, 'videoAutoBlacklistAsModerator')
  59. )
  60. @Column
  61. videoAutoBlacklistAsModerator: UserNotificationSettingValue
  62. @AllowNull(false)
  63. @Default(null)
  64. @Is(
  65. 'UserNotificationSettingBlacklistOnMyVideo',
  66. value => throwIfNotValid(value, isUserNotificationSettingValid, 'blacklistOnMyVideo')
  67. )
  68. @Column
  69. blacklistOnMyVideo: UserNotificationSettingValue
  70. @AllowNull(false)
  71. @Default(null)
  72. @Is(
  73. 'UserNotificationSettingMyVideoPublished',
  74. value => throwIfNotValid(value, isUserNotificationSettingValid, 'myVideoPublished')
  75. )
  76. @Column
  77. myVideoPublished: UserNotificationSettingValue
  78. @AllowNull(false)
  79. @Default(null)
  80. @Is(
  81. 'UserNotificationSettingMyVideoImportFinished',
  82. value => throwIfNotValid(value, isUserNotificationSettingValid, 'myVideoImportFinished')
  83. )
  84. @Column
  85. myVideoImportFinished: UserNotificationSettingValue
  86. @AllowNull(false)
  87. @Default(null)
  88. @Is(
  89. 'UserNotificationSettingNewUserRegistration',
  90. value => throwIfNotValid(value, isUserNotificationSettingValid, 'newUserRegistration')
  91. )
  92. @Column
  93. newUserRegistration: UserNotificationSettingValue
  94. @AllowNull(false)
  95. @Default(null)
  96. @Is(
  97. 'UserNotificationSettingNewInstanceFollower',
  98. value => throwIfNotValid(value, isUserNotificationSettingValid, 'newInstanceFollower')
  99. )
  100. @Column
  101. newInstanceFollower: UserNotificationSettingValue
  102. @AllowNull(false)
  103. @Default(null)
  104. @Is(
  105. 'UserNotificationSettingNewFollow',
  106. value => throwIfNotValid(value, isUserNotificationSettingValid, 'newFollow')
  107. )
  108. @Column
  109. newFollow: UserNotificationSettingValue
  110. @AllowNull(false)
  111. @Default(null)
  112. @Is(
  113. 'UserNotificationSettingCommentMention',
  114. value => throwIfNotValid(value, isUserNotificationSettingValid, 'commentMention')
  115. )
  116. @Column
  117. commentMention: UserNotificationSettingValue
  118. @ForeignKey(() => UserModel)
  119. @Column
  120. userId: number
  121. @BelongsTo(() => UserModel, {
  122. foreignKey: {
  123. allowNull: false
  124. },
  125. onDelete: 'cascade'
  126. })
  127. User: UserModel
  128. @CreatedAt
  129. createdAt: Date
  130. @UpdatedAt
  131. updatedAt: Date
  132. @AfterUpdate
  133. @AfterDestroy
  134. static removeTokenCache (instance: UserNotificationSettingModel) {
  135. return clearCacheByUserId(instance.userId)
  136. }
  137. toFormattedJSON (): UserNotificationSetting {
  138. return {
  139. newCommentOnMyVideo: this.newCommentOnMyVideo,
  140. newVideoFromSubscription: this.newVideoFromSubscription,
  141. videoAbuseAsModerator: this.videoAbuseAsModerator,
  142. videoAutoBlacklistAsModerator: this.videoAutoBlacklistAsModerator,
  143. blacklistOnMyVideo: this.blacklistOnMyVideo,
  144. myVideoPublished: this.myVideoPublished,
  145. myVideoImportFinished: this.myVideoImportFinished,
  146. newUserRegistration: this.newUserRegistration,
  147. commentMention: this.commentMention,
  148. newFollow: this.newFollow,
  149. newInstanceFollower: this.newInstanceFollower
  150. }
  151. }
  152. }