From 1da0a21923e9e526773c65d18c1b51eb950580de Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Fri, 26 Feb 2021 16:18:02 +0000 Subject: [PATCH] Changes that I made a long time ago --- clientapi/routing/getevent.go | 27 ++++++++++++++++++++++----- userapi/api/api.go | 3 +++ userapi/internal/api.go | 3 ++- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/clientapi/routing/getevent.go b/clientapi/routing/getevent.go index 29340cc04..76e06dfd4 100644 --- a/clientapi/routing/getevent.go +++ b/clientapi/routing/getevent.go @@ -23,6 +23,7 @@ import ( userapi "github.com/matrix-org/dendrite/userapi/api" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" + log "github.com/sirupsen/logrus" ) type getEventRequest struct { @@ -79,10 +80,11 @@ func GetEvent( stateReq := api.QueryStateAfterEventsRequest{ RoomID: r.requestedEvent.RoomID(), PrevEventIDs: r.requestedEvent.PrevEventIDs(), - StateToFetch: []gomatrixserverlib.StateKeyTuple{{ - EventType: gomatrixserverlib.MRoomMember, - StateKey: device.UserID, - }}, + // XXX: Appservices require you to fetch the lot + // StateToFetch: []gomatrixserverlib.StateKeyTuple{{ + // EventType: gomatrixserverlib.MRoomMember, + // StateKey: device.UserID, + // }}, } var stateResp api.QueryStateAfterEventsResponse if err := rsAPI.QueryStateAfterEvents(req.Context(), &stateReq, &stateResp); err != nil { @@ -102,9 +104,24 @@ func GetEvent( JSON: jsonerror.NotFound("The event was not found or you do not have permission to read this event"), } } + var appService *config.ApplicationService + if device.AppserviceID != "" { + for _, as := range cfg.Derived.ApplicationServices { + if as.ID == device.AppserviceID { + appService = &as + break + } + } + } for _, stateEvent := range stateResp.StateEvents { - if !stateEvent.StateKeyEquals(device.UserID) { + if stateEvent.Type() != gomatrixserverlib.MRoomMember { + continue + } + // Allow appservices to fetch events + if appService != nil && !appService.IsInterestedInUserID(*stateEvent.StateKey()) { + continue + } else if stateEvent.StateKeyEquals(device.UserID) { continue } membership, err := stateEvent.Membership() diff --git a/userapi/api/api.go b/userapi/api/api.go index 809ba0476..45e4e834e 100644 --- a/userapi/api/api.go +++ b/userapi/api/api.go @@ -241,6 +241,9 @@ type Device struct { LastSeenTS int64 LastSeenIP string UserAgent string + // If the device is for an appservice user, + // this is the appservice ID. + AppserviceID string } // Account represents a Matrix account on this home server. diff --git a/userapi/internal/api.go b/userapi/internal/api.go index cf588a40c..a6a2f685e 100644 --- a/userapi/internal/api.go +++ b/userapi/internal/api.go @@ -379,7 +379,8 @@ func (a *UserInternalAPI) queryAppServiceToken(ctx context.Context, token, appSe // Use AS dummy device ID ID: types.AppServiceDeviceID, // AS dummy device has AS's token. - AccessToken: token, + AccessToken: token, + AppserviceID: appService.ID, } localpart, err := userutil.ParseUsernameParam(appServiceUserID, &a.ServerName)