Add AssignRoomNID to pre-assign roomNIDs (#3111)

This commit is contained in:
Till 2023-06-13 16:28:41 +02:00 committed by GitHub
parent 2c87972a3a
commit 7a2e325d10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 0 deletions

View File

@ -31,6 +31,7 @@ type Database interface {
UserRoomKeys
// Do we support processing input events for more than one room at a time?
SupportsConcurrentRoomInputs() bool
AssignRoomNID(ctx context.Context, roomID spec.RoomID, roomVersion gomatrixserverlib.RoomVersion) (roomNID types.RoomNID, err error)
// RoomInfo returns room information for the given room ID, or nil if there is no room.
RoomInfo(ctx context.Context, roomID string) (*types.RoomInfo, error)
RoomInfoByNID(ctx context.Context, roomNID types.RoomNID) (*types.RoomInfo, error)
@ -212,6 +213,7 @@ type UserRoomKeys interface {
type RoomDatabase interface {
EventDatabase
UserRoomKeys
AssignRoomNID(ctx context.Context, roomID spec.RoomID, roomVersion gomatrixserverlib.RoomVersion) (roomNID types.RoomNID, err error)
// RoomInfo returns room information for the given room ID, or nil if there is no room.
RoomInfo(ctx context.Context, roomID string) (*types.RoomInfo, error)
RoomInfoByNID(ctx context.Context, roomNID types.RoomNID) (*types.RoomInfo, error)

View File

@ -662,6 +662,26 @@ func (d *Database) IsEventRejected(ctx context.Context, roomNID types.RoomNID, e
return d.EventsTable.SelectEventRejected(ctx, nil, roomNID, eventID)
}
func (d *Database) AssignRoomNID(ctx context.Context, roomID spec.RoomID, roomVersion gomatrixserverlib.RoomVersion) (roomNID types.RoomNID, err error) {
// This should already be checked, let's check it anyway.
_, err = gomatrixserverlib.GetRoomVersion(roomVersion)
if err != nil {
return 0, err
}
err = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
roomNID, err = d.assignRoomNID(ctx, txn, roomID.String(), roomVersion)
if err != nil {
return err
}
return nil
})
if err != nil {
return 0, err
}
// Not setting caches, as assignRoomNID already does this
return roomNID, err
}
// GetOrCreateRoomInfo gets or creates a new RoomInfo, which is only safe to use with functions only needing a roomVersion or roomNID.
func (d *Database) GetOrCreateRoomInfo(ctx context.Context, event gomatrixserverlib.PDU) (roomInfo *types.RoomInfo, err error) {
// Get the default room version. If the client doesn't supply a room_version

View File

@ -7,6 +7,7 @@ import (
"time"
"github.com/matrix-org/dendrite/internal/caching"
"github.com/matrix-org/dendrite/roomserver/types"
"github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/gomatrixserverlib/spec"
"github.com/stretchr/testify/assert"
@ -199,3 +200,24 @@ func TestUserRoomKeys(t *testing.T) {
assert.Error(t, err)
})
}
func TestAssignRoomNID(t *testing.T) {
ctx := context.Background()
alice := test.NewUser(t)
room := test.NewRoom(t, alice)
roomID, err := spec.NewRoomID(room.ID)
assert.NoError(t, err)
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
db, close := mustCreateRoomserverDatabase(t, dbType)
defer close()
nid, err := db.AssignRoomNID(ctx, *roomID, room.Version)
assert.NoError(t, err)
assert.Greater(t, nid, types.EventNID(0))
_, err = db.AssignRoomNID(ctx, spec.RoomID{}, "notaroomversion")
assert.Error(t, err)
})
}