2020-05-28 12:15:21 +02:00
|
|
|
package shared
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2020-08-19 16:38:27 +02:00
|
|
|
"fmt"
|
2020-05-28 12:15:21 +02:00
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
)
|
|
|
|
|
2020-08-19 14:24:54 +02:00
|
|
|
type LatestEventsUpdater struct {
|
2020-05-28 12:15:21 +02:00
|
|
|
transaction
|
|
|
|
d *Database
|
2020-09-02 11:02:48 +02:00
|
|
|
roomInfo types.RoomInfo
|
2020-05-28 12:15:21 +02:00
|
|
|
latestEvents []types.StateAtEventAndReference
|
|
|
|
lastEventIDSent string
|
|
|
|
currentStateSnapshotNID types.StateSnapshotNID
|
|
|
|
}
|
|
|
|
|
2020-09-02 11:02:48 +02:00
|
|
|
func NewLatestEventsUpdater(ctx context.Context, d *Database, txn *sql.Tx, roomInfo types.RoomInfo) (*LatestEventsUpdater, error) {
|
2020-05-28 12:15:21 +02:00
|
|
|
eventNIDs, lastEventNIDSent, currentStateSnapshotNID, err :=
|
2020-09-02 11:02:48 +02:00
|
|
|
d.RoomsTable.SelectLatestEventsNIDsForUpdate(ctx, txn, roomInfo.RoomNID)
|
2020-05-28 12:15:21 +02:00
|
|
|
if err != nil {
|
|
|
|
txn.Rollback() // nolint: errcheck
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
stateAndRefs, err := d.EventsTable.BulkSelectStateAtEventAndReference(ctx, txn, eventNIDs)
|
|
|
|
if err != nil {
|
|
|
|
txn.Rollback() // nolint: errcheck
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var lastEventIDSent string
|
|
|
|
if lastEventNIDSent != 0 {
|
|
|
|
lastEventIDSent, err = d.EventsTable.SelectEventID(ctx, txn, lastEventNIDSent)
|
|
|
|
if err != nil {
|
|
|
|
txn.Rollback() // nolint: errcheck
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2020-08-19 14:24:54 +02:00
|
|
|
return &LatestEventsUpdater{
|
2020-09-02 11:02:48 +02:00
|
|
|
transaction{ctx, txn}, d, roomInfo, stateAndRefs, lastEventIDSent, currentStateSnapshotNID,
|
2020-05-28 12:15:21 +02:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RoomVersion implements types.RoomRecentEventsUpdater
|
2020-08-19 14:24:54 +02:00
|
|
|
func (u *LatestEventsUpdater) RoomVersion() (version gomatrixserverlib.RoomVersion) {
|
2020-09-02 11:02:48 +02:00
|
|
|
return u.roomInfo.RoomVersion
|
2020-05-28 12:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// LatestEvents implements types.RoomRecentEventsUpdater
|
2020-08-19 14:24:54 +02:00
|
|
|
func (u *LatestEventsUpdater) LatestEvents() []types.StateAtEventAndReference {
|
2020-05-28 12:15:21 +02:00
|
|
|
return u.latestEvents
|
|
|
|
}
|
|
|
|
|
|
|
|
// LastEventIDSent implements types.RoomRecentEventsUpdater
|
2020-08-19 14:24:54 +02:00
|
|
|
func (u *LatestEventsUpdater) LastEventIDSent() string {
|
2020-05-28 12:15:21 +02:00
|
|
|
return u.lastEventIDSent
|
|
|
|
}
|
|
|
|
|
|
|
|
// CurrentStateSnapshotNID implements types.RoomRecentEventsUpdater
|
2020-08-19 14:24:54 +02:00
|
|
|
func (u *LatestEventsUpdater) CurrentStateSnapshotNID() types.StateSnapshotNID {
|
2020-05-28 12:15:21 +02:00
|
|
|
return u.currentStateSnapshotNID
|
|
|
|
}
|
|
|
|
|
|
|
|
// StorePreviousEvents implements types.RoomRecentEventsUpdater
|
2020-08-19 14:24:54 +02:00
|
|
|
func (u *LatestEventsUpdater) StorePreviousEvents(eventNID types.EventNID, previousEventReferences []gomatrixserverlib.EventReference) error {
|
2020-08-19 16:38:27 +02:00
|
|
|
return u.d.Writer.Do(u.d.DB, u.txn, func(txn *sql.Tx) error {
|
|
|
|
for _, ref := range previousEventReferences {
|
|
|
|
if err := u.d.PrevEventsTable.InsertPreviousEvent(u.ctx, txn, ref.EventID, ref.EventSHA256, eventNID); err != nil {
|
|
|
|
return fmt.Errorf("u.d.PrevEventsTable.InsertPreviousEvent: %w", err)
|
|
|
|
}
|
2020-05-28 12:15:21 +02:00
|
|
|
}
|
2020-08-19 16:38:27 +02:00
|
|
|
return nil
|
|
|
|
})
|
2020-05-28 12:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsReferenced implements types.RoomRecentEventsUpdater
|
2020-08-19 14:24:54 +02:00
|
|
|
func (u *LatestEventsUpdater) IsReferenced(eventReference gomatrixserverlib.EventReference) (bool, error) {
|
2020-05-28 12:15:21 +02:00
|
|
|
err := u.d.PrevEventsTable.SelectPreviousEventExists(u.ctx, u.txn, eventReference.EventID, eventReference.EventSHA256)
|
|
|
|
if err == nil {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return false, nil
|
|
|
|
}
|
2020-08-19 16:38:27 +02:00
|
|
|
return false, fmt.Errorf("u.d.PrevEventsTable.SelectPreviousEventExists: %w", err)
|
2020-05-28 12:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetLatestEvents implements types.RoomRecentEventsUpdater
|
2020-08-19 14:24:54 +02:00
|
|
|
func (u *LatestEventsUpdater) SetLatestEvents(
|
2020-05-28 12:15:21 +02:00
|
|
|
roomNID types.RoomNID, latest []types.StateAtEventAndReference, lastEventNIDSent types.EventNID,
|
|
|
|
currentStateSnapshotNID types.StateSnapshotNID,
|
|
|
|
) error {
|
|
|
|
eventNIDs := make([]types.EventNID, len(latest))
|
|
|
|
for i := range latest {
|
|
|
|
eventNIDs[i] = latest[i].EventNID
|
|
|
|
}
|
2020-08-19 16:38:27 +02:00
|
|
|
return u.d.Writer.Do(u.d.DB, u.txn, func(txn *sql.Tx) error {
|
|
|
|
if err := u.d.RoomsTable.UpdateLatestEventNIDs(u.ctx, txn, roomNID, eventNIDs, lastEventNIDSent, currentStateSnapshotNID); err != nil {
|
|
|
|
return fmt.Errorf("u.d.RoomsTable.updateLatestEventNIDs: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2020-05-28 12:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// HasEventBeenSent implements types.RoomRecentEventsUpdater
|
2020-08-19 14:24:54 +02:00
|
|
|
func (u *LatestEventsUpdater) HasEventBeenSent(eventNID types.EventNID) (bool, error) {
|
2020-05-28 12:15:21 +02:00
|
|
|
return u.d.EventsTable.SelectEventSentToOutput(u.ctx, u.txn, eventNID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarkEventAsSent implements types.RoomRecentEventsUpdater
|
2020-08-19 14:24:54 +02:00
|
|
|
func (u *LatestEventsUpdater) MarkEventAsSent(eventNID types.EventNID) error {
|
2020-08-19 16:38:27 +02:00
|
|
|
return u.d.Writer.Do(u.d.DB, u.txn, func(txn *sql.Tx) error {
|
|
|
|
return u.d.EventsTable.UpdateEventSentToOutput(u.ctx, txn, eventNID)
|
|
|
|
})
|
2020-05-28 12:15:21 +02:00
|
|
|
}
|
|
|
|
|
2020-08-19 14:24:54 +02:00
|
|
|
func (u *LatestEventsUpdater) MembershipUpdater(targetUserNID types.EventStateKeyNID, targetLocal bool) (*MembershipUpdater, error) {
|
2020-09-02 11:02:48 +02:00
|
|
|
return u.d.membershipUpdaterTxn(u.ctx, u.txn, u.roomInfo.RoomNID, targetUserNID, targetLocal)
|
2020-05-28 12:15:21 +02:00
|
|
|
}
|