/api/v1/conversations answered one page, never unread, its read endpoint did nothing, and DELETE was missing; each conversation cost a query per member. Now each conversation keeps its newest post (DmGroup.LastPostId, set as posts arrive, learnt once by migration _013) and pages by it as Mastodon does, and each persona's ConversationState holds what it read and what it took off its list: - unread when someone else wrote last, after what the persona read; - read marks it so, and writing in a conversation reads it; - DELETE takes it off the list until a newer message brings it back. The list reads its states, newest posts, members and accounts in a few queries per page. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LsXgEaXee4GCU1hwYgPJXw
60 lines
2.4 KiB
C#
60 lines
2.4 KiB
C#
using MongoDB.Driver;
|
|
using MongoDB.Entities;
|
|
|
|
using PrivaPub.Models.Group;
|
|
using PrivaPub.Models.Social;
|
|
|
|
namespace PrivaPub.Domain.Statuses
|
|
{
|
|
// A direct conversation's newest post (DmGroup.LastPostId, which pages the conversations list) and each persona's
|
|
// read and removed marks on it (ConversationState). A conversation is unread for a persona when its newest post is
|
|
// someone else's and newer than what the persona read; writing in it reads it.
|
|
public static class ConversationStates
|
|
{
|
|
public static async Task Posted(string conversationId, string postId, string authorAvatarId, CancellationToken token)
|
|
{
|
|
await DB.Default.Update<DmGroup>().MatchID(conversationId)
|
|
.Modify(g => g.UpdatedAt, DateTime.UtcNow)
|
|
.Modify(g => g.LastPostId, postId)
|
|
.ExecuteAsync(token);
|
|
if (authorAvatarId != default)
|
|
await Read(authorAvatarId, conversationId, postId, token);
|
|
}
|
|
|
|
public static Task Read(string avatarId, string conversationId, string postId, CancellationToken token) =>
|
|
Mark(avatarId, conversationId, s => s.LastReadPostId, postId, token);
|
|
|
|
public static Task Hide(string avatarId, string conversationId, string postId, CancellationToken token) =>
|
|
Mark(avatarId, conversationId, s => s.HiddenUpTo, postId, token);
|
|
|
|
public static bool IsUnread(ConversationState state, string lastPostId, string lastAuthorId, string avatarId) =>
|
|
lastPostId != default && lastAuthorId != avatarId && string.CompareOrdinal(lastPostId, state?.LastReadPostId) > 0;
|
|
|
|
public static bool IsHidden(ConversationState state, string lastPostId) =>
|
|
state?.HiddenUpTo != default && string.CompareOrdinal(lastPostId, state.HiddenUpTo) <= 0;
|
|
|
|
// the mark only moves forward; the first mark makes the state (two at once: the unique index keeps one, and the
|
|
// loser marks again)
|
|
static async Task Mark(string avatarId, string conversationId, System.Linq.Expressions.Expression<Func<ConversationState, string>> field,
|
|
string postId, CancellationToken token)
|
|
{
|
|
if (postId == default)
|
|
return;
|
|
Task Write() => DB.Default.Update<ConversationState>()
|
|
.Match(s => s.AvatarId == avatarId && s.ConversationId == conversationId)
|
|
.Modify(b => b.Max(field, postId))
|
|
.Modify(s => s.UpdatedAt, DateTime.UtcNow)
|
|
.Option(o => o.IsUpsert = true)
|
|
.ExecuteAsync(token);
|
|
try
|
|
{
|
|
await Write();
|
|
}
|
|
catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
|
|
{
|
|
await Write();
|
|
}
|
|
}
|
|
}
|
|
}
|