-
Notifications
You must be signed in to change notification settings - Fork 15
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
Changes from all 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 faeba17
Merge branch 'master' into fabien/space-counters
bonustrack f36f123
Merge branch 'master' into fabien/space-counters
wa0x6e 6fa7c39
Merge branch 'master' into fabien/space-counters
wa0x6e 5031e82
Merge branch 'master' into fabien/space-counters
bonustrack e82268b
Merge branch 'master' into fabien/space-counters
bonustrack 188a232
Merge branch 'master' into fabien/space-counters
bonustrack 84b553e
Merge branch 'master' into fabien/space-counters
bonustrack 3765b08
Merge branch 'master' into fabien/space-counters
wa0x6e 16bc059
Merge branch 'master' into fabien/space-counters
ChaituVR 87ce20f
Merge branch 'master' into fabien/space-counters
wa0x6e f9fdef1
Merge branch 'master' into fabien/space-counters
wa0x6e b894b8a
fix: ensure positive counters
wa0x6e c156078
Merge branch 'fabien/space-counters' of https://github.com/snapshot-l…
wa0x6e 1e0b9e7
fix: fix missing arg
wa0x6e f090eef
fix: use total number of votes, instead of number of valid votes
wa0x6e 84d2851
fix: add init script to populate spaces counters
wa0x6e 5079797
chore: add tests for follow and unfollow
wa0x6e 50ce0a5
Merge branch 'master' into fabien/space-counters
ChaituVR 20215e6
Merge branch 'master' into fabien/space-counters
ChaituVR 9e3940a
Merge branch 'master' into fabien/space-counters
wa0x6e 5164618
Update scripts/refresh_spaces_counters.ts
wa0x6e f9c4366
fix: fix proposals counter being skipped when no votes
wa0x6e 9d3f895
fix: fix missing follows_count initializer
wa0x6e 8a45195
Merge branch 'master' into fabien/space-counters
wa0x6e 78cf2b2
Merge branch 'master' into fabien/space-counters
wa0x6e a7bbced
Merge branch 'master' into fabien/space-counters
ChaituVR File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }]); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if these work correctly 🤔 @wa0x6e you tried?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, they should work
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Working :D i was confused