Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: store proposal, vote and follower count on space #292

Merged
merged 27 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a64b8c0
feat: store proposal, vote and follower count on space
bonustrack Feb 24, 2024
faeba17
Merge branch 'master' into fabien/space-counters
bonustrack Feb 24, 2024
f36f123
Merge branch 'master' into fabien/space-counters
wa0x6e Feb 26, 2024
6fa7c39
Merge branch 'master' into fabien/space-counters
wa0x6e Feb 26, 2024
5031e82
Merge branch 'master' into fabien/space-counters
bonustrack Feb 28, 2024
e82268b
Merge branch 'master' into fabien/space-counters
bonustrack Mar 6, 2024
188a232
Merge branch 'master' into fabien/space-counters
bonustrack Mar 7, 2024
84b553e
Merge branch 'master' into fabien/space-counters
bonustrack Mar 18, 2024
3765b08
Merge branch 'master' into fabien/space-counters
wa0x6e Apr 5, 2024
16bc059
Merge branch 'master' into fabien/space-counters
ChaituVR Apr 8, 2024
87ce20f
Merge branch 'master' into fabien/space-counters
wa0x6e May 25, 2024
f9fdef1
Merge branch 'master' into fabien/space-counters
wa0x6e May 25, 2024
b894b8a
fix: ensure positive counters
wa0x6e May 25, 2024
c156078
Merge branch 'fabien/space-counters' of https://github.com/snapshot-l…
wa0x6e May 25, 2024
1e0b9e7
fix: fix missing arg
wa0x6e May 25, 2024
f090eef
fix: use total number of votes, instead of number of valid votes
wa0x6e May 25, 2024
84d2851
fix: add init script to populate spaces counters
wa0x6e May 26, 2024
5079797
chore: add tests for follow and unfollow
wa0x6e May 26, 2024
50ce0a5
Merge branch 'master' into fabien/space-counters
ChaituVR May 28, 2024
20215e6
Merge branch 'master' into fabien/space-counters
ChaituVR May 29, 2024
9e3940a
Merge branch 'master' into fabien/space-counters
wa0x6e Jun 2, 2024
5164618
Update scripts/refresh_spaces_counters.ts
wa0x6e Jun 2, 2024
f9c4366
fix: fix proposals counter being skipped when no votes
wa0x6e Jun 2, 2024
9d3f895
fix: fix missing follows_count initializer
wa0x6e Jun 2, 2024
8a45195
Merge branch 'master' into fabien/space-counters
wa0x6e Jun 30, 2024
78cf2b2
Merge branch 'master' into fabien/space-counters
wa0x6e Jun 30, 2024
a7bbced
Merge branch 'master' into fabien/space-counters
ChaituVR Jul 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions scripts/refresh_spaces_counters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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, COUNT(DISTINCT(proposal)) as proposal_count FROM votes WHERE space = ? GROUP BY space`,
[spaces[i].id]
wa0x6e marked this conversation as resolved.
Show resolved Hide resolved
);

for (const stat of stats) {
await db.queryAsync(`UPDATE spaces SET vote_count = ?, proposal_count = ? WHERE id = ?`, [
stat.vote_count,
stat.proposal_count,
spaces[i].id
]);
console.log(`${i} / ${spaces.length}`);
}
wa0x6e marked this conversation as resolved.
Show resolved Hide resolved
}
}

(async () => {
try {
await main();
process.exit(0);
} catch (e) {
console.error(e);
process.exit(1);
}
})();
6 changes: 5 additions & 1 deletion src/writer/delete-proposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export async function verify(body): Promise<any> {
export async function action(body): Promise<void> {
const msg = jsonParse(body.msg);
const proposal = await getProposal(msg.space, msg.payload.proposal);

const voters = await db.queryAsync(`SELECT voter FROM votes WHERE proposal = ?`, [
msg.payload.proposal
]);
Expand All @@ -33,9 +34,12 @@ export async function action(body): Promise<void> {
SET proposal_count = GREATEST(proposal_count - 1, 0)
WHERE user = ? AND space = ?
LIMIT 1;
UPDATE spaces
SET proposal_count = GREATEST(proposal_count - 1, 0), vote_count = GREATEST(vote_count - ?, 0)
WHERE id = ?;
`;

const parameters = [id, id, proposal.author, msg.space];
const parameters = [id, id, proposal.author, msg.space, voters.length, msg.space];

if (voters.length > 0) {
queries += `
Expand Down
13 changes: 11 additions & 2 deletions src/writer/follow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ export const networks =
export const defaultNetwork = networks[0];

export async function verify(message): Promise<any> {
const query = `SELECT * FROM follows WHERE follower = ? AND space = ? LIMIT 1`;
const follows = await db.queryAsync(query, [message.from, message.space]);

if (follows.length !== 0) return Promise.reject('you are already following this space');

const count = await getFollowsCount(message.from);

if (count >= FOLLOWS_LIMIT_PER_USER) {
Expand All @@ -31,7 +36,11 @@ export async function verify(message): Promise<any> {
return true;
}

export async function action(message, ipfs, receipt, id): Promise<void> {
export async function action(message, ipfs, _receipt, id): Promise<void> {
const query = `
INSERT INTO follows SET ?;
UPDATE spaces SET follower_count = follower_count + 1 WHERE id = ?;
`;
const params = {
id,
ipfs,
Expand All @@ -41,5 +50,5 @@ export async function action(message, ipfs, receipt, id): Promise<void> {
created: message.timestamp
};

await db.queryAsync('INSERT INTO follows SET ?', params);
await db.queryAsync(query, [params, message.space]);
}
7 changes: 4 additions & 3 deletions src/writer/proposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,10 @@ export async function action(body, ipfs, receipt, id): Promise<void> {
const query = `
INSERT INTO proposals SET ?;
INSERT INTO leaderboard (space, user, proposal_count)
VALUES(?, ?, 1)
ON DUPLICATE KEY UPDATE proposal_count = proposal_count + 1
VALUES(?, ?, 1)
ON DUPLICATE KEY UPDATE proposal_count = proposal_count + 1;
UPDATE spaces SET proposal_count = proposal_count + 1 WHERE id = ?;
`;

await db.queryAsync(query, [proposal, space, author]);
await db.queryAsync(query, [proposal, space, author, space]);
}
23 changes: 20 additions & 3 deletions src/writer/unfollow.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
import db from '../helpers/mysql';
import { defaultNetwork } from './follow';

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 || defaultNetwork
]);

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 || defaultNetwork]);
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 || defaultNetwork,
message.space
]);
}
5 changes: 3 additions & 2 deletions src/writer/vote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,10 @@ export async function action(body, ipfs, receipt, id, context): Promise<void> {
INSERT INTO votes SET ?;
INSERT INTO leaderboard (space, user, vote_count, last_vote)
VALUES(?, ?, 1, ?)
ON DUPLICATE KEY UPDATE vote_count = vote_count + 1, last_vote = ?
ON DUPLICATE KEY UPDATE vote_count = vote_count + 1, last_vote = ?;
UPDATE spaces SET vote_count = vote_count + 1 WHERE id = ?;
`,
[params, msg.space, voter, created, created]
[params, msg.space, voter, created, created, msg.space]
);
}

Expand Down
Loading