テスト送信
This commit is contained in:
parent
4ae971fe8d
commit
0f30337fc3
3 changed files with 86 additions and 62 deletions
|
@ -11,6 +11,7 @@ import { api, apiAvailable } from '../services/misskey';
|
||||||
import { getScores } from '../functions/get-scores';
|
import { getScores } from '../functions/get-scores';
|
||||||
import { AlertMode, alertModes } from '../types/AlertMode';
|
import { AlertMode, alertModes } from '../types/AlertMode';
|
||||||
import { Users } from '../models';
|
import { Users } from '../models';
|
||||||
|
import { send } from '../services/send';
|
||||||
|
|
||||||
export const router = new Router<DefaultState, Context>();
|
export const router = new Router<DefaultState, Context>();
|
||||||
|
|
||||||
|
@ -64,12 +65,14 @@ router.get('/', async ctx => {
|
||||||
user,
|
user,
|
||||||
usersCount: await getUserCount(),
|
usersCount: await getUserCount(),
|
||||||
score: await getScores(user),
|
score: await getScores(user),
|
||||||
|
from: ctx.query.from,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// 非ログイン
|
// 非ログイン
|
||||||
await ctx.render('welcome', {
|
await ctx.render('welcome', {
|
||||||
usersCount: await getUserCount(),
|
usersCount: await getUserCount(),
|
||||||
welcomeMessage: welcomeMessage[Math.floor(Math.random() * welcomeMessage.length)],
|
welcomeMessage: welcomeMessage[Math.floor(Math.random() * welcomeMessage.length)],
|
||||||
|
from: ctx.query.from,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -117,42 +120,6 @@ router.get('/login', async ctx => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get('/logout', async ctx => {
|
|
||||||
const token = ctx.cookies.get('token');
|
|
||||||
if (!token) {
|
|
||||||
await die(ctx, 'ログインしていません');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ctx.cookies.set('token', '');
|
|
||||||
await ctx.render('welcome', {
|
|
||||||
usersCount: await getUserCount(),
|
|
||||||
welcomeMessage: 'ログアウトしました。',
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
router.get('/optout', async ctx => {
|
|
||||||
const token = ctx.cookies.get('token');
|
|
||||||
if (!token) {
|
|
||||||
await die(ctx, 'ログインしていません');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ctx.cookies.set('token', '');
|
|
||||||
|
|
||||||
const u = await getUserByMisshaiToken(token);
|
|
||||||
|
|
||||||
if (!u) {
|
|
||||||
await die(ctx);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
await deleteUser(u.username, u.host);
|
|
||||||
|
|
||||||
await ctx.render('welcome', {
|
|
||||||
usersCount: await getUserCount(),
|
|
||||||
welcomeMessage: '連携を解除しました。',
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
router.get('/terms', async ctx => {
|
router.get('/terms', async ctx => {
|
||||||
await ctx.render('term');
|
await ctx.render('term');
|
||||||
});
|
});
|
||||||
|
@ -242,7 +209,56 @@ router.post('/update-settings', async ctx => {
|
||||||
|
|
||||||
await Users.update(u.id, { alertMode: mode });
|
await Users.update(u.id, { alertMode: mode });
|
||||||
|
|
||||||
ctx.redirect('/');
|
ctx.redirect('/?from=updateSettings');
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
router.post('/logout', async ctx => {
|
||||||
|
const token = ctx.cookies.get('token');
|
||||||
|
if (!token) {
|
||||||
|
await die(ctx, 'ログインしていません');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ctx.cookies.set('token', '');
|
||||||
|
ctx.redirect('/?from=logout');
|
||||||
|
});
|
||||||
|
|
||||||
|
router.post('/optout', async ctx => {
|
||||||
|
const token = ctx.cookies.get('token');
|
||||||
|
if (!token) {
|
||||||
|
await die(ctx, 'ログインしていません');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ctx.cookies.set('token', '');
|
||||||
|
|
||||||
|
const u = await getUserByMisshaiToken(token);
|
||||||
|
|
||||||
|
if (!u) {
|
||||||
|
await die(ctx);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await deleteUser(u.username, u.host);
|
||||||
|
|
||||||
|
ctx.redirect('/?from=optout');
|
||||||
|
});
|
||||||
|
|
||||||
|
router.post('/send', async ctx => {
|
||||||
|
const token = ctx.cookies.get('token');
|
||||||
|
if (!token) {
|
||||||
|
await die(ctx, 'ログインしていません');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const u = await getUserByMisshaiToken(token);
|
||||||
|
|
||||||
|
if (!u) {
|
||||||
|
await die(ctx);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await send(u).catch(() => die(ctx));
|
||||||
|
ctx.redirect('/?from=send');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
30
src/services/send.ts
Normal file
30
src/services/send.ts
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
import { User } from '../models/entities/user';
|
||||||
|
import { format } from '../functions/format';
|
||||||
|
import { getScores } from '../functions/get-scores';
|
||||||
|
import { api } from './misskey';
|
||||||
|
|
||||||
|
export const send = async (user: User): Promise<void> => {
|
||||||
|
const text = format(await getScores(user));
|
||||||
|
|
||||||
|
if (user.alertMode === 'note') {
|
||||||
|
console.info(`send ${user.username}@${user.host}'s misshaialert as a note`);
|
||||||
|
const res = await api<Record<string, unknown>>(user.host, 'notes/create', {
|
||||||
|
text,
|
||||||
|
}, user.token);
|
||||||
|
if (res.error) {
|
||||||
|
throw res.error;
|
||||||
|
}
|
||||||
|
} else if (user.alertMode === 'notification') {
|
||||||
|
console.log(`send ${user.username}@${user.host}'s misshaialert as a notification`);
|
||||||
|
const res = await api(user.host, 'notifications/create', {
|
||||||
|
header: 'みす廃あらーと',
|
||||||
|
icon: 'https://i.imgur.com/B991yTl.png',
|
||||||
|
body: text,
|
||||||
|
}, user.token);
|
||||||
|
if (res.error) {
|
||||||
|
throw res.error;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.info(`will not send ${user.username}@${user.host}'s misshaialert`);
|
||||||
|
}
|
||||||
|
};
|
|
@ -7,6 +7,7 @@ import { format } from '../functions/format';
|
||||||
import { deleteUser } from '../functions/users';
|
import { deleteUser } from '../functions/users';
|
||||||
import { updateScore } from '../functions/update-score';
|
import { updateScore } from '../functions/update-score';
|
||||||
import { getScores } from '../functions/get-scores';
|
import { getScores } from '../functions/get-scores';
|
||||||
|
import { send } from './send';
|
||||||
|
|
||||||
export default (): void => {
|
export default (): void => {
|
||||||
cron.schedule('0 0 0 * * *', async () => {
|
cron.schedule('0 0 0 * * *', async () => {
|
||||||
|
@ -15,31 +16,7 @@ export default (): void => {
|
||||||
.getMany();
|
.getMany();
|
||||||
for (const user of users) {
|
for (const user of users) {
|
||||||
try {
|
try {
|
||||||
const text = format(await getScores(user));
|
await send(user);
|
||||||
await updateScore(user);
|
|
||||||
|
|
||||||
if (user.alertMode === 'note') {
|
|
||||||
console.info(`send ${user.username}@${user.host}'s misshaialert as a note`);
|
|
||||||
const res = await api<Record<string, unknown>>(user.host, 'notes/create', {
|
|
||||||
text,
|
|
||||||
}, user.token);
|
|
||||||
if (res.error) {
|
|
||||||
throw res.error;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
} else if (user.alertMode === 'notification') {
|
|
||||||
console.log(`send ${user.username}@${user.host}'s misshaialert as a notification`);
|
|
||||||
const res = await api(user.host, 'notifications/create', {
|
|
||||||
header: 'みす廃あらーと',
|
|
||||||
icon: 'https://i.imgur.com/B991yTl.png',
|
|
||||||
body: text,
|
|
||||||
}, user.token);
|
|
||||||
if (res.error) {
|
|
||||||
throw res.error;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
console.info(`will not send ${user.username}@${user.host}'s misshaialert`);
|
|
||||||
}
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e.code === 'NO_SUCH_USER' || e.code === 'AUTHENTICATION_FAILED') {
|
if (e.code === 'NO_SUCH_USER' || e.code === 'AUTHENTICATION_FAILED') {
|
||||||
// ユーザーが削除されている場合、レコードからも消してとりやめ
|
// ユーザーが削除されている場合、レコードからも消してとりやめ
|
||||||
|
@ -51,6 +28,7 @@ export default (): void => {
|
||||||
} finally {
|
} finally {
|
||||||
if (user.alertMode === 'note')
|
if (user.alertMode === 'note')
|
||||||
await delay(3000);
|
await delay(3000);
|
||||||
|
await updateScore(user);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue