0
0
Fork 0

アラート機能の全面的な書き直し

close #67
This commit is contained in:
Xeltica 2023-02-23 19:37:10 +09:00
parent 6cecc7bec7
commit d2b3b4b9e7
13 changed files with 207 additions and 118 deletions

View file

@ -0,0 +1,8 @@
import {MisskeyError} from '../services/misskey';
export const errorToString = (e: Error) => {
if (e instanceof MisskeyError) {
return JSON.stringify(e.error);
}
return `${e.name}: ${e.message}\n${e.stack}`;
};

View file

@ -1,25 +1,17 @@
import { User } from '../models/entities/user'; import { User } from '../models/entities/user';
import { Score } from '../../common/types/score';
import { api } from '../services/misskey';
import { toSignedString } from '../../common/functions/to-signed-string'; import { toSignedString } from '../../common/functions/to-signed-string';
import {Count} from '../models/count';
/** /**
* *
* @param user * @param user
* @returns * @param count
* @returns
*/ */
export const getScores = async (user: User): Promise<Score> => { export const getDelta = (user: User, count: Count) => {
// TODO 毎回取ってくるのも微妙なので、ある程度キャッシュしたいかも
const miUser = await api<Record<string, number>>(user.host, 'users/show', { username: user.username }, user.token);
if (miUser.error) {
throw miUser.error;
}
return { return {
notesCount: miUser.notesCount, notesDelta: toSignedString(count.notesCount - user.prevNotesCount),
followingCount: miUser.followingCount, followingDelta: toSignedString(count.followingCount - user.prevFollowingCount),
followersCount: miUser.followersCount, followersDelta: toSignedString(count.followersCount - user.prevFollowersCount),
notesDelta: toSignedString(miUser.notesCount - user.prevNotesCount),
followingDelta: toSignedString(miUser.followingCount - user.prevFollowingCount),
followersDelta: toSignedString(miUser.followersCount - user.prevFollowersCount),
}; };
}; };

View file

@ -1,5 +1,6 @@
import { User } from '../models/entities/user'; import { User } from '../models/entities/user';
import { updateUser } from './users'; import { updateUser } from './users';
import {Count} from '../models/count';
/** /**
* Misskeyのユーザーモデル * Misskeyのユーザーモデル
@ -14,12 +15,12 @@ export type MiUser = {
/** /**
* *
* @param user * @param user
* @param miUser Misskeyのユーザー * @param count
*/ */
export const updateScore = async (user: User, miUser: MiUser): Promise<void> => { export const updateScore = async (user: User, count: Count): Promise<void> => {
await updateUser(user.username, user.host, { await updateUser(user.username, user.host, {
prevNotesCount: miUser.notesCount ?? 0, prevNotesCount: count.notesCount ?? 0,
prevFollowingCount: miUser.followingCount ?? 0, prevFollowingCount: count.followingCount ?? 0,
prevFollowersCount: miUser.followersCount ?? 0, prevFollowersCount: count.followersCount ?? 0,
}); });
}; };

View file

@ -0,0 +1,3 @@
export type Acct = `@${string}@${string}`;
export const toAcct = (it: {username: string, host: string}): Acct => `@${it.username}@${it.host}`;

View file

@ -0,0 +1,6 @@
export interface Count
{
notesCount: number;
followingCount: number;
followersCount: number;
}

View file

@ -1,16 +1,35 @@
import axios from 'axios'; import axios from 'axios';
import {printLog} from '../store';
import {delay} from '../utils/delay';
export const ua = `Mozilla/5.0 MisskeyTools +https://github.com/Xeltica/MisskeyTools Node/${process.version}`; export const ua = `Mozilla/5.0 MisskeyTools +https://github.com/shrimpia/misskey-tools Node/${process.version}`;
const RETRY_COUNT = 5;
/** /**
* Misskey APIを呼び出す * Misskey APIを呼び出す
*/ */
export const api = <T = Record<string, unknown>>(host: string, endpoint: string, arg: Record<string, unknown>, i?: string): Promise<T> => { export const api = async <T = Record<string, unknown>>(host: string, endpoint: string, arg: Record<string, unknown>, token?: string): Promise<T> => {
const a = { ...arg }; const a = { ...arg };
if (i) { if (token) {
a.i = i; a.i = token;
} }
return axios.post<T>(`https://${host}/api/${endpoint}`, a).then(res => res.data);
for (let i = 0; i < RETRY_COUNT; i++) {
let data: T;
try {
data = await axios.post<T>(`https://${host}/api/${endpoint}`, a).then(res => res.data);
} catch (e) {
printLog(`接続エラー: ${host}/api/${endpoint} リトライ中 (${i + 1} / ${RETRY_COUNT})\n${e}`, 'error');
await delay(3000);
continue;
}
if (!('error' in data)) {
return data;
}
throw new MisskeyError((data as any).error);
}
throw new TimedOutError();
}; };
/** /**
@ -27,3 +46,18 @@ export const apiAvailable = async (host: string, i: string): Promise<boolean> =>
return false; return false;
} }
}; };
export class TimedOutError extends Error {}
export class MisskeyError extends Error {
constructor(public error: MisskeyErrorObject) {
super();
}
}
export interface MisskeyErrorObject {
message: string;
code: string;
id: string;
kind: string;
}

View file

@ -1,36 +1,12 @@
import { User } from '../models/entities/user'; import { User } from '../models/entities/user';
import { format } from '../../common/functions/format';
import { getScores } from '../functions/get-scores';
import { api } from './misskey'; import { api } from './misskey';
/**
*
* @param user
*/
export const sendAlert = async (user: User) => {
const text = format(await getScores(user), user);
switch (user.alertMode) {
case 'note':
await sendNoteAlert(text, user);
break;
case 'notification':
await sendNotificationAlert(text, user);
break;
case 'both':
await Promise.all([
sendNotificationAlert(text, user),
sendNoteAlert(text, user),
]);
break;
}
};
/** /**
* *
* @param text * @param text
* @param user * @param user
*/ */
const sendNoteAlert = async (text: string, user: User) => { export const sendNoteAlert = async (text: string, user: User) => {
const res = await api<Record<string, unknown>>(user.host, 'notes/create', { const res = await api<Record<string, unknown>>(user.host, 'notes/create', {
text, text,
visibility: user.visibility, visibility: user.visibility,
@ -48,9 +24,9 @@ const sendNoteAlert = async (text: string, user: User) => {
* @param text * @param text
* @param user * @param user
*/ */
const sendNotificationAlert = async (text: string, user: User) => { export const sendNotificationAlert = async (text: string, user: User) => {
const res = await api(user.host, 'notifications/create', { const res = await api(user.host, 'notifications/create', {
header: 'みす廃あらーと', header: 'Misskey Tools',
icon: 'https://i.imgur.com/B991yTl.png', icon: 'https://i.imgur.com/B991yTl.png',
body: text, body: text,
}, user.token); }, user.token);

View file

@ -1,16 +1,23 @@
import cron from 'node-cron'; import cron from 'node-cron';
import delay from 'delay';
import { Not } from 'typeorm';
import { deleteUser } from '../functions/users'; import { deleteUser } from '../functions/users';
import { MiUser, updateScore } from '../functions/update-score'; import { MiUser, updateScore } from '../functions/update-score';
import { updateRating } from '../functions/update-rating'; import { updateRating } from '../functions/update-rating';
import { AlertMode } from '../../common/types/alert-mode';
import { Users } from '../models'; import { Users } from '../models';
import { sendAlert } from './send-alert'; import {sendNoteAlert, sendNotificationAlert} from './send-alert';
import { api } from './misskey'; import {api, MisskeyError, TimedOutError} from './misskey';
import * as Store from '../store'; import * as Store from '../store';
import { User } from '../models/entities/user'; import { User } from '../models/entities/user';
import {groupBy} from '../utils/group-by';
import {clearLog, printLog} from '../store';
import {errorToString} from '../functions/error-to-string';
import {Acct, toAcct} from '../models/acct';
import {Count} from '../models/count';
import {format} from '../../common/functions/format';
const ERROR_CODES_USER_REMOVED = ['NO_SUCH_USER', 'AUTHENTICATION_FAILED', 'YOUR_ACCOUNT_SUSPENDED'];
// TODO: Redisで持つようにしたい
const userScoreCache = new Map<Acct, Count>();
export default (): void => { export default (): void => {
cron.schedule('0 0 0 * * *', work); cron.schedule('0 0 0 * * *', work);
@ -20,71 +27,99 @@ export const work = async () => {
Store.dispatch({ nowCalculating: true }); Store.dispatch({ nowCalculating: true });
clearLog(); clearLog();
printLog('Started.'); printLog('Started.');
try { try {
const users = await Users.find({ alertMode: Not<AlertMode>('nothing') }); const users = await Users.find();
printLog('will process ' + users.length + ' accounts.'); const groupedUsers = groupBy(users, u => u.host);
for (const user of users) {
await update(user).catch(e => handleError(user, e));
printLog(`processed for ${user.username}@${user.host}`);
if (user.alertMode === 'note') { printLog(`${users.length} アカウントのレート計算を開始します。`);
await delay(3000); await calculateAllRating(groupedUsers);
} Store.dispatch({ nowCalculating: false });
}
printLog('finished successfully.'); printLog(`${users.length} アカウントのアラート送信を開始します。`);
await sendAllAlerts(groupedUsers);
printLog('ミス廃アラートワーカーは正常に完了しました。');
} catch (e) { } catch (e) {
const msg = String(e instanceof Error ? e.stack : e); printLog('ミス廃アラートワーカーが異常終了しました。', 'error');
printLog(msg); printLog(e instanceof Error ? errorToString(e) : e, 'error');
printLog('stopped wrongly.');
} finally { } finally {
Store.dispatch({ nowCalculating: false }); Store.dispatch({ nowCalculating: false });
} }
}; };
const clearLog = () => { const calculateAllRating = async (groupedUsers: [string, User[]][]) => {
Store.dispatch({ misshaiWorkerLog: [] }); return await Promise.all(groupedUsers.map(kv => calculateRating(...kv)));
}; };
const printLog = (log: any) => { const calculateRating = async (host: string, users: User[]) => {
Store.dispatch({ misshaiWorkerLog: [ for (const user of users) {
...Store.getState().misshaiWorkerLog, let miUser: MiUser;
String(log), try {
] }); miUser = await api<MiUser>(user.host, 'i', {}, user.token);
}; } catch (e) {
if (!(e instanceof Error)) {
/** printLog('バグエラーオブジェクトはErrorを継承していないといけない', 'error');
* } else if (e instanceof MisskeyError) {
* @param user if (ERROR_CODES_USER_REMOVED.includes(e.error.code)) {
*/
const update = async (user: User) => {
const miUser = await api<MiUser & { error: unknown }>(user.host, 'users/show', { username: user.username }, user.token);
if (miUser.error) throw miUser.error;
await updateRating(user, miUser);
await sendAlert(user);
await updateScore(user, miUser);
};
/**
*
* @param user
* @param e ErrorだったりObjectだったりするのでanyだけど
*/
const handleError = async (user: User, e: any) => {
if (e.code) {
if (e.code === 'NO_SUCH_USER' || e.code === 'AUTHENTICATION_FAILED') {
// ユーザーが削除されている場合、レコードからも消してとりやめ // ユーザーが削除されている場合、レコードからも消してとりやめ
printLog(`${user.username}@${user.host} is deleted, so delete this user from the system`); printLog(`アカウント ${toAcct(user)} は削除されているか、凍結されているか、トークンを失効しています。そのため、本システムからアカウントを削除します。`, 'warn');
await deleteUser(user.username, user.host); await deleteUser(user.username, user.host);
} else { } else {
printLog(`Misskey Error: ${JSON.stringify(e)}`); printLog(`Misskey エラー: ${JSON.stringify(e.error)}`, 'error');
} }
} else if (e instanceof TimedOutError) {
printLog(`サーバー ${user.host} との接続に失敗したため、このサーバーのレート計算を中断します。`, 'error');
return;
} else { } else {
// おそらく通信エラー // おそらく通信エラー
printLog(`Unknown error: ${e.name} ${e.message}`); printLog(`不明なエラーが発生しました。\n${errorToString(e)}`, 'error');
} }
continue;
}
userScoreCache.set(toAcct(user), miUser);
await updateRating(user, miUser);
}
printLog(`${host} ユーザー(${users.length}人) のレート計算が完了しました。`);
};
const sendAllAlerts = async (groupedUsers: [string, User[]][]) => {
return await Promise.all(groupedUsers.map(kv => sendAlerts(...kv)));
};
const sendAlerts = async (host: string, users: User[]) => {
const models = users
.map(user => {
const count = userScoreCache.get(toAcct(user));
if (count == null) return null;
return {
user,
count,
message: format(user, count),
};
})
.filter(u => u != null) as {user: User, count: Count, message: string}[];
// 何もしない
for (const {user, count} of models.filter(m => m.user.alertMode === 'nothing')) {
await updateScore(user, count);
}
// 通知
for (const {user, count, message} of models.filter(m => m.user.alertMode === 'notification' || m.user.alertMode === 'both')) {
await sendNotificationAlert(message, user);
if (user.alertMode === 'notification') {
await updateScore(user, count);
}
}
// 通知
for (const {user, count, message} of models.filter(m => m.user.alertMode === 'note')) {
await sendNoteAlert(message, user);
await updateScore(user, count);
}
printLog(`${host} ユーザー(${users.length}人) へのアラート送信が完了しました。`);
}; };

View file

@ -17,7 +17,7 @@ let _state: Readonly<State> = defaultState;
*/ */
export type State = { export type State = {
nowCalculating: boolean, nowCalculating: boolean,
misshaiWorkerLog: string[], misshaiWorkerLog: Log[],
}; };
/** /**
@ -36,3 +36,20 @@ export const dispatch = (mutation: Partial<State>) => {
...mutation, ...mutation,
}; };
}; };
export type Log = {
text: string;
level: 'error' | 'warn' | 'info';
timestamp: Date;
}
export const clearLog = () => {
dispatch({ misshaiWorkerLog: [] });
};
export const printLog = (log: unknown, level: Log['level'] = 'info') => {
dispatch({ misshaiWorkerLog: [
...getState().misshaiWorkerLog,
{ text: String(log), level, timestamp: new Date() },
] });
};

View file

@ -0,0 +1 @@
export const delay = (ms: number) => new Promise<void>(res => setTimeout(res, ms));

View file

@ -0,0 +1,13 @@
type GetKeyFunction<K extends PropertyKey, V> = (cur: V, idx: number, src: readonly V[]) => K;
export const groupBy = <K extends PropertyKey, V>(array: readonly V[], getKey: GetKeyFunction<K, V>) => {
return Array.from(
array.reduce((map, cur, idx, src) => {
const key = getKey(cur, idx, src);
const list = map.get(key);
if (list) list.push(cur);
else map.set(key, [cur]);
return map;
}, new Map<K, V[]>())
);
};

View file

@ -11,9 +11,6 @@ html
meta(property='og:title' content=title) meta(property='og:title' content=title)
meta(property='og:description' content=desc) meta(property='og:description' content=desc)
meta(property='og:type' content='website') meta(property='og:type' content='website')
meta(name='twitter:card' content='summary')
meta(name='twitter:site' content='@Xeltica')
meta(name='twitter:creator' content='@Xeltica')
link(rel="preload" href="https://koruri.chillout.chat/koruri.css") link(rel="preload" href="https://koruri.chillout.chat/koruri.css")
link(rel="preload", href="/assets/otadesign_rounded.woff") link(rel="preload", href="/assets/otadesign_rounded.woff")
link(rel="preload", href="/assets/otadesign_rounded.woff2") link(rel="preload", href="/assets/otadesign_rounded.woff2")

View file

@ -3,6 +3,8 @@ import { Score } from '../types/score';
import { defaultTemplate } from '../../backend/const'; import { defaultTemplate } from '../../backend/const';
import { IUser } from '../types/user'; import { IUser } from '../types/user';
import { createGacha } from './create-gacha'; import { createGacha } from './create-gacha';
import {Count} from '../../backend/models/count';
import {getDelta} from '../../backend/functions/get-scores';
/** /**
* *
@ -30,11 +32,15 @@ const variableRegex = /\{([a-zA-Z0-9_]+?)\}/g;
/** /**
* *
* @param score
* @param user * @param user
* @param count
* @returns * @returns
*/ */
export const format = (score: Score, user: IUser): string => { export const format = (user: IUser, count: Count): string => {
const score: Score = {
...count,
...getDelta(user, count),
};
const template = user.template || defaultTemplate; const template = user.template || defaultTemplate;
return template.replace(variableRegex, (m, name) => { return template.replace(variableRegex, (m, name) => {
const v = variables[name]; const v = variables[name];