moviewyrm/fedireads/activitypub/follow.py

114 lines
3.5 KiB
Python
Raw Normal View History

''' makin' freinds inthe ap json format '''
from uuid import uuid4
from fedireads.settings import DOMAIN
def get_follow_request(user, to_follow):
''' a local user wants to follow someone '''
uuid = uuid4()
return {
'@context': 'https://www.w3.org/ns/activitystreams',
'id': 'https://%s/%s' % (DOMAIN, str(uuid)),
'summary': '',
'type': 'Follow',
'actor': user.remote_id,
'object': to_follow.remote_id,
}
2020-02-19 06:44:13 +00:00
def get_unfollow(relationship):
''' undo that precious bond of friendship '''
return {
'@context': 'https://www.w3.org/ns/activitystreams',
'id': '%s/undo' % relationship.remote_id,
2020-02-19 06:44:13 +00:00
'type': 'Undo',
'actor': relationship.user_subject.remote_id,
2020-02-19 06:44:13 +00:00
'object': {
'id': relationship.relationship_id,
'type': 'Follow',
'actor': relationship.user_subject.remote_id,
'object': relationship.user_object.remote_id,
2020-02-19 06:44:13 +00:00
}
}
def get_accept(user, relationship):
2020-02-18 04:52:14 +00:00
''' accept a follow request '''
return {
'@context': 'https://www.w3.org/ns/activitystreams',
'id': '%s#accepts/follows/' % user.remote_id,
'type': 'Accept',
'actor': user.remote_id,
'object': {
'id': relationship.relationship_id,
'type': 'Follow',
'actor': relationship.user_subject.remote_id,
'object': relationship.user_object.remote_id,
}
}
def get_reject(user, relationship):
''' reject a follow request '''
return {
'@context': 'https://www.w3.org/ns/activitystreams',
'id': '%s#rejects/follows/' % user.remote_id,
'type': 'Reject',
'actor': user.remote_id,
'object': {
'id': relationship.relationship_id,
'type': 'Follow',
'actor': relationship.user_subject.remote_id,
'object': relationship.user_object.remote_id,
}
}
def get_followers(user, page, follow_queryset):
''' list of people who follow a user '''
id_slug = '%s/followers' % user.remote_id
return get_follow_info(id_slug, page, follow_queryset)
def get_following(user, page, follow_queryset):
''' list of people who follow a user '''
id_slug = '%s/following' % user.remote_id
return get_follow_info(id_slug, page, follow_queryset)
def get_follow_info(id_slug, page, follow_queryset):
''' a list of followers or following '''
if page:
return get_follow_page(follow_queryset, id_slug, page)
count = follow_queryset.count()
return {
'@context': 'https://www.w3.org/ns/activitystreams',
'id': id_slug,
'type': 'OrderedCollection',
'totalItems': count,
'first': '%s?page=1' % id_slug,
}
def get_follow_page(user_list, id_slug, page):
''' format a list of followers/following '''
page = int(page)
page_length = 10
start = (page - 1) * page_length
end = start + page_length
follower_page = user_list.all()[start:end]
data = {
'@context': 'https://www.w3.org/ns/activitystreams',
'id': '%s?page=%d' % (id_slug, page),
'type': 'OrderedCollectionPage',
'totalItems': user_list.count(),
'partOf': id_slug,
'orderedItems': [u.remote_id for u in follower_page],
}
if end <= user_list.count():
# there are still more pages
data['next'] = '%s?page=%d' % (id_slug, page + 1)
if start > 0:
data['prev'] = '%s?page=%d' % (id_slug, page - 1)
return data