mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-07 06:03:09 -06:00
Tweak control flow, comments, use GMSL history visibility type
This commit is contained in:
parent
14b2d4dfe9
commit
644a9330b8
|
|
@ -44,7 +44,7 @@ func GetAliases(
|
||||||
return util.ErrorResponse(fmt.Errorf("rsAPI.QueryCurrentState: %w", err))
|
return util.ErrorResponse(fmt.Errorf("rsAPI.QueryCurrentState: %w", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
visibility := "invite"
|
visibility := gomatrixserverlib.HistoryVisibilityInvited
|
||||||
if historyVisEvent, ok := stateRes.StateEvents[stateTuple]; ok {
|
if historyVisEvent, ok := stateRes.StateEvents[stateTuple]; ok {
|
||||||
var err error
|
var err error
|
||||||
visibility, err = historyVisEvent.HistoryVisibility()
|
visibility, err = historyVisEvent.HistoryVisibility()
|
||||||
|
|
|
||||||
2
go.mod
2
go.mod
|
|
@ -34,7 +34,7 @@ require (
|
||||||
github.com/matrix-org/dugong v0.0.0-20210921133753-66e6b1c67e2e
|
github.com/matrix-org/dugong v0.0.0-20210921133753-66e6b1c67e2e
|
||||||
github.com/matrix-org/go-sqlite3-js v0.0.0-20220419092513-28aa791a1c91
|
github.com/matrix-org/go-sqlite3-js v0.0.0-20220419092513-28aa791a1c91
|
||||||
github.com/matrix-org/gomatrix v0.0.0-20210324163249-be2af5ef2e16
|
github.com/matrix-org/gomatrix v0.0.0-20210324163249-be2af5ef2e16
|
||||||
github.com/matrix-org/gomatrixserverlib v0.0.0-20220607143425-e55d796fd0b3
|
github.com/matrix-org/gomatrixserverlib v0.0.0-20220613132209-aedb3fbb511a
|
||||||
github.com/matrix-org/pinecone v0.0.0-20220408153826-2999ea29ed48
|
github.com/matrix-org/pinecone v0.0.0-20220408153826-2999ea29ed48
|
||||||
github.com/matrix-org/util v0.0.0-20200807132607-55161520e1d4
|
github.com/matrix-org/util v0.0.0-20200807132607-55161520e1d4
|
||||||
github.com/mattn/go-sqlite3 v1.14.13
|
github.com/mattn/go-sqlite3 v1.14.13
|
||||||
|
|
|
||||||
4
go.sum
4
go.sum
|
|
@ -418,8 +418,8 @@ github.com/matrix-org/go-sqlite3-js v0.0.0-20220419092513-28aa791a1c91/go.mod h1
|
||||||
github.com/matrix-org/gomatrix v0.0.0-20190528120928-7df988a63f26/go.mod h1:3fxX6gUjWyI/2Bt7J1OLhpCzOfO/bB3AiX0cJtEKud0=
|
github.com/matrix-org/gomatrix v0.0.0-20190528120928-7df988a63f26/go.mod h1:3fxX6gUjWyI/2Bt7J1OLhpCzOfO/bB3AiX0cJtEKud0=
|
||||||
github.com/matrix-org/gomatrix v0.0.0-20210324163249-be2af5ef2e16 h1:ZtO5uywdd5dLDCud4r0r55eP4j9FuUNpl60Gmntcop4=
|
github.com/matrix-org/gomatrix v0.0.0-20210324163249-be2af5ef2e16 h1:ZtO5uywdd5dLDCud4r0r55eP4j9FuUNpl60Gmntcop4=
|
||||||
github.com/matrix-org/gomatrix v0.0.0-20210324163249-be2af5ef2e16/go.mod h1:/gBX06Kw0exX1HrwmoBibFA98yBk/jxKpGVeyQbff+s=
|
github.com/matrix-org/gomatrix v0.0.0-20210324163249-be2af5ef2e16/go.mod h1:/gBX06Kw0exX1HrwmoBibFA98yBk/jxKpGVeyQbff+s=
|
||||||
github.com/matrix-org/gomatrixserverlib v0.0.0-20220607143425-e55d796fd0b3 h1:2eYcBt8Kg+nW/xIJY5x8Uo2dQLjUF+oxLap00uFC5l8=
|
github.com/matrix-org/gomatrixserverlib v0.0.0-20220613132209-aedb3fbb511a h1:jOkrb6twViAGTHHadA51sQwdloHT0Vx1MCptk9InTHo=
|
||||||
github.com/matrix-org/gomatrixserverlib v0.0.0-20220607143425-e55d796fd0b3/go.mod h1:jX38yp3SSLJNftBg3PXU1ayd0PCLIiDHQ4xAc9DIixk=
|
github.com/matrix-org/gomatrixserverlib v0.0.0-20220613132209-aedb3fbb511a/go.mod h1:jX38yp3SSLJNftBg3PXU1ayd0PCLIiDHQ4xAc9DIixk=
|
||||||
github.com/matrix-org/pinecone v0.0.0-20220408153826-2999ea29ed48 h1:W0sjjC6yjskHX4mb0nk3p0fXAlbU5bAFUFeEtlrPASE=
|
github.com/matrix-org/pinecone v0.0.0-20220408153826-2999ea29ed48 h1:W0sjjC6yjskHX4mb0nk3p0fXAlbU5bAFUFeEtlrPASE=
|
||||||
github.com/matrix-org/pinecone v0.0.0-20220408153826-2999ea29ed48/go.mod h1:ulJzsVOTssIVp1j/m5eI//4VpAGDkMt5NrRuAVX7wpc=
|
github.com/matrix-org/pinecone v0.0.0-20220408153826-2999ea29ed48/go.mod h1:ulJzsVOTssIVp1j/m5eI//4VpAGDkMt5NrRuAVX7wpc=
|
||||||
github.com/matrix-org/util v0.0.0-20190711121626-527ce5ddefc7/go.mod h1:vVQlW/emklohkZnOPwD3LrZUBqdfsbiyO3p1lNV8F6U=
|
github.com/matrix-org/util v0.0.0-20190711121626-527ce5ddefc7/go.mod h1:vVQlW/emklohkZnOPwD3LrZUBqdfsbiyO3p1lNV8F6U=
|
||||||
|
|
|
||||||
|
|
@ -162,7 +162,7 @@ type OutputNewRoomEvent struct {
|
||||||
// was specified
|
// was specified
|
||||||
TransactionID *TransactionID `json:"transaction_id,omitempty"`
|
TransactionID *TransactionID `json:"transaction_id,omitempty"`
|
||||||
// The history visibility of the event.
|
// The history visibility of the event.
|
||||||
HistoryVisibility string `json:"history_visibility"`
|
HistoryVisibility gomatrixserverlib.HistoryVisibility `json:"history_visibility"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *OutputNewRoomEvent) NeededStateEventIDs() ([]*gomatrixserverlib.HeaderedEvent, []string) {
|
func (o *OutputNewRoomEvent) NeededStateEventIDs() ([]*gomatrixserverlib.HeaderedEvent, []string) {
|
||||||
|
|
@ -189,8 +189,8 @@ func (o *OutputNewRoomEvent) NeededStateEventIDs() ([]*gomatrixserverlib.Headere
|
||||||
// should build their current room state up from OutputNewRoomEvents only.
|
// should build their current room state up from OutputNewRoomEvents only.
|
||||||
type OutputOldRoomEvent struct {
|
type OutputOldRoomEvent struct {
|
||||||
// The Event.
|
// The Event.
|
||||||
Event *gomatrixserverlib.HeaderedEvent `json:"event"`
|
Event *gomatrixserverlib.HeaderedEvent `json:"event"`
|
||||||
HistoryVisibility string `json:"history_visibility"`
|
HistoryVisibility gomatrixserverlib.HistoryVisibility `json:"history_visibility"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// An OutputNewInviteEvent is written whenever an invite becomes active.
|
// An OutputNewInviteEvent is written whenever an invite becomes active.
|
||||||
|
|
|
||||||
|
|
@ -299,7 +299,7 @@ func (r *Inputer) processRoomEvent(
|
||||||
// allowed at the time, and also to get the history visibility. We won't
|
// allowed at the time, and also to get the history visibility. We won't
|
||||||
// bother doing this if the event was already rejected as it just ends up
|
// bother doing this if the event was already rejected as it just ends up
|
||||||
// burning CPU time.
|
// burning CPU time.
|
||||||
historyVisibility := "joined" // Default to restrictive.
|
historyVisibility := gomatrixserverlib.HistoryVisibilityJoined // Default to restrictive.
|
||||||
if rejectionErr == nil && !isRejected && !softfail {
|
if rejectionErr == nil && !isRejected && !softfail {
|
||||||
var err error
|
var err error
|
||||||
historyVisibility, rejectionErr, err = r.processStateBefore(ctx, input, missingPrev)
|
historyVisibility, rejectionErr, err = r.processStateBefore(ctx, input, missingPrev)
|
||||||
|
|
@ -376,7 +376,7 @@ func (r *Inputer) processRoomEvent(
|
||||||
input.SendAsServer, // send as server
|
input.SendAsServer, // send as server
|
||||||
input.TransactionID, // transaction ID
|
input.TransactionID, // transaction ID
|
||||||
input.HasState, // rewrites state?
|
input.HasState, // rewrites state?
|
||||||
historyVisibility, // the history visibility
|
historyVisibility, // the history visibility before the event
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return fmt.Errorf("r.updateLatestEvents: %w", err)
|
return fmt.Errorf("r.updateLatestEvents: %w", err)
|
||||||
}
|
}
|
||||||
|
|
@ -430,11 +430,17 @@ func (r *Inputer) processStateBefore(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
input *api.InputRoomEvent,
|
input *api.InputRoomEvent,
|
||||||
missingPrev bool,
|
missingPrev bool,
|
||||||
) (historyVisibility string, rejectionErr error, err error) {
|
) (historyVisibility gomatrixserverlib.HistoryVisibility, rejectionErr error, err error) {
|
||||||
historyVisibility = "joined" // Default to restrictive.
|
historyVisibility = gomatrixserverlib.HistoryVisibilityJoined // Default to restrictive.
|
||||||
event := input.Event.Unwrap()
|
event := input.Event.Unwrap()
|
||||||
|
isCreateEvent := event.Type() == gomatrixserverlib.MRoomCreate && event.StateKeyEquals("")
|
||||||
var stateBeforeEvent []*gomatrixserverlib.Event
|
var stateBeforeEvent []*gomatrixserverlib.Event
|
||||||
if input.HasState {
|
switch {
|
||||||
|
case isCreateEvent:
|
||||||
|
// There's no state before a create event so there is nothing
|
||||||
|
// else to do.
|
||||||
|
return
|
||||||
|
case input.HasState:
|
||||||
// If we're overriding the state then we need to go and retrieve
|
// If we're overriding the state then we need to go and retrieve
|
||||||
// them from the database. It's a hard error if they are missing.
|
// them from the database. It's a hard error if they are missing.
|
||||||
stateEvents, err := r.DB.EventsFromIDs(ctx, input.StateEventIDs)
|
stateEvents, err := r.DB.EventsFromIDs(ctx, input.StateEventIDs)
|
||||||
|
|
@ -445,12 +451,18 @@ func (r *Inputer) processStateBefore(
|
||||||
for _, entry := range stateEvents {
|
for _, entry := range stateEvents {
|
||||||
stateBeforeEvent = append(stateBeforeEvent, entry.Event)
|
stateBeforeEvent = append(stateBeforeEvent, entry.Event)
|
||||||
}
|
}
|
||||||
} else if missingPrev {
|
case missingPrev:
|
||||||
// If we're missing prev events and still failed to fetch them
|
// We don't know all of the prev events, so we can't work out
|
||||||
// before then we're stuck.
|
// the state before the event. Reject it in that case.
|
||||||
rejectionErr = fmt.Errorf("event %q should have prev events", event.EventID())
|
rejectionErr = fmt.Errorf("event %q has missing prev events", event.EventID())
|
||||||
return
|
return
|
||||||
} else if event.Type() != gomatrixserverlib.MRoomCreate && len(event.PrevEventIDs()) > 0 {
|
case len(event.PrevEventIDs()) == 0:
|
||||||
|
// There should be prev events since it's not a create event.
|
||||||
|
// A non-create event that claims to have no prev events is
|
||||||
|
// invalid, so reject it.
|
||||||
|
rejectionErr = fmt.Errorf("event %q must have prev events", event.EventID())
|
||||||
|
return
|
||||||
|
default:
|
||||||
// For all non-create events, there must be prev events, so we'll
|
// For all non-create events, there must be prev events, so we'll
|
||||||
// ask the query API for the relevant tuples needed for auth. We
|
// ask the query API for the relevant tuples needed for auth. We
|
||||||
// will include the history visibility here even though we don't
|
// will include the history visibility here even though we don't
|
||||||
|
|
@ -480,19 +492,13 @@ func (r *Inputer) processStateBefore(
|
||||||
default:
|
default:
|
||||||
stateBeforeEvent = gomatrixserverlib.UnwrapEventHeaders(stateBeforeRes.StateEvents)
|
stateBeforeEvent = gomatrixserverlib.UnwrapEventHeaders(stateBeforeRes.StateEvents)
|
||||||
}
|
}
|
||||||
} else if event.Type() != gomatrixserverlib.MRoomCreate && len(event.PrevEventIDs()) == 0 {
|
|
||||||
// Non-create events that don't have any prev event IDs are
|
|
||||||
// impossible in theory, so reject them.
|
|
||||||
rejectionErr = fmt.Errorf("event %q should have prev events", event.EventID())
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if rejectionErr == nil {
|
// At this point, stateBeforeEvent should be populated either by
|
||||||
// If we haven't rejected the event for some other reason by now,
|
// the supplied state in the input request, or from the prev events.
|
||||||
// see whether the event is allowed against the state at the time.
|
// Check whether the event is allowed or not.
|
||||||
stateBeforeAuth := gomatrixserverlib.NewAuthEvents(stateBeforeEvent)
|
stateBeforeAuth := gomatrixserverlib.NewAuthEvents(stateBeforeEvent)
|
||||||
if rejectionErr = gomatrixserverlib.Allowed(event, &stateBeforeAuth); rejectionErr != nil {
|
if rejectionErr = gomatrixserverlib.Allowed(event, &stateBeforeAuth); rejectionErr != nil {
|
||||||
return
|
return
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// Work out what the history visibility was at the time of the
|
// Work out what the history visibility was at the time of the
|
||||||
// event.
|
// event.
|
||||||
|
|
|
||||||
|
|
@ -56,7 +56,7 @@ func (r *Inputer) updateLatestEvents(
|
||||||
sendAsServer string,
|
sendAsServer string,
|
||||||
transactionID *api.TransactionID,
|
transactionID *api.TransactionID,
|
||||||
rewritesState bool,
|
rewritesState bool,
|
||||||
historyVisibility string,
|
historyVisibility gomatrixserverlib.HistoryVisibility,
|
||||||
) (err error) {
|
) (err error) {
|
||||||
span, ctx := opentracing.StartSpanFromContext(ctx, "updateLatestEvents")
|
span, ctx := opentracing.StartSpanFromContext(ctx, "updateLatestEvents")
|
||||||
defer span.Finish()
|
defer span.Finish()
|
||||||
|
|
@ -119,9 +119,10 @@ type latestEventsUpdater struct {
|
||||||
stateBeforeEventRemoves []types.StateEntry
|
stateBeforeEventRemoves []types.StateEntry
|
||||||
stateBeforeEventAdds []types.StateEntry
|
stateBeforeEventAdds []types.StateEntry
|
||||||
// The snapshots of current state before and after processing this event
|
// The snapshots of current state before and after processing this event
|
||||||
oldStateNID types.StateSnapshotNID
|
oldStateNID types.StateSnapshotNID
|
||||||
newStateNID types.StateSnapshotNID
|
newStateNID types.StateSnapshotNID
|
||||||
historyVisibility string
|
// The history visibility of the event itself (from the state before the event).
|
||||||
|
historyVisibility gomatrixserverlib.HistoryVisibility
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *latestEventsUpdater) doUpdateLatestEvents() error {
|
func (u *latestEventsUpdater) doUpdateLatestEvents() error {
|
||||||
|
|
|
||||||
|
|
@ -97,7 +97,7 @@ func Context(
|
||||||
state, _ := syncDB.CurrentState(ctx, roomID, &stateFilter, nil)
|
state, _ := syncDB.CurrentState(ctx, roomID, &stateFilter, nil)
|
||||||
// verify the user is allowed to see the context for this room/event
|
// verify the user is allowed to see the context for this room/event
|
||||||
for _, x := range state {
|
for _, x := range state {
|
||||||
var hisVis string
|
var hisVis gomatrixserverlib.HistoryVisibility
|
||||||
hisVis, err = x.HistoryVisibility()
|
hisVis, err = x.HistoryVisibility()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue