1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import * as chai from 'chai'
8 flushAndRunMultipleServers,
11 setAccessTokensToServers,
13 updateCustomSubConfig,
15 } from '../../../../shared/extra-utils/index'
16 import { follow, getFollowersListPaginationAndSort, getFollowingListPaginationAndSort } from '../../../../shared/extra-utils/server/follows'
17 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
18 import { ActorFollow } from '../../../../shared/models/actors'
20 const expect = chai.expect
22 async function checkFollow (follower: ServerInfo, following: ServerInfo, exists: boolean) {
24 const res = await getFollowersListPaginationAndSort({ url: following.url, start: 0, count: 5, sort: '-createdAt' })
25 const follows = res.body.data as ActorFollow[]
27 const follow = follows.find(f => {
28 return f.follower.host === follower.host && f.state === 'accepted'
31 if (exists === true) {
32 expect(follow).to.exist
34 expect(follow).to.be.undefined
39 const res = await getFollowingListPaginationAndSort({ url: follower.url, start: 0, count: 5, sort: '-createdAt' })
40 const follows = res.body.data as ActorFollow[]
42 const follow = follows.find(f => {
43 return f.following.host === following.host && f.state === 'accepted'
46 if (exists === true) {
47 expect(follow).to.exist
49 expect(follow).to.be.undefined
54 async function server1Follows2 (servers: ServerInfo[]) {
55 await follow(servers[0].url, [ servers[1].host ], servers[0].accessToken)
57 await waitJobs(servers)
60 async function resetFollows (servers: ServerInfo[]) {
62 await unfollow(servers[0].url, servers[0].accessToken, servers[1])
63 await unfollow(servers[1].url, servers[1].accessToken, servers[0])
67 await waitJobs(servers)
69 await checkFollow(servers[0], servers[1], false)
70 await checkFollow(servers[1], servers[0], false)
73 describe('Test auto follows', function () {
74 let servers: ServerInfo[] = []
76 before(async function () {
79 servers = await flushAndRunMultipleServers(3)
81 // Get the access tokens
82 await setAccessTokensToServers(servers)
85 describe('Auto follow back', function () {
87 it('Should not auto follow back if the option is not enabled', async function () {
90 await server1Follows2(servers)
92 await checkFollow(servers[0], servers[1], true)
93 await checkFollow(servers[1], servers[0], false)
95 await resetFollows(servers)
98 it('Should auto follow back on auto accept if the option is enabled', async function () {
104 autoFollowBack: { enabled: true }
108 await updateCustomSubConfig(servers[1].url, servers[1].accessToken, config)
110 await server1Follows2(servers)
112 await checkFollow(servers[0], servers[1], true)
113 await checkFollow(servers[1], servers[0], true)
115 await resetFollows(servers)
118 it('Should wait the acceptation before auto follow back', async function () {
124 autoFollowBack: { enabled: true }
133 await updateCustomSubConfig(servers[1].url, servers[1].accessToken, config)
135 await server1Follows2(servers)
137 await checkFollow(servers[0], servers[1], false)
138 await checkFollow(servers[1], servers[0], false)
140 await acceptFollower(servers[1].url, servers[1].accessToken, 'peertube@' + servers[0].host)
141 await waitJobs(servers)
143 await checkFollow(servers[0], servers[1], true)
144 await checkFollow(servers[1], servers[0], true)
146 await resetFollows(servers)
148 config.followings.instance.autoFollowBack.enabled = false
149 config.followers.instance.manualApproval = false
150 await updateCustomSubConfig(servers[1].url, servers[1].accessToken, config)
154 describe('Auto follow index', function () {
155 const instanceIndexServer = new MockInstancesIndex()
158 await instanceIndexServer.initialize()
161 it('Should not auto follow index if the option is not enabled', async function () {
165 await waitJobs(servers)
167 await checkFollow(servers[0], servers[1], false)
168 await checkFollow(servers[1], servers[0], false)
171 it('Should auto follow the index', async function () {
174 instanceIndexServer.addInstance(servers[1].host)
180 indexUrl: 'http://localhost:42101/api/v1/instances/hosts',
186 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config)
189 await waitJobs(servers)
191 await checkFollow(servers[0], servers[1], true)
193 await resetFollows(servers)
196 it('Should follow new added instances in the index but not old ones', async function () {
199 instanceIndexServer.addInstance(servers[2].host)
202 await waitJobs(servers)
204 await checkFollow(servers[0], servers[1], false)
205 await checkFollow(servers[0], servers[2], true)
209 after(async function () {
210 await cleanupTests(servers)