utils.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { Sequelize } from 'sequelize-typescript'
  2. type SortType = { sortModel: any, sortValue: string }
  3. // Translate for example "-name" to [ [ 'name', 'DESC' ], [ 'id', 'ASC' ] ]
  4. function getSort (value: string, lastSort: string[] = [ 'id', 'ASC' ]) {
  5. let { direction, field } = buildDirectionAndField(value)
  6. if (field.toLowerCase() === 'match') { // Search
  7. field = Sequelize.col('similarity')
  8. }
  9. return [ [ field, direction ], lastSort ]
  10. }
  11. function getVideoSort (value: string, lastSort: string[] = [ 'id', 'ASC' ]) {
  12. let { direction, field } = buildDirectionAndField(value)
  13. // Alias
  14. if (field.toLowerCase() === 'match') { // Search
  15. field = Sequelize.col('similarity')
  16. } else if (field.toLowerCase() === 'trending') { // Sort by aggregation
  17. return [
  18. [ Sequelize.fn('COALESCE', Sequelize.fn('SUM', Sequelize.col('VideoViews.views')), '0'), direction ],
  19. [ Sequelize.col('VideoModel.views'), direction ],
  20. lastSort
  21. ]
  22. }
  23. return [ [ field, direction ], lastSort ]
  24. }
  25. function getSortOnModel (model: any, value: string, lastSort: string[] = [ 'id', 'ASC' ]) {
  26. let [ firstSort ] = getSort(value)
  27. if (model) return [ [ model, firstSort[0], firstSort[1] ], lastSort ]
  28. return [ firstSort, lastSort ]
  29. }
  30. function throwIfNotValid (value: any, validator: (value: any) => boolean, fieldName = 'value') {
  31. if (validator(value) === false) {
  32. throw new Error(`"${value}" is not a valid ${fieldName}.`)
  33. }
  34. }
  35. function buildTrigramSearchIndex (indexName: string, attribute: string) {
  36. return {
  37. name: indexName,
  38. fields: [ Sequelize.literal('lower(immutable_unaccent(' + attribute + '))') as any ],
  39. using: 'gin',
  40. operator: 'gin_trgm_ops'
  41. }
  42. }
  43. function createSimilarityAttribute (col: string, value: string) {
  44. return Sequelize.fn(
  45. 'similarity',
  46. searchTrigramNormalizeCol(col),
  47. searchTrigramNormalizeValue(value)
  48. )
  49. }
  50. function buildBlockedAccountSQL (serverAccountId: number, userAccountId?: number) {
  51. const blockerIds = [ serverAccountId ]
  52. if (userAccountId) blockerIds.push(userAccountId)
  53. const blockerIdsString = blockerIds.join(', ')
  54. const query = 'SELECT "targetAccountId" AS "id" FROM "accountBlocklist" WHERE "accountId" IN (' + blockerIdsString + ')' +
  55. ' UNION ALL ' +
  56. 'SELECT "account"."id" AS "id" FROM account INNER JOIN "actor" ON account."actorId" = actor.id ' +
  57. 'INNER JOIN "serverBlocklist" ON "actor"."serverId" = "serverBlocklist"."targetServerId" ' +
  58. 'WHERE "serverBlocklist"."accountId" IN (' + blockerIdsString + ')'
  59. return query
  60. }
  61. // ---------------------------------------------------------------------------
  62. export {
  63. buildBlockedAccountSQL,
  64. SortType,
  65. getSort,
  66. getVideoSort,
  67. getSortOnModel,
  68. createSimilarityAttribute,
  69. throwIfNotValid,
  70. buildTrigramSearchIndex
  71. }
  72. // ---------------------------------------------------------------------------
  73. function searchTrigramNormalizeValue (value: string) {
  74. return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', value))
  75. }
  76. function searchTrigramNormalizeCol (col: string) {
  77. return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', Sequelize.col(col)))
  78. }
  79. function buildDirectionAndField (value: string) {
  80. let field: any
  81. let direction: 'ASC' | 'DESC'
  82. if (value.substring(0, 1) === '-') {
  83. direction = 'DESC'
  84. field = value.substring(1)
  85. } else {
  86. direction = 'ASC'
  87. field = value
  88. }
  89. return { direction, field }
  90. }