Skip to content

Commit

Permalink
Add timeline states argument
Browse files Browse the repository at this point in the history
  • Loading branch information
bonustrack committed Apr 19, 2021
1 parent 6a6e272 commit 9202a24
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
27 changes: 23 additions & 4 deletions server/graphql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,31 @@ const schemaFile = importSchema('./**/*.graphql');
export const schema = buildSchema(schemaFile);

export const rootValue = {
timeline: async ({ spaces = [], first = 20, skip = 0 }) => {
const ts = (Date.now() / 1e3).toFixed();
timeline: async ({ spaces = [], first = 10, skip = 0, state }) => {
const ts = parseInt((Date.now() / 1e3).toFixed());
if (spaces.length === 0) spaces = Object.keys(registrySpaces) as any;

const query = `SELECT * FROM messages WHERE type = 'proposal' AND timestamp > ? AND space IN (?) ORDER BY timestamp DESC LIMIT ?, ?`;
const msgs = await db.queryAsync(query, [1614473607, spaces, skip, first]);
const params: any[] = [1614473607, spaces];

let stateStr = '';
if (state === 'pending') {
stateStr = 'AND JSON_EXTRACT(payload, "$.start") > ?';
params.push(ts);
}
if (state === 'active') {
stateStr =
'AND JSON_EXTRACT(payload, "$.start") < ? AND JSON_EXTRACT(payload, "$.end") > ?';
params.push(ts, ts);
}
if (state === 'closed') {
stateStr = 'AND ? > JSON_EXTRACT(payload, "$.end")';
params.push(ts);
}

params.push(skip, first);

const query = `SELECT * FROM messages WHERE type = 'proposal' AND timestamp > ? AND space IN (?) ${stateStr} ORDER BY timestamp DESC LIMIT ?, ?`;
const msgs = await db.queryAsync(query, params);

const authors = Array.from(new Set(msgs.map(msg => msg.address)));
const users = await getProfiles(authors);
Expand Down
12 changes: 0 additions & 12 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,6 @@ router.get('/:space/proposals', async (req, res) => {
});
});

router.get('/timeline', async (req, res) => {
const spacesArr = req.query.spaces
? (req.query.spaces as string).split(',')
: Object.keys(spaces);
const query = `SELECT * FROM messages WHERE type = 'proposal' AND timestamp > ? AND space IN (?) ORDER BY timestamp DESC LIMIT 30`;
db.queryAsync(query, [1618473607, spacesArr]).then(messages => {
res.json(
Object.fromEntries(messages.map(message => formatMessage(message)))
);
});
});

router.get('/:space/proposal/:id', async (req, res) => {
const { space, id } = req.params;
const query = `SELECT * FROM messages WHERE type = 'vote' AND space = ? AND JSON_EXTRACT(payload, "$.proposal") = ? ORDER BY timestamp ASC`;
Expand Down

0 comments on commit 9202a24

Please sign in to comment.