public IHttpActionResult CreateMessage(int roomId, CreateMessageBindingModels model)
        {
            var userId = this.provider.GetId();
            var user = this.Data.Users.GetAll().FirstOrDefault(u => u.Id == userId);
            var room = this.Data.Rooms.GetAll().FirstOrDefault(r => r.Id == roomId);

            if (user == null)
            {
                return this.Unauthorized();
            }

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest("Invalid input model");
            }

            if (room == null)
            {
                return this.BadRequest("Room doesn't exist");
            }

            var message = new Message()
            {
                Text = model.Text,
                SentDate = DateTime.Now,
                Poster = user,
                Room = room
            };

            this.Data.Messages.Add(message);
            this.Data.SaveChanges();

            return this.Ok("Message successfully created");
        }
 public MessageFormViewModel(int id, Message mess)
 {
     userRep = new UserRepository();
     messageRep = new MessageRepository();
     channelRep = new ChannelRepository();
     chat = channelRep.showChatroom(id);
     user = userRep.getOwner(channelRep.showChatroom(id));
     messes = messageRep.showChatroomMessages(id).ToList();
 }
        public void PostFile(FileModel file)
        {
            DropboxServiceProvider dropboxServiceProvider =
                new DropboxServiceProvider(DropboxAppKey, DropboxAppSecret, AccessLevel.AppFolder);

            OAuthToken oauthAccessToken = new OAuthToken("9gyo6l0xq3l7kdd0", "ly7ayinrqbocfy8");

            // Login in Dropbox
            IDropbox dropbox = dropboxServiceProvider.GetApi(oauthAccessToken.Value, oauthAccessToken.Secret);

            // Create new folder
            string newFolderName = "New_Folder_" + DateTime.Now.Ticks;
            Entry createFolderEntry = dropbox.CreateFolderAsync(newFolderName).Result;

            // Upload a file
            IResource fileResource = new FileResource(file.Path + "/" + file.Name);
            Entry uploadFileEntry = dropbox.UploadFileAsync(fileResource,
                "/" + newFolderName + "/" + file.Name).Result;

            // Share a file
            DropboxLink sharedUrl = dropbox.GetMediaLinkAsync(uploadFileEntry.Path).Result;

               // Update database
            Message newMessage = new Message()
            {
                Content = sharedUrl.Url.ToString(),
                SendTime = DateTime.Now,
                User = (from users in db.Users
                        where users.UserId == file.UserId
                        select users).FirstOrDefault(),
                UserId = file.UserId,
                RecieverId = file.RecieverId
            };

            if (file.IsProfilePic)
            {
                User currentUser = (from users in db.Users
                                    where users.UserId == file.UserId
                                    select users).FirstOrDefault();
                currentUser.ProfilePicUrl = sharedUrl.Url.ToString();
            }

            db.Messages.Add(newMessage);
            db.SaveChanges();
        }
Beispiel #4
0
        protected void ButtonAddMessage_Click(object sender, EventArgs e)
        {
            var context = new ApplicationDbContext();
            string content = this.TextBoxContent.Text;
            var author = context.Users.Find(User.Identity.GetUserId());

            var newMessage = new Message()
            {
                Content = content,
                Author = author,
                CreationDate = DateTime.Now
            };

            context.Messages.Add(newMessage);
            context.SaveChanges();

            Response.Redirect("~/Messages/All");
        }
        public IHttpActionResult SendMessage(
            [FromBody]MessageBindingModels model, string channelName)
        {
            if (model == null)
            {
                return this.BadRequest("Model cannot be null (no data in request)");
            }

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            string senderName = this.User.Identity.Name;
            var channelId = this.Data.ChatRooms.FirstOrDefault(c => c.Name == channelName).Id;
            var sender = this.Data.Users.FirstOrDefault(u => u.UserName == senderName);

            var message = new Message()
            {
                SenderId = sender.Id,
                ChatRoomId = channelId,
                MessageDateTime = DateTime.Now,
                Content = model.Content

            };

            this.Data.Messages.Add(message);
            this.Data.SaveChanges();

            var data = this.Data.Messages
                .Where(p => p.Id == message.Id)
                .Select(MessageViewModels.Create)
                .FirstOrDefault();

            return this.Ok(data);
        }
		private void detach_Messages(Message entity)
		{
			this.SendPropertyChanging();
			entity.aspnet_User = null;
		}
		private void detach_Messages(Message entity)
		{
			this.SendPropertyChanging();
			entity.Chatroom1 = null;
		}
		private void attach_Messages(Message entity)
		{
			this.SendPropertyChanging();
			entity.Chatroom1 = this;
		}
