SHA256
Добавить предпросмотр DM в push-уведомлениях
This commit is contained in:
+35
-2
@@ -1,3 +1,5 @@
|
||||
import { isDmPreviewHidden } from './services/dm-preview-settings.js';
|
||||
import { dmPreviewTarget, dmPreviewText, fetchDmPreview } from './services/dm-preview.js';
|
||||
import { initI18n, onLanguageChange, t } from './i18n/index.js';
|
||||
import { preventPagePinchZoom } from './services/pwa-viewport-gestures.js';
|
||||
preventPagePinchZoom();
|
||||
@@ -737,12 +739,16 @@ async function tryLockPortraitOrientation() {
|
||||
}
|
||||
}
|
||||
|
||||
async function notifyHiddenIncomingMessage(fromLogin, toLogin, unreadCount, messageKey, notificationVersionMs) {
|
||||
async function notifyHiddenIncomingMessage(fromLogin, toLogin, unreadCount, messageKey, notificationVersionMs, plaintext = '') {
|
||||
const title = t('remaining.app.message_from_login', { login: fromLogin });
|
||||
const body = dmUnreadLabel(unreadCount);
|
||||
let body = (!await isDmPreviewHidden() && dmPreviewText(plaintext)) || dmUnreadLabel(unreadCount);
|
||||
try {
|
||||
const registration = await navigator.serviceWorker?.getRegistration?.();
|
||||
if (registration?.showNotification) {
|
||||
const tag = dmNotificationTag(toLogin, fromLogin);
|
||||
const existing = await registration.getNotifications({ tag });
|
||||
if (existing.some(row => row.data?.messageKey === messageKey || Number(row.data?.notificationVersionMs || 0) > notificationVersionMs)) return;
|
||||
if (await isDmPreviewHidden()) body = dmUnreadLabel(unreadCount);
|
||||
await registration.showNotification(title, {
|
||||
body,
|
||||
tag: dmNotificationTag(toLogin, fromLogin),
|
||||
@@ -1707,6 +1713,30 @@ async function init() {
|
||||
if ('serviceWorker' in navigator) {
|
||||
navigator.serviceWorker.addEventListener('message', (event) => {
|
||||
const data = event?.data || {};
|
||||
if (data.type === 'SHINE_DM_PREVIEW_REQUEST') {
|
||||
const port = event.ports?.[0];
|
||||
if (!port) return;
|
||||
void (async () => {
|
||||
let text = '';
|
||||
try {
|
||||
const { login, sessionId, storagePwdInMemory: storagePwd } = state.session;
|
||||
const payload = data.payload || {};
|
||||
if (state.session.isAuthorized && wsIsOpen() && authService.ws.isSessionReady()
|
||||
&& dmPreviewTarget(payload, login) && !await isDmPreviewHidden()) {
|
||||
text = await fetchDmPreview({ auth: authService, payload, login, storagePwd,
|
||||
getDirectMessages: (params) => {
|
||||
// Guard against a concurrent logout/reconnect; never reconnect for a preview.
|
||||
if (state.session.sessionId !== sessionId || !wsIsOpen() || !authService.ws.isSessionReady()) return {};
|
||||
return authService.ws.request('GetDirectMessages', params, 7000, { existingOnly: true })
|
||||
.then(response => response.status === 200 ? response.payload : {});
|
||||
} });
|
||||
if (state.session.sessionId !== sessionId || !state.session.isAuthorized || await isDmPreviewHidden()) text = '';
|
||||
}
|
||||
} catch { text = ''; }
|
||||
try { port.postMessage({ text }); } catch {} finally { port.close(); }
|
||||
})();
|
||||
return;
|
||||
}
|
||||
if (data.type === 'SHINE_CALL_PUSH_ACTION') {
|
||||
const action = String(data.action || '').trim().toLowerCase();
|
||||
const payload = data.payload || {};
|
||||
@@ -1808,6 +1838,7 @@ async function init() {
|
||||
const messageType = Number(parsed.messageType || 0);
|
||||
let shouldAckSessionDelivery = true;
|
||||
let text = '';
|
||||
let decryptedForPreview = false;
|
||||
let sessionLoginForRouting = String(state.session?.login || '').trim();
|
||||
if (messageType === 1 || messageType === 2) {
|
||||
const decryptContext = await waitForSignedDmDecryptContext();
|
||||
@@ -1829,6 +1860,7 @@ async function init() {
|
||||
storagePwd: decryptContext.storagePwd,
|
||||
});
|
||||
text = String(decrypted?.text || '');
|
||||
decryptedForPreview = true;
|
||||
} catch (error) {
|
||||
text = dmDecryptFallbackText(error);
|
||||
addAppLogEntry({
|
||||
@@ -1893,6 +1925,7 @@ async function init() {
|
||||
} else if (Notification.permission === 'granted' && !suppressHiddenNotification) {
|
||||
void notifyHiddenIncomingMessage(
|
||||
fromLogin, toLogin, unreadCount, messageKey, Number(payload.notificationVersionMs || 0),
|
||||
decryptedForPreview ? text : '',
|
||||
);
|
||||
} else if (!suppressHiddenNotification) {
|
||||
void playDmSignal({ extended: true });
|
||||
|
||||
Reference in New Issue
Block a user