public OperationResult <bool> SendMessage(string body, long conversationId) { Console.WriteLine($"Send message from {curUser.Login} - to {conversationId}"); try { using (DbMain.EFDbContext.ChatEntities db = new DbMain.EFDbContext.ChatEntities()) { var conversationMemer = db.ConversationMembers.FirstOrDefault(x => x.MemberId == curUser.Id && x.ConversationId == conversationId); if (conversationMemer == null) { return(new OperationResult <bool>(false, false, "You are din't chat user")); } var status = (ConversationMemberStatus)conversationMemer.MemberStatusId; switch (status) { case ConversationMemberStatus.None: case ConversationMemberStatus.Blocked: case ConversationMemberStatus.ReadOnly: case ConversationMemberStatus.LeftConversation: return(new OperationResult <bool>(false, false, $"You do not have permission to post to this Hangout. You status - {status}")); default: break; } var conversation = conversationMemer.Conversation; // conversation.LastChange = DateTimeOffset.UtcNow; DbMain.EFDbContext.ConversationReply reply = new DbMain.EFDbContext.ConversationReply() { AuthorId = curUser.Id, Body = body, ConversationId = conversationId, ConversationReplyStatusId = (int)ConversationReplyStatus.Sent, ReceiverId = curUser.Id }; db.ConversationReplies.Add(reply); if (db.SaveChanges() > 0) { var members = conversation.ConversationMembers.Select(x => x.User.Id).ToList(); members.Remove(curUser.Id); SendMessageToAllMembers(members, reply); // ChatServiceCallbackModel.SendMessageToGroup(members, new ConversationReply() //{ // Author = curUser.Login, // Body = body, // ConversationId = conversationId, // SendingTime = DateTimeOffset.UtcNow, // Status = ConversationReplyStatus.Received //}); return(new OperationResult <bool>(true)); } return(new OperationResult <bool>(false, false, "Send message error")); } } catch (Exception ex) { return(new OperationResult <bool>(false, false, "Internal error")); } }
private async void SendMessageToAllMembers(IEnumerable <long> listUserId, DbMain.EFDbContext.ConversationReply reply) { List <ConversationReply> conversationReplies = new List <ConversationReply>(); List <DbMain.EFDbContext.ConversationReply> dbReplies = new List <DbMain.EFDbContext.ConversationReply>(); await Task.Run(() => { using (DbMain.EFDbContext.ChatEntities db = new DbMain.EFDbContext.ChatEntities()) { int status = (int)ConversationReplyStatus.Received; foreach (var item in listUserId) { var DBreply = new DbMain.EFDbContext.ConversationReply() { AuthorId = reply.AuthorId, Body = reply.Body, ConversationId = reply.ConversationId, ConversationReplyStatusId = status, ReceiverId = item }; db.ConversationReplies.Add(DBreply); dbReplies.Add(DBreply); } db.SaveChanges(); } }); for (int i = 0; i < listUserId.Count(); i++) { ChatServiceCallbackModel.SendMessageToUser(listUserId.ElementAt(i), new ConversationReply { Author = curUser.Login, Body = reply.Body, ConversationId = reply.ConversationId, SendingTime = DateTimeOffset.UtcNow, Status = ConversationReplyStatus.Received, Id = dbReplies[i].Id }); } }