Skip to content

Commit

Permalink
Merge pull request #2700 from manuelsc/BIDS-1739/watchlistremove
Browse files Browse the repository at this point in the history
Batch remove from watchlist for App
  • Loading branch information
manuelsc authored Dec 12, 2023
2 parents 3a3c953 + 403e47b commit 27bce22
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/explorer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ func main() {
apiV1AuthRouter.HandleFunc("/validator/{pubkey}/add", handlers.UserValidatorWatchlistAdd).Methods("POST", "OPTIONS")
apiV1AuthRouter.HandleFunc("/validator/{pubkey}/remove", handlers.UserValidatorWatchlistRemove).Methods("POST", "OPTIONS")
apiV1AuthRouter.HandleFunc("/dashboard/save", handlers.UserDashboardWatchlistAdd).Methods("POST", "OPTIONS")
apiV1AuthRouter.HandleFunc("/dashboard/remove", handlers.UserDashboardWatchlistRemove).Methods("POST", "OPTIONS")
apiV1AuthRouter.HandleFunc("/notifications/bundled/subscribe", handlers.MultipleUsersNotificationsSubscribe).Methods("POST", "OPTIONS")
apiV1AuthRouter.HandleFunc("/notifications/bundled/unsubscribe", handlers.MultipleUsersNotificationsUnsubscribe).Methods("POST", "OPTIONS")
apiV1AuthRouter.HandleFunc("/notifications/subscribe", handlers.UserNotificationsSubscribe).Methods("POST", "OPTIONS")
Expand Down
56 changes: 56 additions & 0 deletions handlers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -1538,6 +1538,62 @@ func UserDashboardWatchlistAdd(w http.ResponseWriter, r *http.Request) {
OKResponse(w, r)
}

// UserDashboardWatchlistRemove godoc
// @Summary unsubscribes a user from a specific validator via index from both watchlist and notification events
// @Tags User
// @Produce json
// @Param pubKey body []string true "Index of validator you want to unsubscribe from"
// @Success 200 {object} types.ApiResponse
// @Failure 400 {object} types.ApiResponse
// @Failure 500 {object} types.ApiResponse
// @Security ApiKeyAuth
// @Router /api/v1/user/dashboard/remove [post]
func UserDashboardWatchlistRemove(w http.ResponseWriter, r *http.Request) {
SetAutoContentType(w, r)
user := getUser(r)

body, err := io.ReadAll(r.Body)
if err != nil {
logger.Errorf("error reading body of request: %v, %v", r.URL.String(), err)
ErrorOrJSONResponse(w, r, "Internal server error", http.StatusInternalServerError)
return
}

indices := make([]string, 0)
err = json.Unmarshal(body, &indices)
if err != nil {
logger.Errorf("error parsing request body: %v, %v", r.URL.String(), err)
ErrorOrJSONResponse(w, r, "Internal server error", http.StatusInternalServerError)
return
}
indicesParsed := make([]int64, 0)
for _, i := range indices {
parsed, err := strconv.ParseInt(i, 10, 64)
if err != nil {
logger.Errorf("error could not parse validator indices: %v, %v", r.URL.String(), err)
ErrorOrJSONResponse(w, r, "Internal server error", http.StatusInternalServerError)
return
}
indicesParsed = append(indicesParsed, parsed)
}

publicKeys := make([]string, 0)
db.WriterDb.Select(&publicKeys, `
SELECT pubkeyhex as pubkey
FROM validators
WHERE validatorindex = ANY($1)
`, pq.Int64Array(indicesParsed))

err = db.RemoveFromWatchlistBatch(user.UserID, publicKeys, utils.GetNetwork())
if err != nil {
logger.Errorf("error could not remove validators from watchlist: %v, %v", r.URL.String(), err)
ErrorOrJSONResponse(w, r, "Internal server error", http.StatusInternalServerError)
return
}

OKResponse(w, r)
}

// UserValidatorWatchlistRemove godoc
// @Summary unsubscribes a user from a specific validator
// @Tags User
Expand Down
2 changes: 2 additions & 0 deletions utils/oauthutils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"bytes"
"encoding/hex"
"encoding/json"
"errors"
Expand Down Expand Up @@ -248,6 +249,7 @@ func AuthorizedAPIMiddleware(next http.Handler) http.Handler {
context.Set(r, JsonBodyKey, keyVal)
context.Set(r, JsonBodyNakedKey, body)
}
r.Body = io.NopCloser(bytes.NewReader(body))
}

context.Set(r, ClaimsContextKey, claims)
Expand Down

0 comments on commit 27bce22

Please sign in to comment.