follows-command.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import { pick } from '@shared/core-utils'
  2. import { ActivityPubActorType, ActorFollow, FollowState, HttpStatusCode, ResultList, ServerFollowCreate } from '@shared/models'
  3. import { AbstractCommand, OverrideCommandOptions } from '../shared'
  4. import { PeerTubeServer } from './server'
  5. export class FollowsCommand extends AbstractCommand {
  6. getFollowers (options: OverrideCommandOptions & {
  7. start?: number
  8. count?: number
  9. sort?: string
  10. search?: string
  11. actorType?: ActivityPubActorType
  12. state?: FollowState
  13. } = {}) {
  14. const path = '/api/v1/server/followers'
  15. const query = pick(options, [ 'start', 'count', 'sort', 'search', 'state', 'actorType' ])
  16. return this.getRequestBody<ResultList<ActorFollow>>({
  17. ...options,
  18. path,
  19. query,
  20. implicitToken: false,
  21. defaultExpectedStatus: HttpStatusCode.OK_200
  22. })
  23. }
  24. getFollowings (options: OverrideCommandOptions & {
  25. start?: number
  26. count?: number
  27. sort?: string
  28. search?: string
  29. actorType?: ActivityPubActorType
  30. state?: FollowState
  31. } = {}) {
  32. const path = '/api/v1/server/following'
  33. const query = pick(options, [ 'start', 'count', 'sort', 'search', 'state', 'actorType' ])
  34. return this.getRequestBody<ResultList<ActorFollow>>({
  35. ...options,
  36. path,
  37. query,
  38. implicitToken: false,
  39. defaultExpectedStatus: HttpStatusCode.OK_200
  40. })
  41. }
  42. follow (options: OverrideCommandOptions & {
  43. hosts?: string[]
  44. handles?: string[]
  45. }) {
  46. const path = '/api/v1/server/following'
  47. const fields: ServerFollowCreate = {}
  48. if (options.hosts) {
  49. fields.hosts = options.hosts.map(f => f.replace(/^http:\/\//, ''))
  50. }
  51. if (options.handles) {
  52. fields.handles = options.handles
  53. }
  54. return this.postBodyRequest({
  55. ...options,
  56. path,
  57. fields,
  58. implicitToken: true,
  59. defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
  60. })
  61. }
  62. async unfollow (options: OverrideCommandOptions & {
  63. target: PeerTubeServer | string
  64. }) {
  65. const { target } = options
  66. const handle = typeof target === 'string'
  67. ? target
  68. : target.host
  69. const path = '/api/v1/server/following/' + handle
  70. return this.deleteRequest({
  71. ...options,
  72. path,
  73. implicitToken: true,
  74. defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
  75. })
  76. }
  77. acceptFollower (options: OverrideCommandOptions & {
  78. follower: string
  79. }) {
  80. const path = '/api/v1/server/followers/' + options.follower + '/accept'
  81. return this.postBodyRequest({
  82. ...options,
  83. path,
  84. implicitToken: true,
  85. defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
  86. })
  87. }
  88. rejectFollower (options: OverrideCommandOptions & {
  89. follower: string
  90. }) {
  91. const path = '/api/v1/server/followers/' + options.follower + '/reject'
  92. return this.postBodyRequest({
  93. ...options,
  94. path,
  95. implicitToken: true,
  96. defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
  97. })
  98. }
  99. removeFollower (options: OverrideCommandOptions & {
  100. follower: PeerTubeServer
  101. }) {
  102. const path = '/api/v1/server/followers/peertube@' + options.follower.host
  103. return this.deleteRequest({
  104. ...options,
  105. path,
  106. implicitToken: true,
  107. defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
  108. })
  109. }
  110. }