sort.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { literal, OrderItem, Sequelize } from 'sequelize'
  2. // Translate for example "-name" to [ [ 'name', 'DESC' ], [ 'id', 'ASC' ] ]
  3. function getSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] {
  4. const { direction, field } = buildSortDirectionAndField(value)
  5. let finalField: string | ReturnType<typeof Sequelize.col>
  6. if (field.toLowerCase() === 'match') { // Search
  7. finalField = Sequelize.col('similarity')
  8. } else {
  9. finalField = field
  10. }
  11. return [ [ finalField, direction ], lastSort ]
  12. }
  13. function getAdminUsersSort (value: string): OrderItem[] {
  14. const { direction, field } = buildSortDirectionAndField(value)
  15. let finalField: string | ReturnType<typeof Sequelize.col>
  16. if (field === 'videoQuotaUsed') { // Users list
  17. finalField = Sequelize.col('videoQuotaUsed')
  18. } else {
  19. finalField = field
  20. }
  21. const nullPolicy = direction === 'ASC'
  22. ? 'NULLS FIRST'
  23. : 'NULLS LAST'
  24. // FIXME: typings
  25. return [ [ finalField as any, direction, nullPolicy ], [ 'id', 'ASC' ] ]
  26. }
  27. function getPlaylistSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] {
  28. const { direction, field } = buildSortDirectionAndField(value)
  29. if (field.toLowerCase() === 'name') {
  30. return [ [ 'displayName', direction ], lastSort ]
  31. }
  32. return getSort(value, lastSort)
  33. }
  34. function getVideoSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] {
  35. const { direction, field } = buildSortDirectionAndField(value)
  36. if (field.toLowerCase() === 'trending') { // Sort by aggregation
  37. return [
  38. [ Sequelize.fn('COALESCE', Sequelize.fn('SUM', Sequelize.col('VideoViews.views')), '0'), direction ],
  39. [ Sequelize.col('VideoModel.views'), direction ],
  40. lastSort
  41. ]
  42. } else if (field === 'publishedAt') {
  43. return [
  44. [ 'ScheduleVideoUpdate', 'updateAt', direction + ' NULLS LAST' ],
  45. [ Sequelize.col('VideoModel.publishedAt'), direction ],
  46. lastSort
  47. ]
  48. }
  49. let finalField: string | ReturnType<typeof Sequelize.col>
  50. // Alias
  51. if (field.toLowerCase() === 'match') { // Search
  52. finalField = Sequelize.col('similarity')
  53. } else {
  54. finalField = field
  55. }
  56. const firstSort: OrderItem = typeof finalField === 'string'
  57. ? finalField.split('.').concat([ direction ]) as OrderItem
  58. : [ finalField, direction ]
  59. return [ firstSort, lastSort ]
  60. }
  61. function getBlacklistSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] {
  62. const { direction, field } = buildSortDirectionAndField(value)
  63. const videoFields = new Set([ 'name', 'duration', 'views', 'likes', 'dislikes', 'uuid' ])
  64. if (videoFields.has(field)) {
  65. return [
  66. [ literal(`"Video.${field}" ${direction}`) ],
  67. lastSort
  68. ] as OrderItem[]
  69. }
  70. return getSort(value, lastSort)
  71. }
  72. function getInstanceFollowsSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] {
  73. const { direction, field } = buildSortDirectionAndField(value)
  74. if (field === 'redundancyAllowed') {
  75. return [
  76. [ 'ActorFollowing.Server.redundancyAllowed', direction ],
  77. lastSort
  78. ]
  79. }
  80. return getSort(value, lastSort)
  81. }
  82. function getChannelSyncSort (value: string): OrderItem[] {
  83. const { direction, field } = buildSortDirectionAndField(value)
  84. if (field.toLowerCase() === 'videochannel') {
  85. return [
  86. [ literal('"VideoChannel.name"'), direction ]
  87. ]
  88. }
  89. return [ [ field, direction ] ]
  90. }
  91. function buildSortDirectionAndField (value: string) {
  92. let field: string
  93. let direction: 'ASC' | 'DESC'
  94. if (value.substring(0, 1) === '-') {
  95. direction = 'DESC'
  96. field = value.substring(1)
  97. } else {
  98. direction = 'ASC'
  99. field = value
  100. }
  101. return { direction, field }
  102. }
  103. export {
  104. buildSortDirectionAndField,
  105. getPlaylistSort,
  106. getSort,
  107. getAdminUsersSort,
  108. getVideoSort,
  109. getBlacklistSort,
  110. getChannelSyncSort,
  111. getInstanceFollowsSort
  112. }