Skip to content

Commit ea0b3f8

Browse files
fsvredditeritbh
andauthored
Accept empty input to SubredditConfig constructor (#66)
Co-authored-by: Erin <[email protected]>
1 parent 3859e96 commit ea0b3f8

File tree

4 files changed

+73
-6
lines changed

4 files changed

+73
-6
lines changed

src/classes/SubredditConfig.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,36 @@
11
import test from 'ava';
2+
import {DEFAULT_CONFIG} from '../helpers/config';
3+
import {SubredditConfig} from './SubredditConfig';
4+
5+
test('constructor: accept empty input', t => {
6+
t.assert(
7+
new SubredditConfig() instanceof SubredditConfig,
8+
'passing nothing to SubredditConfig constructor should return a SubredditConfig instance',
9+
);
10+
11+
t.assert(
12+
new SubredditConfig('') instanceof SubredditConfig,
13+
'passing empty string to SubredditConfig constructor should return a SubredditConfig instance',
14+
);
15+
});
16+
17+
test('constructor: results of passing in nothing and empty input are identical', t => {
18+
const configFromNothing = new SubredditConfig();
19+
const configFromEmpty = new SubredditConfig('');
20+
21+
t.deepEqual(
22+
configFromNothing.toJSON(),
23+
configFromEmpty.toJSON(),
24+
'SubredditConfig initiated with nothing vs empty should be identical',
25+
);
26+
});
27+
28+
test('constructor: on empty input, the default config is returned', t => {
29+
const config = new SubredditConfig();
30+
const configAsJson = config.toJSON();
31+
32+
t.deepEqual(configAsJson, DEFAULT_CONFIG, "SubredditConfig initiated with nothing should return default config");
33+
});
234

335
test.todo('getAllNoteTypes');
436
test.todo('getNoteType');

src/classes/SubredditConfig.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
DEFAULT_CONFIG,
23
DEFAULT_USERNOTE_TYPES,
34
migrateConfigToLatestSchema,
45
} from '../helpers/config';
@@ -15,8 +16,13 @@ import type {Usernote} from '../types/Usernote';
1516
export class SubredditConfig {
1617
private data: RawSubredditConfig;
1718

18-
constructor (jsonString: string) {
19-
this.data = migrateConfigToLatestSchema(JSON.parse(jsonString));
19+
constructor (jsonString?: string) {
20+
if (jsonString) {
21+
this.data = migrateConfigToLatestSchema(JSON.parse(jsonString));
22+
} else {
23+
// TODO: the default config value isn't actually typed correctly, this needs to be cleaned up eventually
24+
this.data = DEFAULT_CONFIG as unknown as RawSubredditConfig;
25+
}
2026
}
2127

2228
/** Returns all usernote types. */

src/classes/ToolboxClient.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {RedditAPIClient} from '@devvit/public-api';
1+
import {RedditAPIClient, WikiPage} from '@devvit/public-api';
22
import {Usernote, UsernoteInit} from '../types/Usernote';
33
import {SubredditConfig} from './SubredditConfig';
44
import {Usernotes} from './Usernotes';
@@ -140,9 +140,24 @@ export class ToolboxClient {
140140
await this.writeUsernotes(subreddit, notes, reason);
141141
}
142142

143-
/** */
143+
/**
144+
* Retrieves toolbox configuration for a subreddit.
145+
* @param subreddit Name of the subreddit to retrieve config for
146+
*/
144147
async getConfig (subreddit: string) {
145-
const page = await this.reddit.getWikiPage(subreddit, TB_CONFIG_PAGE);
146-
return new SubredditConfig(page.content);
148+
let page: WikiPage | undefined;
149+
try {
150+
page = await this.reddit.getWikiPage(subreddit, TB_CONFIG_PAGE);
151+
} catch (error) {
152+
// Devvit throws an error when page is not present, but also
153+
// sometimes for other reasons. Check if the page actually
154+
// exists; if it doesn't we'll use the default config, but if it
155+
// does then something else is wrong and we'll rethrow the error.
156+
const allPages = await this.reddit.getWikiPages(subreddit);
157+
if (allPages.includes(TB_CONFIG_PAGE)) {
158+
throw error;
159+
}
160+
}
161+
return new SubredditConfig(page?.content);
147162
}
148163
}

src/helpers/config.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ export const DEFAULT_USERNOTE_TYPES: readonly RawUsernoteType[] = [
2525
{key: 'botban', color: 'black', text: 'Bot Ban'},
2626
];
2727

28+
/**
29+
* Default subreddit configuration to use if subreddit doesnt have one.
30+
* Empty strings are used in the default config by the plugin even though
31+
* this doesn't match the type exactly.
32+
*/
33+
export const DEFAULT_CONFIG = {
34+
ver: LATEST_KNOWN_CONFIG_SCHEMA,
35+
domainTags: '',
36+
removalReasons: '',
37+
modMacros: '',
38+
usernoteColors: '',
39+
banMacros: '',
40+
};
41+
2842
/**
2943
* Checks the schema version of raw subreddit config data and attempts to update
3044
* it to the latest known schema version if it's out of date. Throws an error if

0 commit comments

Comments
 (0)