mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 17:38:19 +08:00
问题修复
This commit is contained in:
1 parent
4c2edc3948
commit
26c467f60f
10 files changed
+99
-99
No files matched your search
@@ -19,11 +19,13 @@ process.on('restart', async () => {
|
||||
const path = require('path');
|
||||
const i18n = require('./lib/i18n');
|
||||
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');
|
||||
global.bus = new EventEmitter();
|
||||
async function run() {
|
||||
require('./utils');
|
||||
require('./service/db');
|
||||
await new Promise((resolve) => {
|
||||
global.bus.once('connected', () => {
|
||||
|
||||
+5
-43
@@ -8,7 +8,7 @@ const
|
||||
options = require('../options'),
|
||||
perm = require('../permission'),
|
||||
builtin = require('../model/builtin'),
|
||||
md5 = require('../lib/md5'),
|
||||
misc=require('../lib/misc'),
|
||||
{ MIDDLEWARE } = require('../service/server'),
|
||||
{ NotFoundError } = require('../error');
|
||||
|
||||
@@ -29,33 +29,6 @@ class Markdown extends MarkdownIt {
|
||||
}
|
||||
}
|
||||
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 {
|
||||
constructor() {
|
||||
super(
|
||||
@@ -71,25 +44,14 @@ class Nunjucks extends nunjucks.Environment {
|
||||
this.addFilter('markdown', function (self) {
|
||||
return md.render(self);
|
||||
});
|
||||
this.addFilter('gravatar_url', function (email, size) {
|
||||
return `//gravatar.loli.net/avatar/${md5(email.toString().trim().toLowerCase())}?d=mm&s=${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.addFilter('gravatar_url', misc.gravatar_url);
|
||||
this.addFilter('format_size', misc.format_size);
|
||||
this.addGlobal('typeof', o => typeof o);
|
||||
this.addGlobal('console', console);
|
||||
this.addGlobal('static_url', str => `/${str}`);
|
||||
this.addGlobal('reverse_url', str => str);
|
||||
this.addGlobal('datetime_span', datetime_span);
|
||||
this.addGlobal('paginate', paginate);
|
||||
this.addGlobal('datetime_span', misc.datetime_span);
|
||||
this.addGlobal('paginate', misc.paginate);
|
||||
this.addGlobal('perm', perm);
|
||||
this.addGlobal('builtin', builtin);
|
||||
this.addGlobal('status', builtin.STATUS);
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
const
|
||||
Mongo = require('mongodb'),
|
||||
builtin = require('./model/builtin'),
|
||||
{ pwhash } = require('./utils'),
|
||||
pwhash = require('./lib/pwhash'),
|
||||
options = require('./options');
|
||||
|
||||
async function run() {
|
||||
|
||||
@@ -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]);
|
||||
};
|
||||
@@ -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
@@ -1,7 +1,7 @@
|
||||
const
|
||||
system = require('./system'),
|
||||
{ UserNotFoundError, UserAlreadyExistError } = require('../error'),
|
||||
{ pwhash } = require('../utils'),
|
||||
pwhash = require('./lib/pwhash'),
|
||||
validator = require('../lib/validator'),
|
||||
db = require('../service/db'),
|
||||
coll = db.collection('user'),
|
||||
|
||||
@@ -60,6 +60,7 @@ function ROUTE(route, handler) {
|
||||
let args = Object.assign({}, ctx.params, ctx.query, ctx.request.body);
|
||||
if (args.content) validator.checkContent(args.content);
|
||||
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[`_${method}`]) await h[`_${method}`](args);
|
||||
|
||||
+2
-30
@@ -1,6 +1,4 @@
|
||||
const
|
||||
crypto = require('crypto'),
|
||||
map = [
|
||||
const map = [
|
||||
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
|
||||
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
|
||||
'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',
|
||||
'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8',
|
||||
'9', '0'
|
||||
];
|
||||
];
|
||||
|
||||
String.random = function (digit) {
|
||||
let str = '';
|
||||
@@ -16,29 +14,3 @@ String.random = function (digit) {
|
||||
str += map[Math.floor(Math.random() * 62)];
|
||||
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();
|
||||
}
|
||||
};
|
||||
@@ -26,26 +26,6 @@
|
||||
</li>
|
||||
{% 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="{"operation":"{{reply_post_op}}", "{{comment_ref}}":"{{doc._id}}"}"
|
||||
><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="{"operation":"{{comment_edit_op}}","{{comment_ref}}":"{{doc._id}}"}"
|
||||
><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="{"operation":"{{comment_delete_op}}","{{comment_ref}}":"{{doc._id}}"}"
|
||||
><span class="icon icon-delete"></span></a>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endmacro %}
|
||||
|
||||
{% macro reply_operations(doc, rdoc) %}
|
||||
<div class="dczcomments__operations nojs--hide">
|
||||
{% if handler.has_perm(reply_post_perm) %}
|
||||
@@ -135,7 +115,23 @@
|
||||
{{ user.render_inline(udoc, avatar=false) }}
|
||||
@ {{ datetime_span(doc['_id'])|safe }}
|
||||
</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="{"operation":"{{reply_post_op}}", "{{comment_ref}}":"{{doc._id}}"}"
|
||||
><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="{"operation":"{{comment_edit_op}}","{{comment_ref}}":"{{doc._id}}"}"
|
||||
><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="{"operation":"{{comment_delete_op}}","{{comment_ref}}":"{{doc._id}}"}"
|
||||
><span class="icon icon-delete"></span></a>
|
||||
{% endif %}
|
||||
</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']) }}">
|
||||
{{ doc['content']|markdown|safe }}
|
||||
|
||||
@@ -130,7 +130,7 @@
|
||||
{% endif %}
|
||||
</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 }}
|
||||
</div>
|
||||
<div class="commentbox-edit-target"></div>
|
||||
@@ -160,7 +160,7 @@
|
||||
{% endif %}
|
||||
</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 }}
|
||||
</div>
|
||||
<div class="commentbox-edit-target"></div>
|
||||
|
||||
Reference in new issue
Block a user