Skip to content

Commit dfbb5b1

Browse files
authored
feat: Added support for opting out endpoint translation (#56)
1 parent dd8ba8b commit dfbb5b1

File tree

17 files changed

+125
-70
lines changed

17 files changed

+125
-70
lines changed

lib/cache.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ cache.init = function() {
99
file.mkdir(file.cacheDir());
1010
};
1111

12+
cache.deleteAll = function () {
13+
cache.list().forEach(value => {
14+
cache.del(value.name);
15+
})
16+
};
17+
1218
cache.get = function(k) {
1319
const fullpath = file.cacheFile(k);
1420
if (!file.exist(fullpath)) return null;

lib/commands/list.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ const cmd = {
2828
default: false,
2929
describe: 'Show extra details: category, companies, tags.'
3030
})
31+
.option('T', {
32+
alias: 'dontTranslate',
33+
type: 'boolean',
34+
default: false,
35+
describe: 'Set to true to disable endpoint\'s translation',
36+
})
3137
.positional('keyword', {
3238
type: 'string',
3339
default: '',

lib/commands/show.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ const cmd = {
5757
default: false,
5858
describe: 'Show extra question details in source code'
5959
})
60+
.option('T', {
61+
alias: 'dontTranslate',
62+
type: 'boolean',
63+
default: false,
64+
describe: 'Set to true to disable endpoint\'s translation',
65+
})
6066
.positional('keyword', {
6167
type: 'string',
6268
default: '',
@@ -175,7 +181,7 @@ cmd.handler = function(argv) {
175181
session.argv = argv;
176182
if (argv.keyword.length > 0) {
177183
// show specific one
178-
core.getProblem(argv.keyword, function(e, problem) {
184+
core.getProblem(argv.keyword, !argv.dontTranslate, function(e, problem) {
179185
if (e) return log.fail(e);
180186
showProblem(problem, argv);
181187
});
@@ -194,7 +200,7 @@ cmd.handler = function(argv) {
194200
if (problems.length === 0) return log.fail('Problem not found!');
195201

196202
const problem = _.sample(problems);
197-
core.getProblem(problem, function(e, problem) {
203+
core.getProblem(problem, !argv.dontTranslate, function(e, problem) {
198204
if (e) return log.fail(e);
199205
showProblem(problem, argv);
200206
});

lib/commands/star.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ const cmd = {
2929

3030
cmd.handler = function(argv) {
3131
session.argv = argv;
32-
core.getProblem(argv.keyword, function(e, problem) {
32+
// translation doesn't affect question lookup
33+
core.getProblem(argv.keyword, true, function(e, problem) {
3334
if (e) return log.fail(e);
3435

3536
core.starProblem(problem, !argv.delete, function(e, starred) {

lib/commands/submission.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ const cmd = {
4242
default: false,
4343
describe: 'Show extra question details in submission code'
4444
})
45+
.option('T', {
46+
alias: 'dontTranslate',
47+
type: 'boolean',
48+
default: false,
49+
describe: 'Set to true to disable endpoint\'s translation',
50+
})
4551
.positional('keyword', {
4652
type: 'string',
4753
default: '',
@@ -69,7 +75,7 @@ function doTask(problem, queue, cb) {
6975

7076
if (argv.extra) {
7177
// have to get problem details, e.g. problem description.
72-
core.getProblem(problem.fid, function(e, problem) {
78+
core.getProblem(problem.fid, !argv.dontTranslate, function(e, problem) {
7379
if (e) return cb(e);
7480
exportSubmission(problem, argv, onTaskDone);
7581
});
@@ -135,7 +141,7 @@ cmd.handler = function(argv) {
135141
if (!argv.keyword)
136142
return log.fail('missing keyword?');
137143

138-
core.getProblem(argv.keyword, function(e, problem) {
144+
core.getProblem(argv.keyword, !argv.dontTranslate, function(e, problem) {
139145
if (e) return log.fail(e);
140146
q.addTask(problem).run();
141147
});

lib/commands/submit.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ cmd.handler = function(argv) {
4949

5050
const meta = file.meta(argv.filename);
5151

52-
core.getProblem(meta.id, function(e, problem) {
52+
// translation doesn't affect problem lookup
53+
core.getProblem(meta.id, true, function(e, problem) {
5354
if (e) return log.fail(e);
5455

5556
problem.file = argv.filename;

lib/commands/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function runTest(argv) {
6060

6161
const meta = file.meta(argv.filename);
6262

63-
core.getProblem(meta.id, function(e, problem) {
63+
core.getProblem(meta.id, true, function(e, problem) {
6464
if (e) return log.fail(e);
6565

6666
if (!problem.testable)

lib/core.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const QUERY_HANDLERS = {
6161
};
6262

6363
core.filterProblems = function(opts, cb) {
64-
this.getProblems(function(e, problems) {
64+
this.getProblems(!opts.dontTranslate, function(e, problems) {
6565
if (e) return cb(e);
6666

6767
for (let q of (opts.query || '').split('')) {
@@ -82,11 +82,11 @@ core.filterProblems = function(opts, cb) {
8282
});
8383
};
8484

85-
core.getProblem = function(keyword, cb) {
85+
core.getProblem = function(keyword, needTranslation, cb) {
8686
if (keyword.id)
87-
return core.next.getProblem(keyword, cb);
87+
return core.next.getProblem(keyword, needTranslation, cb);
8888

89-
this.getProblems(function(e, problems) {
89+
this.getProblems(needTranslation, function(e, problems) {
9090
if (e) return cb(e);
9191

9292
keyword = Number(keyword) || keyword;
@@ -95,7 +95,7 @@ core.getProblem = function(keyword, cb) {
9595
return x.fid + '' === keyword + '' || x.fid + '' === metaFid + '' || x.name === keyword || x.slug === keyword;
9696
});
9797
if (!problem) return cb('Problem not found!');
98-
core.next.getProblem(problem, cb);
98+
core.next.getProblem(problem, needTranslation, cb);
9999
});
100100
};
101101

lib/helper.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@ const LANGS = [
5252
const h = {};
5353

5454
h.KEYS = {
55-
user: '../user',
56-
stat: '../stat',
57-
plugins: '../../plugins',
58-
problems: 'problems',
59-
problem: p => p.fid + '.' + p.slug + '.' + p.category
55+
user: '../user',
56+
stat: '../stat',
57+
plugins: '../../plugins',
58+
problems: 'problems',
59+
translation: 'translationConfig',
60+
problem: p => p.fid + '.' + p.slug + '.' + p.category
6061
};
6162

6263
h.prettyState = function(state) {

lib/plugins/cache.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,37 @@ var session = require('../session');
99

1010
const plugin = new Plugin(50, 'cache', '', 'Plugin to provide local cache.');
1111

12-
plugin.getProblems = function(cb) {
12+
// this function will clear all caches if needTranslation is different than stored
13+
// it will also store the new needTranslation into cache automatically
14+
function clearCacheIfTchanged(needTranslation) {
15+
const translationConfig = cache.get(h.KEYS.translation);
16+
if (!translationConfig || translationConfig['useEndpointTranslation'] != needTranslation) {
17+
// cache doesn't have the key => old cache version, need to update
18+
// or cache does have the key but it contains a different value
19+
cache.deleteAll();
20+
cache.set(h.KEYS.translation, { useEndpointTranslation: needTranslation });
21+
log.debug('cache cleared: -T option changed');
22+
}
23+
}
24+
25+
plugin.getProblems = function (needTranslation, cb) {
26+
clearCacheIfTchanged(needTranslation);
1327
const problems = cache.get(h.KEYS.problems);
1428
if (problems) {
1529
log.debug('cache hit: problems.json');
1630
return cb(null, problems);
1731
}
1832

19-
plugin.next.getProblems(function(e, problems) {
33+
plugin.next.getProblems(needTranslation, function(e, problems) {
2034
if (e) return cb(e);
2135

2236
cache.set(h.KEYS.problems, problems);
2337
return cb(null, problems);
2438
});
2539
};
2640

27-
plugin.getProblem = function(problem, cb) {
41+
plugin.getProblem = function (problem, needTranslation, cb) {
42+
clearCacheIfTchanged(needTranslation);
2843
const k = h.KEYS.problem(problem);
2944
const _problem = cache.get(k);
3045
if (_problem) {
@@ -42,7 +57,7 @@ plugin.getProblem = function(problem, cb) {
4257
}
4358
}
4459

45-
plugin.next.getProblem(problem, function(e, _problem) {
60+
plugin.next.getProblem(problem, needTranslation, function(e, _problem) {
4661
if (e) return cb(e);
4762

4863
plugin.saveProblem(_problem);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy