1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import * as chai from 'chai'
4 import { UserNotificationType } from '@shared/models'
10 setAccessTokensToServers,
12 } from '@shared/server-commands'
14 const expect = chai.expect
16 async function checkNotifications (server: PeerTubeServer, token: string, expected: UserNotificationType[]) {
17 const { data } = await server.notifications.list({ token, start: 0, count: 10, unread: true })
18 expect(data).to.have.lengthOf(expected.length)
20 for (const type of expected) {
21 expect(data.find(n => n.type === type)).to.exist
25 describe('Test blocklist notifications', function () {
26 let servers: PeerTubeServer[]
29 let userToken1: string
30 let userToken2: string
31 let remoteUserToken: string
33 async function resetState () {
35 await servers[1].subscriptions.remove({ token: remoteUserToken, uri: 'user1_channel@' + servers[0].host })
36 await servers[1].subscriptions.remove({ token: remoteUserToken, uri: 'user2_channel@' + servers[0].host })
39 await waitJobs(servers)
41 await servers[0].notifications.markAsReadAll({ token: userToken1 })
42 await servers[0].notifications.markAsReadAll({ token: userToken2 })
45 const { uuid } = await servers[0].videos.upload({ token: userToken1, attributes: { name: 'video' } })
48 await waitJobs(servers)
52 await servers[1].comments.createThread({
53 token: remoteUserToken,
55 text: '@user2@' + servers[0].host + ' hello'
61 await servers[1].subscriptions.add({ token: remoteUserToken, targetUri: 'user1_channel@' + servers[0].host })
62 await servers[1].subscriptions.add({ token: remoteUserToken, targetUri: 'user2_channel@' + servers[0].host })
65 await waitJobs(servers)
68 before(async function () {
71 servers = await createMultipleServers(2)
72 await setAccessTokensToServers(servers)
75 const user = { username: 'user1', password: 'password' }
76 await servers[0].users.create({
77 username: user.username,
78 password: user.password,
83 userToken1 = await servers[0].login.getAccessToken(user)
84 await servers[0].videos.upload({ token: userToken1, attributes: { name: 'video user 1' } })
88 const user = { username: 'user2', password: 'password' }
89 await servers[0].users.create({ username: user.username, password: user.password })
91 userToken2 = await servers[0].login.getAccessToken(user)
95 const user = { username: 'user3', password: 'password' }
96 await servers[1].users.create({ username: user.username, password: user.password })
98 remoteUserToken = await servers[1].login.getAccessToken(user)
101 await doubleFollow(servers[0], servers[1])
104 describe('User blocks another user', function () {
106 before(async function () {
112 it('Should have appropriate notifications', async function () {
113 const notifs = [ UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, UserNotificationType.NEW_FOLLOW ]
114 await checkNotifications(servers[0], userToken1, notifs)
117 it('Should block an account', async function () {
120 await servers[0].blocklist.addToMyBlocklist({ token: userToken1, account: 'user3@' + servers[1].host })
121 await waitJobs(servers)
124 it('Should not have notifications from this account', async function () {
125 await checkNotifications(servers[0], userToken1, [])
128 it('Should have notifications of this account on user 2', async function () {
129 const notifs = [ UserNotificationType.COMMENT_MENTION, UserNotificationType.NEW_FOLLOW ]
131 await checkNotifications(servers[0], userToken2, notifs)
133 await servers[0].blocklist.removeFromMyBlocklist({ token: userToken1, account: 'user3@' + servers[1].host })
137 describe('User blocks another server', function () {
139 before(async function () {
145 it('Should have appropriate notifications', async function () {
146 const notifs = [ UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, UserNotificationType.NEW_FOLLOW ]
147 await checkNotifications(servers[0], userToken1, notifs)
150 it('Should block an account', async function () {
153 await servers[0].blocklist.addToMyBlocklist({ token: userToken1, server: servers[1].host })
154 await waitJobs(servers)
157 it('Should not have notifications from this account', async function () {
158 await checkNotifications(servers[0], userToken1, [])
161 it('Should have notifications of this account on user 2', async function () {
162 const notifs = [ UserNotificationType.COMMENT_MENTION, UserNotificationType.NEW_FOLLOW ]
164 await checkNotifications(servers[0], userToken2, notifs)
166 await servers[0].blocklist.removeFromMyBlocklist({ token: userToken1, server: servers[1].host })
170 describe('Server blocks a user', function () {
172 before(async function () {
178 it('Should have appropriate notifications', async function () {
180 const notifs = [ UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, UserNotificationType.NEW_FOLLOW ]
181 await checkNotifications(servers[0], userToken1, notifs)
185 const notifs = [ UserNotificationType.COMMENT_MENTION, UserNotificationType.NEW_FOLLOW ]
186 await checkNotifications(servers[0], userToken2, notifs)
190 it('Should block an account', async function () {
193 await servers[0].blocklist.addToServerBlocklist({ account: 'user3@' + servers[1].host })
194 await waitJobs(servers)
197 it('Should not have notifications from this account', async function () {
198 await checkNotifications(servers[0], userToken1, [])
199 await checkNotifications(servers[0], userToken2, [])
201 await servers[0].blocklist.removeFromServerBlocklist({ account: 'user3@' + servers[1].host })
205 describe('Server blocks a server', function () {
207 before(async function () {
213 it('Should have appropriate notifications', async function () {
215 const notifs = [ UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, UserNotificationType.NEW_FOLLOW ]
216 await checkNotifications(servers[0], userToken1, notifs)
220 const notifs = [ UserNotificationType.COMMENT_MENTION, UserNotificationType.NEW_FOLLOW ]
221 await checkNotifications(servers[0], userToken2, notifs)
225 it('Should block an account', async function () {
228 await servers[0].blocklist.addToServerBlocklist({ server: servers[1].host })
229 await waitJobs(servers)
232 it('Should not have notifications from this account', async function () {
233 await checkNotifications(servers[0], userToken1, [])
234 await checkNotifications(servers[0], userToken2, [])
238 after(async function () {
239 await cleanupTests(servers)