~fabrixxm/activist

13a2994d91ee58e56f97729fb7e97ef50178b494 — fabrixxm a month ago b983580
Move AS Objects lists to 'consts'
M activist/activitypub.py => activist/activitypub.py +3 -3
@@ 11,7 11,7 @@ from typing import Optional, Union, Callable
from . import http
from . import activitystream
from .activitystream import ContextType, move_context, ASRef, object_id
from .consts import CTX_AS, MIME_AS
from .consts import CTX_AS, MIME_AS, ACTORS, OBJECTS
from .config import settings
from . import tasks
from . import db


@@ 222,7 222,7 @@ def save_recursive(data:dict) -> db.Object:
    obj = db.Object.from_data(data)

    # if obj is an actor, query webfinger to update "acct" (and photo if defined)
    if obj.type in activitystream.ACTORS and obj.acct is None:
    if obj.type in ACTORS and obj.acct is None:
        webfinger = get_webfinger(obj.id)
        if webfinger is not None:
            for link in webfinger.get('links', []):


@@ 243,7 243,7 @@ def save_recursive(data:dict) -> db.Object:

    obj.save()

    if obj.type in activitystream.OBJECTS:
    if obj.type in OBJECTS:
        # https://www.w3.org/TR/activitystreams-vocabulary/#dfn-inreplyto
        # doesn't limit replies to object types, so we could reply to anything
        # but we don't show replies to activities or links so, we limit

M activist/activitystream.py => activist/activitystream.py +0 -49
@@ 12,55 12,6 @@ ContextType = Union[str, list[Union[str, dict[str, str]]]]



# List of activitie types defined by
# https://www.w3.org/TR/activitystreams-vocabulary/#activity-types
ACTIVITES = ['Accept', 'Add', 'Announce', 'Arrive', 'Block', 'Create', 'Delete', 
             'Dislike', 'Flag', 'Follow', 'Ignore', 'Invite', 'Join', 'Leave', 'Like', 
             'Listen', 'Move', 'Offer', 'Question', 'Reject', 'Read', 'Remove', 
             'TentativeReject', 'TentativeAccept', 'Travel', 'Undo', 'Update', 'View']

# Map activites verb to their past tense
ACTIVITES_PAST = {
    'Accept': 'accepted',
    'Add': 'added',
    'Announce': 'announced',
    'Arrive': 'arrived',
    'Block': 'blocked',
    'Create': 'created',
    'Delete': 'deleted',
    'Dislike': 'disliked',
    'Flag': 'flagged',
    'Follow': 'followed',
    'Ignore': 'ignored',
    'Invite': 'invited',
    'Join': 'joined',
    'Leave': 'leaved',
    'Like': 'liked',
    'Listen': 'listened',
    'Move': 'moved',
    'Offer': 'offered',
    'Question': 'questioned',
    'Reject': 'rejected',
    'Read': 'read',
    'Remove': 'removed',
    'TentativeReject': 'tentatively rejected',
    'TentativeAccept': 'tentatively accepted',
    'Travel': 'traveled',
    'Undo': 'undone',
    'Update': 'updated',
    'View': 'viewed',
}


# List of actor types defined by
# https://www.w3.org/TR/activitystreams-vocabulary/#actor-types
ACTORS = ['Application', 'Group', 'Organization', 'Person', 'Service',]

# List of Object types defined by (but not Tombstone?)
# https://www.w3.org/TR/activitystreams-vocabulary/#object-types
OBJECTS = ['Article', 'Audio', 'Document', 'Event', 'Image', 'Note', 'Page', 'Place', 
           'Profile', 'Relationship', 'Video']


