mirror of
https://github.com/element-hq/dendrite.git
synced 2025-09-13 21:02:25 +03:00
Add /_synapse/admin/v1/event_reports
endpoint (#3342)
Based on #3340 This adds a `/_synapse/admin/v1/event_reports` endpoint, the same Synapse has. This way existing tools also work with Dendrite. Given this is already getting huge (even though many test lines), splitting this into two PRs. (The next adds "getting one report" and "deleting reports") [skip ci]
This commit is contained in:
parent
1bdf0cc541
commit
79072c3dcd
11 changed files with 647 additions and 4 deletions
|
@ -19,7 +19,9 @@ import (
|
|||
"database/sql"
|
||||
"time"
|
||||
|
||||
"github.com/matrix-org/dendrite/internal"
|
||||
"github.com/matrix-org/dendrite/internal/sqlutil"
|
||||
"github.com/matrix-org/dendrite/roomserver/api"
|
||||
"github.com/matrix-org/dendrite/roomserver/storage/tables"
|
||||
"github.com/matrix-org/dendrite/roomserver/types"
|
||||
"github.com/matrix-org/gomatrixserverlib/spec"
|
||||
|
@ -32,8 +34,8 @@ CREATE TABLE IF NOT EXISTS roomserver_reported_events
|
|||
id BIGINT PRIMARY KEY DEFAULT nextval('roomserver_reported_events_id_seq'),
|
||||
room_nid BIGINT NOT NULL,
|
||||
event_nid BIGINT NOT NULL,
|
||||
reporting_user_nid INTEGER NOT NULL, -- the user reporting the event
|
||||
event_sender_nid INTEGER NOT NULL, -- the user who sent the reported event
|
||||
reporting_user_nid BIGINT NOT NULL, -- the user reporting the event
|
||||
event_sender_nid BIGINT NOT NULL, -- the user who sent the reported event
|
||||
reason TEXT,
|
||||
score INTEGER,
|
||||
received_ts BIGINT NOT NULL
|
||||
|
@ -45,8 +47,38 @@ const insertReportedEventSQL = `
|
|||
RETURNING id
|
||||
`
|
||||
|
||||
const selectReportedEventsDescSQL = `
|
||||
WITH countReports AS (
|
||||
SELECT count(*) as report_count
|
||||
FROM roomserver_reported_events
|
||||
WHERE ($1::BIGINT IS NULL OR room_nid = $1::BIGINT) AND ($2::TEXT IS NULL OR reporting_user_nid = $2::BIGINT)
|
||||
)
|
||||
SELECT report_count, id, room_nid, event_nid, reporting_user_nid, event_sender_nid, reason, score, received_ts
|
||||
FROM roomserver_reported_events, countReports
|
||||
WHERE ($1::BIGINT IS NULL OR room_nid = $1::BIGINT) AND ($2::TEXT IS NULL OR reporting_user_nid = $2::BIGINT)
|
||||
ORDER BY received_ts DESC
|
||||
OFFSET $3
|
||||
LIMIT $4
|
||||
`
|
||||
|
||||
const selectReportedEventsAscSQL = `
|
||||
WITH countReports AS (
|
||||
SELECT count(*) as report_count
|
||||
FROM roomserver_reported_events
|
||||
WHERE ($1::BIGINT IS NULL OR room_nid = $1::BIGINT) AND ($2::TEXT IS NULL OR reporting_user_nid = $2::BIGINT)
|
||||
)
|
||||
SELECT report_count, id, room_nid, event_nid, reporting_user_nid, event_sender_nid, reason, score, received_ts
|
||||
FROM roomserver_reported_events, countReports
|
||||
WHERE ($1::BIGINT IS NULL OR room_nid = $1::BIGINT) AND ($2::TEXT IS NULL OR reporting_user_nid = $2::BIGINT)
|
||||
ORDER BY received_ts ASC
|
||||
OFFSET $3
|
||||
LIMIT $4
|
||||
`
|
||||
|
||||
type reportedEventsStatements struct {
|
||||
insertReportedEventsStmt *sql.Stmt
|
||||
insertReportedEventsStmt *sql.Stmt
|
||||
selectReportedEventsDescStmt *sql.Stmt
|
||||
selectReportedEventsAscStmt *sql.Stmt
|
||||
}
|
||||
|
||||
func CreateReportedEventsTable(db *sql.DB) error {
|
||||
|
@ -59,6 +91,8 @@ func PrepareReportedEventsTable(db *sql.DB) (tables.ReportedEvents, error) {
|
|||
|
||||
return s, sqlutil.StatementList{
|
||||
{&s.insertReportedEventsStmt, insertReportedEventSQL},
|
||||
{&s.selectReportedEventsDescStmt, selectReportedEventsDescSQL},
|
||||
{&s.selectReportedEventsAscStmt, selectReportedEventsAscSQL},
|
||||
}.Prepare(db)
|
||||
}
|
||||
|
||||
|
@ -86,3 +120,61 @@ func (r *reportedEventsStatements) InsertReportedEvent(
|
|||
).Scan(&reportID)
|
||||
return reportID, err
|
||||
}
|
||||
|
||||
func (r *reportedEventsStatements) SelectReportedEvents(
|
||||
ctx context.Context,
|
||||
txn *sql.Tx,
|
||||
from, limit uint64,
|
||||
backwards bool,
|
||||
reportingUserID types.EventStateKeyNID,
|
||||
roomNID types.RoomNID,
|
||||
) ([]api.QueryAdminEventReportsResponse, int64, error) {
|
||||
var stmt *sql.Stmt
|
||||
if backwards {
|
||||
stmt = sqlutil.TxStmt(txn, r.selectReportedEventsDescStmt)
|
||||
} else {
|
||||
stmt = sqlutil.TxStmt(txn, r.selectReportedEventsAscStmt)
|
||||
}
|
||||
|
||||
var qryRoomNID *types.RoomNID
|
||||
if roomNID > 0 {
|
||||
qryRoomNID = &roomNID
|
||||
}
|
||||
var qryReportingUser *types.EventStateKeyNID
|
||||
if reportingUserID > 0 {
|
||||
qryReportingUser = &reportingUserID
|
||||
}
|
||||
|
||||
rows, err := stmt.QueryContext(ctx,
|
||||
qryRoomNID,
|
||||
qryReportingUser,
|
||||
from,
|
||||
limit,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
defer internal.CloseAndLogIfError(ctx, rows, "SelectReportedEvents: failed to close rows")
|
||||
|
||||
var result []api.QueryAdminEventReportsResponse
|
||||
var row api.QueryAdminEventReportsResponse
|
||||
var count int64
|
||||
for rows.Next() {
|
||||
if err = rows.Scan(
|
||||
&count,
|
||||
&row.ID,
|
||||
&row.RoomNID,
|
||||
&row.EventNID,
|
||||
&row.ReportingUserNID,
|
||||
&row.SenderNID,
|
||||
&row.Reason,
|
||||
&row.Score,
|
||||
&row.ReceivedTS,
|
||||
); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
result = append(result, row)
|
||||
}
|
||||
|
||||
return result, count, rows.Err()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue