2020-06-06 00:14:42 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2020-07-22 19:27:25 -04:00
|
|
|
|
using System.Net.Mime;
|
2020-11-21 18:54:16 -05:00
|
|
|
|
using System.Runtime.InteropServices.WindowsRuntime;
|
2021-01-30 01:28:20 -05:00
|
|
|
|
using System.Text.RegularExpressions;
|
2020-06-06 00:14:42 -04:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2020-06-06 01:29:13 -04:00
|
|
|
|
using BirdsiteLive.ActivityPub;
|
2020-11-21 18:54:16 -05:00
|
|
|
|
using BirdsiteLive.ActivityPub.Models;
|
2021-02-01 20:13:10 -05:00
|
|
|
|
using BirdsiteLive.Common.Regexes;
|
2020-11-18 22:48:53 -05:00
|
|
|
|
using BirdsiteLive.Common.Settings;
|
2020-06-06 00:14:42 -04:00
|
|
|
|
using BirdsiteLive.Domain;
|
2020-11-18 22:48:53 -05:00
|
|
|
|
using BirdsiteLive.Models;
|
2021-12-09 02:02:30 -05:00
|
|
|
|
using BirdsiteLive.Tools;
|
2020-06-06 00:14:42 -04:00
|
|
|
|
using BirdsiteLive.Twitter;
|
2021-01-30 01:28:20 -05:00
|
|
|
|
using BirdsiteLive.Twitter.Models;
|
2020-06-06 01:29:13 -04:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2020-06-06 00:14:42 -04:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2021-04-15 21:21:22 -04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2020-06-06 01:29:13 -04:00
|
|
|
|
using Microsoft.Extensions.Primitives;
|
|
|
|
|
using Newtonsoft.Json;
|
2020-06-06 00:14:42 -04:00
|
|
|
|
|
|
|
|
|
namespace BirdsiteLive.Controllers
|
|
|
|
|
{
|
|
|
|
|
public class UsersController : Controller
|
|
|
|
|
{
|
2021-01-18 02:07:09 -05:00
|
|
|
|
private readonly ITwitterUserService _twitterUserService;
|
|
|
|
|
private readonly ITwitterTweetsService _twitterTweetService;
|
2020-06-06 00:14:42 -04:00
|
|
|
|
private readonly IUserService _userService;
|
2020-07-22 19:27:25 -04:00
|
|
|
|
private readonly IStatusService _statusService;
|
2020-11-18 22:48:53 -05:00
|
|
|
|
private readonly InstanceSettings _instanceSettings;
|
2021-04-15 21:21:22 -04:00
|
|
|
|
private readonly ILogger<UsersController> _logger;
|
2020-06-06 00:14:42 -04:00
|
|
|
|
|
|
|
|
|
#region Ctor
|
2021-04-15 21:21:22 -04:00
|
|
|
|
public UsersController(ITwitterUserService twitterUserService, IUserService userService, IStatusService statusService, InstanceSettings instanceSettings, ITwitterTweetsService twitterTweetService, ILogger<UsersController> logger)
|
2020-06-06 00:14:42 -04:00
|
|
|
|
{
|
2021-01-18 02:07:09 -05:00
|
|
|
|
_twitterUserService = twitterUserService;
|
2020-06-06 00:14:42 -04:00
|
|
|
|
_userService = userService;
|
2020-07-22 19:27:25 -04:00
|
|
|
|
_statusService = statusService;
|
2020-11-18 22:48:53 -05:00
|
|
|
|
_instanceSettings = instanceSettings;
|
2021-01-18 02:07:09 -05:00
|
|
|
|
_twitterTweetService = twitterTweetService;
|
2021-04-15 21:21:22 -04:00
|
|
|
|
_logger = logger;
|
2020-06-06 00:14:42 -04:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
2020-12-29 02:12:44 -05:00
|
|
|
|
[Route("/users")]
|
|
|
|
|
public IActionResult Index()
|
|
|
|
|
{
|
2021-01-11 01:08:23 -05:00
|
|
|
|
var acceptHeaders = Request.Headers["Accept"];
|
|
|
|
|
if (acceptHeaders.Any())
|
|
|
|
|
{
|
|
|
|
|
var r = acceptHeaders.First();
|
|
|
|
|
if (r.Contains("application/activity+json")) return NotFound();
|
|
|
|
|
}
|
2020-12-29 02:12:44 -05:00
|
|
|
|
return View("UserNotFound");
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-06 00:14:42 -04:00
|
|
|
|
[Route("/@{id}")]
|
|
|
|
|
[Route("/users/{id}")]
|
2021-07-21 00:52:39 -04:00
|
|
|
|
[Route("/users/{id}/remote_follow")]
|
2020-06-06 00:14:42 -04:00
|
|
|
|
public IActionResult Index(string id)
|
|
|
|
|
{
|
2021-04-15 21:21:22 -04:00
|
|
|
|
_logger.LogTrace("User Index: {Id}", id);
|
|
|
|
|
|
2021-01-11 01:08:23 -05:00
|
|
|
|
id = id.Trim(new[] { ' ', '@' }).ToLowerInvariant();
|
2021-01-30 01:28:20 -05:00
|
|
|
|
|
|
|
|
|
// Ensure valid username
|
|
|
|
|
// https://help.twitter.com/en/managing-your-account/twitter-username-rules
|
|
|
|
|
TwitterUser user = null;
|
2021-02-01 20:19:14 -05:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(id) && UserRegexes.TwitterAccount.IsMatch(id) && id.Length <= 15)
|
2021-01-30 01:28:20 -05:00
|
|
|
|
user = _twitterUserService.GetUser(id);
|
2020-07-01 22:45:43 -04:00
|
|
|
|
|
2021-01-11 01:08:23 -05:00
|
|
|
|
var acceptHeaders = Request.Headers["Accept"];
|
|
|
|
|
if (acceptHeaders.Any())
|
2020-06-06 00:14:42 -04:00
|
|
|
|
{
|
2021-01-11 01:08:23 -05:00
|
|
|
|
var r = acceptHeaders.First();
|
|
|
|
|
if (r.Contains("application/activity+json"))
|
|
|
|
|
{
|
|
|
|
|
if (user == null) return NotFound();
|
|
|
|
|
var apUser = _userService.GetUser(user);
|
|
|
|
|
var jsonApUser = JsonConvert.SerializeObject(apUser);
|
|
|
|
|
return Content(jsonApUser, "application/activity+json; charset=utf-8");
|
|
|
|
|
}
|
2020-06-06 00:14:42 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-29 02:12:44 -05:00
|
|
|
|
if (user == null) return View("UserNotFound");
|
|
|
|
|
|
2020-11-18 22:48:53 -05:00
|
|
|
|
var displayableUser = new DisplayTwitterUser
|
|
|
|
|
{
|
|
|
|
|
Name = user.Name,
|
|
|
|
|
Description = user.Description,
|
2021-01-09 22:26:17 -05:00
|
|
|
|
Acct = user.Acct.ToLowerInvariant(),
|
2020-11-18 22:48:53 -05:00
|
|
|
|
Url = user.Url,
|
|
|
|
|
ProfileImageUrl = user.ProfileImageUrl,
|
2021-01-28 18:47:45 -05:00
|
|
|
|
Protected = user.Protected,
|
2020-11-21 18:54:16 -05:00
|
|
|
|
|
2021-01-09 22:26:17 -05:00
|
|
|
|
InstanceHandle = $"@{user.Acct.ToLowerInvariant()}@{_instanceSettings.Domain}"
|
2020-11-18 22:48:53 -05:00
|
|
|
|
};
|
|
|
|
|
return View(displayableUser);
|
2020-06-06 00:14:42 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-01 22:45:43 -04:00
|
|
|
|
[Route("/@{id}/{statusId}")]
|
|
|
|
|
[Route("/users/{id}/statuses/{statusId}")]
|
|
|
|
|
public IActionResult Tweet(string id, string statusId)
|
|
|
|
|
{
|
2021-01-11 01:08:23 -05:00
|
|
|
|
var acceptHeaders = Request.Headers["Accept"];
|
|
|
|
|
if (acceptHeaders.Any())
|
2020-07-01 22:45:43 -04:00
|
|
|
|
{
|
2021-01-11 01:08:23 -05:00
|
|
|
|
var r = acceptHeaders.First();
|
|
|
|
|
if (r.Contains("application/activity+json"))
|
|
|
|
|
{
|
|
|
|
|
if (!long.TryParse(statusId, out var parsedStatusId))
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
2021-01-18 02:07:09 -05:00
|
|
|
|
var tweet = _twitterTweetService.GetTweet(parsedStatusId);
|
2021-01-11 01:08:23 -05:00
|
|
|
|
if (tweet == null)
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
|
|
|
|
//var user = _twitterService.GetUser(id);
|
|
|
|
|
//if (user == null) return NotFound();
|
|
|
|
|
|
|
|
|
|
var status = _statusService.GetStatus(id, tweet);
|
|
|
|
|
var jsonApUser = JsonConvert.SerializeObject(status);
|
|
|
|
|
return Content(jsonApUser, "application/activity+json; charset=utf-8");
|
|
|
|
|
}
|
2020-07-01 22:45:43 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 19:14:51 -05:00
|
|
|
|
return Redirect($"https://twitter.com/{id}/status/{statusId}");
|
2020-07-01 22:45:43 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-06 00:14:42 -04:00
|
|
|
|
[Route("/users/{id}/inbox")]
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public async Task<IActionResult> Inbox()
|
|
|
|
|
{
|
|
|
|
|
var r = Request;
|
|
|
|
|
using (var reader = new StreamReader(Request.Body))
|
|
|
|
|
{
|
|
|
|
|
var body = await reader.ReadToEndAsync();
|
2021-04-15 21:21:22 -04:00
|
|
|
|
|
|
|
|
|
_logger.LogTrace("User Inbox: {Body}", body);
|
2020-11-21 18:54:16 -05:00
|
|
|
|
//System.IO.File.WriteAllText($@"C:\apdebug\{Guid.NewGuid()}.json", body);
|
2020-07-22 19:27:25 -04:00
|
|
|
|
|
2020-06-06 01:29:13 -04:00
|
|
|
|
var activity = ApDeserializer.ProcessActivity(body);
|
2020-07-08 19:50:58 -04:00
|
|
|
|
var signature = r.Headers["Signature"].First();
|
2021-01-15 02:00:10 -05:00
|
|
|
|
|
2020-07-08 18:41:21 -04:00
|
|
|
|
switch (activity?.type)
|
2020-06-06 01:29:13 -04:00
|
|
|
|
{
|
2020-07-01 22:45:43 -04:00
|
|
|
|
case "Follow":
|
2020-07-08 19:50:58 -04:00
|
|
|
|
{
|
|
|
|
|
var succeeded = await _userService.FollowRequestedAsync(signature, r.Method, r.Path,
|
2021-12-09 02:02:30 -05:00
|
|
|
|
r.QueryString.ToString(), HeaderHandler.RequestHeaders(r.Headers), activity as ActivityFollow, body);
|
2020-07-08 19:50:58 -04:00
|
|
|
|
if (succeeded) return Accepted();
|
|
|
|
|
else return Unauthorized();
|
|
|
|
|
}
|
2020-06-28 23:42:23 -04:00
|
|
|
|
case "Undo":
|
2020-07-08 19:50:58 -04:00
|
|
|
|
if (activity is ActivityUndoFollow)
|
|
|
|
|
{
|
|
|
|
|
var succeeded = await _userService.UndoFollowRequestedAsync(signature, r.Method, r.Path,
|
2021-12-09 02:02:30 -05:00
|
|
|
|
r.QueryString.ToString(), HeaderHandler.RequestHeaders(r.Headers), activity as ActivityUndoFollow, body);
|
2020-07-08 19:50:58 -04:00
|
|
|
|
if (succeeded) return Accepted();
|
|
|
|
|
else return Unauthorized();
|
|
|
|
|
}
|
2020-06-28 23:42:23 -04:00
|
|
|
|
return Accepted();
|
2021-12-09 02:02:30 -05:00
|
|
|
|
case "Delete":
|
|
|
|
|
{
|
|
|
|
|
var succeeded = await _userService.DeleteRequestedAsync(signature, r.Method, r.Path,
|
|
|
|
|
r.QueryString.ToString(), HeaderHandler.RequestHeaders(r.Headers), activity as ActivityDelete, body);
|
|
|
|
|
if (succeeded) return Accepted();
|
|
|
|
|
else return Unauthorized();
|
|
|
|
|
}
|
2020-06-06 01:29:13 -04:00
|
|
|
|
default:
|
2020-06-28 23:42:23 -04:00
|
|
|
|
return Accepted();
|
2020-06-06 01:29:13 -04:00
|
|
|
|
}
|
2020-06-06 00:14:42 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-06 01:29:13 -04:00
|
|
|
|
|
2020-11-21 18:54:16 -05:00
|
|
|
|
[Route("/users/{id}/followers")]
|
|
|
|
|
[HttpGet]
|
2021-02-14 12:28:38 -05:00
|
|
|
|
public IActionResult Followers(string id)
|
2020-11-21 18:54:16 -05:00
|
|
|
|
{
|
|
|
|
|
var r = Request.Headers["Accept"].First();
|
|
|
|
|
if (!r.Contains("application/activity+json")) return NotFound();
|
|
|
|
|
|
|
|
|
|
var followers = new Followers
|
|
|
|
|
{
|
|
|
|
|
id = $"https://{_instanceSettings.Domain}/users/{id}/followers"
|
|
|
|
|
};
|
|
|
|
|
var jsonApUser = JsonConvert.SerializeObject(followers);
|
|
|
|
|
return Content(jsonApUser, "application/activity+json; charset=utf-8");
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-09 02:02:30 -05:00
|
|
|
|
|
2020-06-06 00:14:42 -04:00
|
|
|
|
}
|
|
|
|
|
}
|