diff --git a/src/github.com/matrix-org/dendrite/clientapi/writers/membership.go b/src/github.com/matrix-org/dendrite/clientapi/writers/membership.go index 679a7a367..c197036dc 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/writers/membership.go +++ b/src/github.com/matrix-org/dendrite/clientapi/writers/membership.go @@ -67,21 +67,11 @@ func SendMembership( return *reqErr } - localpart, serverName, err := gomatrixserverlib.SplitID('@', stateKey) + profile, err := loadProfile(stateKey, cfg, accountDB) if err != nil { return httputil.LogThenError(req, err) } - var profile *authtypes.Profile - if serverName == cfg.Matrix.ServerName { - profile, err = accountDB.GetProfileByLocalpart(localpart) - if err != nil { - return httputil.LogThenError(req, err) - } - } else { - profile = &authtypes.Profile{} - } - builder := gomatrixserverlib.EventBuilder{ Sender: device.UserID, RoomID: roomID, @@ -125,6 +115,22 @@ func SendMembership( } } +func loadProfile(userID string, cfg config.Dendrite, accountDB *accounts.Database) (*authtypes.Profile, error) { + localpart, serverName, err := gomatrixserverlib.SplitID('@', userID) + if err != nil { + return nil, err + } + + var profile *authtypes.Profile + if serverName == cfg.Matrix.ServerName { + profile, err = accountDB.GetProfileByLocalpart(localpart) + } else { + profile = &authtypes.Profile{} + } + + return profile, err +} + // getMembershipStateKey extracts the target user ID of a membership change. // For "join" and "leave" this will be the ID of the user making the change. // For "ban", "unban", "kick" and "invite" the target user ID will be in the JSON request body.