ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

73 lines
2.4 KiB

  1. import { literal, Model, ModelStatic } from 'sequelize'
  2. import { Literal } from 'sequelize/types/utils'
  3. import { forceNumber } from '@peertube/peertube-core-utils'
  4. import { AttributesOnly } from '@peertube/peertube-typescript-utils'
  5. // FIXME: have to specify the result type to not break peertube typings generation
  6. export function buildLocalAccountIdsIn (): Literal {
  7. return literal(
  8. '(SELECT "account"."id" FROM "account" INNER JOIN "actor" ON "actor"."id" = "account"."actorId" AND "actor"."serverId" IS NULL)'
  9. )
  10. }
  11. // FIXME: have to specify the result type to not break peertube typings generation
  12. export function buildLocalActorIdsIn (): Literal {
  13. return literal(
  14. '(SELECT "actor"."id" FROM "actor" WHERE "actor"."serverId" IS NULL)'
  15. )
  16. }
  17. export function buildBlockedAccountSQL (blockerIds: number[]) {
  18. const blockerIdsString = blockerIds.join(', ')
  19. return 'SELECT "targetAccountId" AS "id" FROM "accountBlocklist" WHERE "accountId" IN (' + blockerIdsString + ')' +
  20. ' UNION ' +
  21. 'SELECT "account"."id" AS "id" FROM account INNER JOIN "actor" ON account."actorId" = actor.id ' +
  22. 'INNER JOIN "serverBlocklist" ON "actor"."serverId" = "serverBlocklist"."targetServerId" ' +
  23. 'WHERE "serverBlocklist"."accountId" IN (' + blockerIdsString + ')'
  24. }
  25. export function buildServerIdsFollowedBy (actorId: any) {
  26. const actorIdNumber = forceNumber(actorId)
  27. return '(' +
  28. 'SELECT "actor"."serverId" FROM "actorFollow" ' +
  29. 'INNER JOIN "actor" ON actor.id = "actorFollow"."targetActorId" ' +
  30. 'WHERE "actorFollow"."actorId" = ' + actorIdNumber +
  31. ')'
  32. }
  33. export function buildSQLAttributes<M extends Model> (options: {
  34. model: ModelStatic<M>
  35. tableName: string
  36. excludeAttributes?: Exclude<keyof AttributesOnly<M>, symbol>[]
  37. aliasPrefix?: string
  38. idBuilder?: string[]
  39. }) {
  40. const { model, tableName, aliasPrefix = '', excludeAttributes, idBuilder } = options
  41. const attributes = Object.keys(model.getAttributes()) as Exclude<keyof AttributesOnly<M>, symbol>[]
  42. const builtAttributes = attributes
  43. .filter(a => {
  44. if (!excludeAttributes) return true
  45. if (excludeAttributes.includes(a)) return false
  46. return true
  47. })
  48. .map(a => {
  49. return `"${tableName}"."${a}" AS "${aliasPrefix}${a}"`
  50. })
  51. if (idBuilder) {
  52. const idSelect = idBuilder.map(a => `"${tableName}"."${a}"`)
  53. .join(` || '-' || `)
  54. builtAttributes.push(`${idSelect} AS "${aliasPrefix}id"`)
  55. }
  56. return builtAttributes
  57. }