ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

250 lines
7.0 KiB

  1. import { type UserNotificationSetting, type UserNotificationSettingValueType } from '@peertube/peertube-models'
  2. import { TokensCache } from '@server/lib/auth/tokens-cache.js'
  3. import { MNotificationSettingFormattable } from '@server/types/models/index.js'
  4. import {
  5. AfterDestroy,
  6. AfterUpdate,
  7. AllowNull,
  8. BelongsTo,
  9. Column,
  10. CreatedAt,
  11. Default,
  12. ForeignKey,
  13. Is, Table,
  14. UpdatedAt
  15. } from 'sequelize-typescript'
  16. import { isUserNotificationSettingValid } from '../../helpers/custom-validators/user-notifications.js'
  17. import { SequelizeModel, throwIfNotValid } from '../shared/index.js'
  18. import { UserModel } from './user.js'
  19. @Table({
  20. tableName: 'userNotificationSetting',
  21. indexes: [
  22. {
  23. fields: [ 'userId' ],
  24. unique: true
  25. }
  26. ]
  27. })
  28. export class UserNotificationSettingModel extends SequelizeModel<UserNotificationSettingModel> {
  29. @AllowNull(false)
  30. @Default(null)
  31. @Is(
  32. 'UserNotificationSettingNewVideoFromSubscription',
  33. value => throwIfNotValid(value, isUserNotificationSettingValid, 'newVideoFromSubscription')
  34. )
  35. @Column
  36. newVideoFromSubscription: UserNotificationSettingValueType
  37. @AllowNull(false)
  38. @Default(null)
  39. @Is(
  40. 'UserNotificationSettingNewCommentOnMyVideo',
  41. value => throwIfNotValid(value, isUserNotificationSettingValid, 'newCommentOnMyVideo')
  42. )
  43. @Column
  44. newCommentOnMyVideo: UserNotificationSettingValueType
  45. @AllowNull(false)
  46. @Default(null)
  47. @Is(
  48. 'UserNotificationSettingAbuseAsModerator',
  49. value => throwIfNotValid(value, isUserNotificationSettingValid, 'abuseAsModerator')
  50. )
  51. @Column
  52. abuseAsModerator: UserNotificationSettingValueType
  53. @AllowNull(false)
  54. @Default(null)
  55. @Is(
  56. 'UserNotificationSettingVideoAutoBlacklistAsModerator',
  57. value => throwIfNotValid(value, isUserNotificationSettingValid, 'videoAutoBlacklistAsModerator')
  58. )
  59. @Column
  60. videoAutoBlacklistAsModerator: UserNotificationSettingValueType
  61. @AllowNull(false)
  62. @Default(null)
  63. @Is(
  64. 'UserNotificationSettingBlacklistOnMyVideo',
  65. value => throwIfNotValid(value, isUserNotificationSettingValid, 'blacklistOnMyVideo')
  66. )
  67. @Column
  68. blacklistOnMyVideo: UserNotificationSettingValueType
  69. @AllowNull(false)
  70. @Default(null)
  71. @Is(
  72. 'UserNotificationSettingMyVideoPublished',
  73. value => throwIfNotValid(value, isUserNotificationSettingValid, 'myVideoPublished')
  74. )
  75. @Column
  76. myVideoPublished: UserNotificationSettingValueType
  77. @AllowNull(false)
  78. @Default(null)
  79. @Is(
  80. 'UserNotificationSettingMyVideoImportFinished',
  81. value => throwIfNotValid(value, isUserNotificationSettingValid, 'myVideoImportFinished')
  82. )
  83. @Column
  84. myVideoImportFinished: UserNotificationSettingValueType
  85. @AllowNull(false)
  86. @Default(null)
  87. @Is(
  88. 'UserNotificationSettingNewUserRegistration',
  89. value => throwIfNotValid(value, isUserNotificationSettingValid, 'newUserRegistration')
  90. )
  91. @Column
  92. newUserRegistration: UserNotificationSettingValueType
  93. @AllowNull(false)
  94. @Default(null)
  95. @Is(
  96. 'UserNotificationSettingNewInstanceFollower',
  97. value => throwIfNotValid(value, isUserNotificationSettingValid, 'newInstanceFollower')
  98. )
  99. @Column
  100. newInstanceFollower: UserNotificationSettingValueType
  101. @AllowNull(false)
  102. @Default(null)
  103. @Is(
  104. 'UserNotificationSettingNewInstanceFollower',
  105. value => throwIfNotValid(value, isUserNotificationSettingValid, 'autoInstanceFollowing')
  106. )
  107. @Column
  108. autoInstanceFollowing: UserNotificationSettingValueType
  109. @AllowNull(false)
  110. @Default(null)
  111. @Is(
  112. 'UserNotificationSettingNewFollow',
  113. value => throwIfNotValid(value, isUserNotificationSettingValid, 'newFollow')
  114. )
  115. @Column
  116. newFollow: UserNotificationSettingValueType
  117. @AllowNull(false)
  118. @Default(null)
  119. @Is(
  120. 'UserNotificationSettingCommentMention',
  121. value => throwIfNotValid(value, isUserNotificationSettingValid, 'commentMention')
  122. )
  123. @Column
  124. commentMention: UserNotificationSettingValueType
  125. @AllowNull(false)
  126. @Default(null)
  127. @Is(
  128. 'UserNotificationSettingAbuseStateChange',
  129. value => throwIfNotValid(value, isUserNotificationSettingValid, 'abuseStateChange')
  130. )
  131. @Column
  132. abuseStateChange: UserNotificationSettingValueType
  133. @AllowNull(false)
  134. @Default(null)
  135. @Is(
  136. 'UserNotificationSettingAbuseNewMessage',
  137. value => throwIfNotValid(value, isUserNotificationSettingValid, 'abuseNewMessage')
  138. )
  139. @Column
  140. abuseNewMessage: UserNotificationSettingValueType
  141. @AllowNull(false)
  142. @Default(null)
  143. @Is(
  144. 'UserNotificationSettingNewPeerTubeVersion',
  145. value => throwIfNotValid(value, isUserNotificationSettingValid, 'newPeerTubeVersion')
  146. )
  147. @Column
  148. newPeerTubeVersion: UserNotificationSettingValueType
  149. @AllowNull(false)
  150. @Default(null)
  151. @Is(
  152. 'UserNotificationSettingNewPeerPluginVersion',
  153. value => throwIfNotValid(value, isUserNotificationSettingValid, 'newPluginVersion')
  154. )
  155. @Column
  156. newPluginVersion: UserNotificationSettingValueType
  157. @AllowNull(false)
  158. @Default(null)
  159. @Is(
  160. 'UserNotificationSettingMyVideoStudioEditionFinished',
  161. value => throwIfNotValid(value, isUserNotificationSettingValid, 'myVideoStudioEditionFinished')
  162. )
  163. @Column
  164. myVideoStudioEditionFinished: UserNotificationSettingValueType
  165. @AllowNull(false)
  166. @Default(null)
  167. @Is(
  168. 'UserNotificationSettingTranscriptionGeneratedForOwner',
  169. value => throwIfNotValid(value, isUserNotificationSettingValid, 'myVideoTranscriptionGenerated')
  170. )
  171. @Column
  172. myVideoTranscriptionGenerated: UserNotificationSettingValueType
  173. @ForeignKey(() => UserModel)
  174. @Column
  175. userId: number
  176. @BelongsTo(() => UserModel, {
  177. foreignKey: {
  178. allowNull: false
  179. },
  180. onDelete: 'cascade'
  181. })
  182. User: Awaited<UserModel>
  183. @CreatedAt
  184. createdAt: Date
  185. @UpdatedAt
  186. updatedAt: Date
  187. @AfterUpdate
  188. @AfterDestroy
  189. static removeTokenCache (instance: UserNotificationSettingModel) {
  190. return TokensCache.Instance.clearCacheByUserId(instance.userId)
  191. }
  192. static updateUserSettings (settings: UserNotificationSetting, userId: number) {
  193. const query = {
  194. where: {
  195. userId
  196. }
  197. }
  198. return UserNotificationSettingModel.update(settings, query)
  199. }
  200. toFormattedJSON (this: MNotificationSettingFormattable): UserNotificationSetting {
  201. return {
  202. newCommentOnMyVideo: this.newCommentOnMyVideo,
  203. newVideoFromSubscription: this.newVideoFromSubscription,
  204. abuseAsModerator: this.abuseAsModerator,
  205. videoAutoBlacklistAsModerator: this.videoAutoBlacklistAsModerator,
  206. blacklistOnMyVideo: this.blacklistOnMyVideo,
  207. myVideoPublished: this.myVideoPublished,
  208. myVideoImportFinished: this.myVideoImportFinished,
  209. newUserRegistration: this.newUserRegistration,
  210. commentMention: this.commentMention,
  211. newFollow: this.newFollow,
  212. newInstanceFollower: this.newInstanceFollower,
  213. autoInstanceFollowing: this.autoInstanceFollowing,
  214. abuseNewMessage: this.abuseNewMessage,
  215. abuseStateChange: this.abuseStateChange,
  216. newPeerTubeVersion: this.newPeerTubeVersion,
  217. myVideoStudioEditionFinished: this.myVideoStudioEditionFinished,
  218. myVideoTranscriptionGenerated: this.myVideoTranscriptionGenerated,
  219. newPluginVersion: this.newPluginVersion
  220. }
  221. }
  222. }