swパッケージに
This commit is contained in:
parent
8db1585f79
commit
6c2a27756c
30 changed files with 429 additions and 20 deletions
7
packages/sw/src/scripts/get-account-from-id.ts
Normal file
7
packages/sw/src/scripts/get-account-from-id.ts
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
import { get } from 'idb-keyval';
|
||||
|
||||
export async function getAccountFromId(id: string) {
|
||||
const accounts = await get('accounts') as { token: string; id: string; }[];
|
||||
if (!accounts) console.log('Accounts are not recorded');
|
||||
return accounts.find(e => e.id === id);
|
||||
}
|
||||
55
packages/sw/src/scripts/get-note-summary.ts
Normal file
55
packages/sw/src/scripts/get-note-summary.ts
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
import * as misskey from 'misskey-js';
|
||||
import { I18n } from '@/scripts/i18n';
|
||||
|
||||
/**
|
||||
* 投稿を表す文字列を取得します。
|
||||
* @param {*} note (packされた)投稿
|
||||
*/
|
||||
export const getNoteSummary = (note: misskey.entities.Note, i18n: I18n<any>): string => {
|
||||
if (note.deletedAt) {
|
||||
return `(${i18n.locale.deletedNote})`;
|
||||
}
|
||||
|
||||
if (note.isHidden) {
|
||||
return `(${i18n.locale.invisibleNote})`;
|
||||
}
|
||||
|
||||
let summary = '';
|
||||
|
||||
// 本文
|
||||
if (note.cw != null) {
|
||||
summary += note.cw;
|
||||
} else {
|
||||
summary += note.text ? note.text : '';
|
||||
}
|
||||
|
||||
// ファイルが添付されているとき
|
||||
if ((note.files || []).length != 0) {
|
||||
summary += ` (${i18n.t('withNFiles', { n: note.files.length })})`;
|
||||
}
|
||||
|
||||
// 投票が添付されているとき
|
||||
if (note.poll) {
|
||||
summary += ` (${i18n.locale.poll})`;
|
||||
}
|
||||
|
||||
// 返信のとき
|
||||
if (note.replyId) {
|
||||
if (note.reply) {
|
||||
summary += `\n\nRE: ${getNoteSummary(note.reply, i18n)}`;
|
||||
} else {
|
||||
summary += '\n\nRE: ...';
|
||||
}
|
||||
}
|
||||
|
||||
// Renoteのとき
|
||||
if (note.renoteId) {
|
||||
if (note.renote) {
|
||||
summary += `\n\nRN: ${getNoteSummary(note.renote, i18n)}`;
|
||||
} else {
|
||||
summary += '\n\nRN: ...';
|
||||
}
|
||||
}
|
||||
|
||||
return summary.trim();
|
||||
};
|
||||
3
packages/sw/src/scripts/get-user-name.ts
Normal file
3
packages/sw/src/scripts/get-user-name.ts
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export default function(user: { name?: string | null, username: string }): string {
|
||||
return user.name || user.username;
|
||||
}
|
||||
33
packages/sw/src/scripts/i18n.ts
Normal file
33
packages/sw/src/scripts/i18n.ts
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
export class I18n<T extends Record<string, any>> {
|
||||
public locale: T;
|
||||
|
||||
constructor(locale: T) {
|
||||
this.locale = locale;
|
||||
|
||||
//#region BIND
|
||||
this.t = this.t.bind(this);
|
||||
//#endregion
|
||||
}
|
||||
|
||||
// string にしているのは、ドット区切りでのパス指定を許可するため
|
||||
// なるべくこのメソッド使うよりもlocale直接参照の方がvueのキャッシュ効いてパフォーマンスが良いかも
|
||||
public t(key: string, args?: Record<string, any>): string {
|
||||
try {
|
||||
let str = key.split('.').reduce((o, i) => o[i], this.locale as T | any | string);
|
||||
|
||||
if (typeof str !== 'string') {
|
||||
return key;
|
||||
}
|
||||
|
||||
if (args) {
|
||||
for (const [k, v] of Object.entries(args)) {
|
||||
str = str.replace(`{${k}}`, v);
|
||||
}
|
||||
}
|
||||
return str;
|
||||
} catch (e) {
|
||||
console.warn(`missing localization '${key}'`);
|
||||
return key;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
packages/sw/src/scripts/login-id.ts
Normal file
11
packages/sw/src/scripts/login-id.ts
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
export function getUrlWithLoginId(url: string, loginId: string) {
|
||||
const u = new URL(url, origin);
|
||||
u.searchParams.append('loginId', loginId);
|
||||
return u.toString();
|
||||
}
|
||||
|
||||
export function getUrlWithoutLoginId(url: string) {
|
||||
const u = new URL(url);
|
||||
u.searchParams.delete('loginId');
|
||||
return u.toString();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue