feat: add inline mentions in comments
This commit is contained in:
@@ -4,12 +4,41 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/prabhupadasays/spsays-bff/internal/model"
|
||||
"github.com/prabhupadasays/spsays-bff/internal/repository"
|
||||
)
|
||||
|
||||
// mentionHandleRe matches @handle tokens. The leading boundary (line start or
|
||||
// a non-handle character that isn't itself "@") keeps it from firing inside
|
||||
// emails (foo@bar) or "@@".
|
||||
var mentionHandleRe = regexp.MustCompile(`(^|[^A-Za-z0-9_@])@([A-Za-z0-9_]{1,30})`)
|
||||
|
||||
// mentionHandles pulls unique @handles out of a comment body (capped, so a
|
||||
// pathological body can't fan out unboundedly).
|
||||
func mentionHandles(body string) []string {
|
||||
matches := mentionHandleRe.FindAllStringSubmatch(body, -1)
|
||||
if matches == nil {
|
||||
return nil
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
out := make([]string, 0, len(matches))
|
||||
for _, m := range matches {
|
||||
key := strings.ToLower(m[2])
|
||||
if seen[key] {
|
||||
continue
|
||||
}
|
||||
seen[key] = true
|
||||
out = append(out, m[2])
|
||||
if len(out) >= 20 {
|
||||
break
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// maxCommentLen caps a comment/reply body.
|
||||
const maxCommentLen = 2000
|
||||
|
||||
@@ -87,6 +116,14 @@ func (s *CommentService) Add(ctx context.Context, viewerID, postID string, paren
|
||||
go s.notif.NotifyPostComment(context.Background(), postID, notifyUserID, viewerID)
|
||||
}
|
||||
}
|
||||
|
||||
// Ping anyone @mentioned in the body — skipping the actor and whoever was
|
||||
// already notified above (post/parent author) so they aren't double-pinged.
|
||||
if s.notif != nil {
|
||||
if handles := mentionHandles(body); len(handles) > 0 {
|
||||
go s.notif.NotifyCommentMentions(context.Background(), postID, viewerID, handles, notifyUserID)
|
||||
}
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user