0
0
Fork 0

コメント追加したり

This commit is contained in:
xeltica 2021-09-04 18:24:11 +09:00
parent 1c45e759c3
commit b9575d2c5b
22 changed files with 945 additions and 809 deletions

View file

@ -7,46 +7,58 @@ import { MiUser, updateScore } from '../functions/update-score';
import { updateRating } from '../functions/update-rating';
import { AlertMode } from '../../common/types/alert-mode';
import { Users } from '../models';
import { send } from './send';
import { sendAlert } from './send-alert';
import { api } from './misskey';
import * as Store from '../store';
import { User } from '../models/entities/user';
export default (): void => {
cron.schedule('0 0 0 * * *', async () => {
Store.dispatch({
nowCalculating: true,
});
const users = await Users.find({
alertMode: Not<AlertMode>('nothing'),
});
Store.dispatch({ nowCalculating: true });
const users = await Users.find({ alertMode: Not<AlertMode>('nothing') });
for (const user of users) {
try {
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 send(user);
await update(user).catch(e => handleError(user, e));
await updateScore(user, miUser);
if (user.alertMode === 'note')
await delay(3000);
} catch (e: any) {
if (e.code) {
if (e.code === 'NO_SUCH_USER' || e.code === 'AUTHENTICATION_FAILED') {
// ユーザーが削除されている場合、レコードからも消してとりやめ
console.info(`${user.username}@${user.host} is deleted, so delete this user from the system`);
await deleteUser(user.username, user.host);
} else {
console.error(`Misskey Error: ${JSON.stringify(e)}`);
}
} else {
// おそらく通信エラー
console.error(`Unknown error: ${e.name} ${e.message}`);
}
if (user.alertMode === 'note') {
return delay(3000);
}
}
Store.dispatch({
nowCalculating: false,
});
Store.dispatch({ nowCalculating: false });
});
};
/**
*
* @param user
*/
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') {
// ユーザーが削除されている場合、レコードからも消してとりやめ
console.info(`${user.username}@${user.host} is deleted, so delete this user from the system`);
await deleteUser(user.username, user.host);
} else {
console.error(`Misskey Error: ${JSON.stringify(e)}`);
}
} else {
// おそらく通信エラー
console.error(`Unknown error: ${e.name} ${e.message}`);
}
};