Beispiel #1
0
        private async void SendMessage()
        {
            if (message.Text.Trim() != string.Empty)
            {
                var newMessage = new Message
                {
                    Time       = DateTime.UtcNow,
                    AuthorId   = MainPageViewModel.CurrentUser.Id,
                    AuthorName = MainPageViewModel.CurrentUser.UserName,
                    Content    = message.Text,
                };

                _client.Client.SendMessage(new MessageCreate
                {
                    Message = new Domain.Protos.Message
                    {
                        Time       = Timestamp.FromDateTime(newMessage.Time),
                        AuthorId   = newMessage.AuthorId.ToString(),
                        AuthorName = newMessage.AuthorName,
                        Content    = newMessage.Content,
                    },
                    ChatId = _uid,
                });
                ScrollViewer.ScrollToBottom();
            }

            message.Text = string.Empty;
        }
        public ActionResult Create(Message message)
        {
            message.Timestamp = DateTime.Now;

            if (ModelState.IsValid)
            {
                // See if a conversion already exists
                var conversation = db.Conversations.GetConversation(message.UserId);

                if (conversation == null)
                {
                    // Create new
                    var newConversation = new Conversation
                    {
                        UserIdFrom = WebSecurity.CurrentUserId,
                        UserIdTo = message.UserId,
                        Timestamp = DateTime.Now,
                        Messages = new List<Message> {message}
                    };
                    message.ConversationId = newConversation.ConversationId;
                    db.Conversations.Add(newConversation);
                }
                else
                {
                    message.UserId = WebSecurity.CurrentUserId;
                    conversation.Messages.Add(message);
                    conversation.Timestamp = DateTime.Now;
                }

                // Set this user as online
                Helpers.User.SetOnline();

                db.Messages.Add(message);
                db.SaveChanges();
            }
            return RedirectToAction("Read", routeValues: new {id = message.ConversationId});
        }