mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-15 19:03:09 -06:00
Merge branch 'master' into state_filtering
This commit is contained in:
commit
a62c6d7c56
|
|
@ -20,13 +20,13 @@ package api
|
|||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
||||
"github.com/matrix-org/dendrite/clientapi/auth/storage/accounts"
|
||||
"github.com/matrix-org/gomatrixserverlib"
|
||||
|
||||
"github.com/matrix-org/dendrite/common"
|
||||
commonHTTP "github.com/matrix-org/dendrite/common/http"
|
||||
opentracing "github.com/opentracing/opentracing-go"
|
||||
)
|
||||
|
|
@ -164,7 +164,7 @@ func RetrieveUserProfile(
|
|||
|
||||
// If no user exists, return
|
||||
if !userResp.UserIDExists {
|
||||
return nil, errors.New("no known profile for given user ID")
|
||||
return nil, common.ErrProfileNoExists
|
||||
}
|
||||
|
||||
// Try to query the user from the local database again
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@
|
|||
|
||||
package authtypes
|
||||
|
||||
// Profile represents the profile for a Matrix account on this home server.
|
||||
// Profile represents the profile for a Matrix account.
|
||||
type Profile struct {
|
||||
Localpart string
|
||||
DisplayName string
|
||||
|
|
|
|||
|
|
@ -230,7 +230,7 @@ func (d *Database) newMembership(
|
|||
}
|
||||
|
||||
// Only "join" membership events can be considered as new memberships
|
||||
if membership == "join" {
|
||||
if membership == gomatrixserverlib.Join {
|
||||
if err := d.saveMembership(ctx, txn, localpart, roomID, eventID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -55,10 +55,6 @@ const (
|
|||
presetPublicChat = "public_chat"
|
||||
)
|
||||
|
||||
const (
|
||||
joinRulePublic = "public"
|
||||
joinRuleInvite = "invite"
|
||||
)
|
||||
const (
|
||||
historyVisibilityShared = "shared"
|
||||
// TODO: These should be implemented once history visibility is implemented
|
||||
|
|
@ -201,7 +197,7 @@ func createRoom(
|
|||
}
|
||||
|
||||
membershipContent := common.MemberContent{
|
||||
Membership: "join",
|
||||
Membership: gomatrixserverlib.Join,
|
||||
DisplayName: profile.DisplayName,
|
||||
AvatarURL: profile.AvatarURL,
|
||||
}
|
||||
|
|
@ -209,19 +205,19 @@ func createRoom(
|
|||
var joinRules, historyVisibility string
|
||||
switch r.Preset {
|
||||
case presetPrivateChat:
|
||||
joinRules = joinRuleInvite
|
||||
joinRules = gomatrixserverlib.Invite
|
||||
historyVisibility = historyVisibilityShared
|
||||
case presetTrustedPrivateChat:
|
||||
joinRules = joinRuleInvite
|
||||
joinRules = gomatrixserverlib.Invite
|
||||
historyVisibility = historyVisibilityShared
|
||||
// TODO If trusted_private_chat, all invitees are given the same power level as the room creator.
|
||||
case presetPublicChat:
|
||||
joinRules = joinRulePublic
|
||||
joinRules = gomatrixserverlib.Public
|
||||
historyVisibility = historyVisibilityShared
|
||||
default:
|
||||
// Default room rules, r.Preset was previously checked for valid values so
|
||||
// only a request with no preset should end up here.
|
||||
joinRules = joinRuleInvite
|
||||
joinRules = gomatrixserverlib.Invite
|
||||
historyVisibility = historyVisibilityShared
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -70,7 +70,7 @@ func JoinRoomByIDOrAlias(
|
|||
return httputil.LogThenError(req, err)
|
||||
}
|
||||
|
||||
content["membership"] = "join"
|
||||
content["membership"] = gomatrixserverlib.Join
|
||||
content["displayname"] = profile.DisplayName
|
||||
content["avatar_url"] = profile.AvatarURL
|
||||
|
||||
|
|
|
|||
|
|
@ -102,7 +102,7 @@ func SendMembership(
|
|||
var returnData interface{} = struct{}{}
|
||||
|
||||
// The join membership requires the room id to be sent in the response
|
||||
if membership == "join" {
|
||||
if membership == gomatrixserverlib.Join {
|
||||
returnData = struct {
|
||||
RoomID string `json:"room_id"`
|
||||
}{roomID}
|
||||
|
|
@ -141,7 +141,7 @@ func buildMembershipEvent(
|
|||
|
||||
// "unban" or "kick" isn't a valid membership value, change it to "leave"
|
||||
if membership == "unban" || membership == "kick" {
|
||||
membership = "leave"
|
||||
membership = gomatrixserverlib.Leave
|
||||
}
|
||||
|
||||
content := common.MemberContent{
|
||||
|
|
@ -192,7 +192,7 @@ func loadProfile(
|
|||
func getMembershipStateKey(
|
||||
body threepid.MembershipRequest, device *authtypes.Device, membership string,
|
||||
) (stateKey string, reason string, err error) {
|
||||
if membership == "ban" || membership == "unban" || membership == "kick" || membership == "invite" {
|
||||
if membership == gomatrixserverlib.Ban || membership == "unban" || membership == "kick" || membership == gomatrixserverlib.Invite {
|
||||
// If we're in this case, the state key is contained in the request body,
|
||||
// possibly along with a reason (for "kick" and "ban") so we need to parse
|
||||
// it
|
||||
|
|
|
|||
|
|
@ -30,43 +30,61 @@ import (
|
|||
"github.com/matrix-org/dendrite/roomserver/api"
|
||||
"github.com/matrix-org/gomatrixserverlib"
|
||||
|
||||
"github.com/matrix-org/gomatrix"
|
||||
"github.com/matrix-org/util"
|
||||
)
|
||||
|
||||
// GetProfile implements GET /profile/{userID}
|
||||
func GetProfile(
|
||||
req *http.Request, accountDB *accounts.Database, userID string, asAPI appserviceAPI.AppServiceQueryAPI,
|
||||
req *http.Request, accountDB *accounts.Database, cfg *config.Dendrite,
|
||||
userID string,
|
||||
asAPI appserviceAPI.AppServiceQueryAPI,
|
||||
federation *gomatrixserverlib.FederationClient,
|
||||
) util.JSONResponse {
|
||||
profile, err := appserviceAPI.RetrieveUserProfile(req.Context(), userID, asAPI, accountDB)
|
||||
profile, err := getProfile(req.Context(), accountDB, cfg, userID, asAPI, federation)
|
||||
if err != nil {
|
||||
if err == common.ErrProfileNoExists {
|
||||
return util.JSONResponse{
|
||||
Code: http.StatusNotFound,
|
||||
JSON: jsonerror.NotFound("The user does not exist or does not have a profile"),
|
||||
}
|
||||
}
|
||||
|
||||
return httputil.LogThenError(req, err)
|
||||
}
|
||||
|
||||
res := common.ProfileResponse{
|
||||
AvatarURL: profile.AvatarURL,
|
||||
DisplayName: profile.DisplayName,
|
||||
}
|
||||
return util.JSONResponse{
|
||||
Code: http.StatusOK,
|
||||
JSON: res,
|
||||
JSON: common.ProfileResponse{
|
||||
AvatarURL: profile.AvatarURL,
|
||||
DisplayName: profile.DisplayName,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// GetAvatarURL implements GET /profile/{userID}/avatar_url
|
||||
func GetAvatarURL(
|
||||
req *http.Request, accountDB *accounts.Database, userID string, asAPI appserviceAPI.AppServiceQueryAPI,
|
||||
req *http.Request, accountDB *accounts.Database, cfg *config.Dendrite,
|
||||
userID string, asAPI appserviceAPI.AppServiceQueryAPI,
|
||||
federation *gomatrixserverlib.FederationClient,
|
||||
) util.JSONResponse {
|
||||
profile, err := appserviceAPI.RetrieveUserProfile(req.Context(), userID, asAPI, accountDB)
|
||||
profile, err := getProfile(req.Context(), accountDB, cfg, userID, asAPI, federation)
|
||||
if err != nil {
|
||||
if err == common.ErrProfileNoExists {
|
||||
return util.JSONResponse{
|
||||
Code: http.StatusNotFound,
|
||||
JSON: jsonerror.NotFound("The user does not exist or does not have a profile"),
|
||||
}
|
||||
}
|
||||
|
||||
return httputil.LogThenError(req, err)
|
||||
}
|
||||
|
||||
res := common.AvatarURL{
|
||||
AvatarURL: profile.AvatarURL,
|
||||
}
|
||||
return util.JSONResponse{
|
||||
Code: http.StatusOK,
|
||||
JSON: res,
|
||||
JSON: common.AvatarURL{
|
||||
AvatarURL: profile.AvatarURL,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -152,18 +170,27 @@ func SetAvatarURL(
|
|||
|
||||
// GetDisplayName implements GET /profile/{userID}/displayname
|
||||
func GetDisplayName(
|
||||
req *http.Request, accountDB *accounts.Database, userID string, asAPI appserviceAPI.AppServiceQueryAPI,
|
||||
req *http.Request, accountDB *accounts.Database, cfg *config.Dendrite,
|
||||
userID string, asAPI appserviceAPI.AppServiceQueryAPI,
|
||||
federation *gomatrixserverlib.FederationClient,
|
||||
) util.JSONResponse {
|
||||
profile, err := appserviceAPI.RetrieveUserProfile(req.Context(), userID, asAPI, accountDB)
|
||||
profile, err := getProfile(req.Context(), accountDB, cfg, userID, asAPI, federation)
|
||||
if err != nil {
|
||||
if err == common.ErrProfileNoExists {
|
||||
return util.JSONResponse{
|
||||
Code: http.StatusNotFound,
|
||||
JSON: jsonerror.NotFound("The user does not exist or does not have a profile"),
|
||||
}
|
||||
}
|
||||
|
||||
return httputil.LogThenError(req, err)
|
||||
}
|
||||
res := common.DisplayName{
|
||||
DisplayName: profile.DisplayName,
|
||||
}
|
||||
|
||||
return util.JSONResponse{
|
||||
Code: http.StatusOK,
|
||||
JSON: res,
|
||||
JSON: common.DisplayName{
|
||||
DisplayName: profile.DisplayName,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -247,6 +274,48 @@ func SetDisplayName(
|
|||
}
|
||||
}
|
||||
|
||||
// getProfile gets the full profile of a user by querying the database or a
|
||||
// remote homeserver.
|
||||
// Returns an error when something goes wrong or specifically
|
||||
// common.ErrProfileNoExists when the profile doesn't exist.
|
||||
func getProfile(
|
||||
ctx context.Context, accountDB *accounts.Database, cfg *config.Dendrite,
|
||||
userID string,
|
||||
asAPI appserviceAPI.AppServiceQueryAPI,
|
||||
federation *gomatrixserverlib.FederationClient,
|
||||
) (*authtypes.Profile, error) {
|
||||
localpart, domain, err := gomatrixserverlib.SplitID('@', userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if domain != cfg.Matrix.ServerName {
|
||||
profile, fedErr := federation.LookupProfile(ctx, domain, userID, "")
|
||||
if fedErr != nil {
|
||||
if x, ok := fedErr.(gomatrix.HTTPError); ok {
|
||||
if x.Code == http.StatusNotFound {
|
||||
return nil, common.ErrProfileNoExists
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fedErr
|
||||
}
|
||||
|
||||
return &authtypes.Profile{
|
||||
Localpart: localpart,
|
||||
DisplayName: profile.DisplayName,
|
||||
AvatarURL: profile.AvatarURL,
|
||||
}, nil
|
||||
}
|
||||
|
||||
profile, err := appserviceAPI.RetrieveUserProfile(ctx, userID, asAPI, accountDB)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return profile, nil
|
||||
}
|
||||
|
||||
func buildMembershipEvents(
|
||||
ctx context.Context,
|
||||
memberships []authtypes.Membership,
|
||||
|
|
@ -264,7 +333,7 @@ func buildMembershipEvents(
|
|||
}
|
||||
|
||||
content := common.MemberContent{
|
||||
Membership: "join",
|
||||
Membership: gomatrixserverlib.Join,
|
||||
}
|
||||
|
||||
content.DisplayName = newProfile.DisplayName
|
||||
|
|
|
|||
|
|
@ -93,7 +93,7 @@ func Setup(
|
|||
}),
|
||||
).Methods(http.MethodPost, http.MethodOptions)
|
||||
r0mux.Handle("/join/{roomIDOrAlias}",
|
||||
common.MakeAuthAPI("join", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
|
||||
common.MakeAuthAPI(gomatrixserverlib.Join, authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
|
||||
vars, err := common.URLDecodeMapValues(mux.Vars(req))
|
||||
if err != nil {
|
||||
return util.ErrorResponse(err)
|
||||
|
|
@ -283,7 +283,7 @@ func Setup(
|
|||
if err != nil {
|
||||
return util.ErrorResponse(err)
|
||||
}
|
||||
return GetProfile(req, accountDB, vars["userID"], asAPI)
|
||||
return GetProfile(req, accountDB, &cfg, vars["userID"], asAPI, federation)
|
||||
}),
|
||||
).Methods(http.MethodGet, http.MethodOptions)
|
||||
|
||||
|
|
@ -293,7 +293,7 @@ func Setup(
|
|||
if err != nil {
|
||||
return util.ErrorResponse(err)
|
||||
}
|
||||
return GetAvatarURL(req, accountDB, vars["userID"], asAPI)
|
||||
return GetAvatarURL(req, accountDB, &cfg, vars["userID"], asAPI, federation)
|
||||
}),
|
||||
).Methods(http.MethodGet, http.MethodOptions)
|
||||
|
||||
|
|
@ -315,7 +315,7 @@ func Setup(
|
|||
if err != nil {
|
||||
return util.ErrorResponse(err)
|
||||
}
|
||||
return GetDisplayName(req, accountDB, vars["userID"], asAPI)
|
||||
return GetDisplayName(req, accountDB, &cfg, vars["userID"], asAPI, federation)
|
||||
}),
|
||||
).Methods(http.MethodGet, http.MethodOptions)
|
||||
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ func SendEvent(
|
|||
) util.JSONResponse {
|
||||
if txnID != nil {
|
||||
// Try to fetch response from transactionsCache
|
||||
if res, ok := txnCache.FetchTransaction(*txnID); ok {
|
||||
if res, ok := txnCache.FetchTransaction(device.AccessToken, *txnID); ok {
|
||||
return *res
|
||||
}
|
||||
}
|
||||
|
|
@ -83,7 +83,7 @@ func SendEvent(
|
|||
}
|
||||
// Add response to transactionsCache
|
||||
if txnID != nil {
|
||||
txnCache.AddTransaction(*txnID, &res)
|
||||
txnCache.AddTransaction(device.AccessToken, *txnID, &res)
|
||||
}
|
||||
|
||||
return res
|
||||
|
|
|
|||
|
|
@ -91,7 +91,7 @@ func CheckAndProcessInvite(
|
|||
producer *producers.RoomserverProducer, membership string, roomID string,
|
||||
evTime time.Time,
|
||||
) (inviteStoredOnIDServer bool, err error) {
|
||||
if membership != "invite" || (body.Address == "" && body.IDServer == "" && body.Medium == "") {
|
||||
if membership != gomatrixserverlib.Invite || (body.Address == "" && body.IDServer == "" && body.Medium == "") {
|
||||
// If none of the 3PID-specific fields are supplied, it's a standard invite
|
||||
// so return nil for it to be processed as such
|
||||
return
|
||||
|
|
|
|||
|
|
@ -86,7 +86,7 @@ func main() {
|
|||
// Build a m.room.member event.
|
||||
b.Type = "m.room.member"
|
||||
b.StateKey = userID
|
||||
b.SetContent(map[string]string{"membership": "join"}) // nolint: errcheck
|
||||
b.SetContent(map[string]string{"membership": gomatrixserverlib.Join}) // nolint: errcheck
|
||||
b.AuthEvents = []gomatrixserverlib.EventReference{create}
|
||||
member := buildAndOutput()
|
||||
|
||||
|
|
|
|||
|
|
@ -22,7 +22,14 @@ import (
|
|||
// DefaultCleanupPeriod represents the default time duration after which cacheCleanService runs.
|
||||
const DefaultCleanupPeriod time.Duration = 30 * time.Minute
|
||||
|
||||
type txnsMap map[string]*util.JSONResponse
|
||||
type txnsMap map[CacheKey]*util.JSONResponse
|
||||
|
||||
// CacheKey is the type for the key in a transactions cache.
|
||||
// This is needed because the spec requires transaction IDs to have a per-access token scope.
|
||||
type CacheKey struct {
|
||||
AccessToken string
|
||||
TxnID string
|
||||
}
|
||||
|
||||
// Cache represents a temporary store for response entries.
|
||||
// Entries are evicted after a certain period, defined by cleanupPeriod.
|
||||
|
|
@ -50,14 +57,14 @@ func NewWithCleanupPeriod(cleanupPeriod time.Duration) *Cache {
|
|||
return &t
|
||||
}
|
||||
|
||||
// FetchTransaction looks up an entry for txnID in Cache.
|
||||
// FetchTransaction looks up an entry for the (accessToken, txnID) tuple in Cache.
|
||||
// Looks in both the txnMaps.
|
||||
// Returns (JSON response, true) if txnID is found, else the returned bool is false.
|
||||
func (t *Cache) FetchTransaction(txnID string) (*util.JSONResponse, bool) {
|
||||
func (t *Cache) FetchTransaction(accessToken, txnID string) (*util.JSONResponse, bool) {
|
||||
t.RLock()
|
||||
defer t.RUnlock()
|
||||
for _, txns := range t.txnsMaps {
|
||||
res, ok := txns[txnID]
|
||||
res, ok := txns[CacheKey{accessToken, txnID}]
|
||||
if ok {
|
||||
return res, true
|
||||
}
|
||||
|
|
@ -65,13 +72,13 @@ func (t *Cache) FetchTransaction(txnID string) (*util.JSONResponse, bool) {
|
|||
return nil, false
|
||||
}
|
||||
|
||||
// AddTransaction adds an entry for txnID in Cache for later access.
|
||||
// AddTransaction adds an entry for the (accessToken, txnID) tuple in Cache.
|
||||
// Adds to the front txnMap.
|
||||
func (t *Cache) AddTransaction(txnID string, res *util.JSONResponse) {
|
||||
func (t *Cache) AddTransaction(accessToken, txnID string, res *util.JSONResponse) {
|
||||
t.Lock()
|
||||
defer t.Unlock()
|
||||
|
||||
t.txnsMaps[0][txnID] = res
|
||||
t.txnsMaps[0][CacheKey{accessToken, txnID}] = res
|
||||
}
|
||||
|
||||
// cacheCleanService is responsible for cleaning up entries after cleanupPeriod.
|
||||
|
|
|
|||
|
|
@ -24,27 +24,54 @@ type fakeType struct {
|
|||
}
|
||||
|
||||
var (
|
||||
fakeTxnID = "aRandomTxnID"
|
||||
fakeResponse = &util.JSONResponse{Code: http.StatusOK, JSON: fakeType{ID: "0"}}
|
||||
fakeAccessToken = "aRandomAccessToken"
|
||||
fakeAccessToken2 = "anotherRandomAccessToken"
|
||||
fakeTxnID = "aRandomTxnID"
|
||||
fakeResponse = &util.JSONResponse{
|
||||
Code: http.StatusOK, JSON: fakeType{ID: "0"},
|
||||
}
|
||||
fakeResponse2 = &util.JSONResponse{
|
||||
Code: http.StatusOK, JSON: fakeType{ID: "1"},
|
||||
}
|
||||
)
|
||||
|
||||
// TestCache creates a New Cache and tests AddTransaction & FetchTransaction
|
||||
func TestCache(t *testing.T) {
|
||||
fakeTxnCache := New()
|
||||
fakeTxnCache.AddTransaction(fakeTxnID, fakeResponse)
|
||||
fakeTxnCache.AddTransaction(fakeAccessToken, fakeTxnID, fakeResponse)
|
||||
|
||||
// Add entries for noise.
|
||||
for i := 1; i <= 100; i++ {
|
||||
fakeTxnCache.AddTransaction(
|
||||
fakeAccessToken,
|
||||
fakeTxnID+string(i),
|
||||
&util.JSONResponse{Code: http.StatusOK, JSON: fakeType{ID: string(i)}},
|
||||
)
|
||||
}
|
||||
|
||||
testResponse, ok := fakeTxnCache.FetchTransaction(fakeTxnID)
|
||||
testResponse, ok := fakeTxnCache.FetchTransaction(fakeAccessToken, fakeTxnID)
|
||||
if !ok {
|
||||
t.Error("Failed to retrieve entry for txnID: ", fakeTxnID)
|
||||
} else if testResponse.JSON != fakeResponse.JSON {
|
||||
t.Error("Fetched response incorrect. Expected: ", fakeResponse.JSON, " got: ", testResponse.JSON)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCacheScope ensures transactions with the same transaction ID are not shared
|
||||
// across multiple access tokens.
|
||||
func TestCacheScope(t *testing.T) {
|
||||
cache := New()
|
||||
cache.AddTransaction(fakeAccessToken, fakeTxnID, fakeResponse)
|
||||
cache.AddTransaction(fakeAccessToken2, fakeTxnID, fakeResponse2)
|
||||
|
||||
if res, ok := cache.FetchTransaction(fakeAccessToken, fakeTxnID); !ok {
|
||||
t.Errorf("failed to retrieve entry for (%s, %s)", fakeAccessToken, fakeTxnID)
|
||||
} else if res.JSON != fakeResponse.JSON {
|
||||
t.Errorf("Wrong cache entry for (%s, %s). Expected: %v; got: %v", fakeAccessToken, fakeTxnID, fakeResponse.JSON, res.JSON)
|
||||
}
|
||||
if res, ok := cache.FetchTransaction(fakeAccessToken2, fakeTxnID); !ok {
|
||||
t.Errorf("failed to retrieve entry for (%s, %s)", fakeAccessToken, fakeTxnID)
|
||||
} else if res.JSON != fakeResponse2.JSON {
|
||||
t.Errorf("Wrong cache entry for (%s, %s). Expected: %v; got: %v", fakeAccessToken, fakeTxnID, fakeResponse2.JSON, res.JSON)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,9 +15,14 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// ErrProfileNoExists is returned when trying to lookup a user's profile that
|
||||
// doesn't exist locally.
|
||||
var ErrProfileNoExists = errors.New("no known profile for given user ID")
|
||||
|
||||
// AccountData represents account data sent from the client API server to the
|
||||
// sync API server
|
||||
type AccountData struct {
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ func MakeJoin(
|
|||
Type: "m.room.member",
|
||||
StateKey: &userID,
|
||||
}
|
||||
err = builder.SetContent(map[string]interface{}{"membership": "join"})
|
||||
err = builder.SetContent(map[string]interface{}{"membership": gomatrixserverlib.Join})
|
||||
if err != nil {
|
||||
return httputil.LogThenError(httpReq, err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -56,7 +56,7 @@ func MakeLeave(
|
|||
Type: "m.room.member",
|
||||
StateKey: &userID,
|
||||
}
|
||||
err = builder.SetContent(map[string]interface{}{"membership": "leave"})
|
||||
err = builder.SetContent(map[string]interface{}{"membership": gomatrixserverlib.Leave})
|
||||
if err != nil {
|
||||
return httputil.LogThenError(httpReq, err)
|
||||
}
|
||||
|
|
@ -153,7 +153,7 @@ func SendLeave(
|
|||
mem, err := event.Membership()
|
||||
if err != nil {
|
||||
return httputil.LogThenError(httpReq, err)
|
||||
} else if mem != "leave" {
|
||||
} else if mem != gomatrixserverlib.Leave {
|
||||
return util.JSONResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
JSON: jsonerror.BadJSON("The membership in the event content must be set to leave"),
|
||||
|
|
|
|||
|
|
@ -202,7 +202,7 @@ func createInviteFrom3PIDInvite(
|
|||
content := common.MemberContent{
|
||||
AvatarURL: profile.AvatarURL,
|
||||
DisplayName: profile.DisplayName,
|
||||
Membership: "invite",
|
||||
Membership: gomatrixserverlib.Invite,
|
||||
ThirdPartyInvite: &common.TPInvite{
|
||||
Signed: inv.Signed,
|
||||
},
|
||||
|
|
|
|||
|
|
@ -233,7 +233,7 @@ func joinedHostsFromEvents(evs []gomatrixserverlib.Event) ([]types.JoinedHost, e
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if membership != "join" {
|
||||
if membership != gomatrixserverlib.Join {
|
||||
continue
|
||||
}
|
||||
_, serverName, err := gomatrixserverlib.SplitID('@', *ev.StateKey())
|
||||
|
|
|
|||
2
go.mod
2
go.mod
|
|
@ -24,7 +24,7 @@ require (
|
|||
github.com/lib/pq v0.0.0-20170918175043-23da1db4f16d
|
||||
github.com/matrix-org/dugong v0.0.0-20171220115018-ea0a4690a0d5
|
||||
github.com/matrix-org/gomatrix v0.0.0-20190528120928-7df988a63f26
|
||||
github.com/matrix-org/gomatrixserverlib v0.0.0-20190724145009-a6df10ef35d6
|
||||
github.com/matrix-org/gomatrixserverlib v0.0.0-20190805173246-3a2199d5ecd6
|
||||
github.com/matrix-org/naffka v0.0.0-20171115094957-662bfd0841d0
|
||||
github.com/matrix-org/util v0.0.0-20171127121716-2e2df66af2f5
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.1
|
||||
|
|
|
|||
2
go.sum
2
go.sum
|
|
@ -56,6 +56,8 @@ github.com/matrix-org/gomatrixserverlib v0.0.0-20190619132215-178ed5e3b8e2 h1:pY
|
|||
github.com/matrix-org/gomatrixserverlib v0.0.0-20190619132215-178ed5e3b8e2/go.mod h1:sf0RcKOdiwJeTti7A313xsaejNUGYDq02MQZ4JD4w/E=
|
||||
github.com/matrix-org/gomatrixserverlib v0.0.0-20190724145009-a6df10ef35d6 h1:B8n1H5Wb1B5jwLzTylBpY0kJCMRqrofT7PmOw4aJFJA=
|
||||
github.com/matrix-org/gomatrixserverlib v0.0.0-20190724145009-a6df10ef35d6/go.mod h1:sf0RcKOdiwJeTti7A313xsaejNUGYDq02MQZ4JD4w/E=
|
||||
github.com/matrix-org/gomatrixserverlib v0.0.0-20190805173246-3a2199d5ecd6 h1:xr69Hk6QM3RIN6JSvx3RpDowBGpHpDDqhqXCeySwYow=
|
||||
github.com/matrix-org/gomatrixserverlib v0.0.0-20190805173246-3a2199d5ecd6/go.mod h1:sf0RcKOdiwJeTti7A313xsaejNUGYDq02MQZ4JD4w/E=
|
||||
github.com/matrix-org/naffka v0.0.0-20171115094957-662bfd0841d0 h1:p7WTwG+aXM86+yVrYAiCMW3ZHSmotVvuRbjtt3jC+4A=
|
||||
github.com/matrix-org/naffka v0.0.0-20171115094957-662bfd0841d0/go.mod h1:cXoYQIENbdWIQHt1SyCo6Bl3C3raHwJ0wgVrXHSqf+A=
|
||||
github.com/matrix-org/util v0.0.0-20171013132526-8b1c8ab81986 h1:TiWl4hLvezAhRPM8tPcPDFTysZ7k4T/1J4GPp/iqlZo=
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ import (
|
|||
|
||||
"github.com/matrix-org/dendrite/clientapi/httputil"
|
||||
"github.com/matrix-org/dendrite/publicroomsapi/storage"
|
||||
"github.com/matrix-org/gomatrixserverlib"
|
||||
|
||||
"github.com/matrix-org/util"
|
||||
)
|
||||
|
|
@ -39,7 +40,7 @@ func GetVisibility(
|
|||
|
||||
var v roomVisibility
|
||||
if isPublic {
|
||||
v.Visibility = "public"
|
||||
v.Visibility = gomatrixserverlib.Public
|
||||
} else {
|
||||
v.Visibility = "private"
|
||||
}
|
||||
|
|
@ -61,7 +62,7 @@ func SetVisibility(
|
|||
return *reqErr
|
||||
}
|
||||
|
||||
isPublic := v.Visibility == "public"
|
||||
isPublic := v.Visibility == gomatrixserverlib.Public
|
||||
if err := publicRoomsDatabase.SetRoomVisibility(req.Context(), isPublic, roomID); err != nil {
|
||||
return httputil.LogThenError(req, err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -185,7 +185,7 @@ func (d *PublicRoomsServerDatabase) updateNumJoinedUsers(
|
|||
return err
|
||||
}
|
||||
|
||||
if membership != "join" {
|
||||
if membership != gomatrixserverlib.Join {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ func IsServerAllowed(
|
|||
) bool {
|
||||
for _, ev := range authEvents {
|
||||
membership, err := ev.Membership()
|
||||
if err != nil || membership != "join" {
|
||||
if err != nil || membership != gomatrixserverlib.Join {
|
||||
continue
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -23,13 +23,6 @@ import (
|
|||
"github.com/matrix-org/gomatrixserverlib"
|
||||
)
|
||||
|
||||
// Membership values
|
||||
// TODO: Factor these out somewhere sensible?
|
||||
const join = "join"
|
||||
const leave = "leave"
|
||||
const invite = "invite"
|
||||
const ban = "ban"
|
||||
|
||||
// updateMembership updates the current membership and the invites for each
|
||||
// user affected by a change in the current state of the room.
|
||||
// Returns a list of output events to write to the kafka log to inform the
|
||||
|
|
@ -91,8 +84,8 @@ func updateMembership(
|
|||
) ([]api.OutputEvent, error) {
|
||||
var err error
|
||||
// Default the membership to Leave if no event was added or removed.
|
||||
oldMembership := leave
|
||||
newMembership := leave
|
||||
oldMembership := gomatrixserverlib.Leave
|
||||
newMembership := gomatrixserverlib.Leave
|
||||
|
||||
if remove != nil {
|
||||
oldMembership, err = remove.Membership()
|
||||
|
|
@ -106,7 +99,7 @@ func updateMembership(
|
|||
return nil, err
|
||||
}
|
||||
}
|
||||
if oldMembership == newMembership && newMembership != join {
|
||||
if oldMembership == newMembership && newMembership != gomatrixserverlib.Join {
|
||||
// If the membership is the same then nothing changed and we can return
|
||||
// immediately, unless it's a Join update (e.g. profile update).
|
||||
return updates, nil
|
||||
|
|
@ -118,11 +111,11 @@ func updateMembership(
|
|||
}
|
||||
|
||||
switch newMembership {
|
||||
case invite:
|
||||
case gomatrixserverlib.Invite:
|
||||
return updateToInviteMembership(mu, add, updates)
|
||||
case join:
|
||||
case gomatrixserverlib.Join:
|
||||
return updateToJoinMembership(mu, add, updates)
|
||||
case leave, ban:
|
||||
case gomatrixserverlib.Leave, gomatrixserverlib.Ban:
|
||||
return updateToLeaveMembership(mu, add, newMembership, updates)
|
||||
default:
|
||||
panic(fmt.Errorf(
|
||||
|
|
@ -183,7 +176,7 @@ func updateToJoinMembership(
|
|||
for _, eventID := range retired {
|
||||
orie := api.OutputRetireInviteEvent{
|
||||
EventID: eventID,
|
||||
Membership: join,
|
||||
Membership: gomatrixserverlib.Join,
|
||||
RetiredByEventID: add.EventID(),
|
||||
TargetUserID: *add.StateKey(),
|
||||
}
|
||||
|
|
|
|||
|
|
@ -359,7 +359,7 @@ func (r *RoomserverQueryAPI) getMembershipsBeforeEventNID(
|
|||
return nil, err
|
||||
}
|
||||
|
||||
if membership == "join" {
|
||||
if membership == gomatrixserverlib.Join {
|
||||
events = append(events, event)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,12 +35,6 @@ import (
|
|||
"github.com/matrix-org/gomatrixserverlib"
|
||||
)
|
||||
|
||||
const (
|
||||
membershipJoin = "join"
|
||||
membershipLeave = "leave"
|
||||
membershipBan = "ban"
|
||||
)
|
||||
|
||||
type stateDelta struct {
|
||||
roomID string
|
||||
stateEvents []gomatrixserverlib.Event
|
||||
|
|
@ -362,7 +356,7 @@ func (d *SyncServerDatasource) IncrementalSync(
|
|||
)
|
||||
} else {
|
||||
joinedRoomIDs, err = d.roomstate.selectRoomIDsWithMembership(
|
||||
ctx, nil, device.UserID, membershipJoin,
|
||||
ctx, nil, device.UserID, gomatrixserverlib.Join,
|
||||
)
|
||||
}
|
||||
if err != nil {
|
||||
|
|
@ -411,7 +405,7 @@ func (d *SyncServerDatasource) getResponseWithPDUsForCompleteSync(
|
|||
res = types.NewResponse(toPos)
|
||||
|
||||
// Extract room state and recent events for all rooms the user is joined to.
|
||||
joinedRoomIDs, err = d.roomstate.selectRoomIDsWithMembership(ctx, txn, userID, membershipJoin)
|
||||
joinedRoomIDs, err = d.roomstate.selectRoomIDsWithMembership(ctx, txn, userID, gomatrixserverlib.Join)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -591,7 +585,7 @@ func (d *SyncServerDatasource) addRoomDeltaToResponse(
|
|||
res *types.Response,
|
||||
) error {
|
||||
endPos := toPos
|
||||
if delta.membershipPos > 0 && delta.membership == membershipLeave {
|
||||
if delta.membershipPos > 0 && delta.membership == gomatrixserverlib.Leave {
|
||||
// make sure we don't leak recent events after the leave event.
|
||||
// TODO: History visibility makes this somewhat complex to handle correctly. For example:
|
||||
// TODO: This doesn't work for join -> leave in a single /sync request (see events prior to join).
|
||||
|
|
@ -629,7 +623,7 @@ func (d *SyncServerDatasource) addRoomDeltaToResponse(
|
|||
}
|
||||
|
||||
switch delta.membership {
|
||||
case membershipJoin:
|
||||
case gomatrixserverlib.Join:
|
||||
jr := types.NewJoinResponse()
|
||||
// Use the short form of batch token for prev_batch
|
||||
jr.Timeline.PrevBatch = strconv.FormatInt(prevPDUPos, 10)
|
||||
|
|
@ -637,9 +631,9 @@ func (d *SyncServerDatasource) addRoomDeltaToResponse(
|
|||
jr.Timeline.Limited = false // TODO: if len(events) >= numRecents + 1 and then set limited:true
|
||||
jr.State.Events = gomatrixserverlib.ToClientEvents(delta.stateEvents, gomatrixserverlib.FormatSync)
|
||||
res.Rooms.Join[delta.roomID] = *jr
|
||||
case membershipLeave:
|
||||
case gomatrixserverlib.Leave:
|
||||
fallthrough // transitions to leave are the same as ban
|
||||
case membershipBan:
|
||||
case gomatrixserverlib.Ban:
|
||||
// TODO: recentEvents may contain events that this user is not allowed to see because they are
|
||||
// no longer in the room.
|
||||
lr := types.NewLeaveResponse()
|
||||
|
|
@ -777,7 +771,7 @@ func (d *SyncServerDatasource) getStateDeltas(
|
|||
// the 'state' part of the response though, so is transparent modulo bandwidth concerns as it is not added to
|
||||
// the timeline.
|
||||
if membership := getMembershipFromEvent(&ev.Event, userID); membership != "" {
|
||||
if membership == membershipJoin {
|
||||
if membership == gomatrixserverlib.Join {
|
||||
// send full room state down instead of a delta
|
||||
var s []streamEvent
|
||||
s, err = d.currentStateStreamEventsForRoom(ctx, txn, roomID, stateFilterPart)
|
||||
|
|
@ -800,13 +794,13 @@ func (d *SyncServerDatasource) getStateDeltas(
|
|||
}
|
||||
|
||||
// Add in currently joined rooms
|
||||
joinedRoomIDs, err := d.roomstate.selectRoomIDsWithMembership(ctx, txn, userID, membershipJoin)
|
||||
joinedRoomIDs, err := d.roomstate.selectRoomIDsWithMembership(ctx, txn, userID, gomatrixserverlib.Join)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
for _, joinedRoomID := range joinedRoomIDs {
|
||||
deltas = append(deltas, stateDelta{
|
||||
membership: membershipJoin,
|
||||
membership: gomatrixserverlib.Join,
|
||||
stateEvents: streamEventsToEvents(device, state[joinedRoomID]),
|
||||
roomID: joinedRoomID,
|
||||
})
|
||||
|
|
@ -824,7 +818,7 @@ func (d *SyncServerDatasource) getStateDeltasForFullStateSync(
|
|||
fromPos, toPos int64, userID string,
|
||||
stateFilterPart *gomatrixserverlib.FilterPart,
|
||||
) ([]stateDelta, []string, error) {
|
||||
joinedRoomIDs, err := d.roomstate.selectRoomIDsWithMembership(ctx, txn, userID, "join")
|
||||
joinedRoomIDs, err := d.roomstate.selectRoomIDsWithMembership(ctx, txn, userID, gomatrixserverlib.Join)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
|
@ -839,7 +833,7 @@ func (d *SyncServerDatasource) getStateDeltasForFullStateSync(
|
|||
return nil, nil, stateErr
|
||||
}
|
||||
deltas = append(deltas, stateDelta{
|
||||
membership: "join",
|
||||
membership: gomatrixserverlib.Join,
|
||||
stateEvents: streamEventsToEvents(device, s),
|
||||
roomID: joinedRoomID,
|
||||
})
|
||||
|
|
@ -858,7 +852,7 @@ func (d *SyncServerDatasource) getStateDeltasForFullStateSync(
|
|||
for roomID, stateStreamEvents := range state {
|
||||
for _, ev := range stateStreamEvents {
|
||||
if membership := getMembershipFromEvent(&ev.Event, userID); membership != "" {
|
||||
if membership != "join" { // We've already added full state for all joined rooms above.
|
||||
if membership != gomatrixserverlib.Join { // We've already added full state for all joined rooms above.
|
||||
deltas = append(deltas, stateDelta{
|
||||
membership: membership,
|
||||
membershipPos: ev.streamPosition,
|
||||
|
|
|
|||
|
|
@ -93,16 +93,16 @@ func (n *Notifier) OnNewEvent(
|
|||
} else {
|
||||
// Keep the joined user map up-to-date
|
||||
switch membership {
|
||||
case "invite":
|
||||
case gomatrixserverlib.Invite:
|
||||
usersToNotify = append(usersToNotify, targetUserID)
|
||||
case "join":
|
||||
case gomatrixserverlib.Join:
|
||||
// Manually append the new user's ID so they get notified
|
||||
// along all members in the room
|
||||
usersToNotify = append(usersToNotify, targetUserID)
|
||||
n.addJoinedUser(ev.RoomID(), targetUserID)
|
||||
case "leave":
|
||||
case gomatrixserverlib.Leave:
|
||||
fallthrough
|
||||
case "ban":
|
||||
case gomatrixserverlib.Ban:
|
||||
n.removeJoinedUser(ev.RoomID(), targetUserID)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
3
testfile
3
testfile
|
|
@ -159,9 +159,12 @@ Inbound federation rejects remote attempts to kick local users to rooms
|
|||
An event which redacts itself should be ignored
|
||||
A pair of events which redact each other should be ignored
|
||||
Full state sync includes joined rooms
|
||||
A message sent after an initial sync appears in the timeline of an incremental sync.
|
||||
Can add tag
|
||||
Can remove tag
|
||||
Can list tags for a room
|
||||
Tags appear in an initial v2 /sync
|
||||
Newly updated tags appear in an incremental v2 /sync
|
||||
Deleted tags appear in an incremental v2 /sync
|
||||
/event/ on non world readable room does not work
|
||||
Outbound federation can query profile data
|
||||
|
|
|
|||
Loading…
Reference in a new issue