Skip to content

Commit

Permalink
fix: ignore deleted spaces when checking for followed spaces limit (#486
Browse files Browse the repository at this point in the history
)

* fix: ignore deleted spaces when checking for followed spaces limit

* test: add tests

---------

Co-authored-by: Chaitanya <[email protected]>
  • Loading branch information
wa0x6e and ChaituVR authored Jan 8, 2025
1 parent 1fc30c1 commit 0776492
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/writer/follow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import db from '../helpers/mysql';
import { DEFAULT_NETWORK_ID, NETWORK_IDS } from '../helpers/utils';

export const getFollowsCount = async (follower: string): Promise<number> => {
const query = `SELECT COUNT(*) AS count FROM follows WHERE follower = ?`;
const query = `
SELECT COUNT(*) AS count
FROM follows
JOIN spaces ON spaces.id = follows.space
WHERE follower = ? AND spaces.deleted = 0
`;

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

Expand Down
32 changes: 26 additions & 6 deletions test/integration/writer/follows.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { verify, action } from '../../../src/writer/follow';
import { FOLLOWS_LIMIT_PER_USER } from '../../../src/helpers/limits';
import db, { sequencerDB } from '../../../src/helpers/mysql';
import { action, verify } from '../../../src/writer/follow';
import { spacesSqlFixtures } from '../../fixtures/space';

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

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

beforeAll(async () => {
let i = 0;
let i = 1;
const promises: Promise<any>[] = [];

while (i <= FOLLOWS_LIMIT_PER_USER) {
promises.push(
db.queryAsync('INSERT INTO snapshot_sequencer_test.spaces SET ?', {
...space,
id: `${TEST_PREFIX}${i}.eth`,
deleted: 0,
settings: JSON.stringify(space.settings)
})
);
promises.push(
db.queryAsync(
'INSERT INTO follows SET id = ?, ipfs = ?, follower = ?, space = ?, created = ?',
[i, i, followerId, `test-${i}.eth`, i]
[i, i, followerId, `${TEST_PREFIX}${i}.eth`, i]
)
);

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

it('ignores deleted spaces from the limit', async () => {
await db.queryAsync('UPDATE snapshot_sequencer_test.spaces SET deleted = 1 WHERE id = ?', [
`${TEST_PREFIX}1.eth`
]);

await expect(verify({ from: followerId })).resolves.toEqual(true);

return db.queryAsync('UPDATE snapshot_sequencer_test.spaces SET deleted = 0 WHERE id = ?', [
`${TEST_PREFIX}1.eth`
]);
});

it('returns true when the user has not reached the limit', () => {
return expect(verify({ from: '0x1' })).resolves.toEqual(true);
});
Expand Down Expand Up @@ -111,15 +131,15 @@ describe('writer/follow', () => {
it('should increment the follower count of the space', async () => {
await db.queryAsync('INSERT INTO spaces SET ?', {
...space,
id: `${TEST_PREFIX}-${space.id}`,
id: `${TEST_PREFIX}${space.id}`,
settings: JSON.stringify(space.settings)
});

const id = '3';
const ipfs = '4';
const message = {
from: '0x4',
space: `${TEST_PREFIX}-${space.id}`,
space: `${TEST_PREFIX}${space.id}`,
timestamp: 1
};

Expand Down

0 comments on commit 0776492

Please sign in to comment.