Add persistent Inbox and RFC 8291/8292 Web Push notification system

This commit is contained in:
tyuXX 2026-10-31 15:41:51 +03:00
parent 4714d25724
commit 7cc9ba7f4d
27 changed files with 3573 additions and 17 deletions

View file

@ -326,6 +326,13 @@ public class ChatClientConnection : IDisposable
DecryptMessageIfNeeded(msg);
}
}
foreach (var item in _state.GetInboxItems())
{
if (E2eeCrypto.IsEncryptedPayload(item.ContentPreview))
{
DecryptInboxItemIfNeeded(item);
}
}
_state.NeedsRedraw = true;
}
@ -462,6 +469,28 @@ public class ChatClientConnection : IDisposable
}
break;
case MessageTypes.InboxSync:
var inSync = envelope.Unpack<InboxSyncPacket>();
if (inSync != null)
{
foreach (var item in inSync.Items)
{
DecryptInboxItemIfNeeded(item);
}
_state.SetInboxSync(inSync.Items, inSync.UnreadCount, inSync.Preferences);
}
break;
case MessageTypes.InboxNewItem:
var inNew = envelope.Unpack<InboxNewItemPacket>();
if (inNew != null && inNew.Item != null)
{
DecryptInboxItemIfNeeded(inNew.Item);
_state.AddOrUpdateInboxItem(inNew.Item, inNew.UnreadCount);
_state.SetStatus($"[Inbox] New {inNew.Item.Category} from @{inNew.Item.SenderUsername} (Press F5)", 4);
}
break;
case MessageTypes.SystemNotice:
var notice = envelope.Unpack<SystemNoticePacket>();
if (notice != null)
@ -562,6 +591,43 @@ public class ChatClientConnection : IDisposable
}
}
private void DecryptInboxItemIfNeeded(InboxItem item)
{
if (!item.IsEncrypted && !E2eeCrypto.IsEncryptedPayload(item.ContentPreview))
{
return;
}
item.IsEncrypted = true;
if (!E2eeCrypto.IsEncryptedPayload(item.ContentPreview) || string.IsNullOrWhiteSpace(_state.PrivateKeyBase64))
{
return;
}
string? peerPubKey = !string.IsNullOrWhiteSpace(item.SenderPublicKey)
? item.SenderPublicKey
: item.RecipientPublicKey;
if (string.IsNullOrWhiteSpace(peerPubKey))
{
var conv = _state.GetConversationById(item.ChannelId);
if (conv != null)
{
peerPubKey = _state.GetPeerPublicKeyForDm(conv);
}
}
if (!string.IsNullOrWhiteSpace(peerPubKey))
{
string decrypted = E2eeCrypto.DecryptMessage(item.ContentPreview, _state.PrivateKeyBase64, peerPubKey);
if (!decrypted.StartsWith("[Encrypted Message"))
{
item.ContentPreview = decrypted;
}
}
}
public async Task SendEnvelopeAsync(NetworkEnvelope envelope)
{
if (_webSocket == null || _webSocket.State != WebSocketState.Open) return;
@ -753,6 +819,39 @@ public class ChatClientConnection : IDisposable
return SendEnvelopeAsync(envelope);
}
public Task RequestInboxSyncAsync()
{
var envelope = NetworkEnvelope.Create(MessageTypes.InboxSync, new { });
return SendEnvelopeAsync(envelope);
}
public Task SendInboxActionAsync(string action, string? itemId = null, string? channelId = null)
{
var envelope = NetworkEnvelope.Create(MessageTypes.InboxAction, new InboxActionPacket
{
Action = action,
ItemId = itemId,
ChannelId = channelId
});
return SendEnvelopeAsync(envelope);
}
public Task UpdateInboxPreferencesAsync(UpdateInboxPreferencesPacket packet)
{
var envelope = NetworkEnvelope.Create(MessageTypes.UpdateInboxPreferences, packet);
return SendEnvelopeAsync(envelope);
}
public Task ToggleFollowChannelAsync(string channelId, bool? follow = null)
{
var envelope = NetworkEnvelope.Create(MessageTypes.ToggleFollowChannel, new ToggleFollowChannelPacket
{
ChannelId = channelId,
Follow = follow
});
return SendEnvelopeAsync(envelope);
}
public async Task<bool> UploadFileAsync(string localFilePath, string channelId)
{
if (!File.Exists(localFilePath))