Example #1
0
 // update conversations
 private void OldReply(string interlocutor, ConversationReply reply)
 {
     if (SelectedFriend != null && interlocutor == SelectedFriend.Name)
     {
         RepliesList.Insert(0, new Reply(reply, client.Name));
     }
 }
Example #2
0
 public Reply(ConversationReply reply, string myLogin)
 {
     Author    = reply.Author;
     Time      = reply.Time;
     Text      = reply.Text;
     IsMyReply = reply.Author == myLogin;
 }
        public Task <AuthenticationServiceResponse> AddConversationReply(ConversationReplyModel conversationReplyModel)
        {
            try
            {
                var conversationReply = new ConversationReply();
                Mapper.Map(conversationReplyModel, conversationReply);
                conversationReply.CreatedBy = GetLoginUserId();
                //conversationReply.CreatedDate = DateTime.UtcNow;
                conversationReply.Read = false;
                _conversationService.conversationReplyRepository.Insert(conversationReply);
                _conversationService.Save();

                var conversation = _conversationService.conversationRepository.GetById(conversationReply.ConversationId);
                conversation.UpdatedDate = DateTime.UtcNow;
                conversation.UpdatedBy   = GetLoginUserId();
                _conversationService.conversationRepository.Update(conversation);
                _conversationService.Save();

                Mapper.Map(conversationReply, conversationReplyModel);
                ApplicationUser User = (new AuthenticationService()).GetUserById(conversationReply.CreatedBy);
                Mapper.Map(User, conversationReplyModel.UserModel);
                return(Task.FromResult(new AuthenticationServiceResponse {
                    Data = conversationReplyModel, Success = true
                }));
            }
            catch (Exception ex)
            {
                return(Task.FromResult(new AuthenticationServiceResponse {
                    Data = ex, Success = false, Message = "Something went wrong"
                }));
            }
        }
Example #4
0
        public async void SendMessage(String body, long conversationId)
        {
            var conv = Conversations.FirstOrDefault(x => x.Id == conversationId);

            if (conv == null)
            {
                Error.Invoke("Send message", "ConversationId not found error");
                return;
            }
            ConversationReply reply = new ConversationReply()
            {
                Author         = Author.Login,
                Body           = body,
                ConversationId = conversationId,
                SendingTime    = DateTimeOffset.Now,
                Status         = ConversationReplyStatus.Sending
            };

            conv.Messages.Add(reply);
            var res = await Task.Run(() => chat.SendMessage(body, conversationId));

            if (res.IsOk)
            {
                reply.Status = ConversationReplyStatus.Sent;
            }
            else
            {
                reply.Status = ConversationReplyStatus.SendingError;
            }
        }
Example #5
0
 public async static void SendMessageToUser(long user, ConversationReply reply)
 {
     if (OnlineUsers.TryGetValue(user, out ServiceImplementation.ChatServiceProvider provider))
     {
         await Task.Run(() => provider?.Callback.IncomingMessage(reply));
     }
 }
Example #6
0
        public async Task <MessageDTO> AddMessage(MessageDTO message)
        {
            var board = db.Boards
                        .Include(b => b.UserBoards)
                        .FirstOrDefault(b => b.Id == message.BoardId);

            if (board == null)
            {
                throw new Exception("Board is not exist!");
            }
            if (board.UserBoards.Where(ub => ub.UserId == message.UserId).Count() != 0)
            {
                throw new Exception("User do not have premission!");
            }
            var conversationReplies = new ConversationReply
            {
                BoardId = Convert.ToInt32(message.BoardId),
                UserId  = message.UserId,
                Text    = message.Text,
                Date    = message.Date.ToUniversalTime()
            };

            message.Date = message.Date.ToUniversalTime();
            db.ConversationReplies.Add(conversationReplies);
            await db.SaveChangesAsync();

            return(message);
        }
Example #7
0
        public static ConversationReplyModel ToModel(this ConversationReply conversationReply)
        {
            var currentUser = ApplicationContext.Current.CurrentUser;
            var model       = new ConversationReplyModel()
            {
                UserId           = conversationReply.UserId,
                DateCreatedUtc   = conversationReply.DateCreated,
                DateCreatedLocal =
                    DateTimeHelper.GetDateInUserTimeZone(conversationReply.DateCreated, DateTimeKind.Utc, currentUser),
                ReplyText      = conversationReply.ReplyText,
                ConversationId = conversationReply.ConversationId,
                Id             = conversationReply.Id
            };

            if (currentUser.Id == conversationReply.UserId)
            {
                var replyStatus = conversationReply.ConversationReplyStatus.FirstOrDefault(x => x.UserId != currentUser.Id);
                //it's the reply of logged in user, so we'll send if the message has been read or not by the receiver
                if (replyStatus != null)
                {
                    model.ReplyStatus            = replyStatus.ReplyStatus;
                    model.ReplyStatusLastUpdated = replyStatus.LastUpdated;
                    if (model.ReplyStatus == ReplyStatus.Deleted)
                    {
                        //we don't want to let other person know of this
                        model.ReplyStatus = ReplyStatus.Read;
                    }
                }
            }
            return(model);
        }
Example #8
0
 public async static void SendMessageToGroup(IEnumerable <long> users, ConversationReply reply)
 {
     foreach (var item in users)
     {
         if (OnlineUsers.TryGetValue(item, out ServiceImplementation.ChatServiceProvider provider))
         {
             await Task.Run(() => provider?.Callback.IncomingMessage(reply));
         }
     }
 }
        public void IncomingMessage(ConversationReply reply)
        {
            var conv = chatCallbackModel.Conversations.FirstOrDefault(x => x.Id == reply.ConversationId);

            if (conv != null)
            {
                conv.Messages.Add(reply);
                conv.NewMessagesCount++;
                chatCallbackModel.Conversations.Remove(conv);
                chatCallbackModel.Conversations.Insert(0, conv);
            }
        }
Example #10
0
        // update conversations
        private void NewReply(string interlocutor, ConversationReply reply)
        {
            if (SelectedFriend != null && interlocutor == SelectedFriend.Name)
            {
                RepliesList.Add(new Reply(reply, client.Name));
            }
            else
            {
                Friend f = FriendsList.FirstOrDefault(x => x.Name.Equals(interlocutor));

                if (f != null)
                {
                    f.HasUnreadMessages = true;
                }
            }
        }
Example #11
0
 // Отправка сообщений
 public void Send(int conversationId, string senderId, string message)
 {
     if (!String.IsNullOrWhiteSpace(message))
     {
         ConversationReply mess = new ConversationReply
         {
             SenderId       = senderId,
             ReplyText      = message,
             Time           = DateTime.Now,
             ConversationId = conversationId
         };
         db.ConversationReplies.Add(mess);
         db.SaveChanges();
         var sender = db.Users.FirstOrDefault(e => e.Id == senderId);
         Clients.All.addMessage(senderId, mess.Time.ToString("H:mm"), sender.Name, message);
     }
 }
Example #12
0
 // POST: api/Chat
 public ConversationReply Post([FromBody] ConversationReply chat)
 {
     using (ChatContextDataContext db = new ChatContextDataContext())
     {
         var    tkn          = Token.GetToken(Request);
         int    userid       = tkn.IdUser;
         string sentfromname = (from a in db.TblUser
                                where a.Id == userid
                                select a.Name).FirstOrDefault();
         ConversationReply con = new ConversationReply();
         con.Reply = chat.Reply;
         if (chat.From_Id == userid)
         {
             con.From_Id = chat.To_Id;
         }
         else if (chat.To_Id == userid)
         {
             con.From_Id = chat.From_Id;
         }
         else
         {
             con.From_Id = chat.From_Id;
         }
         //con.From_Id = chat.From_Id;
         con.Timestamp = DateTime.Now.ToString();
         con.To_Id     = chat.To_Id;
         con.Con_Id    = chat.Con_Id;
         int    conid   = con.Con_Id;
         string message = con.Reply.ToString();
         int    nama    = con.From_Id;
         //int to = con.To_Id;
         string sentTo = (from a in db.TblUser
                          where a.Id == con.From_Id
                          select a.Name).FirstOrDefault();
         //db.ConversationReply.InsertOnSubmit(con);
         //db.SubmitChanges();
         var option = new PusherOptions();
         option.Encrypted = true;
         var pusher = new Pusher("318710", "e566743c8d2d940b3849", "6a71cf9d67948c9fd93e", option);
         var result = pusher.Trigger("my-channel_" + conid, "my-event_" + conid, new { message = message, sentTo = sentTo, type = "received", sentby = userid, setbyname = sentfromname });
         var notif  = pusher.Trigger("my-channel" + sentTo, "newmessage", new { message = "new message from " + sentfromname });
         return(con);
     }
 }
