2017-08-22 11:12:51 +01: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 14:07:03 +01:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
|
|
|
|
2017-08-22 11:12:51 +01:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/httputil"
|
2020-03-02 16:20:44 +00:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
2017-08-22 11:12:51 +01:00
|
|
|
"github.com/matrix-org/dendrite/publicroomsapi/storage"
|
2019-08-06 15:07:36 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2017-08-22 11:12:51 +01: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 14:07:05 +00:00
|
|
|
req *http.Request, publicRoomsDatabase storage.Database,
|
2017-08-22 11:12:51 +01:00
|
|
|
roomID string,
|
|
|
|
) util.JSONResponse {
|
2017-09-14 14:46:56 +01:00
|
|
|
isPublic, err := publicRoomsDatabase.GetRoomVisibility(req.Context(), roomID)
|
2017-08-22 11:12:51 +01:00
|
|
|
if err != nil {
|
2020-03-02 16:20:44 +00:00
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("publicRoomsDatabase.GetRoomVisibility failed")
|
|
|
|
return jsonerror.InternalServerError()
|
2017-08-22 11:12:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var v roomVisibility
|
|
|
|
if isPublic {
|
2019-08-06 15:07:36 +01:00
|
|
|
v.Visibility = gomatrixserverlib.Public
|
2017-08-22 11:12:51 +01:00
|
|
|
} else {
|
|
|
|
v.Visibility = "private"
|
|
|
|
}
|
|
|
|
|
|
|
|
return util.JSONResponse{
|
2018-03-13 15:55:45 +00:00
|
|
|
Code: http.StatusOK,
|
2017-08-22 11:12:51 +01:00
|
|
|
JSON: v,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetVisibility implements PUT /directory/list/room/{roomID}
|
2020-04-14 14:07:03 +01:00
|
|
|
// TODO: Allow admin users to edit the room visibility
|
2017-08-22 11:12:51 +01:00
|
|
|
func SetVisibility(
|
2020-05-01 10:48:17 +01:00
|
|
|
req *http.Request, publicRoomsDatabase storage.Database, rsAPI api.RoomserverInternalAPI, dev *authtypes.Device,
|
2017-08-22 11:12:51 +01:00
|
|
|
roomID string,
|
|
|
|
) util.JSONResponse {
|
2020-04-14 14:07:03 +01:00
|
|
|
queryMembershipReq := api.QueryMembershipForUserRequest{
|
|
|
|
RoomID: roomID,
|
|
|
|
UserID: dev.UserID,
|
|
|
|
}
|
|
|
|
var queryMembershipRes api.QueryMembershipForUserResponse
|
2020-05-01 10:48:17 +01:00
|
|
|
err := rsAPI.QueryMembershipForUser(req.Context(), &queryMembershipReq, &queryMembershipRes)
|
2020-04-14 14:07:03 +01:00
|
|
|
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
|
2020-05-01 10:48:17 +01:00
|
|
|
err = rsAPI.QueryLatestEventsAndState(req.Context(), &queryEventsReq, &queryEventsRes)
|
2020-04-14 14:07:03 +01:00
|
|
|
if err != nil || len(queryEventsRes.StateEvents) == 0 {
|
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("could not query events from room")
|
|
|
|
return jsonerror.InternalServerError()
|
|
|
|
}
|
|
|
|
|
2020-04-17 12:17:20 +01:00
|
|
|
// NOTSPEC: Check if the user's power is greater than power required to change m.room.aliases event
|
|
|
|
power, _ := gomatrixserverlib.NewPowerLevelContentFromEvent(queryEventsRes.StateEvents[0].Event)
|
2020-04-14 14:07:03 +01:00
|
|
|
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 11:12:51 +01:00
|
|
|
var v roomVisibility
|
|
|
|
if reqErr := httputil.UnmarshalJSONRequest(req, &v); reqErr != nil {
|
|
|
|
return *reqErr
|
|
|
|
}
|
|
|
|
|
2019-08-06 15:07:36 +01:00
|
|
|
isPublic := v.Visibility == gomatrixserverlib.Public
|
2017-09-14 14:46:56 +01:00
|
|
|
if err := publicRoomsDatabase.SetRoomVisibility(req.Context(), isPublic, roomID); err != nil {
|
2020-03-02 16:20:44 +00:00
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("publicRoomsDatabase.SetRoomVisibility failed")
|
|
|
|
return jsonerror.InternalServerError()
|
2017-08-22 11:12:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return util.JSONResponse{
|
2018-03-13 15:55:45 +00:00
|
|
|
Code: http.StatusOK,
|
2017-08-22 11:12:51 +01:00
|
|
|
JSON: struct{}{},
|
|
|
|
}
|
|
|
|
}
|