This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"plumber/internal/events"
|
||||
"plumber/internal/store"
|
||||
)
|
||||
|
||||
func (s *Server) publishPostCreated(post, root *store.Post, author *store.User) {
|
||||
s.publishPost(events.PostCreated{PostEvent: s.postEvent(post, root, author)}, root)
|
||||
}
|
||||
|
||||
func (s *Server) publishPostUpdated(post, root *store.Post, author *store.User) {
|
||||
s.publishPost(events.PostUpdated{PostEvent: s.postEvent(post, root, author)}, root)
|
||||
}
|
||||
|
||||
func (s *Server) publishPost(ev any, root *store.Post) {
|
||||
if root != nil && root.PostState == store.PostStateHidden {
|
||||
return
|
||||
}
|
||||
s.cfg.Events.Publish(context.Background(), ev)
|
||||
}
|
||||
|
||||
func (s *Server) postEvent(post, root *store.Post, author *store.User) events.PostEvent {
|
||||
if post == nil {
|
||||
return events.PostEvent{}
|
||||
}
|
||||
rootID := post.ID
|
||||
if root != nil {
|
||||
rootID = root.ID
|
||||
}
|
||||
ev := events.PostEvent{
|
||||
PostID: post.ID,
|
||||
RootID: rootID,
|
||||
Title: post.Title,
|
||||
Body: post.Body,
|
||||
City: post.City,
|
||||
AuthorID: post.AuthorID,
|
||||
AuthorName: post.AuthorName,
|
||||
AuthorRole: string(post.AuthorRole),
|
||||
Permalink: events.Permalink(s.cfg.BaseURL, rootID, post.ID),
|
||||
}
|
||||
if post.ParentID != nil {
|
||||
ev.ParentID = *post.ParentID
|
||||
}
|
||||
if author != nil {
|
||||
if ev.AuthorName == "" {
|
||||
ev.AuthorName = author.Name
|
||||
}
|
||||
if ev.AuthorRole == "" {
|
||||
ev.AuthorRole = string(author.Role)
|
||||
}
|
||||
}
|
||||
if n := len(post.Images); n > 0 {
|
||||
ev.Images = make([]events.Image, 0, n)
|
||||
for _, img := range post.Images {
|
||||
ev.Images = append(ev.Images, events.Image{
|
||||
URL: img.PublicURL,
|
||||
Description: img.Description,
|
||||
})
|
||||
}
|
||||
}
|
||||
return ev
|
||||
}
|
||||
Reference in New Issue
Block a user