def object_id(obj:ASRef) -> Optional[str]:
    """

M activist/consts.py => activist/consts.py +50 -0
@@ 5,3 5,53 @@ AS_PUBLIC = "https://www.w3.org/ns/activitystreams#Public"

MIME_AS = "application/activity+json"
MIME_HTML = "text/html"


# List of activitie types defined by
# https://www.w3.org/TR/activitystreams-vocabulary/#activity-types
ACTIVITES = ['Accept', 'Add', 'Announce', 'Arrive', 'Block', 'Create', 'Delete', 
             'Dislike', 'Flag', 'Follow', 'Ignore', 'Invite', 'Join', 'Leave', 'Like', 
             'Listen', 'Move', 'Offer', 'Question', 'Reject', 'Read', 'Remove', 
             'TentativeReject', 'TentativeAccept', 'Travel', 'Undo', 'Update', 'View']

# Map activites verb to their past tense
ACTIVITES_PAST = {
    'Accept': 'accepted',
    'Add': 'added',
    'Announce': 'announced',
    'Arrive': 'arrived',
    'Block': 'blocked',
    'Create': 'created',
    'Delete': 'deleted',
    'Dislike': 'disliked',
    'Flag': 'flagged',
    'Follow': 'followed',
    'Ignore': 'ignored',
    'Invite': 'invited',
    'Join': 'joined',
    'Leave': 'leaved',
    'Like': 'liked',
    'Listen': 'listened',
    'Move': 'moved',
    'Offer': 'offered',
    'Question': 'questioned',
    'Reject': 'rejected',
    'Read': 'read',
    'Remove': 'removed',
    'TentativeReject': 'tentatively rejected',
    'TentativeAccept': 'tentatively accepted',
    'Travel': 'traveled',
    'Undo': 'undone',
    'Update': 'updated',
    'View': 'viewed',
}


# List of actor types defined by
# https://www.w3.org/TR/activitystreams-vocabulary/#actor-types
ACTORS = ['Application', 'Group', 'Organization', 'Person', 'Service',]

# List of Object types defined by (but not Tombstone?)
# https://www.w3.org/TR/activitystreams-vocabulary/#object-types
OBJECTS = ['Article', 'Audio', 'Document', 'Event', 'Image', 'Note', 'Page', 'Place', 
           'Profile', 'Relationship', 'Video']

M activist/tasks.py => activist/tasks.py +1 -1
@@ 7,7 7,7 @@ import logging
import datetime
from typing import Optional
from .background import background, UrecoverableBacgroundTaskException
from .activitystream import AS_PUBLIC, ACTORS
from .consts import AS_PUBLIC, ACTORS
from .config import settings
from . import activitypub
from . import db

M activist/template.py => activist/template.py +7 -6
@@ 20,6 20,7 @@ from . import utils
from .app import app
from .config import settings
from .activitystream import ASRef
from .consts import ACTIVITES_PAST, ACTIVITES, ACTORS, OBJECTS


@app.template_filter('getlist')


@@ 112,29 113,29 @@ def firstof(obj:Union[db.Object,dict], *args) -> Any:


@app.template_filter('pastaction')
def pastaction(obj:ASRef) -> str:
def pastaction(obj:ASRef, **custom) -> str:
    otype = activitystream.object_type(obj)
    if otype is None:
        return "done something unknown to"
    return activitystream.ACTIVITES_PAST.get(otype, otype.lower())
        return "unknown activity"
    return custom.get(otype, ACTIVITES_PAST.get(otype, otype.lower()))


@app.template_filter('isobject')
def isobject(obj:ASRef) -> bool:
    otype = activitystream.object_type(obj)
    return otype in activitystream.OBJECTS
    return otype in OBJECTS


@app.template_filter('isactivity')
def isactivity(obj:ASRef) -> bool:
    otype = activitystream.object_type(obj)
    return otype in activitystream.ACTIVITES
    return otype in ACTIVITES


@app.template_filter('isactor')
def isactor(obj:ASRef) -> bool:
    otype = activitystream.object_type(obj)
    return otype in activitystream.ACTORS
    return otype in ACTORS


@app.template_filter("enumerate")

M activist/web.py => activist/web.py +6 -6
@@ 21,7 21,7 @@ from . import activitystream
from . import activities
from .config import settings
from .utils import rawactivity_by_id, activityfy
from .consts import MIME_AS, MIME_HTML, AS_PUBLIC
from .consts import MIME_AS, MIME_HTML, AS_PUBLIC, OBJECTS, ACTORS, ACTIVITES
from . import template
from .background import Background, TaskType



@@ 139,7 139,7 @@ def get_object(uid:str):
        abort(404)

    # if obj is and activity we show the subjext of the activity
    if obj.type in activitystream.ACTIVITES:
    if obj.type in ACTIVITES:
        return render_template("object/Activity.html.j2", obj=obj, replies=True)

    return render_template("object/Object.html.j2", obj=obj, replies=True)


@@ 447,7 447,7 @@ def _public_search(searchq:str):

        query = db.Object.query().select().where(
            "data like ?", f"%{AS_PUBLIC}%",
            type = activitystream.OBJECTS,
            type = OBJECTS,
            attributedTo = settings.USER_URL
        )
        for term in terms:


@@ 496,17 496,17 @@ def _private_search(searchq:str):
                            attributedTo = actors,
                            id = actors,
                        ),
                        type = activitystream.OBJECTS + activitystream.ACTORS
                        type = OBJECTS + ACTORS
                    ).fetch()
            else:
                # we don't indicize tags yet, let's do an ugly 'like' searcg in objects data
                data = db.Object.query().select().where(
                    "UPPER(data) like ?", f"%{searchq.upper()}%",
                    type = activitystream.OBJECTS + activitystream.ACTORS
                    type = OBJECTS + ACTORS
                ).fetch()
        else:
            # dont' return activities
            query = db.Object.query().select().where(type = activitystream.OBJECTS + activitystream.ACTORS)
            query = db.Object.query().select().where(type = OBJECTS + ACTORS)
            for term in terms:
                if term.startswith("http"):
                    query.where(Or(id = term, actor = term, attributedTo = term))