2020-12-03 05:11:46 -06:00
|
|
|
// Copyright 2020 New Vector 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 perform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2021-11-24 04:45:23 -06:00
|
|
|
fsAPI "github.com/matrix-org/dendrite/federationapi/api"
|
2020-12-03 05:11:46 -06:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/internal/input"
|
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Unpeeker struct {
|
|
|
|
ServerName gomatrixserverlib.ServerName
|
|
|
|
Cfg *config.RoomServer
|
2022-05-05 13:30:38 -05:00
|
|
|
FSAPI fsAPI.RoomserverFederationAPI
|
2023-02-24 02:40:20 -06:00
|
|
|
Inputer *input.Inputer
|
2020-12-03 05:11:46 -06:00
|
|
|
}
|
|
|
|
|
2021-11-24 04:45:23 -06:00
|
|
|
// PerformPeek handles peeking into matrix rooms, including over federation by talking to the federationapi.
|
2020-12-03 05:11:46 -06:00
|
|
|
func (r *Unpeeker) PerformUnpeek(
|
|
|
|
ctx context.Context,
|
|
|
|
req *api.PerformUnpeekRequest,
|
|
|
|
res *api.PerformUnpeekResponse,
|
2022-08-11 09:29:33 -05:00
|
|
|
) error {
|
2020-12-03 05:11:46 -06:00
|
|
|
if err := r.performUnpeek(ctx, req); err != nil {
|
|
|
|
perr, ok := err.(*api.PerformError)
|
|
|
|
if ok {
|
|
|
|
res.Error = perr
|
|
|
|
} else {
|
|
|
|
res.Error = &api.PerformError{
|
|
|
|
Msg: err.Error(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-11 09:29:33 -05:00
|
|
|
return nil
|
2020-12-03 05:11:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Unpeeker) performUnpeek(
|
|
|
|
ctx context.Context,
|
|
|
|
req *api.PerformUnpeekRequest,
|
|
|
|
) error {
|
|
|
|
// FIXME: there's way too much duplication with performJoin
|
|
|
|
_, domain, err := gomatrixserverlib.SplitID('@', req.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return &api.PerformError{
|
|
|
|
Code: api.PerformErrorBadRequest,
|
|
|
|
Msg: fmt.Sprintf("Supplied user ID %q in incorrect format", req.UserID),
|
|
|
|
}
|
|
|
|
}
|
2022-10-26 06:59:19 -05:00
|
|
|
if !r.Cfg.Matrix.IsLocalServerName(domain) {
|
2020-12-03 05:11:46 -06:00
|
|
|
return &api.PerformError{
|
|
|
|
Code: api.PerformErrorBadRequest,
|
|
|
|
Msg: fmt.Sprintf("User %q does not belong to this homeserver", req.UserID),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(req.RoomID, "!") {
|
|
|
|
return r.performUnpeekRoomByID(ctx, req)
|
|
|
|
}
|
|
|
|
return &api.PerformError{
|
|
|
|
Code: api.PerformErrorBadRequest,
|
|
|
|
Msg: fmt.Sprintf("Room ID %q is invalid", req.RoomID),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Unpeeker) performUnpeekRoomByID(
|
|
|
|
_ context.Context,
|
|
|
|
req *api.PerformUnpeekRequest,
|
|
|
|
) (err error) {
|
|
|
|
// Get the domain part of the room ID.
|
|
|
|
_, _, err = gomatrixserverlib.SplitID('!', req.RoomID)
|
|
|
|
if err != nil {
|
|
|
|
return &api.PerformError{
|
|
|
|
Code: api.PerformErrorBadRequest,
|
|
|
|
Msg: fmt.Sprintf("Room ID %q is invalid: %s", req.RoomID, err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: handle federated peeks
|
|
|
|
|
2022-07-01 04:54:07 -05:00
|
|
|
err = r.Inputer.OutputProducer.ProduceRoomEvents(req.RoomID, []api.OutputEvent{
|
2020-12-03 05:11:46 -06:00
|
|
|
{
|
|
|
|
Type: api.OutputTypeRetirePeek,
|
|
|
|
RetirePeek: &api.OutputRetirePeek{
|
|
|
|
RoomID: req.RoomID,
|
|
|
|
UserID: req.UserID,
|
|
|
|
DeviceID: req.DeviceID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// By this point, if req.RoomIDOrAlias contained an alias, then
|
|
|
|
// it will have been overwritten with a room ID by performPeekRoomByAlias.
|
|
|
|
// We should now include this in the response so that the CS API can
|
|
|
|
// return the right room ID.
|
|
|
|
return nil
|
|
|
|
}
|