public void CreateChat(string[] lu, string name) { var db = new ChatV2DBEntities(); var currentuser = db.User.Where(x => x.IdUser == WebSecurity.CurrentUserId).FirstOrDefault(); Group group = new Group() { Title = name, CreateDate = DateTime.Now, CreatorId = currentuser.IdUser }; db.Group.Add(group); db.SaveChanges(); db.GroupMember.Add(new GroupMember() { IdGroup = group.IdGroup, IdMemberStatus = 1, IdUser = currentuser.IdUser }); db.SaveChanges(); foreach (string u in lu) { db.GroupMember.Add(new GroupMember() { IdGroup = group.IdGroup, IdMemberStatus = 1, IdUser = int.Parse(u) }); } db.SaveChanges(); }
public override System.Threading.Tasks.Task OnReconnected() { var db = new ChatV2DBEntities(); var user = db.User.Where(x => x.Username == this.Context.User.Identity.Name).FirstOrDefault(); if (!UserToConnection.Keys.Any(x => x == user.IdUser.ToString())) { UserToConnection.Add(user.IdUser.ToString(), Context.ConnectionId); } return base.OnReconnected(); }
public PartialViewResult CreateChat() { var db = new ChatV2DBEntities(); var currentuser = db.User.Where(x => x.IdUser == WebSecurity.CurrentUserId).FirstOrDefault(); var users = db.User .Where(x => db.PrivateMessage.Any(m => m.IdRecipient == currentuser.IdUser && m.IdSender == x.IdUser && m.IdStatusMessage == 1) && db.PrivateMessage.Any(m => m.IdRecipient == x.IdUser && m.IdSender == currentuser.IdUser && m.IdStatusMessage == 1) ).ToList(); return PartialView(users); }
public override System.Threading.Tasks.Task OnDisconnected(bool stopCalled) { var db = new ChatV2DBEntities(); var user = db.User.Where(x => x.Username == this.Context.User.Identity.Name).FirstOrDefault(); if (UserToConnection.Keys.Any(x => x == user.IdUser.ToString())) { UserToConnection.Remove(user.IdUser.ToString()); } return base.OnDisconnected(stopCalled); }
public void AddFriend(int idsender, int idrecipient) { var db = new ChatV2DBEntities(); if (!db.PrivateMessage.Any(x => (x.IdSender == idsender && x.IdRecipient == idrecipient && x.IdStatusMessage == 1))) { db.PrivateMessage.Add(new PrivateMessage() { DatetimeSending = DateTime.Now, IdRecipient = idrecipient, IdSender = idsender, IdStatusMessage = 1, Text = "request" }); db.SaveChanges(); } }
public override System.Threading.Tasks.Task OnConnected() { var db = new ChatV2DBEntities(); var user = db.User.Where(x => x.Username == this.Context.User.Identity.Name).FirstOrDefault(); if (!UserToConnection.Keys.Any(x => x == user.IdUser.ToString())) //first in hub { UserToConnection.Add(user.IdUser.ToString(), Context.ConnectionId); } else { if(UserToConnection.Where(x => x.Key == user.IdUser.ToString()).FirstOrDefault().Value != Context.ConnectionId) //second+ in hub { ConnectionToGroup.Remove(UserToConnection.Where(x => x.Key == user.IdUser.ToString()).FirstOrDefault().Value); // clean old connection UserToConnection.Remove(user.IdUser.ToString()); UserToConnection.Add(user.IdUser.ToString(), Context.ConnectionId); } } return base.OnConnected(); }
private static Dictionary<string, string> UserToConnection = new Dictionary<string, string>(); //users on hub #endregion Fields #region Methods public void Join(string idgroup) { var db = new ChatV2DBEntities(); var user = db.User.Where(x => x.Username == this.Context.User.Identity.Name).FirstOrDefault(); if (!ConnectionToGroup.Any(x => x.Key == Context.ConnectionId)) //if user is not joined { if(UserToConnection.Any(x => x.Key == user.IdUser.ToString())) //and user already on hub { if (UserToConnection.Where(x => x.Key == user.IdUser.ToString()).FirstOrDefault().Value == Context.ConnectionId) { ConnectionToGroup.Add(Context.ConnectionId, idgroup); } else { UserToConnection.Remove(user.IdUser.ToString()); UserToConnection.Add(user.IdUser.ToString(), Context.ConnectionId); ConnectionToGroup.Add(Context.ConnectionId, idgroup); } } else { //if user comes over } } else { //if user already joined if (UserToConnection.Where(x => x.Key == user.IdUser.ToString()).FirstOrDefault().Value == Context.ConnectionId) //everything ok with connection { ConnectionToGroup.Add(Context.ConnectionId, idgroup); } else { //if user comes over } } }
public void SendPrivateMessage(int idsender, int idrecipient, string message) { var db = new ChatV2DBEntities(); if (db.PrivateMessage.Any(x => (x.IdSender == idsender && x.IdRecipient == idrecipient && x.IdStatusMessage == 1)) && db.PrivateMessage.Any(x => (x.IdSender == idrecipient && x.IdRecipient == idsender && x.IdStatusMessage == 1))) { db.PrivateMessage.Add(new PrivateMessage() { IdRecipient = idrecipient, IdSender = idsender, IdStatusMessage = 2, DatetimeSending = DateTime.Now, Text = message }); db.SaveChanges(); Clients.Caller.addNewMessageToPage(idsender, db.User.Where(x => x.IdUser == idsender).FirstOrDefault().Username, message, DateTime.Now.ToLongTimeString()); if (UserToConnection.Where(x => x.Key == idrecipient.ToString()).FirstOrDefault().Value != null) Clients.Client(UserToConnection.Where(x => x.Key == idrecipient.ToString()).FirstOrDefault().Value).addNewMessageToPage(idsender, db.User.Where(x => x.IdUser == idsender).FirstOrDefault().Username, message, DateTime.Now.ToLongTimeString()); } }
public PartialViewResult GroupChat(int idcurrentuser, int idgroupchat) { try { var db = new ChatV2DBEntities(); GroupMessageData data = new GroupMessageData(); if (db.GroupMember.Any(x => x.IdUser == idcurrentuser && x.IdGroup == idgroupchat)) { data.CurrentUser = db.User.Where(x => x.IdUser == idcurrentuser).FirstOrDefault(); data.Group = db.Group.Where(x => x.IdGroup == idgroupchat).FirstOrDefault(); data.Interlocutors = db.GroupMember .Join(db.User, gm => gm.IdUser, u => u.IdUser, (gm, u) => new { gm, u }) .Where(x => x.gm.IdGroup == idgroupchat) .Where(x => x.u.IdUser != idcurrentuser) .Select(x => x.u).ToList(); if (db.GroupMessage.Where(x => x.IdRecipientsGroup == idgroupchat).ToList().Count > 100) { data.Messages = db.GroupMessage.Where(x => x.IdRecipientsGroup == idgroupchat).ToList() .OrderByDescending(x => x.DatetimeSending).Take(100).Reverse().ToList(); } else { data.Messages = db.GroupMessage.Where(x => x.IdRecipientsGroup == idgroupchat).ToList(); } return PartialView(data); } return PartialView(null); } catch (Exception ex) { return PartialView("ErrorException", ex); } }
public JsonResult SearchUser(string uname, string idcuruser) { var db = new ChatV2DBEntities(); List<User> users = new List<ChatModels.User>(); int cur = Convert.ToInt32(idcuruser); var v = db.User .Select(x => new { IdUser = x.IdUser, Username = x.Username, isFriend = db.PrivateMessage.Any(z => (z.IdSender == cur && z.IdRecipient == x.IdUser && z.IdStatusMessage == 1)) }) .Where(x => !x.isFriend && x.IdUser != cur && x.Username.Contains(uname)) .Select(x => new { x.IdUser, x.Username }).ToList(); return new JsonResult() { Data = v, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; }
public PartialViewResult PrivateChat(int? idcurrentuser, int? idinterlocutor) { try { if (idcurrentuser == null && idinterlocutor == null) { return PartialView(null); } else { var db = new ChatV2DBEntities(); PrivateMessageData data = new PrivateMessageData(); if (db.PrivateMessage.Where(x => (x.IdSender == idcurrentuser && x.IdRecipient == idinterlocutor) || (x.IdSender == idinterlocutor && x.IdRecipient == idcurrentuser)).Count() > 100) { data.Messages = db.PrivateMessage .Where(x => (x.IdSender == idcurrentuser && x.IdRecipient == idinterlocutor) || (x.IdSender == idinterlocutor && x.IdRecipient == idcurrentuser)).OrderByDescending(x => x.DatetimeSending).Take(100).Reverse().ToList(); } else { data.Messages = db.PrivateMessage .Where(x => (x.IdSender == idcurrentuser && x.IdRecipient == idinterlocutor) || (x.IdSender == idinterlocutor && x.IdRecipient == idcurrentuser)).OrderBy(x => x.DatetimeSending).ToList(); } data.CurrentUser = db.User.Where(x => x.IdUser == idcurrentuser).FirstOrDefault(); data.Interlocutor = db.User.Where(x => x.IdUser == idinterlocutor).FirstOrDefault(); return PartialView(data); } } catch (Exception ex) { return PartialView("ErrorException", ex); } }
public ActionResult Index() { try { if (!WebSecurity.IsAuthenticated) { return RedirectToAction("Login", "Account"); } else { var db = new ChatV2DBEntities(); IndexData data = new IndexData(); data.CurentUser = db.User.Where(x => x.IdUser == WebSecurity.CurrentUserId).FirstOrDefault(); data.Contacts.AddRange(db.User .Where(u => db.PrivateMessage.Any(x => (x.IdSender == u.IdUser && x.IdRecipient == data.CurentUser.IdUser && x.IdStatusMessage == 1)) && db.PrivateMessage.Any(x => (x.IdSender == data.CurentUser.IdUser && x.IdRecipient == u.IdUser && x.IdStatusMessage == 1)))); data.Followers.AddRange(db.User .Where(u => db.PrivateMessage.Any(x => (x.IdSender == u.IdUser && x.IdRecipient == data.CurentUser.IdUser && x.IdStatusMessage == 1)) && !db.PrivateMessage.Any(x => (x.IdSender == data.CurentUser.IdUser && x.IdRecipient == u.IdUser && x.IdStatusMessage == 1)))); data.Chats.AddRange(db.Group .Join(db.GroupMember, g => g.IdGroup, gm => gm.IdGroup, (g, gm) => new { g, gm }) .Where(x => x.gm.IdUser == data.CurentUser.IdUser) .Select(x=> x.g) ); return View(data); } } catch (Exception ex) { return View("ErrorException", ex.Message); } }
public void SendGroupMessage(int idsender, int idgroup, string message) { var db = new ChatV2DBEntities(); if(db.GroupMember.Any(x => x.IdGroup == idgroup && x.IdUser == idsender)) { db.GroupMessage.Add(new GroupMessage() { IdRecipientsGroup = idgroup, IdSender = idsender, DatetimeSending = DateTime.Now, Text = message }); db.SaveChanges(); } Clients.Caller.addNewMessageToPage(idsender, db.User.Where(x => x.IdUser == idsender).FirstOrDefault().Username, message, DateTime.Now.ToLongTimeString()); Clients.Clients(ConnectionToGroup.Where(x => x.Value == idgroup.ToString() && x.Key != Context.ConnectionId).Select(x => x.Key).ToList()).addNewMessageToPage(idsender, db.User.Where(x => x.IdUser == idsender).FirstOrDefault().Username, message, DateTime.Now.ToLongTimeString()); }