[mastodon-client] fix polls

This commit is contained in:
Laura Hausmann 2023-07-08 01:42:51 +02:00
parent 2dd7bdcfa1
commit 326b0ea408
No known key found for this signature in database
GPG Key ID: D044E84C5BE01605
4 changed files with 25 additions and 19 deletions

View File

@ -9,5 +9,6 @@ namespace Entity {
votes_count: number votes_count: number
options: Array<PollOption> options: Array<PollOption>
voted: boolean voted: boolean
own_votes: Array<number>
} }
} }

View File

@ -841,7 +841,7 @@ export interface MegalodonInterface {
* @param choices Array of own votes containing index for each option (starting from 0). * @param choices Array of own votes containing index for each option (starting from 0).
* @return Poll * @return Poll
*/ */
votePoll(id: string, choices: Array<number>, status_id?: string | null): Promise<Response<Entity.Poll>> votePoll(id: string, choices: Array<number>): Promise<Response<Entity.Poll>>
// ====================================== // ======================================
// statuses/scheduled_statuses // statuses/scheduled_statuses
// ====================================== // ======================================

View File

@ -1688,31 +1688,35 @@ export default class Misskey implements MegalodonInterface {
// ====================================== // ======================================
// statuses/polls // statuses/polls
// ====================================== // ======================================
public async getPoll(_id: string): Promise<Response<Entity.Poll>> { public async getPoll(id: string): Promise<Response<Entity.Poll>> {
return new Promise((_, reject) => { const res = await this.getStatus(id);
const err = new NoImplementedError('misskey does not support') if (res.data.poll == null)
reject(err) throw new Error('poll not found');
}) return { ...res, data: res.data.poll }
} }
/** /**
* POST /api/notes/polls/vote * POST /api/notes/polls/vote
*/ */
public async votePoll(_id: string, choices: Array<number>, status_id?: string | null): Promise<Response<Entity.Poll>> { public async votePoll(id: string, choices: Array<number>): Promise<Response<Entity.Poll>> {
if (!status_id) { if (!id) {
return new Promise((_, reject) => { return new Promise((_, reject) => {
const err = new ArgumentError('status_id is required') const err = new ArgumentError('id is required')
reject(err) reject(err)
}) })
} }
const params = {
noteId: status_id, for (const c of choices) {
choice: choices[0] const params = {
} noteId: id,
await this.client.post<{}>('/api/notes/polls/vote', params) choice: +c
}
await this.client.post<{}>('/api/notes/polls/vote', params)
}
const res = await this.client const res = await this.client
.post<MisskeyAPI.Entity.Note>('/api/notes/show', { .post<MisskeyAPI.Entity.Note>('/api/notes/show', {
noteId: status_id noteId: id
}) })
.then(async res => { .then(async res => {
const note = await this.noteWithDetails(res.data, this.baseUrlToHost(this.baseUrl), this.getFreshAccountCache()) const note = await this.noteWithDetails(res.data, this.baseUrlToHost(this.baseUrl), this.getFreshAccountCache())

View File

@ -275,18 +275,19 @@ namespace MisskeyAPI {
} }
} }
poll = (p: Entity.Poll): MegalodonEntity.Poll => { poll = (p: Entity.Poll, id: string): MegalodonEntity.Poll => {
const now = dayjs() const now = dayjs()
const expire = dayjs(p.expiresAt) const expire = dayjs(p.expiresAt)
const count = p.choices.reduce((sum, choice) => sum + choice.votes, 0) const count = p.choices.reduce((sum, choice) => sum + choice.votes, 0)
return { return {
id: '', id: id,
expires_at: p.expiresAt, expires_at: p.expiresAt,
expired: now.isAfter(expire), expired: now.isAfter(expire),
multiple: p.multiple, multiple: p.multiple,
votes_count: count, votes_count: count,
options: p.choices.map(c => this.choice(c)), 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: [], mentions: [],
tags: [], tags: [],
card: null, card: null,
poll: n.poll ? this.poll(n.poll) : null, poll: n.poll ? this.poll(n.poll, n.id) : null,
application: null, application: null,
language: null, language: null,
pinned: null, pinned: null,