Ejemplo n.º 1
0
        public void AddMessageToSource(Message message, bool addUnread = true)
        {
            if (MainWindow.Instance.MainFrame.Content.Equals(Login.Instance)) return;

            if (!App.Connection.ModelController.Users.ContainsKey(message.AuthorId))
                App.Connection.ModelController.Users.Add(message.AuthorId, message.Author);
            message.Author.CopyTo(message.User);
            message.Timestamp = message.Timestamp.ToLocalTime();
            if (Messages.Count > 0)
            {
                if (message.CanMerge(Messages.Last()))
                {
                    Messages[Messages.Count - 1].Content += "\n" + message.Content;
                    Messages[Messages.Count - 1].Timestamp = message.Timestamp;
                    if (addUnread) AddUnread();
                }
                else Messages.Add(message);
            }else Messages.Add(message);

            try {
                MainWindow.Instance.Title = String.Format("{0} ({1} Unread Messages)", ((Page)Main.Instance.Frame.Content).Title, App.Connection.MessageController.TotalUnreadMessages);
            }
            catch (Exception ex){
                #if DEBUG
                Console.WriteLine(ex.Message);
                #endif
                MainWindow.Instance.Title = String.Format("Cloudsdale ({0} Unread Messages)", App.Connection.MessageController.TotalUnreadMessages);
            }
            if (Messages.Count > 50) Messages.RemoveAt(0);
        }
Ejemplo n.º 2
0
        internal async void Send(string message)
        {
            message = message.TrimEnd().Replace("\r", "");

            var messageModel = new Message
            {
                Content = message.EscapeLiteral(),
                Device = "desktop",
                ClientId = FayeConnector.ClientId
            };

            var messageData = await JsonConvert.SerializeObjectAsync(messageModel);

            messageModel.Id = Guid.NewGuid().ToString();
            messageModel.Author = App.Connection.SessionController.CurrentSession;

            var client = new HttpClient
            {
                DefaultRequestHeaders = {
                    { "Accept", "application/json" }, 
                    { "X-Auth-Token", App.Connection.SessionController.CurrentSession.AuthToken }
                }
            };

            try
            {
                InputBox.IsEnabled = false;
                client.PostAsync(Endpoints.CloudMessages.Replace("[:id]", Cloud.Id), new JsonContent(messageData));
                InputBox.Text = "";
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                InputBox.IsEnabled = true;
                InputBox.Text = "";
                InputBox.Focus();
            }
        }