swパッケージに

This commit is contained in:
tamaina 2021-11-20 14:44:59 +09:00
parent 8db1585f79
commit 6c2a27756c
30 changed files with 429 additions and 20 deletions

View 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);
}

View 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();
};

View file

@ -0,0 +1,3 @@
export default function(user: { name?: string | null, username: string }): string {
return user.name || user.username;
}

View 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;
}
}
}

View 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();
}