using System; using System.Threading.Tasks; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace RedditSharp.Things { public class RedditUser : Thing { private const string OverviewUrl = "/user/{0}.json"; private const string CommentsUrl = "/user/{0}/comments.json"; private const string LinksUrl = "/user/{0}/submitted.json"; private const string SubscribedSubredditsUrl = "/subreddits/mine.json"; private const string LikedUrl = "/user/{0}/liked.json"; private const string DislikedUrl = "/user/{0}/disliked.json"; private const int MAX_LIMIT = 100; public RedditUser Init(Reddit reddit, JToken json, IWebAgent webAgent) { CommonInit(reddit, json, webAgent); JsonConvert.PopulateObject(json["name"] == null ? json["data"].ToString() : json.ToString(), this, reddit.JsonSerializerSettings); return this; } public async Task InitAsync(Reddit reddit, JToken json, IWebAgent webAgent) { CommonInit(reddit, json, webAgent); await Task.Factory.StartNew(() => JsonConvert.PopulateObject(json["name"] == null ? json["data"].ToString() : json.ToString(), this, reddit.JsonSerializerSettings)); return this; } private void CommonInit(Reddit reddit, JToken json, IWebAgent webAgent) { base.Init(json); Reddit = reddit; WebAgent = webAgent; } [JsonIgnore] protected Reddit Reddit { get; set; } [JsonIgnore] protected IWebAgent WebAgent { get; set; } [JsonProperty("name")] public string Name { get; set; } [JsonProperty("is_gold")] public bool HasGold { get; set; } [JsonProperty("is_mod")] public bool IsModerator { get; set; } [JsonProperty("link_karma")] public int LinkKarma { get; set; } [JsonProperty("comment_karma")] public int CommentKarma { get; set; } [JsonProperty("created")] [JsonConverter(typeof(UnixTimestampConverter))] public DateTime Created { get; set; } public Listing Overview { get { return new Listing(Reddit, string.Format(OverviewUrl, Name), WebAgent); } } public Listing LikedPosts { get { return new Listing(Reddit, string.Format(LikedUrl, Name), WebAgent); } } public Listing DislikedPosts { get { return new Listing(Reddit, string.Format(DislikedUrl, Name), WebAgent); } } public Listing Comments { get { return new Listing(Reddit, string.Format(CommentsUrl, Name), WebAgent); } } public Listing Posts { get { return new Listing(Reddit, string.Format(LinksUrl, Name), WebAgent); } } public Listing SubscribedSubreddits { get { return new Listing(Reddit, SubscribedSubredditsUrl, WebAgent); } } /// /// Get a listing of comments from the user sorted by , from time /// and limited to . /// /// How to sort the comments (hot, new, top, controversial). /// How many comments to fetch per request. Max is 100. /// What time frame of comments to show (hour, day, week, month, year, all). /// The listing of comments requested. public Listing GetComments(Sort sorting = Sort.New, int limit = 25, FromTime fromTime = FromTime.All) { if ((limit < 1) || (limit > MAX_LIMIT)) throw new ArgumentOutOfRangeException("limit", "Valid range: [1," + MAX_LIMIT + "]"); string commentsUrl = string.Format(CommentsUrl, Name); commentsUrl += string.Format("?sort={0}&limit={1}&t={2}", Enum.GetName(typeof(Sort), sorting), limit, Enum.GetName(typeof(FromTime), fromTime)); return new Listing(Reddit, commentsUrl, WebAgent); } /// /// Get a listing of posts from the user sorted by , from time /// and limited to . /// /// How to sort the posts (hot, new, top, controversial). /// How many posts to fetch per request. Max is 100. /// What time frame of posts to show (hour, day, week, month, year, all). /// The listing of posts requested. public Listing GetPosts(Sort sorting = Sort.New, int limit = 25, FromTime fromTime = FromTime.All) { if ((limit < 1) || (limit > 100)) throw new ArgumentOutOfRangeException("limit", "Valid range: [1,100]"); string linksUrl = string.Format(LinksUrl, Name); linksUrl += string.Format("?sort={0}&limit={1}&t={2}", Enum.GetName(typeof(Sort), sorting), limit, Enum.GetName(typeof(FromTime), fromTime)); return new Listing(Reddit, linksUrl, WebAgent); } public override string ToString() { return Name; } #region Obsolete Getter Methods [Obsolete("Use Overview property instead")] public Listing GetOverview() { return Overview; } [Obsolete("Use Comments property instead")] public Listing GetComments() { return Comments; } [Obsolete("Use Posts property instead")] public Listing GetPosts() { return Posts; } [Obsolete("Use SubscribedSubreddits property instead")] public Listing GetSubscribedSubreddits() { return SubscribedSubreddits; } #endregion Obsolete Getter Methods } public enum Sort { New, Hot, Top, Controversial } public enum FromTime { All, Year, Month, Week, Day, Hour } }