public async Task<MalMessageModel> GetMessageDetails(MalMessageModel msg) { try { var client = await MalHttpContextProvider.GetHttpContextAsync(); var response = await client.GetAsync($"/mymessages.php?go=read&id={msg.Id}"); var raw = await response.Content.ReadAsStringAsync(); var doc = new HtmlDocument(); doc.LoadHtml(raw); var msgNode = doc.FirstOfDescendantsWithClass("td", "dialog-text"); var msgContent = msgNode.ChildNodes.Skip(3) .Where(node => node.NodeType == HtmlNodeType.Text) .Aggregate("", (current, textNode) => current + textNode.InnerText); msg.Content = WebUtility.HtmlDecode(msgContent.Trim()); var ids = doc.FirstOfDescendantsWithClass("input", "inputButton btn-middle flat").Attributes["onclick"].Value .Split('='); msg.ThreadId = ids[4].Substring(0, ids[3].IndexOf('&')); msg.ReplyId = ids[3].Substring(0, ids[3].IndexOf('&')); return msg; } catch (WebException) { MalHttpContextProvider.ErrorMessage("Messages"); } return new MalMessageModel(); }
public MalNotification(MalMessageModel malMessageModel) { IsRead = false; Type = MalNotificationsTypes.Messages; Header = $"{malMessageModel.Sender} sent you a message!"; Content = malMessageModel.Content; Id = malMessageModel.Id; IsSupported = true; LaunchArgs = $"https://myanimelist.net/mymessages.php?go=read&id={malMessageModel.Id}|{JsonConvert.SerializeObject(malMessageModel)}"; Date = malMessageModel.Date; }
public MessageEntry(MalMessageModel msg) { Msg = msg; if (Msg.Sender.Equals(Credentials.UserName, StringComparison.CurrentCultureIgnoreCase)) { HorizontalAlignment = HorizontalAlignment.Right; Margin = new Thickness(20, 0, 0, 0); CornerRadius = new CornerRadius(10, 10, 0, 10); Background = Application.Current.Resources["SystemControlHighlightAltListAccentLowBrush"] as Brush; } else { HorizontalAlignment = HorizontalAlignment.Left; Margin = new Thickness(0, 0, 20, 0); CornerRadius = new CornerRadius(10, 10, 10, 0); Background = Application.Current.Resources["SystemControlHighlightListAccentLowBrush"] as Brush; Background.Opacity = .5; } }
public async Task<List<MalMessageModel>> GetMessagesInThread(MalMessageModel msg) { try { var client = await MalHttpContextProvider.GetHttpContextAsync(); var response = await client.GetAsync($"/mymessages.php?go=read&id={msg.Id}&threadid={msg.ThreadId}"); var raw = await response.Content.ReadAsStringAsync(); var doc = new HtmlDocument(); doc.LoadHtml(raw); var output = new List<MalMessageModel>(); foreach ( var msgHistoryNode in doc.FirstOfDescendantsWithClass("table", "pmessage-message-history").Descendants("tr")) { var current = new MalMessageModel(); var tds = msgHistoryNode.Descendants("td").ToList(); current.Content = WebUtility.HtmlDecode(tds[2].InnerText.Trim()); if (string.IsNullOrEmpty(current.Content)) continue; current.Subject = msg.Subject; current.Date = tds[0].InnerText.Trim(); current.Sender = tds[1].InnerText.Trim(); output.Add(current); } return output; } catch (Exception) { MalHttpContextProvider.ErrorMessage("Messages"); } return new List<MalMessageModel>(); }
public static async Task<List<MalMessageModel>> GetComToComMessages(string path) { try { var client = await MalHttpContextProvider.GetHttpContextAsync(); var response = await client.GetAsync($"/comtocom.php?{path}"); var doc = new HtmlDocument(); doc.LoadHtml(await response.Content.ReadAsStringAsync()); var output = new List<MalMessageModel>(); foreach (var commentBox in doc.FirstOfDescendantsWithId("div","content").ChildNodes.Where(node => node.Name == "div").Skip(1)) { try { var current = new MalMessageModel(); current.Content = WebUtility.HtmlDecode(commentBox.FirstOfDescendantsWithClass("div", "spaceit").InnerText).Trim(); current.Date = commentBox.Descendants("small").First().InnerText.Trim(new[] { '|', ' ' }); current.Sender = WebUtility.HtmlDecode(commentBox.Descendants("a").Skip(1).First().InnerText.Trim()); output.Add(current); } catch (Exception) { //html } } output.Reverse(); return output; } catch (WebException) { MalHttpContextProvider.ErrorMessage("Messages"); return new List<MalMessageModel>(); } }
public async void Init(MalMessageDetailsNavArgs args,bool force = false) { if (args.WorkMode == MessageDetailsWorkMode.Message) { var arg = args.Arg as MalMessageModel; if (arg == null) //compose new { _newMessage = true; MessageSet.Clear(); NewMessageFieldsVisibility = true; return; } NewMessageFieldsVisibility = false; _newMessage = false; if (!force &&_prevMsg?.Id == arg.Id) return; _prevMsg = arg; MessageSet.Clear(); LoadingVisibility = true; if (MessageThreads.ContainsKey(arg.ThreadId)) { MessageSet.AddRange(MessageThreads[arg.ThreadId]); } else { var msgs = await new MalMessageDetailsQuery().GetMessagesInThread(arg); msgs.Reverse(); MessageSet.AddRange(msgs); } } else { NewMessageFieldsVisibility = false; var arg = args.Arg as MalComment; if(!force && arg.ComToCom == (_prevArgs?.Arg as MalComment)?.ComToCom) return; _prevMsg = null; LoadingVisibility = true; MessageSet.Clear(); MessageSet = new SmartObservableCollection<MalMessageModel>( (await ProfileCommentQueries.GetComToComMessages(arg.ComToCom))); RaisePropertyChanged(() => MessageSet); } _prevArgs = args; LoadingVisibility = false; }
//private async void FetchHistory() //{ // LoadingVisibility = true; // FetchHistoryVisibility = false; // MessageSet.Clear(); // var result = await new MalMessageDetailsQuery().GetMessagesInThread(_prevMsg); // result.Reverse(); //newest first // _messageThreads[_prevMsg.ThreadId] = result; // MessageSet.AddRange(result.Select(model => new MessageEntry(model))); // LoadingVisibility = false; //} private async void SendMessage() { IsSendButtonEnabled = false; if (!string.IsNullOrEmpty(MessageText)) { if (_prevArgs == null || _prevArgs.WorkMode == MessageDetailsWorkMode.Message) { if (_newMessage) { if (MessageSubject == null || MessageTarget == null) { IsSendButtonEnabled = true; return; } if (await new SendMessageQuery().SendMessage(MessageSubject, MessageText, MessageTarget)) { try { var message = new MalMessageModel(); var id = await new MalMessagesQuery().GetFirstSentMessageId(); message.Id = id; message = await new MalMessageDetailsQuery().GetMessageDetails(message); message.Target = MessageTarget; message.Sender = Credentials.UserName; message.IsRead = true; message.Date = DateTime.Now.ToString("d"); message.Subject = MessageSubject; MessageThreads[message.ThreadId] = new List<MalMessageModel> {message}; _prevMsg = message; _newMessage = false; NewMessageFieldsVisibility = false; ViewModelLocator.GeneralMain.CurrentOffStatus = $"{message.Sender} - {message.Subject}"; MessageSet.Clear(); MessageSet.AddRange(new[] { message }); MessageText = ""; MessageSubject = ""; MessageTarget = ""; } catch (Exception) { ResourceLocator.MessageDialogProvider.ShowMessageDialog("Unable to send this message.", "Error"); } } else { ResourceLocator.MessageDialogProvider.ShowMessageDialog("Unable to send this message.", "Error"); } IsSendButtonEnabled = true; return; } if ( await new SendMessageQuery().SendMessage(_prevMsg.Subject, MessageText, _prevMsg.Target, _prevMsg.ThreadId, _prevMsg.ReplyId)) { var message = new MalMessageModel { Subject = _prevMsg.Subject, Content = MessageText, Date = DateTime.Now.ToString("d"), Id = "0", Sender = Credentials.UserName, Target = _prevMsg.Target, ThreadId = _prevMsg.ThreadId, ReplyId = _prevMsg.ReplyId }; if (MessageThreads.ContainsKey(_prevMsg.ThreadId)) { MessageThreads[_prevMsg.ThreadId].Insert(0, message); } else { MessageThreads[_prevMsg.ThreadId] = new List<MalMessageModel> {_prevMsg, message}; } MessageSet.AddRange(new[] { message }); MessageText = ""; MessageSubject = ""; MessageTarget = ""; RaisePropertyChanged(() => MessageText); } else { ResourceLocator.MessageDialogProvider.ShowMessageDialog("Unable to send this message.", "Error"); } } else //comment { var comment = _prevArgs.Arg as MalComment; if (await ProfileCommentQueries.SendCommentReply(comment.ComToCom.Split('=').Last(), MessageText)) { MessageSet.Add( new MalMessageModel { Content = MessageText, Sender = Credentials.UserName, Date = DateTime.Now.ToString("d") } ); MessageText = ""; RaisePropertyChanged(()=> MessageSet); RaisePropertyChanged(() => MessageText); } } } IsSendButtonEnabled = true; }
private MalMessageModel ParseInboxHtmlToMalMessage(HtmlNode msgNode, bool read) { var current = new MalMessageModel(); current.Sender = msgNode.FirstOfDescendantsWithClass("div", "mym mym_user").InnerText.Trim(); current.Target = Credentials.UserName; var contentNode = msgNode.FirstOfDescendantsWithClass("div", "mym mym_subject"); current.Subject = WebUtility.HtmlDecode(contentNode.Descendants("a").First().ChildNodes[0].InnerText.Trim().Trim('-')); current.Content = WebUtility.HtmlDecode(contentNode.Descendants("span").First().InnerText.Trim()); current.Id = contentNode.FirstOfDescendantsWithClass("a", "subject-link").Attributes["href"].Value.Split('=') .Last(); current.Date = msgNode.FirstOfDescendantsWithClass("span", "mym_date").InnerText.Trim(); current.IsRead = read; var ids = msgNode.FirstOfDescendantsWithClass("span", "mym_actions").Descendants("a").First().Attributes["href"] .Value.Split('='); current.ThreadId = ids[3].Substring(0, ids[3].IndexOf('&')); current.ReplyId = ids[2].Substring(0, ids[3].IndexOf('&')); return current; }
private MalMessageModel ParseOutboxHtmlToMalMessage(HtmlNode msgNode) { var current = new MalMessageModel(); current.Target = msgNode.FirstOfDescendantsWithClass("div", "mym mym_user").InnerText.Trim(); current.Sender = Credentials.UserName; var contentNode = msgNode.FirstOfDescendantsWithClass("div", "mym mym_subject"); current.Subject = WebUtility.HtmlDecode(contentNode.Descendants("a").First().ChildNodes[0].InnerText.Trim().Trim('-')); current.Content = WebUtility.HtmlDecode(contentNode.Descendants("span").First().InnerText.Trim()); current.Date = msgNode.FirstOfDescendantsWithClass("span", "mym_date").InnerText.Trim(); current.IsMine = true; return current; }