wip
This commit is contained in:
parent
efea6b72e6
commit
84d25077b5
6 changed files with 10 additions and 19 deletions
64
src/common/functions/format.ts
Normal file
64
src/common/functions/format.ts
Normal file
|
@ -0,0 +1,64 @@
|
|||
import { config } from '../../config';
|
||||
import { User } from '../../backend/models/entities/user';
|
||||
import { Score } from '../types/score';
|
||||
import { defaultTemplate } from '../../backend/const';
|
||||
import { IUser } from '../types/user';
|
||||
|
||||
/**
|
||||
* 埋め込み変数の型
|
||||
*/
|
||||
export type Variable = {
|
||||
replace?: string | ((score: Score, user: User) => string);
|
||||
};
|
||||
|
||||
/**
|
||||
* 埋め込み可能な変数のリスト
|
||||
*/
|
||||
export const variables: Record<string, Variable> = {
|
||||
notesCount: {
|
||||
replace: (score) => String(score.notesCount),
|
||||
},
|
||||
followingCount: {
|
||||
replace: (score) => String(score.followingCount),
|
||||
},
|
||||
followersCount: {
|
||||
replace: (score) => String(score.followersCount),
|
||||
},
|
||||
notesDelta: {
|
||||
replace: (score) => String(score.notesDelta),
|
||||
},
|
||||
followingDelta: {
|
||||
replace: (score) => String(score.followingDelta),
|
||||
},
|
||||
followersDelta: {
|
||||
replace: (score) => String(score.followersDelta),
|
||||
},
|
||||
url: {
|
||||
replace: config.url,
|
||||
},
|
||||
username: {
|
||||
replace: (_, user) => String(user.username),
|
||||
},
|
||||
host: {
|
||||
replace: (_, user) => String(user.host),
|
||||
},
|
||||
rating: {
|
||||
replace: (_, user) => String(user.rating),
|
||||
},
|
||||
};
|
||||
|
||||
const variableRegex = /\{([a-zA-Z0-9_]+?)\}/g;
|
||||
|
||||
/**
|
||||
* スコア情報とユーザー情報からテキストを生成する
|
||||
* @param score スコア情報
|
||||
* @param user ユーザー情報
|
||||
* @returns 生成したテキスト
|
||||
*/
|
||||
export const format = (score: Score, user: IUser): string => {
|
||||
const template = user.template || defaultTemplate;
|
||||
return template.replace(variableRegex, (m, name) => {
|
||||
const v = variables[name];
|
||||
return !v || !v.replace ? m : typeof v.replace === 'function' ? v.replace(score, user) : v.replace;
|
||||
}) + '\n\n#misshaialert';
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue