From 7966a91334b13cdf77471b7ce0c0912331fff447 Mon Sep 17 00:00:00 2001
From: rimu <3310831+rimu@users.noreply.github.com>
Date: Thu, 28 Dec 2023 21:00:26 +1300
Subject: [PATCH] matrix user id on user profiles
---
app/activitypub/routes.py | 2 +
app/activitypub/util.py | 1 +
app/models.py | 1 +
app/post/routes.py | 2 +-
app/templates/user/edit_profile.html | 2 +
app/user/forms.py | 1 +
app/user/routes.py | 2 +
.../versions/88d210da7f2b_user_matrix_id.py | 40 +++++++++++++++++++
8 files changed, 50 insertions(+), 1 deletion(-)
create mode 100644 migrations/versions/88d210da7f2b_user_matrix_id.py
diff --git a/app/activitypub/routes.py b/app/activitypub/routes.py
index abb22f2b..e101353c 100644
--- a/app/activitypub/routes.py
+++ b/app/activitypub/routes.py
@@ -181,6 +181,8 @@ def user_profile(actor):
"mediaType": "text/markdown"
}
actor_data['summary'] = markdown_to_html(user.about)
+ if user.matrix_user_id:
+ actor_data['matrixUserId'] = user.matrix_user_id
resp = jsonify(actor_data)
resp.content_type = 'application/activity+json'
return resp
diff --git a/app/activitypub/util.py b/app/activitypub/util.py
index aca2370e..4f70fc9f 100644
--- a/app/activitypub/util.py
+++ b/app/activitypub/util.py
@@ -304,6 +304,7 @@ def actor_json_to_model(activity_json, address, server):
user = User(user_name=activity_json['preferredUsername'],
email=f"{address}@{server}",
about_html=parse_summary(activity_json),
+ matrix_user_id=activity_json['matrixUserId'] if 'matrixUserId' in activity_json else '',
created=activity_json['published'] if 'published' in activity_json else utcnow(),
ap_id=f"{address}@{server}",
ap_public_url=activity_json['id'],
diff --git a/app/models.py b/app/models.py
index ad29473b..45e9988e 100644
--- a/app/models.py
+++ b/app/models.py
@@ -239,6 +239,7 @@ class User(UserMixin, db.Model):
about = db.Column(db.Text) # markdown
about_html = db.Column(db.Text) # html
keywords = db.Column(db.String(256))
+ matrix_user_id = db.Column(db.String(256))
show_nsfw = db.Column(db.Boolean, default=False)
show_nsfl = db.Column(db.Boolean, default=False)
created = db.Column(db.DateTime, default=utcnow)
diff --git a/app/post/routes.py b/app/post/routes.py
index 9e34a613..8798c0aa 100644
--- a/app/post/routes.py
+++ b/app/post/routes.py
@@ -298,7 +298,7 @@ def comment_vote(comment_id, vote_direction):
current_user.last_seen = utcnow()
db.session.commit()
- current_user.recalculate_attitude(vote_direction)
+ current_user.recalculate_attitude()
db.session.commit()
comment.post.flush_cache()
diff --git a/app/templates/user/edit_profile.html b/app/templates/user/edit_profile.html
index 145b714a..5cd2c215 100644
--- a/app/templates/user/edit_profile.html
+++ b/app/templates/user/edit_profile.html
@@ -19,6 +19,8 @@
{{ render_field(form.password_field) }}
{{ render_field(form.about) }}
+ {{ render_field(form.matrix_user_id) }}
+ e.g. @something:matrix.org. Include leading @ and use : before server
{{ render_field(form.profile_file) }}
Provide a square image that looks good when small.
{{ render_field(form.banner_file) }}
diff --git a/app/user/forms.py b/app/user/forms.py
index 54c67e3a..cda64d91 100644
--- a/app/user/forms.py
+++ b/app/user/forms.py
@@ -11,6 +11,7 @@ class ProfileForm(FlaskForm):
password_field = PasswordField(_l('Set new password'), validators=[Optional(), Length(min=1, max=50)],
render_kw={"autocomplete": 'Off'})
about = TextAreaField(_l('Bio'), validators=[Optional(), Length(min=3, max=5000)])
+ matrix_user_id = StringField(_l('Matrix User ID'), validators=[Optional(), Length(max=255)])
profile_file = FileField(_('Avatar image'))
banner_file = FileField(_('Top banner image'))
bot = BooleanField(_l('This profile is a bot'))
diff --git a/app/user/routes.py b/app/user/routes.py
index a47cc9b9..2a6d1b3c 100644
--- a/app/user/routes.py
+++ b/app/user/routes.py
@@ -69,6 +69,7 @@ def edit_profile(actor):
current_user.set_password(form.password_field.data)
current_user.about = form.about.data
current_user.about_html = markdown_to_html(form.about.data)
+ current_user.matrix_user_id = form.matrix_user_id.data
current_user.bot = form.bot.data
profile_file = request.files['profile_file']
if profile_file and profile_file.filename != '':
@@ -89,6 +90,7 @@ def edit_profile(actor):
elif request.method == 'GET':
form.email.data = current_user.email
form.about.data = current_user.about
+ form.matrix_user_id.data = current_user.matrix_user_id
form.password_field.data = ''
return render_template('user/edit_profile.html', title=_('Edit profile'), form=form, user=current_user)
diff --git a/migrations/versions/88d210da7f2b_user_matrix_id.py b/migrations/versions/88d210da7f2b_user_matrix_id.py
new file mode 100644
index 00000000..9956bc20
--- /dev/null
+++ b/migrations/versions/88d210da7f2b_user_matrix_id.py
@@ -0,0 +1,40 @@
+"""user matrix id
+
+Revision ID: 88d210da7f2b
+Revises: e8113bc01e3a
+Create Date: 2023-12-28 20:51:08.605373
+
+"""
+from alembic import op
+import sqlalchemy as sa
+
+
+# revision identifiers, used by Alembic.
+revision = '88d210da7f2b'
+down_revision = 'e8113bc01e3a'
+branch_labels = None
+depends_on = None
+
+
+def upgrade():
+ # ### commands auto generated by Alembic - please adjust! ###
+ with op.batch_alter_table('report', schema=None) as batch_op:
+ batch_op.drop_constraint('report_suspect_reply_id_fkey', type_='foreignkey')
+ batch_op.drop_column('suspect_reply_id')
+
+ with op.batch_alter_table('user', schema=None) as batch_op:
+ batch_op.add_column(sa.Column('matrix_user_id', sa.String(length=256), nullable=True))
+
+ # ### end Alembic commands ###
+
+
+def downgrade():
+ # ### commands auto generated by Alembic - please adjust! ###
+ with op.batch_alter_table('user', schema=None) as batch_op:
+ batch_op.drop_column('matrix_user_id')
+
+ with op.batch_alter_table('report', schema=None) as batch_op:
+ batch_op.add_column(sa.Column('suspect_reply_id', sa.INTEGER(), autoincrement=False, nullable=True))
+ batch_op.create_foreign_key('report_suspect_reply_id_fkey', 'post_reply', ['suspect_reply_id'], ['id'])
+
+ # ### end Alembic commands ###