mirror of https://github.com/msgbyte/tailchat
refactor: 增加通知插件
parent
1fff755586
commit
14436c63df
@ -0,0 +1,8 @@
|
|||||||
|
import { buildRegList } from './buildRegList';
|
||||||
|
|
||||||
|
interface PluginSocketEventListener {
|
||||||
|
eventName: string;
|
||||||
|
eventFn: (...args: any[]) => void;
|
||||||
|
}
|
||||||
|
export const [socketEventListeners, regSocketEventListener] =
|
||||||
|
buildRegList<PluginSocketEventListener>();
|
@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"label": "消息通知插件",
|
||||||
|
"name": "com.msgbyte.notify",
|
||||||
|
"url": "/plugins/com.msgbyte.notify/index.js",
|
||||||
|
"version": "0.0.0",
|
||||||
|
"author": "msgbyte",
|
||||||
|
"description": "为应用增加通知的能力",
|
||||||
|
"requireRestart": true
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"name": "@plugins/com.msgbyte.notify",
|
||||||
|
"main": "src/index.ts",
|
||||||
|
"version": "0.0.0",
|
||||||
|
"private": true,
|
||||||
|
"dependencies": {}
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
import {
|
||||||
|
regSocketEventListener,
|
||||||
|
getGlobalState,
|
||||||
|
getCachedUserInfo,
|
||||||
|
} from '@capital/common';
|
||||||
|
|
||||||
|
if (Notification.permission === 'default') {
|
||||||
|
Notification.requestPermission();
|
||||||
|
}
|
||||||
|
|
||||||
|
regSocketEventListener({
|
||||||
|
eventName: 'chat.message.add',
|
||||||
|
eventFn: (message) => {
|
||||||
|
const currentUserId = getGlobalState()?.user.info._id;
|
||||||
|
if (currentUserId !== message.author) {
|
||||||
|
// 创建通知
|
||||||
|
|
||||||
|
// TODO: 需要增加所在群组
|
||||||
|
if (Notification.permission === 'granted') {
|
||||||
|
Promise.all([getCachedUserInfo(currentUserId)]).then(([userInfo]) => {
|
||||||
|
console.log(userInfo, message);
|
||||||
|
const nickname = userInfo?.nickname ?? '';
|
||||||
|
const icon = userInfo?.avatar ?? undefined;
|
||||||
|
const content = message.content;
|
||||||
|
|
||||||
|
new Notification(`来自 ${nickname}`, {
|
||||||
|
body: content,
|
||||||
|
icon,
|
||||||
|
tag: 'tailchat-message',
|
||||||
|
renotify: true,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"rootDir": "./src",
|
||||||
|
"baseUrl": "./src",
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"jsx": "react",
|
||||||
|
"paths": {
|
||||||
|
"@capital/*": ["../../../src/plugin/*"],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
|
||||||
|
# yarn lockfile v1
|
||||||
|
|
||||||
|
|
@ -0,0 +1,13 @@
|
|||||||
|
import type { AppStore, AppState } from 'tailchat-shared';
|
||||||
|
|
||||||
|
let _store: AppStore;
|
||||||
|
export function setGlobalStore(store: AppStore) {
|
||||||
|
_store = store;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getGlobalState(): AppState | null {
|
||||||
|
if (!_store) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return _store.getState();
|
||||||
|
}
|
Loading…
Reference in New Issue