feat: Improve Push Notification (#7667)
* clean up * ev => data * refactor * clean up * add type * antenna * channel * fix * add Packed type * add PackedRef * fix lint * add emoji schema * add reversiGame * add reversiMatching * remove signin schema (use Signin entity) * add schemas refs, fix Packed type * wip PackedHoge => Packed<'Hoge'> * add Packed type * note-reaction * user * user-group * user-list * note * app, messaging-message * notification * drive-file * drive-folder * following * muting * blocking * hashtag * page * app (with modifying schema) * import user? * channel * antenna * clip * gallery-post * emoji * Packed * reversi-matching * update stream.ts * https://github.com/misskey-dev/misskey/pull/7769#issuecomment-917542339 * fix lint * clean up? * add app * fix * nanka iroiro * wip * wip * fix lint * fix loginId * fix * refactor * refactor * remove follow action * clean up * Revert "remove follow action" This reverts commitdefbb41648
. * Revert "clean up" This reverts commitf94919cb9c
. * remove fetch specification * renoteの条件追加 * apiFetch => cli * bypass fetch? * fix * refactor: use path alias * temp: add submodule * remove submodule * enhane: unison-reloadに指定したパスに移動できるように * null * null * feat: ログインするアカウントのIDをクエリ文字列で指定する機能 * null * await? * rename * rename * Update read.ts * merge * get-note-summary * fix * swパッケージに * add missing packages * fix getNoteSummary * add webpack-cli * ✌️ * remove plugins * sw-inject分離したがテストしてない * fix notification.vue * remove a blank line * disconnect intersection observer * disconnect2 * fix notification.vue * remove a blank line * disconnect intersection observer * disconnect2 * fix * ✌️ * clean up config * typesを戻した * Update packages/client/src/components/notification.vue Co-authored-by: Acid Chicken (硫酸鶏) <root@acid-chicken.com> * disconnect * oops * Failed to load the script unexpectedly回避 sw.jsとlib.tsを分離してみた * truncate notification * Update packages/client/src/ui/_common_/common.vue Co-authored-by: syuilo <Syuilotan@yahoo.co.jp> * clean up * clean up * キャッシュ対策 * Truncate push notification message * クライアントがあったらストリームに接続しているということなので通知しない判定の位置を修正 * components/drive-file-thumbnail.vue * components/drive-select-dialog.vue * components/drive-window.vue * merge * fix * Service Workerのビルドにesbuildを使うようにする * return createEmptyNotification() * fix * i18n.ts * update * ✌️ * remove ts-loader * fix * fix * enhance: Service Workerを常に登録するように * pollEnded * URLをsw.jsに戻す * clean up Co-authored-by: Acid Chicken (硫酸鶏) <root@acid-chicken.com> Co-authored-by: syuilo <Syuilotan@yahoo.co.jp>
This commit is contained in:
237
packages/sw/src/scripts/create-notification.ts
Normal file
237
packages/sw/src/scripts/create-notification.ts
Normal file
@@ -0,0 +1,237 @@
|
||||
/*
|
||||
* Notification manager for SW
|
||||
*/
|
||||
declare var self: ServiceWorkerGlobalScope;
|
||||
|
||||
import { swLang } from '@/scripts/lang';
|
||||
import { cli } from '@/scripts/operations';
|
||||
import { pushNotificationDataMap } from '@/types';
|
||||
import getUserName from '@/scripts/get-user-name';
|
||||
import { I18n } from '@/scripts/i18n';
|
||||
import { getAccountFromId } from '@/scripts/get-account-from-id';
|
||||
|
||||
export async function createNotification<K extends keyof pushNotificationDataMap>(data: pushNotificationDataMap[K]) {
|
||||
const n = await composeNotification(data);
|
||||
|
||||
if (n) {
|
||||
return self.registration.showNotification(...n);
|
||||
} else {
|
||||
console.error('Could not compose notification', data);
|
||||
return createEmptyNotification();
|
||||
}
|
||||
}
|
||||
|
||||
async function composeNotification<K extends keyof pushNotificationDataMap>(data: pushNotificationDataMap[K]): Promise<[string, NotificationOptions] | null> {
|
||||
if (!swLang.i18n) swLang.fetchLocale();
|
||||
const i18n = await swLang.i18n as I18n<any>;
|
||||
const { t } = i18n;
|
||||
switch (data.type) {
|
||||
/*
|
||||
case 'driveFileCreated': // TODO (Server Side)
|
||||
return [t('_notification.fileUploaded'), {
|
||||
body: body.name,
|
||||
icon: body.url,
|
||||
data
|
||||
}];
|
||||
*/
|
||||
case 'notification':
|
||||
switch (data.body.type) {
|
||||
case 'follow':
|
||||
// users/showの型定義をswos.apiへ当てはめるのが困難なのでapiFetch.requestを直接使用
|
||||
const account = await getAccountFromId(data.userId);
|
||||
if (!account) return null;
|
||||
const userDetail = await cli.request('users/show', { userId: data.body.userId }, account.token);
|
||||
return [t('_notification.youWereFollowed'), {
|
||||
body: getUserName(data.body.user),
|
||||
icon: data.body.user.avatarUrl,
|
||||
data,
|
||||
actions: userDetail.isFollowing ? [] : [
|
||||
{
|
||||
action: 'follow',
|
||||
title: t('_notification._actions.followBack')
|
||||
}
|
||||
],
|
||||
}];
|
||||
|
||||
case 'mention':
|
||||
return [t('_notification.youGotMention', { name: getUserName(data.body.user) }), {
|
||||
body: data.body.note.text || '',
|
||||
icon: data.body.user.avatarUrl,
|
||||
data,
|
||||
actions: [
|
||||
{
|
||||
action: 'reply',
|
||||
title: t('_notification._actions.reply')
|
||||
}
|
||||
],
|
||||
}];
|
||||
|
||||
case 'reply':
|
||||
return [t('_notification.youGotReply', { name: getUserName(data.body.user) }), {
|
||||
body: data.body.note.text || '',
|
||||
icon: data.body.user.avatarUrl,
|
||||
data,
|
||||
actions: [
|
||||
{
|
||||
action: 'reply',
|
||||
title: t('_notification._actions.reply')
|
||||
}
|
||||
],
|
||||
}];
|
||||
|
||||
case 'renote':
|
||||
return [t('_notification.youRenoted', { name: getUserName(data.body.user) }), {
|
||||
body: data.body.note.text || '',
|
||||
icon: data.body.user.avatarUrl,
|
||||
data,
|
||||
actions: [
|
||||
{
|
||||
action: 'showUser',
|
||||
title: getUserName(data.body.user)
|
||||
}
|
||||
],
|
||||
}];
|
||||
|
||||
case 'quote':
|
||||
return [t('_notification.youGotQuote', { name: getUserName(data.body.user) }), {
|
||||
body: data.body.note.text || '',
|
||||
icon: data.body.user.avatarUrl,
|
||||
data,
|
||||
actions: [
|
||||
{
|
||||
action: 'reply',
|
||||
title: t('_notification._actions.reply')
|
||||
},
|
||||
...((data.body.note.visibility === 'public' || data.body.note.visibility === 'home') ? [
|
||||
{
|
||||
action: 'renote',
|
||||
title: t('_notification._actions.renote')
|
||||
}
|
||||
] : [])
|
||||
],
|
||||
}];
|
||||
|
||||
case 'reaction':
|
||||
return [`${data.body.reaction} ${getUserName(data.body.user)}`, {
|
||||
body: data.body.note.text || '',
|
||||
icon: data.body.user.avatarUrl,
|
||||
data,
|
||||
actions: [
|
||||
{
|
||||
action: 'showUser',
|
||||
title: getUserName(data.body.user)
|
||||
}
|
||||
],
|
||||
}];
|
||||
|
||||
case 'pollVote':
|
||||
return [t('_notification.youGotPoll', { name: getUserName(data.body.user) }), {
|
||||
body: data.body.note.text || '',
|
||||
icon: data.body.user.avatarUrl,
|
||||
data,
|
||||
}];
|
||||
|
||||
case 'pollEnded':
|
||||
return [t('_notification.pollEnded'), {
|
||||
body: data.body.note.text || '',
|
||||
data,
|
||||
}];
|
||||
|
||||
case 'receiveFollowRequest':
|
||||
return [t('_notification.youReceivedFollowRequest'), {
|
||||
body: getUserName(data.body.user),
|
||||
icon: data.body.user.avatarUrl,
|
||||
data,
|
||||
actions: [
|
||||
{
|
||||
action: 'accept',
|
||||
title: t('accept')
|
||||
},
|
||||
{
|
||||
action: 'reject',
|
||||
title: t('reject')
|
||||
}
|
||||
],
|
||||
}];
|
||||
|
||||
case 'followRequestAccepted':
|
||||
return [t('_notification.yourFollowRequestAccepted'), {
|
||||
body: getUserName(data.body.user),
|
||||
icon: data.body.user.avatarUrl,
|
||||
data,
|
||||
}];
|
||||
|
||||
case 'groupInvited':
|
||||
return [t('_notification.youWereInvitedToGroup', { userName: getUserName(data.body.user) }), {
|
||||
body: data.body.invitation.group.name,
|
||||
data,
|
||||
actions: [
|
||||
{
|
||||
action: 'accept',
|
||||
title: t('accept')
|
||||
},
|
||||
{
|
||||
action: 'reject',
|
||||
title: t('reject')
|
||||
}
|
||||
],
|
||||
}];
|
||||
|
||||
case 'app':
|
||||
return [data.body.header || data.body.body, {
|
||||
body: data.body.header && data.body.body,
|
||||
icon: data.body.icon,
|
||||
data
|
||||
}];
|
||||
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
case 'unreadMessagingMessage':
|
||||
if (data.body.groupId === null) {
|
||||
return [t('_notification.youGotMessagingMessageFromUser', { name: getUserName(data.body.user) }), {
|
||||
icon: data.body.user.avatarUrl,
|
||||
tag: `messaging:user:${data.body.userId}`,
|
||||
data,
|
||||
renotify: true,
|
||||
}];
|
||||
}
|
||||
return [t('_notification.youGotMessagingMessageFromGroup', { name: data.body.group.name }), {
|
||||
icon: data.body.user.avatarUrl,
|
||||
tag: `messaging:group:${data.body.groupId}`,
|
||||
data,
|
||||
renotify: true,
|
||||
}];
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function createEmptyNotification() {
|
||||
return new Promise<void>(async res => {
|
||||
if (!swLang.i18n) swLang.fetchLocale();
|
||||
const i18n = await swLang.i18n as I18n<any>;
|
||||
const { t } = i18n;
|
||||
|
||||
await self.registration.showNotification(
|
||||
t('_notification.emptyPushNotificationMessage'),
|
||||
{
|
||||
silent: true,
|
||||
tag: 'read_notification',
|
||||
}
|
||||
);
|
||||
|
||||
res();
|
||||
|
||||
setTimeout(async () => {
|
||||
for (const n of
|
||||
[
|
||||
...(await self.registration.getNotifications({ tag: 'user_visible_auto_notification' })),
|
||||
...(await self.registration.getNotifications({ tag: 'read_notification' }))
|
||||
]
|
||||
) {
|
||||
n.close();
|
||||
}
|
||||
}, 1000);
|
||||
});
|
||||
}
|
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);
|
||||
}
|
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;
|
||||
}
|
29
packages/sw/src/scripts/i18n.ts
Normal file
29
packages/sw/src/scripts/i18n.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
export class I18n<T extends Record<string, any>> {
|
||||
public ts: T;
|
||||
|
||||
constructor(locale: T) {
|
||||
this.ts = locale;
|
||||
|
||||
//#region BIND
|
||||
this.t = this.t.bind(this);
|
||||
//#endregion
|
||||
}
|
||||
|
||||
// string にしているのは、ドット区切りでのパス指定を許可するため
|
||||
// なるべくこのメソッド使うよりもlocale直接参照の方がvueのキャッシュ効いてパフォーマンスが良いかも
|
||||
public t(key: string, args?: Record<string, string>): string {
|
||||
try {
|
||||
let str = key.split('.').reduce((o, i) => o[i], this.ts) as unknown as string;
|
||||
|
||||
if (args) {
|
||||
for (const [k, v] of Object.entries(args)) {
|
||||
str = str.replace(`{${k}}`, v);
|
||||
}
|
||||
}
|
||||
return str;
|
||||
} catch (err) {
|
||||
console.warn(`missing localization '${key}'`);
|
||||
return key;
|
||||
}
|
||||
}
|
||||
}
|
47
packages/sw/src/scripts/lang.ts
Normal file
47
packages/sw/src/scripts/lang.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Language manager for SW
|
||||
*/
|
||||
declare var self: ServiceWorkerGlobalScope;
|
||||
|
||||
import { get, set } from 'idb-keyval';
|
||||
import { I18n } from '@/scripts/i18n';
|
||||
|
||||
class SwLang {
|
||||
public cacheName = `mk-cache-${_VERSION_}`;
|
||||
|
||||
public lang: Promise<string> = get('lang').then(async prelang => {
|
||||
if (!prelang) return 'en-US';
|
||||
return prelang;
|
||||
});
|
||||
|
||||
public setLang(newLang: string) {
|
||||
this.lang = Promise.resolve(newLang);
|
||||
set('lang', newLang);
|
||||
return this.fetchLocale();
|
||||
}
|
||||
|
||||
public i18n: Promise<I18n<any>> | null = null;
|
||||
|
||||
public fetchLocale() {
|
||||
return this.i18n = this._fetch();
|
||||
}
|
||||
|
||||
private async _fetch() {
|
||||
// Service Workerは何度も起動しそのたびにlocaleを読み込むので、CacheStorageを使う
|
||||
const localeUrl = `/assets/locales/${await this.lang}.${_VERSION_}.json`;
|
||||
let localeRes = await caches.match(localeUrl);
|
||||
|
||||
// _DEV_がtrueの場合は常に最新化
|
||||
if (!localeRes || _DEV_) {
|
||||
localeRes = await fetch(localeUrl);
|
||||
const clone = localeRes?.clone();
|
||||
if (!clone?.clone().ok) Error('locale fetching error');
|
||||
|
||||
caches.open(this.cacheName).then(cache => cache.put(localeUrl, clone));
|
||||
}
|
||||
|
||||
return new I18n(await localeRes.json());
|
||||
}
|
||||
}
|
||||
|
||||
export const swLang = new SwLang();
|
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();
|
||||
}
|
50
packages/sw/src/scripts/notification-read.ts
Normal file
50
packages/sw/src/scripts/notification-read.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
declare var self: ServiceWorkerGlobalScope;
|
||||
|
||||
import { get } from 'idb-keyval';
|
||||
import { pushNotificationDataMap } from '@/types';
|
||||
import { api } from '@/scripts/operations';
|
||||
|
||||
type Accounts = {
|
||||
[x: string]: {
|
||||
queue: string[],
|
||||
timeout: number | null
|
||||
}
|
||||
};
|
||||
|
||||
class SwNotificationReadManager {
|
||||
private accounts: Accounts = {};
|
||||
|
||||
public async construct() {
|
||||
const accounts = await get('accounts');
|
||||
if (!accounts) Error('Accounts are not recorded');
|
||||
|
||||
this.accounts = accounts.reduce((acc, e) => {
|
||||
acc[e.id] = {
|
||||
queue: [],
|
||||
timeout: null
|
||||
};
|
||||
return acc;
|
||||
}, {} as Accounts);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
// プッシュ通知の既読をサーバーに送信
|
||||
public async read<K extends keyof pushNotificationDataMap>(data: pushNotificationDataMap[K]) {
|
||||
if (data.type !== 'notification' || !(data.userId in this.accounts)) return;
|
||||
|
||||
const account = this.accounts[data.userId];
|
||||
|
||||
account.queue.push(data.body.id as string);
|
||||
|
||||
// 最後の呼び出しから200ms待ってまとめて処理する
|
||||
if (account.timeout) clearTimeout(account.timeout);
|
||||
account.timeout = setTimeout(() => {
|
||||
account.timeout = null;
|
||||
|
||||
api('notifications/read', data.userId, { notificationIds: account.queue });
|
||||
}, 200);
|
||||
}
|
||||
}
|
||||
|
||||
export const swNotificationRead = (new SwNotificationReadManager()).construct();
|
70
packages/sw/src/scripts/operations.ts
Normal file
70
packages/sw/src/scripts/operations.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Operations
|
||||
* 各種操作
|
||||
*/
|
||||
declare var self: ServiceWorkerGlobalScope;
|
||||
|
||||
import * as Misskey from 'misskey-js';
|
||||
import { SwMessage, swMessageOrderType } from '@/types';
|
||||
import { acct as getAcct } from '@/filters/user';
|
||||
import { getAccountFromId } from '@/scripts/get-account-from-id';
|
||||
import { getUrlWithLoginId } from '@/scripts/login-id';
|
||||
|
||||
export const cli = new Misskey.api.APIClient({ origin, fetch: (...args) => fetch(...args) });
|
||||
|
||||
export async function api<E extends keyof Misskey.Endpoints>(endpoint: E, userId: string, options?: Misskey.Endpoints[E]['req']) {
|
||||
const account = await getAccountFromId(userId);
|
||||
if (!account) return;
|
||||
|
||||
return cli.request(endpoint, options, account.token);
|
||||
}
|
||||
|
||||
// rendered acctからユーザーを開く
|
||||
export function openUser(acct: string, loginId: string) {
|
||||
return openClient('push', `/@${acct}`, loginId, { acct });
|
||||
}
|
||||
|
||||
// noteIdからノートを開く
|
||||
export function openNote(noteId: string, loginId: string) {
|
||||
return openClient('push', `/notes/${noteId}`, loginId, { noteId });
|
||||
}
|
||||
|
||||
export async function openChat(body: any, loginId: string) {
|
||||
if (body.groupId === null) {
|
||||
return openClient('push', `/my/messaging/${getAcct(body.user)}`, loginId, { body });
|
||||
} else {
|
||||
return openClient('push', `/my/messaging/group/${body.groupId}`, loginId, { body });
|
||||
}
|
||||
}
|
||||
|
||||
// post-formのオプションから投稿フォームを開く
|
||||
export async function openPost(options: any, loginId: string) {
|
||||
// クエリを作成しておく
|
||||
let url = `/share?`;
|
||||
if (options.initialText) url += `text=${options.initialText}&`;
|
||||
if (options.reply) url += `replyId=${options.reply.id}&`;
|
||||
if (options.renote) url += `renoteId=${options.renote.id}&`;
|
||||
|
||||
return openClient('post', url, loginId, { options });
|
||||
}
|
||||
|
||||
export async function openClient(order: swMessageOrderType, url: string, loginId: string, query: any = {}) {
|
||||
const client = await findClient();
|
||||
|
||||
if (client) {
|
||||
client.postMessage({ type: 'order', ...query, order, loginId, url } as SwMessage);
|
||||
return client;
|
||||
}
|
||||
|
||||
return self.clients.openWindow(getUrlWithLoginId(url, loginId));
|
||||
}
|
||||
|
||||
export async function findClient() {
|
||||
const clients = await self.clients.matchAll({
|
||||
type: 'window'
|
||||
});
|
||||
for (const c of clients) {
|
||||
if (c.url.indexOf('?zen') < 0) return c;
|
||||
}
|
||||
return null;
|
||||
}
|
Reference in New Issue
Block a user