Example #13
0
        public void SendMessage(String body, long conversationId)
        {
            var conv = Conversations.FirstOrDefault(x => x.Id == conversationId);

            if (conv == null)
            {
                Error.Invoke("Send message", "ConversationId not found error");
            }
            ConversationReply reply = new ConversationReply()
            {
                Author         = Author.Login,
                Body           = body,
                ConversationId = conversationId,
                SendingTime    = DateTime.UtcNow,
                Status         = ConversationReplyStatus.Sending
            };

            conv.Messages.Add(reply);
        }
        public Task <AuthenticationServiceResponse> AddConversation(string projectId, string userId, string reply)
        {
            try
            {
                int CreatingUserId = GetLoginUserId();
                int ProjectId      = projectId.Decrypt();
                int UserId         = userId.Decrypt();

                Conversation      conversation      = new Conversation();
                ConversationReply conversationReply = new ConversationReply();
                //ConversationModel conversationModel = new ConversationModel();
                ConversationReplyModel conversationReplyModel = new ConversationReplyModel();

                // Get or Set new Conversation
                var getConversation = _conversationService.conversationRepository.Get(x => x.ProjectID == ProjectId && (x.CreatedBy == CreatingUserId || x.CreatedBy == UserId) &&
                                                                                      (x.To == UserId || x.To == CreatingUserId) && x.IsDeleted == false).FirstOrDefault();
                //if (!getConversation.Any())
                if (getConversation == null)
                {
                    conversation.ProjectID   = ProjectId;
                    conversation.CreatedBy   = CreatingUserId;
                    conversation.To          = UserId; //Specific user with tih we need to start new conversation
                    conversation.IsDeleted   = false;
                    conversation.UpdatedBy   = CreatingUserId;
                    conversation.CreatedDate = DateTime.UtcNow;
                    //conversation.UpdatedDate = DateTime.UtcNow;
                    _conversationService.conversationRepository.Insert(conversation);
                    _conversationService.Save();
                    conversation = _conversationService.conversationRepository.GetById(conversation.ConversationId);
                }
                else
                {
                    conversation             = getConversation;
                    conversation.UpdatedDate = DateTime.UtcNow;
                    conversation.UpdatedBy   = GetLoginUserId();
                    _conversationService.conversationRepository.Update(conversation);
                    _conversationService.Save();
                }

                // Insert Conversation Reply
                conversationReply.Reply          = reply;
                conversationReply.CreatedBy      = CreatingUserId;
                conversationReply.ConversationId = conversation.ConversationId;
                conversationReply.IsDeleted      = false;
                //conversationReply.CreatedDate = DateTime.UtcNow;
                _conversationService.conversationReplyRepository.Insert(conversationReply);
                _conversationService.Save();

                Mapper.Map(conversationReply, conversationReplyModel);

                //conversation.ConversationReply.Add(conversationReply);

                //Mapper.Map(conversation, conversationModel);
                ////Mapper.Map(conversa)

                //var user = _unitOfWork.applicationUserRepository.GetById(conversation.CreatedBy);
                //Mapper.Map(user, conversationModel.CreatedByUserModel);

                return(Task.FromResult(new AuthenticationServiceResponse {
                    Data = conversationReplyModel, Success = true, Message = "Message sent succesfully"
                }));
            }
            catch (Exception ex)
            {
                return(Task.FromResult(new AuthenticationServiceResponse {
                    Data = ex, Success = false, Message = "Something went Wrong"
                }));
            }
        }
Example #15
0
        public IHttpActionResult Post(int toUserId, ConversationEntityModel requestModel)
        {
            const string contextName = "conversation_post";
            var          currentUser = ApplicationContext.Current.CurrentUser;

            if (currentUser.Id == toUserId)
            {
                return(RespondFailure("Can't have conversation with one's self", contextName));
            }

            //check if we have any previous conversation between logged in user and user in question
            var conversation = GetConversationWithUser(toUserId, true);

            if (conversation == null)
            {
                //we'll need to insert a new conversation
                conversation = new Conversation()
                {
                    CreatedOn    = DateTime.UtcNow,
                    LastUpdated  = DateTime.UtcNow,
                    UserId       = currentUser.Id,
                    ReceiverType = !requestModel.Group ? typeof(User).Name : "Group",
                    ReceiverId   = toUserId
                };
                _conversationService.Insert(conversation);

                //set user ids to allow conversation
                conversation.AddUsers(new List <int>()
                {
                    currentUser.Id, toUserId
                });
            }

            //save the reply now
            var reply = new ConversationReply()
            {
                ConversationId          = conversation.Id,
                ReplyText               = requestModel.ReplyText,
                DateCreated             = DateTime.UtcNow,
                IpAddress               = WebHelper.GetClientIpAddress(),
                UserId                  = currentUser.Id,
                ConversationReplyStatus = new List <ConversationReplyStatus>()
            };

            //add each conversation user with their reply status
            var conversationUsers = conversation.GetUserIds();

            foreach (var userId in conversationUsers)
            {
                var replyStatus = userId == currentUser.Id ? ReplyStatus.Sent : ReplyStatus.Received;
                reply.ConversationReplyStatus.Add(new ConversationReplyStatus()
                {
                    UserId      = userId,
                    ReplyStatus = replyStatus,
                    LastUpdated = DateTime.UtcNow,
                    Reply       = reply
                });
            }
            _conversationReplyService.Insert(reply);

            var model = reply.ToModel();

            Hub.Clients.User(currentUser.Id.ToString()).conversationReply(model, conversation.Id, toUserId);

            //change reply status for other receivers
            model.ReplyStatus = 0;
            //notify hubs except current user
            var conversationUserIds = conversation.GetUserIds().Select(x => x.ToString()).ToList();

            conversationUserIds.Remove(currentUser.Id.ToString());
            Hub.Clients.Users(conversationUserIds).conversationReply(model, conversation.Id);

            return(RespondSuccess());
        }