diff --git a/src/Yavsc.Abstract/Messaging/NotificationTypes.cs b/src/Yavsc.Abstract/Messaging/NotificationTypes.cs index 56e88d07..d551cbbc 100644 --- a/src/Yavsc.Abstract/Messaging/NotificationTypes.cs +++ b/src/Yavsc.Abstract/Messaging/NotificationTypes.cs @@ -10,6 +10,5 @@ namespace Yavsc public const string Error = "error"; public const string ContactRefused = "contact refused"; public const string ExistingUserName ="existing user name"; - public const string ServerMessage = "server message"; } } diff --git a/src/Yavsc.Server/Interfaces/IGoogleCloudMessageSender.cs b/src/Yavsc.Server/Interfaces/IYavscMessageSender.cs similarity index 76% rename from src/Yavsc.Server/Interfaces/IGoogleCloudMessageSender.cs rename to src/Yavsc.Server/Interfaces/IYavscMessageSender.cs index 4509b8bf..9fcc4449 100644 --- a/src/Yavsc.Server/Interfaces/IGoogleCloudMessageSender.cs +++ b/src/Yavsc.Server/Interfaces/IYavscMessageSender.cs @@ -1,4 +1,4 @@ - + using System.Collections.Generic; using System.Threading.Tasks; using Yavsc.Interfaces.Workflow; @@ -11,18 +11,18 @@ namespace Yavsc.Services public interface IYavscMessageSender { Task NotifyBookQueryAsync( - IEnumerable DeviceIds, + IEnumerable connectionIds, RdvQueryEvent ev); Task NotifyEstimateAsync( - IEnumerable registrationId, + IEnumerable connectionIds, EstimationEvent ev); Task NotifyHairCutQueryAsync( - IEnumerable registrationId, + IEnumerable connectionIds, HairCutQueryEvent ev); Task NotifyAsync( - IEnumerable regids, + IEnumerable connectionIds, IEvent yaev); } } diff --git a/src/Yavsc.Server/Models/ApplicationUser.cs b/src/Yavsc.Server/Models/ApplicationUser.cs index bbc921bb..6cff916c 100644 --- a/src/Yavsc.Server/Models/ApplicationUser.cs +++ b/src/Yavsc.Server/Models/ApplicationUser.cs @@ -57,7 +57,7 @@ namespace Yavsc.Models /// /// [InverseProperty("DeviceOwner"),JsonIgnore] - public virtual List DeviceDeclarations { get; set; } + public virtual List DeviceDeclaration { get; set; } [InverseProperty("Owner"),JsonIgnore] public virtual List Connections { get; set; } diff --git a/src/Yavsc/ApiControllers/Business/BillingController.cs b/src/Yavsc/ApiControllers/Business/BillingController.cs index 3e92a084..6daf24c6 100644 --- a/src/Yavsc/ApiControllers/Business/BillingController.cs +++ b/src/Yavsc/ApiControllers/Business/BillingController.cs @@ -108,7 +108,7 @@ namespace Yavsc.ApiControllers public async Task ProSign(string billingCode, long id) { var estimate = dbContext.Estimates. - Include(e=>e.Client).Include(e=>e.Client.DeviceDeclarations) + Include(e=>e.Client).Include(e=>e.Client.DeviceDeclaration) .Include(e=>e.Bill).Include(e=>e.Owner).Include(e=>e.Owner.Performer) .FirstOrDefault(e=>e.Id == id); if (estimate == null) @@ -127,12 +127,10 @@ namespace Yavsc.ApiControllers var yaev = new EstimationEvent(estimate,_localizer); - var regids = estimate.Client.DeviceDeclarations.Select(d => d.DeviceId).ToArray(); + var regids = new [] { estimate.Client.Id }; bool gcmSent = false; - if (regids.Length>0) { var grep = await _GCMSender.NotifyEstimateAsync(regids,yaev); gcmSent = grep.success>0; - } return Ok (new { ProviderValidationDate = estimate.ProviderValidationDate, GCMSent = gcmSent }); } diff --git a/src/Yavsc/ApiControllers/Relationship/ChatApiController.cs b/src/Yavsc/ApiControllers/Relationship/ChatApiController.cs index adc405d4..f66dd86c 100644 --- a/src/Yavsc/ApiControllers/Relationship/ChatApiController.cs +++ b/src/Yavsc/ApiControllers/Relationship/ChatApiController.cs @@ -1,14 +1,19 @@ using System.Collections.Generic; using System.Linq; -using Microsoft.AspNet.Mvc; using Microsoft.Data.Entity; +using System.Threading.Tasks; +using System.Security.Claims; +using Microsoft.AspNet.Mvc; +using Microsoft.AspNet.Identity; +using Yavsc.Models; +using Yavsc.ViewModels.Chat; + namespace Yavsc.Controllers { - using Microsoft.AspNet.Identity; - using Models; - using ViewModels.Chat; + using System.Threading.Tasks; + [Route("api/chat")] public class ChatApiController : Controller { @@ -25,30 +30,63 @@ namespace Yavsc.Controllers public IEnumerable GetUserList() { List result = new List(); - var cxsQuery = dbContext.ChatConnection?.Include(c=>c.Owner) - .Where(cx => cx.Connected).GroupBy( c => c.ApplicationUserId ); + var cxsQuery = dbContext.ChatConnection?.Include(c => c.Owner) + .Where(cx => cx.Connected).GroupBy(c => c.ApplicationUserId); - if (cxsQuery!=null) - foreach (var g in cxsQuery) { + if (cxsQuery != null) + foreach (var g in cxsQuery) + { - var uid = g.Key; - var cxs = g.ToList(); - if (cxs !=null) - if (cxs.Count>0) { - var user = cxs.First().Owner; - if (user!=null ) { - result.Add(new ChatUserInfo { UserName = user.UserName, - UserId = user.Id, Avatar = user.Avatar, Connections = cxs, - Roles = ( userManager.GetRolesAsync(user) ).Result.ToArray() }); + var uid = g.Key; + var cxs = g.ToList(); + if (cxs != null) + if (cxs.Count > 0) + { + var user = cxs.First().Owner; + if (user != null) + { + result.Add(new ChatUserInfo + { + UserName = user.UserName, + UserId = user.Id, + Avatar = user.Avatar, + Connections = cxs, + Roles = (userManager.GetRolesAsync(user)).Result.ToArray() + }); + } + else + { + result.Add(new ChatUserInfo { Connections = cxs }); + } } - else { - result.Add(new ChatUserInfo { Connections = cxs }); - } - } - } + } + return result; } - + // GET: api/chat/userName + [HttpGet("{userName}", Name = "uinfo")] + public IActionResult GetUserInfo([FromRoute] string userName) + { + if (!ModelState.IsValid) + // Miguel mech profiler + { + return HttpBadRequest(ModelState); + } + var uid = User.GetUserId(); + var user = dbContext.ApplicationUser.Include(u => u.Connections).FirstOrDefault(u => u.UserName == userName); + + if (user == null) return HttpNotFound(); + + return Ok(new ChatUserInfo + { + UserName = user.UserName, + UserId = user.Id, + Avatar = user.Avatar, + Connections = user.Connections, + Roles = (userManager.GetRolesAsync(user)).Result.ToArray() + }); + } + } } diff --git a/src/Yavsc/Controllers/Contracting/CommandController.cs b/src/Yavsc/Controllers/Contracting/CommandController.cs index b4224132..9cc4cbe9 100644 --- a/src/Yavsc/Controllers/Contracting/CommandController.cs +++ b/src/Yavsc/Controllers/Contracting/CommandController.cs @@ -29,12 +29,11 @@ namespace Yavsc.Controllers protected IStringLocalizer _localizer; protected SiteSettings _siteSettings; protected SmtpSettings _smtpSettings; - protected ICalendarManager _calendarManager; protected readonly ILogger _logger; public CommandController(ApplicationDbContext context, IOptions googleSettings, - IYavscMessageSender GCMSender, + IYavscMessageSender messageSender, UserManager userManager, ICalendarManager calendarManager, IStringLocalizer localizer, @@ -44,7 +43,7 @@ namespace Yavsc.Controllers ILoggerFactory loggerFactory) { _context = context; - _MessageSender = GCMSender; + _MessageSender = messageSender; _emailSender = emailSender; _googleSettings = googleSettings.Value; _userManager = userManager; @@ -136,7 +135,7 @@ namespace Yavsc.Controllers ); var pro = _context.Performers.Include( u => u.Performer - ).Include(u => u.Performer.DeviceDeclarations) + ).Include(u => u.Performer.DeviceDeclaration) .FirstOrDefault( x => x.PerformerId == command.PerformerId ); @@ -145,9 +144,6 @@ namespace Yavsc.Controllers command.ClientId = uid; command.PerformerProfile = pro; // FIXME Why!! - // ModelState.ClearValidationState("PerformerProfile.Avatar"); - // ModelState.ClearValidationState("Client.Avatar"); - // ModelState.ClearValidationState("ClientId"); ModelState.MarkFieldSkipped("ClientId"); if (ModelState.IsValid) @@ -173,8 +169,8 @@ namespace Yavsc.Controllers try { _logger.LogInformation("sending message"); - var regids = new [] { command.PerformerProfile.Performer.Id }; - nrep = await _MessageSender.NotifyBookQueryAsync(regids, yaev); + var uids = new[] { command.PerformerProfile.PerformerId }; + nrep = await _MessageSender.NotifyBookQueryAsync(uids, yaev); // TODO setup a profile choice to allow notifications // both on mailbox and mobile // if (grep==null || grep.success<=0 || grep.failure>0) @@ -182,19 +178,24 @@ namespace Yavsc.Controllers } catch (Exception ex) { - _logger.LogError("Message sending failed with: "+ex.Message); + _logger.LogError("Message sending failed with: " + ex.Message); throw; } } - else { - nrep = new MessageWithPayloadResponse { failure=1, results = new MessageWithPayloadResponse.Result[] { - new MessageWithPayloadResponse.Result - { - error=NotificationTypes.ContactRefused, - registration_id= pro.PerformerId + else + { + nrep = new MessageWithPayloadResponse + { + failure = 1, + results = new MessageWithPayloadResponse.Result[] { + new MessageWithPayloadResponse.Result + { + error=NotificationTypes.ContactRefused, + registration_id= pro.PerformerId } - } }; + } + }; _logger.LogInformation("Command.Create && ( !pro.AcceptNotifications || |pro.AcceptPublicContact ) "); } ViewBag.MessagingResponsePayload = nrep; diff --git a/src/Yavsc/Controllers/Contracting/CommandFormsController.cs b/src/Yavsc/Controllers/Contracting/CommandFormsController.cs index e0d14cba..ad7b764e 100644 --- a/src/Yavsc/Controllers/Contracting/CommandFormsController.cs +++ b/src/Yavsc/Controllers/Contracting/CommandFormsController.cs @@ -15,7 +15,7 @@ namespace Yavsc.Controllers public CommandFormsController(ApplicationDbContext context) { - _context = context; + _context = context; } // GET: CommandForms @@ -48,9 +48,10 @@ namespace Yavsc.Controllers SetViewBag(); return View(); } - private void SetViewBag(CommandForm commandForm=null) { + private void SetViewBag(CommandForm commandForm = null) + { ViewBag.ActivityCode = new SelectList(_context.Activities, "Code", "Name", commandForm?.ActivityCode); - ViewBag.ActionName = Startup.Forms.Select( c => new SelectListItem { Value = c, Text = c, Selected = (commandForm?.ActionName == c) } ); + ViewBag.ActionName = Startup.Forms.Select(c => new SelectListItem { Value = c, Text = c, Selected = (commandForm?.ActionName == c) }); } // POST: CommandForms/Create [HttpPost] diff --git a/src/Yavsc/Controllers/Haircut/HairCutCommandController.cs b/src/Yavsc/Controllers/Haircut/HairCutCommandController.cs index 7608f3da..3a852183 100644 --- a/src/Yavsc/Controllers/Haircut/HairCutCommandController.cs +++ b/src/Yavsc/Controllers/Haircut/HairCutCommandController.cs @@ -31,8 +31,8 @@ namespace Yavsc.Controllers { public HairCutCommandController(ApplicationDbContext context, IOptions payPalSettings, - IOptions googleSettings, - IYavscMessageSender GCMSender, + IOptions googleSettings, + IYavscMessageSender GCMSender, UserManager userManager, IStringLocalizer localizer, IEmailSender emailSender, @@ -53,7 +53,7 @@ namespace Yavsc.Controllers .Include(x => x.PerformerProfile) .Include(x => x.Prestation) .Include(x => x.PerformerProfile.Performer) - .Include(x => x.PerformerProfile.Performer.DeviceDeclarations) + .Include(x => x.PerformerProfile.Performer.DeviceDeclaration) .Include(x => x.Regularisation) .SingleAsync(m => m.Id == id); query.SelectedProfile = await _context.BrusherProfile.SingleAsync(b => b.UserId == query.PerformerId); @@ -100,11 +100,10 @@ namespace Yavsc.Controllers var yaev = command.CreatePaymentEvent(paymentInfo, _localizer); if (command.PerformerProfile.AcceptNotifications) { - if (command.PerformerProfile.Performer.DeviceDeclarations.Count > 0) + if (command.PerformerProfile.Performer.DeviceDeclaration.Count > 0) { - var regids = command.PerformerProfile.Performer - .DeviceDeclarations.Select(d => d.DeviceId); - grep = await _MessageSender.NotifyAsync(regids, yaev); + var regid = command.PerformerProfile.PerformerId; + grep = await _MessageSender.NotifyAsync(new [] {regid}, yaev); } // TODO setup a profile choice to allow notifications // both on mailbox and mobile @@ -217,7 +216,7 @@ namespace Yavsc.Controllers _logger.LogInformation("le Model _est_ valide."); var pro = _context.Performers.Include( u => u.Performer - ).Include(u => u.Performer.DeviceDeclarations) + ).Include(u => u.Performer.DeviceDeclaration) .FirstOrDefault( x => x.PerformerId == model.PerformerId ); @@ -268,10 +267,10 @@ namespace Yavsc.Controllers { if (pro.AcceptNotifications) { - if (pro.Performer.DeviceDeclarations.Count > 0) + if (pro.Performer.DeviceDeclaration.Count > 0) { - var regids = pro.Performer.DeviceDeclarations.Select(d => d.DeviceId); - grep = await _MessageSender.NotifyHairCutQueryAsync(regids, yaev); + var uids = new[] { pro.PerformerId }; + grep = await _MessageSender.NotifyHairCutQueryAsync(uids, yaev); } // TODO setup a profile choice to allow notifications // both on mailbox and mobile @@ -390,7 +389,7 @@ namespace Yavsc.Controllers ); var pro = _context.Performers.Include( u => u.Performer - ).Include(u => u.Performer.DeviceDeclarations) + ).Include(u => u.Performer.DeviceDeclaration) .FirstOrDefault( x => x.PerformerId == command.PerformerId ); @@ -427,11 +426,10 @@ namespace Yavsc.Controllers if (pro.AcceptNotifications && pro.AcceptPublicContact) { - if (pro.Performer.DeviceDeclarations?.Count > 0) + if (pro.Performer.DeviceDeclaration?.Count > 0) { - var regids = command.PerformerProfile.Performer - .DeviceDeclarations.Select(d => d.DeviceId); - grep = await _MessageSender.NotifyHairCutQueryAsync(regids, yaev); + var uids = new [] { command.PerformerProfile.PerformerId }; + grep = await _MessageSender.NotifyHairCutQueryAsync(uids, yaev); } // TODO setup a profile choice to allow notifications // both on mailbox and mobile, and to allow calendar event insertion. diff --git a/src/Yavsc/Helpers/WorkflowHelpers.cs b/src/Yavsc/Helpers/WorkflowHelpers.cs index 2d6b2a78..9e8ea149 100644 --- a/src/Yavsc/Helpers/WorkflowHelpers.cs +++ b/src/Yavsc/Helpers/WorkflowHelpers.cs @@ -21,7 +21,7 @@ namespace Yavsc.Helpers .Include(p=>p.Activity) .Include(p=>p.Performer) .Include(p=>p.Performer.Posts) - .Include(p=>p.Performer.DeviceDeclarations) + .Include(p=>p.Performer.DeviceDeclaration) .Where(p => p.Active && p.Activity.Any(u=>u.DoesCode==actCode)).OrderBy( x => x.Rate ) .ToArray(); List result = new List (); diff --git a/src/Yavsc/Services/YavscMessageSender.cs b/src/Yavsc/Services/YavscMessageSender.cs index ce43a18f..0a1be895 100644 --- a/src/Yavsc/Services/YavscMessageSender.cs +++ b/src/Yavsc/Services/YavscMessageSender.cs @@ -11,6 +11,7 @@ using Yavsc.Models; using Yavsc.Models.Google.Messaging; using Yavsc.Models.Haircut; using Yavsc.Models.Messaging; +using Yavsc.Services; namespace Yavsc.Services { @@ -58,11 +59,11 @@ namespace Yavsc.Services try { List results = new List(); - foreach(var clientId in raa) { + foreach(var userId in raa) { MessageWithPayloadResponse.Result result = new MessageWithPayloadResponse.Result(); - result.registration_id = clientId; + result.registration_id = userId; - var user = _dbContext.Users.FirstOrDefault(u=> u.Id == clientId); + var user = _dbContext.Users.FirstOrDefault(u=> u.Id == userId); if (user==null) { response.failure++; @@ -82,10 +83,9 @@ namespace Yavsc.Services var body = ev.CreateBody(); + var cxids = ChatHub.ChatUserNames.Where (kv=>kv.Value == user.UserName).Select(kv => kv.Key).ToArray(); - - var hubClient = hubContext.Clients.User(clientId); - if (hubClient == null) + if (cxids.Length==0) { _logger.LogDebug($"Sending to {user.UserName} <{user.Email}> : {body}"); var mailSent = await _emailSender.SendEmailAsync( @@ -107,11 +107,15 @@ namespace Yavsc.Services } } else { - _logger.LogDebug($"Sending signal to {string.Join(" ",raa)} : "+JsonConvert.SerializeObject(ev)); - // we assume that each hub connected client will handle this signal + _logger.LogDebug($"Sending signal to {string.Join(" ",cxids)} : "+JsonConvert.SerializeObject(ev)); - hubClient.notify(NotificationTypes.ServerMessage, - $"# {ev.Sender} (un client) vous demande un rendez-vous\n"+body); + foreach( var cxid in cxids) { + var hubClient = hubContext.Clients.User(cxid); + var data = new Dictionary(); + data["event"] = ev; + + hubClient.push(ev.Topic, data ); + } result.message_id=MimeKit.Utils.MimeUtils.GenerateMessageId( siteSettings.Authority @@ -130,25 +134,25 @@ namespace Yavsc.Services } } - public async Task NotifyBookQueryAsync( IEnumerable registrationIds, RdvQueryEvent ev) + public async Task NotifyBookQueryAsync( IEnumerable userIds, RdvQueryEvent ev) { - return await NotifyEvent(registrationIds, ev); + return await NotifyEvent(userIds, ev); } - public async Task NotifyEstimateAsync(IEnumerable registrationIds, EstimationEvent ev) + public async Task NotifyEstimateAsync(IEnumerable userIds, EstimationEvent ev) { - return await NotifyEvent(registrationIds, ev); + return await NotifyEvent(userIds, ev); } public async Task NotifyHairCutQueryAsync( - IEnumerable registrationIds, HairCutQueryEvent ev) + IEnumerable userIds, HairCutQueryEvent ev) { - return await NotifyEvent(registrationIds, ev); + return await NotifyEvent(userIds, ev); } - public async Task NotifyAsync(IEnumerable regids, IEvent yaev) + public async Task NotifyAsync(IEnumerable userIds, IEvent yaev) { - return await NotifyEvent(regids, yaev); + return await NotifyEvent(userIds, yaev); } /* SMS with Twilio: