From f1e8d19cea3c28116c3d2519cedfbd43d56efc3c Mon Sep 17 00:00:00 2001 From: Till Faelligen Date: Mon, 28 Mar 2022 11:33:12 +0200 Subject: [PATCH 1/3] Fix build --- clientapi/routing/consent_tracking.go | 4 +--- clientapi/routing/routing.go | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/clientapi/routing/consent_tracking.go b/clientapi/routing/consent_tracking.go index 92a31a41a..f61c8d997 100644 --- a/clientapi/routing/consent_tracking.go +++ b/clientapi/routing/consent_tracking.go @@ -29,7 +29,6 @@ import ( "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/dendrite/setup/config" userapi "github.com/matrix-org/dendrite/userapi/api" - userdb "github.com/matrix-org/dendrite/userapi/storage" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" "github.com/sirupsen/logrus" @@ -129,7 +128,6 @@ func sendServerNoticeForConsent(userAPI userapi.UserInternalAPI, rsAPI api.Rooms cfgNotices *config.ServerNotices, cfgClient *config.ClientAPI, senderDevice *userapi.Device, - accountsDB userdb.Database, asAPI appserviceAPI.AppServiceQueryAPI, ) { res := &userapi.QueryOutdatedPolicyResponse{} @@ -180,7 +178,7 @@ func sendServerNoticeForConsent(userAPI userapi.UserInternalAPI, rsAPI api.Rooms Body: msgBody.String(), }, } - _, err = sendServerNotice(context.Background(), req, rsAPI, cfgNotices, cfgClient, senderDevice, accountsDB, asAPI, userAPI, nil, nil, nil) + _, err = sendServerNotice(context.Background(), req, rsAPI, cfgNotices, cfgClient, senderDevice, asAPI, userAPI, nil, nil, nil) if err != nil { logrus.WithError(err).WithField("userID", userID).Error("failed to send server notice for consent to user") continue diff --git a/clientapi/routing/routing.go b/clientapi/routing/routing.go index ddf540069..bac4379a7 100644 --- a/clientapi/routing/routing.go +++ b/clientapi/routing/routing.go @@ -178,7 +178,7 @@ func Setup( logrus.Warnf("Consent tracking is enabled, but server notes are not. No server notice will be sent to users") } else { // start a new go routine to send messages about consent - go sendServerNoticeForConsent(userAPI, rsAPI, &cfg.Matrix.ServerNotices, cfg, serverNotificationSender, accountDB, asAPI) + go sendServerNoticeForConsent(userAPI, rsAPI, &cfg.Matrix.ServerNotices, cfg, serverNotificationSender, asAPI) } publicAPIMux.Handle("/consent", httputil.MakeHTMLAPI("consent", func(writer http.ResponseWriter, request *http.Request) *util.JSONResponse { From c99e3aff1b79270aa5c9a9c1b26e4bfaaa1dde0b Mon Sep 17 00:00:00 2001 From: Till Faelligen Date: Mon, 28 Mar 2022 12:01:15 +0200 Subject: [PATCH 2/3] Fix linter issues --- clientapi/routing/consent_tracking.go | 2 +- clientapi/routing/server_notices.go | 36 ++++++++++++---------- userapi/storage/postgres/accounts_table.go | 5 +-- userapi/storage/sqlite3/accounts_table.go | 5 +-- 4 files changed, 26 insertions(+), 22 deletions(-) diff --git a/clientapi/routing/consent_tracking.go b/clientapi/routing/consent_tracking.go index f61c8d997..c3075e15b 100644 --- a/clientapi/routing/consent_tracking.go +++ b/clientapi/routing/consent_tracking.go @@ -96,7 +96,7 @@ func consent(writer http.ResponseWriter, req *http.Request, userAPI userapi.User } return &internalError } - if err := userAPI.PerformUpdatePolicyVersion( + if err = userAPI.PerformUpdatePolicyVersion( req.Context(), &userapi.UpdatePolicyVersionRequest{ PolicyVersion: data.Version, diff --git a/clientapi/routing/server_notices.go b/clientapi/routing/server_notices.go index effef5774..e86539273 100644 --- a/clientapi/routing/server_notices.go +++ b/clientapi/routing/server_notices.go @@ -107,7 +107,7 @@ func sendServerNotice( return util.JSONResponse{ Code: http.StatusBadRequest, JSON: jsonerror.BadJSON("Invalid request"), - }, nil + }, fmt.Errorf("Invalid JSON") } qryServerNoticeRoom := &userapi.QueryServerNoticeRoomResponse{} @@ -116,11 +116,11 @@ func sendServerNotice( return util.JSONResponse{ Code: http.StatusBadRequest, JSON: jsonerror.BadJSON("Invalid request"), - }, nil + }, fmt.Errorf("Invalid JSON") } err = userAPI.SelectServerNoticeRoomID(ctx, &userapi.QueryServerNoticeRoomRequest{Localpart: localpart}, qryServerNoticeRoom) if err != nil { - return util.ErrorResponse(err), nil + return util.ErrorResponse(err), err } senderUserID := fmt.Sprintf("@%s:%s", cfgNotices.LocalPart, cfgClient.Matrix.ServerName) @@ -129,17 +129,18 @@ func sendServerNotice( // create a new room for the user if qryServerNoticeRoom.RoomID == "" { + var pl, cc []byte powerLevelContent := eventutil.InitialPowerLevelsContent(senderUserID) powerLevelContent.Users[serverNoticeRequest.UserID] = -10 // taken from Synapse - pl, err := json.Marshal(powerLevelContent) + pl, err = json.Marshal(powerLevelContent) if err != nil { - return util.ErrorResponse(err), nil + return util.ErrorResponse(err), err } createContent := map[string]interface{}{} createContent["m.federate"] = false - cc, err := json.Marshal(createContent) + cc, err = json.Marshal(createContent) if err != nil { - return util.ErrorResponse(err), nil + return util.ErrorResponse(err), err } crReq := createRoomRequest{ Invite: []string{serverNoticeRequest.UserID}, @@ -158,7 +159,7 @@ func sendServerNotice( case createRoomResponse: roomID = data.RoomID res := &userapi.UpdateServerNoticeRoomResponse{} - err := userAPI.UpdateServerNoticeRoomID(ctx, &userapi.UpdateServerNoticeRoomRequest{RoomID: roomID, Localpart: localpart}, res) + err = userAPI.UpdateServerNoticeRoomID(ctx, &userapi.UpdateServerNoticeRoomRequest{RoomID: roomID, Localpart: localpart}, res) if err != nil { util.GetLogger(ctx).WithError(err).Error("UpdateServerNoticeRoomID failed") return jsonerror.InternalServerError(), nil @@ -171,25 +172,26 @@ func sendServerNotice( }} if err = saveTagData(ctx, serverNoticeRequest.UserID, roomID, userAPI, serverAlertTag); err != nil { util.GetLogger(ctx).WithError(err).Error("saveTagData failed") - return jsonerror.InternalServerError(), nil + return jsonerror.InternalServerError(), err } default: // if we didn't get a createRoomResponse, we probably received an error, so return that. - return roomRes, nil + return roomRes, fmt.Errorf("Unable to create room") } } else { res := &api.QueryMembershipForUserResponse{} - err := rsAPI.QueryMembershipForUser(ctx, &api.QueryMembershipForUserRequest{UserID: serverNoticeRequest.UserID, RoomID: roomID}, res) + err = rsAPI.QueryMembershipForUser(ctx, &api.QueryMembershipForUserRequest{UserID: serverNoticeRequest.UserID, RoomID: roomID}, res) if err != nil { - return util.ErrorResponse(err), nil + return util.ErrorResponse(err), err } // re-invite the user if res.Membership != gomatrixserverlib.Join { - res, err := sendInvite(ctx, userAPI, senderDevice, roomID, serverNoticeRequest.UserID, "Server notice room", cfgClient, rsAPI, asAPI, time.Now()) + var inviteRes util.JSONResponse + inviteRes, err = sendInvite(ctx, userAPI, senderDevice, roomID, serverNoticeRequest.UserID, "Server notice room", cfgClient, rsAPI, asAPI, time.Now()) if err != nil { - return res, nil + return inviteRes, err } } } @@ -203,7 +205,7 @@ func sendServerNotice( e, resErr := generateSendEvent(ctx, request, senderDevice, roomID, "m.room.message", nil, cfgClient, rsAPI, time.Now()) if resErr != nil { logrus.Errorf("failed to send message: %+v", resErr) - return *resErr, nil + return *resErr, fmt.Errorf("Unable to send event") } timeToGenerateEvent := time.Since(startedGeneratingEvent) @@ -218,7 +220,7 @@ func sendServerNotice( // pass the new event to the roomserver and receive the correct event ID // event ID in case of duplicate transaction is discarded startedSubmittingEvent := time.Now() - if err := api.SendEvents( + if err = api.SendEvents( ctx, rsAPI, api.KindNew, []*gomatrixserverlib.HeaderedEvent{ @@ -230,7 +232,7 @@ func sendServerNotice( false, ); err != nil { util.GetLogger(ctx).WithError(err).Error("SendEvents failed") - return jsonerror.InternalServerError(), nil + return jsonerror.InternalServerError(), err } util.GetLogger(ctx).WithFields(logrus.Fields{ "event_id": e.EventID(), diff --git a/userapi/storage/postgres/accounts_table.go b/userapi/storage/postgres/accounts_table.go index 097fe8998..c7ae8f103 100644 --- a/userapi/storage/postgres/accounts_table.go +++ b/userapi/storage/postgres/accounts_table.go @@ -19,6 +19,7 @@ import ( "database/sql" "time" + "github.com/matrix-org/dendrite/internal" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/dendrite/clientapi/userutil" @@ -232,7 +233,7 @@ func (s *accountsStatements) BatchSelectPrivacyPolicy( stmt = sqlutil.TxStmt(txn, stmt) } rows, err := stmt.QueryContext(ctx, policyVersion) - defer rows.Close() + defer internal.CloseAndLogIfError(ctx, rows, "BatchSelectPrivacyPolicy: rows.close() failed") for rows.Next() { var userID string if err := rows.Scan(&userID); err != nil { @@ -289,4 +290,4 @@ func (s *accountsStatements) UpdateServerNoticeRoomID( } _, err = stmt.ExecContext(ctx, roomID, localpart) return -} \ No newline at end of file +} diff --git a/userapi/storage/sqlite3/accounts_table.go b/userapi/storage/sqlite3/accounts_table.go index 9f7e75226..5fb6b5ffe 100644 --- a/userapi/storage/sqlite3/accounts_table.go +++ b/userapi/storage/sqlite3/accounts_table.go @@ -19,6 +19,7 @@ import ( "database/sql" "time" + "github.com/matrix-org/dendrite/internal" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/dendrite/clientapi/userutil" @@ -235,7 +236,7 @@ func (s *accountsStatements) BatchSelectPrivacyPolicy( if err != nil { return nil, err } - defer rows.Close() + defer internal.CloseAndLogIfError(ctx, rows, "BatchSelectPrivacyPolicy: rows.close() failed") for rows.Next() { var userID string if err := rows.Scan(&userID); err != nil { @@ -292,4 +293,4 @@ func (s *accountsStatements) UpdateServerNoticeRoomID( } _, err = stmt.ExecContext(ctx, roomID, localpart) return -} \ No newline at end of file +} From 2a18023a1a16a3533302c0af1da7a1a3e243465a Mon Sep 17 00:00:00 2001 From: Till Faelligen Date: Mon, 28 Mar 2022 12:13:47 +0200 Subject: [PATCH 3/3] Linter again.. --- clientapi/clientapi.go | 1 - userapi/internal/api.go | 2 +- userapi/storage/shared/storage.go | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/clientapi/clientapi.go b/clientapi/clientapi.go index 5bcab8a00..31a53a706 100644 --- a/clientapi/clientapi.go +++ b/clientapi/clientapi.go @@ -63,4 +63,3 @@ func AddPublicRoutes( extRoomsProvider, mscCfg, ) } - diff --git a/userapi/internal/api.go b/userapi/internal/api.go index f05954834..2763df136 100644 --- a/userapi/internal/api.go +++ b/userapi/internal/api.go @@ -886,4 +886,4 @@ func (a *UserInternalAPI) UpdateServerNoticeRoomID( res *api.UpdateServerNoticeRoomResponse, ) (err error) { return a.DB.UpdateServerNoticeRoomID(ctx, req.Localpart, req.RoomID) -} \ No newline at end of file +} diff --git a/userapi/storage/shared/storage.go b/userapi/storage/shared/storage.go index e69aaf55e..eadf6f816 100644 --- a/userapi/storage/shared/storage.go +++ b/userapi/storage/shared/storage.go @@ -809,4 +809,4 @@ func (d *Database) UpdateServerNoticeRoomID(ctx context.Context, localpart, room return d.Accounts.UpdateServerNoticeRoomID(ctx, txn, localpart, roomID) }) return -} \ No newline at end of file +}