Ejemplo n.º 1
0
def outbox_activity_shares(item_id):
    # TODO(tsileo): handle Tombstone
    if not is_api_request():
        abort(404)
    data = DB.outbox.find_one({'id': item_id, 'meta.deleted': False})
    if not data:
        abort(404)
    obj = activitypub.parse_activity(data['activity'])
    if obj.type_enum != ActivityType.CREATE:
        abort(404)

    q = {
        'meta.undo':
        False,
        'type':
        ActivityType.ANNOUNCE.value,
        '$or': [{
            'activity.object.id': obj.get_object().id
        }, {
            'activity.object': obj.get_object().id
        }],
    }

    return jsonify(**activitypub.build_ordered_collection(
        DB.inbox,
        q=q,
        cursor=request.args.get('cursor'),
        map_func=lambda doc: doc['activity'],
        col_name=f'outbox/{item_id}/shares',
        first_page=request.args.get('page') == 'first',
    ))
Ejemplo n.º 2
0
def tags(tag):
    if not DB.outbox.count({
            'activity.object.tag.type': 'Hashtag',
            'activity.object.tag.name': '#' + tag
    }):
        abort(404)
    if not is_api_request():
        return render_template(
            'tags.html',
            tag=tag,
            outbox_data=DB.outbox.find({
                'type': 'Create',
                'activity.object.type': 'Note',
                'meta.deleted': False,
                'activity.object.tag.type': 'Hashtag',
                'activity.object.tag.name': '#' + tag
            }),
        )
    q = {
        'meta.deleted': False,
        'meta.undo': False,
        'type': ActivityType.CREATE.value,
        'activity.object.tag.type': 'Hashtag',
        'activity.object.tag.name': '#' + tag,
    }
    return jsonify(**activitypub.build_ordered_collection(
        DB.outbox,
        q=q,
        cursor=request.args.get('cursor'),
        map_func=lambda doc: doc['activity']['object']['id'],
        col_name=f'tags/{tag}',
    ))
Ejemplo n.º 3
0
def liked():
    if not is_api_request():
        abort(404)
    q = {
        'meta.deleted': False,
        'meta.undo': False,
        'type': ActivityType.LIKE.value,
    }
    return jsonify(**activitypub.build_ordered_collection(
        DB.outbox,
        q=q,
        cursor=request.args.get('cursor'),
        map_func=lambda doc: doc['activity']['object'],
        col_name='liked',
    ))
Ejemplo n.º 4
0
def inbox():
    if request.method == 'GET':
        if not is_api_request():
            abort(404)
        try:
            _api_required()
        except BadSignature:
            abort(404)

        return jsonify(**activitypub.build_ordered_collection(
            DB.inbox,
            q={'meta.deleted': False},
            cursor=request.args.get('cursor'),
            map_func=lambda doc: doc['activity'],
        ))

    data = request.get_json(force=True)
    logger.debug(f'req_headers={request.headers}')
    logger.debug(f'raw_data={data}')
    try:
        if not verify_request(ACTOR_SERVICE):
            raise Exception('failed to verify request')
    except Exception:
        logger.exception(
            'failed to verify request, trying to verify the payload by fetching the remote'
        )
        try:
            data = OBJECT_SERVICE.get(data['id'])
        except Exception:
            logger.exception(f'failed to fetch remote id at {data["id"]}')
            return Response(
                status=422,
                headers={'Content-Type': 'application/json'},
                response=json.dumps({
                    'error':
                    'failed to verify request (using HTTP signatures or fetching the IRI)'
                }),
            )

    activity = activitypub.parse_activity(data)
    logger.debug(f'inbox activity={activity}/{data}')
    activity.process_from_inbox()

    return Response(status=201, )
Ejemplo n.º 5
0
def followers():
    if is_api_request():
        return jsonify(**activitypub.build_ordered_collection(
            DB.followers,
            cursor=request.args.get('cursor'),
            map_func=lambda doc: doc['remote_actor'],
        ))

    followers = [
        ACTOR_SERVICE.get(doc['remote_actor'])
        for doc in DB.followers.find(limit=50)
    ]
    return render_template(
        'followers.html',
        me=ME,
        notes=DB.inbox.find({
            'object.object.type': 'Note'
        }).count(),
        followers=DB.followers.count(),
        following=DB.following.count(),
        followers_data=followers,
    )
Ejemplo n.º 6
0
def outbox():
    if request.method == 'GET':
        if not is_api_request():
            abort(404)
        # TODO(tsileo): filter the outbox if not authenticated
        # FIXME(tsileo): filter deleted, add query support for build_ordered_collection
        q = {
            'meta.deleted': False,
            #'type': {'$in': [ActivityType.CREATE.value, ActivityType.ANNOUNCE.value]},
        }
        return jsonify(**activitypub.build_ordered_collection(
            DB.outbox,
            q=q,
            cursor=request.args.get('cursor'),
            map_func=lambda doc: activity_from_doc(doc),
        ))

    # Handle POST request
    try:
        _api_required()
    except BadSignature:
        abort(401)

    data = request.get_json(force=True)
    print(data)
    activity = activitypub.parse_activity(data)

    if activity.type_enum == ActivityType.NOTE:
        activity = activity.build_create()

    activity.post_to_outbox()

    # Purge the cache if a custom hook is set, as new content was published
    custom_cache_purge_hook()

    return Response(status=201, headers={'Location': activity.id})