onMessage is called in response to a message received on the receipt events topic from the client api.
(ctx context.Context, msg *nats.Msg)
| 73 | // onMessage is called in response to a message received on the receipt |
| 74 | // events topic from the client api. |
| 75 | func (t *OutputReceiptConsumer) onMessage(ctx context.Context, msg *nats.Msg) bool { |
| 76 | receipt := syncTypes.OutputReceiptEvent{ |
| 77 | UserID: msg.Header.Get(jetstream.UserID), |
| 78 | RoomID: msg.Header.Get(jetstream.RoomID), |
| 79 | EventID: msg.Header.Get(jetstream.EventID), |
| 80 | Type: msg.Header.Get("type"), |
| 81 | } |
| 82 | |
| 83 | // only send receipt events which originated from us |
| 84 | _, receiptServerName, err := gomatrixserverlib.SplitID('@', receipt.UserID) |
| 85 | if err != nil { |
| 86 | log.WithError(err).WithField("user_id", receipt.UserID).Error("failed to extract domain from receipt sender") |
| 87 | return true |
| 88 | } |
| 89 | if receiptServerName != t.ServerName { |
| 90 | return true |
| 91 | } |
| 92 | |
| 93 | timestamp, err := strconv.ParseUint(msg.Header.Get("timestamp"), 10, 64) |
| 94 | if err != nil { |
| 95 | // If the message was invalid, log it and move on to the next message in the stream |
| 96 | log.WithError(err).Errorf("EDU output log: message parse failure") |
| 97 | sentry.CaptureException(err) |
| 98 | return true |
| 99 | } |
| 100 | |
| 101 | receipt.Timestamp = gomatrixserverlib.Timestamp(timestamp) |
| 102 | |
| 103 | joined, err := t.db.GetJoinedHosts(ctx, receipt.RoomID) |
| 104 | if err != nil { |
| 105 | log.WithError(err).WithField("room_id", receipt.RoomID).Error("failed to get joined hosts for room") |
| 106 | return false |
| 107 | } |
| 108 | |
| 109 | names := make([]gomatrixserverlib.ServerName, len(joined)) |
| 110 | for i := range joined { |
| 111 | names[i] = joined[i].ServerName |
| 112 | } |
| 113 | |
| 114 | content := map[string]fedTypes.FederationReceiptMRead{} |
| 115 | content[receipt.RoomID] = fedTypes.FederationReceiptMRead{ |
| 116 | User: map[string]fedTypes.FederationReceiptData{ |
| 117 | receipt.UserID: { |
| 118 | Data: fedTypes.ReceiptTS{ |
| 119 | TS: receipt.Timestamp, |
| 120 | }, |
| 121 | EventIDs: []string{receipt.EventID}, |
| 122 | }, |
| 123 | }, |
| 124 | } |
| 125 | |
| 126 | edu := &gomatrixserverlib.EDU{ |
| 127 | Type: gomatrixserverlib.MReceipt, |
| 128 | Origin: string(t.ServerName), |
| 129 | } |
| 130 | if edu.Content, err = json.Marshal(content); err != nil { |
| 131 | log.WithError(err).Error("failed to marshal EDU JSON") |
| 132 | return true |
nothing calls this directly
no test coverage detected