Skip to content

Commit 2383291

Browse files
committed
Fix poll voting for shared (boosted) posts
Fix #142
1 parent 9ec4c2d commit 2383291

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ Version 0.5.6
66

77
To be released.
88

9+
- Fixed a bug where voting to a poll which had been shared (boosted) had not
10+
been sent to the correct recipient. [[#142]]
11+
12+
[#142]: https://github.com/fedify-dev/hollo/issues/142
13+
914

1015
Version 0.5.5
1116
-------------

src/api/v1/polls.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Create, Note } from "@fedify/fedify";
22
import { zValidator } from "@hono/zod-validator";
33
import { and, eq, inArray, sql } from "drizzle-orm";
4+
import { maxBy } from "es-toolkit";
45
import { Hono } from "hono";
56
import { z } from "zod";
67
import { 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
}

src/schema.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -489,11 +489,8 @@ export const polls = pgTable("polls", {
489489
export type Poll = typeof polls.$inferSelect;
490490
export type NewPoll = typeof polls.$inferInsert;
491491

492-
export const pollRelations = relations(polls, ({ one, many }) => ({
493-
post: one(posts, {
494-
fields: [polls.id],
495-
references: [posts.pollId],
496-
}),
492+
export const pollRelations = relations(polls, ({ many }) => ({
493+
posts: many(posts),
497494
options: many(pollOptions),
498495
votes: many(pollVotes),
499496
}));

0 commit comments

Comments
 (0)