mirror of
https://github.com/hoernschen/dendrite.git
synced 2025-07-29 12:42:46 +00:00
Associate transactions with session IDs instead of device IDs (#789)
This commit is contained in:
parent
5eb63f1d1e
commit
43308d2f3f
9 changed files with 55 additions and 39 deletions
|
@ -75,9 +75,9 @@ type InputRoomEvent struct {
|
|||
}
|
||||
|
||||
// TransactionID contains the transaction ID sent by a client when sending an
|
||||
// event, along with the ID of that device.
|
||||
// event, along with the ID of the client session.
|
||||
type TransactionID struct {
|
||||
DeviceID string `json:"device_id"`
|
||||
SessionID int64 `json:"session_id"`
|
||||
TransactionID string `json:"id"`
|
||||
}
|
||||
|
||||
|
|
|
@ -32,7 +32,7 @@ type RoomEventDatabase interface {
|
|||
StoreEvent(
|
||||
ctx context.Context,
|
||||
event gomatrixserverlib.Event,
|
||||
txnAndDeviceID *api.TransactionID,
|
||||
txnAndSessionID *api.TransactionID,
|
||||
authEventNIDs []types.EventNID,
|
||||
) (types.RoomNID, types.StateAtEvent, error)
|
||||
// Look up the state entries for a list of string event IDs
|
||||
|
@ -67,7 +67,7 @@ type RoomEventDatabase interface {
|
|||
// Returns an empty string if no such event exists.
|
||||
GetTransactionEventID(
|
||||
ctx context.Context, transactionID string,
|
||||
deviceID string, userID string,
|
||||
sessionID int64, userID string,
|
||||
) (string, error)
|
||||
}
|
||||
|
||||
|
@ -100,7 +100,7 @@ func processRoomEvent(
|
|||
if input.TransactionID != nil {
|
||||
tdID := input.TransactionID
|
||||
eventID, err = db.GetTransactionEventID(
|
||||
ctx, tdID.TransactionID, tdID.DeviceID, input.Event.Sender(),
|
||||
ctx, tdID.TransactionID, tdID.SessionID, input.Event.Sender(),
|
||||
)
|
||||
// On error OR event with the transaction already processed/processesing
|
||||
if err != nil || eventID != "" {
|
||||
|
|
|
@ -47,7 +47,7 @@ func Open(dataSourceName string) (*Database, error) {
|
|||
// StoreEvent implements input.EventDatabase
|
||||
func (d *Database) StoreEvent(
|
||||
ctx context.Context, event gomatrixserverlib.Event,
|
||||
txnAndDeviceID *api.TransactionID, authEventNIDs []types.EventNID,
|
||||
txnAndSessionID *api.TransactionID, authEventNIDs []types.EventNID,
|
||||
) (types.RoomNID, types.StateAtEvent, error) {
|
||||
var (
|
||||
roomNID types.RoomNID
|
||||
|
@ -58,10 +58,10 @@ func (d *Database) StoreEvent(
|
|||
err error
|
||||
)
|
||||
|
||||
if txnAndDeviceID != nil {
|
||||
if txnAndSessionID != nil {
|
||||
if err = d.statements.insertTransaction(
|
||||
ctx, txnAndDeviceID.TransactionID,
|
||||
txnAndDeviceID.DeviceID, event.Sender(), event.EventID(),
|
||||
ctx, txnAndSessionID.TransactionID,
|
||||
txnAndSessionID.SessionID, event.Sender(), event.EventID(),
|
||||
); err != nil {
|
||||
return 0, types.StateAtEvent{}, err
|
||||
}
|
||||
|
@ -322,9 +322,9 @@ func (d *Database) GetLatestEventsForUpdate(
|
|||
// GetTransactionEventID implements input.EventDatabase
|
||||
func (d *Database) GetTransactionEventID(
|
||||
ctx context.Context, transactionID string,
|
||||
deviceID string, userID string,
|
||||
sessionID int64, userID string,
|
||||
) (string, error) {
|
||||
eventID, err := d.statements.selectTransactionEventID(ctx, transactionID, deviceID, userID)
|
||||
eventID, err := d.statements.selectTransactionEventID(ctx, transactionID, sessionID, userID)
|
||||
if err == sql.ErrNoRows {
|
||||
return "", nil
|
||||
}
|
||||
|
|
|
@ -23,8 +23,8 @@ const transactionsSchema = `
|
|||
CREATE TABLE IF NOT EXISTS roomserver_transactions (
|
||||
-- The transaction ID of the event.
|
||||
transaction_id TEXT NOT NULL,
|
||||
-- The device ID of the originating transaction.
|
||||
device_id TEXT NOT NULL,
|
||||
-- The session ID of the originating transaction.
|
||||
session_id BIGINT NOT NULL,
|
||||
-- User ID of the sender who authored the event
|
||||
user_id TEXT NOT NULL,
|
||||
-- Event ID corresponding to the transaction
|
||||
|
@ -32,16 +32,16 @@ CREATE TABLE IF NOT EXISTS roomserver_transactions (
|
|||
event_id TEXT NOT NULL,
|
||||
-- A transaction ID is unique for a user and device
|
||||
-- This automatically creates an index.
|
||||
PRIMARY KEY (transaction_id, device_id, user_id)
|
||||
PRIMARY KEY (transaction_id, session_id, user_id)
|
||||
);
|
||||
`
|
||||
const insertTransactionSQL = "" +
|
||||
"INSERT INTO roomserver_transactions (transaction_id, device_id, user_id, event_id)" +
|
||||
"INSERT INTO roomserver_transactions (transaction_id, session_id, user_id, event_id)" +
|
||||
" VALUES ($1, $2, $3, $4)"
|
||||
|
||||
const selectTransactionEventIDSQL = "" +
|
||||
"SELECT event_id FROM roomserver_transactions" +
|
||||
" WHERE transaction_id = $1 AND device_id = $2 AND user_id = $3"
|
||||
" WHERE transaction_id = $1 AND session_id = $2 AND user_id = $3"
|
||||
|
||||
type transactionStatements struct {
|
||||
insertTransactionStmt *sql.Stmt
|
||||
|
@ -63,12 +63,12 @@ func (s *transactionStatements) prepare(db *sql.DB) (err error) {
|
|||
func (s *transactionStatements) insertTransaction(
|
||||
ctx context.Context,
|
||||
transactionID string,
|
||||
deviceID string,
|
||||
sessionID int64,
|
||||
userID string,
|
||||
eventID string,
|
||||
) (err error) {
|
||||
_, err = s.insertTransactionStmt.ExecContext(
|
||||
ctx, transactionID, deviceID, userID, eventID,
|
||||
ctx, transactionID, sessionID, userID, eventID,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
@ -76,11 +76,11 @@ func (s *transactionStatements) insertTransaction(
|
|||
func (s *transactionStatements) selectTransactionEventID(
|
||||
ctx context.Context,
|
||||
transactionID string,
|
||||
deviceID string,
|
||||
sessionID int64,
|
||||
userID string,
|
||||
) (eventID string, err error) {
|
||||
err = s.selectTransactionEventIDStmt.QueryRowContext(
|
||||
ctx, transactionID, deviceID, userID,
|
||||
ctx, transactionID, sessionID, userID,
|
||||
).Scan(&eventID)
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue