-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: store proposal, vote and follower count on space (#292)
* feat: store proposal, vote and follower count on space * fix: ensure positive counters * fix: fix missing arg * fix: use total number of votes, instead of number of valid votes * fix: add init script to populate spaces counters * chore: add tests for follow and unfollow * Update scripts/refresh_spaces_counters.ts Co-authored-by: Chaitanya <[email protected]> * fix: fix proposals counter being skipped when no votes * fix: fix missing follows_count initializer --------- Co-authored-by: Wan Qi Chen <[email protected]> Co-authored-by: Chaitanya <[email protected]>
- Loading branch information
1 parent
daa929f
commit 2869080
Showing
8 changed files
with
193 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import 'dotenv/config'; | ||
import db from '../src/helpers/mysql'; | ||
|
||
// Usage: yarn ts-node scripts/refresh_spaces_counters.ts | ||
async function main() { | ||
const spaces = await db.queryAsync(`SELECT id FROM spaces`); | ||
|
||
console.log(`Found ${spaces.length} spaces`); | ||
|
||
for (const i in spaces) { | ||
const stats = await db.queryAsync( | ||
`SELECT COUNT(voter) as vote_count FROM votes WHERE space = ?`, | ||
[spaces[i].id] | ||
); | ||
const stat = stats[0]; | ||
await db.queryAsync(`UPDATE spaces SET vote_count = ? WHERE id = ?`, [ | ||
stat.vote_count, | ||
spaces[i].id | ||
]); | ||
console.log(`${i} / ${spaces.length}`); | ||
} | ||
|
||
await db.queryAsync( | ||
'UPDATE spaces SET proposal_count = (SELECT count(id) from proposals WHERE space = spaces.id)' | ||
); | ||
|
||
await db.queryAsync( | ||
'UPDATE spaces SET follower_count = (SELECT count(id) from follows WHERE space = spaces.id)' | ||
); | ||
} | ||
|
||
(async () => { | ||
try { | ||
await main(); | ||
process.exit(0); | ||
} catch (e) { | ||
console.error(e); | ||
process.exit(1); | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,27 @@ | ||
import db from '../helpers/mysql'; | ||
import { DEFAULT_NETWORK_ID } from '../helpers/utils'; | ||
|
||
export async function verify(): Promise<any> { | ||
export async function verify(message): Promise<any> { | ||
const query = `SELECT * FROM follows WHERE follower = ? AND space = ? AND network = ? LIMIT 1`; | ||
const follows = await db.queryAsync(query, [ | ||
message.from, | ||
message.space, | ||
message.network || DEFAULT_NETWORK_ID | ||
]); | ||
|
||
if (follows.length === 0) return Promise.reject('you can only unfollow a space you follow'); | ||
|
||
return true; | ||
} | ||
|
||
export async function action(message): Promise<void> { | ||
const query = 'DELETE FROM follows WHERE follower = ? AND space = ? AND network = ? LIMIT 1'; | ||
await db.queryAsync(query, [message.from, message.space, message.network || DEFAULT_NETWORK_ID]); | ||
const query = ` | ||
DELETE FROM follows WHERE follower = ? AND space = ? AND network = ? LIMIT 1; | ||
UPDATE spaces SET follower_count = GREATEST(follower_count - 1, 0) WHERE id = ?;`; | ||
await db.queryAsync(query, [ | ||
message.from, | ||
message.space, | ||
message.network || DEFAULT_NETWORK_ID, | ||
message.space | ||
]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { verify, action } from '../../../src/writer/unfollow'; | ||
import db, { sequencerDB } from '../../../src/helpers/mysql'; | ||
import { spacesSqlFixtures } from '../../fixtures/space'; | ||
|
||
describe('writer/unfollow', () => { | ||
const TEST_PREFIX = 'test-unfollow-'; | ||
const space = spacesSqlFixtures[0]; | ||
const followerId = '0x0'; | ||
|
||
afterAll(async () => { | ||
await db.queryAsync('DELETE FROM follows'); | ||
await db.queryAsync('DELETE FROM spaces WHERE id LIKE ?', [`${TEST_PREFIX}-%`]); | ||
await db.endAsync(); | ||
await sequencerDB.endAsync(); | ||
}); | ||
|
||
describe('verify()', () => { | ||
beforeAll(async () => { | ||
let i = 0; | ||
const promises: Promise<any>[] = []; | ||
|
||
while (i <= 2) { | ||
promises.push( | ||
db.queryAsync( | ||
'INSERT INTO follows SET id = ?, ipfs = ?, follower = ?, space = ?, network = ?, created = ?', | ||
[i, i, followerId, `${TEST_PREFIX}-test-${i}.eth`, 's', i] | ||
) | ||
); | ||
|
||
i++; | ||
} | ||
|
||
await Promise.all(promises); | ||
}); | ||
|
||
it('rejects when the user has not followed the space', () => { | ||
return expect(verify({ from: '0x1', space: `${TEST_PREFIX}-test-2.eth` })).rejects.toEqual( | ||
'you can only unfollow a space you follow' | ||
); | ||
}); | ||
|
||
it('returns true when the user has followed the space', () => { | ||
return expect( | ||
verify({ from: followerId, space: `${TEST_PREFIX}-test-0.eth`, network: 's' }) | ||
).resolves.toEqual(true); | ||
}); | ||
}); | ||
|
||
describe('action()', () => { | ||
it('should unfollow and decrement the follower count of the space', async () => { | ||
await db.queryAsync('INSERT INTO spaces SET ?', { | ||
...space, | ||
id: `${TEST_PREFIX}-test-0.eth`, | ||
settings: JSON.stringify(space.settings), | ||
follower_count: 2 | ||
}); | ||
|
||
const message = { | ||
from: followerId, | ||
space: `${TEST_PREFIX}-test-0.eth`, | ||
network: 's' | ||
}; | ||
|
||
expect( | ||
db.queryAsync('SELECT id FROM follows WHERE follower = ? AND space = ?', [ | ||
message.from, | ||
message.space | ||
]) | ||
).resolves.not.toEqual([]); | ||
|
||
await action(message); | ||
|
||
expect( | ||
db.queryAsync('SELECT * FROM follows WHERE follower = ? AND space = ?', [ | ||
message.from, | ||
message.space | ||
]) | ||
).resolves.toEqual([]); | ||
return expect( | ||
db.queryAsync('SELECT follower_count FROM spaces WHERE id = ?', [message.space]) | ||
).resolves.toEqual([{ follower_count: 1 }]); | ||
}); | ||
}); | ||
}); |