Skip to content

Commit 0955308

Browse files
committed
Add thread ID to read receipts
1 parent 10546ab commit 0955308

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

bridge/bridge.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ type MembershipHandlingPortal interface {
5959

6060
type ReadReceiptHandlingPortal interface {
6161
Portal
62-
HandleMatrixReadReceipt(sender User, eventID id.EventID, receiptTimestamp time.Time)
62+
HandleMatrixReadReceipt(sender User, eventID id.EventID, receipt event.ReadReceipt)
6363
}
6464

6565
type TypingPortal interface {

bridge/matrix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ func (mx *MatrixHandler) HandleReceipt(evt *event.Event) {
588588
dp.ScheduleDisappearing()
589589
}
590590
} else {
591-
rrPortal.HandleMatrixReadReceipt(user, eventID, time.UnixMilli(receipt.Timestamp))
591+
rrPortal.HandleMatrixReadReceipt(user, eventID, receipt)
592592
}
593593
}
594594
}

event/ephemeral.go

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package event
88

99
import (
1010
"encoding/json"
11+
"time"
1112

1213
"maunium.net/go/mautrix/id"
1314
)
@@ -59,12 +60,19 @@ func (ur UserReceipts) Set(userID id.UserID, receipt ReadReceipt) {
5960
ur[userID] = receipt
6061
}
6162

63+
type ThreadID = id.EventID
64+
65+
const ReadReceiptThreadMain ThreadID = "main"
66+
6267
type ReadReceipt struct {
63-
Timestamp int64 `json:"ts"`
68+
Timestamp time.Time
69+
70+
// Thread ID for thread-specific read receipts from MSC3771
71+
ThreadID ThreadID
6472

6573
// Extra contains any unknown fields in the read receipt event.
6674
// Most servers don't allow clients to set them, so this will be empty in most cases.
67-
Extra map[string]interface{} `json:"-"`
75+
Extra map[string]interface{}
6876
}
6977

7078
func (rr *ReadReceipt) UnmarshalJSON(data []byte) error {
@@ -84,15 +92,34 @@ func (rr *ReadReceipt) UnmarshalJSON(data []byte) error {
8492
if err != nil {
8593
return err
8694
}
87-
ts, _ := parsed["ts"].(float64)
95+
threadID, _ := parsed["thread_id"].(string)
96+
ts, tsOK := parsed["ts"].(float64)
97+
delete(parsed, "thread_id")
8898
delete(parsed, "ts")
8999
*rr = ReadReceipt{
90-
Timestamp: int64(ts),
91-
Extra: parsed,
100+
ThreadID: ThreadID(threadID),
101+
Extra: parsed,
102+
}
103+
if tsOK {
104+
rr.Timestamp = time.UnixMilli(int64(ts))
92105
}
93106
return nil
94107
}
95108

109+
func (rr ReadReceipt) MarshalJSON() ([]byte, error) {
110+
data := rr.Extra
111+
if data == nil {
112+
data = make(map[string]interface{})
113+
}
114+
if rr.ThreadID != "" {
115+
data["thread_id"] = rr.ThreadID
116+
}
117+
if !rr.Timestamp.IsZero() {
118+
data["ts"] = rr.Timestamp.UnixMilli()
119+
}
120+
return json.Marshal(data)
121+
}
122+
96123
type Presence string
97124

98125
const (

0 commit comments

Comments
 (0)