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"
"fmt"
"net/http"
"net/url"
appserviceAPI "github.com/matrix-org/dendrite/appservice/api"
"github.com/matrix-org/dendrite/clientapi/jsonerror"
@ -210,7 +211,12 @@ func buildConsentURI(cfgClient *config.ClientAPI, userID string) (string, error)
}
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) {

View file

@ -112,43 +112,25 @@ func sendServerNotice(
}, nil
}
// get rooms for specified user
allUserRooms, err := getAllUserRooms(ctx, rsAPI, serverNoticeRequest.UserID)
qryServerNoticeRoom := &userapi.QueryServerNoticeRoomResponse{}
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 {
return util.ErrorResponse(err), nil
}
// get rooms of the sender
senderUserID := fmt.Sprintf("@%s:%s", cfgNotices.LocalPart, cfgClient.Matrix.ServerName)
senderRooms := api.QueryRoomsForUserResponse{}
if err := rsAPI.QueryRoomsForUser(ctx, &api.QueryRoomsForUserRequest{
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
)
roomID := qryServerNoticeRoom.RoomID
roomVersion := gomatrixserverlib.RoomVersionV6
// create a new room for the user
if len(commonRooms) == 0 {
if qryServerNoticeRoom.RoomID == "" {
powerLevelContent := eventutil.InitialPowerLevelsContent(senderUserID)
powerLevelContent.Users[serverNoticeRequest.UserID] = -10 // taken from Synapse
pl, err := json.Marshal(powerLevelContent)
@ -177,7 +159,12 @@ func sendServerNotice(
switch data := roomRes.JSON.(type) {
case createRoomResponse:
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
serverAlertTag := gomatrix.TagContent{Tags: map[string]gomatrix.TagProperties{
"m.server_notice": {
@ -195,11 +182,17 @@ func sendServerNotice(
}
} else {
roomID = commonRooms[0]
// re-invite the user
res, err := sendInvite(ctx, accountsDB, senderDevice, roomID, serverNoticeRequest.UserID, "Server notice room", cfgClient, rsAPI, asAPI, time.Now())
res := &api.QueryMembershipForUserResponse{}
err := rsAPI.QueryMembershipForUser(ctx, &api.QueryMembershipForUserRequest{UserID: serverNoticeRequest.UserID, RoomID: roomID}, res)
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
}
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) {
if r.UserID == "" {
return false