public ActionResult MessagingPage(int id = 0)
        {
            int ownID = (int)Session["userID"];

            if (id != 0 && id != ownID)
            {
                if (_messageMapService.GetMap(ownID, id) == null)
                {
                    _messageMapService.Add(new MessageMap {
                        FromAccountID = ownID, LastMessageDate = DateTime.Now, ToAccountID = id
                    });
                    MessageMap targetMessageMap = _messageMapService.GetMap(id, ownID);

                    if (targetMessageMap == null)
                    {
                        _messageMapService.Add(new MessageMap {
                            FromAccountID = id, LastMessageDate = DateTime.Now, ToAccountID = ownID
                        });
                    }
                }
            }

            List <MessagePageData> messageMaps = _messageMapService.GetMessages(ownID);

            int[] indexOfDeletedMaps = new int[0];

            for (int i = 0; i < messageMaps.Count; i++)
            {
                int otherAccountID = messageMaps[i].AccountID;
                if (messageMaps.Where(x => x.AccountID == otherAccountID).ToList().Count > 1 && messageMaps[i].FromAccountID == ownID)
                {
                    Array.Resize(ref indexOfDeletedMaps, indexOfDeletedMaps.Length + 1);
                    indexOfDeletedMaps[indexOfDeletedMaps.Length - 1] = i;
                }
            }

            Array.Sort(indexOfDeletedMaps);
            for (int i = indexOfDeletedMaps.Length - 1; i > -1; i--)
            {
                messageMaps.Remove(messageMaps[indexOfDeletedMaps[i]]);
            }

            return(View(new MessagePageViewModel {
                AccountsOfMessages = messageMaps.OrderByDescending(x => x.LastMessageDate).ToList(), MainAccountID = (int)Session["userID"]
            }));
        }
Beispiel #2
0
        public void SendMessage(string message, int targetID, int ownID)
        {
            MessageMap messageMap = _messageMapService.GetMap(ownID, targetID);

            messageMap.LastMessageDate = DateTime.Now;
            _messageMapService.Update(messageMap);

            MessageMap targetMessageMap = _messageMapService.GetMap(targetID, ownID);

            targetMessageMap.LastMessageDate = DateTime.Now;
            _messageMapService.Update(targetMessageMap);

            _messageService.Add(new Message
            {
                IsActive       = true,
                IsRead         = false,
                MessageContent = message,
                MessageMapID   = messageMap.MessageMapID,
                SendDate       = DateTime.Now
            });

            Clients.All.addMessage(message, targetID, ownID);
        }