diff --git a/packages/megalodon/src/entities/poll.ts b/packages/megalodon/src/entities/poll.ts index 7e85bd55b..42e7f69ac 100644 --- a/packages/megalodon/src/entities/poll.ts +++ b/packages/megalodon/src/entities/poll.ts @@ -10,5 +10,6 @@ namespace Entity { options: Array voted: boolean emojis?: [] + own_votes?: Array } } diff --git a/packages/megalodon/src/misskey.ts b/packages/megalodon/src/misskey.ts index 3df5aba35..5d87b51fb 100644 --- a/packages/megalodon/src/misskey.ts +++ b/packages/megalodon/src/misskey.ts @@ -1502,42 +1502,50 @@ 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) - }) + 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) { - return new Promise((_, reject) => { - const err = new ArgumentError('status_id is required') - reject(err) - }) - } - const params = { - noteId: status_id, - choice: choices[0] - } - await this.client.post<{}>('/api/notes/polls/vote', params) + public async votePoll(_id: string, choices: Array): Promise> { + if (!_id) { + return new Promise((_, reject) => { + const err = new ArgumentError("id is required"); + reject(err); + }); + } + + 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 - }) - .then(res => { - const note = MisskeyAPI.Converter.note(res.data, this.baseUrl) - return { ...res, data: note.poll } - }) + .post("/api/notes/show", { + noteId: _id, + }) + .then(async (res) => { + const note = await MisskeyAPI.Converter.note( + res.data, + this.baseUrl, + ); + return { ...res, data: note.poll }; + }); + if (!res.data) { return new Promise((_, reject) => { - const err = new UnexpectedError('poll does not exist') - reject(err) - }) + const err = new UnexpectedError("poll does not exist"); + reject(err); + }); } - return { ...res, data: res.data } + + return { ...res, data: res.data }; } // ====================================== diff --git a/packages/megalodon/src/misskey/api_client.ts b/packages/megalodon/src/misskey/api_client.ts index 1193fba90..c8be7b6b5 100644 --- a/packages/megalodon/src/misskey/api_client.ts +++ b/packages/megalodon/src/misskey/api_client.ts @@ -242,18 +242,19 @@ namespace MisskeyAPI { } } - export const poll = (p: Entity.Poll): MegalodonEntity.Poll => { + export const 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: Array.isArray(p.choices) ? p.choices.map(c => choice(c)) : [], voted: Array.isArray(p.choices) ? p.choices.some(c => c.isVoted) : false, + own_votes: Array.isArray(p.choices) ? p.choices.filter((c) => c.isVoted).map((c) => p.choices.indexOf(c)) : [], emojis: [], } } @@ -294,7 +295,7 @@ namespace MisskeyAPI { mentions: [], tags: [], card: null, - poll: n.poll ? poll(n.poll) : null, + poll: n.poll ? poll(n.poll, n.id) : null, application: null, language: null, pinned: null,