utils.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. const firstSort = typeof field === 'string' ?
  24. field.split('.').concat([ direction ]) :
  25. [ field, direction ]
  26. return [ firstSort, lastSort ]
  27. }
  28. function getSortOnModel (model: any, value: string, lastSort: string[] = [ 'id', 'ASC' ]) {
  29. let [ firstSort ] = getSort(value)
  30. if (model) return [ [ model, firstSort[0], firstSort[1] ], lastSort ]
  31. return [ firstSort, lastSort ]
  32. }
  33. function throwIfNotValid (value: any, validator: (value: any) => boolean, fieldName = 'value') {
  34. if (validator(value) === false) {
  35. throw new Error(`"${value}" is not a valid ${fieldName}.`)
  36. }
  37. }
  38. function buildTrigramSearchIndex (indexName: string, attribute: string) {
  39. return {
  40. name: indexName,
  41. fields: [ Sequelize.literal('lower(immutable_unaccent(' + attribute + '))') as any ],
  42. using: 'gin',
  43. operator: 'gin_trgm_ops'
  44. }
  45. }
  46. function createSimilarityAttribute (col: string, value: string) {
  47. return Sequelize.fn(
  48. 'similarity',
  49. searchTrigramNormalizeCol(col),
  50. searchTrigramNormalizeValue(value)
  51. )
  52. }
  53. function buildBlockedAccountSQL (serverAccountId: number, userAccountId?: number) {
  54. const blockerIds = [ serverAccountId ]
  55. if (userAccountId) blockerIds.push(userAccountId)
  56. const blockerIdsString = blockerIds.join(', ')
  57. const query = 'SELECT "targetAccountId" AS "id" FROM "accountBlocklist" WHERE "accountId" IN (' + blockerIdsString + ')' +
  58. ' UNION ALL ' +
  59. 'SELECT "account"."id" AS "id" FROM account INNER JOIN "actor" ON account."actorId" = actor.id ' +
  60. 'INNER JOIN "serverBlocklist" ON "actor"."serverId" = "serverBlocklist"."targetServerId" ' +
  61. 'WHERE "serverBlocklist"."accountId" IN (' + blockerIdsString + ')'
  62. return query
  63. }
  64. // ---------------------------------------------------------------------------
  65. export {
  66. buildBlockedAccountSQL,
  67. SortType,
  68. getSort,
  69. getVideoSort,
  70. getSortOnModel,
  71. createSimilarityAttribute,
  72. throwIfNotValid,
  73. buildTrigramSearchIndex
  74. }
  75. // ---------------------------------------------------------------------------
  76. function searchTrigramNormalizeValue (value: string) {
  77. return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', value))
  78. }
  79. function searchTrigramNormalizeCol (col: string) {
  80. return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', Sequelize.col(col)))
  81. }
  82. function buildDirectionAndField (value: string) {
  83. let field: any
  84. let direction: 'ASC' | 'DESC'
  85. if (value.substring(0, 1) === '-') {
  86. direction = 'DESC'
  87. field = value.substring(1)
  88. } else {
  89. direction = 'ASC'
  90. field = value
  91. }
  92. return { direction, field }
  93. }