Skip to content

Commit

Permalink
update poll results when all votes are deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Dec 21, 2024
1 parent 9bafcf9 commit d6287ea
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions outbox/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type pollResult struct {
}

func (p *Poller) Run(ctx context.Context) error {
rows, err := p.DB.QueryContext(ctx, `select poll, option, count(*) from (select polls.id as poll, votes.object->>'$.name' as option, votes.author as voter from notes polls join notes votes on votes.object->>'$.inReplyTo' = polls.id where polls.object->>'$.type' = 'Question' and polls.id like $1 and polls.object->>'$.closed' is null and votes.object->>'$.name' is not null group by poll, option, voter) group by poll, option`, fmt.Sprintf("https://%s/%%", p.Domain))
rows, err := p.DB.QueryContext(ctx, `select poll, option, count(*) from (select polls.id as poll, votes.object->>'$.name' as option, votes.author as voter from notes polls left join notes votes on votes.object->>'$.inReplyTo' = polls.id where polls.object->>'$.type' = 'Question' and polls.id like $1 and polls.object->>'$.closed' is null and (votes.object->>'$.name' is not null or votes.id is null) group by poll, option, voter) group by poll, option`, fmt.Sprintf("https://%s/%%", p.Domain))
if err != nil {
return err
}
Expand All @@ -47,15 +47,18 @@ func (p *Poller) Run(ctx context.Context) error {
polls := map[string]*ap.Object{}

for rows.Next() {
var pollID, option string
var pollID string
var option sql.NullString
var count int64
if err := rows.Scan(&pollID, &option, &count); err != nil {
slog.Warn("Failed to scan poll result", "error", err)
continue
}

if _, ok := polls[pollID]; ok {
votes[pollResult{PollID: pollID, Option: option}] = count
if option.Valid {
votes[pollResult{PollID: pollID, Option: option.String}] = count
}
continue
}

Expand All @@ -66,7 +69,9 @@ func (p *Poller) Run(ctx context.Context) error {
}

polls[pollID] = &obj
votes[pollResult{PollID: pollID, Option: option}] = count
if option.Valid {
votes[pollResult{PollID: pollID, Option: option.String}] = count
}
}
rows.Close()

Expand Down

0 comments on commit d6287ea

Please sign in to comment.