Publish Discord inbound replies on the post event bus.
CI / test (pull_request) Successful in 6m21s

This commit is contained in:
2026-08-29 11:38:11 -07:00
parent a1b0351048
commit 2b3aac413d
5 changed files with 66 additions and 55 deletions
+15 -39
View File
@@ -7,11 +7,10 @@ import (
"fmt"
"log"
"strings"
"time"
"github.com/bwmarrin/discordgo"
"plumber/internal/mail"
"plumber/internal/events"
"plumber/internal/store"
)
@@ -127,8 +126,9 @@ func (b *Bot) handleInbound(in inboundMessage) {
MessageID: in.ID,
}); err != nil {
log.Printf("discord: save inbound link %s: %v", reply.ID, err)
return
}
b.notifyInboundReply(parent, root, reply, author)
b.publishInbound(reply, root, author)
log.Printf("discord: inbound reply %s -> post %s", in.ID, reply.ID)
}
@@ -184,44 +184,20 @@ func (b *Bot) postAndRoot(ctx context.Context, postID string) (*store.Post, *sto
return post, current, nil
}
func (b *Bot) notifyInboundReply(parent, root, reply *store.Post, author *store.User) {
if parent == nil || root == nil || reply == nil || author == nil || b.mail == nil {
func (b *Bot) publishInbound(reply, root *store.Post, author *store.User) {
if b == nil || b.bus == nil || reply == nil || root == nil || author == nil {
return
}
if _, disabled := b.mail.(mail.Nop); disabled {
return
ev := events.PostEvent{
PostID: reply.ID,
RootID: root.ID,
Body: reply.Body,
AuthorID: author.ID,
AuthorName: author.Name,
AuthorRole: string(author.Role),
}
recipientID := parent.AuthorID
if author.Admin() {
recipientID = root.AuthorID
if reply.ParentID != nil {
ev.ParentID = *reply.ParentID
}
if recipientID == author.ID {
return
}
msg := mail.PostReply{
RootID: root.ID,
RootTitle: root.Title,
ReplyID: reply.ID,
ReplyBody: reply.Body,
ReplyAuthorName: author.Name,
}
go func() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
recipient, err := b.store.UserByID(ctx, recipientID)
if err != nil {
log.Printf("notify reply %s: load recipient: %v", msg.ReplyID, err)
return
}
if recipient == nil || strings.TrimSpace(recipient.Email) == "" {
return
}
msg.ToEmail = recipient.Email
msg.ToName = recipient.Name
if err := b.mail.NotifyPostReply(ctx, msg); err != nil {
log.Printf("notify reply %s: %v", msg.ReplyID, err)
return
}
log.Printf("notify reply %s: accepted", msg.ReplyID)
}()
b.bus.Publish(context.Background(), events.PostCreated{PostEvent: ev})
}