From 5298431cc72584baaf40a4faedd03e86e17e6351 Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Tue, 1 Nov 2016 23:22:46 +0100 Subject: [PATCH] wip social --- Yavsc/ApiControllers/ContactsApiController.cs | 147 ++++++++++++++++++ Yavsc/Hubs/ChatHub.cs | 123 +++++++++------ .../20161010102616_recontact.Designer.cs | 1 - Yavsc/Model/ApplicationDbContext.cs | 3 + Yavsc/Model/Chat/Connection.cs | 10 ++ Yavsc/Model/Identity/ApplicationUser.cs | 4 + Yavsc/Model/Messaging/ClientProviderInfo.cs | 4 + Yavsc/Views/Shared/_Layout.cshtml | 1 + Yavsc/Views/Shared/_PerformerPartial.cshtml | 2 + 9 files changed, 245 insertions(+), 50 deletions(-) create mode 100644 Yavsc/ApiControllers/ContactsApiController.cs create mode 100644 Yavsc/Model/Chat/Connection.cs diff --git a/Yavsc/ApiControllers/ContactsApiController.cs b/Yavsc/ApiControllers/ContactsApiController.cs new file mode 100644 index 00000000..d031c485 --- /dev/null +++ b/Yavsc/ApiControllers/ContactsApiController.cs @@ -0,0 +1,147 @@ +using System.Collections.Generic; +using System.Linq; +using Microsoft.AspNet.Http; +using Microsoft.AspNet.Mvc; +using Microsoft.Data.Entity; +using Yavsc.Model; +using Yavsc.Models; + +namespace Yavsc.Controllers +{ + [Produces("application/json")] + [Route("api/ContactsApi")] + public class ContactsApiController : Controller + { + private ApplicationDbContext _context; + + public ContactsApiController(ApplicationDbContext context) + { + _context = context; + } + + // GET: api/ContactsApi + [HttpGet] + public IEnumerable GetClientProviderInfo() + { + return _context.ClientProviderInfo; + } + + // GET: api/ContactsApi/5 + [HttpGet("{id}", Name = "GetClientProviderInfo")] + public IActionResult GetClientProviderInfo([FromRoute] string id) + { + if (!ModelState.IsValid) + { + return HttpBadRequest(ModelState); + } + + ClientProviderInfo clientProviderInfo = _context.ClientProviderInfo.Single(m => m.UserId == id); + + if (clientProviderInfo == null) + { + return HttpNotFound(); + } + + return Ok(clientProviderInfo); + } + + // PUT: api/ContactsApi/5 + [HttpPut("{id}")] + public IActionResult PutClientProviderInfo(string id, [FromBody] ClientProviderInfo clientProviderInfo) + { + if (!ModelState.IsValid) + { + return HttpBadRequest(ModelState); + } + + if (id != clientProviderInfo.UserId) + { + return HttpBadRequest(); + } + + _context.Entry(clientProviderInfo).State = EntityState.Modified; + + try + { + _context.SaveChanges(); + } + catch (DbUpdateConcurrencyException) + { + if (!ClientProviderInfoExists(id)) + { + return HttpNotFound(); + } + else + { + throw; + } + } + + return new HttpStatusCodeResult(StatusCodes.Status204NoContent); + } + + // POST: api/ContactsApi + [HttpPost] + public IActionResult PostClientProviderInfo([FromBody] ClientProviderInfo clientProviderInfo) + { + if (!ModelState.IsValid) + { + return HttpBadRequest(ModelState); + } + + _context.ClientProviderInfo.Add(clientProviderInfo); + try + { + _context.SaveChanges(); + } + catch (DbUpdateException) + { + if (ClientProviderInfoExists(clientProviderInfo.UserId)) + { + return new HttpStatusCodeResult(StatusCodes.Status409Conflict); + } + else + { + throw; + } + } + + return CreatedAtRoute("GetClientProviderInfo", new { id = clientProviderInfo.UserId }, clientProviderInfo); + } + + // DELETE: api/ContactsApi/5 + [HttpDelete("{id}")] + public IActionResult DeleteClientProviderInfo(string id) + { + if (!ModelState.IsValid) + { + return HttpBadRequest(ModelState); + } + + ClientProviderInfo clientProviderInfo = _context.ClientProviderInfo.Single(m => m.UserId == id); + if (clientProviderInfo == null) + { + return HttpNotFound(); + } + + _context.ClientProviderInfo.Remove(clientProviderInfo); + _context.SaveChanges(); + + return Ok(clientProviderInfo); + } + + protected override void Dispose(bool disposing) + { + if (disposing) + { + _context.Dispose(); + } + base.Dispose(disposing); + } + + private bool ClientProviderInfoExists(string id) + { + return _context.ClientProviderInfo.Count(e => e.UserId == id) > 0; + } + } +} \ No newline at end of file diff --git a/Yavsc/Hubs/ChatHub.cs b/Yavsc/Hubs/ChatHub.cs index 9125efa8..13b3ec9e 100644 --- a/Yavsc/Hubs/ChatHub.cs +++ b/Yavsc/Hubs/ChatHub.cs @@ -19,64 +19,89 @@ // You should have received a copy of the GNU Lesser General Public License // along with this program. If not, see . using System.Threading.Tasks; -using System.Security.Claims; -using System.Security.Principal; using Microsoft.AspNet.SignalR; -using System; -// using Microsoft.AspNet.Authorization; +using System.Collections.Generic; +using System.Linq; namespace Yavsc { - public class ChatHub : Hub - { - public override Task OnConnected() + public class ChatHub : Hub { - bool isAuth = false; - string userId = null; - if (Context.User!=null) { - isAuth = Context.User.Identity.IsAuthenticated; - userId = Context.User.Identity.Name; - var group = isAuth ? - "authenticated":"anonymous"; - // Log ("Cx: " + group); - Groups.Add(Context.ConnectionId, group); - } else Groups.Add(Context.ConnectionId, "anonymous"); + public override Task OnConnected() + { + bool isAuth = false; + string userId = null; + if (Context.User != null) + { + isAuth = Context.User.Identity.IsAuthenticated; + userId = Context.User.Identity.Name; + var group = isAuth ? + "authenticated" : "anonymous"; + // Log ("Cx: " + group); + Groups.Add(Context.ConnectionId, group); + } + else Groups.Add(Context.ConnectionId, "anonymous"); - Clients.Group("authenticated").notify("connected", Context.ConnectionId, userId); - return base.OnConnected (); - } - public override Task OnDisconnected (bool stopCalled) - { - string userId = Context.User?.Identity.Name; - Clients.Group("authenticated").notify("disconnected", Context.ConnectionId, userId); - return base.OnDisconnected (stopCalled); - } + Clients.Group("authenticated").notify("connected", Context.ConnectionId, userId); - public override Task OnReconnected () - { - return base.OnReconnected (); - } + list.Add(new UserInfo + { + ConnectionId = Context.ConnectionId, + UserName = userId + }); - public void Send(string name, string message) - { - string uname = (Context.User!=null) ? - $"[{Context.User.Identity.Name}]": - $"(anony{name})"; - Clients.All.addMessage(uname,message); - } + return base.OnConnected(); + } + public override Task OnDisconnected(bool stopCalled) + { + string userId = Context.User?.Identity.Name; + Clients.Group("authenticated").notify("disconnected", Context.ConnectionId, userId); + list.Remove(list.Single(c=>c.ConnectionId==Context.ConnectionId)); + return base.OnDisconnected(stopCalled); + } + + public override Task OnReconnected() + { + return base.OnReconnected(); + } + public void Send(string name, string message) + { + string uname = (Context.User != null) ? + $"[{Context.User.Identity.Name}]" : + $"(anony{name})"; + Clients.All.addMessage(uname, message); + } - [Authorize] - public void SendPV (string connectionId, string message) - { - var sender = Context.User.Identity.Name; - // TODO personal black|white list + - // Contact list allowed only + - // only pro - var hubCxContext = Clients.User(connectionId); - var cli = Clients.Client(connectionId); - cli.addPV(sender,message); - } - } + + [Authorize] + public void SendPV(string connectionId, string message) + { + var sender = Context.User.Identity.Name; + // TODO personal black|white list + + // Contact list allowed only + + // only pro + var hubCxContext = Clients.User(connectionId); + var cli = Clients.Client(connectionId); + cli.addPV(sender, message); + } + public class UserInfo + { + + public string ConnectionId { get; set; } + + public string UserId { get; set; } + + public string UserName { get; set; } + + } + + static List list = new List(); + [Authorize] + public IEnumerable GetUserList() + { + return list; + } + } } diff --git a/Yavsc/Migrations/20161010102616_recontact.Designer.cs b/Yavsc/Migrations/20161010102616_recontact.Designer.cs index ab984b1b..c2d41f25 100644 --- a/Yavsc/Migrations/20161010102616_recontact.Designer.cs +++ b/Yavsc/Migrations/20161010102616_recontact.Designer.cs @@ -1,7 +1,6 @@ using System; using Microsoft.Data.Entity; using Microsoft.Data.Entity.Infrastructure; -using Microsoft.Data.Entity.Metadata; using Microsoft.Data.Entity.Migrations; using Yavsc.Models; diff --git a/Yavsc/Model/ApplicationDbContext.cs b/Yavsc/Model/ApplicationDbContext.cs index 02acf328..61a0d136 100644 --- a/Yavsc/Model/ApplicationDbContext.cs +++ b/Yavsc/Model/ApplicationDbContext.cs @@ -12,6 +12,7 @@ using Yavsc.Models.OAuth; using Yavsc.Models.Workflow; using Yavsc.Models.Identity; using Yavsc.Models.Market; +using Yavsc.Model; namespace Yavsc.Models { @@ -182,5 +183,7 @@ namespace Yavsc.Models public DbSet EstimateTemplates { get; set; } public DbSet Contacts { get; set; } + + public DbSet ClientProviderInfo { get; set; } } } diff --git a/Yavsc/Model/Chat/Connection.cs b/Yavsc/Model/Chat/Connection.cs new file mode 100644 index 00000000..349d0e2d --- /dev/null +++ b/Yavsc/Model/Chat/Connection.cs @@ -0,0 +1,10 @@ +namespace Yavsc.Model.Chat +{ + public class Connection + { + public string ConnectionID { get; set; } + public string UserAgent { get; set; } + public bool Connected { get; set; } + } + +} \ No newline at end of file diff --git a/Yavsc/Model/Identity/ApplicationUser.cs b/Yavsc/Model/Identity/ApplicationUser.cs index eecb1838..2f242aec 100644 --- a/Yavsc/Model/Identity/ApplicationUser.cs +++ b/Yavsc/Model/Identity/ApplicationUser.cs @@ -4,6 +4,7 @@ using Microsoft.AspNet.Identity.EntityFramework; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using Yavsc.Models.Identity; +using Yavsc.Model.Chat; namespace Yavsc.Models { @@ -53,6 +54,9 @@ namespace Yavsc.Models [InverseProperty("DeviceOwner")] public virtual List Devices { get; set; } + public ICollection Connections { get; set; } + + /// /// User's circles /// diff --git a/Yavsc/Model/Messaging/ClientProviderInfo.cs b/Yavsc/Model/Messaging/ClientProviderInfo.cs index c2bce7c8..bb9807fc 100644 --- a/Yavsc/Model/Messaging/ClientProviderInfo.cs +++ b/Yavsc/Model/Messaging/ClientProviderInfo.cs @@ -1,14 +1,18 @@ +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; namespace Yavsc.Model { public class ClientProviderInfo { public string UserName { get; set; } public string Avatar { get; set; } + [Key] public string UserId { get; set; } public int Rate { get; set; } public string EMail { get; set; } public string Phone { get; set; } public Location BillingAddress { get; set; } + public string ChatHubConnectionId { get; set; } } } diff --git a/Yavsc/Views/Shared/_Layout.cshtml b/Yavsc/Views/Shared/_Layout.cshtml index fc701d6e..9e3e6b35 100755 --- a/Yavsc/Views/Shared/_Layout.cshtml +++ b/Yavsc/Views/Shared/_Layout.cshtml @@ -49,6 +49,7 @@
  • @SR["Home"]
  • @SR["Blogs"]
  • @SR["About"] @SiteSettings.Value.Title
  • +
  • @SR["Chat"]
  • @SR["Contact"]
  • @await Html.PartialAsync("_LoginPartial") diff --git a/Yavsc/Views/Shared/_PerformerPartial.cshtml b/Yavsc/Views/Shared/_PerformerPartial.cshtml index 568f10dd..7b8496c0 100644 --- a/Yavsc/Views/Shared/_PerformerPartial.cshtml +++ b/Yavsc/Views/Shared/_PerformerPartial.cshtml @@ -30,6 +30,8 @@
  • @SR["Uses the mobile application, and receives push notifications"]
  • } + + }