|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using System.Net; |
| 7 | +using System.Net.WebSockets; |
| 8 | +using System.Security.Authentication; |
| 9 | +using Newtonsoft.Json; |
| 10 | +using Newtonsoft.Json.Linq; |
| 11 | + |
| 12 | +namespace RedditSharp.Things |
| 13 | +{ |
| 14 | + public class LiveUpdate : CreatedThing |
| 15 | + { |
| 16 | + private const string StrikeUpdateUrl = "/api/live/{0}/strike_update"; |
| 17 | + private const string DeleteUpdateUrl = "/api/live/{0}/delete_update"; |
| 18 | + |
| 19 | + [JsonProperty("body")] |
| 20 | + public string Body { get; set; } |
| 21 | + |
| 22 | + [JsonProperty("body_html")] |
| 23 | + public string BodyHtml { get; set; } |
| 24 | + |
| 25 | + [JsonProperty("name")] |
| 26 | + public string Name { get; set; } |
| 27 | + |
| 28 | + [JsonProperty("mobile_embeds")] |
| 29 | + public ICollection<MobileEmbed> MobileEmbeds { get; set; } |
| 30 | + |
| 31 | + [JsonProperty("author")] |
| 32 | + public string Author { get; set; } |
| 33 | + |
| 34 | + [JsonProperty("embeds")] |
| 35 | + public ICollection<Embed> Embeds { get; set; } |
| 36 | + |
| 37 | + [JsonProperty("stricken")] |
| 38 | + public bool IsStricken { get; set; } |
| 39 | + |
| 40 | + [JsonIgnore] |
| 41 | + private Reddit Reddit { get; set; } |
| 42 | + |
| 43 | + [JsonIgnore] |
| 44 | + private IWebAgent WebAgent { get; set; } |
| 45 | + |
| 46 | + public void Strike() |
| 47 | + { |
| 48 | + SimpleAction(StrikeUpdateUrl); |
| 49 | + } |
| 50 | + |
| 51 | + public void Delete() |
| 52 | + { |
| 53 | + SimpleAction(DeleteUpdateUrl); |
| 54 | + } |
| 55 | + |
| 56 | + public async Task<LiveUpdate> InitAsync(Reddit reddit, JToken post, IWebAgent webAgent) |
| 57 | + { |
| 58 | + CommonInit(reddit, post, webAgent); |
| 59 | + await JsonConvert.PopulateObjectAsync(post["data"].ToString(), this, reddit.JsonSerializerSettings); |
| 60 | + return this; |
| 61 | + } |
| 62 | + |
| 63 | + public LiveUpdate Init(Reddit reddit, JToken post, IWebAgent webAgent) |
| 64 | + { |
| 65 | + CommonInit(reddit, post, webAgent); |
| 66 | + JsonConvert.PopulateObject(post["data"].ToString(), this, reddit.JsonSerializerSettings); |
| 67 | + return this; |
| 68 | + } |
| 69 | + |
| 70 | + private void CommonInit(Reddit reddit, JToken json, IWebAgent webAgent) |
| 71 | + { |
| 72 | + base.Init(json); |
| 73 | + Reddit = reddit; |
| 74 | + WebAgent = webAgent; |
| 75 | + } |
| 76 | + |
| 77 | + public void SimpleAction(string url) |
| 78 | + { |
| 79 | + if (Reddit.User == null) |
| 80 | + throw new AuthenticationException("No user logged in."); |
| 81 | + var request = WebAgent.CreatePost(String.Format(url, Name)); |
| 82 | + var stream = request.GetRequestStream(); |
| 83 | + WebAgent.WritePostBody(stream, new |
| 84 | + { |
| 85 | + api_type = "json", |
| 86 | + id = Name, |
| 87 | + uh = Reddit.User.Modhash |
| 88 | + }); |
| 89 | + stream.Close(); |
| 90 | + var response = request.GetResponse(); |
| 91 | + var data = WebAgent.GetResponseString(response.GetResponseStream()); |
| 92 | + } |
| 93 | + |
| 94 | + public class MobileEmbed |
| 95 | + { |
| 96 | + [JsonProperty("provider_url")] |
| 97 | + public string ProviderUrl { get; set; } |
| 98 | + |
| 99 | + [JsonProperty("description")] |
| 100 | + public string Description { get; set; } |
| 101 | + |
| 102 | + [JsonProperty("original_url")] |
| 103 | + public string Original_Url { get; set; } |
| 104 | + |
| 105 | + [JsonProperty("url")] |
| 106 | + public string Url { get; set; } |
| 107 | + |
| 108 | + [JsonProperty("title")] |
| 109 | + public string Title { get; set; } |
| 110 | + |
| 111 | + [JsonProperty("thumbnail_width")] |
| 112 | + public int ThumbnailWidth { get; set; } |
| 113 | + |
| 114 | + [JsonProperty("thumbnail_height")] |
| 115 | + public int ThumbnailHeight { get; set; } |
| 116 | + |
| 117 | + [JsonProperty("thumbnail_url")] |
| 118 | + public string ThumbnailUrl { get; set; } |
| 119 | + |
| 120 | + [JsonProperty("author_name")] |
| 121 | + public string AuthorName { get; set; } |
| 122 | + |
| 123 | + [JsonProperty("version")] |
| 124 | + public string Version { get; set; } |
| 125 | + |
| 126 | + [JsonProperty("provider_name")] |
| 127 | + public string ProviderName { get; set; } |
| 128 | + |
| 129 | + [JsonProperty("type")] |
| 130 | + public string Type { get; set; } |
| 131 | + |
| 132 | + [JsonProperty("author_url")] |
| 133 | + public string AuthorUrl { get; set; } |
| 134 | + } |
| 135 | + |
| 136 | + public class Embed |
| 137 | + { |
| 138 | + [JsonProperty("url")] |
| 139 | + public string AuthorUrl { get; set; } |
| 140 | + |
| 141 | + [JsonProperty("width")] |
| 142 | + public int Width { get; set; } |
| 143 | + |
| 144 | + [JsonProperty("height")] |
| 145 | + public int Height { get; set; } |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments