cloutier--bird.makeup/src/BirdsiteLive/Controllers/WellKnownController.cs

190 lines
6.1 KiB
C#
Raw Normal View History

2020-03-21 03:29:36 -04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
2020-06-06 00:14:42 -04:00
using BirdsiteLive.Common.Settings;
2020-03-21 17:11:35 -04:00
using BirdsiteLive.Models;
2020-07-07 18:30:52 -04:00
using BirdsiteLive.Models.WellKnownModels;
2020-03-21 18:58:23 -04:00
using BirdsiteLive.Twitter;
2020-03-21 03:29:36 -04:00
using Microsoft.AspNetCore.Mvc;
2020-03-21 17:11:35 -04:00
using Microsoft.Extensions.Options;
2020-03-21 03:29:36 -04:00
namespace BirdsiteLive.Controllers
{
[ApiController]
public class WellKnownController : ControllerBase
{
2020-03-21 18:58:23 -04:00
private readonly ITwitterService _twitterService;
2020-03-21 17:11:35 -04:00
private readonly InstanceSettings _settings;
2020-03-21 03:29:36 -04:00
#region Ctor
2020-07-07 21:03:20 -04:00
public WellKnownController(InstanceSettings settings, ITwitterService twitterService)
2020-03-21 03:29:36 -04:00
{
2020-03-21 18:58:23 -04:00
_twitterService = twitterService;
2020-07-07 21:03:20 -04:00
_settings = settings;
2020-03-21 03:29:36 -04:00
}
#endregion
2020-07-06 21:25:10 -04:00
[Route("/.well-known/nodeinfo")]
public IActionResult WellKnownNodeInfo()
{
var nodeInfo = new WellKnownNodeInfo
{
links = new Link[]
{
new Link()
{
rel = "http://nodeinfo.diaspora.software/ns/schema/2.0",
href = $"https://{_settings.Domain}/nodeinfo/2.0.json"
2020-07-07 18:30:52 -04:00
},
new Link()
{
rel = "http://nodeinfo.diaspora.software/ns/schema/2.1",
href = $"https://{_settings.Domain}/nodeinfo/2.1.json"
2020-07-06 21:25:10 -04:00
}
}
};
return new JsonResult(nodeInfo);
}
2020-07-07 18:30:52 -04:00
[Route("/nodeinfo/{id}.json")]
public IActionResult NodeInfo(string id)
2020-07-06 21:25:10 -04:00
{
2020-12-28 00:17:06 -05:00
var version = System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString(3);
2020-07-07 18:30:52 -04:00
if (id == "2.0")
2020-07-06 21:25:10 -04:00
{
2020-07-07 18:30:52 -04:00
var nodeInfo = new NodeInfoV20
2020-07-06 21:25:10 -04:00
{
2020-07-07 18:30:52 -04:00
version = "2.0",
usage = new Usage()
{
localPosts = 0,
users = new Users()
{
total = 0
}
},
software = new Software()
{
name = "birdsitelive",
2020-12-28 00:17:06 -05:00
version = version
2020-07-07 18:30:52 -04:00
},
protocols = new[]
2020-07-06 21:25:10 -04:00
{
2020-07-07 18:30:52 -04:00
"activitypub"
},
openRegistrations = false,
services = new Models.WellKnownModels.Services()
{
inbound = new object[0],
outbound = new object[0]
2020-07-08 18:41:47 -04:00
},
metadata = new Metadata()
{
email = _settings.AdminEmail
2020-07-06 21:25:10 -04:00
}
2020-07-07 18:30:52 -04:00
};
return new JsonResult(nodeInfo);
}
if (id == "2.1")
{
var nodeInfo = new NodeInfoV21
2020-07-07 02:57:25 -04:00
{
2020-07-07 18:30:52 -04:00
version = "2.1",
usage = new Usage()
{
localPosts = 0,
users = new Users()
{
total = 0
}
},
software = new SoftwareV21()
{
name = "birdsitelive",
2020-12-28 00:17:06 -05:00
version = version,
2020-07-07 18:30:52 -04:00
repository = "https://github.com/NicolasConstant/BirdsiteLive"
},
protocols = new[]
{
"activitypub"
},
openRegistrations = false,
services = new Models.WellKnownModels.Services()
{
inbound = new object[0],
outbound = new object[0]
2020-07-08 18:41:47 -04:00
},
metadata = new Metadata()
{
email = _settings.AdminEmail
2020-07-07 18:30:52 -04:00
}
};
return new JsonResult(nodeInfo);
}
2020-07-06 21:25:10 -04:00
2020-07-07 18:30:52 -04:00
return NotFound();
2020-07-06 21:25:10 -04:00
}
2020-03-21 03:29:36 -04:00
[Route("/.well-known/webfinger")]
2020-03-21 17:11:35 -04:00
public IActionResult Webfinger(string resource = null)
2020-03-21 03:29:36 -04:00
{
2020-03-21 17:11:35 -04:00
var acct = resource.Split("acct:")[1].Trim();
string name = null;
string domain = null;
var splitAcct = acct.Split('@', StringSplitOptions.RemoveEmptyEntries);
var atCount = acct.Count(x => x == '@');
if (atCount == 1 && acct.StartsWith('@'))
{
name = splitAcct[1];
}
else if (atCount == 1 || atCount == 2)
{
name = splitAcct[0];
domain = splitAcct[1];
}
else
{
return BadRequest();
}
2020-03-21 03:29:36 -04:00
2020-03-21 17:11:35 -04:00
if (!string.IsNullOrWhiteSpace(domain) && domain != _settings.Domain)
return NotFound();
2020-07-06 21:25:10 -04:00
2020-03-22 16:38:15 -04:00
var user = _twitterService.GetUser(name);
if (user == null)
return NotFound();
2020-03-21 03:29:36 -04:00
2020-03-21 17:11:35 -04:00
var result = new WebFingerResult()
2020-03-21 03:29:36 -04:00
{
2020-03-21 17:11:35 -04:00
subject = $"acct:{name}@{_settings.Domain}",
2020-07-06 21:25:10 -04:00
aliases = new[]
2020-03-21 17:11:35 -04:00
{
$"https://{_settings.Domain}/@{name}",
$"https://{_settings.Domain}/users/{name}"
},
2020-03-21 03:29:36 -04:00
links = new List<WebFingerLink>
{
2020-03-21 17:11:35 -04:00
new WebFingerLink()
{
rel = "http://webfinger.net/rel/profile-page",
type = "text/html",
href = $"https://{_settings.Domain}/@{name}"
},
2020-03-21 03:29:36 -04:00
new WebFingerLink()
{
rel = "self",
type = "application/activity+json",
2020-03-21 17:11:35 -04:00
href = $"https://{_settings.Domain}/users/{name}"
2020-03-21 03:29:36 -04:00
}
}
};
2020-03-21 17:11:35 -04:00
return new JsonResult(result);
2020-03-21 03:29:36 -04:00
}
2020-07-06 21:25:10 -04:00
}
2020-03-21 03:29:36 -04:00
}