2017-08-22 05:12:51 -05:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package directory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2020-04-14 08:07:03 -05:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
|
|
|
|
2017-08-22 05:12:51 -05:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/httputil"
|
2020-03-02 10:20:44 -06:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
2017-08-22 05:12:51 -05:00
|
|
|
"github.com/matrix-org/dendrite/publicroomsapi/storage"
|
2019-08-06 09:07:36 -05:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2017-08-22 05:12:51 -05:00
|
|
|
|
|
|
|
"github.com/matrix-org/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
type roomVisibility struct {
|
|
|
|
Visibility string `json:"visibility"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetVisibility implements GET /directory/list/room/{roomID}
|
|
|
|
func GetVisibility(
|
2020-01-03 08:07:05 -06:00
|
|
|
req *http.Request, publicRoomsDatabase storage.Database,
|
2017-08-22 05:12:51 -05:00
|
|
|
roomID string,
|
|
|
|
) util.JSONResponse {
|
2017-09-14 08:46:56 -05:00
|
|
|
isPublic, err := publicRoomsDatabase.GetRoomVisibility(req.Context(), roomID)
|
2017-08-22 05:12:51 -05:00
|
|
|
if err != nil {
|
2020-03-02 10:20:44 -06:00
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("publicRoomsDatabase.GetRoomVisibility failed")
|
|
|
|
return jsonerror.InternalServerError()
|
2017-08-22 05:12:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var v roomVisibility
|
|
|
|
if isPublic {
|
2019-08-06 09:07:36 -05:00
|
|
|
v.Visibility = gomatrixserverlib.Public
|
2017-08-22 05:12:51 -05:00
|
|
|
} else {
|
|
|
|
v.Visibility = "private"
|
|
|
|
}
|
|
|
|
|
|
|
|
return util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusOK,
|
2017-08-22 05:12:51 -05:00
|
|
|
JSON: v,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetVisibility implements PUT /directory/list/room/{roomID}
|
2020-04-14 08:07:03 -05:00
|
|
|
// TODO: Allow admin users to edit the room visibility
|
2017-08-22 05:12:51 -05:00
|
|
|
func SetVisibility(
|
2020-04-14 08:07:03 -05:00
|
|
|
req *http.Request, publicRoomsDatabase storage.Database, queryAPI api.RoomserverQueryAPI, dev *authtypes.Device,
|
2017-08-22 05:12:51 -05:00
|
|
|
roomID string,
|
|
|
|
) util.JSONResponse {
|
2020-04-14 08:07:03 -05:00
|
|
|
queryMembershipReq := api.QueryMembershipForUserRequest{
|
|
|
|
RoomID: roomID,
|
|
|
|
UserID: dev.UserID,
|
|
|
|
}
|
|
|
|
var queryMembershipRes api.QueryMembershipForUserResponse
|
|
|
|
err := queryAPI.QueryMembershipForUser(req.Context(), &queryMembershipReq, &queryMembershipRes)
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("could not query membership for user")
|
|
|
|
return jsonerror.InternalServerError()
|
|
|
|
}
|
|
|
|
// Check if user id is in room
|
|
|
|
if !queryMembershipRes.IsInRoom {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusForbidden,
|
|
|
|
JSON: jsonerror.Forbidden("user does not belong to room"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
queryEventsReq := api.QueryLatestEventsAndStateRequest{
|
|
|
|
RoomID: roomID,
|
|
|
|
StateToFetch: []gomatrixserverlib.StateKeyTuple{{
|
|
|
|
EventType: gomatrixserverlib.MRoomPowerLevels,
|
|
|
|
StateKey: "",
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
var queryEventsRes api.QueryLatestEventsAndStateResponse
|
|
|
|
err = queryAPI.QueryLatestEventsAndState(req.Context(), &queryEventsReq, &queryEventsRes)
|
|
|
|
if err != nil || len(queryEventsRes.StateEvents) == 0 {
|
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("could not query events from room")
|
|
|
|
return jsonerror.InternalServerError()
|
|
|
|
}
|
|
|
|
power, _ := gomatrixserverlib.NewPowerLevelContentFromEvent(queryEventsRes.StateEvents[0].Event)
|
|
|
|
|
|
|
|
// Check if the user's power is greater than power required to change m.room.aliases event
|
|
|
|
if power.UserLevel(dev.UserID) < power.EventLevel(gomatrixserverlib.MRoomAliases, true) {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusForbidden,
|
|
|
|
JSON: jsonerror.Forbidden("userID doesn't have power level to change visibility"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-22 05:12:51 -05:00
|
|
|
var v roomVisibility
|
|
|
|
if reqErr := httputil.UnmarshalJSONRequest(req, &v); reqErr != nil {
|
|
|
|
return *reqErr
|
|
|
|
}
|
|
|
|
|
2019-08-06 09:07:36 -05:00
|
|
|
isPublic := v.Visibility == gomatrixserverlib.Public
|
2017-09-14 08:46:56 -05:00
|
|
|
if err := publicRoomsDatabase.SetRoomVisibility(req.Context(), isPublic, roomID); err != nil {
|
2020-03-02 10:20:44 -06:00
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("publicRoomsDatabase.SetRoomVisibility failed")
|
|
|
|
return jsonerror.InternalServerError()
|
2017-08-22 05:12:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusOK,
|
2017-08-22 05:12:51 -05:00
|
|
|
JSON: struct{}{},
|
|
|
|
}
|
|
|
|
}
|