MCPcopy Create free account
hub / github.com/daodst/chat / onMessage

Method onMessage

userapi/consumers/syncapi_readupdate.go:67–136  ·  view source on GitHub ↗
(ctx context.Context, msg *nats.Msg)

Source from the content-addressed store, hash-verified

65}
66
67func (s *OutputReadUpdateConsumer) onMessage(ctx context.Context, msg *nats.Msg) bool {
68 var read types.ReadUpdate
69 if err := json.Unmarshal(msg.Data, &read); err != nil {
70 log.WithError(err).Error("userapi clientapi consumer: message parse failure")
71 return true
72 }
73 if read.FullyRead == 0 && read.Read == 0 {
74 return true
75 }
76
77 userID := string(msg.Header.Get(jetstream.UserID))
78 roomID := string(msg.Header.Get(jetstream.RoomID))
79
80 localpart, domain, err := gomatrixserverlib.SplitID('@', userID)
81 if err != nil {
82 log.WithError(err).Error("userapi clientapi consumer: SplitID failure")
83 return true
84 }
85 if domain != s.ServerName {
86 log.Error("userapi clientapi consumer: not a local user")
87 return true
88 }
89
90 log := log.WithFields(log.Fields{
91 "room_id": roomID,
92 "user_id": userID,
93 })
94 log.Tracef("Received read update from sync API: %#v", read)
95
96 if read.Read > 0 {
97 updated, err := s.db.SetNotificationsRead(ctx, localpart, roomID, int64(read.Read), true)
98 if err != nil {
99 log.WithError(err).Error("userapi EDU consumer")
100 return false
101 }
102
103 if updated {
104 if err = s.syncProducer.GetAndSendNotificationData(ctx, userID, roomID); err != nil {
105 log.WithError(err).Error("userapi EDU consumer: GetAndSendNotificationData failed")
106 return false
107 }
108 if err = util.NotifyUserCountsAsync(ctx, s.pgClient, localpart, s.db); err != nil {
109 log.WithError(err).Error("userapi EDU consumer: NotifyUserCounts failed")
110 return false
111 }
112 }
113 }
114
115 if read.FullyRead > 0 {
116 deleted, err := s.db.DeleteNotificationsUpTo(ctx, localpart, roomID, int64(read.FullyRead))
117 if err != nil {
118 log.WithError(err).Errorf("userapi clientapi consumer: DeleteNotificationsUpTo failed")
119 return false
120 }
121
122 if deleted {
123 if err := util.NotifyUserCountsAsync(ctx, s.pgClient, localpart, s.db); err != nil {
124 log.WithError(err).Error("userapi clientapi consumer: NotifyUserCounts failed")

Callers

nothing calls this directly

Calls 3

GetMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected