Ejemplo n.º 1
0
        public void AddMessageToLog(ChatMessage message)
        {
            if (Log == null)
            {
                Log = new ChatLog(Id);
            }

            Log.Messages.Add(message);
        }
Ejemplo n.º 2
0
 public ChatMessageViewModel(ChatMessage message)
 {
     TimeSend = message.TimeSend;
     Message = message.Message;
     SenderName = message.Sender.UserName;
 }
Ejemplo n.º 3
0
 public ActionResult Delete(ChatMessage model)
 {
     string errorText;
     TempData["Error"] = context.Delete(model, out errorText) ? null : errorText;
     return RedirectToAction("Messages", new { id = model.LogId });
 }
Ejemplo n.º 4
0
        public void Send(string messageText, string roomId)
        {
            using (var context = new MagicDbContext())
            {
                var userId = Context.User.Identity.GetUserId();
                var sender = context.Users.Find(userId);
                var message = new ChatMessage(messageText)
                {
                    Sender = sender,
                    Message = messageText
                };

                var chatRoom = context.ChatRooms.Include(r => r.Users.Select(u => u.User)).First(r => r.Id == roomId);
                foreach (var user in chatRoom.Users)
                {
                    SubscribeActiveConnections(chatRoom.Id, user.UserId);
                }

                Clients.Group(roomId).addMessage(roomId, message.TimeSend.ToString("HH:mm:ss"), message.Sender.UserName, message.Sender.ColorCode, message.Message);
                
                chatRoom.AddMessageToLog(message);
                context.InsertOrUpdate(chatRoom);

                foreach (var notification in chatRoom.Users.Where(u => u.User.Status == UserStatus.Offline).Select(u => new ChatMessageNotification
                    {
                        RecipientId = u.UserId,
                        MessageId = message.Id,
                        LogId = message.LogId
                    }))
                {
                    context.Insert(notification);
                }
            }
        }