From a84bd73b329e9b3d65f2a5e2b5bb65b5c9dd45f0 Mon Sep 17 00:00:00 2001 From: freamon Date: Sun, 24 Mar 2024 22:10:41 +0000 Subject: [PATCH] Return list of sticky posts for local community featured url --- app/activitypub/routes.py | 25 +++++++++++++++++++++++-- app/activitypub/util.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/app/activitypub/routes.py b/app/activitypub/routes.py index 6bd0a8cf..f1b34fd2 100644 --- a/app/activitypub/routes.py +++ b/app/activitypub/routes.py @@ -18,7 +18,7 @@ from app.activitypub.util import public_key, users_total, active_half_year, acti lemmy_site_data, instance_weight, is_activitypub_request, downvote_post_reply, downvote_post, upvote_post_reply, \ upvote_post, activity_already_ingested, delete_post_or_comment, community_members, \ user_removed_from_remote_server, create_post, create_post_reply, update_post_reply_from_activity, \ - update_post_from_activity, undo_vote, undo_downvote + update_post_from_activity, undo_vote, undo_downvote, post_to_page from app.utils import gibberish, get_setting, is_image_url, allowlist_html, html_to_markdown, render_template, \ domain_from_url, markdown_to_html, community_membership, ap_datetime, markdown_to_text, ip_address, can_downvote, \ can_upvote, can_create_post, awaken_dormant_instance, shorten_string, can_create_post_reply, sha256_digest, \ @@ -1029,6 +1029,27 @@ def community_outbox(actor): return jsonify(community_data) +@bp.route('/c//featured', methods=['GET']) +def community_featured(actor): + actor = actor.strip() + community = Community.query.filter_by(name=actor, banned=False, ap_id=None).first() + if community is not None: + posts = Post.query.filter_by(community_id=community.id, sticky=True).all() + + community_data = { + "@context": default_context(), + "type": "OrderedCollection", + "id": f"https://{current_app.config['SERVER_NAME']}/c/{actor}/featured", + "totalItems": len(posts), + "orderedItems": [] + } + + for post in posts: + community_data['orderedItems'].append(post_to_page(post, community)) + + return jsonify(community_data) + + @bp.route('/c//moderators', methods=['GET']) def community_moderators_route(actor): actor = actor.strip() @@ -1069,7 +1090,7 @@ def community_followers(actor): if community is not None: result = { "@context": default_context(), - "id": f'https://{current_app.config["SERVER_NAME"]}/c/actor/followers', + "id": f'https://{current_app.config["SERVER_NAME"]}/c/{actor}/followers', "type": "Collection", "totalItems": community_members(community.id), "items": [] diff --git a/app/activitypub/util.py b/app/activitypub/util.py index 239dedcf..71836864 100644 --- a/app/activitypub/util.py +++ b/app/activitypub/util.py @@ -178,6 +178,43 @@ def post_to_activity(post: Post, community: Community): return activity_data +def post_to_page(post: Post, community: Community): + activity_data = { + "type": "Page", + "id": post.ap_id, + "attributedTo": post.author.ap_public_url, + "to": [ + f"https://{current_app.config['SERVER_NAME']}/c/{community.name}", + "https://www.w3.org/ns/activitystreams#Public" + ], + "name": post.title, + "cc": [], + "content": post.body_html if post.body_html else '', + "mediaType": "text/html", + "source": { + "content": post.body if post.body else '', + "mediaType": "text/markdown" + }, + "attachment": [], + "commentsEnabled": post.comments_enabled, + "sensitive": post.nsfw or post.nsfl, + "published": ap_datetime(post.created_at), + "stickied": post.sticky, + "audience": f"https://{current_app.config['SERVER_NAME']}/c/{community.name}" + } + if post.edited_at is not None: + activity_data["updated"] = ap_datetime(post.edited_at) + if post.language is not None: + activity_data["language"] = {"identifier": post.language} + if post.type == POST_TYPE_LINK and post.url is not None: + activity_data["attachment"] = [{"href": post.url, "type": "Link"}] + if post.image_id is not None: + activity_data["image"] = {"url": post.image.view_url(), "type": "Image"} + if post.image.alt_text: + activity_data["image"]['altText'] = post.image.alt_text + return activity_data + + def banned_user_agents(): return [] # todo: finish this function