[mastodon-client] fix polls

This commit is contained in:
Laura Hausmann 2023-07-08 01:42:51 +02:00
parent 524c8f07ec
commit 9d37f01113
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
options: Array<PollOption>
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).
* @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
// ======================================

View file

@ -1688,31 +1688,35 @@ export default class Misskey implements MegalodonInterface {
// ======================================
// statuses/polls
// ======================================
public async getPoll(_id: string): Promise<Response<Entity.Poll>> {
return new Promise((_, reject) => {
const err = new NoImplementedError('misskey does not support')
reject(err)
})
public async getPoll(id: string): Promise<Response<Entity.Poll>> {
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<number>, status_id?: string | null): Promise<Response<Entity.Poll>> {
if (!status_id) {
public async votePoll(id: string, choices: Array<number>): Promise<Response<Entity.Poll>> {
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<MisskeyAPI.Entity.Note>('/api/notes/show', {
noteId: status_id
noteId: id
})
.then(async res => {
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 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,