@@ -215,15 +215,33 @@ export class LocalCommunity extends RpcLocalCommunity implements CreateNewLocalC
215215 override raw : RpcLocalCommunity [ "raw" ] = { } ;
216216 private _postUpdatesBuckets = [ 86400 , 604800 , 2592000 , 3153600000 ] ; // 1 day, 1 week, 1 month, 100 years. Expecting to be sorted from smallest to largest
217217
218- private _defaultCommunityChallenges : CommunityChallengeSetting [ ] = [
219- {
220- name : "question" ,
221- options : {
222- question : "Placeholder challenge. Set your own challenges otherwise you risk getting spammed" ,
223- answer : "Placeholder answer"
218+ private static _defaultChallengeQuestionText =
219+ "What is the answer to this community's challenge? (check community.settings.challenges to see the answer, or set your own challenge)" ;
220+
221+ static _generateDefaultChallenges ( answer ?: string ) : CommunityChallengeSetting [ ] {
222+ return [
223+ {
224+ name : "question" ,
225+ options : {
226+ question : LocalCommunity . _defaultChallengeQuestionText ,
227+ answer : answer ?? uuidV4 ( )
228+ }
224229 }
225- }
226- ] ;
230+ ] ;
231+ }
232+
233+ static _isDefaultChallengeStructure ( challenges : CommunityChallengeSetting [ ] | undefined ) : boolean {
234+ if ( ! challenges || challenges . length !== 1 ) return false ;
235+ const c = challenges [ 0 ] ;
236+ return (
237+ c . name === "question" &&
238+ c . options ?. question === LocalCommunity . _defaultChallengeQuestionText &&
239+ typeof c . options ?. answer === "string" &&
240+ c . options . answer . length > 0
241+ ) ;
242+ }
243+
244+ private _defaultCommunityChallenges : CommunityChallengeSetting [ ] = LocalCommunity . _generateDefaultChallenges ( ) ;
227245
228246 // These caches below will be used to facilitate challenges exchange with authors, they will expire after 10 minutes
229247 // Most of the time they will be delete and cleaned up automatically
@@ -542,12 +560,24 @@ export class LocalCommunity extends RpcLocalCommunity implements CreateNewLocalC
542560 async _setChallengesToDefaultIfNotDefined ( log : Logger ) {
543561 if (
544562 this . _usingDefaultChallenge !== false &&
545- ( ! this . settings ?. challenges || remeda . isDeepEqual ( this . settings ?. challenges , this . _defaultCommunityChallenges ) )
563+ ( ! this . settings ?. challenges || LocalCommunity . _isDefaultChallengeStructure ( this . settings ?. challenges ) )
546564 )
547565 this . _usingDefaultChallenge = true ;
548- if ( this . _usingDefaultChallenge && ! remeda . isDeepEqual ( this . settings ?. challenges , this . _defaultCommunityChallenges ) ) {
549- await this . edit ( { settings : { ...this . settings , challenges : this . _defaultCommunityChallenges } } ) ;
550- log ( `Defaulted the challenges of community (${ this . address } ) to` , this . _defaultCommunityChallenges ) ;
566+
567+ if ( this . _usingDefaultChallenge ) {
568+ const currentAnswer = this . settings ?. challenges ?. [ 0 ] ?. options ?. answer ;
569+ if ( currentAnswer ) {
570+ // Preserve the existing per-community random answer in the template
571+ this . _defaultCommunityChallenges = LocalCommunity . _generateDefaultChallenges ( currentAnswer ) ;
572+ }
573+
574+ if ( ! remeda . isDeepEqual ( this . settings ?. challenges , this . _defaultCommunityChallenges ) ) {
575+ await this . edit ( { settings : { ...this . settings , challenges : this . _defaultCommunityChallenges } } ) ;
576+ log (
577+ `Upgraded default challenge for community (${ this . address } ) with answer:` ,
578+ this . _defaultCommunityChallenges [ 0 ] . options ! . answer
579+ ) ;
580+ }
551581 }
552582 }
553583
@@ -567,7 +597,10 @@ export class LocalCommunity extends RpcLocalCommunity implements CreateNewLocalC
567597 if ( ! this . settings ?. challenges ) {
568598 this . settings = { ...this . settings , challenges : this . _defaultCommunityChallenges } ;
569599 this . _usingDefaultChallenge = true ;
570- log ( `Defaulted the challenges of community (${ this . address } ) to` , this . _defaultCommunityChallenges ) ;
600+ log (
601+ `Generated default challenge for community (${ this . address } ) with answer:` ,
602+ this . _defaultCommunityChallenges [ 0 ] . options ! . answer
603+ ) ;
571604 }
572605 if ( typeof this . settings ?. purgeDisapprovedCommentsOlderThan !== "number" ) {
573606 this . settings = { ...this . settings , purgeDisapprovedCommentsOlderThan : 1.21e6 } ; // two weeks
@@ -3240,7 +3273,7 @@ export class LocalCommunity extends RpcLocalCommunity implements CreateNewLocalC
32403273 challenges : await Promise . all (
32413274 newChallengeSettings . map ( ( cs ) => getCommunityChallengeFromCommunityChallengeSettings ( cs , this . _pkc ) )
32423275 ) ,
3243- _usingDefaultChallenge : remeda . isDeepEqual ( newChallengeSettings , this . _defaultCommunityChallenges )
3276+ _usingDefaultChallenge : LocalCommunity . _isDefaultChallengeStructure ( newChallengeSettings )
32443277 } ;
32453278 }
32463279
0 commit comments