user-video-history.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, IsInt, Model, Table, UpdatedAt } from 'sequelize-typescript'
  2. import { VideoModel } from '../video/video'
  3. import { UserModel } from './user'
  4. import { Transaction, Op, DestroyOptions } from 'sequelize'
  5. @Table({
  6. tableName: 'userVideoHistory',
  7. indexes: [
  8. {
  9. fields: [ 'userId', 'videoId' ],
  10. unique: true
  11. },
  12. {
  13. fields: [ 'userId' ]
  14. },
  15. {
  16. fields: [ 'videoId' ]
  17. }
  18. ]
  19. })
  20. export class UserVideoHistoryModel extends Model<UserVideoHistoryModel> {
  21. @CreatedAt
  22. createdAt: Date
  23. @UpdatedAt
  24. updatedAt: Date
  25. @AllowNull(false)
  26. @IsInt
  27. @Column
  28. currentTime: number
  29. @ForeignKey(() => VideoModel)
  30. @Column
  31. videoId: number
  32. @BelongsTo(() => VideoModel, {
  33. foreignKey: {
  34. allowNull: false
  35. },
  36. onDelete: 'CASCADE'
  37. })
  38. Video: VideoModel
  39. @ForeignKey(() => UserModel)
  40. @Column
  41. userId: number
  42. @BelongsTo(() => UserModel, {
  43. foreignKey: {
  44. allowNull: false
  45. },
  46. onDelete: 'CASCADE'
  47. })
  48. User: UserModel
  49. static listForApi (user: UserModel, start: number, count: number) {
  50. return VideoModel.listForApi({
  51. start,
  52. count,
  53. sort: '-UserVideoHistories.updatedAt',
  54. nsfw: null, // All
  55. includeLocalVideos: true,
  56. withFiles: false,
  57. user,
  58. historyOfUser: user
  59. })
  60. }
  61. static removeHistoryBefore (user: UserModel, beforeDate: string, t: Transaction) {
  62. const query: DestroyOptions = {
  63. where: {
  64. userId: user.id
  65. },
  66. transaction: t
  67. }
  68. if (beforeDate) {
  69. query.where.updatedAt = {
  70. [Op.lt]: beforeDate
  71. }
  72. }
  73. return UserVideoHistoryModel.destroy(query)
  74. }
  75. }