private static async Task ReceiveMessage(HttpContext context, dynamic obj) { var uid = context.LoginId(); int id = int.Parse(obj.id.ToString()); var msg = obj.message.ToString(); using (var c = new WebChat()) { var conv = c.Conversations .Include(co => co.ConversationParticipants) .SingleOrDefault(co => co.Id == id); if (conv != null && conv.ConversationParticipants.Any(cp => cp.UserId == uid)) { var msgObject = new Messages { Conversation = conv, Text = MessageUtils.Reformat(msg), UserId = context.LoginId() }; c.Add(msgObject); c.SaveChanges(); await BroadcastMessage(c, msgObject, uid); } } }
public JsonResult GetUserConversation(int id) { if (HttpContext.LoginId() > 0) { using (var c = new WebChat()) { var conv = c.GetConversationBetween( c.Users .Where(u => u.Id == id || u.Id == HttpContext.LoginId() ) ); if (conv == null) { conv = new Conversations(); var p1 = new ConversationParticipants { UserId = HttpContext.LoginId() }; conv.ConversationParticipants.Add(p1); var p2 = new ConversationParticipants { UserId = id }; conv.ConversationParticipants.Add(p2); c.Add(conv); c.SaveChanges(); conv = c.GetConversationBetween( c.Users .Where(u => u.Id == id || u.Id == HttpContext.LoginId() ) ); } return(new JsonResult(new { id = conv.Id })); } } return(new JsonResult("")); }
public JsonResult BlockUser(int id) { if (HttpContext.LoginId() > 0) { using (var c = new WebChat()) { var u1 = c.Users.FirstOrDefault(u => u.Id == HttpContext.LoginId()); var u2 = c.Users.FirstOrDefault(u => u.Id == id); if (u1 != null && u2 != null) { int count = 0; foreach (Blocks f in c.Blocks) { var blockOne = f.Block1 == u1.Id && f.Block2 == u2.Id; var blockTwo = f.Block1 == u2.Id && f.Block2 == u1.Id; if (blockOne || blockTwo) { count++; } } if (count == 0) { var blockLink = new Blocks { Block1Navigation = u1, Block2Navigation = u2 }; c.Add(blockLink); c.SaveChanges(); return(new JsonResult(new { added = true })); } } return(new JsonResult(new { added = false })); } } return(new JsonResult("")); }
public JsonResult AddUserAsFriend(int id) { if (HttpContext.LoginId() > 0) { using (var c = new WebChat()) { var u1 = c.Users.FirstOrDefault(u => u.Id == HttpContext.LoginId()); var u2 = c.Users.FirstOrDefault(u => u.Id == id); if (u1 != null && u2 != null) { int count = 0; foreach (Friends f in c.Friends) { var friendOne = f.Friend1 == u1.Id && f.Friend2 == u2.Id; var friendTwo = f.Friend1 == u2.Id && f.Friend2 == u1.Id; if (friendOne || friendTwo) { count++; } } if (count == 0) { var friendLink = new Friends { Friend1Navigation = u1, Friend2Navigation = u2 }; c.Add(friendLink); c.SaveChanges(); return(new JsonResult(new { added = true })); } } return(new JsonResult(new { added = false })); } } return(new JsonResult("")); }