Skip to content

Commit e0b4f42

Browse files
committed
fix(challenges): preserve default challenge upgrade path when default structure changes
The answer-preserve logic in _setChallengesToDefaultIfNotDefined unconditionally overwrote _defaultCommunityChallenges with the old stored answer, preventing upgrades when the default challenge structure changed. Guard it with _isDefaultChallengeStructure check, and re-assert _usingDefaultChallenge after edit() since edit() recalculates it via _isDefaultChallengeStructure.
1 parent f352de3 commit e0b4f42

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

src/runtime/node/community/local-community.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ export class LocalCommunity extends RpcLocalCommunity implements CreateNewLocalC
241241
);
242242
}
243243

244-
private _defaultCommunityChallenges: CommunityChallengeSetting[] = LocalCommunity._generateDefaultChallenges();
244+
_defaultCommunityChallenges: CommunityChallengeSetting[] = LocalCommunity._generateDefaultChallenges();
245245

246246
// These caches below will be used to facilitate challenges exchange with authors, they will expire after 10 minutes
247247
// Most of the time they will be delete and cleaned up automatically
@@ -566,16 +566,22 @@ export class LocalCommunity extends RpcLocalCommunity implements CreateNewLocalC
566566

567567
if (this._usingDefaultChallenge) {
568568
const currentAnswer = this.settings?.challenges?.[0]?.options?.answer;
569-
if (currentAnswer) {
569+
if (currentAnswer && LocalCommunity._isDefaultChallengeStructure(this._defaultCommunityChallenges)) {
570570
// Preserve the existing per-community random answer in the template
571571
this._defaultCommunityChallenges = LocalCommunity._generateDefaultChallenges(currentAnswer);
572572
}
573573

574574
if (!remeda.isDeepEqual(this.settings?.challenges, this._defaultCommunityChallenges)) {
575575
await this.edit({ settings: { ...this.settings, challenges: this._defaultCommunityChallenges } });
576+
// edit() recalculates _usingDefaultChallenge via _isDefaultChallengeStructure,
577+
// which may return false for non-standard defaults (e.g. []).
578+
// Re-assert true since we know this is still a default-driven upgrade.
579+
this._usingDefaultChallenge = true;
576580
log(
577-
`Upgraded default challenge for community (${this.address}) with answer:`,
578-
this._defaultCommunityChallenges[0].options!.answer
581+
`Upgraded default challenge for community (${this.address})`,
582+
this._defaultCommunityChallenges[0]?.options?.answer
583+
? `with answer: ${this._defaultCommunityChallenges[0].options!.answer}`
584+
: `to ${this._defaultCommunityChallenges.length} challenge(s)`
579585
);
580586
}
581587
}

0 commit comments

Comments
 (0)