mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-16 18:43:10 -06:00
Consolidate duplicate event building code
This commit is contained in:
parent
99772ceb1f
commit
35f2563a97
|
|
@ -160,6 +160,7 @@ func (r *RoomserverInternalAPI) SetFederationAPI(fsAPI fsAPI.RoomserverFederatio
|
||||||
Cfg: &r.Cfg.RoomServer,
|
Cfg: &r.Cfg.RoomServer,
|
||||||
DB: r.DB,
|
DB: r.DB,
|
||||||
FSAPI: r.fsAPI,
|
FSAPI: r.fsAPI,
|
||||||
|
RSAPI: r,
|
||||||
Inputer: r.Inputer,
|
Inputer: r.Inputer,
|
||||||
}
|
}
|
||||||
r.Publisher = &perform.Publisher{
|
r.Publisher = &perform.Publisher{
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,6 @@ import (
|
||||||
"github.com/matrix-org/dendrite/roomserver/internal/input"
|
"github.com/matrix-org/dendrite/roomserver/internal/input"
|
||||||
"github.com/matrix-org/dendrite/roomserver/internal/query"
|
"github.com/matrix-org/dendrite/roomserver/internal/query"
|
||||||
"github.com/matrix-org/dendrite/roomserver/storage"
|
"github.com/matrix-org/dendrite/roomserver/storage"
|
||||||
"github.com/matrix-org/dendrite/roomserver/types"
|
|
||||||
"github.com/matrix-org/dendrite/setup/config"
|
"github.com/matrix-org/dendrite/setup/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -305,7 +304,13 @@ func (r *Joiner) performJoinRoomByID(
|
||||||
// locally on the homeserver.
|
// locally on the homeserver.
|
||||||
// TODO: Check what happens if the room exists on the server
|
// TODO: Check what happens if the room exists on the server
|
||||||
// but everyone has since left. I suspect it does the wrong thing.
|
// but everyone has since left. I suspect it does the wrong thing.
|
||||||
event, buildRes, err := buildEvent(ctx, r.DB, r.Cfg.Matrix, userDomain, &eb)
|
|
||||||
|
var buildRes rsAPI.QueryLatestEventsAndStateResponse
|
||||||
|
identity, err := r.Cfg.Matrix.SigningIdentityFor(userDomain)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", fmt.Errorf("error joining local room: %q", err)
|
||||||
|
}
|
||||||
|
event, err := eventutil.QueryAndBuildEvent(ctx, &eb, r.Cfg.Matrix, identity, time.Now(), r.RSAPI, &buildRes)
|
||||||
|
|
||||||
switch err {
|
switch err {
|
||||||
case nil:
|
case nil:
|
||||||
|
|
@ -430,46 +435,3 @@ func (r *Joiner) populateAuthorisedViaUserForRestrictedJoin(
|
||||||
}
|
}
|
||||||
return res.AuthorisedVia, nil
|
return res.AuthorisedVia, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildEvent(
|
|
||||||
ctx context.Context, db storage.Database, cfg *config.Global,
|
|
||||||
senderDomain gomatrixserverlib.ServerName,
|
|
||||||
builder *gomatrixserverlib.EventBuilder,
|
|
||||||
) (*gomatrixserverlib.HeaderedEvent, *rsAPI.QueryLatestEventsAndStateResponse, error) {
|
|
||||||
eventsNeeded, err := gomatrixserverlib.StateNeededForEventBuilder(builder)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, fmt.Errorf("gomatrixserverlib.StateNeededForEventBuilder: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(eventsNeeded.Tuples()) == 0 {
|
|
||||||
return nil, nil, errors.New("expecting state tuples for event builder, got none")
|
|
||||||
}
|
|
||||||
|
|
||||||
var queryRes rsAPI.QueryLatestEventsAndStateResponse
|
|
||||||
err = helpers.QueryLatestEventsAndState(ctx, db, &rsAPI.QueryLatestEventsAndStateRequest{
|
|
||||||
RoomID: builder.RoomID,
|
|
||||||
StateToFetch: eventsNeeded.Tuples(),
|
|
||||||
}, &queryRes)
|
|
||||||
if err != nil {
|
|
||||||
switch err.(type) {
|
|
||||||
case types.MissingStateError:
|
|
||||||
// We know something about the room but the state seems to be
|
|
||||||
// insufficient to actually build a new event, so in effect we
|
|
||||||
// had might as well treat the room as if it doesn't exist.
|
|
||||||
return nil, nil, eventutil.ErrRoomNoExists
|
|
||||||
default:
|
|
||||||
return nil, nil, fmt.Errorf("QueryLatestEventsAndState: %w", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
identity, err := cfg.SigningIdentityFor(senderDomain)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
ev, err := eventutil.BuildEvent(ctx, builder, cfg, identity, time.Now(), &eventsNeeded, &queryRes)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
return ev, &queryRes, nil
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -19,8 +19,10 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
||||||
|
"github.com/matrix-org/dendrite/internal/eventutil"
|
||||||
"github.com/matrix-org/gomatrix"
|
"github.com/matrix-org/gomatrix"
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
"github.com/matrix-org/util"
|
"github.com/matrix-org/util"
|
||||||
|
|
@ -28,6 +30,7 @@ import (
|
||||||
|
|
||||||
fsAPI "github.com/matrix-org/dendrite/federationapi/api"
|
fsAPI "github.com/matrix-org/dendrite/federationapi/api"
|
||||||
"github.com/matrix-org/dendrite/roomserver/api"
|
"github.com/matrix-org/dendrite/roomserver/api"
|
||||||
|
rsAPI "github.com/matrix-org/dendrite/roomserver/api"
|
||||||
"github.com/matrix-org/dendrite/roomserver/internal/helpers"
|
"github.com/matrix-org/dendrite/roomserver/internal/helpers"
|
||||||
"github.com/matrix-org/dendrite/roomserver/internal/input"
|
"github.com/matrix-org/dendrite/roomserver/internal/input"
|
||||||
"github.com/matrix-org/dendrite/roomserver/storage"
|
"github.com/matrix-org/dendrite/roomserver/storage"
|
||||||
|
|
@ -39,6 +42,7 @@ type Leaver struct {
|
||||||
Cfg *config.RoomServer
|
Cfg *config.RoomServer
|
||||||
DB storage.Database
|
DB storage.Database
|
||||||
FSAPI fsAPI.RoomserverFederationAPI
|
FSAPI fsAPI.RoomserverFederationAPI
|
||||||
|
RSAPI rsAPI.RoomserverInternalAPI
|
||||||
UserAPI userapi.RoomserverUserAPI
|
UserAPI userapi.RoomserverUserAPI
|
||||||
Inputer *input.Inputer
|
Inputer *input.Inputer
|
||||||
}
|
}
|
||||||
|
|
@ -173,9 +177,15 @@ func (r *Leaver) performLeaveRoomByID(
|
||||||
// a leave event.
|
// a leave event.
|
||||||
// TODO: Check what happens if the room exists on the server
|
// TODO: Check what happens if the room exists on the server
|
||||||
// but everyone has since left. I suspect it does the wrong thing.
|
// but everyone has since left. I suspect it does the wrong thing.
|
||||||
event, buildRes, err := buildEvent(ctx, r.DB, r.Cfg.Matrix, senderDomain, &eb)
|
|
||||||
|
var buildRes rsAPI.QueryLatestEventsAndStateResponse
|
||||||
|
identity, err := r.Cfg.Matrix.SigningIdentityFor(senderDomain)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("eventutil.BuildEvent: %w", err)
|
return nil, fmt.Errorf("SigningIdentityFor: %w", err)
|
||||||
|
}
|
||||||
|
event, err := eventutil.QueryAndBuildEvent(ctx, &eb, r.Cfg.Matrix, identity, time.Now(), r.RSAPI, &buildRes)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("eventutil.QueryAndBuildEvent: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Give our leave event to the roomserver input stream. The
|
// Give our leave event to the roomserver input stream. The
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue