using FoxTube.Core.Models; using FoxTube.Core.Models.Inbox; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Threading.Tasks; using Windows.Data.Xml.Dom; using Windows.Storage; using Windows.UI.Notifications; namespace FoxTube.Core.Helpers { public static class Inbox { static HttpClient client = new HttpClient(); static ApplicationDataContainer storage = ApplicationData.Current.RoamingSettings; public static async Task>GetInbox() { List list = new List(); list.AddRange(await GetMessages()); list.AddRange(await GetChangelogs()); list.OrderByDescending(i => i.TimeStamp); return list; } public static async void PushNew() { try { // TODO: Add backend HttpResponseMessage response = await client.GetAsync("https://xfox111.net/FoxTube/Messages?toast=true&publishedAfter=" + storage.Values["Inbox.lastCheck"] + "&lang=" + Settings.Language); if (response.StatusCode == System.Net.HttpStatusCode.NoContent) return; XmlDocument doc = new XmlDocument(); doc.LoadXml(await response.Content.ReadAsStringAsync()); foreach (IXmlNode toast in doc.LastChild.ChildNodes) ToastNotificationManager.CreateToastNotifier().Show(new ToastNotification(toast.GetXml().ToXml())); storage.Values["Inbox.lastCheck"] = DateTime.Now; } catch (Exception e) { Metrics.AddEvent("Unable to retrieve developers' messages", ("Exception", e.GetType().ToString()), ("Message", e.Message), ("App version", Metrics.CurrentVersion), ("StackTrace", e.StackTrace)); } } static async Task>GetChangelogs() { List list = new List(); try { // TODO: Add backend HttpResponseMessage response = await client.GetAsync($"https://xfox111.net/FoxTube/Changelogs?lang={Settings.Language}¤tVersion={Metrics.CurrentVersion}"); if (response.StatusCode == System.Net.HttpStatusCode.NoContent) return list; dynamic responseObj = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); foreach (var item in responseObj) list.Add(new Changelog(item.Version, item.Content, item.Description, item.TimeStamp)); } catch (Exception e) { Metrics.AddEvent("Unable to retrieve changelogs", ("Exception", e.GetType().ToString()), ("Message", e.Message), ("App version", Metrics.CurrentVersion), ("StackTrace", e.StackTrace)); } return list; } static async Task>GetMessages() { List list = new List(); try { // TODO: Add backend HttpResponseMessage response = await client.GetAsync("https://xfox111.net/FoxTube/Messages?lang=" + Settings.Language); if (response.StatusCode == System.Net.HttpStatusCode.NoContent) return list; dynamic responseObj = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); foreach (var item in responseObj) list.Add(new DeveloperMessage(item.Id, item.Title, item.Content, item.TimeStamp, item.Avatar)); } catch (Exception e) { Metrics.AddEvent("Unable to retrieve developers' messages", ("Exception", e.GetType().ToString()), ("Message", e.Message), ("App version", Metrics.CurrentVersion), ("StackTrace", e.StackTrace)); } return list; } /// /// Fires toast notification with the last changelog content /// public static async void PushChangelog() { try { // TODO: Add backend Settings.LastReviewedVersion = Metrics.CurrentVersion; HttpResponseMessage response = await client.GetAsync("https://xfox111.net/FoxTube/Changelogs?toast=true&lang=" + Settings.Language + "&version=" + Metrics.CurrentVersion); if (response.StatusCode == System.Net.HttpStatusCode.NoContent) return; ToastNotificationManager.CreateToastNotifier().Show(new ToastNotification((await response.Content.ReadAsStringAsync()).ToXml())); } catch (Exception e) { Metrics.AddEvent("Unable to retrieve changelog", ("Exception", e.GetType().ToString()), ("Message", e.Message), ("App version", Metrics.CurrentVersion), ("StackTrace", e.StackTrace)); } } } }