11import { Create , Note } from "@fedify/fedify" ;
22import { zValidator } from "@hono/zod-validator" ;
33import { and , eq , inArray , sql } from "drizzle-orm" ;
4+ import { maxBy } from "es-toolkit" ;
45import { Hono } from "hono" ;
56import { z } from "zod" ;
67import { db } from "../../db" ;
@@ -62,7 +63,7 @@ app.post(
6263 with : { account : true } ,
6364 where : eq ( pollVotes . accountId , owner . id ) ,
6465 } ,
65- post : {
66+ posts : {
6667 with : {
6768 account : { with : { owner : true } } ,
6869 replyTarget : true ,
@@ -133,7 +134,7 @@ app.post(
133134 with : { account : true } ,
134135 where : eq ( pollVotes . accountId , owner . id ) ,
135136 } ,
136- post : {
137+ posts : {
137138 with : {
138139 account : { with : { owner : true } } ,
139140 replyTarget : true ,
@@ -148,14 +149,16 @@ app.post(
148149 } ) ;
149150 if ( poll == null ) throw new Error ( "Record not found" ) ;
150151 const fedCtx = federation . createContext ( c . req . raw , undefined ) ;
151- if ( poll . post . account . owner == null ) {
152+ const posts = poll . posts . filter ( ( p ) => p . sharingId == null ) ;
153+ const post = maxBy ( posts , ( p ) => + ( p . published ?? p . updated ) ) ! ;
154+ if ( post . account . owner == null ) {
152155 for ( const choice of choices ) {
153156 await fedCtx . sendActivity (
154157 owner ,
155158 [
156159 {
157- id : new URL ( poll . post . account . iri ) ,
158- inboxId : new URL ( poll . post . account . inboxUrl ) ,
160+ id : new URL ( post . account . iri ) ,
161+ inboxId : new URL ( post . account . inboxUrl ) ,
159162 } ,
160163 ] ,
161164 new Create ( {
@@ -164,13 +167,13 @@ app.post(
164167 owner . account . iri ,
165168 ) ,
166169 actor : new URL ( owner . account . iri ) ,
167- to : new URL ( poll . post . account . iri ) ,
170+ to : new URL ( post . account . iri ) ,
168171 object : new Note ( {
169172 id : new URL ( `#votes/${ poll . id } /${ choice } ` , owner . account . iri ) ,
170173 name : poll . options [ choice ] . title ,
171174 attribution : new URL ( owner . account . iri ) ,
172- replyTarget : new URL ( poll . post . iri ) ,
173- to : new URL ( poll . post . account . iri ) ,
175+ replyTarget : new URL ( post . iri ) ,
176+ to : new URL ( post . account . iri ) ,
174177 } ) ,
175178 } ) ,
176179 {
@@ -180,7 +183,7 @@ app.post(
180183 }
181184 } else {
182185 await fedCtx . sendActivity (
183- poll . post . account . owner ,
186+ post . account . owner ,
184187 poll . votes . map ( ( v ) => ( {
185188 id : new URL ( v . account . iri ) ,
186189 inboxId : new URL ( v . account . inboxUrl ) ,
@@ -191,7 +194,7 @@ app.post(
191194 sharedInbox : new URL ( v . account . sharedInboxUrl ) ,
192195 } ,
193196 } ) ) ,
194- toUpdate ( { ...poll . post , poll } , fedCtx ) ,
197+ toUpdate ( { ...post , poll } , fedCtx ) ,
195198 { excludeBaseUris : [ new URL ( c . req . url ) ] } ,
196199 ) ;
197200 }
0 commit comments