special lemmy api endpoints for instance discovery

This commit is contained in:
rimu 2023-11-23 15:10:44 +13:00
parent 1d013693fa
commit b707251bf6
4 changed files with 1137 additions and 2 deletions

View file

@ -9,9 +9,10 @@ from app.community.routes import show_community
from app.user.routes import show_profile from app.user.routes import show_profile
from app.constants import POST_TYPE_LINK, POST_TYPE_IMAGE from app.constants import POST_TYPE_LINK, POST_TYPE_IMAGE
from app.models import User, Community, CommunityJoinRequest, CommunityMember, CommunityBan, ActivityPubLog, Post, \ from app.models import User, Community, CommunityJoinRequest, CommunityMember, CommunityBan, ActivityPubLog, Post, \
PostReply, Instance, PostVote, PostReplyVote, File PostReply, Instance, PostVote, PostReplyVote, File, AllowedInstances, BannedInstances
from app.activitypub.util import public_key, users_total, active_half_year, active_month, local_posts, local_comments, \ from app.activitypub.util import public_key, users_total, active_half_year, active_month, local_posts, local_comments, \
post_to_activity, find_actor_or_create, default_context, instance_blocked, find_reply_parent, find_liked_object post_to_activity, find_actor_or_create, default_context, instance_blocked, find_reply_parent, find_liked_object, \
lemmy_site_data
from app.utils import gibberish, get_setting, is_image_url, allowlist_html, html_to_markdown, render_template, \ from app.utils import gibberish, get_setting, is_image_url, allowlist_html, html_to_markdown, render_template, \
domain_from_url, markdown_to_html domain_from_url, markdown_to_html
import werkzeug.exceptions import werkzeug.exceptions
@ -74,6 +75,7 @@ def nodeinfo():
@bp.route('/nodeinfo/2.0') @bp.route('/nodeinfo/2.0')
@bp.route('/nodeinfo/2.0.json')
def nodeinfo2(): def nodeinfo2():
nodeinfo_data = { nodeinfo_data = {
@ -99,6 +101,37 @@ def nodeinfo2():
return jsonify(nodeinfo_data) return jsonify(nodeinfo_data)
@bp.route('/api/v3/site')
def lemmy_site():
return jsonify(lemmy_site_data())
@bp.route('/api/v3/federated_instances')
def lemmy_federated_instances():
instances = Instance.query.all()
linked = []
allowed = []
blocked = []
for instance in instances:
instance_data = {"id": instance.id, "domain": instance.domain, "published": instance.created_at.isoformat(), "updated": instance.updated_at.isoformat()}
if instance.software:
instance_data['software'] = instance.software
if instance.version:
instance_data['version'] = instance.version
linked.append(instance_data)
for instance in AllowedInstances.query.all():
allowed.append({"id": instance.id, "domain": instance.domain, "published": datetime.utcnow(), "updated": datetime.utcnow()})
for instance in BannedInstances.query.all():
blocked.append({"id": instance.id, "domain": instance.domain, "published": datetime.utcnow(), "updated": datetime.utcnow()})
return jsonify({
"federated_instances": {
"linked": linked,
"allowed": allowed,
"blocked": blocked
}
})
@bp.route('/u/<actor>', methods=['GET']) @bp.route('/u/<actor>', methods=['GET'])
def user_profile(actor): def user_profile(actor):
""" Requests to this endpoint can be for a JSON representation of the user, or a HTML rendering of their profile. """ Requests to this endpoint can be for a JSON representation of the user, or a HTML rendering of their profile.

File diff suppressed because it is too large Load diff

View file

@ -472,6 +472,10 @@ class Instance(db.Model):
shared_inbox = db.Column(db.String(256)) shared_inbox = db.Column(db.String(256))
outbox = db.Column(db.String(256)) outbox = db.Column(db.String(256))
vote_weight = db.Column(db.Float, default=1.0) vote_weight = db.Column(db.Float, default=1.0)
software = db.Column(db.String(50))
version = db.Column(db.String(50))
created_at = db.Column(db.DateTime, default=datetime.utcnow)
updated_at = db.Column(db.DateTime, default=datetime.utcnow)
class Settings(db.Model): class Settings(db.Model):

View file

@ -0,0 +1,38 @@
"""add software and dates to instance
Revision ID: 4a3ca1701711
Revises: 84a5cb2a5e5b
Create Date: 2023-11-23 14:33:06.928554
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '4a3ca1701711'
down_revision = '84a5cb2a5e5b'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('instance', schema=None) as batch_op:
batch_op.add_column(sa.Column('software', sa.String(length=50), nullable=True))
batch_op.add_column(sa.Column('version', sa.String(length=50), nullable=True))
batch_op.add_column(sa.Column('created_at', sa.DateTime(), nullable=True))
batch_op.add_column(sa.Column('updated_at', sa.DateTime(), nullable=True))
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('instance', schema=None) as batch_op:
batch_op.drop_column('updated_at')
batch_op.drop_column('created_at')
batch_op.drop_column('version')
batch_op.drop_column('software')
# ### end Alembic commands ###