bookwyrm/bookwyrm/wellknown.py

131 lines
3.7 KiB
Python
Raw Normal View History

2020-02-15 19:31:35 +00:00
''' responds to various requests to /.well-know '''
2020-11-01 19:09:16 +00:00
from dateutil.relativedelta import relativedelta
2020-11-01 18:42:48 +00:00
from django.http import HttpResponseNotFound
2020-02-15 19:31:35 +00:00
from django.http import JsonResponse
from django.utils import timezone
2020-02-15 19:31:35 +00:00
from bookwyrm import models
from bookwyrm.settings import DOMAIN, VERSION
2020-02-15 19:31:35 +00:00
def webfinger(request):
''' allow other servers to ask about a user '''
if request.method != 'GET':
return HttpResponseNotFound()
resource = request.GET.get('resource')
if not resource and not resource.startswith('acct:'):
2020-11-01 18:42:48 +00:00
return HttpResponseNotFound()
2020-11-01 20:29:31 +00:00
username = resource.replace('acct:', '')
2020-11-01 18:42:48 +00:00
try:
user = models.User.objects.get(username=username)
except models.User.DoesNotExist:
2020-02-15 19:31:35 +00:00
return HttpResponseNotFound('No account found')
2020-11-01 18:42:48 +00:00
2020-02-15 19:31:35 +00:00
return JsonResponse({
2020-11-01 20:29:31 +00:00
'subject': 'acct:%s' % (user.username),
2020-02-15 19:31:35 +00:00
'links': [
{
'rel': 'self',
'type': 'application/activity+json',
'href': user.remote_id
2020-02-15 19:31:35 +00:00
}
]
})
2020-02-15 19:40:21 +00:00
def nodeinfo_pointer(request):
''' direct servers to nodeinfo '''
2020-02-15 19:31:35 +00:00
if request.method != 'GET':
return HttpResponseNotFound()
return JsonResponse({
'links': [
{
'rel': 'http://nodeinfo.diaspora.software/ns/schema/2.0',
'href': 'https://%s/nodeinfo/2.0' % DOMAIN
}
]
})
2020-03-29 07:05:09 +00:00
2020-02-15 19:40:21 +00:00
def nodeinfo(request):
''' basic info about the server '''
if request.method != 'GET':
return HttpResponseNotFound()
2020-03-29 07:05:09 +00:00
status_count = models.Status.objects.filter(user__local=True).count()
2020-11-07 20:07:09 +00:00
user_count = models.User.objects.filter(local=True).count()
month_ago = timezone.now() - relativedelta(months=1)
last_month_count = models.User.objects.filter(
2020-11-07 20:07:09 +00:00
local=True,
last_active_date__gt=month_ago
).count()
six_months_ago = timezone.now() - relativedelta(months=6)
six_month_count = models.User.objects.filter(
2020-11-07 20:07:09 +00:00
local=True,
last_active_date__gt=six_months_ago
).count()
2020-11-08 05:07:07 +00:00
site = models.SiteSettings.get()
2020-02-15 19:40:21 +00:00
return JsonResponse({
2020-05-09 21:26:27 +00:00
'version': '2.0',
'software': {
'name': 'bookwyrm',
'version': VERSION
2020-02-15 19:40:21 +00:00
},
2020-05-09 21:26:27 +00:00
'protocols': [
'activitypub'
2020-02-15 19:40:21 +00:00
],
2020-05-09 21:26:27 +00:00
'usage': {
'users': {
'total': user_count,
'activeMonth': last_month_count,
'activeHalfyear': six_month_count,
2020-02-15 19:40:21 +00:00
},
2020-05-09 21:26:27 +00:00
'localPosts': status_count,
2020-02-15 19:40:21 +00:00
},
2020-11-08 05:07:07 +00:00
'openRegistrations': site.allow_registration,
2020-02-15 19:40:21 +00:00
})
2020-02-15 19:31:35 +00:00
def instance_info(request):
2020-11-08 05:07:07 +00:00
''' let's talk about your cool unique instance '''
2020-02-15 19:31:35 +00:00
if request.method != 'GET':
return HttpResponseNotFound()
2020-11-08 05:07:07 +00:00
user_count = models.User.objects.filter(local=True).count()
status_count = models.Status.objects.filter(user__local=True).count()
site = models.SiteSettings.get()
2020-02-15 19:31:35 +00:00
return JsonResponse({
'uri': DOMAIN,
2020-11-08 05:07:07 +00:00
'title': site.name,
'short_description': '',
'description': site.instance_description,
2020-02-15 19:31:35 +00:00
'version': '0.0.1',
'stats': {
'user_count': user_count,
'status_count': status_count,
},
2020-11-08 05:07:07 +00:00
'thumbnail': 'https://%s/static/images/logo.png' % DOMAIN,
2020-02-15 19:31:35 +00:00
'languages': [
'en'
],
2020-11-08 05:07:07 +00:00
'registrations': site.allow_registration,
2020-02-15 19:31:35 +00:00
'approval_required': False,
})
2020-03-29 07:05:09 +00:00
def peers(request):
''' list of federated servers this instance connects with '''
if request.method != 'GET':
return HttpResponseNotFound()
2020-03-29 22:51:43 +00:00
names = models.FederatedServer.objects.values_list('server_name', flat=True)
return JsonResponse(list(names), safe=False)