Skip to content

Commit 251deb0

Browse files
committed
Implement PartialUser and RelUser.
1 parent 6f26518 commit 251deb0

File tree

3 files changed

+191
-112
lines changed

3 files changed

+191
-112
lines changed

RedditSharp/Things/RedditUser.cs

Lines changed: 7 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Newtonsoft.Json;
22
using Newtonsoft.Json.Linq;
3+
using RedditSharp.Things.User;
34
using System;
45
using System.Threading.Tasks;
56

@@ -8,7 +9,7 @@ namespace RedditSharp.Things
89
/// <summary>
910
/// A reddit user.
1011
/// </summary>
11-
public class RedditUser : CreatedThing
12+
public class RedditUser : PartialUser
1213
{
1314
/// <inheritdoc />
1415
public RedditUser(IWebAgent agent, JToken json) : base(agent, json)
@@ -29,11 +30,11 @@ public RedditUser(IWebAgent agent, JToken json) : base(agent, json)
2930
/// <inheritdoc/>
3031
internal override JToken GetJsonData(JToken json) => json["name"] == null ? json["data"] : json;
3132

32-
/// <summary>
33-
/// Reddit username.
34-
/// </summary>
35-
[JsonProperty("name")]
36-
public string Name { get; internal set; }
33+
/// <summary>
34+
/// This method has no effect.
35+
/// </summary>
36+
/// <returns>This same <see cref="RedditUser"/>.</returns>
37+
public override Task<RedditUser> GetFullUserAsync() => Task.FromResult(this);
3738

3839
/// <summary>
3940
/// Returns true if the user has reddit gold.
@@ -106,43 +107,8 @@ public RedditUser(IWebAgent agent, JToken json) : base(agent, json)
106107
[JsonProperty("pref_show_snoovatar")]
107108
public bool ShowSnoovatar { get; private set; }
108109

109-
/// <summary>
110-
/// Prefix for fullname. Includes trailing underscore
111-
/// </summary>
112-
public static string KindPrefix { get { return "t2_"; } }
113-
114110
#endregion
115111

116-
/// <summary>
117-
/// Return the users overview.
118-
/// </summary>
119-
public Listing<VotableThing> GetOverview(int max = -1) => Listing<VotableThing>.Create(WebAgent, OverviewUrl, max, 100);
120-
121-
/// <summary>
122-
/// Return a <see cref="Listing{T}"/> of posts liked by the logged in user.
123-
/// </summary>
124-
public Listing<Post> GetLikedPosts(int max = -1) => Listing<Post>.Create(WebAgent, LikedUrl, max, 100);
125-
126-
/// <summary>
127-
/// Return a <see cref="Listing{T}"/> of posts disliked by the logged in user.
128-
/// </summary>
129-
public Listing<Post> GetDislikedPosts(int max = -1) => Listing<Post>.Create(WebAgent, DislikedUrl, max, 100);
130-
131-
/// <summary>
132-
/// Return a <see cref="Listing{T}"/> of comments made by the user.
133-
/// </summary>
134-
public Listing<Comment> GetComments(int max = -1) => Listing<Comment>.Create(WebAgent, CommentsUrl, max, 100);
135-
136-
/// <summary>
137-
/// Return a <see cref="Listing{T}"/> of posts made by the user.
138-
/// </summary>
139-
public Listing<Post> GetPosts(int max = -1) => Listing<Post>.Create(WebAgent, LinksUrl, max, 100);
140-
141-
/// <summary>
142-
/// Return a list of subscribed subreddits for the logged in user.
143-
/// </summary>
144-
public Listing<Subreddit> GetSubscribedSubreddits(int max = -1) => Listing<Subreddit>.Create(WebAgent, SubscribedSubredditsUrl, max, 100);
145-
146112
static string QueryString(Sort sort, int limit, FromTime time) =>
147113
$"?sort={sort.ToString("g")}&limit={limit}&t={time.ToString("g")}";
148114

@@ -151,77 +117,6 @@ static void CheckRange(int limit, int max_limit)
151117
if ((limit < 1) || (limit > max_limit))
152118
throw new ArgumentOutOfRangeException(nameof(limit), $"Valid range: [1, {max_limit}]");
153119
}
154-
/// <summary>
155-
/// Returns a <see cref="RedditUser"/> by username
156-
/// </summary>
157-
/// <param name="agent">WebAgent to perform search</param>
158-
/// <param name="username">Username of user to return</param>
159-
/// <returns></returns>
160-
public static async Task<RedditUser> GetUserAsync(IWebAgent agent, string username)
161-
{
162-
var json = await agent.Get(string.Format(UserInfoUrl, username)).ConfigureAwait(false);
163-
return new RedditUser(agent, json);
164-
}
165-
166-
/// <summary>
167-
/// Get a listing of comments and posts from the user sorted by <paramref name="sorting"/>, from time <paramref name="fromTime"/>
168-
/// and limited to <paramref name="limit"/>.
169-
/// </summary>
170-
/// <param name="sorting">How to sort the comments (hot, new, top, controversial).</param>
171-
/// <param name="limit">How many comments to fetch per request. Max is 100.</param>
172-
/// <param name="fromTime">What time frame of comments to show (hour, day, week, month, year, all).</param>
173-
/// <returns>The listing of comments requested.</returns>
174-
public Listing<VotableThing> GetOverview(Sort sorting = Sort.New, int limit = 25, FromTime fromTime = FromTime.All)
175-
{
176-
CheckRange(limit, MAX_LIMIT);
177-
string overviewUrl = OverviewUrl + QueryString(sorting, limit, fromTime);
178-
return new Listing<VotableThing>(WebAgent, overviewUrl);
179-
}
180-
181-
/// <summary>
182-
/// Get a listing of comments from the user sorted by <paramref name="sorting"/>, from time <paramref name="fromTime"/>
183-
/// and limited to <paramref name="limit"/>.
184-
/// </summary>
185-
/// <param name="sorting">How to sort the comments (hot, new, top, controversial).</param>
186-
/// <param name="limit">How many comments to fetch per request. Max is 100.</param>
187-
/// <param name="fromTime">What time frame of comments to show (hour, day, week, month, year, all).</param>
188-
/// <returns>The listing of comments requested.</returns>
189-
public Listing<Comment> GetComments(Sort sorting = Sort.New, int limit = 25, FromTime fromTime = FromTime.All)
190-
{
191-
CheckRange(limit, MAX_LIMIT);
192-
string commentsUrl = CommentsUrl + QueryString(sorting, limit, fromTime);
193-
return new Listing<Comment>(WebAgent, commentsUrl);
194-
}
195-
196-
/// <summary>
197-
/// Get a listing of posts from the user sorted by <paramref name="sorting"/>, from time <paramref name="fromTime"/>
198-
/// and limited to <paramref name="limit"/>.
199-
/// </summary>
200-
/// <param name="sorting">How to sort the posts (hot, new, top, controversial).</param>
201-
/// <param name="limit">How many posts to fetch per request. Max is 100.</param>
202-
/// <param name="fromTime">What time frame of posts to show (hour, day, week, month, year, all).</param>
203-
/// <returns>The listing of posts requested.</returns>
204-
public Listing<Post> GetPosts(Sort sorting = Sort.New, int limit = 25, FromTime fromTime = FromTime.All)
205-
{
206-
CheckRange(limit, 100);
207-
string linksUrl = LinksUrl + QueryString(sorting, limit, fromTime);
208-
return new Listing<Post>(WebAgent, linksUrl);
209-
}
210-
211-
/// <summary>
212-
/// Get a listing of comments and posts saved by the user sorted by <paramref name="sorting"/>, from time <paramref name="fromTime"/>
213-
/// and limited to <paramref name="limit"/>.
214-
/// </summary>
215-
/// <param name="sorting">How to sort the comments (hot, new, top, controversial).</param>
216-
/// <param name="limit">How many comments to fetch per request. Max is 100.</param>
217-
/// <param name="fromTime">What time frame of comments to show (hour, day, week, month, year, all).</param>
218-
/// <returns>The listing of posts and/or comments requested that the user saved.</returns>
219-
public Listing<VotableThing> GetSaved(Sort sorting = Sort.New, int limit = 25, FromTime fromTime = FromTime.All)
220-
{
221-
CheckRange(limit, 100);
222-
string savedUrl = SavedUrl + QueryString(sorting, limit, fromTime);
223-
return new Listing<VotableThing>(WebAgent, savedUrl);
224-
}
225120

