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-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-06-06 00:14:42 -04:00
|
|
|
|
using BirdsiteLive.Domain;
|
|
|
|
|
using BirdsiteLive.Twitter;
|
2020-06-06 01:29:13 -04:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2020-06-06 00:14:42 -04:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
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
|
|
|
|
|
{
|
|
|
|
|
private readonly ITwitterService _twitterService;
|
|
|
|
|
private readonly IUserService _userService;
|
2020-07-22 19:27:25 -04:00
|
|
|
|
private readonly IStatusService _statusService;
|
2020-06-06 00:14:42 -04:00
|
|
|
|
|
|
|
|
|
#region Ctor
|
2020-07-22 19:27:25 -04:00
|
|
|
|
public UsersController(ITwitterService twitterService, IUserService userService, IStatusService statusService)
|
2020-06-06 00:14:42 -04:00
|
|
|
|
{
|
|
|
|
|
_twitterService = twitterService;
|
|
|
|
|
_userService = userService;
|
2020-07-22 19:27:25 -04:00
|
|
|
|
_statusService = statusService;
|
2020-06-06 00:14:42 -04:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
[Route("/@{id}")]
|
|
|
|
|
[Route("/users/{id}")]
|
|
|
|
|
public IActionResult Index(string id)
|
|
|
|
|
{
|
|
|
|
|
var user = _twitterService.GetUser(id);
|
|
|
|
|
if (user == null) return NotFound();
|
2020-07-01 22:45:43 -04:00
|
|
|
|
|
2020-06-06 00:14:42 -04:00
|
|
|
|
var r = Request.Headers["Accept"].First();
|
|
|
|
|
if (r.Contains("application/activity+json"))
|
|
|
|
|
{
|
|
|
|
|
var apUser = _userService.GetUser(user);
|
2020-06-06 01:29:13 -04:00
|
|
|
|
var jsonApUser = JsonConvert.SerializeObject(apUser);
|
2020-07-03 00:39:06 -04:00
|
|
|
|
return Content(jsonApUser, "application/activity+json; charset=utf-8");
|
2020-06-06 00:14:42 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return View(user);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-01 22:45:43 -04:00
|
|
|
|
[Route("/@{id}/{statusId}")]
|
|
|
|
|
[Route("/users/{id}/statuses/{statusId}")]
|
|
|
|
|
public IActionResult Tweet(string id, string statusId)
|
|
|
|
|
{
|
|
|
|
|
var r = Request.Headers["Accept"].First();
|
|
|
|
|
if (r.Contains("application/activity+json"))
|
|
|
|
|
{
|
|
|
|
|
if (!long.TryParse(statusId, out var parsedStatusId))
|
|
|
|
|
return NotFound();
|
2020-07-08 19:50:58 -04:00
|
|
|
|
|
2020-07-01 22:45:43 -04:00
|
|
|
|
var tweet = _twitterService.GetTweet(parsedStatusId);
|
2020-07-08 19:50:58 -04:00
|
|
|
|
if (tweet == null)
|
2020-07-01 22:45:43 -04:00
|
|
|
|
return NotFound();
|
|
|
|
|
|
2020-07-22 02:03:52 -04:00
|
|
|
|
//var user = _twitterService.GetUser(id);
|
|
|
|
|
//if (user == null) return NotFound();
|
2020-07-01 22:45:43 -04:00
|
|
|
|
|
2020-07-22 19:27:25 -04:00
|
|
|
|
var status = _statusService.GetStatus(id, tweet);
|
2020-07-01 22:45:43 -04:00
|
|
|
|
var jsonApUser = JsonConvert.SerializeObject(status);
|
2020-07-03 00:39:06 -04:00
|
|
|
|
return Content(jsonApUser, "application/activity+json; charset=utf-8");
|
2020-07-01 22:45:43 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return View("Tweet", statusId);
|
|
|
|
|
}
|
|
|
|
|
|
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();
|
2020-07-22 19:27:25 -04:00
|
|
|
|
//System.IO.File.WriteAllText($@"C:\apdebug\{Guid.NewGuid()}.json", body);
|
|
|
|
|
|
2020-06-06 01:29:13 -04:00
|
|
|
|
var activity = ApDeserializer.ProcessActivity(body);
|
2020-06-06 00:14:42 -04:00
|
|
|
|
// Do something
|
2020-07-08 19:50:58 -04:00
|
|
|
|
var signature = r.Headers["Signature"].First();
|
2020-06-06 01:29:13 -04:00
|
|
|
|
|
2020-07-16 01:18:14 -04:00
|
|
|
|
Console.WriteLine(body);
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
|
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,
|
|
|
|
|
r.QueryString.ToString(), RequestHeaders(r.Headers), activity as ActivityFollow);
|
|
|
|
|
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,
|
|
|
|
|
r.QueryString.ToString(), RequestHeaders(r.Headers), activity as ActivityUndoFollow);
|
|
|
|
|
if (succeeded) return Accepted();
|
|
|
|
|
else return Unauthorized();
|
|
|
|
|
}
|
2020-06-28 23:42:23 -04:00
|
|
|
|
return Accepted();
|
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-07-08 18:41:21 -04:00
|
|
|
|
return Accepted();
|
2020-06-06 00:14:42 -04:00
|
|
|
|
}
|
2020-06-06 01:29:13 -04:00
|
|
|
|
|
|
|
|
|
private Dictionary<string, string> RequestHeaders(IHeaderDictionary header)
|
|
|
|
|
{
|
2020-06-10 20:40:25 -04:00
|
|
|
|
return header.ToDictionary<KeyValuePair<string, StringValues>, string, string>(h => h.Key.ToLowerInvariant(), h => h.Value);
|
2020-06-06 01:29:13 -04:00
|
|
|
|
}
|
2020-06-06 00:14:42 -04:00
|
|
|
|
}
|
|
|
|
|
}
|