mirror of
https://github.com/hoernschen/dendrite.git
synced 2024-12-27 23:48:27 +00:00
Tweak messages
This commit is contained in:
parent
10f2e8d92f
commit
9ee4ca0088
2 changed files with 75 additions and 0 deletions
|
@ -16,6 +16,7 @@ package routing
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sort"
|
"sort"
|
||||||
|
@ -156,6 +157,7 @@ func OnIncomingMessagesRequest(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Implement filtering (#587)
|
// TODO: Implement filtering (#587)
|
||||||
|
|
||||||
// Check the room ID's format.
|
// Check the room ID's format.
|
||||||
|
@ -166,6 +168,74 @@ func OnIncomingMessagesRequest(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Enforce boundaries based on history visibility.
|
||||||
|
visibility := "shared"
|
||||||
|
hisVisFilter := gomatrixserverlib.DefaultStateFilter()
|
||||||
|
hisVisFilter.Types = []string{"m.room.history_visibility"}
|
||||||
|
hisVisEvents, err := db.CurrentState(req.Context(), roomID, &hisVisFilter)
|
||||||
|
if err != nil {
|
||||||
|
util.GetLogger(req.Context()).WithError(err).Error("db.CurrentState for history visibility failed")
|
||||||
|
return jsonerror.InternalServerError()
|
||||||
|
}
|
||||||
|
if len(hisVisEvents) == 1 {
|
||||||
|
var content struct {
|
||||||
|
HistoryVisibility string `json:"history_visibility"`
|
||||||
|
}
|
||||||
|
if err = json.Unmarshal(hisVisEvents[0].Content(), &content); err != nil {
|
||||||
|
util.GetLogger(req.Context()).WithError(err).Error("json.Unmarshal for history visibility failed")
|
||||||
|
return jsonerror.InternalServerError()
|
||||||
|
}
|
||||||
|
visibility = content.HistoryVisibility
|
||||||
|
}
|
||||||
|
|
||||||
|
switch visibility {
|
||||||
|
case "joined", "invited": // TODO: treat invites properly
|
||||||
|
membership, _, err := db.MostRecentMembership(req.Context(), roomID, device.UserID) // nolint:govet
|
||||||
|
if err != nil {
|
||||||
|
util.GetLogger(req.Context()).WithError(err).Error("db.MostRecentMembership for history visibility failed")
|
||||||
|
return jsonerror.InternalServerError()
|
||||||
|
}
|
||||||
|
if membership == nil {
|
||||||
|
return util.JSONResponse{
|
||||||
|
Code: http.StatusForbidden,
|
||||||
|
JSON: jsonerror.Forbidden("History visibility prevents non-members from seeing this room"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pos, err := db.EventPositionInTopology(req.Context(), membership.EventID())
|
||||||
|
if err != nil {
|
||||||
|
util.GetLogger(req.Context()).WithError(err).Error("db.PositionInTopology for history visibility failed")
|
||||||
|
return jsonerror.InternalServerError()
|
||||||
|
}
|
||||||
|
if backwardOrdering {
|
||||||
|
if to.Depth < pos.Depth || to.PDUPosition < pos.PDUPosition {
|
||||||
|
to = pos
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if from.Depth < pos.Depth || from.PDUPosition < pos.PDUPosition {
|
||||||
|
from = pos
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
case "shared":
|
||||||
|
pos, err := db.EventPositionInTopology(req.Context(), hisVisEvents[0].EventID()) // nolint:govet
|
||||||
|
if err != nil {
|
||||||
|
util.GetLogger(req.Context()).WithError(err).Error("db.PositionInTopology for history visibility failed")
|
||||||
|
return jsonerror.InternalServerError()
|
||||||
|
}
|
||||||
|
if backwardOrdering {
|
||||||
|
if to.Depth < pos.Depth || to.PDUPosition < pos.PDUPosition {
|
||||||
|
to = pos
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if from.Depth < pos.Depth || from.PDUPosition < pos.PDUPosition {
|
||||||
|
from = pos
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
case "world_readable":
|
||||||
|
// do nothing, as world-readable imposes no boundaries
|
||||||
|
}
|
||||||
|
|
||||||
mReq := messagesReq{
|
mReq := messagesReq{
|
||||||
ctx: req.Context(),
|
ctx: req.Context(),
|
||||||
db: db,
|
db: db,
|
||||||
|
@ -238,6 +308,8 @@ func (r *messagesReq) retrieveEvents() (
|
||||||
eventFilter := gomatrixserverlib.DefaultRoomEventFilter()
|
eventFilter := gomatrixserverlib.DefaultRoomEventFilter()
|
||||||
eventFilter.Limit = r.limit
|
eventFilter.Limit = r.limit
|
||||||
|
|
||||||
|
// Work out if the history visibility affects the range of the request.
|
||||||
|
|
||||||
// Retrieve the events from the local database.
|
// Retrieve the events from the local database.
|
||||||
var streamEvents []types.StreamEvent
|
var streamEvents []types.StreamEvent
|
||||||
if r.fromStream != nil {
|
if r.fromStream != nil {
|
||||||
|
|
|
@ -517,6 +517,9 @@ func (d *Database) MostRecentMembership(
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, fmt.Errorf("d.CurrentRoomState.SelectStateEvent: %w", err)
|
return nil, 0, fmt.Errorf("d.CurrentRoomState.SelectStateEvent: %w", err)
|
||||||
}
|
}
|
||||||
|
if event == nil {
|
||||||
|
return nil, 0, nil
|
||||||
|
}
|
||||||
pos, err := d.OutputEvents.SelectPositionInStream(ctx, nil, event.EventID())
|
pos, err := d.OutputEvents.SelectPositionInStream(ctx, nil, event.EventID())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, fmt.Errorf("d.OutputEvents.SelectPositionInStream: %w", err)
|
return nil, 0, fmt.Errorf("d.OutputEvents.SelectPositionInStream: %w", err)
|
||||||
|
|
Loading…
Reference in a new issue