diff --git a/appservice/http.go b/appservice/http.go index 1ce7c31b..55f87c5f 100644 --- a/appservice/http.go +++ b/appservice/http.go @@ -1,4 +1,4 @@ -// Copyright (c) 2021 Tulir Asokan +// Copyright (c) 2022 Tulir Asokan // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -146,6 +146,11 @@ func (as *AppService) handleTransaction(id string, txn *Transaction) { } else if txn.MSC2409EphemeralEvents != nil { as.handleEvents(txn.MSC2409EphemeralEvents, event.EphemeralEventType) } + if txn.ToDeviceEvents != nil { + as.handleEvents(txn.ToDeviceEvents, event.ToDeviceEventType) + } else if txn.MSC2409ToDeviceEvents != nil { + as.handleEvents(txn.MSC2409ToDeviceEvents, event.ToDeviceEventType) + } } as.handleEvents(txn.Events, event.UnknownEventType) if txn.DeviceLists != nil { @@ -186,9 +191,7 @@ func (as *AppService) handleDeviceLists(dl *mautrix.DeviceLists) { func (as *AppService) handleEvents(evts []*event.Event, defaultTypeClass event.TypeClass) { for _, evt := range evts { evt.Mautrix.ReceivedAt = time.Now() - if len(evt.ToUserID) > 0 { - evt.Type.Class = event.ToDeviceEventType - } else if defaultTypeClass != event.UnknownEventType { + if defaultTypeClass != event.UnknownEventType { evt.Type.Class = defaultTypeClass } else if evt.StateKey != nil { evt.Type.Class = event.StateEventType diff --git a/appservice/protocol.go b/appservice/protocol.go index 93ca9a1e..137491a7 100644 --- a/appservice/protocol.go +++ b/appservice/protocol.go @@ -1,4 +1,4 @@ -// Copyright (c) 2021 Tulir Asokan +// Copyright (c) 2022 Tulir Asokan // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -25,11 +25,13 @@ type OTKCountMap = map[id.UserID]map[id.DeviceID]mautrix.OTKCount type Transaction struct { Events []*event.Event `json:"events"` EphemeralEvents []*event.Event `json:"ephemeral,omitempty"` + ToDeviceEvents []*event.Event `json:"to_device,omitempty"` DeviceLists *mautrix.DeviceLists `json:"device_lists,omitempty"` DeviceOTKCount OTKCountMap `json:"device_one_time_keys_count,omitempty"` MSC2409EphemeralEvents []*event.Event `json:"de.sorunome.msc2409.ephemeral,omitempty"` + MSC2409ToDeviceEvents []*event.Event `json:"de.sorunome.msc2409.to_device,omitempty"` MSC3202DeviceLists *mautrix.DeviceLists `json:"org.matrix.msc3202.device_lists,omitempty"` MSC3202DeviceOTKCount OTKCountMap `json:"org.matrix.msc3202.device_one_time_keys_count,omitempty"` } @@ -37,6 +39,7 @@ type Transaction struct { func (txn *Transaction) MarshalZerologObject(ctx *zerolog.Event) { ctx.Int("pdu", len(txn.Events)) ctx.Int("edu", len(txn.EphemeralEvents)) + ctx.Int("to_device", len(txn.ToDeviceEvents)) if len(txn.DeviceOTKCount) > 0 { ctx.Int("otk_count_users", len(txn.DeviceOTKCount)) } @@ -55,6 +58,11 @@ func (txn *Transaction) ContentString() string { } else if len(txn.MSC2409EphemeralEvents) > 0 { parts = append(parts, fmt.Sprintf("%d EDUs (unstable)", len(txn.MSC2409EphemeralEvents))) } + if len(txn.ToDeviceEvents) > 0 { + parts = append(parts, fmt.Sprintf("%d to-device events", len(txn.ToDeviceEvents))) + } else if len(txn.MSC2409ToDeviceEvents) > 0 { + parts = append(parts, fmt.Sprintf("%d to-device events (unstable)", len(txn.MSC2409ToDeviceEvents))) + } if len(txn.DeviceOTKCount) > 0 { parts = append(parts, fmt.Sprintf("OTK counts for %d users", len(txn.DeviceOTKCount))) } else if len(txn.MSC3202DeviceOTKCount) > 0 {