comments.html: refactor so that something can be sanely
changed in it the comments.html template (along with submission.html) has numerous undesirable properties which i will describe now. unless you are very familiar with the codebase, it can be extremely difficult to grok. this is pretty insane as there is nothing fundamentally complex about the goal of comments.html: return a component that shows a username and info, reports if any, comment content, and actions a user can take. this behemeoth was initially 886 lines in the old version of this codebase, and this is with awards and a lot of other cruft removed. anyway, the maintainability of this file is about on par with some legacy application that keels over and dies if you sneeze vaguely in its direction. the nicest thing i can say about it is that it isn't currently crashing. anyway some of the problems include: * large, splittable components, are not split into separate files. this makes it incredibly difficult to find or make changes across the template and makes it nearly impossible to find or change a specific thing. this is most easily exemplified in the modals, which should by all accounts be separate templates, just inlined into comments.html. * the nesting is oftentimes incorrect. inexplicably, probably out of laziness from when the code was first written, things will end up fully left aligned, while multiple layers deep into a nesting context. if an if statement or an endif is changed, it is *incredibly* difficult to figure out where the error was. you can't trust the nesting. * multiple repeated checks for things that are always true. this is probably a symptom of the above two problems but it's very noticeable once you fix the nesting. for example there is a block near the very top of the actions bar which checks for parent_submission which effectively checks "is this in a post" (this commit won't complain about parent_submission checks but i do have opinions on those). all of the action buttons further down the chain also check for parent_submission, or even check inconsistently (by using if c.post) within this context this is a completely unnecessary check in this context. while it is potentially useful (and in fact because #251 requires we dismantle the assumption a little bit) to have these checks now, the fact that they were initially added shows that when the code was all initial written, there was little care into thinking about comment state. * mobile actions are duplicated and duplicated inline. i actually do find it probably pretty hard to support this normally given the codebase's DOM so whatever, duplicate the things, *but* if we're going to do that, inlining it into the middle of an incredibly long template is really difficult to comprehend as a design decision. ...anyway yeah this PR intends to fix these problems and enable work to be done on #251. this is a "perfect is the enemy of good" commit. it doesn't change much fundamental and is not intended to erase the sins of the original file, but at least make it maintainable. this also fixes a minor bug with #473 where the GIF modal was left in by accident.
This commit is contained in:
parent
4c6c375215
commit
9895fa1bba
20 changed files with 665 additions and 800 deletions
|
@ -1,6 +1,7 @@
|
|||
from os import environ
|
||||
import re
|
||||
import time
|
||||
from typing import Optional
|
||||
from urllib.parse import urlencode, urlparse, parse_qs
|
||||
from flask import *
|
||||
from sqlalchemy import *
|
||||
|
@ -88,7 +89,7 @@ class Comment(Base):
|
|||
|
||||
@property
|
||||
@lazy
|
||||
def top_comment(self):
|
||||
def top_comment(self) -> Optional["Comment"]:
|
||||
return g.db.query(Comment).filter_by(id=self.top_comment_id).one_or_none()
|
||||
|
||||
@lazy
|
||||
|
@ -379,20 +380,99 @@ class Comment(Base):
|
|||
@lazy
|
||||
def collapse_for_user(self, v, path):
|
||||
if v and self.author_id == v.id: return False
|
||||
|
||||
if path == '/admin/removed/comments': return False
|
||||
|
||||
if self.over_18 and not (v and v.over_18) and not (self.post and self.post.over_18): return True
|
||||
|
||||
if self.is_banned: return True
|
||||
|
||||
if v and v.filter_words and self.body and any(x in self.body for x in v.filter_words): return True
|
||||
|
||||
return False
|
||||
|
||||
@property
|
||||
@lazy
|
||||
def is_op(self): return self.author_id==self.post.author_id
|
||||
def is_op(self):
|
||||
return self.author_id == self.post.author_id
|
||||
|
||||
@property
|
||||
@lazy
|
||||
def is_comment(self) -> bool:
|
||||
'''
|
||||
Returns whether this is an actual comment (i.e. not a private message)
|
||||
'''
|
||||
return bool(self.parent_submission)
|
||||
|
||||
@property
|
||||
@lazy
|
||||
def is_message(self) -> bool:
|
||||
'''
|
||||
Returns whether this is a private message or modmail
|
||||
'''
|
||||
return not self.is_comment
|
||||
|
||||
@property
|
||||
@lazy
|
||||
def is_strict_message(self) -> bool:
|
||||
'''
|
||||
Returns whether this is a private message or modmail
|
||||
but is not a notification
|
||||
'''
|
||||
return self.is_message and not self.is_notification
|
||||
|
||||
@property
|
||||
@lazy
|
||||
def is_modmail(self) -> bool:
|
||||
'''
|
||||
Returns whether this is a modmail message
|
||||
'''
|
||||
if not self.is_message: return False
|
||||
if self.sentto == MODMAIL_ID: return True
|
||||
|
||||
top_comment: Optional["Comment"] = self.top_comment
|
||||
return bool(top_comment.sentto == MODMAIL_ID)
|
||||
|
||||
@property
|
||||
@lazy
|
||||
def is_notification(self) -> bool:
|
||||
'''
|
||||
Returns whether this is a notification
|
||||
'''
|
||||
return self.is_message and not self.sentto
|
||||
|
||||
@lazy
|
||||
def header_msg(self, v, is_notification_page:bool, reply_count:int) -> str:
|
||||
if self.post:
|
||||
post_html:str = f"<a href=\"{self.post.permalink}\">{self.post.realtitle(v)}</a>"
|
||||
if v:
|
||||
if v.id == self.author_id and reply_count:
|
||||
text = f"Comment {'Replies' if reply_count != 1 else 'Reply'}"
|
||||
elif v.id == self.post.author_id and self.level == 1:
|
||||
text = "Post Reply"
|
||||
elif self.parent_submission in v.subscribed_idlist():
|
||||
text = "Subscribed Thread"
|
||||
else:
|
||||
text = "Username Mention"
|
||||
if is_notification_page:
|
||||
return f"{text}: {post_html}"
|
||||
return post_html
|
||||
elif self.author_id in {AUTOJANNY_ID, NOTIFICATIONS_ID}:
|
||||
return "Notification"
|
||||
elif self.sentto == MODMAIL_ID:
|
||||
return "Sent to admins"
|
||||
else:
|
||||
return f"Sent to @{self.senttouser.username}"
|
||||
|
||||
@lazy
|
||||
def voted_display(self, v) -> int:
|
||||
'''
|
||||
Returns data used to modify how to show the vote buttons.
|
||||
|
||||
:returns: A number between `-2` and `1`. `-2` is returned if `v` is
|
||||
`None`. `1` is returned if the user is the comment author.
|
||||
Otherwise, a value of `-1` (downvote),` 0` (no vote or no data), or `1`
|
||||
(upvote) is returned.
|
||||
'''
|
||||
if not v: return -2
|
||||
if v.id == self.author_id: return 1
|
||||
return getattr(self, 'voted', 0)
|
||||
|
||||
@lazy
|
||||
def active_flags(self, v): return len(self.flags(v))
|
||||
def active_flags(self, v):
|
||||
return len(self.flags(v))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue