onMessage is called in response to a message received on the typing events topic from the client api.
(ctx context.Context, msg *nats.Msg)
| 70 | // onMessage is called in response to a message received on the typing |
| 71 | // events topic from the client api. |
| 72 | func (t *OutputTypingConsumer) onMessage(ctx context.Context, msg *nats.Msg) bool { |
| 73 | // Extract the typing event from msg. |
| 74 | roomID := msg.Header.Get(jetstream.RoomID) |
| 75 | userID := msg.Header.Get(jetstream.UserID) |
| 76 | typing, err := strconv.ParseBool(msg.Header.Get("typing")) |
| 77 | if err != nil { |
| 78 | log.WithError(err).Errorf("EDU output log: typing parse failure") |
| 79 | return true |
| 80 | } |
| 81 | |
| 82 | // only send typing events which originated from us |
| 83 | _, typingServerName, err := gomatrixserverlib.SplitID('@', userID) |
| 84 | if err != nil { |
| 85 | log.WithError(err).WithField("user_id", userID).Error("Failed to extract domain from typing sender") |
| 86 | _ = msg.Ack() |
| 87 | return true |
| 88 | } |
| 89 | if typingServerName != t.ServerName { |
| 90 | return true |
| 91 | } |
| 92 | |
| 93 | joined, err := t.db.GetJoinedHosts(ctx, roomID) |
| 94 | if err != nil { |
| 95 | log.WithError(err).WithField("room_id", roomID).Error("failed to get joined hosts for room") |
| 96 | return false |
| 97 | } |
| 98 | |
| 99 | names := make([]gomatrixserverlib.ServerName, len(joined)) |
| 100 | for i := range joined { |
| 101 | names[i] = joined[i].ServerName |
| 102 | } |
| 103 | |
| 104 | edu := &gomatrixserverlib.EDU{Type: "m.typing"} |
| 105 | if edu.Content, err = json.Marshal(map[string]interface{}{ |
| 106 | "room_id": roomID, |
| 107 | "user_id": userID, |
| 108 | "typing": typing, |
| 109 | }); err != nil { |
| 110 | log.WithError(err).Error("failed to marshal EDU JSON") |
| 111 | return true |
| 112 | } |
| 113 | if err := t.queues.SendEDU(edu, t.ServerName, names); err != nil { |
| 114 | log.WithError(err).Error("failed to send EDU") |
| 115 | return false |
| 116 | } |
| 117 | |
| 118 | return true |
| 119 | } |
nothing calls this directly
no test coverage detected