Skip to content

Commit 0776492

Browse files
wa0x6eChaituVR
andauthored
fix: ignore deleted spaces when checking for followed spaces limit (#486)
* fix: ignore deleted spaces when checking for followed spaces limit * test: add tests --------- Co-authored-by: Chaitanya <[email protected]>
1 parent 1fc30c1 commit 0776492

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

src/writer/follow.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import db from '../helpers/mysql';
33
import { DEFAULT_NETWORK_ID, NETWORK_IDS } from '../helpers/utils';
44

55
export const getFollowsCount = async (follower: string): Promise<number> => {
6-
const query = `SELECT COUNT(*) AS count FROM follows WHERE follower = ?`;
6+
const query = `
7+
SELECT COUNT(*) AS count
8+
FROM follows
9+
JOIN spaces ON spaces.id = follows.space
10+
WHERE follower = ? AND spaces.deleted = 0
11+
`;
712

813
const [{ count }] = await db.queryAsync(query, [follower]);
914

test/integration/writer/follows.test.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { verify, action } from '../../../src/writer/follow';
21
import { FOLLOWS_LIMIT_PER_USER } from '../../../src/helpers/limits';
32
import db, { sequencerDB } from '../../../src/helpers/mysql';
3+
import { action, verify } from '../../../src/writer/follow';
44
import { spacesSqlFixtures } from '../../fixtures/space';
55

66
describe('writer/follow', () => {
@@ -9,7 +9,7 @@ describe('writer/follow', () => {
99

1010
afterAll(async () => {
1111
await db.queryAsync('DELETE FROM follows');
12-
await db.queryAsync('DELETE FROM spaces WHERE id = ?', [`${TEST_PREFIX}-${space.id}`]);
12+
await db.queryAsync('DELETE FROM spaces WHERE id LIKE ?', [`${TEST_PREFIX}%`]);
1313
await db.endAsync();
1414
await sequencerDB.endAsync();
1515
});
@@ -18,14 +18,22 @@ describe('writer/follow', () => {
1818
const followerId = '0x0';
1919

2020
beforeAll(async () => {
21-
let i = 0;
21+
let i = 1;
2222
const promises: Promise<any>[] = [];
2323

2424
while (i <= FOLLOWS_LIMIT_PER_USER) {
25+
promises.push(
26+
db.queryAsync('INSERT INTO snapshot_sequencer_test.spaces SET ?', {
27+
...space,
28+
id: `${TEST_PREFIX}${i}.eth`,
29+
deleted: 0,
30+
settings: JSON.stringify(space.settings)
31+
})
32+
);
2533
promises.push(
2634
db.queryAsync(
2735
'INSERT INTO follows SET id = ?, ipfs = ?, follower = ?, space = ?, created = ?',
28-
[i, i, followerId, `test-${i}.eth`, i]
36+
[i, i, followerId, `${TEST_PREFIX}${i}.eth`, i]
2937
)
3038
);
3139

@@ -41,6 +49,18 @@ describe('writer/follow', () => {
4149
);
4250
});
4351

52+
it('ignores deleted spaces from the limit', async () => {
53+
await db.queryAsync('UPDATE snapshot_sequencer_test.spaces SET deleted = 1 WHERE id = ?', [
54+
`${TEST_PREFIX}1.eth`
55+
]);
56+
57+
await expect(verify({ from: followerId })).resolves.toEqual(true);
58+
59+
return db.queryAsync('UPDATE snapshot_sequencer_test.spaces SET deleted = 0 WHERE id = ?', [
60+
`${TEST_PREFIX}1.eth`
61+
]);
62+
});
63+
4464
it('returns true when the user has not reached the limit', () => {
4565
return expect(verify({ from: '0x1' })).resolves.toEqual(true);
4666
});
@@ -111,15 +131,15 @@ describe('writer/follow', () => {
111131
it('should increment the follower count of the space', async () => {
112132
await db.queryAsync('INSERT INTO spaces SET ?', {
113133
...space,
114-
id: `${TEST_PREFIX}-${space.id}`,
134+
id: `${TEST_PREFIX}${space.id}`,
115135
settings: JSON.stringify(space.settings)
116136
});
117137

118138
const id = '3';
119139
const ipfs = '4';
120140
const message = {
121141
from: '0x4',
122-
space: `${TEST_PREFIX}-${space.id}`,
142+
space: `${TEST_PREFIX}${space.id}`,
123143
timestamp: 1
124144
};
125145

0 commit comments

Comments
 (0)