mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-16 02:23:10 -06:00
Refine SenderID/UserID usage
This commit is contained in:
parent
26ef0246e8
commit
0c902f49b6
|
|
@ -181,10 +181,25 @@ func SetLocalAlias(
|
||||||
return *resErr
|
return *resErr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
userID, err := spec.NewUserID(device.UserID, true)
|
||||||
|
if err != nil {
|
||||||
|
return util.JSONResponse{
|
||||||
|
Code: http.StatusInternalServerError,
|
||||||
|
JSON: spec.InternalServerError{Err: "UserID for device is invalid"},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
deviceSenderID, err := rsAPI.QuerySenderIDForRoom(req.Context(), alias, *userID)
|
||||||
|
if err != nil {
|
||||||
|
return util.JSONResponse{
|
||||||
|
Code: http.StatusInternalServerError,
|
||||||
|
JSON: spec.InternalServerError{Err: "Could not find SenderID for this device"},
|
||||||
|
}
|
||||||
|
}
|
||||||
queryReq := roomserverAPI.SetRoomAliasRequest{
|
queryReq := roomserverAPI.SetRoomAliasRequest{
|
||||||
UserID: device.UserID,
|
SenderID: deviceSenderID,
|
||||||
RoomID: r.RoomID,
|
RoomID: r.RoomID,
|
||||||
Alias: alias,
|
Alias: alias,
|
||||||
}
|
}
|
||||||
var queryRes roomserverAPI.SetRoomAliasResponse
|
var queryRes roomserverAPI.SetRoomAliasResponse
|
||||||
if err := rsAPI.SetRoomAlias(req.Context(), &queryReq, &queryRes); err != nil {
|
if err := rsAPI.SetRoomAlias(req.Context(), &queryReq, &queryRes); err != nil {
|
||||||
|
|
@ -215,9 +230,25 @@ func RemoveLocalAlias(
|
||||||
alias string,
|
alias string,
|
||||||
rsAPI roomserverAPI.ClientRoomserverAPI,
|
rsAPI roomserverAPI.ClientRoomserverAPI,
|
||||||
) util.JSONResponse {
|
) util.JSONResponse {
|
||||||
|
userID, err := spec.NewUserID(device.UserID, true)
|
||||||
|
if err != nil {
|
||||||
|
return util.JSONResponse{
|
||||||
|
Code: http.StatusInternalServerError,
|
||||||
|
JSON: spec.InternalServerError{Err: "UserID for device is invalid"},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
deviceSenderID, err := rsAPI.QuerySenderIDForRoom(req.Context(), alias, *userID)
|
||||||
|
if err != nil {
|
||||||
|
return util.JSONResponse{
|
||||||
|
Code: http.StatusInternalServerError,
|
||||||
|
JSON: spec.InternalServerError{Err: "Could not find SenderID for this device"},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
queryReq := roomserverAPI.RemoveRoomAliasRequest{
|
queryReq := roomserverAPI.RemoveRoomAliasRequest{
|
||||||
Alias: alias,
|
Alias: alias,
|
||||||
UserID: device.UserID,
|
SenderID: deviceSenderID,
|
||||||
}
|
}
|
||||||
var queryRes roomserverAPI.RemoveRoomAliasResponse
|
var queryRes roomserverAPI.RemoveRoomAliasResponse
|
||||||
if err := rsAPI.RemoveRoomAlias(req.Context(), &queryReq, &queryRes); err != nil {
|
if err := rsAPI.RemoveRoomAlias(req.Context(), &queryReq, &queryRes); err != nil {
|
||||||
|
|
|
||||||
|
|
@ -113,11 +113,12 @@ func ruleMatches(rule *Rule, kind Kind, event gomatrixserverlib.PDU, ec Evaluati
|
||||||
return rule.RuleID == event.RoomID(), nil
|
return rule.RuleID == event.RoomID(), nil
|
||||||
|
|
||||||
case SenderKind:
|
case SenderKind:
|
||||||
|
userID := ""
|
||||||
sender, err := event.UserID()
|
sender, err := event.UserID()
|
||||||
if err != nil {
|
if err == nil {
|
||||||
return false, nil
|
userID = sender.String()
|
||||||
}
|
}
|
||||||
return rule.RuleID == sender.String(), nil
|
return rule.RuleID == userID, nil
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return false, nil
|
return false, nil
|
||||||
|
|
|
||||||
|
|
@ -82,11 +82,11 @@ func TestRuleMatches(t *testing.T) {
|
||||||
{"contentMatch", ContentKind, Rule{Enabled: true, Pattern: pointer("b")}, `{"content":{"body":"abc"}}`, true},
|
{"contentMatch", ContentKind, Rule{Enabled: true, Pattern: pointer("b")}, `{"content":{"body":"abc"}}`, true},
|
||||||
{"contentNoMatch", ContentKind, Rule{Enabled: true, Pattern: pointer("d")}, `{"content":{"body":"abc"}}`, false},
|
{"contentNoMatch", ContentKind, Rule{Enabled: true, Pattern: pointer("d")}, `{"content":{"body":"abc"}}`, false},
|
||||||
|
|
||||||
{"roomMatch", RoomKind, Rule{Enabled: true, RuleID: "!room@example.com"}, `{"room_id":"!room@example.com"}`, true},
|
{"roomMatch", RoomKind, Rule{Enabled: true, RuleID: "!room:example.com"}, `{"room_id":"!room:example.com"}`, true},
|
||||||
{"roomNoMatch", RoomKind, Rule{Enabled: true, RuleID: "!room@example.com"}, `{"room_id":"!otherroom@example.com"}`, false},
|
{"roomNoMatch", RoomKind, Rule{Enabled: true, RuleID: "!room:example.com"}, `{"room_id":"!otherroom:example.com"}`, false},
|
||||||
|
|
||||||
{"senderMatch", SenderKind, Rule{Enabled: true, RuleID: "@user@example.com"}, `{"sender":"@user@example.com"}`, true},
|
{"senderMatch", SenderKind, Rule{Enabled: true, RuleID: "@user:example.com"}, `{"sender":"@user:example.com"}`, true},
|
||||||
{"senderNoMatch", SenderKind, Rule{Enabled: true, RuleID: "@user@example.com"}, `{"sender":"@otheruser@example.com"}`, false},
|
{"senderNoMatch", SenderKind, Rule{Enabled: true, RuleID: "@user:example.com"}, `{"sender":"@otheruser:example.com"}`, false},
|
||||||
}
|
}
|
||||||
for _, tst := range tsts {
|
for _, tst := range tsts {
|
||||||
t.Run(tst.Name, func(t *testing.T) {
|
t.Run(tst.Name, func(t *testing.T) {
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ import "regexp"
|
||||||
// SetRoomAliasRequest is a request to SetRoomAlias
|
// SetRoomAliasRequest is a request to SetRoomAlias
|
||||||
type SetRoomAliasRequest struct {
|
type SetRoomAliasRequest struct {
|
||||||
// ID of the user setting the alias
|
// ID of the user setting the alias
|
||||||
UserID string `json:"user_id"`
|
SenderID string `json:"user_id"`
|
||||||
// New alias for the room
|
// New alias for the room
|
||||||
Alias string `json:"alias"`
|
Alias string `json:"alias"`
|
||||||
// The room ID the alias is referring to
|
// The room ID the alias is referring to
|
||||||
|
|
@ -62,7 +62,7 @@ type GetAliasesForRoomIDResponse struct {
|
||||||
// RemoveRoomAliasRequest is a request to RemoveRoomAlias
|
// RemoveRoomAliasRequest is a request to RemoveRoomAlias
|
||||||
type RemoveRoomAliasRequest struct {
|
type RemoveRoomAliasRequest struct {
|
||||||
// ID of the user removing the alias
|
// ID of the user removing the alias
|
||||||
UserID string `json:"user_id"`
|
SenderID string `json:"user_id"`
|
||||||
// The room alias to remove
|
// The room alias to remove
|
||||||
Alias string `json:"alias"`
|
Alias string `json:"alias"`
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -65,6 +65,11 @@ type InputRoomEventsAPI interface {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type QuerySenderIDAPI interface {
|
||||||
|
// Accepts either roomID or alias
|
||||||
|
QuerySenderIDForRoom(ctx context.Context, roomAliasOrID string, userID spec.UserID) (string, error)
|
||||||
|
}
|
||||||
|
|
||||||
// Query the latest events and state for a room from the room server.
|
// Query the latest events and state for a room from the room server.
|
||||||
type QueryLatestEventsAndStateAPI interface {
|
type QueryLatestEventsAndStateAPI interface {
|
||||||
QueryLatestEventsAndState(ctx context.Context, req *QueryLatestEventsAndStateRequest, res *QueryLatestEventsAndStateResponse) error
|
QueryLatestEventsAndState(ctx context.Context, req *QueryLatestEventsAndStateRequest, res *QueryLatestEventsAndStateResponse) error
|
||||||
|
|
@ -158,6 +163,7 @@ type ClientRoomserverAPI interface {
|
||||||
QueryLatestEventsAndStateAPI
|
QueryLatestEventsAndStateAPI
|
||||||
QueryBulkStateContentAPI
|
QueryBulkStateContentAPI
|
||||||
QueryEventsAPI
|
QueryEventsAPI
|
||||||
|
QuerySenderIDAPI
|
||||||
QueryMembershipForUser(ctx context.Context, req *QueryMembershipForUserRequest, res *QueryMembershipForUserResponse) error
|
QueryMembershipForUser(ctx context.Context, req *QueryMembershipForUserRequest, res *QueryMembershipForUserResponse) error
|
||||||
QueryMembershipsForRoom(ctx context.Context, req *QueryMembershipsForRoomRequest, res *QueryMembershipsForRoomResponse) error
|
QueryMembershipsForRoom(ctx context.Context, req *QueryMembershipsForRoomRequest, res *QueryMembershipsForRoomResponse) error
|
||||||
QueryRoomsForUser(ctx context.Context, req *QueryRoomsForUserRequest, res *QueryRoomsForUserResponse) error
|
QueryRoomsForUser(ctx context.Context, req *QueryRoomsForUserRequest, res *QueryRoomsForUserResponse) error
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,7 @@ func (r *RoomserverInternalAPI) SetRoomAlias(
|
||||||
response.AliasExists = false
|
response.AliasExists = false
|
||||||
|
|
||||||
// Save the new alias
|
// Save the new alias
|
||||||
if err := r.DB.SetRoomAlias(ctx, request.Alias, request.RoomID, request.UserID); err != nil {
|
if err := r.DB.SetRoomAlias(ctx, request.Alias, request.RoomID, request.SenderID); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -119,7 +119,7 @@ func (r *RoomserverInternalAPI) RemoveRoomAlias(
|
||||||
request *api.RemoveRoomAliasRequest,
|
request *api.RemoveRoomAliasRequest,
|
||||||
response *api.RemoveRoomAliasResponse,
|
response *api.RemoveRoomAliasResponse,
|
||||||
) error {
|
) error {
|
||||||
_, virtualHost, err := r.Cfg.Global.SplitLocalID('@', request.UserID)
|
_, virtualHost, err := r.Cfg.Global.SplitLocalID('@', request.SenderID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -140,7 +140,7 @@ func (r *RoomserverInternalAPI) RemoveRoomAlias(
|
||||||
return fmt.Errorf("r.DB.GetCreatorIDForAlias: %w", err)
|
return fmt.Errorf("r.DB.GetCreatorIDForAlias: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if creatorID != request.UserID {
|
if creatorID != request.SenderID {
|
||||||
var plEvent *types.HeaderedEvent
|
var plEvent *types.HeaderedEvent
|
||||||
var pls *gomatrixserverlib.PowerLevelContent
|
var pls *gomatrixserverlib.PowerLevelContent
|
||||||
|
|
||||||
|
|
@ -154,7 +154,7 @@ func (r *RoomserverInternalAPI) RemoveRoomAlias(
|
||||||
return fmt.Errorf("plEvent.PowerLevels: %w", err)
|
return fmt.Errorf("plEvent.PowerLevels: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if pls.UserLevel(request.UserID) < pls.EventLevel(spec.MRoomCanonicalAlias, true) {
|
if pls.UserLevel(request.SenderID) < pls.EventLevel(spec.MRoomCanonicalAlias, true) {
|
||||||
response.Removed = false
|
response.Removed = false
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
@ -172,8 +172,8 @@ func (r *RoomserverInternalAPI) RemoveRoomAlias(
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
sender := request.UserID
|
sender := request.SenderID
|
||||||
if request.UserID != ev.SenderID() {
|
if request.SenderID != ev.SenderID() {
|
||||||
sender = ev.SenderID()
|
sender = ev.SenderID()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -350,9 +350,9 @@ func (c *Creator) PerformCreateRoom(ctx context.Context, userID spec.UserID, roo
|
||||||
// been taken.
|
// been taken.
|
||||||
if roomAlias != "" {
|
if roomAlias != "" {
|
||||||
aliasReq := api.SetRoomAliasRequest{
|
aliasReq := api.SetRoomAliasRequest{
|
||||||
Alias: roomAlias,
|
Alias: roomAlias,
|
||||||
RoomID: roomID.String(),
|
RoomID: roomID.String(),
|
||||||
UserID: userID.String(),
|
SenderID: userID.String(),
|
||||||
}
|
}
|
||||||
|
|
||||||
var aliasResp api.SetRoomAliasResponse
|
var aliasResp api.SetRoomAliasResponse
|
||||||
|
|
|
||||||
|
|
@ -176,13 +176,13 @@ func moveLocalAliases(ctx context.Context,
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, alias := range aliasRes.Aliases {
|
for _, alias := range aliasRes.Aliases {
|
||||||
removeAliasReq := api.RemoveRoomAliasRequest{UserID: userID, Alias: alias}
|
removeAliasReq := api.RemoveRoomAliasRequest{SenderID: userID, Alias: alias}
|
||||||
removeAliasRes := api.RemoveRoomAliasResponse{}
|
removeAliasRes := api.RemoveRoomAliasResponse{}
|
||||||
if err = URSAPI.RemoveRoomAlias(ctx, &removeAliasReq, &removeAliasRes); err != nil {
|
if err = URSAPI.RemoveRoomAlias(ctx, &removeAliasReq, &removeAliasRes); err != nil {
|
||||||
return fmt.Errorf("Failed to remove old room alias: %w", err)
|
return fmt.Errorf("Failed to remove old room alias: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
setAliasReq := api.SetRoomAliasRequest{UserID: userID, Alias: alias, RoomID: newRoomID}
|
setAliasReq := api.SetRoomAliasRequest{SenderID: userID, Alias: alias, RoomID: newRoomID}
|
||||||
setAliasRes := api.SetRoomAliasResponse{}
|
setAliasRes := api.SetRoomAliasResponse{}
|
||||||
if err = URSAPI.SetRoomAlias(ctx, &setAliasReq, &setAliasRes); err != nil {
|
if err = URSAPI.SetRoomAlias(ctx, &setAliasReq, &setAliasRes); err != nil {
|
||||||
return fmt.Errorf("Failed to set new room alias: %w", err)
|
return fmt.Errorf("Failed to set new room alias: %w", err)
|
||||||
|
|
|
||||||
|
|
@ -1034,3 +1034,8 @@ func (r *Queryer) QueryRestrictedJoinAllowed(ctx context.Context, req *api.Query
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Queryer) QuerySenderIDForRoom(ctx context.Context, roomAliasOrID string, userID spec.UserID) (string, error) {
|
||||||
|
// TODO: implement this properly with pseudoIDs
|
||||||
|
return userID.String(), nil
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -267,7 +267,7 @@ func TestPurgeRoom(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
aliasResp := &api.SetRoomAliasResponse{}
|
aliasResp := &api.SetRoomAliasResponse{}
|
||||||
if err = rsAPI.SetRoomAlias(ctx, &api.SetRoomAliasRequest{RoomID: room.ID, Alias: "myalias", UserID: alice.ID}, aliasResp); err != nil {
|
if err = rsAPI.SetRoomAlias(ctx, &api.SetRoomAliasRequest{RoomID: room.ID, Alias: "myalias", SenderID: alice.ID}, aliasResp); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
// check the alias is actually there
|
// check the alias is actually there
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue