mirror of
https://github.com/fergalmoran/podnoms.git
synced 2025-12-22 09:18:08 +00:00
60 lines
2.8 KiB
C#
60 lines
2.8 KiB
C#
using System.Threading.Tasks;
|
|
using Lib.Net.Http.WebPush;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
using Microsoft.Extensions.Options;
|
|
using Newtonsoft.Json;
|
|
using PodNoms.Api.Models.Settings;
|
|
using PodNoms.Api.Models.ViewModels;
|
|
using PodNoms.Api.Services.Auth;
|
|
using PodNoms.Api.Services.Hubs;
|
|
using PodNoms.Api.Services.Push;
|
|
using PodNoms.Api.Services.Slack;
|
|
using WebPush = Lib.Net.Http.WebPush;
|
|
|
|
namespace PodNoms.Api.Services {
|
|
public class SupportChatService : ISupportChatService {
|
|
private readonly ChatSettings _chatSettings;
|
|
private readonly IPushNotificationService _notificationService;
|
|
private readonly HubLifetimeManager<ChatHub> _hub;
|
|
private readonly UserManager<ApplicationUser> _userManager;
|
|
private readonly IPushSubscriptionStore _subscriptionStore;
|
|
private readonly SlackSupportClient _slackSupport;
|
|
public SupportChatService(UserManager<ApplicationUser> userManager, IOptions<ChatSettings> chatSettings,
|
|
IPushSubscriptionStore subscriptionStore, IPushNotificationService notificationService,
|
|
HubLifetimeManager<ChatHub> hub, SlackSupportClient slackSupport) {
|
|
this._chatSettings = chatSettings.Value;
|
|
this._notificationService = notificationService;
|
|
this._hub = hub;
|
|
this._userManager = userManager;
|
|
this._subscriptionStore = subscriptionStore;
|
|
this._slackSupport = slackSupport;
|
|
|
|
}
|
|
public async Task<bool> InitiateSupportRequest(string fromUser, ChatViewModel message) {
|
|
if (!string.IsNullOrEmpty(_chatSettings.CurrentChatUser)) {
|
|
var user = await _userManager.FindByEmailAsync(_chatSettings.CurrentChatUser);
|
|
if (!string.IsNullOrEmpty(user?.Id)) {
|
|
message.ToUserId = user.Id;
|
|
message.ToUserName = user.FullName;
|
|
//send firebase message to notify via web worker
|
|
WebPush.PushMessage pushMessage = new WebPush.PushMessage(message.Message) {
|
|
Topic = "New support chat message",
|
|
Urgency = PushMessageUrgency.Normal
|
|
};
|
|
await _subscriptionStore.ForEachSubscriptionAsync(user.Id, (WebPush.PushSubscription subscription) => {
|
|
_notificationService.SendNotificationAsync(subscription, pushMessage);
|
|
});
|
|
|
|
//send SignalR message to notify in chat.component
|
|
await _hub.SendUserAsync(user.Id, "SendMessage", new object[] { message });
|
|
|
|
//send slack message
|
|
var slackResult = await _slackSupport.NotifyUser(message);
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
} |