@@ -6,17 +6,14 @@ import (
66 "fmt"
77 "html"
88 "log"
9- "net/url"
109 "slices"
1110 "sync"
12- "testing"
1311 "time"
1412
15- "github.com/matrix-org/policyserv/config"
16- "github.com/matrix-org/policyserv/filter/audit"
13+ "github.com/matrix-org/gomatrixserverlib"
1714 "github.com/matrix-org/policyserv/filter/classification"
1815 "github.com/matrix-org/policyserv/filter/confidence"
19- "github.com/matrix-org/gomatrixserverlib "
16+ "github.com/matrix-org/policyserv/notifiers "
2017)
2118
2219type auditContext struct {
@@ -25,26 +22,26 @@ type auditContext struct {
2522 FinalVectors confidence.Vectors
2623 IncrementalVectors []confidence.Vectors
2724 FilterResponses map [string ][]classification.Classification
28- WebhookUrl string
25+ CommunityId string
2926
30- lock sync.Mutex // use a lock instead of a sync.Map because sync.Map doesn't support generics (and library support appears lacking in quality)
31- instanceConfig * config. InstanceConfig
27+ lock sync.Mutex // use a lock instead of a sync.Map because sync.Map doesn't support generics (and library support appears lacking in quality)
28+ notifier notifiers. MatrixNotifier
3229}
3330
34- func newAuditContext (instanceConfig * config. InstanceConfig , event gomatrixserverlib.PDU , webhookUrl string ) (* auditContext , error ) {
31+ func newAuditContext (notifier notifiers. MatrixNotifier , communityId string , event gomatrixserverlib.PDU ) (* auditContext , error ) {
3532 return & auditContext {
3633 Event : event ,
3734 FilterResponses : make (map [string ][]classification.Classification ),
3835 IncrementalVectors : make ([]confidence.Vectors , 0 ),
39- WebhookUrl : webhookUrl ,
36+ CommunityId : communityId ,
4037
4138 // Populated later
4239 IsSpam : false ,
4340 FinalVectors : nil ,
4441
4542 // Internal
46- lock : sync.Mutex {},
47- instanceConfig : instanceConfig ,
43+ lock : sync.Mutex {},
44+ notifier : notifier ,
4845 }, nil
4946}
5047
@@ -60,32 +57,18 @@ func (c *auditContext) AppendSetGroupVectors(vectors confidence.Vectors) {
6057 c .IncrementalVectors = append (c .IncrementalVectors , vectors )
6158}
6259
63- func (c * auditContext ) Publish (workQueue * audit. Queue ) error {
60+ func (c * auditContext ) Publish () error {
6461 c .lock .Lock ()
6562 defer c .lock .Unlock ()
6663
6764 // Note: we log the audit context so if the webhook fails (or isn't configured) then we
6865 // have an idea of what happened.
6966 log .Printf ("[%s | %s | %s] Audit publish: %#v" , c .Event .EventID (), c .Event .RoomID (), c .Event .SenderID (), c )
7067
71- if c . WebhookUrl == "" || ! c .IsSpam {
68+ if ! c .IsSpam {
7269 return nil // nothing to publish
7370 }
7471
75- // Validate URL
76- whUrl , err := url .Parse (c .WebhookUrl )
77- if err != nil {
78- return err
79- }
80- if ! testing .Testing () {
81- if whUrl .Scheme != "https" {
82- return fmt .Errorf ("webhook URL must be HTTPS" )
83- }
84- }
85- if ! slices .Contains (c .instanceConfig .AllowedWebhookDomains , whUrl .Host ) {
86- return fmt .Errorf ("webhook URL host not allowed" )
87- }
88-
8972 respsJson , err := json .MarshalIndent (c .FilterResponses , "" , " " )
9073 if err != nil {
9174 return err // "should never happen"
@@ -116,5 +99,15 @@ func (c *auditContext) Publish(workQueue *audit.Queue) error {
11699 htmlAudit += "</details>" // close the details block from earlier
117100 }
118101
119- return workQueue .Submit (c .Event .EventID (), htmlAudit , whUrl .String ())
102+ // we don't html2text this because long events can cause hookshot to only show text versions, making all
103+ // of our work to contain the spam to a <details> block useless. We still put some sort of message here
104+ // though so clients which don't support HTML can still see something useful.
105+ textAudit := "This event requires HTML."
106+
107+ msgId , err := c .notifier .Send (c .CommunityId , textAudit , htmlAudit )
108+ if err != nil {
109+ return fmt .Errorf ("failed to send audit message: %w" , err )
110+ }
111+ log .Printf ("[%s | %s | %s] Audit message sent: %s" , c .Event .EventID (), c .Event .RoomID (), c .Event .SenderID (), msgId )
112+ return nil
120113}
0 commit comments