Use NIDs instead of strings, add event_sender_nid

This commit is contained in:
Till Faelligen 2024-03-20 07:42:42 +01:00
parent e1622798ac
commit 6e57553480
No known key found for this signature in database
GPG key ID: 3DF82D8AB9211D4E
4 changed files with 48 additions and 22 deletions

View file

@ -29,18 +29,19 @@ const reportedEventsScheme = `
CREATE SEQUENCE IF NOT EXISTS roomserver_reported_events_id_seq; CREATE SEQUENCE IF NOT EXISTS roomserver_reported_events_id_seq;
CREATE TABLE IF NOT EXISTS roomserver_reported_events CREATE TABLE IF NOT EXISTS roomserver_reported_events
( (
id BIGINT PRIMARY KEY DEFAULT nextval('roomserver_reported_events_id_seq'), id BIGINT PRIMARY KEY DEFAULT nextval('roomserver_reported_events_id_seq'),
room_nid BIGINT NOT NULL, room_nid BIGINT NOT NULL,
event_nid BIGINT NOT NULL, event_nid BIGINT NOT NULL,
user_id TEXT NOT NULL, reporting_user_nid INTEGER NOT NULL, -- the user reporting the event
reason TEXT, event_sender_nid INTEGER NOT NULL, -- the user who sent the reported event
score INTEGER, reason TEXT,
received_ts BIGINT NOT NULL score INTEGER,
received_ts BIGINT NOT NULL
);` );`
const insertReportedEventSQL = ` const insertReportedEventSQL = `
INSERT INTO roomserver_reported_events (room_nid, event_nid, user_id, reason, score, received_ts) INSERT INTO roomserver_reported_events (room_nid, event_nid, reporting_user_nid, event_sender_nid, reason, score, received_ts)
VALUES ($1, $2, $3, $4, $5, $6) VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING id RETURNING id
` `
@ -66,7 +67,8 @@ func (r *reportedEventsStatements) InsertReportedEvent(
txn *sql.Tx, txn *sql.Tx,
roomNID types.RoomNID, roomNID types.RoomNID,
eventNID types.EventNID, eventNID types.EventNID,
reportingUserID string, reportingUserID types.EventStateKeyNID,
eventSenderID types.EventStateKeyNID,
reason string, reason string,
score int64, score int64,
) (int64, error) { ) (int64, error) {
@ -77,6 +79,7 @@ func (r *reportedEventsStatements) InsertReportedEvent(
roomNID, roomNID,
eventNID, eventNID,
reportingUserID, reportingUserID,
eventSenderID,
reason, reason,
score, score,
spec.AsTimestamp(time.Now()), spec.AsTimestamp(time.Now()),

View file

@ -1905,9 +1905,28 @@ func (d *Database) InsertReportedEvent(
return 0, fmt.Errorf("unable to find requested event") return 0, fmt.Errorf("unable to find requested event")
} }
stateKeyNIDs, err := d.EventStateKeyNIDs(ctx, []string{reportingUserID, events[0].SenderID().ToUserID().String()})
if err != nil {
return 0, fmt.Errorf("failed to query eventStateKeyNIDs: %w", err)
}
// We expect exactly 2 stateKeyNIDs
if len(stateKeyNIDs) != 2 {
return 0, fmt.Errorf("expected 2 stateKeyNIDs, received %d", len(stateKeyNIDs))
}
var reportID int64 var reportID int64
err = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error { err = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
reportID, err = d.ReportedEventsTable.InsertReportedEvent(ctx, txn, roomInfo.RoomNID, events[0].EventNID, reportingUserID, reason, score) reportID, err = d.ReportedEventsTable.InsertReportedEvent(
ctx,
txn,
roomInfo.RoomNID,
events[0].EventNID,
stateKeyNIDs[reportingUserID],
stateKeyNIDs[events[0].SenderID().ToUserID().String()],
reason,
score,
)
if err != nil { if err != nil {
return err return err
} }

View file

@ -28,18 +28,19 @@ import (
const reportedEventsScheme = ` const reportedEventsScheme = `
CREATE TABLE IF NOT EXISTS roomserver_reported_events CREATE TABLE IF NOT EXISTS roomserver_reported_events
( (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
room_nid INTEGER NOT NULL, room_nid INTEGER NOT NULL,
event_nid INTEGER NOT NULL, event_nid INTEGER NOT NULL,
user_id TEXT NOT NULL, reporting_user_nid INTEGER NOT NULL, -- the user reporting the event
reason TEXT, event_sender_nid INTEGER NOT NULL, -- the user who sent the reported event
score INTEGER, reason TEXT,
received_ts INTEGER NOT NULL score INTEGER,
received_ts INTEGER NOT NULL
);` );`
const insertReportedEventSQL = ` const insertReportedEventSQL = `
INSERT INTO roomserver_reported_events (room_nid, event_nid, user_id, reason, score, received_ts) INSERT INTO roomserver_reported_events (room_nid, event_nid, reporting_user_nid, event_sender_nid, reason, score, received_ts)
VALUES ($1, $2, $3, $4, $5, $6) VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING id RETURNING id
` `
@ -65,7 +66,8 @@ func (r *reportedEventsStatements) InsertReportedEvent(
txn *sql.Tx, txn *sql.Tx,
roomNID types.RoomNID, roomNID types.RoomNID,
eventNID types.EventNID, eventNID types.EventNID,
reportingUserID string, reportingUserID types.EventStateKeyNID,
eventSenderID types.EventStateKeyNID,
reason string, reason string,
score int64, score int64,
) (int64, error) { ) (int64, error) {
@ -76,6 +78,7 @@ func (r *reportedEventsStatements) InsertReportedEvent(
roomNID, roomNID,
eventNID, eventNID,
reportingUserID, reportingUserID,
eventSenderID,
reason, reason,
score, score,
spec.AsTimestamp(time.Now()), spec.AsTimestamp(time.Now()),

View file

@ -133,7 +133,8 @@ type ReportedEvents interface {
txn *sql.Tx, txn *sql.Tx,
roomNID types.RoomNID, roomNID types.RoomNID,
eventNID types.EventNID, eventNID types.EventNID,
reportingUserID string, reportingUserID types.EventStateKeyNID,
eventSenderID types.EventStateKeyNID,
reason string, reason string,
score int64, score int64,
) (int64, error) ) (int64, error)