Skip to content

Commit a4e71c9

Browse files
committed
fix/notification: fix sync issue by getting last comment from author
1 parent 284a234 commit a4e71c9

File tree

5 files changed

+24
-20
lines changed

5 files changed

+24
-20
lines changed

cmd/notifier.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ package cmd
2222

2323
import (
2424
"fmt"
25-
"time"
2625
"log"
2726
"context"
2827
"encoding/json"
@@ -113,8 +112,9 @@ func processContractNotification(msg *redis.Message) {
113112
return
114113
}
115114

115+
// @deprecated
116116
// Add a little sleep to wait for UpdateContractHook as it writes after publishing
117-
time.Sleep(1 * time.Second)
117+
//time.Sleep(1 * time.Second)
118118

119119
// Push notification
120120
if err := graph.PushContractNotifications(notif); err != nil {

db/dql.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -402,20 +402,19 @@ var dqlQueries map[string]string = map[string]string{
402402
{{.user_payload}}
403403
}
404404
}`,
405-
// @debug sub filter comments on username/author...
406405
"getLastComment": `{
407406
all(func: uid({{.tid}})) @normalize {
408407
title: Tension.title
409408
Tension.receiver {
410409
rootnameid: Node.rootnameid
411410
receiverid: Node.nameid
412411
}
413-
Tension.comments(first:1, orderdesc: Post.createdAt) {
412+
Tension.comments(first:1, orderdesc: Post.createdAt) @cascade {
414413
message: Post.message
414+
Post.createdBy @filter(eq(User.username, "{{.username}}"))
415415
}
416416
}
417417
}`,
418-
// @debug sub filter comments on username/author...
419418
"getLastContractComment": `{
420419
all(func: uid({{.cid}})) @normalize {
421420
Contract.tension {
@@ -424,8 +423,9 @@ var dqlQueries map[string]string = map[string]string{
424423
receiverid: Node.nameid
425424
}
426425
}
427-
Contract.comments(first:1, orderdesc: Post.createdAt) {
426+
Contract.comments(first:1, orderdesc: Post.createdAt) @cascade {
428427
message: Post.message
428+
Post.createdBy @filter(eq(User.username, "{{.username}}"))
429429
}
430430
}
431431
}`,

graph/contract_resolver.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,14 @@ func updateContractHook(ctx context.Context, obj interface{}, next graphql.Resol
181181
}
182182
}
183183
if ok {
184+
// Execute query
185+
data, err := next(ctx)
186+
if err != nil { return data, err }
184187
// Notify users by email
185188
if input.Set.Comments != nil && len(input.Set.Comments) > 0 {
186189
PublishContractEvent(model.ContractNotif{Uctx: uctx, Tid: contract.Tension.ID, Contract: contract, ContractEvent: model.NewComment})
187190
}
188-
// Execute query
189-
return next(ctx)
191+
return data, err
190192
} else {
191193
return nil, LogErr("Access denied", fmt.Errorf("You are not authorized to access this ressource."))
192194
}

graph/notifications.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ var ctx context.Context = context.Background()
4747
// Publisher functions (Redis)
4848
//
4949

50-
// Will trigger processTensionNotification in cmd/notifier.go
51-
// and PushEventNotifications
50+
// Will trigger Event notifications in cmd/notifier.go
51+
// PublishTensionEvent -> cmd.processTensionNotification -> PushEventNotifications
5252
func PublishTensionEvent(notif model.EventNotif) error {
5353
payload, _ := json.Marshal(notif)
5454
if err := cache.Publish(ctx, "api-tension-notification", payload).Err(); err != nil {
@@ -59,8 +59,8 @@ func PublishTensionEvent(notif model.EventNotif) error {
5959
return nil
6060
}
6161

62-
// Will trigger processContractNotification in cmd/notifier.go
63-
// and PushContractNotifications
62+
// Will trigger Contract notifications in cmd/notifier.go
63+
// PublishContractEvent -> cmd.processContractNotification -> PushContractNotifications
6464
func PublishContractEvent(notif model.ContractNotif) error {
6565
payload, _ := json.Marshal(notif)
6666
if err := cache.Publish(ctx, "api-contract-notification", payload).Err(); err != nil {
@@ -71,8 +71,8 @@ func PublishContractEvent(notif model.ContractNotif) error {
7171
return nil
7272
}
7373

74-
// Will trigger processNotifNotification in cmd/notifier.go
75-
// and PushNotifNotifications
74+
// Will trigger Notif notifications in cmd/notifier.go
75+
// PublishNotifEvent -> cmd.processNotifNotification -> PushNotifNotifications
7676
func PublishNotifEvent(notif model.NotifNotif) error {
7777
payload, _ := json.Marshal(notif)
7878
if err := cache.Publish(ctx, "api-notif-notification", payload).Err(); err != nil {
@@ -165,7 +165,7 @@ func PushEventNotifications(notif model.EventNotif) error {
165165
}
166166
// +
167167
// Add mentions and **set tension data**
168-
if m, err := db.GetDB().Meta("getLastComment", map[string]string{"tid":notif.Tid}); err != nil {
168+
if m, err := db.GetDB().Meta("getLastComment", map[string]string{"tid":notif.Tid, "username":notif.Uctx.Username}); err != nil {
169169
return err
170170
} else if len(m) > 0 {
171171
notif.Rootnameid = m[0]["rootnameid"].(string)
@@ -293,7 +293,7 @@ func PushContractNotifications(notif model.ContractNotif) error {
293293
}
294294
// +
295295
// Add mentionned and **set tension data**
296-
if m, err := db.GetDB().Meta("getLastContractComment", map[string]string{"cid":notif.Contract.ID}); err != nil {
296+
if m, err := db.GetDB().Meta("getLastContractComment", map[string]string{"cid":notif.Contract.ID, "username":notif.Uctx.Username}); err != nil {
297297
return err
298298
} else if len(m) > 0 {
299299
notif.Rootnameid = m[0]["rootnameid"].(string)

graph/tension_resolver.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,12 @@ func updateTensionHook(ctx context.Context, obj interface{}, next graphql.Resolv
152152
ok, contract, err = TensionEventHook(uctx, ids[0], input.Set.History, blob)
153153
if err != nil { return nil, err }
154154
if ok {
155-
// History and notification Logics --
156-
// In order to notify user on the given event, we need to know their ids to pass and link them
157-
// to the notification (UserEvent edge) function. To do so we first cut the history from the original
158-
// input, and push then the history (see the PushHistory function).
155+
// History and notification Logics
156+
// --
157+
// In order to notify user on the given event, we need to know
158+
// their ids to pass and link them to the user's notifications (UserEvent edge).
159+
// To do so we first cut the history from the original input,
160+
// and push then the history (see the [[PushHistory]] function).
159161
ctx = context.WithValue(ctx, "cut_history", true)
160162
// Execute query
161163
data, err := next(ctx)

0 commit comments

Comments
 (0)