URL Encode, use new method to get server notice room

This commit is contained in:
Till Faelligen 2022-03-07 09:45:59 +01:00
parent c7d2254698
commit dcfc0bcd43
2 changed files with 35 additions and 47 deletions

View file

@ -22,6 +22,7 @@ import (
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"net/http" "net/http"
"net/url"
appserviceAPI "github.com/matrix-org/dendrite/appservice/api" appserviceAPI "github.com/matrix-org/dendrite/appservice/api"
"github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/dendrite/clientapi/jsonerror"
@ -210,7 +211,12 @@ func buildConsentURI(cfgClient *config.ClientAPI, userID string) (string, error)
} }
userMAC := mac.Sum(nil) userMAC := mac.Sum(nil)
return fmt.Sprintf("%s/_matrix/client/consent?u=%s&h=%s&v=%s", cfgClient.Matrix.UserConsentOptions.BaseURL, userID, userMAC, consentOpts.Version), nil params := url.Values{}
params.Add("u", userID)
params.Add("h", string(userMAC))
params.Add("v", consentOpts.Version)
return fmt.Sprintf("%s/_matrix/client/consent?%s", cfgClient.Matrix.UserConsentOptions.BaseURL, params.Encode()), nil
} }
func validHMAC(username, userHMAC, secret string) (bool, error) { func validHMAC(username, userHMAC, secret string) (bool, error) {

View file

@ -112,43 +112,25 @@ func sendServerNotice(
}, nil }, nil
} }
// get rooms for specified user qryServerNoticeRoom := &userapi.QueryServerNoticeRoomResponse{}
allUserRooms, err := getAllUserRooms(ctx, rsAPI, serverNoticeRequest.UserID) localpart, _, err := gomatrixserverlib.SplitID('@', serverNoticeRequest.UserID)
if err != nil {
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.BadJSON("Invalid request"),
}, nil
}
err = userAPI.SelectServerNoticeRoomID(ctx, &userapi.QueryServerNoticeRoomRequest{Localpart: localpart}, qryServerNoticeRoom)
if err != nil { if err != nil {
return util.ErrorResponse(err), nil return util.ErrorResponse(err), nil
} }
// get rooms of the sender
senderUserID := fmt.Sprintf("@%s:%s", cfgNotices.LocalPart, cfgClient.Matrix.ServerName) senderUserID := fmt.Sprintf("@%s:%s", cfgNotices.LocalPart, cfgClient.Matrix.ServerName)
senderRooms := api.QueryRoomsForUserResponse{} roomID := qryServerNoticeRoom.RoomID
if err := rsAPI.QueryRoomsForUser(ctx, &api.QueryRoomsForUserRequest{ roomVersion := gomatrixserverlib.RoomVersionV6
UserID: senderUserID,
WantMembership: "join",
}, &senderRooms); err != nil {
return util.ErrorResponse(err), nil
}
// check if we have rooms in common
commonRooms := []string{}
for _, userRoomID := range allUserRooms {
for _, senderRoomID := range senderRooms.RoomIDs {
if userRoomID == senderRoomID {
commonRooms = append(commonRooms, senderRoomID)
}
}
}
if len(commonRooms) > 1 {
return util.ErrorResponse(fmt.Errorf("expected to find one room, but got %d", len(commonRooms))), nil
}
var (
roomID string
roomVersion = gomatrixserverlib.RoomVersionV6
)
// create a new room for the user // create a new room for the user
if len(commonRooms) == 0 { if qryServerNoticeRoom.RoomID == "" {
powerLevelContent := eventutil.InitialPowerLevelsContent(senderUserID) powerLevelContent := eventutil.InitialPowerLevelsContent(senderUserID)
powerLevelContent.Users[serverNoticeRequest.UserID] = -10 // taken from Synapse powerLevelContent.Users[serverNoticeRequest.UserID] = -10 // taken from Synapse
pl, err := json.Marshal(powerLevelContent) pl, err := json.Marshal(powerLevelContent)
@ -177,7 +159,12 @@ func sendServerNotice(
switch data := roomRes.JSON.(type) { switch data := roomRes.JSON.(type) {
case createRoomResponse: case createRoomResponse:
roomID = data.RoomID roomID = data.RoomID
res := &userapi.UpdateServerNoticeRoomResponse{}
err := userAPI.UpdateServerNoticeRoomID(ctx, &userapi.UpdateServerNoticeRoomRequest{RoomID: roomID, Localpart: localpart}, res)
if err != nil {
util.GetLogger(ctx).WithError(err).Error("UpdateServerNoticeRoomID failed")
return jsonerror.InternalServerError(), nil
}
// tag the room, so we can later check if the user tries to reject an invite // tag the room, so we can later check if the user tries to reject an invite
serverAlertTag := gomatrix.TagContent{Tags: map[string]gomatrix.TagProperties{ serverAlertTag := gomatrix.TagContent{Tags: map[string]gomatrix.TagProperties{
"m.server_notice": { "m.server_notice": {
@ -195,11 +182,17 @@ func sendServerNotice(
} }
} else { } else {
roomID = commonRooms[0] res := &api.QueryMembershipForUserResponse{}
// re-invite the user err := rsAPI.QueryMembershipForUser(ctx, &api.QueryMembershipForUserRequest{UserID: serverNoticeRequest.UserID, RoomID: roomID}, res)
res, err := sendInvite(ctx, accountsDB, senderDevice, roomID, serverNoticeRequest.UserID, "Server notice room", cfgClient, rsAPI, asAPI, time.Now())
if err != nil { if err != nil {
return res, nil return util.ErrorResponse(err), nil
}
// re-invite the user
if res.Membership != gomatrixserverlib.Join {
res, err := sendInvite(ctx, accountsDB, senderDevice, roomID, serverNoticeRequest.UserID, "Server notice room", cfgClient, rsAPI, asAPI, time.Now())
if err != nil {
return res, nil
}
} }
} }
@ -265,17 +258,6 @@ func sendServerNotice(
return res, nil return res, nil
} }
func getAllUserRooms(ctx context.Context, rsAPI api.RoomserverInternalAPI, userID string) ([]string, error) {
userRooms := api.QueryRoomsForUserResponse{}
if err := rsAPI.QueryRoomsForUser(ctx, &api.QueryRoomsForUserRequest{
UserID: userID,
WantMembership: "all",
}, &userRooms); err != nil {
return nil, err
}
return userRooms.RoomIDs, nil
}
func (r sendServerNoticeRequest) valid() (ok bool) { func (r sendServerNoticeRequest) valid() (ok bool) {
if r.UserID == "" { if r.UserID == "" {
return false return false