From d36b855457214a58e84e6e3d28320eb62cb9e1e3 Mon Sep 17 00:00:00 2001
From: Mar0xy <marie@kaifa.ch>
Date: Mon, 25 Sep 2023 23:56:47 +0200
Subject: [PATCH] upd: fix poll handling

---
 packages/megalodon/src/entities/poll.ts      |  1 +
 packages/megalodon/src/misskey.ts            | 62 +++++++++++---------
 packages/megalodon/src/misskey/api_client.ts |  7 ++-
 3 files changed, 40 insertions(+), 30 deletions(-)

diff --git a/packages/megalodon/src/entities/poll.ts b/packages/megalodon/src/entities/poll.ts
index 7e85bd55b4..42e7f69ac1 100644
--- a/packages/megalodon/src/entities/poll.ts
+++ b/packages/megalodon/src/entities/poll.ts
@@ -10,5 +10,6 @@ namespace Entity {
     options: Array<PollOption>
     voted: boolean
     emojis?: []
+    own_votes?: Array<number>
   }
 }
diff --git a/packages/megalodon/src/misskey.ts b/packages/megalodon/src/misskey.ts
index 3df5aba351..5d87b51fb8 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<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) {
-      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<number>): Promise<Response<Entity.Poll>> {
+    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<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 };
   }
 
   // ======================================
diff --git a/packages/megalodon/src/misskey/api_client.ts b/packages/megalodon/src/misskey/api_client.ts
index 1193fba90b..c8be7b6b5c 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,