PageRenderTime 13ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/StarryEyes/Models/Subsystems/ContributionService.cs

https://github.com/a1lic/StarryEyes
C# | 87 lines | 78 code | 9 blank | 0 comment | 3 complexity | 150fad06baed2edc3806cdc28030820a MD5 | raw file
 1using System;
 2using System.IO;
 3using System.Linq;
 4using System.Net.Http;
 5using System.Reactive.Linq;
 6using System.Threading.Tasks;
 7using System.Xml.Linq;
 8using Livet;
 9using StarryEyes.Settings;
10
11namespace StarryEyes.Models.Subsystems
12{
13    internal static class ContributionService
14    {
15        private static readonly ObservableSynchronizedCollectionEx<Contributor> _contributors =
16            new ObservableSynchronizedCollectionEx<Contributor>();
17
18        internal static ObservableSynchronizedCollectionEx<Contributor> Contributors
19        {
20            get { return _contributors; }
21        }
22
23        static ContributionService()
24        {
25            Observable.Timer(TimeSpan.FromSeconds(0), TimeSpan.FromHours(8))
26                      .Subscribe(_ => Task.Run(async () => await UpdateContributors()));
27        }
28
29        private static async Task UpdateContributors()
30        {
31            try
32            {
33                var vms = await Task.Run(async () =>
34                {
35                    var hc = new HttpClient();
36                    var str = await hc.GetStringAsync(App.ContributorsUrl);
37                    using (var sr = new StringReader(str))
38                    {
39                        var xml = XDocument.Load(sr);
40                        return xml.Root
41                                  .Descendants("contributor")
42                                  .Where(
43                                      e =>
44                                      e.Attribute("visible") == null ||
45                                      e.Attribute("visible").Value.ToLower() != "false")
46                                  .Select(Contributor.FromXml)
47                                  .ToArray();
48                    }
49                });
50                Contributors.Clear();
51                vms.OrderBy(v => v.ScreenName ?? "~" + v.Name)
52                   .ForEach(Contributors.Add);
53            }
54            catch (Exception ex)
55            {
56                System.Diagnostics.Debug.WriteLine(ex);
57            }
58        }
59
60        public static bool IsContributor()
61        {
62            var users = Contributors.Select(c => c.ScreenName).ToArray();
63            return Setting.Accounts.Collection.Any(c => users.Contains(c.UnreliableScreenName));
64        }
65    }
66
67    public class Contributor
68    {
69        public static Contributor FromXml(XElement xElement)
70        {
71            var twitter = xElement.Attribute("twitter");
72            return twitter != null
73                       ? new Contributor(xElement.Value, twitter.Value)
74                       : new Contributor(xElement.Value, null);
75        }
76
77        public Contributor(string name, string screenName)
78        {
79            this.Name = name;
80            this.ScreenName = screenName;
81        }
82
83        public string Name { get; set; }
84
85        public string ScreenName { get; set; }
86    }
87}