mirror of
https://github.com/Govor-team/Govor.git
synced 2026-11-17 12:59:25 +00:00
Add reactions, reliable push & session checks
Implements message reactions and reaction packs (domain models, services, admin HTTP API, media processing via ImageSharp and limits) and adds reliable push delivery (ChatPushNotification, ChatPushDispatcher, ChatPushWorker, improved ChatNotificationService). Enhances message reading (ReadChat API, unread counts, concurrency-safe receipts) and tightens session security (access-token session validation in JWT, hub filter, SessionAuthorization). Improves SignalR behavior (session groups, reaction/channel events, hub/error mappings). Adds EF migrations, many controller fixes (Forbid handling, password API), tests (Govor.Server.Tests), docs, CI and tooling updates (Dockerfile → .NET 10, GH Actions matrix for sqlite/postgres, Govor.Server.slnx, global.json), and small infra changes (AutoMapper bump, NullPushProvider, storage/token handling).
This commit is contained in:
@@ -40,6 +40,21 @@ public class FirebasePushProvider : IPushNotificationProvider
|
||||
{
|
||||
if (tokens.Count == 0)
|
||||
return new SendPushResult(0, 0, []);
|
||||
|
||||
if (tokens.Count > 500)
|
||||
{
|
||||
var success = 0;
|
||||
var failure = 0;
|
||||
var invalid = new List<string>();
|
||||
foreach (var batch in tokens.Distinct().Chunk(500))
|
||||
{
|
||||
var result = await SendMulticastAsync(batch, message);
|
||||
success += result.SuccessCount;
|
||||
failure += result.FailureCount;
|
||||
invalid.AddRange(result.FailedTokens);
|
||||
}
|
||||
return new SendPushResult(success, failure, invalid);
|
||||
}
|
||||
|
||||
var multicastMsg = new MulticastMessage
|
||||
{
|
||||
@@ -89,7 +104,7 @@ public class FirebasePushProvider : IPushNotificationProvider
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "FCM multicast failed");
|
||||
return new SendPushResult(0, tokens.Count, tokens.ToList());
|
||||
throw; // A provider outage does not invalidate device tokens.
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,8 +139,6 @@ public class FirebasePushProvider : IPushNotificationProvider
|
||||
|
||||
private static bool IsInvalidTokenError(FirebaseMessagingException ex)
|
||||
{
|
||||
return ex.MessagingErrorCode is MessagingErrorCode.Unregistered
|
||||
or MessagingErrorCode.InvalidArgument
|
||||
or MessagingErrorCode.SenderIdMismatch;
|
||||
return ex.MessagingErrorCode is MessagingErrorCode.Unregistered;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,11 +9,11 @@ public class NullPushProvider : IPushNotificationProvider
|
||||
|
||||
public Task<SendPushResult> SendToTokenAsync(string token, PushMessage message)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
return Task.FromResult(new SendPushResult(0, 0, []));
|
||||
}
|
||||
|
||||
public Task<SendPushResult> SendMulticastAsync(IReadOnlyList<string> tokens, PushMessage message)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
return Task.FromResult(new SendPushResult(0, 0, []));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,6 +75,8 @@ public class PushNotificationService : IPushNotificationService
|
||||
_logger.LogWarning("Removed {Count} invalid FCM tokens for session {SessionId}", result.FailureCount, sessionId);
|
||||
}
|
||||
|
||||
if (result.FailureCount > result.FailedTokens.Count())
|
||||
return Result.Failure(Error.ServerError("Push.DeliveryFailed", "Push delivery failed temporarily."));
|
||||
return Result.Success();
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -97,6 +99,8 @@ public class PushNotificationService : IPushNotificationService
|
||||
_logger.LogWarning("Removed {Count} invalid FCM tokens for target: {Target}", result.FailureCount, targetInfo);
|
||||
}
|
||||
|
||||
if (result.FailureCount > result.FailedTokens.Count())
|
||||
return Result.Failure(Error.ServerError("Push.DeliveryFailed", "Push delivery failed temporarily."));
|
||||
return Result.Success();
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -117,4 +121,4 @@ public class PushNotificationService : IPushNotificationService
|
||||
ChannelId = channelId
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,8 +63,10 @@ public class PushTokenService : IPushTokenService
|
||||
{
|
||||
var tokens = await _context.UserPushTokens
|
||||
.AsNoTracking()
|
||||
.Where(t => t.UserId == userId && t.IsActive)
|
||||
.Where(t => t.UserId == userId && t.IsActive && _context.UserSessions.Any(s =>
|
||||
s.Id == t.UserSessionId && s.UserId == t.UserId && !s.IsRevoked && s.ExpiresAt > _nowDateTimeProvider.Now))
|
||||
.Select(t => t.Token)
|
||||
.Distinct()
|
||||
.ToListAsync();
|
||||
|
||||
return tokens;
|
||||
@@ -82,8 +84,10 @@ public class PushTokenService : IPushTokenService
|
||||
{
|
||||
var tokens = await _context.UserPushTokens
|
||||
.AsNoTracking()
|
||||
.Where(t => userIds.Contains(t.UserId) && t.IsActive)
|
||||
.Where(t => userIds.Contains(t.UserId) && t.IsActive && _context.UserSessions.Any(s =>
|
||||
s.Id == t.UserSessionId && s.UserId == t.UserId && !s.IsRevoked && s.ExpiresAt > _nowDateTimeProvider.Now))
|
||||
.Select(t => t.Token)
|
||||
.Distinct()
|
||||
.ToListAsync();
|
||||
|
||||
return tokens;
|
||||
@@ -101,7 +105,8 @@ public class PushTokenService : IPushTokenService
|
||||
{
|
||||
var token = await _context.UserPushTokens
|
||||
.AsNoTracking()
|
||||
.Where(t => t.UserSessionId == sessionId && t.IsActive)
|
||||
.Where(t => t.UserSessionId == sessionId && t.IsActive && _context.UserSessions.Any(s =>
|
||||
s.Id == t.UserSessionId && s.UserId == t.UserId && !s.IsRevoked && s.ExpiresAt > _nowDateTimeProvider.Now))
|
||||
.Select(t => t.Token)
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
@@ -138,13 +143,22 @@ public class PushTokenService : IPushTokenService
|
||||
|
||||
public async Task<Result<Unit, Error>> AddOrUpdateTokenAsync(Guid userId, Guid sessionId, string token, string platform)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(token))
|
||||
if (string.IsNullOrWhiteSpace(token) || token.Length > 512 || string.IsNullOrWhiteSpace(platform) || platform.Length > 50)
|
||||
{
|
||||
return Result<Unit, Error>.Failure(Error.Failure("PushToken.Empty", "Push token cannot be empty."));
|
||||
}
|
||||
|
||||
var existingToken = await _context.UserPushTokens
|
||||
.FirstOrDefaultAsync(t => t.Platform == platform && t.UserId == userId && t.UserSessionId == sessionId);
|
||||
if (!await _context.UserSessions.AnyAsync(s => s.Id == sessionId && s.UserId == userId &&
|
||||
!s.IsRevoked && s.ExpiresAt > _nowDateTimeProvider.Now))
|
||||
return Result<Unit, Error>.Failure(Error.Forbidden("PushToken.Session", "An active own session is required."));
|
||||
|
||||
// Token and session both have unique indexes. Reassign the device row when an
|
||||
// account changes, instead of inserting a duplicate or retaining the old owner.
|
||||
var sessionToken = await _context.UserPushTokens.FirstOrDefaultAsync(t => t.UserSessionId == sessionId);
|
||||
var deviceToken = await _context.UserPushTokens.FirstOrDefaultAsync(t => t.Token == token);
|
||||
if (sessionToken is not null && deviceToken is not null && sessionToken.Id != deviceToken.Id)
|
||||
_context.UserPushTokens.Remove(sessionToken);
|
||||
var existingToken = deviceToken ?? sessionToken;
|
||||
|
||||
if (existingToken is null)
|
||||
{
|
||||
@@ -167,6 +181,8 @@ public class PushTokenService : IPushTokenService
|
||||
existingToken.Platform = platform;
|
||||
existingToken.Token = token;
|
||||
existingToken.UpdatedAt = _nowDateTimeProvider.Now;
|
||||
existingToken.IsActive = true;
|
||||
_context.Entry(existingToken).Property(t => t.IsActive).IsModified = true;
|
||||
}
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
Reference in New Issue
Block a user