public async Task GetRALLComments() { RedditSharp.WebAgent agent = new RedditSharp.WebAgent(authFixture.AccessToken); RedditSharp.Reddit reddit = new RedditSharp.Reddit(agent, true); var comments = reddit.RSlashAll.GetComments(5); Assert.Equal(5, await comments.Count()); }
public async Task PageComments() { RedditSharp.WebAgent agent = new RedditSharp.WebAgent(authFixture.AccessToken); RedditSharp.Reddit reddit = new RedditSharp.Reddit(agent, true); var comments = await reddit.RSlashAll.GetComments().Take(55).ToList(); Assert.Equal(55, comments.Count); }
public async Task GetModerators() { RedditSharp.WebAgent agent = new RedditSharp.WebAgent(authFixture.AccessToken); RedditSharp.Reddit reddit = new RedditSharp.Reddit(agent); var sub = await reddit.GetSubredditAsync(authFixture.Config["TestSubreddit"]); var mods = await sub.GetModeratorsAsync(); Assert.NotEmpty(mods); Assert.NotEmpty(mods.Where(m => m.Permissions != RedditSharp.ModeratorPermission.None)); }
public async Task GetCommentsMore() { RedditSharp.WebAgent agent = new RedditSharp.WebAgent(authFixture.AccessToken); RedditSharp.Reddit reddit = new RedditSharp.Reddit(agent); var post = (Post)await reddit.GetThingByFullnameAsync("t3_5u37lj"); var comments = await post.GetCommentsWithMoresAsync(limit : 9); Assert.NotEmpty(comments); Assert.Equal(10, comments.Count); }
public async Task GetContributors() { RedditSharp.WebAgent agent = new RedditSharp.WebAgent(authFixture.AccessToken); RedditSharp.Reddit reddit = new RedditSharp.Reddit(agent); var sub = await reddit.GetSubredditAsync(authFixture.Config["TestSubreddit"]); var contribs = await sub.GetContributors().ToList(); Assert.NotEmpty(contribs); Assert.Contains <string>(authFixture.TestUserName.ToLower(), contribs.Select(c => c.Name.ToLower())); }
public async Task SubmitPost() { RedditSharp.WebAgent agent = new RedditSharp.WebAgent(authFixture.AccessToken); RedditSharp.Reddit reddit = new RedditSharp.Reddit(agent, true); var sub = await reddit.GetSubredditAsync(authFixture.Config["TestSubreddit"]); var post = await sub.SubmitPostAsync("ThisIsASubmittedPost", "https://github.com/CrustyJew/RedditSharp/issues/76", resubmit : true); Assert.NotNull(post); await post.DelAsync(); }
public virtual async Task RevokeRefreshTokenAsync(string token, string username) { string ClientId = Configuration["RedditClientID"]; string ClientSecret = Configuration["RedditClientSecret"]; string RediretURI = Configuration["RedditRedirectURI"]; RedditSharp.WebAgent agent = new RedditSharp.WebAgent(); RedditSharp.AuthProvider ap = new RedditSharp.AuthProvider(ClientId, ClientSecret, RediretURI, agent); await ap.RevokeTokenAsync(token, true); await agentPool.RemoveWebAgentAsync(username); //TODO revoke here instead }
public Reddit(bool useSsl) { DefaultWebAgent defaultAgent = new DefaultWebAgent(); JsonSerializerSettings = new JsonSerializerSettings { CheckAdditionalContent = false, DefaultValueHandling = DefaultValueHandling.Ignore }; DefaultWebAgent.Protocol = useSsl ? "https" : "http"; WebAgent = defaultAgent; CaptchaSolver = new ConsoleCaptchaSolver(); }
public WikiTests(AuthenticatedTestsFixture authenticatedFixture) { authFixture = authenticatedFixture; RedditSharp.WebAgent agent = new RedditSharp.WebAgent(authFixture.AccessToken); reddit = new RedditSharp.Reddit(agent); sub = reddit.GetSubredditAsync(authFixture.Config["TestSubreddit"]).Result; var names = sub.GetWiki.GetPageNamesAsync().Result; if (!names.Select(n => n.ToLower()).Contains(WIKI_PAGE_NAME)) { sub.GetWiki.EditPageAsync(WIKI_PAGE_NAME, "**test** content ***up*** in *hur*").Wait(); } }
public async Task EnumerateAllComments() { RedditSharp.WebAgent agent = new RedditSharp.WebAgent(authFixture.AccessToken); RedditSharp.Reddit reddit = new RedditSharp.Reddit(agent); var post = (Post)await reddit.GetThingByFullnameAsync("t3_5u37lj"); var comments = post.EnumerateCommentTreeAsync(5); List <Comment> commentsList = new List <Comment>(); await comments.ForEachAsync(c => commentsList.Add(c)); Assert.NotEmpty(commentsList); Assert.Equal(25, commentsList.Count); }
public async Task StreamComments() { RedditSharp.WebAgent agent = new RedditSharp.WebAgent(authFixture.AccessToken); RedditSharp.Reddit reddit = new RedditSharp.Reddit(agent, true); var count = 0; var comments = reddit.RSlashAll.GetComments().GetEnumerator(50, 100, true); while (await comments.MoveNext(CancellationToken.None)) { count++; } Assert.Equal(100, count); }
public async Task ImportToolboxNotes(List <Models.Note> notes, string subreddit) { var unames = notes.Select(n => n.AppliesToUsername).Distinct().ToArray(); var webagent = new RedditSharp.WebAgent(); webagent.UserAgent = "Snoonotes Importer"; webagent.RateLimiter.Mode = RedditSharp.RateLimitMode.None; for (int i = 0; i < unames.Length; i += 20) { Helpers.ImportStatusHelper.ImportStatuses[subreddit] = $"Checking username {i + 1} of {unames.Length}"; var curNames = unames.Skip(i).Take(20); List <Task <string> > tasks = new List <Task <string> >(); foreach (var name in curNames) { tasks.Add(GetRedditUsername(webagent, name)); } while (tasks.Count > 0) { var task = await Task.WhenAny(tasks).ConfigureAwait(false); tasks.Remove(task); string name = await task.ConfigureAwait(false); if (!string.IsNullOrWhiteSpace(name)) { notes.ForEach(n => n.AppliesToUsername = n.AppliesToUsername == name.ToLower() ? name : n.AppliesToUsername); } } } Helpers.ImportStatusHelper.ImportStatuses[subreddit] = "Saving notes to database..."; var count = await notesDAL.AddNewToolBoxNotesAsync(notes).ConfigureAwait(false); Helpers.ImportStatusHelper.ImportStatuses[subreddit] = $"Done! Imported {count} new notes"; }
public override async Task <bool> UpdateModsForSubAsync(Models.Subreddit sub, ClaimsPrincipal user) { if (!user.HasClaim("uri:snoonotes:admin", sub.SubName.ToLower())) { throw new UnauthorizedAccessException("You don't have 'Full' permissions to this subreddit!"); } if (sub.SubName.ToLower() == Configuration["CabalSubreddit"].ToLower()) { return(false); } sub = (await subDAL.GetSubreddits(new string[] { sub.SubName })).First(); if (sub == null) { throw new Exception("Unrecognized subreddit"); } string subName = sub.SubName.ToLower(); //var usersWithAccess = userManager.Users.Where(u => // u.Claims.Where(c => // c.ClaimType == ClaimTypes.Role && c.ClaimValue == sub.SubName.ToLower()).Count() > 0).ToList(); var usersWithAccess = await _userManager.GetUsersInRoleAsync(subName); //var x = userManager.Users.Where(u=>u.Claims.Select(c => c.ClaimValue).Contains("videos")).ToList(); //var y = userManager.Users.Select(u => u.Claims); var ident = await _userManager.FindByNameAsync(user.Identity.Name); ClaimsIdentity curuser = user.Identity as ClaimsIdentity; RedditSharp.IWebAgent agent; if (ident.HasConfig) { agent = await agentPool.GetOrCreateWebAgentAsync(curuser.Name, (uname, uagent, rlimit) => { return(Task.FromResult(new RedditSharp.RefreshTokenPoolEntry(uname, ident.RefreshToken, rlimit, uagent))); }); } else { agent = new RedditSharp.WebAgent(); } RedditSharp.Reddit rd = new RedditSharp.Reddit(agent); RedditSharp.Things.Subreddit subinfo; try { subinfo = await rd.GetSubredditAsync(sub.SubName); } catch { return(false); } var modsWithAccess = (await subinfo.GetModeratorsAsync()).Where(m => ((int)m.Permissions & sub.Settings.AccessMask) > 0); // get list of users to remove perms from var usersToRemove = usersWithAccess.Where(u => !modsWithAccess.Select(m => m.Name.ToLower()).Contains(u.UserName.ToLower())).ToList(); foreach (var appuser in usersToRemove) { await _userManager.RemoveFromRoleAsync(appuser, subName); //if ( appuser.Claims.Where( c => c.ClaimType == "uri:snoonotes:admin" && c.ClaimValue == subName ).Count() > 0 ) { await _userManager.RemoveClaimAsync(appuser, new Claim("uri:snoonotes:admin", subName)); //} } var usersToAdd = modsWithAccess.Where(m => ((int)m.Permissions & sub.Settings.AccessMask) > 0 && !usersWithAccess.Select(u => u.UserName.ToLower()).Contains(m.Name.ToLower())); foreach (var appuser in usersToAdd) { try { var u = await _userManager.FindByNameAsync(appuser.Name); if (u != null) { //assume it won't be adding a duplicate *holds breath* if (appuser.Permissions.HasFlag(RedditSharp.ModeratorPermission.All)) { await _userManager.AddToRoleAsync(u, subName); await _userManager.AddClaimAsync(u, new Claim("uri:snoonotes:admin:", subName)); } else if (((int)appuser.Permissions & sub.Settings.AccessMask) > 0) { await _userManager.AddToRoleAsync(u, subName); } } } catch { //TODO something, mighta caught a non registered user? } } usersWithAccess = usersWithAccess.Union(await _userManager.GetUsersForClaimAsync(new Claim("uri:snoonotes:admin", subName))).ToList(); var usersToCheckUpdates = usersWithAccess.Where(u => modsWithAccess.Select(m => m.Name.ToLower()).Contains(u.UserName.ToLower())).ToList(); foreach (var appuser in usersToCheckUpdates) { var access = modsWithAccess.Where(m => m.Name.ToLower() == appuser.UserName.ToLower()).Single().Permissions; if (access == RedditSharp.ModeratorPermission.All) { if (!appuser.Claims.Any(c => c.ClaimType == "uri:snoonotes:admin" && c.ClaimValue == subName)) { await _userManager.AddClaimAsync(appuser, new Claim("uri:snoonotes:admin", subName)); } } else { //if( appuser.Claims.Any( c => c.ClaimType == "uri:snoonotes:admin" && c.ClaimValue == subName ) ) { await _userManager.RemoveClaimAsync(appuser, new Claim("uri:snoonotes:admin", subName)); //} } } return(true); }