Skip to content

Commit d25ae76

Browse files
authored
Merge pull request #205 from jkrejcha/feature-partial-user
Implement a PartialUser class for user listings (PR)
2 parents d3759c6 + 267ed55 commit d25ae76

File tree

9 files changed

+241
-195
lines changed

9 files changed

+241
-195
lines changed
Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,23 @@
11
using Newtonsoft.Json;
22
using Newtonsoft.Json.Linq;
3+
using RedditSharp.Things.User;
34

45
namespace RedditSharp
56
{
67
/// <summary>
78
/// Represents a moderator.
89
/// </summary>
9-
public class ModeratorUser
10+
public class ModeratorUser : RelatedUser
1011
{
11-
/// <summary>
12-
/// Create ModeratorUser from given JSON
13-
/// </summary>
14-
/// <param name="json"></param>
15-
public ModeratorUser(JToken json)
12+
public ModeratorUser(IWebAgent agent, JToken json) : base(agent, json)
1613
{
17-
Helpers.PopulateObject(json, this);
1814
}
1915

20-
/// <summary>
21-
/// Moderator username.
22-
/// </summary>
23-
[JsonProperty("name")]
24-
public string Name { get; private set; }
25-
26-
/// <summary>
27-
/// base36 Id of the moderator.
28-
/// </summary>
29-
[JsonProperty("id")]
30-
public string Id { get; private set; }
31-
3216
/// <summary>
3317
/// Permissions the moderator has in the subreddit.
3418
/// </summary>
3519
[JsonProperty("mod_permissions")]
3620
[JsonConverter(typeof (ModeratorPermissionConverter))]
3721
public ModeratorPermission Permissions { get; private set; }
38-
39-
/// <inheritdoc/>
40-
public override string ToString()
41-
{
42-
return Name;
43-
}
4422
}
4523
}

RedditSharp/Things/BannedUser.cs

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,29 @@
11
using Newtonsoft.Json;
22
using Newtonsoft.Json.Linq;
3-
using RedditSharp.Extensions;
3+
using RedditSharp.Things.User;
44
using System;
55

66
namespace RedditSharp.Things
77
{
88
/// <summary>
99
/// A user that is banned in a subreddit.
1010
/// </summary>
11-
public class BannedUser : RedditUser
11+
public class BannedUser : NotedUser
1212
{
1313
/// <inheritdoc />
1414
public BannedUser(IWebAgent agent, JToken json) : base(agent, json) {
15-
var data = json["name"] == null ? json["data"] : json;
16-
base.Name = data["name"].ValueOrDefault<string>();
17-
var id = data["id"].ValueOrDefault<string>();
18-
if (id.Contains("_"))
19-
{
20-
base.Kind = "t2";
21-
base.Id = id.Split('_')[1];
22-
base.FullName = id;
23-
}
2415
}
2516

2617
/// <summary>
2718
/// Date the user was banned.
2819
/// </summary>
29-
[JsonProperty("date")]
30-
[JsonConverter(typeof(UnixTimestampConverter))]
31-
public DateTime? BanDate { get; private set; }
20+
[Obsolete("User RelUser.Date")]
21+
public DateTime? BanDate { get => DateUTC; private set => DateUTC = value; }
3222

3323
/// <summary>
3424
/// Ban note.
3525
/// </summary>
3626
[JsonProperty("note")]
3727
public string Note { get; private set; }
38-
39-
/// <summary>
40-
/// This will always return 0 for BannedUsers
41-
/// </summary>
42-
[JsonIgnore]
43-
public new int CommentKarma => 0;
44-
45-
/// <summary>
46-
/// This will always return 0 for BannedUsers
47-
/// </summary>
48-
[JsonIgnore]
49-
public new int LinkKarma => 0;
5028
}
5129
}

RedditSharp/Things/Comment.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ private void ParseComments(JToken data, Thing sender)
164164
[JsonIgnore]
165165
public Thing Parent { get; internal set; }
166166

167-
/// <inheritdoc/>
168-
public override string Shortlink => Permalink.ToString();
167+
/// <inheritdoc/>
168+
public override string Shortlink => Permalink.ToString();
169169

170170
/// <summary>
171171
/// Reply to this comment.

RedditSharp/Things/Contributor.cs

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,23 @@
11
using Newtonsoft.Json;
22
using Newtonsoft.Json.Linq;
3-
using RedditSharp.Extensions;
3+
using RedditSharp.Things.User;
44
using System;
55

