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 user space activty #127

Merged
merged 30 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
8557794
feat: update user_space counter
wa0x6e Aug 24, 2023
f85c449
chore: update sql schema for tests
wa0x6e Aug 24, 2023
0fb5b34
fix: decrease proposals_count on proposal deletion
wa0x6e Aug 24, 2023
f74fff8
fix: updates votes_count on proposal deletion
wa0x6e Aug 24, 2023
1efe0fa
fix: add counters generation script
wa0x6e Aug 26, 2023
8225ee9
fix: extract counters update to actions.ts
wa0x6e Aug 27, 2023
9a2cbb8
fix: fix wrong import
wa0x6e Aug 27, 2023
36f85f6
fix: trigger query only when necessary
wa0x6e Aug 27, 2023
e9f4687
fix: rename table
wa0x6e Aug 27, 2023
c5799c0
fix: fix argument to always be an array of strings
wa0x6e Aug 31, 2023
c7991e6
fix: fix invalid SQL queries
wa0x6e Aug 31, 2023
1a0041f
refactor: batch the refreshProposalsCounters
wa0x6e Aug 31, 2023
211797a
fix: use PRIMARY KEY instead of UNIQUE KEY
wa0x6e Sep 8, 2023
217c908
chore: fix column name to follow convention
wa0x6e Sep 8, 2023
adb2a96
fix: update to process one space at a time
wa0x6e Sep 8, 2023
1e4d175
chore: remove useless prettier
wa0x6e Sep 25, 2023
bfec416
Merge branch 'master' into update-user-space-counters
wa0x6e Apr 1, 2024
1353add
Merge branch 'master' into update-user-space-counters
wa0x6e Apr 8, 2024
4a4ee97
refactor: better console log
wa0x6e Apr 8, 2024
ff8a647
refactor: trigger activities update alongside main query
wa0x6e Apr 8, 2024
84bbd6e
Update scripts/generate_user_space_activities_counters.ts
ChaituVR Apr 14, 2024
e2bda13
Merge branch 'master' into update-user-space-counters
ChaituVR Apr 14, 2024
562cd2a
fix: fix invalid argument type
wa0x6e Apr 14, 2024
8d2117b
Merge branch 'update-user-space-counters' of https://github.com/snaps…
wa0x6e Apr 14, 2024
65cdaa4
fix: allow counter refresh script to target down a single space
wa0x6e Apr 14, 2024
d599a98
Merge branch 'master' into update-user-space-counters
wa0x6e Apr 14, 2024
2a1d8ad
refactor: rename table to `leaderboard`
wa0x6e Apr 16, 2024
dd3fdb5
Merge branch 'update-user-space-counters' of https://github.com/snaps…
wa0x6e Apr 16, 2024
7ecb58e
fix: rename votes and proposals count column
wa0x6e Apr 16, 2024
fcff05c
Update src/writer/delete-space.ts
wa0x6e Apr 16, 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
12 changes: 12 additions & 0 deletions src/writer/delete-proposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,23 @@ 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 id = msg.payload.proposal;

const query = `
DELETE FROM proposals WHERE id = ? LIMIT 1;
DELETE FROM votes WHERE proposal = ?;
`;
await db.queryAsync(query, [id, id]);

await db.queryAsync(
`UPDATE user_space SET proposals_count = proposals_count - 1 WHERE user_id = ? AND space_id = ? LIMIT 1`,
[proposal.author, msg.space]
);
await db.queryAsync(
`INSERT INTO user_space (votes_count, user_id, space_id)
(select * from (select count(id) as votes_count, voter, space from votes where space = ? group by voter, space) as t)
ON DUPLICATE KEY UPDATE votes_count = t.votes_count`,
[msg.space]
);
}
13 changes: 12 additions & 1 deletion src/writer/proposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,5 +223,16 @@ export async function action(body, ipfs, receipt, id): Promise<void> {
const query = 'INSERT IGNORE INTO proposals SET ?; ';
const params: any[] = [proposal];

await db.queryAsync(query, params);
const result = await db.queryAsync(query, params);

if (result.affectedRows > 0) {
await db.queryAsync(
`
INSERT INTO user_space (space_id, user_id, proposals_count)
VALUES(?, ?, 1)
ON DUPLICATE KEY UPDATE proposals_count = proposals_count + 1
`,
[space, author]
);
}
wa0x6e marked this conversation as resolved.
Show resolved Hide resolved
}
13 changes: 12 additions & 1 deletion src/writer/vote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,18 @@ export async function action(body, ipfs, receipt, id, context): Promise<void> {
);
} else {
// Store vote in dedicated table
await db.queryAsync('INSERT IGNORE INTO votes SET ?', params);
const result = await db.queryAsync('INSERT IGNORE INTO votes SET ?', params);

if (result.affectedRows > 0) {
await db.queryAsync(
`
INSERT INTO user_space (space_id, user_id, votes_count)
VALUES(?, ?, 1)
ON DUPLICATE KEY UPDATE votes_count = votes_count + 1
`,
[msg.space, voter]
);
}
}

// Update proposal scores and voters vp
Expand Down
11 changes: 11 additions & 0 deletions test/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,14 @@ CREATE TABLE messages (
INDEX type (type),
INDEX receipt (receipt)
);

CREATE TABLE user_space (
user_id varchar(64) NOT NULL,
space_id varchar(64) NOT NULL,
wa0x6e marked this conversation as resolved.
Show resolved Hide resolved
votes_count smallint unsigned NOT NULL DEFAULT '0',
proposals_count smallint unsigned NOT NULL DEFAULT '0',
UNIQUE KEY user_id_space_id (user_id,space_id),
wa0x6e marked this conversation as resolved.
Show resolved Hide resolved
INDEX space_id (space_id),
INDEX votes_count (votes_count),
INDEX proposals_count (proposals_count)
);