upd: fix poll handling

This commit is contained in:
Mar0xy 2023-09-25 23:56:47 +02:00
parent f7c6f8ac3d
commit d36b855457
No known key found for this signature in database
GPG key ID: 56569BBE47D2C828
3 changed files with 40 additions and 30 deletions

View file

@ -10,5 +10,6 @@ namespace Entity {
options: Array<PollOption> options: Array<PollOption>
voted: boolean voted: boolean
emojis?: [] emojis?: []
own_votes?: Array<number>
} }
} }

View file

@ -1502,42 +1502,50 @@ 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) throw new Error("poll not found");
reject(err) 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(res => { .then(async (res) => {
const note = MisskeyAPI.Converter.note(res.data, this.baseUrl) const note = await MisskeyAPI.Converter.note(
return { ...res, data: note.poll } res.data,
}) this.baseUrl,
);
return { ...res, data: note.poll };
});
if (!res.data) { if (!res.data) {
return new Promise((_, reject) => { return new Promise((_, reject) => {
const err = new UnexpectedError('poll does not exist') const err = new UnexpectedError("poll does not exist");
reject(err) reject(err);
}) });
} }
return { ...res, data: res.data }
return { ...res, data: res.data };
} }
// ====================================== // ======================================

View file

@ -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 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: Array.isArray(p.choices) ? p.choices.map(c => choice(c)) : [], options: Array.isArray(p.choices) ? p.choices.map(c => choice(c)) : [],
voted: Array.isArray(p.choices) ? p.choices.some(c => c.isVoted) : false, 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: [], emojis: [],
} }
} }
@ -294,7 +295,7 @@ namespace MisskeyAPI {
mentions: [], mentions: [],
tags: [], tags: [],
card: null, card: null,
poll: n.poll ? poll(n.poll) : null, poll: n.poll ? poll(n.poll, n.id) : null,
application: null, application: null,
language: null, language: null,
pinned: null, pinned: null,