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

@ -135,6 +135,8 @@ public class DatabaseTests : IDisposable
Assert.True(File.Exists(settingsPath));
Assert.True(options.AllowDmE2ee);
Assert.Equal(10, options.MaxGroupChatUsers);
Assert.False(string.IsNullOrWhiteSpace(options.VapidPublicKey));
Assert.False(string.IsNullOrWhiteSpace(options.VapidPrivateKey));
options.ServerName = "Custom JSON Server";
options.AllowDmE2ee = false;
@ -215,4 +217,108 @@ public class DatabaseTests : IDisposable
Assert.Null(await _db.GetFileRecordAsync("file_grp_1"));
Assert.False(File.Exists(tempFilePath));
}
[Fact]
public async Task InboxAndWebPushSubscriptions_DatabaseCrudOperations_ShouldSucceed()
{
var bob = new User { Username = "BobInbox", PasswordHash = "h", PasswordSalt = "s" };
await _db.CreateUserAsync(bob);
// 1. Default preferences & channel follow overrides
var prefs = await _db.GetInboxPreferencesAsync(bob.Id);
Assert.True(prefs.MentionsEnabled);
Assert.True(prefs.AnnouncementsEnabled);
Assert.True(prefs.DirectMessagesEnabled);
Assert.True(prefs.FollowedChannelsEnabled);
Assert.True(prefs.PushNotificationsEnabled);
Assert.Empty(prefs.FollowedChannelIds);
Assert.Empty(prefs.MutedChannelIds);
var channels = await _db.GetChannelsAsync();
var general = channels.First(c => c.Name == "general");
var announcements = channels.First(c => c.Name == "announcements");
await _db.SetChannelFollowModeAsync(bob.Id, general.Id, ConversationFollowModes.Followed);
await _db.SetChannelFollowModeAsync(bob.Id, announcements.Id, ConversationFollowModes.Muted);
prefs.MentionsEnabled = false;
await _db.SaveInboxPreferencesAsync(prefs);
var reloadedPrefs = await _db.GetInboxPreferencesAsync(bob.Id);
Assert.False(reloadedPrefs.MentionsEnabled);
Assert.Contains(general.Id, reloadedPrefs.FollowedChannelIds);
Assert.Contains(announcements.Id, reloadedPrefs.MutedChannelIds);
// 2. Inbox Items CRUD
var item1 = new InboxItem
{
UserId = bob.Id,
MessageId = "msg_1",
ChannelId = general.Id,
ChannelName = "general",
ChannelType = ChannelTypes.Channel,
SenderId = "u_alice",
SenderUsername = "Alice",
Category = InboxCategories.Mention,
ContentPreview = "Hey @BobInbox!",
IsRead = false,
CreatedAt = DateTime.UtcNow.AddMinutes(-2)
};
var item2 = new InboxItem
{
UserId = bob.Id,
MessageId = "msg_2",
ChannelId = announcements.Id,
ChannelName = "announcements",
ChannelType = ChannelTypes.Channel,
SenderId = "u_alice",
SenderUsername = "Alice",
Category = InboxCategories.Announcement,
ContentPreview = "Server maintenance tonight",
IsRead = false,
CreatedAt = DateTime.UtcNow
};
await _db.CreateInboxItemAsync(item1);
await _db.CreateInboxItemAsync(item2);
Assert.Equal(2, await _db.GetUnreadInboxCountAsync(bob.Id));
var items = await _db.GetInboxItemsForUserAsync(bob.Id);
Assert.Equal(2, items.Count);
Assert.Equal(item2.Id, items[0].Id); // Ordered newest first
// Mark channel read
await _db.MarkChannelInboxItemsReadAsync(bob.Id, general.Id);
Assert.Equal(1, await _db.GetUnreadInboxCountAsync(bob.Id));
// Clear read items
await _db.ClearInboxItemsAsync(bob.Id, onlyRead: true);
items = await _db.GetInboxItemsForUserAsync(bob.Id);
Assert.Single(items);
Assert.Equal(item2.Id, items[0].Id);
// Mark all read & clear all
await _db.MarkAllInboxItemsReadAsync(bob.Id);
Assert.Equal(0, await _db.GetUnreadInboxCountAsync(bob.Id));
await _db.ClearInboxItemsAsync(bob.Id, onlyRead: false);
Assert.Empty(await _db.GetInboxItemsForUserAsync(bob.Id));
// 3. Web Push Subscriptions CRUD
var sub = new PushSubscriptionRecord
{
UserId = bob.Id,
Endpoint = "https://push.example.org/send/sub_1",
P256dh = "p256dh_key_value",
Auth = "auth_secret_value",
UserAgent = "Mozilla/5.0 Test"
};
await _db.SavePushSubscriptionAsync(sub);
var subs = await _db.GetPushSubscriptionsForUserAsync(bob.Id);
Assert.Single(subs);
Assert.Equal("https://push.example.org/send/sub_1", subs[0].Endpoint);
await _db.RemovePushSubscriptionByEndpointAsync("https://push.example.org/send/sub_1");
Assert.Empty(await _db.GetPushSubscriptionsForUserAsync(bob.Id));
}
}