follows.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import * as request from 'supertest'
  2. import { ServerInfo } from './servers'
  3. import { waitJobs } from './jobs'
  4. import { makePostBodyRequest } from '../requests/requests'
  5. import { ActivityPubActorType, FollowState } from '@shared/models'
  6. import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
  7. function getFollowersListPaginationAndSort (options: {
  8. url: string
  9. start: number
  10. count: number
  11. sort: string
  12. search?: string
  13. actorType?: ActivityPubActorType
  14. state?: FollowState
  15. }) {
  16. const { url, start, count, sort, search, state, actorType } = options
  17. const path = '/api/v1/server/followers'
  18. const query = {
  19. start,
  20. count,
  21. sort,
  22. search,
  23. state,
  24. actorType
  25. }
  26. return request(url)
  27. .get(path)
  28. .query(query)
  29. .set('Accept', 'application/json')
  30. .expect(HttpStatusCode.OK_200)
  31. .expect('Content-Type', /json/)
  32. }
  33. function acceptFollower (url: string, token: string, follower: string, statusCodeExpected = HttpStatusCode.NO_CONTENT_204) {
  34. const path = '/api/v1/server/followers/' + follower + '/accept'
  35. return makePostBodyRequest({
  36. url,
  37. token,
  38. path,
  39. statusCodeExpected
  40. })
  41. }
  42. function rejectFollower (url: string, token: string, follower: string, statusCodeExpected = HttpStatusCode.NO_CONTENT_204) {
  43. const path = '/api/v1/server/followers/' + follower + '/reject'
  44. return makePostBodyRequest({
  45. url,
  46. token,
  47. path,
  48. statusCodeExpected
  49. })
  50. }
  51. function getFollowingListPaginationAndSort (options: {
  52. url: string
  53. start: number
  54. count: number
  55. sort: string
  56. search?: string
  57. actorType?: ActivityPubActorType
  58. state?: FollowState
  59. }) {
  60. const { url, start, count, sort, search, state, actorType } = options
  61. const path = '/api/v1/server/following'
  62. const query = {
  63. start,
  64. count,
  65. sort,
  66. search,
  67. state,
  68. actorType
  69. }
  70. return request(url)
  71. .get(path)
  72. .query(query)
  73. .set('Accept', 'application/json')
  74. .expect(HttpStatusCode.OK_200)
  75. .expect('Content-Type', /json/)
  76. }
  77. function follow (follower: string, following: string[], accessToken: string, expectedStatus = HttpStatusCode.NO_CONTENT_204) {
  78. const path = '/api/v1/server/following'
  79. const followingHosts = following.map(f => f.replace(/^http:\/\//, ''))
  80. return request(follower)
  81. .post(path)
  82. .set('Accept', 'application/json')
  83. .set('Authorization', 'Bearer ' + accessToken)
  84. .send({ hosts: followingHosts })
  85. .expect(expectedStatus)
  86. }
  87. async function unfollow (url: string, accessToken: string, target: ServerInfo, expectedStatus = HttpStatusCode.NO_CONTENT_204) {
  88. const path = '/api/v1/server/following/' + target.host
  89. return request(url)
  90. .delete(path)
  91. .set('Accept', 'application/json')
  92. .set('Authorization', 'Bearer ' + accessToken)
  93. .expect(expectedStatus)
  94. }
  95. function removeFollower (url: string, accessToken: string, follower: ServerInfo, expectedStatus = HttpStatusCode.NO_CONTENT_204) {
  96. const path = '/api/v1/server/followers/peertube@' + follower.host
  97. return request(url)
  98. .delete(path)
  99. .set('Accept', 'application/json')
  100. .set('Authorization', 'Bearer ' + accessToken)
  101. .expect(expectedStatus)
  102. }
  103. async function doubleFollow (server1: ServerInfo, server2: ServerInfo) {
  104. await Promise.all([
  105. follow(server1.url, [ server2.url ], server1.accessToken),
  106. follow(server2.url, [ server1.url ], server2.accessToken)
  107. ])
  108. // Wait request propagation
  109. await waitJobs([ server1, server2 ])
  110. return true
  111. }
  112. // ---------------------------------------------------------------------------
  113. export {
  114. getFollowersListPaginationAndSort,
  115. getFollowingListPaginationAndSort,
  116. unfollow,
  117. removeFollower,
  118. follow,
  119. doubleFollow,
  120. acceptFollower,
  121. rejectFollower
  122. }