Skip to content

Commit

Permalink
Implemented Blocking/Unblocking for Twitter API v2
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeMayo committed Apr 8, 2021
1 parent 08fc64e commit a06dfe1
Showing 1 changed file with 46 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using System.Threading.Tasks;
using LinqToTwitter;
using LinqToTwitter.Common;

namespace ConsoleDemo.CSharp
{
Expand Down Expand Up @@ -29,11 +30,11 @@ internal static async Task RunAsync(TwitterContext twitterCtx)
break;
case '2':
Console.WriteLine("\n\tBlocking user...\n");
await CreateBlockAsync(twitterCtx);
await BlockUserAsync(twitterCtx);
break;
case '3':
Console.WriteLine("\n\tUnblocking user...\n");
await DestroyBlockAsync(twitterCtx);
await UnblockUserAsync(twitterCtx);
break;
case 'q':
case 'Q':
Expand Down Expand Up @@ -86,26 +87,60 @@ static async Task ListBlockIDsAsyc(TwitterContext twitterCtx)
result.IDs.ForEach(block => Console.WriteLine("ID: {0}", block));
}

static async Task CreateBlockAsync(TwitterContext twitterCtx)
static async Task BlockUserAsync(TwitterContext twitterCtx)
{
Console.Write("User Screen Name to Block: ");
string? userName = Console.ReadLine() ?? "";

var user = await twitterCtx.CreateBlockAsync(0, userName, true);
TwitterUserQuery? userResponse =
await
(from usr in twitterCtx.TwitterUser
where usr.Type == UserType.UsernameLookup &&
usr.Usernames == userName
select usr)
.SingleOrDefaultAsync();

if (user != null)
Console.WriteLine("User Name: " + user.Name);
string? targetUserID = userResponse?.ID;
string? sourceUserID = twitterCtx.Authorizer?.CredentialStore?.UserID.ToString();

if (targetUserID == null || sourceUserID == null)
{
Console.WriteLine($"Either {nameof(targetUserID)} or {nameof(sourceUserID)} is null.");
return;
}

BlockingResponse? user = await twitterCtx.BlockUserAsync(sourceUserID, targetUserID);

if (user?.Data != null)
Console.WriteLine("Is Blocked: " + user.Data.Blocking);
}

static async Task DestroyBlockAsync(TwitterContext twitterCtx)
static async Task UnblockUserAsync(TwitterContext twitterCtx)
{
Console.Write("User Screen Name to Unblock: ");
Console.Write("User Screen Name to Block: ");
string? userName = Console.ReadLine() ?? "";

var user = await twitterCtx.DestroyBlockAsync(0, userName, true);
TwitterUserQuery? userResponse =
await
(from usr in twitterCtx.TwitterUser
where usr.Type == UserType.UsernameLookup &&
usr.Usernames == userName
select usr)
.SingleOrDefaultAsync();

string? targetUserID = userResponse?.ID;
string? sourceUserID = twitterCtx.Authorizer?.CredentialStore?.UserID.ToString();

if (targetUserID == null || sourceUserID == null)
{
Console.WriteLine($"Either {nameof(targetUserID)} or {nameof(sourceUserID)} is null.");
return;
}

BlockingResponse? user = await twitterCtx.UnblockUserAsync(sourceUserID, targetUserID);

if (user != null)
Console.WriteLine("User Name: " + user.Name);
if (user?.Data != null)
Console.WriteLine("Is Blocked: " + user.Data.Blocking);
}
}
}

0 comments on commit a06dfe1

Please sign in to comment.