mirror of
https://github.com/Govor-team/Govor.git
synced 2026-11-16 20:56:03 +00:00
29cc51ee0c
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).
145 lines
4.2 KiB
C#
145 lines
4.2 KiB
C#
using FirebaseAdmin.Messaging;
|
|
using Govor.Application.Interfaces.PushNotifications;
|
|
using Govor.Application.Interfaces.PushNotifications.Models;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Govor.Application.PushNotifications.Providers;
|
|
|
|
public class FirebasePushProvider : IPushNotificationProvider
|
|
{
|
|
private readonly ILogger<FirebasePushProvider> _logger;
|
|
public FirebasePushProvider(ILogger<FirebasePushProvider> logger)
|
|
{
|
|
_logger = logger;
|
|
}
|
|
|
|
public string Name => "FCM";
|
|
|
|
public async Task<SendPushResult> SendToTokenAsync(string token, PushMessage message)
|
|
{
|
|
var msg = BuildFcmMessage(message, token: token);
|
|
|
|
try
|
|
{
|
|
string response = await FirebaseMessaging.DefaultInstance.SendAsync(msg);
|
|
return new SendPushResult(1, 0, []);
|
|
}
|
|
catch (FirebaseMessagingException ex)
|
|
{
|
|
if (IsInvalidTokenError(ex))
|
|
{
|
|
_logger.LogError(ex, "FCM send failed");
|
|
return new SendPushResult(0, 1, [token]);
|
|
}
|
|
|
|
throw; // return failed
|
|
}
|
|
}
|
|
|
|
public async Task<SendPushResult> SendMulticastAsync(IReadOnlyList<string> tokens, PushMessage message)
|
|
{
|
|
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
|
|
{
|
|
Tokens = tokens,
|
|
Notification = new Notification
|
|
{
|
|
Title = message.Title,
|
|
Body = message.Body,
|
|
},
|
|
Data = message.Data,
|
|
Android = message.ChannelId != null ? new AndroidConfig
|
|
{
|
|
Priority = Priority.High,
|
|
Notification = new AndroidNotification
|
|
{
|
|
ChannelId = message.ChannelId ?? "chat_messages",
|
|
Tag = message.Tag,
|
|
}
|
|
} : null
|
|
};
|
|
|
|
try
|
|
{
|
|
var response = await FirebaseMessaging.DefaultInstance.SendEachForMulticastAsync(multicastMsg);
|
|
|
|
var failedTokens = new List<string>();
|
|
|
|
for (int i = 0; i < response.Responses.Count; i++)
|
|
{
|
|
if (!response.Responses[i].IsSuccess)
|
|
{
|
|
var ex = response.Responses[i].Exception;
|
|
|
|
if (ex != null && IsInvalidTokenError(ex))
|
|
{
|
|
failedTokens.Add(tokens[i]); // invalid
|
|
}
|
|
}
|
|
}
|
|
|
|
return new SendPushResult(
|
|
response.SuccessCount,
|
|
response.FailureCount,
|
|
failedTokens
|
|
);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "FCM multicast failed");
|
|
throw; // A provider outage does not invalidate device tokens.
|
|
}
|
|
}
|
|
|
|
private Message BuildFcmMessage(PushMessage pm, string? token = null)
|
|
{
|
|
var msg = new Message
|
|
{
|
|
Notification = new Notification
|
|
{
|
|
Title = pm.Title,
|
|
Body = pm.Body
|
|
},
|
|
Data = pm.Data,
|
|
Token = token
|
|
};
|
|
|
|
if (pm.ChannelId is not null)
|
|
{
|
|
msg.Android = new AndroidConfig
|
|
{
|
|
Priority = Priority.High,
|
|
Notification = new AndroidNotification
|
|
{
|
|
ChannelId = pm.ChannelId,
|
|
Tag = pm.Tag,
|
|
}
|
|
};
|
|
}
|
|
|
|
return msg;
|
|
}
|
|
|
|
private static bool IsInvalidTokenError(FirebaseMessagingException ex)
|
|
{
|
|
return ex.MessagingErrorCode is MessagingErrorCode.Unregistered;
|
|
}
|
|
}
|