Exemplo n.º 1
0
 public ActionResult AddNewMessage(MessageModel model)
 {
     if (model.MessageText != null)
         if (!string.IsNullOrWhiteSpace(model.MessageText.Trim()))
         {
             var user = CurrentUser;
             if (user != null)
             {
                 var chat = MongoDBConnector.Chats.Find(Builders<Chat>.Filter.Eq(x => x.name, model.FromChat)).ToListAsync().Result;
                 if (chat.First().AllowedUser.Contains(user.UserName) || user.IsAdmin)
                 {
                     model.IsSystem = false;
                     model.Sender = user.UserName;
                     model.Time = DateTime.UtcNow;
                     MongoDBConnector.Messages.InsertOneAsync(model);
                     MongoDBConnector.SetUserLastActionTime(model.Sender, model.Time);
                     Response.StatusCode = 200;
                     return Json(true);
                 }
             }
         }
     Response.StatusCode = 400;
     return Json(false);
 }
Exemplo n.º 2
0
 public ActionResult Chat(string name)
 {
     var chat = MongoDBConnector.Chats.Find(Builders<Chat>.Filter.Eq(x => x.name, name)).ToListAsync().Result;
     var user = CurrentUser;
     if (chat.Count == 0)
     {
         return RedirectToAction("ErrorPage", "Home", ErrorModel.ChatDoesntExist );
     }
     else
     {
         if (user != null)
         {
             MongoDBConnector.SetUserLastActionTime(user.UserName, DateTime.UtcNow);
             if (chat.First().AllowedUser.Contains(user.UserName) || user.IsAdmin)
             {
                 MongoDBConnector.SetUserLastActionTime(user.UserName, DateTime.UtcNow);
                 var sysMessage = new MessageModel
                 {
                     IsSystem = true,
                     MessageText = string.Format("{0} вошел в чат", user.UserName),
                     Sender = user.UserName,
                     Time = DateTime.UtcNow,
                     FromChat = name
                 };
                 MongoDBConnector.Messages.InsertOneAsync(sysMessage);
                 return View(new Tuple<ChatUserModel, Chat>(user, chat.First()));
             }
         }
         return RedirectToAction("ErrorPage", "Home", ErrorModel.AccessDenied );
     }
 }