2019-03-02 03:51:59 -06:00
|
|
|
import $ from 'cafy';
|
|
|
|
import define from '../../define';
|
2019-04-07 07:50:36 -05:00
|
|
|
import { Logs } from '../../../../models';
|
|
|
|
import { Brackets } from 'typeorm';
|
2019-03-02 03:51:59 -06:00
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
tags: ['admin'],
|
|
|
|
|
|
|
|
requireCredential: true,
|
|
|
|
requireModerator: true,
|
|
|
|
|
|
|
|
params: {
|
|
|
|
limit: {
|
|
|
|
validator: $.optional.num.range(1, 100),
|
|
|
|
default: 30
|
|
|
|
},
|
|
|
|
|
|
|
|
level: {
|
|
|
|
validator: $.optional.nullable.str,
|
|
|
|
default: null as any
|
|
|
|
},
|
|
|
|
|
|
|
|
domain: {
|
|
|
|
validator: $.optional.nullable.str,
|
|
|
|
default: null as any
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default define(meta, async (ps) => {
|
2019-04-07 07:50:36 -05:00
|
|
|
const query = Logs.createQueryBuilder('log');
|
|
|
|
|
|
|
|
if (ps.level) query.andWhere('log.level = :level', { level: ps.level });
|
2019-03-02 03:51:59 -06:00
|
|
|
|
|
|
|
if (ps.domain) {
|
2019-04-07 07:50:36 -05:00
|
|
|
const whiteDomains = ps.domain.split(' ').filter(x => !x.startsWith('-'));
|
2019-04-13 00:11:15 -05:00
|
|
|
const blackDomains = ps.domain.split(' ').filter(x => x.startsWith('-')).map(x => x.substr(1));
|
2019-04-07 07:50:36 -05:00
|
|
|
|
|
|
|
if (whiteDomains.length > 0) {
|
|
|
|
query.andWhere(new Brackets(qb => {
|
|
|
|
for (const whiteDomain of whiteDomains) {
|
|
|
|
let i = 0;
|
|
|
|
for (const subDomain of whiteDomain.split('.')) {
|
|
|
|
const p = `whiteSubDomain_${subDomain}_${i}`;
|
|
|
|
// SQL is 1 based, so we need '+ 1'
|
|
|
|
qb.orWhere(`log.domain[${i + 1}] = :${p}`, { [p]: subDomain });
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (blackDomains.length > 0) {
|
|
|
|
query.andWhere(new Brackets(qb => {
|
|
|
|
for (const blackDomain of blackDomains) {
|
2019-04-13 00:11:15 -05:00
|
|
|
const subDomains = blackDomain.split('.');
|
2019-04-07 07:50:36 -05:00
|
|
|
let i = 0;
|
2019-04-13 00:11:15 -05:00
|
|
|
for (const subDomain of subDomains) {
|
2019-04-07 07:50:36 -05:00
|
|
|
const p = `blackSubDomain_${subDomain}_${i}`;
|
2019-04-13 00:11:15 -05:00
|
|
|
if (i === subDomains.length - 1) {
|
|
|
|
// SQL is 1 based, so we need '+ 1'
|
|
|
|
qb.andWhere(`log.domain[${i + 1}] != :${p}`, { [p]: subDomain });
|
|
|
|
} else {
|
|
|
|
// SQL is 1 based, so we need '+ 1'
|
|
|
|
qb.andWhere(`log.domain[${i + 1}] == :${p}`, { [p]: subDomain });
|
|
|
|
}
|
2019-04-07 07:50:36 -05:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}));
|
2019-03-02 03:51:59 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 11:43:22 -05:00
|
|
|
const logs = await query.orderBy('log.createdAt', 'DESC').take(ps.limit!).getMany();
|
2019-03-02 03:51:59 -06:00
|
|
|
|
|
|
|
return logs;
|
|
|
|
});
|