Repository navigation
fix(MyAnonaMouse): persist refreshed mam_id so sessions don't expire after 30 days #308
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
Changes from all commits
7d2e797
fc12598
fc272f3
cb590ef
3ac6404
87c33fb
fa44d13
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| using Chaptarr.Http; | ||
| using FluentValidation; | ||
| using NzbDrone.Common.Cache; | ||
| using NzbDrone.Core.Indexers; | ||
| using NzbDrone.Core.Indexers.MyAnonaMouse; | ||
| using NzbDrone.Core.Validation; | ||
|
|
||
| namespace Chaptarr.Api.V1.Indexers | ||
|
|
@@ -11,11 +13,75 @@ public class IndexerController : ProviderControllerBase<IndexerResource, Indexer | |
| public static readonly IndexerResourceMapper ResourceMapper = new(); | ||
| public static readonly IndexerBulkResourceMapper BulkResourceMapper = new(); | ||
|
|
||
| public IndexerController(IndexerFactory indexerFactory, DownloadClientExistsValidator downloadClientExistsValidator) | ||
| private readonly ICached<MamTestResult> _mamTests; | ||
|
|
||
| public IndexerController(IIndexerFactory indexerFactory, DownloadClientExistsValidator downloadClientExistsValidator, ICacheManager cacheManager) | ||
| : base(indexerFactory, "indexer", ResourceMapper, BulkResourceMapper) | ||
| { | ||
| _mamTests = cacheManager.GetCache<MamTestResult>(typeof(IndexerController)); | ||
| SharedValidator.RuleFor(c => c.Priority).InclusiveBetween(1, 50); | ||
| SharedValidator.RuleFor(c => c.DownloadClientId).SetValidator(downloadClientExistsValidator); | ||
| } | ||
|
|
||
| protected override void Test(IndexerDefinition definition, bool includeWarnings) | ||
| { | ||
| if (definition.Settings is not MyAnonaMouseSettings settings) | ||
| { | ||
| base.Test(definition, includeWarnings); | ||
| return; | ||
| } | ||
|
|
||
| var connection = MamConnection.For(definition); | ||
| var key = definition.Id.ToString(); | ||
| _mamTests.Remove(key); | ||
| base.Test(definition, includeWarnings); | ||
|
|
||
| _mamTests.Set(key, new MamTestResult(connection, settings)); | ||
| } | ||
|
|
||
| protected override void PrepareForSave(IndexerDefinition definition, IndexerDefinition existing) | ||
| { | ||
| if (definition.Settings is not MyAnonaMouseSettings settings) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| // Quickstart reads the hidden class field, but a form opened before a refresh can post its old value. | ||
| if (existing?.Settings is MyAnonaMouseSettings stored) | ||
| { | ||
| settings.UserClass = stored.UserClass; | ||
| } | ||
|
|
||
| var tested = _mamTests.Find(definition.Id.ToString()); | ||
| if (tested?.Covers(MamConnection.For(definition)) == true && | ||
| tested.Settings.UnsatisfiedStatusRefreshedUtc is { } testedAt && | ||
| (settings.UnsatisfiedStatusRefreshedUtc is not { } storedAt || testedAt > storedAt)) | ||
|
Comment on lines
+56
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adopt the tested account status when the account changes. On update, the submitted settings are built from the stored definition, so Adopt the tested status whenever the submitted connection differs from the stored one. Keep the timestamp rule only when the connection is unchanged. Suggested fix
|
||
| { | ||
| settings.CopyAccountStatusFrom(tested.Settings); | ||
| } | ||
| } | ||
|
|
||
| protected override void TestOnSave(IndexerDefinition definition, IndexerDefinition existing, bool includeWarnings) | ||
| { | ||
| var connection = MamConnection.For(definition); | ||
| if (connection == null) | ||
| { | ||
| base.TestOnSave(definition, existing, includeWarnings); | ||
| return; | ||
| } | ||
|
|
||
| if (connection == MamConnection.For(existing) || _mamTests.Find(definition.Id.ToString())?.Covers(connection) == true) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Require a protected save to use only a cached test that ran with protection enabled.
A cached result should cover a protected save only when that result was produced with protection enabled and the account summary was verified. Track the protection setting and verification outcome in the cached result. Make
|
||
| { | ||
| return; | ||
| } | ||
|
|
||
| base.Test(definition, includeWarnings); | ||
| } | ||
|
|
||
| private sealed record MamTestResult(MamConnection Connection, MyAnonaMouseSettings Settings) | ||
| { | ||
| public bool Covers(MamConnection connection) => | ||
| connection == Connection || connection == Connection with { MamId = Settings.MamId }; | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bound how long a MAM test result can skip a Save-time test.
A successful MAM test for a new indexer caches its result under key
0. That entry has no expiry. On Create,TestOnSavereturns early when the cached result covers the connection, so an enabled indexer can be created with amam_idthat has since expired. The entry remains until the same key is tested again, the cache is cleared, or the process restarts.Give the cached test result a finite lifetime, or make
TestOnSaveignore results older than that window. An expired cookie must then trigger a fresh test. The window length is a choice for the maintainers. The existingSetlifetime parameter and the expiry check inFindcan enforce it.