video-playlist-element.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import {
  2. AllowNull,
  3. BelongsTo,
  4. Column,
  5. CreatedAt,
  6. DataType,
  7. Default,
  8. ForeignKey,
  9. Is,
  10. IsInt,
  11. Min,
  12. Model,
  13. Table,
  14. UpdatedAt
  15. } from 'sequelize-typescript'
  16. import { VideoModel } from './video'
  17. import { VideoPlaylistModel } from './video-playlist'
  18. import { getSort, throwIfNotValid } from '../utils'
  19. import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
  20. import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
  21. import { PlaylistElementObject } from '../../../shared/models/activitypub/objects/playlist-element-object'
  22. import * as validator from 'validator'
  23. import { AggregateOptions, Op, Sequelize, Transaction } from 'sequelize'
  24. @Table({
  25. tableName: 'videoPlaylistElement',
  26. indexes: [
  27. {
  28. fields: [ 'videoPlaylistId' ]
  29. },
  30. {
  31. fields: [ 'videoId' ]
  32. },
  33. {
  34. fields: [ 'videoPlaylistId', 'videoId' ],
  35. unique: true
  36. },
  37. {
  38. fields: [ 'url' ],
  39. unique: true
  40. }
  41. ]
  42. })
  43. export class VideoPlaylistElementModel extends Model<VideoPlaylistElementModel> {
  44. @CreatedAt
  45. createdAt: Date
  46. @UpdatedAt
  47. updatedAt: Date
  48. @AllowNull(false)
  49. @Is('VideoPlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
  50. @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_PLAYLISTS.URL.max))
  51. url: string
  52. @AllowNull(false)
  53. @Default(1)
  54. @IsInt
  55. @Min(1)
  56. @Column
  57. position: number
  58. @AllowNull(true)
  59. @IsInt
  60. @Min(0)
  61. @Column
  62. startTimestamp: number
  63. @AllowNull(true)
  64. @IsInt
  65. @Min(0)
  66. @Column
  67. stopTimestamp: number
  68. @ForeignKey(() => VideoPlaylistModel)
  69. @Column
  70. videoPlaylistId: number
  71. @BelongsTo(() => VideoPlaylistModel, {
  72. foreignKey: {
  73. allowNull: false
  74. },
  75. onDelete: 'CASCADE'
  76. })
  77. VideoPlaylist: VideoPlaylistModel
  78. @ForeignKey(() => VideoModel)
  79. @Column
  80. videoId: number
  81. @BelongsTo(() => VideoModel, {
  82. foreignKey: {
  83. allowNull: false
  84. },
  85. onDelete: 'CASCADE'
  86. })
  87. Video: VideoModel
  88. static deleteAllOf (videoPlaylistId: number, transaction?: Transaction) {
  89. const query = {
  90. where: {
  91. videoPlaylistId
  92. },
  93. transaction
  94. }
  95. return VideoPlaylistElementModel.destroy(query)
  96. }
  97. static loadByPlaylistAndVideo (videoPlaylistId: number, videoId: number) {
  98. const query = {
  99. where: {
  100. videoPlaylistId,
  101. videoId
  102. }
  103. }
  104. return VideoPlaylistElementModel.findOne(query)
  105. }
  106. static loadByPlaylistAndVideoForAP (playlistId: number | string, videoId: number | string) {
  107. const playlistWhere = validator.isUUID('' + playlistId) ? { uuid: playlistId } : { id: playlistId }
  108. const videoWhere = validator.isUUID('' + videoId) ? { uuid: videoId } : { id: videoId }
  109. const query = {
  110. include: [
  111. {
  112. attributes: [ 'privacy' ],
  113. model: VideoPlaylistModel.unscoped(),
  114. where: playlistWhere
  115. },
  116. {
  117. attributes: [ 'url' ],
  118. model: VideoModel.unscoped(),
  119. where: videoWhere
  120. }
  121. ]
  122. }
  123. return VideoPlaylistElementModel.findOne(query)
  124. }
  125. static listUrlsOfForAP (videoPlaylistId: number, start: number, count: number, t?: Transaction) {
  126. const query = {
  127. attributes: [ 'url' ],
  128. offset: start,
  129. limit: count,
  130. order: getSort('position'),
  131. where: {
  132. videoPlaylistId
  133. },
  134. transaction: t
  135. }
  136. return VideoPlaylistElementModel
  137. .findAndCountAll(query)
  138. .then(({ rows, count }) => {
  139. return { total: count, data: rows.map(e => e.url) }
  140. })
  141. }
  142. static getNextPositionOf (videoPlaylistId: number, transaction?: Transaction) {
  143. const query: AggregateOptions<number> = {
  144. where: {
  145. videoPlaylistId
  146. },
  147. transaction
  148. }
  149. return VideoPlaylistElementModel.max('position', query)
  150. .then(position => position ? position + 1 : 1)
  151. }
  152. static reassignPositionOf (
  153. videoPlaylistId: number,
  154. firstPosition: number,
  155. endPosition: number,
  156. newPosition: number,
  157. transaction?: Transaction
  158. ) {
  159. const query = {
  160. where: {
  161. videoPlaylistId,
  162. position: {
  163. [Op.gte]: firstPosition,
  164. [Op.lte]: endPosition
  165. }
  166. },
  167. transaction,
  168. validate: false // We use a literal to update the position
  169. }
  170. return VideoPlaylistElementModel.update({ position: Sequelize.literal(`${newPosition} + "position" - ${firstPosition}`) }, query)
  171. }
  172. static increasePositionOf (
  173. videoPlaylistId: number,
  174. fromPosition: number,
  175. toPosition?: number,
  176. by = 1,
  177. transaction?: Transaction
  178. ) {
  179. const query = {
  180. where: {
  181. videoPlaylistId,
  182. position: {
  183. [Op.gte]: fromPosition
  184. }
  185. },
  186. transaction
  187. }
  188. return VideoPlaylistElementModel.increment({ position: by }, query)
  189. }
  190. toActivityPubObject (): PlaylistElementObject {
  191. const base: PlaylistElementObject = {
  192. id: this.url,
  193. type: 'PlaylistElement',
  194. url: this.Video.url,
  195. position: this.position
  196. }
  197. if (this.startTimestamp) base.startTimestamp = this.startTimestamp
  198. if (this.stopTimestamp) base.stopTimestamp = this.stopTimestamp
  199. return base
  200. }
  201. }