2020-11-09 18:46:11 +00:00
|
|
|
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/lib/pq"
|
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/eduserver/api"
|
|
|
|
"github.com/matrix-org/dendrite/internal"
|
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage/tables"
|
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
)
|
|
|
|
|
|
|
|
const receiptsSchema = `
|
2020-12-11 14:02:17 +00:00
|
|
|
CREATE SEQUENCE IF NOT EXISTS syncapi_receipt_id;
|
|
|
|
|
2020-11-09 18:46:11 +00:00
|
|
|
-- Stores data about receipts
|
|
|
|
CREATE TABLE IF NOT EXISTS syncapi_receipts (
|
|
|
|
-- The ID
|
2020-12-11 14:02:17 +00:00
|
|
|
id BIGINT PRIMARY KEY DEFAULT nextval('syncapi_receipt_id'),
|
2020-11-09 18:46:11 +00:00
|
|
|
room_id TEXT NOT NULL,
|
|
|
|
receipt_type TEXT NOT NULL,
|
|
|
|
user_id TEXT NOT NULL,
|
|
|
|
event_id TEXT NOT NULL,
|
|
|
|
receipt_ts BIGINT NOT NULL,
|
|
|
|
CONSTRAINT syncapi_receipts_unique UNIQUE (room_id, receipt_type, user_id)
|
|
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS syncapi_receipts_room_id ON syncapi_receipts(room_id);
|
|
|
|
`
|
|
|
|
|
|
|
|
const upsertReceipt = "" +
|
|
|
|
"INSERT INTO syncapi_receipts" +
|
|
|
|
" (room_id, receipt_type, user_id, event_id, receipt_ts)" +
|
|
|
|
" VALUES ($1, $2, $3, $4, $5)" +
|
|
|
|
" ON CONFLICT (room_id, receipt_type, user_id)" +
|
2020-12-11 14:02:17 +00:00
|
|
|
" DO UPDATE SET id = nextval('syncapi_receipt_id'), event_id = $4, receipt_ts = $5" +
|
2020-11-09 18:46:11 +00:00
|
|
|
" RETURNING id"
|
|
|
|
|
|
|
|
const selectRoomReceipts = "" +
|
2020-12-18 11:11:21 +00:00
|
|
|
"SELECT id, room_id, receipt_type, user_id, event_id, receipt_ts" +
|
2020-11-09 18:46:11 +00:00
|
|
|
" FROM syncapi_receipts" +
|
|
|
|
" WHERE room_id = ANY($1) AND id > $2"
|
|
|
|
|
2020-12-11 14:02:17 +00:00
|
|
|
const selectMaxReceiptIDSQL = "" +
|
|
|
|
"SELECT MAX(id) FROM syncapi_receipts"
|
|
|
|
|
2020-11-09 18:46:11 +00:00
|
|
|
type receiptStatements struct {
|
|
|
|
db *sql.DB
|
|
|
|
upsertReceipt *sql.Stmt
|
|
|
|
selectRoomReceipts *sql.Stmt
|
2020-12-11 14:02:17 +00:00
|
|
|
selectMaxReceiptID *sql.Stmt
|
2020-11-09 18:46:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewPostgresReceiptsTable(db *sql.DB) (tables.Receipts, error) {
|
|
|
|
_, err := db.Exec(receiptsSchema)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r := &receiptStatements{
|
|
|
|
db: db,
|
|
|
|
}
|
|
|
|
if r.upsertReceipt, err = db.Prepare(upsertReceipt); err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to prepare upsertReceipt statement: %w", err)
|
|
|
|
}
|
|
|
|
if r.selectRoomReceipts, err = db.Prepare(selectRoomReceipts); err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to prepare selectRoomReceipts statement: %w", err)
|
|
|
|
}
|
2020-12-11 14:02:17 +00:00
|
|
|
if r.selectMaxReceiptID, err = db.Prepare(selectMaxReceiptIDSQL); err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to prepare selectRoomReceipts statement: %w", err)
|
|
|
|
}
|
2020-11-09 18:46:11 +00:00
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *receiptStatements) UpsertReceipt(ctx context.Context, txn *sql.Tx, roomId, receiptType, userId, eventId string, timestamp gomatrixserverlib.Timestamp) (pos types.StreamPosition, err error) {
|
|
|
|
stmt := sqlutil.TxStmt(txn, r.upsertReceipt)
|
|
|
|
err = stmt.QueryRowContext(ctx, roomId, receiptType, userId, eventId, timestamp).Scan(&pos)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-18 11:11:21 +00:00
|
|
|
func (r *receiptStatements) SelectRoomReceiptsAfter(ctx context.Context, roomIDs []string, streamPos types.StreamPosition) (types.StreamPosition, []api.OutputReceiptEvent, error) {
|
2021-01-08 16:59:06 +00:00
|
|
|
lastPos := streamPos
|
2020-11-09 18:46:11 +00:00
|
|
|
rows, err := r.selectRoomReceipts.QueryContext(ctx, pq.Array(roomIDs), streamPos)
|
|
|
|
if err != nil {
|
2020-12-18 11:11:21 +00:00
|
|
|
return 0, nil, fmt.Errorf("unable to query room receipts: %w", err)
|
2020-11-09 18:46:11 +00:00
|
|
|
}
|
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "SelectRoomReceiptsAfter: rows.close() failed")
|
|
|
|
var res []api.OutputReceiptEvent
|
|
|
|
for rows.Next() {
|
|
|
|
r := api.OutputReceiptEvent{}
|
2020-12-18 11:11:21 +00:00
|
|
|
var id types.StreamPosition
|
|
|
|
err = rows.Scan(&id, &r.RoomID, &r.Type, &r.UserID, &r.EventID, &r.Timestamp)
|
2020-11-09 18:46:11 +00:00
|
|
|
if err != nil {
|
2020-12-18 11:11:21 +00:00
|
|
|
return 0, res, fmt.Errorf("unable to scan row to api.Receipts: %w", err)
|
2020-11-09 18:46:11 +00:00
|
|
|
}
|
|
|
|
res = append(res, r)
|
2020-12-18 11:11:21 +00:00
|
|
|
if id > lastPos {
|
|
|
|
lastPos = id
|
|
|
|
}
|
2020-11-09 18:46:11 +00:00
|
|
|
}
|
2020-12-18 11:11:21 +00:00
|
|
|
return lastPos, res, rows.Err()
|
2020-11-09 18:46:11 +00:00
|
|
|
}
|
2020-12-11 14:02:17 +00:00
|
|
|
|
|
|
|
func (s *receiptStatements) SelectMaxReceiptID(
|
|
|
|
ctx context.Context, txn *sql.Tx,
|
|
|
|
) (id int64, err error) {
|
|
|
|
var nullableID sql.NullInt64
|
|
|
|
stmt := sqlutil.TxStmt(txn, s.selectMaxReceiptID)
|
|
|
|
err = stmt.QueryRowContext(ctx).Scan(&nullableID)
|
|
|
|
if nullableID.Valid {
|
|
|
|
id = nullableID.Int64
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|