66
namespace RedditSharp.Things
77
{
88
/// <summary>
99
/// A contributor to a subreddit.
1010
/// </summary>
11-
public class Contributor : RedditUser
11+
public class Contributor : RelatedUser
1212
{
1313
/// <inheritdoc />
1414
public Contributor(IWebAgent agent, JToken json) : base(agent, json) {
15-
var data = json["name"] == null ? json["data"] : json;
16-
base.Name = data["name"].ValueOrDefault<string>();
17-
var id = data["id"].ValueOrDefault<string>();
18-
if (id.Contains("_"))
19-
{
20-
base.Id = id.Split('_')[1];
21-
base.FullName = id;
22-
}
2315
}
2416

25-
/// <summary>
26-
/// Contributor name.
27-
/// </summary>
28-
[JsonProperty("name")]
29-
public new string Name { get; private set; }
30-
3117
/// <summary>
3218
/// Date contributor was added.
3319
/// </summary>
34-
[JsonProperty("date")]
35-
[JsonConverter(typeof(UnixTimestampConverter))]
36-
public DateTime DateAdded { get; private set; }
37-
38-
/// <summary>
39-
/// This will always return 0 for Contributors
40-
/// </summary>
41-
[JsonIgnore]
42-
public new int CommentKarma => 0;
43-
44-
/// <summary>
45-
/// This will always return 0 for Contributors
46-
/// </summary>
47-
[JsonIgnore]
48-
public new int LinkKarma => 0;
20+
[Obsolete("User RelUser.Date")]
21+
public DateTime? DateAdded { get => DateUTC; private set => DateUTC = value; }
4922
}
5023
}

RedditSharp/Things/RedditUser.cs

Lines changed: 8 additions & 110 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)
@@ -30,10 +31,13 @@ public RedditUser(IWebAgent agent, JToken json) : base(agent, json)
3031
internal override JToken GetJsonData(JToken json) => json["name"] == null ? json["data"] : json;
3132

3233
/// <summary>
33-
/// Reddit username.
34+
/// This method returns itself as the full <see cref="RedditUser"/> is
35+
/// already initalized so no more further initialization needs to
36+
/// be done.
3437
/// </summary>
35-
[JsonProperty("name")]
36-
public string Name { get; internal set; }
38+
/// <returns>This same <see cref="RedditUser"/>.</returns>
39+
/// <seealso cref="PartialUser.GetFullUserAsync"/>
40+
public override Task<RedditUser> GetFullUserAsync() => Task.FromResult(this);
3741

3842
/// <summary>
3943
/// Returns true if the user has reddit gold.
@@ -106,43 +110,8 @@ public RedditUser(IWebAgent agent, JToken json) : base(agent, json)
106110
[JsonProperty("pref_show_snoovatar")]
107111
public bool ShowSnoovatar { get; private set; }
108112

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

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-
146115
static string QueryString(Sort sort, int limit, FromTime time) =>
147116
$"?sort={sort.ToString("g")}&limit={limit}&t={time.ToString("g")}";
148117

@@ -151,77 +120,6 @@ static void CheckRange(int limit, int max_limit)
151120
if ((limit < 1) || (limit > max_limit))
152121
throw new ArgumentOutOfRangeException(nameof(limit), $"Valid range: [1, {max_limit}]");
153122
}
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-
}
225123

226124
/// <inheritdoc/>
227125
public override string ToString() => Name;

RedditSharp/Things/Subreddit.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ public static async Task<IEnumerable<ModeratorUser>> GetModeratorsAsync(IWebAgen
950950
var mods = data["children"].ToArray();
951951
var result = new ModeratorUser[mods.Length];
952952
for(var i = 0; i < mods.Length; i++) {
953-
var mod = new ModeratorUser(mods[i]);
953+
var mod = new ModeratorUser(agent, mods[i]);
954954
result[i] = mod;
955955
}
956956
return result;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using Newtonsoft.Json;
3+
using Newtonsoft.Json.Linq;
4+
5+
namespace RedditSharp.Things.User
6+
{
7+
public class NotedUser : RelatedUser
8+
{
9+
public NotedUser(IWebAgent agent, JToken json) : base(agent, json)
10+
{
11+
}
12+
13+
[JsonProperty("note")]
14+
public String Note { get; internal set; }
15+
}
16+
}

0 commit comments

Comments
 (0)