mirror of
https://github.com/element-hq/dendrite.git
synced 2025-09-13 21:02:25 +03:00
Add event reporting (#3340)
Part of #3216 and #3226 There will be a follow up PR which is going to add the same admin endpoints Synapse has, so existing tools also work for Dendrite.
This commit is contained in:
parent
de95499178
commit
b9abbf7b20
12 changed files with 474 additions and 1 deletions
|
@ -61,6 +61,7 @@ type EventDatabase struct {
|
|||
EventStateKeysTable tables.EventStateKeys
|
||||
PrevEventsTable tables.PreviousEvents
|
||||
RedactionsTable tables.Redactions
|
||||
ReportedEventsTable tables.ReportedEvents
|
||||
}
|
||||
|
||||
func (d *Database) SupportsConcurrentRoomInputs() bool {
|
||||
|
@ -1882,6 +1883,59 @@ func (d *Database) SelectUserIDsForPublicKeys(ctx context.Context, publicKeys ma
|
|||
return result, err
|
||||
}
|
||||
|
||||
// InsertReportedEvent stores a reported event.
|
||||
func (d *Database) InsertReportedEvent(
|
||||
ctx context.Context,
|
||||
roomID, eventID, reportingUserID, reason string,
|
||||
score int64,
|
||||
) (int64, error) {
|
||||
roomInfo, err := d.roomInfo(ctx, nil, roomID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if roomInfo == nil {
|
||||
return 0, fmt.Errorf("room does not exist")
|
||||
}
|
||||
|
||||
events, err := d.eventsFromIDs(ctx, nil, roomInfo, []string{eventID}, NoFilter)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if len(events) == 0 {
|
||||
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
|
||||
err = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
return reportID, err
|
||||
}
|
||||
|
||||
// FIXME TODO: Remove all this - horrible dupe with roomserver/state. Can't use the original impl because of circular loops
|
||||
// it should live in this package!
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue