account-blocklist.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
  2. import { AccountModel } from './account'
  3. import { getSort } from '../utils'
  4. import { AccountBlock } from '../../../shared/models/blocklist'
  5. import { Op } from 'sequelize'
  6. enum ScopeNames {
  7. WITH_ACCOUNTS = 'WITH_ACCOUNTS'
  8. }
  9. @Scopes(() => ({
  10. [ScopeNames.WITH_ACCOUNTS]: {
  11. include: [
  12. {
  13. model: AccountModel,
  14. required: true,
  15. as: 'ByAccount'
  16. },
  17. {
  18. model: AccountModel,
  19. required: true,
  20. as: 'BlockedAccount'
  21. }
  22. ]
  23. }
  24. }))
  25. @Table({
  26. tableName: 'accountBlocklist',
  27. indexes: [
  28. {
  29. fields: [ 'accountId', 'targetAccountId' ],
  30. unique: true
  31. },
  32. {
  33. fields: [ 'targetAccountId' ]
  34. }
  35. ]
  36. })
  37. export class AccountBlocklistModel extends Model<AccountBlocklistModel> {
  38. @CreatedAt
  39. createdAt: Date
  40. @UpdatedAt
  41. updatedAt: Date
  42. @ForeignKey(() => AccountModel)
  43. @Column
  44. accountId: number
  45. @BelongsTo(() => AccountModel, {
  46. foreignKey: {
  47. name: 'accountId',
  48. allowNull: false
  49. },
  50. as: 'ByAccount',
  51. onDelete: 'CASCADE'
  52. })
  53. ByAccount: AccountModel
  54. @ForeignKey(() => AccountModel)
  55. @Column
  56. targetAccountId: number
  57. @BelongsTo(() => AccountModel, {
  58. foreignKey: {
  59. name: 'targetAccountId',
  60. allowNull: false
  61. },
  62. as: 'BlockedAccount',
  63. onDelete: 'CASCADE'
  64. })
  65. BlockedAccount: AccountModel
  66. static isAccountMutedBy (accountId: number, targetAccountId: number) {
  67. return AccountBlocklistModel.isAccountMutedByMulti([ accountId ], targetAccountId)
  68. .then(result => result[accountId])
  69. }
  70. static isAccountMutedByMulti (accountIds: number[], targetAccountId: number) {
  71. const query = {
  72. attributes: [ 'accountId', 'id' ],
  73. where: {
  74. accountId: {
  75. [Op.in]: accountIds // FIXME: sequelize ANY seems broken
  76. },
  77. targetAccountId
  78. },
  79. raw: true
  80. }
  81. return AccountBlocklistModel.unscoped()
  82. .findAll(query)
  83. .then(rows => {
  84. const result: { [accountId: number]: boolean } = {}
  85. for (const accountId of accountIds) {
  86. result[accountId] = !!rows.find(r => r.accountId === accountId)
  87. }
  88. return result
  89. })
  90. }
  91. static loadByAccountAndTarget (accountId: number, targetAccountId: number) {
  92. const query = {
  93. where: {
  94. accountId,
  95. targetAccountId
  96. }
  97. }
  98. return AccountBlocklistModel.findOne(query)
  99. }
  100. static listForApi (accountId: number, start: number, count: number, sort: string) {
  101. const query = {
  102. offset: start,
  103. limit: count,
  104. order: getSort(sort),
  105. where: {
  106. accountId
  107. }
  108. }
  109. return AccountBlocklistModel
  110. .scope([ ScopeNames.WITH_ACCOUNTS ])
  111. .findAndCountAll(query)
  112. .then(({ rows, count }) => {
  113. return { total: count, data: rows }
  114. })
  115. }
  116. toFormattedJSON (): AccountBlock {
  117. return {
  118. byAccount: this.ByAccount.toFormattedJSON(),
  119. blockedAccount: this.BlockedAccount.toFormattedJSON(),
  120. createdAt: this.createdAt
  121. }
  122. }
  123. }