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

@ -19,6 +19,9 @@ module.exports = {
'indent': [
'error',
'tab',
{
'SwitchCase': 1,
}
],
'quotes': [
'error',

View file

@ -15,6 +15,12 @@ export class RankingController {
return this.getResponse(getState().nowCalculating, limit ? Number(limit) : undefined);
}
/**
* DBに問い合わせてランキングを取得する
* @param isCalculating
* @param limit
* @returns
*/
private async getResponse(isCalculating: boolean, limit?: number) {
const ranking = isCalculating ? [] : (await getRanking(limit)).map((u) => ({
id: u.id,

View file

@ -2,6 +2,9 @@ import { config } from '../../config';
import { User } from '../models/entities/user';
import { Score } from '../../common/types/score';
/**
* 稿
*/
export const defaultTemplate = `昨日のMisskeyの活動は
: {notesCount}({notesDelta})
@ -11,11 +14,17 @@ export const defaultTemplate = `昨日のMisskeyの活動は
{url}`;
/**
*
*/
export type Variable = {
description?: string;
replace?: string | ((score: Score, user: User) => string);
};
/**
*
*/
export const variables: Record<string, Variable> = {
notesCount: {
description: 'ノート数',
@ -61,6 +70,12 @@ export const variables: Record<string, Variable> = {
const variableRegex = /\{([a-zA-Z0-9_]+?)\}/g;
/**
*
* @param score
* @param user
* @returns
*/
export const format = (score: Score, user: User): string => {
const template = user.template || defaultTemplate;
return template.replace(variableRegex, (m, name) => {

View file

@ -2,6 +2,9 @@ import rndstr from 'rndstr';
import { UsedToken } from '../models/entities/used-token';
import { UsedTokens } from '../models';
/**
*
*/
export const genToken = async (): Promise<string> => {
let used: UsedToken | undefined = undefined;
let token: string;

View file

@ -3,6 +3,11 @@ import { Score } from '../../common/types/score';
import { api } from '../services/misskey';
import { toSignedString } from './to-signed-string';
/**
*
* @param user
* @returns
*/
export const getScores = async (user: User): Promise<Score> => {
const miUser = await api<Record<string, number>>(user.host, 'users/show', { username: user.username }, user.token);
if (miUser.error) {

View file

@ -1,6 +1,11 @@
import { Users } from '../models';
import { User } from '../models/entities/user';
/**
*
* @param limit
* @returns
*/
export const getRanking = async (limit: number | null = 10): Promise<User[]> => {
const query = Users.createQueryBuilder('user')
.where('"user"."bannedFromRanking" IS NOT TRUE')

View file

@ -1 +1,6 @@
/**
*
* @param num
* @returns
*/
export const toSignedString = (num: number): string => num < 0 ? num.toString() : '+' + num;

View file

@ -4,6 +4,11 @@ import { User } from '../models/entities/user';
import { updateUser } from './users';
import { MiUser } from './update-score';
/**
*
* @param user
* @param miUser Misskeyのユーザー
*/
export const updateRating = async (user: User, miUser: MiUser): Promise<void> => {
const elapsedDays = dayjs().diff(dayjs(miUser.createdAt), 'd') + 1;
await updateUser(user.username, user.host, {

View file

@ -1,6 +1,9 @@
import { User } from '../models/entities/user';
import { updateUser } from './users';
/**
* Misskeyのユーザーモデル
*/
export type MiUser = {
notesCount: number,
followingCount: number,
@ -8,6 +11,11 @@ export type MiUser = {
createdAt: string,
};
/**
*
* @param user
* @param miUser Misskeyのユーザー
*/
export const updateScore = async (user: User, miUser: MiUser): Promise<void> => {
await updateUser(user.username, user.host, {
prevNotesCount: miUser.notesCount ?? 0,

View file

@ -2,12 +2,22 @@ import { User } from '../models/entities/user';
import { Users } from '../models';
import { DeepPartial } from 'typeorm';
import { genToken } from './gen-token';
import pick from 'object.pick';
/**
*
* @param username
* @param host
* @returns
*/
export const getUser = (username: string, host: string): Promise<User | undefined> => {
return Users.findOne({ username, host });
};
/**
*
* @param user
* @returns
*/
export const updateUsersMisshaiToken = async (user: User | User['id']): Promise<string> => {
const u = typeof user === 'number'
? user
@ -18,10 +28,21 @@ export const updateUsersMisshaiToken = async (user: User | User['id']): Promise<
return misshaiToken;
};
/**
*
* @param token
* @returns
*/
export const getUserByMisshaiToken = (token: string): Promise<User | undefined> => {
return Users.findOne({ misshaiToken: token });
};
/**
*
* @param username
* @param host
* @param token
*/
export const upsertUser = async (username: string, host: string, token: string): Promise<void> => {
const u = await getUser(username, host);
if (u) {
@ -31,14 +52,29 @@ export const upsertUser = async (username: string, host: string, token: string):
}
};
/**
*
* @param username
* @param host
* @param record
*/
export const updateUser = async (username: string, host: string, record: DeepPartial<User>): Promise<void> => {
await Users.update({ username, host }, record);
};
/**
*
* @param username
* @param host
*/
export const deleteUser = async (username: string, host: string): Promise<void> => {
await Users.delete({ username, host });
};
/**
*
* @returns
*/
export const getUserCount = (): Promise<number> => {
return Users.count();
};

View file

@ -11,7 +11,7 @@ import { upsertUser, getUser, updateUser, updateUsersMisshaiToken, getUserByMiss
import { api } from './services/misskey';
import { AlertMode, alertModes } from '../common/types/alert-mode';
import { Users } from './models';
import { send } from './services/send';
import { sendAlert } from './services/send-alert';
import { visibilities, Visibility } from '../common/types/visibility';
import { defaultTemplate } from './functions/format';
import { die } from './die';
@ -212,7 +212,7 @@ router.post('/send', async ctx => {
await die(ctx);
return;
}
await send(u).catch(() => die(ctx));
await sendAlert(u).catch(() => die(ctx));
ctx.redirect('/?from=send');
});

View file

@ -8,7 +8,13 @@ export const entities = [
UsedToken,
];
/**
*
* @param force
* @returns DBコネクション
*/
export const initDb = async (force = false): Promise<Connection> => {
// forceがtrueでない限り、既に接続が存在する場合はそれを返す
if (!force) {
try {
const conn = getConnection();
@ -19,6 +25,7 @@ export const initDb = async (force = false): Promise<Connection> => {
}
}
// 接続がないか、forceがtrueの場合は新規作成する
return createConnection({
type: 'postgres',
host: config.db.host,

View file

@ -4,9 +4,11 @@ import _const from '../const';
export const ua = `Mozilla/5.0 misshaialertBot/${_const.version} +https://github.com/Xeltica/misshaialert Node/${process.version}`;
axios.defaults.headers['User-Agent'] = ua;
axios.defaults.validateStatus = (stat) => stat < 500;
/**
* Misskey APIを呼び出す
*/
export const api = <T = Record<string, unknown>>(host: string, endpoint: string, arg: Record<string, unknown>, i?: string): Promise<T> => {
const a = { ...arg };
if (i) {
@ -15,6 +17,12 @@ export const api = <T = Record<string, unknown>>(host: string, endpoint: string,
return axios.post<T>(`https://${host}/api/${endpoint}`, a).then(res => res.data);
};
/**
*
* @param host
* @param i
* @returns truefalse
*/
export const apiAvailable = async (host: string, i: string): Promise<boolean> => {
try {
const res = await api(host, 'i', {}, i);

View file

@ -0,0 +1,55 @@
import { User } from '../models/entities/user';
import { format } from '../functions/format';
import { getScores } from '../functions/get-scores';
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;
}
};
/**
*
* @param text
* @param user
*/
const sendNoteAlert = async (text: string, user: User) => {
const res = await api<Record<string, unknown>>(user.host, 'notes/create', {
text,
visibility: user.visibility,
localOnly: user.localOnly,
remoteFollowersOnly: user.remoteFollowersOnly,
}, user.token);
if (res.error) {
throw res.error || res;
}
};
/**
*
* @param text
* @param user
*/
const sendNotificationAlert = async (text: string, user: User) => {
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 || res;
}
};

View file

@ -1,34 +0,0 @@
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), user);
if (user.alertMode === 'note') {
console.info(`send ${user.username}@${user.host}'s misshaialert as a note`);
const opts = {
text,
visibility: user.visibility,
} as Record<string, unknown>;
if (user.localOnly) opts.localOnly = user.localOnly;
if (user.remoteFollowersOnly) opts.remoteFollowersOnly = user.remoteFollowersOnly;
const res = await api<Record<string, unknown>>(user.host, 'notes/create', opts, user.token);
if (res.error) {
throw res.error || res;
}
} else if (user.alertMode === 'notification') {
console.info(`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 || res;
}
} else {
console.info(`will not send ${user.username}@${user.host}'s misshaialert`);
}
};

View file

@ -7,30 +7,48 @@ 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 {
await update(user).catch(e => handleError(user, e));
if (user.alertMode === 'note') {
return delay(3000);
}
}
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 send(user);
await sendAlert(user);
await updateScore(user, miUser);
};
if (user.alertMode === 'note')
await delay(3000);
} catch (e: any) {
/**
*
* @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') {
// ユーザーが削除されている場合、レコードからも消してとりやめ
@ -43,10 +61,4 @@ export default (): void => {
// おそらく通信エラー
console.error(`Unknown error: ${e.name} ${e.message}`);
}
}
}
Store.dispatch({
nowCalculating: false,
});
});
};

View file

@ -2,18 +2,32 @@
// getStateを介してステートを取得し、dispatchによって更新する
// stateを直接編集できないようになっている
/**
*
*/
const defaultState: State = {
nowCalculating: false,
};
let _state: Readonly<State> = defaultState;
/**
*
*/
export type State = {
nowCalculating: boolean,
};
export const getState = () => Object.freeze({..._state});
/**
*
* @returns
*/
export const getState = () => Object.freeze({ ..._state });
/**
*
* @param mutation
*/
export const dispatch = (mutation: Partial<State>) => {
_state = {
..._state,

View file

@ -4,10 +4,10 @@ import { BrowserRouter, Link, Route, Switch, useLocation } from 'react-router-do
import { IndexPage } from './pages';
import { RankingPage } from './pages/ranking';
import { Header } from './components/Header';
import { TermPage } from './pages/term';
import 'xeltica-ui/dist/css/xeltica-ui.min.css';
import './style.scss';
import { TermPage } from './pages/term';
const AppInner : React.VFC = () => {
const $location = useLocation();

View file

@ -0,0 +1,30 @@
import React, { useMemo } from 'react';
export type TabItem = {
label: string;
};
export type TabProps = {
items: TabItem[];
selected: number;
onSelect: (index: number) => void;
};
// タブコンポーネント
export const Tab: React.FC<TabProps> = (props) => {
return (
<div className="tab">
{props.items.map((item, index) => {
return (
<button
key={index}
className={'item ' + (index === props.selected ? 'selected' : '')}
onClick={() => props.onSelect(index)}
>
{item.label}
</button>
);
})}
</div>
);
};

View file

@ -7,3 +7,27 @@ body {
margin: auto;
max-width: 720px;
}
.tab {
.item {
position: relative;
padding: var(--margin);
&.active {
color: var(--primary);
transition: width 0.2s ease;
&::after {
content: "";
width: 100%;
}
}
&::after {
content: "";
bottom: 0;
left: 0;
right: 0;
height: 2px;
width: 0;
background-color: var(--primary);
}
}
}

1379
yarn.lock

File diff suppressed because it is too large Load diff