mirror of
https://github.com/Govor-team/Govor.git
synced 2026-11-18 05:15:06 +00:00
Expose chat read positions and E2EE errors
Add optional chat activity and read-position details to the unread-count endpoint. Return stable E2EE rejection codes for stale timestamps and untrusted signing devices, with tests for both behaviors.
This commit is contained in:
@@ -148,6 +148,33 @@ public class MessageReadingService : IMessageReadingService
|
||||
chat.CountAsync(m => m.SenderId != readerId &&
|
||||
!_dbContext.MessageViews.Any(v => v.MessageId == m.Id && v.UserId == readerId));
|
||||
|
||||
public async Task<Result<ChatActivity, Error>> GetChatActivityAsync(Guid readerId, Guid chatId, RecipientType type,
|
||||
bool includeReadPosition = false)
|
||||
{
|
||||
if (!await _dbContext.HasChatAccessAsync(readerId, chatId, type))
|
||||
return Result.Failure<ChatActivity>(Denied());
|
||||
|
||||
var chat = _dbContext.Messages.AsNoTracking()
|
||||
.Where(m => m.RecipientId == chatId && m.RecipientType == type);
|
||||
var activity = await chat.GroupBy(_ => 1).Select(g => new ChatActivity(
|
||||
g.Count(m => m.SenderId != readerId &&
|
||||
!_dbContext.MessageViews.Any(v => v.MessageId == m.Id && v.UserId == readerId)),
|
||||
g.Max(m => (DateTime?)m.SentAt))).SingleOrDefaultAsync();
|
||||
if (activity is null) return new ChatActivity(0, null);
|
||||
if (!includeReadPosition) return activity;
|
||||
|
||||
var firstUnread = await chat.Where(m => m.SenderId != readerId &&
|
||||
!_dbContext.MessageViews.Any(v => v.MessageId == m.Id && v.UserId == readerId))
|
||||
.OrderBy(m => m.SentAt).ThenBy(m => m.Id)
|
||||
.Select(m => new { m.Id, m.SentAt }).FirstOrDefaultAsync();
|
||||
// Outgoing messages count as read. Do not skip an earlier unread incoming message.
|
||||
var readHistory = firstUnread is null ? chat : chat.Where(m => m.SentAt < firstUnread.SentAt ||
|
||||
(m.SentAt == firstUnread.SentAt && m.Id.CompareTo(firstUnread.Id) < 0));
|
||||
var lastRead = await readHistory.OrderByDescending(m => m.SentAt).ThenByDescending(m => m.Id)
|
||||
.Select(m => (Guid?)m.Id).FirstOrDefaultAsync();
|
||||
return activity with { LastReadMessageId = lastRead, FirstUnreadMessageId = firstUnread?.Id };
|
||||
}
|
||||
|
||||
private static Error Denied() => Error.Forbidden("Chat.AccessDenied",
|
||||
"You are not a member of this chat.");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user