~fabrixxm/activist

aebe5f53067ca985714ee2728890d664e832926b — fabrixxm a month ago 13a2994
add handling for incoming 'Like', 'Dislike'

and Fix handling incoming reactions on unknown objects
1 files changed, 26 insertions(+), 21 deletions(-)

M activist/activitypub.py
M activist/activitypub.py => activist/activitypub.py +26 -21
@@ 331,13 331,13 @@ def handle_create(activity:dict, actor:dict) -> bool:
    return True


def handle_announce(activity:dict, actor:dict) -> bool:
    """ Announce """
def handle_reaction(activity:dict, actor:dict) -> bool:
    """ Announce, Like, Dislike... """
    ## Boost

    obj = expand_object(activity, 'object')
    if obj is None:
        logger.info("handle_announce: Cannot fetch announced object, ingnore %r", activity)
        logger.info("handle_reaction: Cannot fetch object, ingnore %r", activity)
        return False

    # we accept announce for object we already know or from actor we follow


@@ 346,16 346,21 @@ def handle_announce(activity:dict, actor:dict) -> bool:
    if not (is_known or is_followed):
        why = "" if is_known else " Is unkown object"
        why = "" if is_followed else " Not from actor we follow"
        logger.debug("handle_announce: %s, ignore", why)
        logger.debug("handle_reaction: %s, ignore", why)
        return False
    
    # update object 'updated' datetime to 'now'
    asobj = db.Object.get(obj['id'])
    if asobj is not None:
        asobj.updated = datetime.datetime.now(datetime.timezone.utc)
    else:
        # save object if we didn't have it yet
        asobj = save_recursive(obj)


    
    # store object in user "network" list
    db.List.append('network', obj['id'])
    db.List.append('network', asobj.id)
    return True




@@ 488,7 493,7 @@ def handle_incoming(activity:dict):
    # TODO: check request signature
    logger.debug("handle_incoming %r", activity)

    accepted_activities = ["Create", "Announce", "Accept", "Reject", "Follow", "Undo"]
    accepted_activities = ["Create", "Announce", "Like", "Dislike", "Accept", "Reject", "Follow", "Undo"]

    acttype = activity.get('type', None)
    


@@ 510,27 515,27 @@ def handle_incoming(activity:dict):

    logger.debug("handle_incoming activity type %r : %r", acttype, activity)

    if acttype is None:
        return

    if acttype == "Create":
        can_store = handle_create(activity, actor)
    match acttype:
        case "Create":
            can_store = handle_create(activity, actor)

    elif acttype == "Announce":
        can_store = handle_announce(activity, actor)
        case "Announce" | "Like" | "Dislike":
            can_store = handle_reaction(activity, actor)

    elif acttype == "Accept":
        can_store = handle_accept(activity, actor)
        case "Accept":
            can_store = handle_accept(activity, actor)

    elif acttype == "Reject":
        can_store = handle_reject(activity, actor)
        case "Reject":
            can_store = handle_reject(activity, actor)

    elif acttype == "Follow":
        can_store = handle_follow(activity, actor)
        case "Follow":
            can_store = handle_follow(activity, actor)

    elif acttype == "Undo":
        can_store = handle_undo(activity, actor)
        case "Undo":
            can_store = handle_undo(activity, actor)

        case None:
            return

    if can_store:
        # store activity