Use one transaction to upgrade rooms

This commit is contained in:
Till Faelligen 2022-11-07 10:05:45 +01:00
parent 74207f3b0c
commit 2aa90266c2
No known key found for this signature in database
GPG key ID: ACCDC9606D472758
3 changed files with 32 additions and 23 deletions

View file

@ -447,29 +447,7 @@ func (r *Inputer) processRoomEvent(
func (r *Inputer) handleRemoteRoomUpgrade(ctx context.Context, event *gomatrixserverlib.Event) error { func (r *Inputer) handleRemoteRoomUpgrade(ctx context.Context, event *gomatrixserverlib.Event) error {
oldRoomID := event.RoomID() oldRoomID := event.RoomID()
newRoomID := gjson.GetBytes(event.Content(), "replacement_room").Str newRoomID := gjson.GetBytes(event.Content(), "replacement_room").Str
// un-publish old room return r.DB.UpgradeRoom(ctx, oldRoomID, newRoomID, event.Sender())
if err := r.DB.PublishRoom(ctx, oldRoomID, "", "", false); err != nil {
return fmt.Errorf("failed to unpublish room: %w", err)
}
// publish new room
if err := r.DB.PublishRoom(ctx, newRoomID, "", "", true); err != nil {
return fmt.Errorf("failed to publish room: %w", err)
}
aliases, err := r.DB.GetAliasesForRoomID(ctx, oldRoomID)
if err != nil {
return fmt.Errorf("failed to get room aliases: %w", err)
}
for _, alias := range aliases {
if err = r.DB.RemoveRoomAlias(ctx, alias); err != nil {
fmt.Errorf("failed to remove room alias: %w", err)
}
if err = r.DB.SetRoomAlias(ctx, alias, newRoomID, event.Sender()); err != nil {
return fmt.Errorf("failed to set room alias: %w", err)
}
}
return nil
} }
// processStateBefore works out what the state is before the event and // processStateBefore works out what the state is before the event and

View file

@ -172,4 +172,5 @@ type Database interface {
ForgetRoom(ctx context.Context, userID, roomID string, forget bool) error ForgetRoom(ctx context.Context, userID, roomID string, forget bool) error
GetHistoryVisibilityState(ctx context.Context, roomInfo *types.RoomInfo, eventID string, domain string) ([]*gomatrixserverlib.Event, error) GetHistoryVisibilityState(ctx context.Context, roomInfo *types.RoomInfo, eventID string, domain string) ([]*gomatrixserverlib.Event, error)
UpgradeRoom(ctx context.Context, oldRoomID, newRoomID, eventSender string) error
} }

View file

@ -1386,6 +1386,36 @@ func (d *Database) ForgetRoom(ctx context.Context, userID, roomID string, forget
}) })
} }
func (d *Database) UpgradeRoom(ctx context.Context, oldRoomID, newRoomID, eventSender string) error {
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
// un-publish old room
if err := d.PublishedTable.UpsertRoomPublished(ctx, txn, oldRoomID, "", "", false); err != nil {
return fmt.Errorf("failed to unpublish room: %w", err)
}
// publish new room
if err := d.PublishedTable.UpsertRoomPublished(ctx, txn, newRoomID, "", "", true); err != nil {
return fmt.Errorf("failed to publish room: %w", err)
}
// Migrate any existing room aliases
aliases, err := d.RoomAliasesTable.SelectAliasesFromRoomID(ctx, txn, oldRoomID)
if err != nil {
return fmt.Errorf("failed to get room aliases: %w", err)
}
for _, alias := range aliases {
if err = d.RoomAliasesTable.DeleteRoomAlias(ctx, txn, alias); err != nil {
fmt.Errorf("failed to remove room alias: %w", err)
}
if err = d.RoomAliasesTable.InsertRoomAlias(ctx, txn, alias, newRoomID, eventSender); err != nil {
return fmt.Errorf("failed to set room alias: %w", err)
}
}
return nil
})
}
// FIXME TODO: Remove all this - horrible dupe with roomserver/state. Can't use the original impl because of circular loops // FIXME TODO: Remove all this - horrible dupe with roomserver/state. Can't use the original impl because of circular loops
// it should live in this package! // it should live in this package!