问题修复

This commit is contained in:
masnn committed 2020-04-10 23:17:33 +08:00
1 parent 4c2edc3948
commit 26c467f60f
10 files changed
+99 -99

No files matched your search

+3 -1
View File
@@ -19,11 +19,13 @@ process.on('restart', async () => {
const path = require('path'); const path = require('path');
const i18n = require('./lib/i18n'); const i18n = require('./lib/i18n');
i18n(path.resolve(__dirname, '..', 'locales', 'zh_CN.yaml'), 'zh_CN'); i18n(path.resolve(__dirname, '..', 'locales', 'zh_CN.yaml'), 'zh_CN');
i18n(path.resolve(__dirname, '..', 'locales', 'zh_TW.yaml'), 'zh_TW');
i18n(path.resolve(__dirname, '..', 'locales', 'en.yaml'), 'en');
const EventEmitter = require('events'); const EventEmitter = require('events');
global.bus = new EventEmitter(); global.bus = new EventEmitter();
async function run() { async function run() {
require('./utils');
require('./service/db'); require('./service/db');
await new Promise((resolve) => { await new Promise((resolve) => {
global.bus.once('connected', () => { global.bus.once('connected', () => {
+5 -43
View File
@@ -8,7 +8,7 @@ const
options = require('../options'), options = require('../options'),
perm = require('../permission'), perm = require('../permission'),
builtin = require('../model/builtin'), builtin = require('../model/builtin'),
md5 = require('../lib/md5'), misc=require('../lib/misc'),
{ MIDDLEWARE } = require('../service/server'), { MIDDLEWARE } = require('../service/server'),
{ NotFoundError } = require('../error'); { NotFoundError } = require('../error');
@@ -29,33 +29,6 @@ class Markdown extends MarkdownIt {
} }
} }
const md = new Markdown(); const md = new Markdown();
function datetime_span(dt, { relative = true } = {}) {
if (dt.generationTime) dt = new Date(dt.generationTime * 1000);
else if (typeof dt == 'number' || typeof dt == 'string') dt = new Date(dt);
return '<span class="time{0}" data-timestamp="{1}">{2}</span>'.format(
relative ? ' relative' : '',
dt.getTime() / 1000,
dt.toLocaleString()
);
}
function* paginate(page, num_pages) {
let radius = 2, first, last;
if (page > 1) {
yield ['first', 1];
yield ['previous', page - 1];
}
if (page <= radius) [first, last] = [1, Math.min(1 + radius * 2, num_pages)];
else if (page >= num_pages - radius) [first, last] = [Math.max(1, num_pages - radius * 2), num_pages];
else[first, last] = [page - radius, page + radius];
if (first > 1) yield ['ellipsis', 0];
for (let page0 = first; page0 < last + 1; page0++) {
if (page0 != page) yield ['page', page0];
else yield ['current', page];
}
if (last < num_pages) yield ['ellipsis', 0];
if (page < num_pages) yield ['next', page + 1];
yield ['last', num_pages];
}
class Nunjucks extends nunjucks.Environment { class Nunjucks extends nunjucks.Environment {
constructor() { constructor() {
super( super(
@@ -71,25 +44,14 @@ class Nunjucks extends nunjucks.Environment {
this.addFilter('markdown', function (self) { this.addFilter('markdown', function (self) {
return md.render(self); return md.render(self);
}); });
this.addFilter('gravatar_url', function (email, size) { this.addFilter('gravatar_url', misc.gravatar_url);
return `//gravatar.loli.net/avatar/${md5(email.toString().trim().toLowerCase())}?d=mm&s=${size}`; this.addFilter('format_size', misc.format_size);
});
this.addFilter('format_size', function (size, base = 1) {
size *= base;
let unit = 1024;
let unit_names = ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
for (let unit_name of unit_names) {
if (size < unit) return '{0} {1}'.format(Math.round(size), unit_name);
size /= unit;
}
return '{0} {1}'.format(Math.round(size * unit), unit_names[unit_names.length - 1]);
});
this.addGlobal('typeof', o => typeof o); this.addGlobal('typeof', o => typeof o);
this.addGlobal('console', console); this.addGlobal('console', console);
this.addGlobal('static_url', str => `/${str}`); this.addGlobal('static_url', str => `/${str}`);
this.addGlobal('reverse_url', str => str); this.addGlobal('reverse_url', str => str);
this.addGlobal('datetime_span', datetime_span); this.addGlobal('datetime_span', misc.datetime_span);
this.addGlobal('paginate', paginate); this.addGlobal('paginate', misc.paginate);
this.addGlobal('perm', perm); this.addGlobal('perm', perm);
this.addGlobal('builtin', builtin); this.addGlobal('builtin', builtin);
this.addGlobal('status', builtin.STATUS); this.addGlobal('status', builtin.STATUS);
+1 -1
View File
@@ -1,7 +1,7 @@
const const
Mongo = require('mongodb'), Mongo = require('mongodb'),
builtin = require('./model/builtin'), builtin = require('./model/builtin'),
{ pwhash } = require('./utils'), pwhash = require('./lib/pwhash'),
options = require('./options'); options = require('./options');
async function run() { async function run() {
+41
View File
@@ -0,0 +1,41 @@
const md5 = require('./md5');
exports.gravatar_url = function (email, size) {
return `//gravatar.loli.net/avatar/${md5((email || '').toString().trim().toLowerCase())}?d=mm&s=${size}`;
};
exports.datetime_span = function (dt, { relative = true } = {}) {
if (dt.generationTime) dt = new Date(dt.generationTime * 1000);
else if (typeof dt == 'number' || typeof dt == 'string') dt = new Date(dt);
return '<span class="time{0}" data-timestamp="{1}">{2}</span>'.format(
relative ? ' relative' : '',
dt.getTime() / 1000,
dt.toLocaleString()
);
};
exports.paginate = function* (page, num_pages) {
let radius = 2, first, last;
if (page > 1) {
yield ['first', 1];
yield ['previous', page - 1];
}
if (page <= radius) [first, last] = [1, Math.min(1 + radius * 2, num_pages)];
else if (page >= num_pages - radius) [first, last] = [Math.max(1, num_pages - radius * 2), num_pages];
else[first, last] = [page - radius, page + radius];
if (first > 1) yield ['ellipsis', 0];
for (let page0 = first; page0 < last + 1; page0++) {
if (page0 != page) yield ['page', page0];
else yield ['current', page];
}
if (last < num_pages) yield ['ellipsis', 0];
if (page < num_pages) yield ['next', page + 1];
yield ['last', num_pages];
};
exports.format_size = function (size, base = 1) {
size *= base;
let unit = 1024;
let unit_names = ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
for (let unit_name of unit_names) {
if (size < unit) return '{0} {1}'.format(Math.round(size), unit_name);
size /= unit;
}
return '{0} {1}'.format(Math.round(size * unit), unit_names[unit_names.length - 1]);
};
+26
View File
@@ -0,0 +1,26 @@
const crypto = require('crypto');
module.exports = {
_b64encode: str => new Buffer(str).toString('base64'),
_b64decode: str => new Buffer(str, 'base64').toString(),
_md5: str => crypto.createHash('md5').update(str).digest('hex'),
_sha1: str => crypto.createHash('sha1').update(str).digest('hex'),
/**
* @param {string} password
* @param {string} salt
* @param {string} hash
*/
check(password, salt, hash) {
return hash == this.hash(password, salt);
},
/**
* @param {string} password
* @param {string} salt
*/
hash(password, salt) {
let res = crypto.pbkdf2Sync(password, salt, 100000, 64, 'sha256');
return res.toString('Hex');
},
salt() {
return String.random();
}
};
+1 -1
View File
@@ -1,7 +1,7 @@
const const
system = require('./system'), system = require('./system'),
{ UserNotFoundError, UserAlreadyExistError } = require('../error'), { UserNotFoundError, UserAlreadyExistError } = require('../error'),
{ pwhash } = require('../utils'), pwhash = require('./lib/pwhash'),
validator = require('../lib/validator'), validator = require('../lib/validator'),
db = require('../service/db'), db = require('../service/db'),
coll = db.collection('user'), coll = db.collection('user'),
+1
View File
@@ -60,6 +60,7 @@ function ROUTE(route, handler) {
let args = Object.assign({}, ctx.params, ctx.query, ctx.request.body); let args = Object.assign({}, ctx.params, ctx.query, ctx.request.body);
if (args.content) validator.checkContent(args.content); if (args.content) validator.checkContent(args.content);
if (args.title) validator.checkContent(args.title); if (args.title) validator.checkContent(args.title);
if (args.uid) args.uid = parseInt(validator.checkUid(args.uid));
if (h._prepare) await h._prepare(args); if (h._prepare) await h._prepare(args);
if (h.prepare) await h.prepare(args); if (h.prepare) await h.prepare(args);
if (h[`_${method}`]) await h[`_${method}`](args); if (h[`_${method}`]) await h[`_${method}`](args);
+2 -30
View File
@@ -1,6 +1,4 @@
const const map = [
crypto = require('crypto'),
map = [
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
@@ -8,7 +6,7 @@ const
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8',
'9', '0' '9', '0'
]; ];
String.random = function (digit) { String.random = function (digit) {
let str = ''; let str = '';
@@ -16,29 +14,3 @@ String.random = function (digit) {
str += map[Math.floor(Math.random() * 62)]; str += map[Math.floor(Math.random() * 62)];
return str; return str;
}; };
exports.pwhash = {
_b64encode: str => new Buffer(str).toString('base64'),
_b64decode: str => new Buffer(str, 'base64').toString(),
_md5: str => crypto.createHash('md5').update(str).digest('hex'),
_sha1: str => crypto.createHash('sha1').update(str).digest('hex'),
/**
* @param {string} password
* @param {string} salt
* @param {string} hash
*/
check(password, salt, hash) {
return hash == this.hash(password, salt);
},
/**
* @param {string} password
* @param {string} salt
*/
hash(password, salt) {
let res = crypto.pbkdf2Sync(password, salt, 100000, 64, 'sha256');
return res.toString('Hex');
},
salt() {
return String.random();
}
};
+17 -21
View File
@@ -26,26 +26,6 @@
</li> </li>
{% endmacro %} {% endmacro %}
{% macro comment_operations(doc) %}
<div class="dczcomments__operations nojs--hide">
{% if handler.has_perm(reply_post_perm) %}
<a href="javascript:;" data-tooltip="{{ _('Reply') }}" name="dczcomments__op-reply-comment"
data-form="{&quot;operation&quot;:&quot;{{reply_post_op}}&quot;, &quot;{{comment_ref}}&quot;:&quot;{{doc._id}}&quot;}"
><span class="icon icon-reply"></span></a>
{% endif %}
{% if doc.owner == handler.state.user._id or handler.has_perm(comment_edit_perm) %}
<a href="javascript:;" data-tooltip="{{ _('Edit') }}" name="dczcomments__op-edit-comment"
data-form="{&quot;operation&quot;:&quot;{{comment_edit_op}}&quot;,&quot;{{comment_ref}}&quot;:&quot;{{doc._id}}&quot;}"
><span class="icon icon-edit"></span></a>
{% endif %}
{% if doc.owner == handler.state.user._id or handler.has_perm(comment_delete_perm) %}
<a href="javascript:;" data-tooltip="{{ _('Delete') }}" name="dczcomments__op-delete-comment"
data-form="{&quot;operation&quot;:&quot;{{comment_delete_op}}&quot;,&quot;{{comment_ref}}&quot;:&quot;{{doc._id}}&quot;}"
><span class="icon icon-delete"></span></a>
{% endif %}
</div>
{% endmacro %}
{% macro reply_operations(doc, rdoc) %} {% macro reply_operations(doc, rdoc) %}
<div class="dczcomments__operations nojs--hide"> <div class="dczcomments__operations nojs--hide">
{% if handler.has_perm(reply_post_perm) %} {% if handler.has_perm(reply_post_perm) %}
@@ -135,7 +115,23 @@
{{ user.render_inline(udoc, avatar=false) }} {{ user.render_inline(udoc, avatar=false) }}
@ {{ datetime_span(doc['_id'])|safe }} @ {{ datetime_span(doc['_id'])|safe }}
</div> </div>
{{ comment_operations(doc) }} <div class="dczcomments__operations nojs--hide">
{% if handler.has_perm(reply_post_perm) %}
<a href="javascript:;" data-tooltip="{{ _('Reply') }}" name="dczcomments__op-reply-comment"
data-form="{&quot;operation&quot;:&quot;{{reply_post_op}}&quot;, &quot;{{comment_ref}}&quot;:&quot;{{doc._id}}&quot;}"
><span class="icon icon-reply"></span></a>
{% endif %}
{% if doc.owner == handler.state.user._id or handler.has_perm(comment_edit_perm) %}
<a href="javascript:;" data-tooltip="{{ _('Edit') }}" name="dczcomments__op-edit-comment"
data-form="{&quot;operation&quot;:&quot;{{comment_edit_op}}&quot;,&quot;{{comment_ref}}&quot;:&quot;{{doc._id}}&quot;}"
><span class="icon icon-edit"></span></a>
{% endif %}
{% if doc.owner == handler.state.user._id or handler.has_perm(comment_delete_perm) %}
<a href="javascript:;" data-tooltip="{{ _('Delete') }}" name="dczcomments__op-delete-comment"
data-form="{&quot;operation&quot;:&quot;{{comment_delete_op}}&quot;,&quot;{{comment_ref}}&quot;:&quot;{{doc._id}}&quot;}"
><span class="icon icon-delete"></span></a>
{% endif %}
</div>
</div> </div>
<div class="typo" data-emoji-enabled data-raw-url="{{ reverse_url('discussion_reply_raw', did=doc['parent_doc_id'], drid=doc['doc_id']) }}"> <div class="typo" data-emoji-enabled data-raw-url="{{ reverse_url('discussion_reply_raw', did=doc['parent_doc_id'], drid=doc['doc_id']) }}">
{{ doc['content']|markdown|safe }} {{ doc['content']|markdown|safe }}
+2 -2
View File
@@ -130,7 +130,7 @@
{% endif %} {% endif %}
</div> </div>
</div> </div>
<div class="typo" data-emoji-enabled data-raw-url="{{ reverse_url('problem_solution_raw', pid=doc['parent_doc_id'], psid=doc['doc_id']) }}"> <div class="typo" data-emoji-enabled data-raw-url="/p/{{doc.pid}}/solution/{{doc._id}}/raw">
{{ doc['content']|markdown|safe }} {{ doc['content']|markdown|safe }}
</div> </div>
<div class="commentbox-edit-target"></div> <div class="commentbox-edit-target"></div>
@@ -160,7 +160,7 @@
{% endif %} {% endif %}
</div> </div>
</div> </div>
<div class="typo" data-emoji-enabled data-raw-url="{{ reverse_url('problem_solution_reply_raw', pid=doc['parent_doc_id'], psid=doc['doc_id'], psrid=rdoc['_id']) }}"> <div class="typo" data-emoji-enabled data-raw-url="/p/{{doc.pid}}/solution/{{doc._id}}/{{rdoc._id}}/raw">
{{ rdoc['content']|markdown|safe }} {{ rdoc['content']|markdown|safe }}
</div> </div>
<div class="commentbox-edit-target"></div> <div class="commentbox-edit-target"></div>