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>
voted: boolean
emojis?: []
own_votes?: Array<number>
}
}

View File

@ -1502,42 +1502,50 @@ 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)
})
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')
reject(err)
})
const err = new ArgumentError("id is required");
reject(err);
});
}
for (const c of choices) {
const params = {
noteId: status_id,
choice: choices[0]
noteId: _id,
choice: +c,
};
await this.client.post<{}>("/api/notes/polls/vote", params);
}
await this.client.post<{}>('/api/notes/polls/vote', params)
const res = await this.client
.post<MisskeyAPI.Entity.Note>('/api/notes/show', {
noteId: status_id
})
.then(res => {
const note = MisskeyAPI.Converter.note(res.data, this.baseUrl)
return { ...res, data: note.poll }
.post<MisskeyAPI.Entity.Note>("/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 };
}
// ======================================

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 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,