Skip to content

Commit 8949222

Browse files
committed
fix: enhance post update response with thread and actor information
Also implement FindByPost method to retrieve threads by post ID
1 parent 6cf015a commit 8949222

File tree

2 files changed

+72
-6
lines changed

2 files changed

+72
-6
lines changed

backend/src/mongodb/thread.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,27 @@ func (t *ThreadsType) UpdateThread(threadID primitive.ObjectID, data UpdateThrea
240240
return &updatedThread, nil
241241

242242
}
243+
244+
// FindByPost finds the thread that contains the given post either as entry or as a comment
245+
func (t *ThreadsType) FindByPost(postID primitive.ObjectID) (*models.Thread, error) {
246+
ctx := context.Background()
247+
248+
var thread models.Thread
249+
250+
filter := bson.M{
251+
"$or": bson.A{
252+
bson.M{"entry": postID},
253+
bson.M{"comments": postID},
254+
},
255+
}
256+
257+
err := t.Collection.FindOne(ctx, filter).Decode(&thread)
258+
if err != nil {
259+
if err == mongo.ErrNoDocuments {
260+
return nil, nil
261+
}
262+
return nil, err
263+
}
264+
265+
return &thread, nil
266+
}

backend/src/router/post.go

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,53 @@ func updatePost(w http.ResponseWriter, r *http.Request) {
6565
return
6666
}
6767

68-
json.NewEncoder(w).Encode(updatedPost)
68+
// attempt to enrich response with thread and actor (speaker/company)
69+
var threadID *primitive.ObjectID
70+
var speakerID *primitive.ObjectID
71+
var companyID *primitive.ObjectID
72+
73+
if thread, terr := mongodb.Threads.FindByPost(updatedPost.ID); terr == nil && thread != nil {
74+
threadID = &thread.ID
75+
76+
// try to find owning speaker/company for this thread
77+
if sp, _ := mongodb.Speakers.FindThread(thread.ID); sp != nil {
78+
sid := sp.ID
79+
speakerID = &sid
80+
}
81+
if co, _ := mongodb.Companies.FindThread(thread.ID); co != nil && speakerID == nil {
82+
cid := co.ID
83+
companyID = &cid
84+
}
85+
}
86+
87+
// build response payload keeping backward compatibility intent minimal
88+
resp := map[string]interface{}{
89+
"post": updatedPost,
90+
}
91+
if threadID != nil {
92+
resp["thread"] = threadID.Hex()
93+
}
94+
if speakerID != nil {
95+
resp["speaker"] = speakerID.Hex()
96+
}
97+
if companyID != nil {
98+
resp["company"] = companyID.Hex()
99+
}
100+
101+
json.NewEncoder(w).Encode(resp)
102+
103+
// notify with enriched context so notifications have thread/actor
104+
notif := mongodb.CreateNotificationData{
105+
Kind: models.NotificationKindUpdated,
106+
Post: &updatedPost.ID,
107+
Thread: threadID,
108+
}
109+
if speakerID != nil {
110+
notif.Speaker = speakerID
111+
}
112+
if companyID != nil {
113+
notif.Company = companyID
114+
}
69115

70-
// notify
71-
mongodb.Notifications.Notify(credentials.ID, mongodb.CreateNotificationData{
72-
Kind: models.NotificationKindUpdated,
73-
Post: &updatedPost.ID,
74-
})
116+
mongodb.Notifications.Notify(credentials.ID, notif)
75117
}

0 commit comments

Comments
 (0)