2020-02-18 04:47:27 +00:00
|
|
|
''' 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',
|
2020-05-13 01:56:28 +00:00
|
|
|
'actor': user.remote_id,
|
|
|
|
'object': to_follow.remote_id,
|
2020-02-18 04:47:27 +00:00
|
|
|
}
|
|
|
|
|
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',
|
2020-05-13 01:56:28 +00:00
|
|
|
'id': '%s/undo' % relationship.remote_id,
|
2020-02-19 06:44:13 +00:00
|
|
|
'type': 'Undo',
|
2020-05-13 01:56:28 +00:00
|
|
|
'actor': relationship.user_subject.remote_id,
|
2020-02-19 06:44:13 +00:00
|
|
|
'object': {
|
|
|
|
'id': relationship.relationship_id,
|
|
|
|
'type': 'Follow',
|
2020-05-13 01:56:28 +00:00
|
|
|
'actor': relationship.user_subject.remote_id,
|
|
|
|
'object': relationship.user_object.remote_id,
|
2020-02-19 06:44:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-18 04:47:27 +00:00
|
|
|
|
2020-03-13 17:04:39 +00:00
|
|
|
def get_accept(user, relationship):
|
2020-02-18 04:52:14 +00:00
|
|
|
''' accept a follow request '''
|
2020-02-18 04:47:27 +00:00
|
|
|
return {
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
2020-05-13 01:56:28 +00:00
|
|
|
'id': '%s#accepts/follows/' % user.remote_id,
|
2020-02-18 04:47:27 +00:00
|
|
|
'type': 'Accept',
|
2020-05-13 01:56:28 +00:00
|
|
|
'actor': user.remote_id,
|
2020-03-13 17:04:39 +00:00
|
|
|
'object': {
|
|
|
|
'id': relationship.relationship_id,
|
|
|
|
'type': 'Follow',
|
2020-05-13 01:56:28 +00:00
|
|
|
'actor': relationship.user_subject.remote_id,
|
|
|
|
'object': relationship.user_object.remote_id,
|
2020-03-13 17:04:39 +00:00
|
|
|
}
|
2020-02-18 04:47:27 +00:00
|
|
|
}
|
|
|
|
|
2020-03-13 17:04:39 +00:00
|
|
|
|
|
|
|
def get_reject(user, relationship):
|
|
|
|
''' reject a follow request '''
|
|
|
|
return {
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
2020-05-13 01:56:28 +00:00
|
|
|
'id': '%s#rejects/follows/' % user.remote_id,
|
2020-03-13 17:04:39 +00:00
|
|
|
'type': 'Reject',
|
2020-05-13 01:56:28 +00:00
|
|
|
'actor': user.remote_id,
|
2020-03-13 17:04:39 +00:00
|
|
|
'object': {
|
|
|
|
'id': relationship.relationship_id,
|
|
|
|
'type': 'Follow',
|
2020-05-13 01:56:28 +00:00
|
|
|
'actor': relationship.user_subject.remote_id,
|
|
|
|
'object': relationship.user_object.remote_id,
|
2020-03-13 17:04:39 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-28 01:39:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_followers(user, page, follow_queryset):
|
|
|
|
''' list of people who follow a user '''
|
2020-05-13 01:56:28 +00:00
|
|
|
id_slug = '%s/followers' % user.remote_id
|
2020-03-28 01:39:26 +00:00
|
|
|
return get_follow_info(id_slug, page, follow_queryset)
|
|
|
|
|
|
|
|
|
|
|
|
def get_following(user, page, follow_queryset):
|
|
|
|
''' list of people who follow a user '''
|
2020-05-13 01:56:28 +00:00
|
|
|
id_slug = '%s/following' % user.remote_id
|
2020-03-28 01:39:26 +00:00
|
|
|
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,
|
2020-05-13 01:56:28 +00:00
|
|
|
'orderedItems': [u.remote_id for u in follower_page],
|
2020-03-28 01:39:26 +00:00
|
|
|
}
|
|
|
|
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
|