Beispiel #9
0
        public IHttpActionResult PostMessage(Message message)
        {
            if (ModelState.IsValid)
            {
                message.PostOn = DateTime.Now;
                message.UserId = this.UserProvider.GetUserId();                
                
                this.Data.Messages.Add(message);
                this.Data.SaveChanges();
            }

            var notification = PubNubNotificationManager.Instance;
            notification.PublishMessage("global", "new message");

            var messageData = new MessageDataModel()
            {
                Body = message.Body,
                PostOn = message.PostOn
            };

            return this.Ok(messageData);
        }
Beispiel #10
0
 /// <summary>
 /// Legger et nytt meldingsobjekt i databasen
 /// </summary>
 /// <param name="newMessage">Meldingsobjekt</param>
 public void addMessage(Message newMessage)
 {
     db.Messages.InsertOnSubmit(newMessage);
     db.SubmitChanges();
 }
Beispiel #11
0
        public ActionResult Submit(FormCollection collection)
        {
            try
            {
                Message mess = new Message();
                Chatroom chat = new Chatroom();

                //Logikk for å lagre en beskjed i databasen
                mess.Date = System.DateTime.Now;
                mess.AuthorId = userRep.getUserId(User.Identity.Name);
                UpdateModel(chat, "chat");
                UpdateModel(mess, "mess");
                mess.Chatroom = chat.Id;
                if (mess.Message1 == null || mess.Message1.Length > 200) //Skjekker om beskjedeb er tom
                {
                    ModelState.AddModelError("mess.Message1", "Må være mellom 1 og 200 tegn");
                    return View("Room", new MessageFormViewModel(mess.Chatroom, mess));
                }
                messageRep.addMessage(mess);

                return RedirectToAction("../Channel/Room/" + chat.Name);
            }
            catch
            {
                return View();
            }
        }
Beispiel #12
0
        private void AddMessage(int id, string user, string msgText)
        {
            ChatRoom room = roomRepository.GetChatRoom(id);

            Message msg = new Message();

            msg.MessageOwner = user;
            msg.MessageTimeStamp = DateTime.Now;
            msg.MessageText = msgText;

            room.Messages.Add(msg);
            roomRepository.Save();
        }
        public IHttpActionResult PostMessages(string friendId, [FromBody] MessageBindingModel message)
        {
            var userId = User.Identity.GetUserId();

            if (friendId == userId)
                return BadRequest("You canot send messages to yourself!");

            if (!Data.Users.Any(u => u.Id == friendId))
            {
                return BadRequest("You cannot sent message to an unexisting user!");
            }

            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            var newMessage = new Message
            {
                ContentString = message.ContentString,
                ContentType = "text",
                SentOn = DateTime.Now,
                SenderId = userId,
                ReceiverId = friendId
            };

            Data.Messages.Add(newMessage);

            if (!Data.Notifications.Any(n => n.SenderId == userId && n.ReceiverId == friendId))
            {
                Data.Notifications.Add(new Notification
                {
                    SenderId = userId,
                    ReceiverId = friendId,
                    UnseenMessages = 0
                });

                Data.SaveChanges();
            }

            var notification = Data.Notifications
                .First(n => n.SenderId == userId && n.ReceiverId == friendId);

            notification.UnseenMessages++;

            Data.SaveChanges();

            return Ok(newMessage);
        }
        public IHttpActionResult SendMessage(
            [FromBody]MessageBindingModels model)
        {
            if (model == null)
            {
                return this.BadRequest("Model cannot be null (no data in request)");
            }

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var reciever = this.Data.Users.FirstOrDefault(u => u.UserName == model.Reciever);

            if (reciever == null)
            {
                return this.BadRequest(string.Format(
                    "User {0} does not exist",
                    model.Reciever));
            }

            string senderName = this.User.Identity.Name;

            var sender = this.Data.Users.FirstOrDefault(u => u.UserName == senderName);

            var message = new Message()
            {
                SenderId = sender.Id,
                ReceiverId = reciever.Id,
                MessageDateTime = DateTime.Now,
                Content = model.Content

            };

            this.Data.Messages.Add(message);
            this.Data.SaveChanges();

            var data = this.Data.Messages
                .Where(p => p.Id == message.Id)
                .Select(MessageViewModels.Create)
                .FirstOrDefault();

            return this.Ok(data);
        }
 partial void InsertMessage(Message instance);
        protected void ButtonSendMessage_Click(object sender, EventArgs e)
        {
            using (var context = new ApplicationDbContext())
            {
                string contents = (this.GridViewMessages.FooterRow.FindControl("TextBoxNewContents") as TextBox).Text;

                if (string.IsNullOrWhiteSpace(contents))
                {
                    LabelErrorMessage.Text = "Contents cannot be empty";
                    return;
                }

                var user = context.Users.Find(User.Identity.GetUserId());
                if (user == null)
                {
                    LabelErrorMessage.Text = "You must login to send a message.";
                    return;
                }

                var message = new Message
                {
                    Author = user,
                    Contents = contents.Trim(),
                    Timestamp = DateTime.Now
                };

                context.Messages.Add(message);
                context.SaveChanges();
                DataBind();
                Response.Redirect(Request.RawUrl);
            }
        }
 partial void UpdateMessage(Message instance);
Beispiel #18
0
 /// <summary>
 /// Metoden oppdaterer databasen etter hvile objekt som kommer inn.
 /// </summary>
 /// <param name="message">meldingsobjekt</param>
 public void updateMessage(Message message)
 {
     db.SubmitChanges();
 }
 partial void DeleteMessage(Message instance);
Beispiel #20
0
 /// <summary>
 /// Sletter en melding fra databasen
 /// </summary>
 /// <param name="message">Meldingsobjekt</param>
 public void deleteMessage(Message message)
 {
     db.Messages.DeleteOnSubmit(message);
     db.SubmitChanges();
 }
Beispiel #21
0
        public ActionResult Submit(FormCollection collection)
        {
            try
            {
                Message mess = new Message();
                Chatroom chat = new Chatroom();
                Image image = new Image();
                HttpPostedFileBase file;

                //Logikk for å lagre en beskjed i databasen
                mess.Date = System.DateTime.Now;
                mess.AuthorId = userRep.getUserId(User.Identity.Name);
                UpdateModel(chat, "chat");
                UpdateModel(mess, "mess");
                mess.Chatroom = chat.Id;
                if (mess.Message1 != null)
                {
                    if (mess.Message1.Length > 200) //Skjekker om beskjedeb er tom
                    {
                        ModelState.AddModelError("mess.Message1", "Må være under 200 tegn");
                        return View("Room", new MessageFormViewModel(mess.Chatroom, mess));
                    }
                }

                file = Request.Files[0];
                //Logikk for å få bilde inn i databasen
                if (file.ContentLength > 0)
                {
                    image.imageName = file.FileName;
                    image.imageSize = file.ContentLength;
                    image.imageType = file.ContentType;
                    byte[] theImage = new byte[file.ContentLength];
                    file.InputStream.Read(theImage, 0, (int)file.ContentLength);
                    image.image1 = theImage;
                    mess.imageId = messageRep.addImage(image);

                }

                messageRep.addMessage(mess);

                return RedirectToAction("../Channel/Room/"+chat.Id);
            }
            catch
            {
                return View("../Channel/");
            }
        }