2019-03-02 10:51:59 +01:00
|
|
|
import $ from 'cafy';
|
|
|
|
import define from '../../define';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { Logs } from '../../../../models';
|
|
|
|
import { Brackets } from 'typeorm';
|
2019-03-02 10:51:59 +01:00
|
|
|
|
|
|
|
export const meta = {
|
2021-03-06 14:34:11 +01:00
|
|
|
desc: {
|
|
|
|
'ja-JP': 'ログを表示します。',
|
|
|
|
'en-US': 'Show logs.'
|
|
|
|
},
|
|
|
|
|
2019-03-02 10:51:59 +01:00
|
|
|
tags: ['admin'],
|
|
|
|
|
2020-02-15 13:33:32 +01:00
|
|
|
requireCredential: true as const,
|
2019-03-02 10:51:59 +01:00
|
|
|
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
|
|
|
|
}
|
2021-03-06 14:34:11 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
res: {
|
|
|
|
type: 'array' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
items: {
|
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
properties: {
|
|
|
|
id: {
|
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
format: 'id',
|
|
|
|
description: 'The unique identifier for this log.',
|
|
|
|
example: 'xxxxxxxxxx',
|
|
|
|
},
|
|
|
|
createdAt: {
|
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
format: 'date-time',
|
|
|
|
description: 'The date that the Drive file was created on Misskey.'
|
|
|
|
},
|
|
|
|
domain: {
|
|
|
|
type: 'array' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
items: {
|
|
|
|
type: 'string' as const,
|
|
|
|
optional: true as const, nullable: false as const
|
|
|
|
}
|
|
|
|
},
|
|
|
|
level: {
|
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const
|
|
|
|
},
|
|
|
|
worker: {
|
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const
|
|
|
|
},
|
|
|
|
machine: {
|
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
description: 'The name of the running machine.'
|
|
|
|
},
|
|
|
|
message: {
|
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
description: 'Log body.'
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-02 10:51:59 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default define(meta, async (ps) => {
|
2019-04-07 14:50:36 +02:00
|
|
|
const query = Logs.createQueryBuilder('log');
|
|
|
|
|
|
|
|
if (ps.level) query.andWhere('log.level = :level', { level: ps.level });
|
2019-03-02 10:51:59 +01:00
|
|
|
|
|
|
|
if (ps.domain) {
|
2019-04-07 14:50:36 +02:00
|
|
|
const whiteDomains = ps.domain.split(' ').filter(x => !x.startsWith('-'));
|
2019-04-13 07:11:15 +02:00
|
|
|
const blackDomains = ps.domain.split(' ').filter(x => x.startsWith('-')).map(x => x.substr(1));
|
2019-04-07 14:50:36 +02: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-05-15 10:05:41 +02:00
|
|
|
qb.andWhere(new Brackets(qb => {
|
|
|
|
const subDomains = blackDomain.split('.');
|
|
|
|
let i = 0;
|
|
|
|
for (const subDomain of subDomains) {
|
|
|
|
const p = `blackSubDomain_${subDomain}_${i}`;
|
|
|
|
// 全体で否定できないのでド・モルガンの法則で
|
|
|
|
// !(P && Q) を !P || !Q で表す
|
|
|
|
// SQL is 1 based, so we need '+ 1'
|
|
|
|
qb.orWhere(`log.domain[${i + 1}] != :${p}`, { [p]: subDomain });
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}));
|
2019-04-07 14:50:36 +02:00
|
|
|
}
|
|
|
|
}));
|
2019-03-02 10:51:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 18:43:22 +02:00
|
|
|
const logs = await query.orderBy('log.createdAt', 'DESC').take(ps.limit!).getMany();
|
2019-03-02 10:51:59 +01:00
|
|
|
|
|
|
|
return logs;
|
|
|
|
});
|