From 326b0ea40862795a0d057ed3f3c61a17a83d4726 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Sat, 8 Jul 2023 01:42:51 +0200 Subject: [PATCH] [mastodon-client] fix polls --- packages/megalodon/src/entities/poll.ts | 1 + packages/megalodon/src/megalodon.ts | 2 +- packages/megalodon/src/misskey.ts | 32 +++++++++++--------- packages/megalodon/src/misskey/api_client.ts | 9 +++--- 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/packages/megalodon/src/entities/poll.ts b/packages/megalodon/src/entities/poll.ts index 69706e8ae..c4f8f4f6d 100644 --- a/packages/megalodon/src/entities/poll.ts +++ b/packages/megalodon/src/entities/poll.ts @@ -9,5 +9,6 @@ namespace Entity { votes_count: number options: Array voted: boolean + own_votes: Array } } diff --git a/packages/megalodon/src/megalodon.ts b/packages/megalodon/src/megalodon.ts index 74966ccf4..1cd0a7db3 100644 --- a/packages/megalodon/src/megalodon.ts +++ b/packages/megalodon/src/megalodon.ts @@ -841,7 +841,7 @@ export interface MegalodonInterface { * @param choices Array of own votes containing index for each option (starting from 0). * @return Poll */ - votePoll(id: string, choices: Array, status_id?: string | null): Promise> + votePoll(id: string, choices: Array): Promise> // ====================================== // statuses/scheduled_statuses // ====================================== diff --git a/packages/megalodon/src/misskey.ts b/packages/megalodon/src/misskey.ts index c7db1aad4..07826f316 100644 --- a/packages/megalodon/src/misskey.ts +++ b/packages/megalodon/src/misskey.ts @@ -1688,31 +1688,35 @@ export default class Misskey implements MegalodonInterface { // ====================================== // statuses/polls // ====================================== - public async getPoll(_id: string): Promise> { - return new Promise((_, reject) => { - const err = new NoImplementedError('misskey does not support') - reject(err) - }) + public async getPoll(id: string): Promise> { + const res = await this.getStatus(id); + if (res.data.poll == null) + throw new Error('poll not found'); + return { ...res, data: res.data.poll } } /** * POST /api/notes/polls/vote */ - public async votePoll(_id: string, choices: Array, status_id?: string | null): Promise> { - if (!status_id) { + public async votePoll(id: string, choices: Array): Promise> { + if (!id) { return new Promise((_, reject) => { - const err = new ArgumentError('status_id is required') + const err = new ArgumentError('id is required') reject(err) }) } - const params = { - noteId: status_id, - choice: choices[0] - } - await this.client.post<{}>('/api/notes/polls/vote', params) + + for (const c of choices) { + const params = { + noteId: id, + choice: +c + } + await this.client.post<{}>('/api/notes/polls/vote', params) + } + const res = await this.client .post('/api/notes/show', { - noteId: status_id + noteId: id }) .then(async res => { const note = await this.noteWithDetails(res.data, this.baseUrlToHost(this.baseUrl), this.getFreshAccountCache()) diff --git a/packages/megalodon/src/misskey/api_client.ts b/packages/megalodon/src/misskey/api_client.ts index 1833f9956..180ec6fdb 100644 --- a/packages/megalodon/src/misskey/api_client.ts +++ b/packages/megalodon/src/misskey/api_client.ts @@ -275,18 +275,19 @@ namespace MisskeyAPI { } } - poll = (p: Entity.Poll): MegalodonEntity.Poll => { + poll = (p: Entity.Poll, id: string): MegalodonEntity.Poll => { const now = dayjs() const expire = dayjs(p.expiresAt) const count = p.choices.reduce((sum, choice) => sum + choice.votes, 0) return { - id: '', + id: id, expires_at: p.expiresAt, expired: now.isAfter(expire), multiple: p.multiple, votes_count: count, options: p.choices.map(c => this.choice(c)), - voted: p.choices.some(c => c.isVoted) + voted: p.choices.some(c => c.isVoted), + own_votes: p.choices.filter(c => c.isVoted).map(c => p.choices.indexOf(c)) } } @@ -318,7 +319,7 @@ namespace MisskeyAPI { mentions: [], tags: [], card: null, - poll: n.poll ? this.poll(n.poll) : null, + poll: n.poll ? this.poll(n.poll, n.id) : null, application: null, language: null, pinned: null,