summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2021-03-22 12:36:57 +0900
committersyuilo <Syuilotan@yahoo.co.jp>2021-03-22 12:36:57 +0900
commit1f0abef0843a3da364965a3327e7c0eba00b015a (patch)
tree5732940c0d6f8cdaecdc9bb175566965353688e7 /src
parentrefactoring (diff)
downloadsharkey-1f0abef0843a3da364965a3327e7c0eba00b015a.tar.gz
sharkey-1f0abef0843a3da364965a3327e7c0eba00b015a.tar.bz2
sharkey-1f0abef0843a3da364965a3327e7c0eba00b015a.zip
refactor: extract functions
Diffstat (limited to 'src')
-rw-r--r--src/misc/populate-emojis.ts38
1 files changed, 26 insertions, 12 deletions
diff --git a/src/misc/populate-emojis.ts b/src/misc/populate-emojis.ts
index f3056fdebd..f21e24b219 100644
--- a/src/misc/populate-emojis.ts
+++ b/src/misc/populate-emojis.ts
@@ -13,6 +13,30 @@ type PopulatedEmoji = {
url: string;
};
+function normalizeHost(src: string | undefined, noteUserHost: string | null): string | null {
+ // クエリに使うホスト
+ let host = src === '.' ? null // .はローカルホスト (ここがマッチするのはリアクションのみ)
+ : src === undefined ? noteUserHost // ノートなどでホスト省略表記の場合はローカルホスト (ここがリアクションにマッチすることはない)
+ : isSelfHost(src) ? null // 自ホスト指定
+ : (src || noteUserHost); // 指定されたホスト || ノートなどの所有者のホスト (こっちがリアクションにマッチすることはない)
+
+ host = toPunyNullable(host);
+
+ return host;
+}
+
+function parseEmojiStr(emojiName: string, noteUserHost: string | null) {
+ const match = emojiName.match(/^(\w+)(?:@([\w.-]+))?$/);
+ if (!match) return { name: null, host: null };
+
+ const name = match[1];
+
+ // ホスト正規化
+ const host = toPunyNullable(normalizeHost(match[2], noteUserHost));
+
+ return { name, host };
+}
+
/**
* 添付用絵文字情報を解決する
* @param emojiName ノートやユーザープロフィールに添付された、またはリアクションのカスタム絵文字名 (:は含めない, リアクションでローカルホストの場合は@.を付ける (これはdecodeReactionで可能))
@@ -20,18 +44,8 @@ type PopulatedEmoji = {
* @returns 絵文字情報, nullは未マッチを意味する
*/
export async function populateEmoji(emojiName: string, noteUserHost: string | null): Promise<PopulatedEmoji | null> {
- const match = emojiName.match(/^(\w+)(?:@([\w.-]+))?$/);
- if (!match) return null;
-
- const name = match[1];
-
- // クエリに使うホスト
- let host = match[2] === '.' ? null // .はローカルホスト (ここがマッチするのはリアクションのみ)
- : match[2] === undefined ? noteUserHost // ノートなどでホスト省略表記の場合はローカルホスト (ここがリアクションにマッチすることはない)
- : isSelfHost(match[2]) ? null // 自ホスト指定
- : (match[2] || noteUserHost); // 指定されたホスト || ノートなどの所有者のホスト (こっちがリアクションにマッチすることはない)
-
- host = toPunyNullable(host);
+ const { name, host } = parseEmojiStr(emojiName, noteUserHost);
+ if (name == null) return null;
const queryOrNull = async () => (await Emojis.findOne({
name,