Remove @context in embedded collection

This commit is contained in:
Thomas Sileo 2018-06-04 19:10:04 +02:00
parent d2e62ed5b6
commit b89ee21e49
2 changed files with 28 additions and 9 deletions

View file

@ -30,6 +30,15 @@ ObjectType = Dict[str, Any]
ObjectOrIDType = Union[str, ObjectType] ObjectOrIDType = Union[str, ObjectType]
COLLECTION_CTX = [
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",
{
"Hashtag": "as:Hashtag",
"sensitive": "as:sensitive",
}
]
class ActivityType(Enum): class ActivityType(Enum):
"""Supported activity `type`.""" """Supported activity `type`."""
ANNOUNCE = 'Announce' ANNOUNCE = 'Announce'
@ -1196,11 +1205,11 @@ def build_ordered_collection(col, q=None, cursor=None, map_func=None, limit=50,
data = [_remove_id(doc) for doc in data] data = [_remove_id(doc) for doc in data]
if map_func: if map_func:
data = [map_func(doc) for doc in data] data = [map_func(doc) for doc in data]
# No cursor, this is the first page and we return an OrderedCollection # No cursor, this is the first page and we return an OrderedCollection
if not cursor: if not cursor:
resp = { resp = {
'@context': CTX_AS, '@context': COLLECTION_CTX,
'id': f'{BASE_URL}/{col_name}', 'id': f'{BASE_URL}/{col_name}',
'totalItems': total_items, 'totalItems': total_items,
'type': ActivityType.ORDERED_COLLECTION.value, 'type': ActivityType.ORDERED_COLLECTION.value,
@ -1223,7 +1232,7 @@ def build_ordered_collection(col, q=None, cursor=None, map_func=None, limit=50,
# If there's a cursor, then we return an OrderedCollectionPage # If there's a cursor, then we return an OrderedCollectionPage
resp = { resp = {
'@context': CTX_AS, '@context': COLLECTION_CTX,
'type': ActivityType.ORDERED_COLLECTION_PAGE.value, 'type': ActivityType.ORDERED_COLLECTION_PAGE.value,
'id': BASE_URL + '/' + col_name + '?cursor=' + start_cursor, 'id': BASE_URL + '/' + col_name + '?cursor=' + start_cursor,
'totalItems': total_items, 'totalItems': total_items,

22
app.py
View file

@ -601,9 +601,19 @@ def add_extra_collection(raw_doc: Dict[str, Any]) -> Dict[str, Any]:
return raw_doc return raw_doc
def activity_from_doc(raw_doc: Dict[str, Any]) -> Dict[str, Any]: def remove_context(activity: Dict[str, Any]) -> Dict[str, Any]:
if '@context' in activity:
del activity['@context']
return activity
def activity_from_doc(raw_doc: Dict[str, Any], embed: bool = False) -> Dict[str, Any]:
raw_doc = add_extra_collection(raw_doc) raw_doc = add_extra_collection(raw_doc)
return clean_activity(raw_doc['activity']) activity = clean_activity(raw_doc['activity'])
if embed:
return remove_context(activity)
return activity
@app.route('/outbox', methods=['GET', 'POST']) @app.route('/outbox', methods=['GET', 'POST'])
@ -621,7 +631,7 @@ def outbox():
DB.outbox, DB.outbox,
q=q, q=q,
cursor=request.args.get('cursor'), cursor=request.args.get('cursor'),
map_func=lambda doc: activity_from_doc(doc), map_func=lambda doc: activity_from_doc(doc, embed=True),
)) ))
# Handle POST request # Handle POST request
@ -719,7 +729,7 @@ def outbox_activity_likes(item_id):
DB.inbox, DB.inbox,
q=q, q=q,
cursor=request.args.get('cursor'), cursor=request.args.get('cursor'),
map_func=lambda doc: doc['activity'], map_func=lambda doc: remove_context(doc['activity']),
col_name=f'outbox/{item_id}/likes', col_name=f'outbox/{item_id}/likes',
first_page=request.args.get('page') == 'first', first_page=request.args.get('page') == 'first',
)) ))
@ -748,7 +758,7 @@ def outbox_activity_shares(item_id):
DB.inbox, DB.inbox,
q=q, q=q,
cursor=request.args.get('cursor'), cursor=request.args.get('cursor'),
map_func=lambda doc: doc['activity'], map_func=lambda doc: remove_context(doc['activity']),
col_name=f'outbox/{item_id}/shares', col_name=f'outbox/{item_id}/shares',
first_page=request.args.get('page') == 'first', first_page=request.args.get('page') == 'first',
)) ))
@ -1008,7 +1018,7 @@ def inbox():
DB.inbox, DB.inbox,
q={'meta.deleted': False}, q={'meta.deleted': False},
cursor=request.args.get('cursor'), cursor=request.args.get('cursor'),
map_func=lambda doc: doc['activity'], map_func=lambda doc: remove_context(doc['activity']),
)) ))
data = request.get_json(force=True) data = request.get_json(force=True)