mirror of https://github.com/msgbyte/tailchat
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.0 KiB
TypeScript
40 lines
1.0 KiB
TypeScript
|
5 years ago
|
import {
|
||
|
|
regSocketEventListener,
|
||
|
|
getGlobalState,
|
||
|
|
getCachedUserInfo,
|
||
|
|
} from '@capital/common';
|
||
|
|
|
||
|
|
export function initNotify() {
|
||
|
|
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') {
|
||
|
5 years ago
|
Promise.all([getCachedUserInfo(message.author)]).then(
|
||
|
|
([userInfo]) => {
|
||
|
|
const nickname = userInfo?.nickname ?? '';
|
||
|
|
const icon = userInfo?.avatar ?? undefined;
|
||
|
|
const content = message.content;
|
||
|
5 years ago
|
|
||
|
5 years ago
|
new Notification(`来自 ${nickname}`, {
|
||
|
|
body: content,
|
||
|
|
icon,
|
||
|
|
tag: 'tailchat-message',
|
||
|
|
renotify: true,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
);
|
||
|
5 years ago
|
}
|
||
|
|
}
|
||
|
|
},
|
||
|
|
});
|
||
|
|
}
|