aboutsummaryrefslogtreecommitdiff
path: root/clientapi
diff options
context:
space:
mode:
authorTill <2353100+S7evinK@users.noreply.github.com>2024-03-21 19:27:34 +0100
committerGitHub <noreply@github.com>2024-03-21 19:27:34 +0100
commitb9abbf7b20b4faaffe754c4a1ea4d5f0e7bd72b9 (patch)
tree36509a7245746cf95f82d581dad0a5f79b0b47da /clientapi
parentde954991787053b97936b8476d9a68fc29c289ae (diff)
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.
Diffstat (limited to 'clientapi')
-rw-r--r--clientapi/clientapi_test.go89
-rw-r--r--clientapi/routing/report_event.go93
-rw-r--r--clientapi/routing/routing.go10
3 files changed, 192 insertions, 0 deletions
diff --git a/clientapi/clientapi_test.go b/clientapi/clientapi_test.go
index fffe4b6b..c550b208 100644
--- a/clientapi/clientapi_test.go
+++ b/clientapi/clientapi_test.go
@@ -2346,3 +2346,92 @@ func TestCreateRoomInvite(t *testing.T) {
}
})
}
+
+func TestReportEvent(t *testing.T) {
+ alice := test.NewUser(t)
+ bob := test.NewUser(t)
+ charlie := test.NewUser(t)
+ room := test.NewRoom(t, alice)
+
+ room.CreateAndInsert(t, charlie, spec.MRoomMember, map[string]interface{}{
+ "membership": "join",
+ }, test.WithStateKey(charlie.ID))
+ eventToReport := room.CreateAndInsert(t, alice, "m.room.message", map[string]interface{}{"body": "hello world"})
+
+ test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
+ cfg, processCtx, close := testrig.CreateConfig(t, dbType)
+ routers := httputil.NewRouters()
+ cm := sqlutil.NewConnectionManager(processCtx, cfg.Global.DatabaseOptions)
+ caches := caching.NewRistrettoCache(128*1024*1024, time.Hour, caching.DisableMetrics)
+ defer close()
+ natsInstance := jetstream.NATSInstance{}
+ jsctx, _ := natsInstance.Prepare(processCtx, &cfg.Global.JetStream)
+ defer jetstream.DeleteAllStreams(jsctx, &cfg.Global.JetStream)
+
+ // Use an actual roomserver for this
+ rsAPI := roomserver.NewInternalAPI(processCtx, cfg, cm, &natsInstance, caches, caching.DisableMetrics)
+ rsAPI.SetFederationAPI(nil, nil)
+ userAPI := userapi.NewInternalAPI(processCtx, cfg, cm, &natsInstance, rsAPI, nil, caching.DisableMetrics, testIsBlacklistedOrBackingOff)
+
+ if err := api.SendEvents(context.Background(), rsAPI, api.KindNew, room.Events(), "test", "test", "test", nil, false); err != nil {
+ t.Fatalf("failed to send events: %v", err)
+ }
+
+ // We mostly need the rsAPI for this test, so nil for other APIs/caches etc.
+ AddPublicRoutes(processCtx, routers, cfg, &natsInstance, nil, rsAPI, nil, nil, nil, userAPI, nil, nil, caching.DisableMetrics)
+
+ accessTokens := map[*test.User]userDevice{
+ alice: {},
+ bob: {},
+ charlie: {},
+ }
+ createAccessTokens(t, accessTokens, userAPI, processCtx.Context(), routers)
+
+ reqBody := map[string]any{
+ "reason": "baaad",
+ "score": -100,
+ }
+ body, err := json.Marshal(reqBody)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ w := httptest.NewRecorder()
+
+ var req *http.Request
+ t.Run("Bob is not joined and should not be able to report the event", func(t *testing.T) {
+ req = httptest.NewRequest(http.MethodPost, fmt.Sprintf("/_matrix/client/v3/rooms/%s/report/%s", room.ID, eventToReport.EventID()), strings.NewReader(string(body)))
+ req.Header.Set("Authorization", "Bearer "+accessTokens[bob].accessToken)
+
+ routers.Client.ServeHTTP(w, req)
+
+ if w.Code != http.StatusNotFound {
+ t.Fatalf("expected report to fail, got HTTP %d instead: %s", w.Code, w.Body.String())
+ }
+ })
+
+ t.Run("Charlie is joined but the event does not exist", func(t *testing.T) {
+ w = httptest.NewRecorder()
+ req = httptest.NewRequest(http.MethodPost, fmt.Sprintf("/_matrix/client/v3/rooms/%s/report/$doesNotExist", room.ID), strings.NewReader(string(body)))
+ req.Header.Set("Authorization", "Bearer "+accessTokens[charlie].accessToken)
+
+ routers.Client.ServeHTTP(w, req)
+
+ if w.Code != http.StatusNotFound {
+ t.Fatalf("expected report to fail, got HTTP %d instead: %s", w.Code, w.Body.String())
+ }
+ })
+
+ t.Run("Charlie is joined and allowed to report the event", func(t *testing.T) {
+ w = httptest.NewRecorder()
+ req = httptest.NewRequest(http.MethodPost, fmt.Sprintf("/_matrix/client/v3/rooms/%s/report/%s", room.ID, eventToReport.EventID()), strings.NewReader(string(body)))
+ req.Header.Set("Authorization", "Bearer "+accessTokens[charlie].accessToken)
+
+ routers.Client.ServeHTTP(w, req)
+
+ if w.Code != http.StatusOK {
+ t.Fatalf("expected report to be successful, got HTTP %d instead: %s", w.Code, w.Body.String())
+ }
+ })
+ })
+}
diff --git a/clientapi/routing/report_event.go b/clientapi/routing/report_event.go
new file mode 100644
index 00000000..4dc6498d
--- /dev/null
+++ b/clientapi/routing/report_event.go
@@ -0,0 +1,93 @@
+// Copyright 2023 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 routing
+
+import (
+ "net/http"
+
+ "github.com/matrix-org/dendrite/clientapi/httputil"
+ "github.com/matrix-org/dendrite/roomserver/api"
+ userAPI "github.com/matrix-org/dendrite/userapi/api"
+ "github.com/matrix-org/gomatrixserverlib/spec"
+ "github.com/matrix-org/util"
+)
+
+type reportEventRequest struct {
+ Reason string `json:"reason"`
+ Score int64 `json:"score"`
+}
+
+func ReportEvent(
+ req *http.Request,
+ device *userAPI.Device,
+ roomID, eventID string,
+ rsAPI api.ClientRoomserverAPI,
+) util.JSONResponse {
+ defer req.Body.Close() // nolint: errcheck
+
+ deviceUserID, err := spec.NewUserID(device.UserID, true)
+ if err != nil {
+ return util.JSONResponse{
+ Code: http.StatusForbidden,
+ JSON: spec.NotFound("You don't have permission to report this event, bad userID"),
+ }
+ }
+ // The requesting user must be a member of the room
+ errRes := checkMemberInRoom(req.Context(), rsAPI, *deviceUserID, roomID)
+ if errRes != nil {
+ return util.JSONResponse{
+ Code: http.StatusNotFound, // Spec demands this...
+ JSON: spec.NotFound("The event was not found or you are not joined to the room."),
+ }
+ }
+
+ // Parse the request
+ report := reportEventRequest{}
+ if resErr := httputil.UnmarshalJSONRequest(req, &report); resErr != nil {
+ return *resErr
+ }
+
+ queryRes := &api.QueryEventsByIDResponse{}
+ if err = rsAPI.QueryEventsByID(req.Context(), &api.QueryEventsByIDRequest{
+ RoomID: roomID,
+ EventIDs: []string{eventID},
+ }, queryRes); err != nil {
+ return util.JSONResponse{
+ Code: http.StatusInternalServerError,
+ JSON: spec.InternalServerError{Err: err.Error()},
+ }
+ }
+
+ // No event was found or it was already redacted
+ if len(queryRes.Events) == 0 || queryRes.Events[0].Redacted() {
+ return util.JSONResponse{
+ Code: http.StatusNotFound,
+ JSON: spec.NotFound("The event was not found or you are not joined to the room."),
+ }
+ }
+
+ _, err = rsAPI.InsertReportedEvent(req.Context(), roomID, eventID, device.UserID, report.Reason, report.Score)
+ if err != nil {
+ return util.JSONResponse{
+ Code: http.StatusInternalServerError,
+ JSON: spec.InternalServerError{Err: err.Error()},
+ }
+ }
+
+ return util.JSONResponse{
+ Code: http.StatusOK,
+ JSON: struct{}{},
+ }
+}
diff --git a/clientapi/routing/routing.go b/clientapi/routing/routing.go
index 3e23ab40..40e59822 100644
--- a/clientapi/routing/routing.go
+++ b/clientapi/routing/routing.go
@@ -1523,4 +1523,14 @@ func Setup(
return GetJoinedMembers(req, device, vars["roomID"], rsAPI)
}),
).Methods(http.MethodGet, http.MethodOptions)
+
+ v3mux.Handle("/rooms/{roomID}/report/{eventID}",
+ httputil.MakeAuthAPI("report_event", userAPI, func(req *http.Request, device *userapi.Device) util.JSONResponse {
+ vars, err := httputil.URLDecodeMapValues(mux.Vars(req))
+ if err != nil {
+ return util.ErrorResponse(err)
+ }
+ return ReportEvent(req, device, vars["roomID"], vars["eventID"], rsAPI)
+ }),
+ ).Methods(http.MethodPost, http.MethodOptions)
}