user_notes relationship, caching, cleanup

This commit is contained in:
Hendrik L 2024-12-11 16:33:42 +01:00
parent 148df4fcba
commit b4f1706a6b
4 changed files with 19 additions and 19 deletions

View file

@ -710,6 +710,7 @@ class User(UserMixin, db.Model):
cover = db.relationship('File', lazy='joined', foreign_keys=[cover_id], single_parent=True, cascade="all, delete-orphan")
instance = db.relationship('Instance', lazy='joined', foreign_keys=[instance_id])
conversations = db.relationship('Conversation', lazy='dynamic', secondary=conversation_member, backref=db.backref('members', lazy='joined'))
user_notes = db.relationship('UserNote', lazy='dynamic', foreign_keys="UserNote.target_id")
ap_id = db.Column(db.String(255), index=True) # e.g. username@server
ap_profile_id = db.Column(db.String(255), index=True, unique=True) # e.g. https://server/u/username
@ -1079,8 +1080,9 @@ class User(UserMixin, db.Model):
def has_read_post(self, post):
return self.read_post.filter(read_posts.c.read_post_id == post.id).count() > 0
@cache.memoize(timeout=500)
def get_note(self, by_user):
user_note = UserNote.query.filter(UserNote.target_id == self.id, UserNote.user_id == by_user.id).first()
user_note = self.user_notes.filter(UserNote.target_id == self.id, UserNote.user_id == by_user.id).first()
if user_note:
return user_note.body
else:

View file

@ -30,7 +30,7 @@
{% if current_user.is_authenticated -%}
{% set user_note = user.get_note(current_user) %}
{% if user_note -%}
<span class="user_note" title="{{ _('User note') }}">[{{ user_note | truncate(12, True) }}]</span>
<span class="user_note" title="{{ _('User note: %(note)s', note=user_note) }}">[{{ user_note | truncate(12, True) }}]</span>
{% endif -%}
{% endif -%}
{% endif -%}

View file

@ -12,9 +12,8 @@
<div class="card-body p-6">
<div class="card-title">{{ _('Edit note for "%(user_name)s"', user_name=user.display_name()) }}</div>
<div class="card-body">
<strong>{{ _('Emoji quick access') }}</strong>
<div>
<strong>Emoji quick access</strong>
<br />
<button id="thumbsup" class="emojitoggle">👍</button>
<button id="thumbsdown" class="emojitoggle">👎</button>
<button id="smile" class="emojitoggle">😄</button>
@ -23,27 +22,27 @@
<button id="red-heart" class="emojitoggle">❤️</button>
<button id="rocket" class="emojitoggle">🚀</button>
<button id="eyes" class="emojitoggle">👀</button>
<br />
</div><div>
<button id="star" class="emojitoggle"></button>
<button id="medal" class="emojitoggle">🥇</button>
<button id="check" class="emojitoggle">☑️</button>
<button id="fire" class="emojitoggle">🔥</button>
<button id="robot" class="emojitoggle">🤖</button>
<button id="ghost" class="emojitoggle">👻</button>
<button id="clown" class="emojitoggle">🤡</button>
<button id="poo" class="emojitoggle">💩</button>
<button id="check" class="emojitoggle">☑️</button>
<br />
</div><div>
<button id="speech-bubble" class="emojitoggle">💬</button>
<button id="anger-bubble" class="emojitoggle">🗯️</button>
<button id="hundred" class="emojitoggle">💯</button>
<button id="rofl" class="emojitoggle">🤣</button>
<button id="zany" class="emojitoggle">🤪</button>
<button id="warning" class="emojitoggle">⚠️</button>
<button id="no-entry" class="emojitoggle"></button>
<button id="vomit" class="emojitoggle">🤮</button>
<button id="speech-bubble" class="emojitoggle">💬</button>
<button id="anger-bubble" class="emojitoggle">🗯️</button>
<button id="rofl" class="emojitoggle">🤣</button>
<button id="zany" class="emojitoggle">🤪</button>
<button id="hundred" class="emojitoggle">💯</button>
</div>
{{ render_form(form) }}
<div class="row mt-5"><small class="field_hint">This note appears next to the username. It's just for you and not displayed to anyone else.</small></div>
<div class="row mt-5"><small class="field_hint">{{ _('This note appears next to their username. It\'s meant just for you and not displayed to anyone else.') }}</small></div>
</div>
</div>
</div>

View file

@ -1337,7 +1337,7 @@ def user_read_posts_delete():
def edit_user_note(actor):
actor = actor.strip()
if '@' in actor:
user: User = User.query.filter_by(ap_id=actor, deleted=False, banned=False).first()
user: User = User.query.filter_by(ap_id=actor, deleted=False).first()
else:
user: User = User.query.filter_by(user_name=actor, deleted=False, ap_id=None).first()
if user is None:
@ -1352,13 +1352,12 @@ def edit_user_note(actor):
usernote = UserNote(target_id=user.id, user_id=current_user.id, body=text)
db.session.add(usernote)
db.session.commit()
cache.delete_memoized(User.get_note, user, current_user)
flash(_('Your changes have been saved.'), 'success')
referrer = request.headers.get('Referer', None)
if referrer is not None:
return redirect(referrer)
else:
return redirect(url_for('user.edit_user_note', actor=actor))
goto = request.args.get('redirect') if 'redirect' in request.args else f'/u/{actor}'
return redirect(goto)
elif request.method == 'GET':
form.note.data = user.get_note(current_user)