getting rid of GCM
This commit is contained in:
@ -27,10 +27,6 @@ namespace Yavsc.Models.Google.Messaging
|
|||||||
/// Message with payload response.
|
/// Message with payload response.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class MessageWithPayloadResponse {
|
public class MessageWithPayloadResponse {
|
||||||
/// <summary>
|
|
||||||
/// The multicast identifier.
|
|
||||||
/// </summary>
|
|
||||||
public string multicast_id;
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The success count.
|
/// The success count.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -57,7 +57,13 @@ namespace Yavsc
|
|||||||
{ "openid", "profile", "email", "https://www.googleapis.com/auth/calendar" };
|
{ "openid", "profile", "email", "https://www.googleapis.com/auth/calendar" };
|
||||||
|
|
||||||
public static readonly string NoneCode = "none";
|
public static readonly string NoneCode = "none";
|
||||||
|
public const string HubGroupAuthenticated = "authenticated";
|
||||||
|
public const string HubGroupAnonymous = "anonymous";
|
||||||
|
public const string HubGroupCops= "cops";
|
||||||
public const int MaxChanelName = 255;
|
public const int MaxChanelName = 255;
|
||||||
}
|
}
|
||||||
|
public static class NotificationTypes {
|
||||||
|
public const string Connected = "connected";
|
||||||
|
public const string DisConnected = "disconnected";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,9 +37,11 @@ namespace Yavsc
|
|||||||
{
|
{
|
||||||
ApplicationDbContext _dbContext;
|
ApplicationDbContext _dbContext;
|
||||||
ILogger _logger;
|
ILogger _logger;
|
||||||
|
IUserIdProvider _userIdProvider;
|
||||||
|
|
||||||
public ChatHub()
|
public ChatHub(IUserIdProvider userIdProvider)
|
||||||
{
|
{
|
||||||
|
_userIdProvider = userIdProvider;
|
||||||
var scope = Startup.Services.GetRequiredService<IServiceScopeFactory>().CreateScope();
|
var scope = Startup.Services.GetRequiredService<IServiceScopeFactory>().CreateScope();
|
||||||
_dbContext = scope.ServiceProvider.GetService<ApplicationDbContext>();
|
_dbContext = scope.ServiceProvider.GetService<ApplicationDbContext>();
|
||||||
var loggerFactory = scope.ServiceProvider.GetService<ILoggerFactory>();
|
var loggerFactory = scope.ServiceProvider.GetService<ILoggerFactory>();
|
||||||
@ -54,35 +56,39 @@ namespace Yavsc
|
|||||||
{
|
{
|
||||||
isAuth = Context.User.Identity.IsAuthenticated;
|
isAuth = Context.User.Identity.IsAuthenticated;
|
||||||
userName = Context.User.Identity.Name;
|
userName = Context.User.Identity.Name;
|
||||||
|
|
||||||
var group = isAuth ?
|
var group = isAuth ?
|
||||||
"authenticated" : "anonymous";
|
Constants.HubGroupAuthenticated : Constants.HubGroupAnonymous;
|
||||||
// Log ("Cx: " + group);
|
// Log ("Cx: " + group);
|
||||||
await Groups.Add(Context.ConnectionId, group);
|
await Groups.Add(Context.ConnectionId, group);
|
||||||
if (isAuth)
|
if (isAuth)
|
||||||
{
|
{
|
||||||
var user = _dbContext.Users.Include(u=>u.Connections).Single(u => u.UserName == userName);
|
var userHadConnections = _dbContext.ChatConnection.Any(accx => accx.ConnectionId == Context.ConnectionId);
|
||||||
if (user.Connections==null)
|
|
||||||
user.Connections = new List<ChatConnection>();
|
if (userHadConnections) {
|
||||||
var ucx = user.Connections.FirstOrDefault(c=>c.ConnectionId == Context.ConnectionId);
|
var ccx = _dbContext.ChatConnection.First(c=> c.ConnectionId == Context.ConnectionId);
|
||||||
if (ucx==null)
|
ccx.Connected=true;
|
||||||
user.Connections.Add(new ChatConnection
|
}
|
||||||
|
else
|
||||||
|
_dbContext.ChatConnection.Add(new ChatConnection
|
||||||
{
|
{
|
||||||
ConnectionId = Context.ConnectionId,
|
ConnectionId = Context.ConnectionId,
|
||||||
UserAgent = Context.Request.Headers["User-Agent"],
|
UserAgent = Context.Request.Headers["User-Agent"],
|
||||||
Connected = true
|
Connected = true
|
||||||
});
|
});
|
||||||
else {
|
|
||||||
ucx.Connected = true;
|
|
||||||
}
|
|
||||||
_dbContext.SaveChanges();
|
_dbContext.SaveChanges();
|
||||||
|
var userId = _userIdProvider.GetUserId(this.Context.Request);
|
||||||
Clients.CallerState.BlackListedBy = await _dbContext.BlackListed.Where(r=>r.UserId == user.Id).Select(r=>r.OwnerId).ToArrayAsync();
|
Clients.CallerState.BlackListedBy = await _dbContext.BlackListed.Where(r=>r.UserId == userId).Select(r=>r.OwnerId).ToArrayAsync();
|
||||||
|
// TODO ChatHubConnectioinFlags
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
// FIXME is this line reached ?
|
||||||
|
await Groups.Add(Context.ConnectionId, Constants.HubGroupAnonymous);
|
||||||
}
|
}
|
||||||
else await Groups.Add(Context.ConnectionId, "anonymous");
|
else await Groups.Add(Context.ConnectionId, Constants.HubGroupAnonymous);
|
||||||
|
|
||||||
Clients.Group("authenticated").notify("connected", Context.ConnectionId, userName);
|
// TODO only notify followers
|
||||||
|
Clients.Group(Constants.HubGroupAuthenticated).notify(NotificationTypes.Connected, Context.ConnectionId, userName);
|
||||||
|
|
||||||
await base.OnConnected();
|
await base.OnConnected();
|
||||||
}
|
}
|
||||||
@ -200,7 +206,7 @@ namespace Yavsc
|
|||||||
|
|
||||||
public void Abort()
|
public void Abort()
|
||||||
{
|
{
|
||||||
var cx = _dbContext .ChatConnection.SingleOrDefault(c=>c.ConnectionId == Context.ConnectionId);
|
var cx = _dbContext.ChatConnection.SingleOrDefault(c=>c.ConnectionId == Context.ConnectionId);
|
||||||
if (cx!=null) {
|
if (cx!=null) {
|
||||||
_dbContext.ChatConnection.Remove(cx);
|
_dbContext.ChatConnection.Remove(cx);
|
||||||
_dbContext.SaveChanges();
|
_dbContext.SaveChanges();
|
||||||
|
@ -1,102 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using Microsoft.Extensions.OptionsModel;
|
|
||||||
using Newtonsoft.Json;
|
|
||||||
using Yavsc.Interfaces.Workflow;
|
|
||||||
using Yavsc.Models.Google.Messaging;
|
|
||||||
using Yavsc.Models.Haircut;
|
|
||||||
using Yavsc.Models.Messaging;
|
|
||||||
using Yavsc.Server.Helpers;
|
|
||||||
|
|
||||||
namespace Yavsc.Services
|
|
||||||
{
|
|
||||||
public class GCMSender : IYavscMessageSender
|
|
||||||
{
|
|
||||||
private ILogger _logger;
|
|
||||||
SiteSettings siteSettings;
|
|
||||||
GoogleAuthSettings googleSettings;
|
|
||||||
|
|
||||||
public GCMSender(
|
|
||||||
|
|
||||||
ILoggerFactory loggerFactory,
|
|
||||||
IOptions<SiteSettings> sitesOptions,
|
|
||||||
IOptions<SmtpSettings> smtpOptions,
|
|
||||||
IOptions<GoogleAuthSettings> googleOptions
|
|
||||||
)
|
|
||||||
{ _logger = loggerFactory.CreateLogger<MailSender>();
|
|
||||||
siteSettings = sitesOptions?.Value;
|
|
||||||
googleSettings = googleOptions?.Value;
|
|
||||||
|
|
||||||
}
|
|
||||||
public async Task <MessageWithPayloadResponse> NotifyEvent<Event>
|
|
||||||
( IEnumerable<string> regids, Event ev)
|
|
||||||
where Event : IEvent
|
|
||||||
{
|
|
||||||
if (ev == null)
|
|
||||||
throw new Exception("Spécifier un évènement");
|
|
||||||
|
|
||||||
if (ev.Sender == null)
|
|
||||||
throw new Exception("Spécifier un expéditeur");
|
|
||||||
|
|
||||||
if (regids == null )
|
|
||||||
throw new NotImplementedException("Notify & No GCM reg ids");
|
|
||||||
var raa = regids.ToArray();
|
|
||||||
if (raa.Length<1)
|
|
||||||
throw new NotImplementedException("No GCM reg ids");
|
|
||||||
var msg = new MessageWithPayload<Event>()
|
|
||||||
{
|
|
||||||
data = ev,
|
|
||||||
registration_ids = regids.ToArray()
|
|
||||||
};
|
|
||||||
_logger.LogInformation("Sendding to Google : "+JsonConvert.SerializeObject(msg));
|
|
||||||
try {
|
|
||||||
using (var m = new SimpleJsonPostMethod("https://gcm-http.googleapis.com/gcm/send",$"key={googleSettings.ApiKey}")) {
|
|
||||||
return await m.Invoke<MessageWithPayloadResponse>(msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex) {
|
|
||||||
_logger.LogError(ex.Message);
|
|
||||||
throw new Exception ("Quelque chose s'est mal passé à l'envoi",ex);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public async Task<MessageWithPayloadResponse> NotifyBookQueryAsync( IEnumerable<string> registrationIds, RdvQueryEvent ev)
|
|
||||||
{
|
|
||||||
return await NotifyEvent<RdvQueryEvent>(registrationIds, ev);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<MessageWithPayloadResponse> NotifyEstimateAsync(IEnumerable<string> registrationIds, EstimationEvent ev)
|
|
||||||
{
|
|
||||||
return await NotifyEvent<EstimationEvent>(registrationIds, ev);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<MessageWithPayloadResponse> NotifyHairCutQueryAsync(
|
|
||||||
IEnumerable<string> registrationIds, HairCutQueryEvent ev)
|
|
||||||
{
|
|
||||||
return await NotifyEvent<HairCutQueryEvent>(registrationIds, ev);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<MessageWithPayloadResponse> NotifyAsync(IEnumerable<string> regids, IEvent yaev)
|
|
||||||
{
|
|
||||||
return await NotifyEvent<IEvent>(regids, yaev);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* SMS with Twilio:
|
|
||||||
public Task SendSmsAsync(TwilioSettings twilioSettigns, string number, string message)
|
|
||||||
{
|
|
||||||
var Twilio = new TwilioRestClient(twilioSettigns.AccountSID, twilioSettigns.Token);
|
|
||||||
var result = Twilio.SendMessage( twilioSettigns.SMSAccountFrom, number, message);
|
|
||||||
// Status is one of Queued, Sending, Sent, Failed or null if the number is not valid
|
|
||||||
Trace.TraceInformation(result.Status);
|
|
||||||
// Twilio doesn't currently have an async API, so return success.
|
|
||||||
|
|
||||||
return Task.FromResult(result.Status != "Failed");
|
|
||||||
|
|
||||||
} */
|
|
||||||
}
|
|
||||||
}
|
|
139
src/Yavsc/Services/YavscMessageSender.cs
Normal file
139
src/Yavsc/Services/YavscMessageSender.cs
Normal file
@ -0,0 +1,139 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNet.SignalR;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Microsoft.Extensions.OptionsModel;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Yavsc.Interfaces.Workflow;
|
||||||
|
using Yavsc.Models;
|
||||||
|
using Yavsc.Models.Google.Messaging;
|
||||||
|
using Yavsc.Models.Haircut;
|
||||||
|
using Yavsc.Models.Messaging;
|
||||||
|
|
||||||
|
namespace Yavsc.Services
|
||||||
|
{
|
||||||
|
public class YavscMessageSender : IYavscMessageSender
|
||||||
|
{
|
||||||
|
private ILogger _logger;
|
||||||
|
IEmailSender _emailSender;
|
||||||
|
SiteSettings siteSettings;
|
||||||
|
private IHubContext hubContext;
|
||||||
|
ApplicationDbContext _dbContext;
|
||||||
|
|
||||||
|
public YavscMessageSender(
|
||||||
|
ILoggerFactory loggerFactory,
|
||||||
|
IOptions<SiteSettings> sitesOptions,
|
||||||
|
IOptions<SmtpSettings> smtpOptions,
|
||||||
|
IEmailSender emailSender,
|
||||||
|
ApplicationDbContext dbContext
|
||||||
|
)
|
||||||
|
{
|
||||||
|
_logger = loggerFactory.CreateLogger<MailSender>();
|
||||||
|
_emailSender = emailSender;
|
||||||
|
siteSettings = sitesOptions?.Value;
|
||||||
|
hubContext = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
|
||||||
|
}
|
||||||
|
public async Task <MessageWithPayloadResponse> NotifyEvent<Event>
|
||||||
|
(IEnumerable<string> userIds, Event ev)
|
||||||
|
where Event : IEvent
|
||||||
|
{
|
||||||
|
|
||||||
|
if (ev == null)
|
||||||
|
throw new Exception("Spécifier un évènement");
|
||||||
|
|
||||||
|
if (ev.Sender == null)
|
||||||
|
throw new Exception("Spécifier un expéditeur");
|
||||||
|
|
||||||
|
if (userIds == null )
|
||||||
|
throw new NotImplementedException("Notify e No user id");
|
||||||
|
|
||||||
|
MessageWithPayloadResponse response = new MessageWithPayloadResponse();
|
||||||
|
|
||||||
|
var raa = userIds.ToArray();
|
||||||
|
if (raa.Length<1)
|
||||||
|
throw new NotImplementedException("No GCM reg ids");
|
||||||
|
|
||||||
|
try {
|
||||||
|
_logger.LogInformation($"Sending to {string.Join(" ",raa)} using signalR : "+JsonConvert.SerializeObject(ev));
|
||||||
|
List<MessageWithPayloadResponse.Result> results = new List<MessageWithPayloadResponse.Result>();
|
||||||
|
foreach(var clientId in raa) {
|
||||||
|
MessageWithPayloadResponse.Result result = new MessageWithPayloadResponse.Result();
|
||||||
|
result.registration_id = clientId;
|
||||||
|
|
||||||
|
var hubClient = hubContext.Clients.User(clientId);
|
||||||
|
if (hubClient == null)
|
||||||
|
{
|
||||||
|
var user = _dbContext.Users.FirstOrDefault(u=> u.Id == clientId);
|
||||||
|
if (user==null)
|
||||||
|
{
|
||||||
|
result.error = "no such user.";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!user.EmailConfirmed){
|
||||||
|
result.error = "user has not confirmed his email address.";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
var body = ev.CreateBody();
|
||||||
|
var sent = await _emailSender.SendEmailAsync(ev.Sender, user.Email,
|
||||||
|
ev.Topic, body
|
||||||
|
);
|
||||||
|
result.message_id = sent.MessageId;
|
||||||
|
if (!sent.Sent) {
|
||||||
|
result.error = sent.ErrorMessage;
|
||||||
|
response.failure++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
response.success++;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// we assume that each hub connected client will handle this signal
|
||||||
|
hubClient.notify(ev);
|
||||||
|
response.success++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
response.results = results.ToArray();
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
_logger.LogError("Quelque chose s'est mal passé à l'envoi: "+ex.Message);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<MessageWithPayloadResponse> NotifyBookQueryAsync( IEnumerable<string> registrationIds, RdvQueryEvent ev)
|
||||||
|
{
|
||||||
|
return await NotifyEvent<RdvQueryEvent>(registrationIds, ev);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<MessageWithPayloadResponse> NotifyEstimateAsync(IEnumerable<string> registrationIds, EstimationEvent ev)
|
||||||
|
{
|
||||||
|
return await NotifyEvent<EstimationEvent>(registrationIds, ev);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<MessageWithPayloadResponse> NotifyHairCutQueryAsync(
|
||||||
|
IEnumerable<string> registrationIds, HairCutQueryEvent ev)
|
||||||
|
{
|
||||||
|
return await NotifyEvent<HairCutQueryEvent>(registrationIds, ev);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<MessageWithPayloadResponse> NotifyAsync(IEnumerable<string> regids, IEvent yaev)
|
||||||
|
{
|
||||||
|
return await NotifyEvent<IEvent>(regids, yaev);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* SMS with Twilio:
|
||||||
|
public Task SendSmsAsync(TwilioSettings twilioSettigns, string number, string message)
|
||||||
|
{
|
||||||
|
var Twilio = new TwilioRestClient(twilioSettigns.AccountSID, twilioSettigns.Token);
|
||||||
|
var result = Twilio.SendMessage( twilioSettigns.SMSAccountFrom, number, message);
|
||||||
|
// Status is one of Queued, Sending, Sent, Failed or null if the number is not valid
|
||||||
|
Trace.TraceInformation(result.Status);
|
||||||
|
// Twilio doesn't currently have an async API, so return success.
|
||||||
|
|
||||||
|
return Task.FromResult(result.Status != "Failed");
|
||||||
|
|
||||||
|
} */
|
||||||
|
}
|
||||||
|
}
|
@ -274,7 +274,7 @@ namespace Yavsc
|
|||||||
|
|
||||||
// Add application services.
|
// Add application services.
|
||||||
services.AddTransient<IEmailSender, MailSender>();
|
services.AddTransient<IEmailSender, MailSender>();
|
||||||
services.AddTransient<IYavscMessageSender, GCMSender>();
|
services.AddTransient<IYavscMessageSender, YavscMessageSender>();
|
||||||
services.AddTransient<IBillingService, BillingService>();
|
services.AddTransient<IBillingService, BillingService>();
|
||||||
services.AddTransient<IDataStore, FileDataStore>( (sp) => new FileDataStore("googledatastore",false) );
|
services.AddTransient<IDataStore, FileDataStore>( (sp) => new FileDataStore("googledatastore",false) );
|
||||||
services.AddTransient<ICalendarManager, CalendarManager>();
|
services.AddTransient<ICalendarManager, CalendarManager>();
|
||||||
|
Reference in New Issue
Block a user