-
-
Notifications
You must be signed in to change notification settings - Fork 119
feat(tracker-rules): add per-tracker limits and services page #630
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
d339da4
feat(tracker-rules): add per-tracker limits and services page
s0up4200 8d2b3f3
fix test
s0up4200 c9a6fb9
fix(services): harden tracker rule updates, reannounce debounce, and …
s0up4200 8baf753
Merge branch 'main' into feat/tracker-rules-services-page
s0up4200 3e7f5c8
fix(reannounce): use consistent debounce window for cooldown checks
s0up4200 d3ed6db
Merge branch 'main' into feat/tracker-rules-services-page
s0up4200 fad9e4f
fix(docs): clarify reannounce and tracker rules
s0up4200 6afda8b
typo
s0up4200 d8f8f3f
typo
s0up4200 38e6f18
docs(reannounce): update quick start and activity log instructions fo…
s0up4200 120d3f4
Merge branch 'main' into feat/tracker-rules-services-page
s0up4200 a1391ef
fix(web): preserve connection status when updating instance in cache …
s0up4200 bd8b4bb
fix(web): indent subcategories in MultiSelect category options
s0up4200 2669fff
fix(reannounce): enforce InitialWaitSeconds filtering on monitored to…
s0up4200 82f0b17
fix(web): stop preserving stale connectionStatus in instance cache up…
s0up4200 ade7a15
feat(tracker-rules): add enabled field to allow disabling rules witho…
s0up4200 fb6a03c
refactor(tracker-rules): remove default rule feature
s0up4200 5d8192b
fix(tracker-rules): prevent cross-instance data leak in Update
s0up4200 16cf1f0
fix(models): check rows.Err() after iteration in TrackerRuleStore.List
s0up4200 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,272 @@ | ||
| // Copyright (c) 2025, s0up and the autobrr contributors. | ||
| // SPDX-License-Identifier: GPL-2.0-or-later | ||
|
|
||
| package handlers | ||
|
|
||
| import ( | ||
| "database/sql" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/go-chi/chi/v5" | ||
| "github.com/rs/zerolog/log" | ||
|
|
||
| "github.com/autobrr/qui/internal/models" | ||
| "github.com/autobrr/qui/internal/services/trackerrules" | ||
| ) | ||
|
|
||
| type TrackerRuleHandler struct { | ||
| store *models.TrackerRuleStore | ||
| service *trackerrules.Service | ||
| } | ||
|
|
||
| func NewTrackerRuleHandler(store *models.TrackerRuleStore, service *trackerrules.Service) *TrackerRuleHandler { | ||
| return &TrackerRuleHandler{ | ||
| store: store, | ||
| service: service, | ||
| } | ||
| } | ||
|
|
||
| type TrackerRulePayload struct { | ||
| Name string `json:"name"` | ||
| TrackerPattern string `json:"trackerPattern"` | ||
| TrackerDomains []string `json:"trackerDomains"` | ||
| Category *string `json:"category"` | ||
| Tag *string `json:"tag"` | ||
| UploadLimitKiB *int64 `json:"uploadLimitKiB"` | ||
| DownloadLimitKiB *int64 `json:"downloadLimitKiB"` | ||
| RatioLimit *float64 `json:"ratioLimit"` | ||
| SeedingTimeLimitMinutes *int64 `json:"seedingTimeLimitMinutes"` | ||
| Enabled *bool `json:"enabled"` | ||
| SortOrder *int `json:"sortOrder"` | ||
| } | ||
|
|
||
| func (p *TrackerRulePayload) toModel(instanceID int, id int) *models.TrackerRule { | ||
| normalizedDomains := normalizeTrackerDomains(p.TrackerDomains) | ||
| trackerPattern := p.TrackerPattern | ||
| if len(normalizedDomains) > 0 { | ||
| trackerPattern = strings.Join(normalizedDomains, ",") | ||
| } | ||
|
|
||
| rule := &models.TrackerRule{ | ||
| ID: id, | ||
| InstanceID: instanceID, | ||
| Name: p.Name, | ||
| TrackerPattern: trackerPattern, | ||
| TrackerDomains: normalizedDomains, | ||
| Category: cleanStringPtr(p.Category), | ||
| Tag: cleanStringPtr(p.Tag), | ||
| UploadLimitKiB: p.UploadLimitKiB, | ||
| DownloadLimitKiB: p.DownloadLimitKiB, | ||
| RatioLimit: p.RatioLimit, | ||
| SeedingTimeLimitMinutes: p.SeedingTimeLimitMinutes, | ||
| Enabled: true, | ||
| } | ||
| if p.Enabled != nil { | ||
| rule.Enabled = *p.Enabled | ||
| } | ||
| if p.SortOrder != nil { | ||
| rule.SortOrder = *p.SortOrder | ||
| } | ||
| return rule | ||
| } | ||
|
|
||
| func (h *TrackerRuleHandler) List(w http.ResponseWriter, r *http.Request) { | ||
| instanceID, err := parseInstanceID(w, r) | ||
| if err != nil { | ||
| return | ||
| } | ||
|
|
||
| rules, err := h.store.ListByInstance(r.Context(), instanceID) | ||
| if err != nil { | ||
| log.Error().Err(err).Int("instanceID", instanceID).Msg("failed to list tracker rules") | ||
| RespondError(w, http.StatusInternalServerError, "Failed to load tracker rules") | ||
| return | ||
| } | ||
|
|
||
| RespondJSON(w, http.StatusOK, rules) | ||
| } | ||
|
|
||
| func (h *TrackerRuleHandler) Create(w http.ResponseWriter, r *http.Request) { | ||
| instanceID, err := parseInstanceID(w, r) | ||
| if err != nil { | ||
| return | ||
| } | ||
|
|
||
| var payload TrackerRulePayload | ||
| if err := json.NewDecoder(r.Body).Decode(&payload); err != nil { | ||
| RespondError(w, http.StatusBadRequest, "Invalid request payload") | ||
| return | ||
| } | ||
|
|
||
| if payload.Name == "" { | ||
| RespondError(w, http.StatusBadRequest, "Name is required") | ||
| return | ||
| } | ||
|
|
||
| if len(normalizeTrackerDomains(payload.TrackerDomains)) == 0 && strings.TrimSpace(payload.TrackerPattern) == "" { | ||
| RespondError(w, http.StatusBadRequest, "Select at least one tracker") | ||
| return | ||
| } | ||
|
|
||
| rule, err := h.store.Create(r.Context(), payload.toModel(instanceID, 0)) | ||
| if err != nil { | ||
| log.Error().Err(err).Int("instanceID", instanceID).Msg("failed to create tracker rule") | ||
| RespondError(w, http.StatusInternalServerError, "Failed to create tracker rule") | ||
| return | ||
| } | ||
|
|
||
| RespondJSON(w, http.StatusCreated, rule) | ||
| } | ||
|
|
||
| func (h *TrackerRuleHandler) Update(w http.ResponseWriter, r *http.Request) { | ||
| instanceID, err := parseInstanceID(w, r) | ||
| if err != nil { | ||
| return | ||
| } | ||
|
|
||
| ruleIDStr := chi.URLParam(r, "ruleID") | ||
| ruleID, err := strconv.Atoi(ruleIDStr) | ||
| if err != nil || ruleID <= 0 { | ||
| RespondError(w, http.StatusBadRequest, "Invalid rule ID") | ||
| return | ||
| } | ||
|
|
||
| var payload TrackerRulePayload | ||
| if err := json.NewDecoder(r.Body).Decode(&payload); err != nil { | ||
| RespondError(w, http.StatusBadRequest, "Invalid request payload") | ||
| return | ||
| } | ||
|
|
||
| if payload.Name == "" { | ||
| RespondError(w, http.StatusBadRequest, "Name is required") | ||
| return | ||
| } | ||
|
|
||
| if len(normalizeTrackerDomains(payload.TrackerDomains)) == 0 && strings.TrimSpace(payload.TrackerPattern) == "" { | ||
| RespondError(w, http.StatusBadRequest, "Select at least one tracker") | ||
| return | ||
| } | ||
|
|
||
| rule, err := h.store.Update(r.Context(), payload.toModel(instanceID, ruleID)) | ||
| if err != nil { | ||
| if errors.Is(err, sql.ErrNoRows) { | ||
| log.Error().Err(err).Int("instanceID", instanceID).Int("ruleID", ruleID).Msg("tracker rule not found for update") | ||
| RespondError(w, http.StatusNotFound, "Tracker rule not found") | ||
| return | ||
| } | ||
| log.Error().Err(err).Int("instanceID", instanceID).Int("ruleID", ruleID).Msg("failed to update tracker rule") | ||
| RespondError(w, http.StatusInternalServerError, "Failed to update tracker rule") | ||
| return | ||
| } | ||
|
|
||
| RespondJSON(w, http.StatusOK, rule) | ||
| } | ||
|
|
||
| func (h *TrackerRuleHandler) Delete(w http.ResponseWriter, r *http.Request) { | ||
| instanceID, err := parseInstanceID(w, r) | ||
| if err != nil { | ||
| return | ||
| } | ||
|
|
||
| ruleIDStr := chi.URLParam(r, "ruleID") | ||
| ruleID, err := strconv.Atoi(ruleIDStr) | ||
| if err != nil || ruleID <= 0 { | ||
| RespondError(w, http.StatusBadRequest, "Invalid rule ID") | ||
| return | ||
| } | ||
|
|
||
| if err := h.store.Delete(r.Context(), instanceID, ruleID); err != nil { | ||
| if errors.Is(err, sql.ErrNoRows) { | ||
| RespondError(w, http.StatusNotFound, "Tracker rule not found") | ||
| return | ||
| } | ||
| log.Error().Err(err).Int("instanceID", instanceID).Int("ruleID", ruleID).Msg("failed to delete tracker rule") | ||
| RespondError(w, http.StatusInternalServerError, "Failed to delete tracker rule") | ||
| return | ||
| } | ||
|
|
||
| RespondJSON(w, http.StatusNoContent, nil) | ||
| } | ||
|
|
||
| func (h *TrackerRuleHandler) Reorder(w http.ResponseWriter, r *http.Request) { | ||
| instanceID, err := parseInstanceID(w, r) | ||
| if err != nil { | ||
| return | ||
| } | ||
|
|
||
| var payload struct { | ||
| OrderedIDs []int `json:"orderedIds"` | ||
| } | ||
| if err := json.NewDecoder(r.Body).Decode(&payload); err != nil || len(payload.OrderedIDs) == 0 { | ||
| RespondError(w, http.StatusBadRequest, "Invalid request payload") | ||
| return | ||
| } | ||
|
|
||
| if err := h.store.Reorder(r.Context(), instanceID, payload.OrderedIDs); err != nil { | ||
| log.Error().Err(err).Int("instanceID", instanceID).Msg("failed to reorder tracker rules") | ||
| RespondError(w, http.StatusInternalServerError, "Failed to reorder tracker rules") | ||
| return | ||
| } | ||
|
|
||
| RespondJSON(w, http.StatusNoContent, nil) | ||
| } | ||
|
|
||
| func (h *TrackerRuleHandler) ApplyNow(w http.ResponseWriter, r *http.Request) { | ||
| instanceID, err := parseInstanceID(w, r) | ||
| if err != nil { | ||
| return | ||
| } | ||
|
|
||
| if h.service != nil { | ||
| if err := h.service.ApplyOnceForInstance(r.Context(), instanceID); err != nil { | ||
| log.Error().Err(err).Int("instanceID", instanceID).Msg("tracker rules: manual apply failed") | ||
| RespondError(w, http.StatusInternalServerError, "Failed to apply tracker rules") | ||
| return | ||
| } | ||
| } | ||
|
|
||
| RespondJSON(w, http.StatusAccepted, map[string]string{"status": "applied"}) | ||
| } | ||
|
|
||
| func parseInstanceID(w http.ResponseWriter, r *http.Request) (int, error) { | ||
| instanceIDStr := chi.URLParam(r, "instanceID") | ||
| instanceID, err := strconv.Atoi(instanceIDStr) | ||
| if err != nil || instanceID <= 0 { | ||
| RespondError(w, http.StatusBadRequest, "Invalid instance ID") | ||
| return 0, fmt.Errorf("invalid instance ID: %s", instanceIDStr) | ||
| } | ||
| return instanceID, nil | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| func cleanStringPtr(value *string) *string { | ||
| if value == nil { | ||
| return nil | ||
| } | ||
| trimmed := strings.TrimSpace(*value) | ||
| if trimmed == "" { | ||
| return nil | ||
| } | ||
| return &trimmed | ||
| } | ||
|
|
||
| func normalizeTrackerDomains(domains []string) []string { | ||
| seen := make(map[string]struct{}) | ||
| var out []string | ||
| for _, d := range domains { | ||
| trimmed := strings.TrimSpace(d) | ||
| if trimmed == "" { | ||
| continue | ||
| } | ||
| if _, exists := seen[trimmed]; exists { | ||
| continue | ||
| } | ||
| seen[trimmed] = struct{}{} | ||
| out = append(out, trimmed) | ||
| } | ||
| return out | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.