226121
/// <inheritdoc/>
227122
public override string ToString() => Name;
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
3+
using System;
4+
using System.Threading.Tasks;
5+
6+
namespace RedditSharp.Things.User
7+
{
8+
/// <summary>
9+
/// A partial user
10+
/// </summary>
11+
public class PartialUser : CreatedThing
12+
{
13+
/// <inheritdoc />
14+
public PartialUser(IWebAgent agent, JToken json) : base(agent, json)
15+
{
16+
}
17+
#region Properties
18+
private string OverviewUrl => $"/user/{Name}.json";
19+
private string CommentsUrl => $"/user/{Name}/comments.json";
20+
private string LinksUrl => $"/user/{Name}/submitted.json";
21+
private const string SubscribedSubredditsUrl = "/subreddits/mine.json";
22+
private string LikedUrl => $"/user/{Name}/liked.json";
23+
private string DislikedUrl => $"/user/{Name}/disliked.json";
24+
private string SavedUrl => $"/user/{Name}/saved.json";
25+
private const string UserInfoUrl = "/user/{0}/about.json";
26+
27+
private const int MAX_LIMIT = 100;
28+
29+
/// <inheritdoc/>
30+
internal override JToken GetJsonData(JToken json) => json["name"] == null ? json["data"] : json;
31+
32+
/// <summary>
33+
/// Reddit username.
34+
/// </summary>
35+
[JsonProperty("name")]
36+
public string Name { get; internal set; }
37+
38+
/// <summary>
39+
/// Prefix for fullname. Includes trailing underscore
40+
/// </summary>
41+
public static string KindPrefix { get { return "t2_"; } }
42+
43+
#endregion
44+
45+
/// <summary>
46+
/// Gets a full <see cref="RedditUser"/> object which includes all user information.
47+
/// </summary>
48+
/// <returns></returns>
49+
public virtual async Task<RedditUser> GetFullUserAsync() => await GetUserAsync(WebAgent, Name);
50+
51+
/// <summary>
52+
/// Return the users overview.
53+
/// </summary>
54+
public Listing<VotableThing> GetOverview(int max = -1) => Listing<VotableThing>.Create(WebAgent, OverviewUrl, max, 100);
55+
56+
/// <summary>
57+
/// Return a <see cref="Listing{T}"/> of posts liked by the logged in user.
58+
/// </summary>
59+
public Listing<Post> GetLikedPosts(int max = -1) => Listing<Post>.Create(WebAgent, LikedUrl, max, 100);
60+
61+
/// <summary>
62+
/// Return a <see cref="Listing{T}"/> of posts disliked by the logged in user.
63+
/// </summary>
64+
public Listing<Post> GetDislikedPosts(int max = -1) => Listing<Post>.Create(WebAgent, DislikedUrl, max, 100);
65+
66+
/// <summary>
67+
/// Return a <see cref="Listing{T}"/> of comments made by the user.
68+
/// </summary>
69+
public Listing<Comment> GetComments(int max = -1) => Listing<Comment>.Create(WebAgent, CommentsUrl, max, 100);
70+
71+
/// <summary>
72+
/// Return a <see cref="Listing{T}"/> of posts made by the user.
73+
/// </summary>
74+
public Listing<Post> GetPosts(int max = -1) => Listing<Post>.Create(WebAgent, LinksUrl, max, 100);
75+
76+
/// <summary>
77+
/// Return a list of subscribed subreddits for the logged in user.
78+
/// </summary>
79+
public Listing<Subreddit> GetSubscribedSubreddits(int max = -1) => Listing<Subreddit>.Create(WebAgent, SubscribedSubredditsUrl, max, 100);
80+
81+
static string QueryString(Sort sort, int limit, FromTime time) =>
82+
$"?sort={sort.ToString("g")}&limit={limit}&t={time.ToString("g")}";
83+
84+
static void CheckRange(int limit, int max_limit)
85+
{
86+
if ((limit < 1) || (limit > max_limit))
87+
throw new ArgumentOutOfRangeException(nameof(limit), $"Valid range: [1, {max_limit}]");
88+
}
89+
/// <summary>
90+
/// Returns a <see cref="RedditUser"/> by username
91+
/// </summary>
92+
/// <param name="agent">WebAgent to perform search</param>
93+
/// <param name="username">Username of user to return</param>
94+
/// <returns></returns>
95+
public static async Task<RedditUser> GetUserAsync(IWebAgent agent, string username)
96+
{
97+
var json = await agent.Get(string.Format(UserInfoUrl, username)).ConfigureAwait(false);
98+
return new RedditUser(agent, json);
99+
}
100+
101+
/// <summary>
102+
/// Get a listing of comments and posts from the user sorted by <paramref name="sorting"/>, from time <paramref name="fromTime"/>
103+
/// and limited to <paramref name="limit"/>.
104+
/// </summary>
105+
/// <param name="sorting">How to sort the comments (hot, new, top, controversial).</param>
106+
/// <param name="limit">How many comments to fetch per request. Max is 100.</param>
107+
/// <param name="fromTime">What time frame of comments to show (hour, day, week, month, year, all).</param>
108+
/// <returns>The listing of comments requested.</returns>
109+
public Listing<VotableThing> GetOverview(Sort sorting = Sort.New, int limit = 25, FromTime fromTime = FromTime.All)
110+
{
111+
CheckRange(limit, MAX_LIMIT);
112+
string overviewUrl = OverviewUrl + QueryString(sorting, limit, fromTime);
113+
return new Listing<VotableThing>(WebAgent, overviewUrl);
114+
}
115+
116+
/// <summary>
117+
/// Get a listing of comments from the user sorted by <paramref name="sorting"/>, from time <paramref name="fromTime"/>
118+
/// and limited to <paramref name="limit"/>.
119+
/// </summary>
120+
/// <param name="sorting">How to sort the comments (hot, new, top, controversial).</param>
121+
/// <param name="limit">How many comments to fetch per request. Max is 100.</param>
122+
/// <param name="fromTime">What time frame of comments to show (hour, day, week, month, year, all).</param>
123+
/// <returns>The listing of comments requested.</returns>
124+
public Listing<Comment> GetComments(Sort sorting = Sort.New, int limit = 25, FromTime fromTime = FromTime.All)
125+
{
126+
CheckRange(limit, MAX_LIMIT);
127+
string commentsUrl = CommentsUrl + QueryString(sorting, limit, fromTime);
128+
return new Listing<Comment>(WebAgent, commentsUrl);
129+
}
130+
131+
/// <summary>
132+
/// Get a listing of posts from the user sorted by <paramref name="sorting"/>, from time <paramref name="fromTime"/>
133+
/// and limited to <paramref name="limit"/>.
134+
/// </summary>
135+
/// <param name="sorting">How to sort the posts (hot, new, top, controversial).</param>
136+
/// <param name="limit">How many posts to fetch per request. Max is 100.</param>
137+
/// <param name="fromTime">What time frame of posts to show (hour, day, week, month, year, all).</param>
138+
/// <returns>The listing of posts requested.</returns>
139+
public Listing<Post> GetPosts(Sort sorting = Sort.New, int limit = 25, FromTime fromTime = FromTime.All)
140+
{
141+
CheckRange(limit, 100);
142+
string linksUrl = LinksUrl + QueryString(sorting, limit, fromTime);
143+
return new Listing<Post>(WebAgent, linksUrl);
144+
}
145+
146+
/// <summary>
147+
/// Get a listing of comments and posts saved by the user sorted by <paramref name="sorting"/>, from time <paramref name="fromTime"/>
148+
/// and limited to <paramref name="limit"/>.
149+
/// </summary>
150+
/// <param name="sorting">How to sort the comments (hot, new, top, controversial).</param>
151+
/// <param name="limit">How many comments to fetch per request. Max is 100.</param>
152+
/// <param name="fromTime">What time frame of comments to show (hour, day, week, month, year, all).</param>
153+
/// <returns>The listing of posts and/or comments requested that the user saved.</returns>
154+
public Listing<VotableThing> GetSaved(Sort sorting = Sort.New, int limit = 25, FromTime fromTime = FromTime.All)
155+
{
156+
CheckRange(limit, 100);
157+
string savedUrl = SavedUrl + QueryString(sorting, limit, fromTime);
158+
return new Listing<VotableThing>(WebAgent, savedUrl);
159+
}
160+
161+
/// <inheritdoc/>
162+
public override string ToString() => Name;
163+
}
164+
}

RedditSharp/Things/User/RelUser.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using Newtonsoft.Json;
3+
using Newtonsoft.Json.Linq;
4+
5+
namespace RedditSharp.Things.User
6+
{
7+
public class RelUser : PartialUser
8+
{
9+
public RelUser(IWebAgent agent, JToken json) : base(agent, json)
10+
{
11+
}
12+
13+
/// <summary>
14+
/// UTC time of when this user was added to the list
15+
/// </summary>
16+
[JsonProperty("date")]
17+
[JsonConverter(typeof(UnixTimestampConverter))]
18+
public DateTime? DateUTC { get; set; }
19+
}
20+
}

0 commit comments

Comments
 (0)