2021-10-18 20:46:57 +00:00
|
|
|
from os import environ
|
2021-08-15 19:17:19 +00:00
|
|
|
import re
|
2021-10-18 20:46:57 +00:00
|
|
|
import time
|
2021-08-15 19:17:19 +00:00
|
|
|
from urllib.parse import urlencode, urlparse, parse_qs
|
2021-07-21 01:12:26 +00:00
|
|
|
from flask import *
|
|
|
|
from sqlalchemy import *
|
2021-11-07 13:37:26 +00:00
|
|
|
from sqlalchemy.orm import relationship
|
2021-10-18 20:46:57 +00:00
|
|
|
from files.__main__ import Base
|
2021-10-05 23:11:17 +00:00
|
|
|
from files.classes.votes import CommentVote
|
2022-01-19 09:07:16 +00:00
|
|
|
from files.helpers.const import *
|
2022-06-24 14:30:59 +00:00
|
|
|
from files.helpers.regex import *
|
2022-06-24 15:35:55 +00:00
|
|
|
from files.helpers.regex import *
|
2021-08-04 15:35:10 +00:00
|
|
|
from files.helpers.lazy import lazy
|
2021-07-31 13:55:49 +00:00
|
|
|
from .flags import CommentFlag
|
2022-02-07 11:39:26 +00:00
|
|
|
from .votes import CommentVote
|
2022-06-26 04:57:31 +00:00
|
|
|
from .saves import CommentSaveRelationship
|
|
|
|
from random import randint
|
2022-03-06 03:40:23 +00:00
|
|
|
from math import floor
|
2021-09-12 06:12:44 +00:00
|
|
|
|
2022-06-22 19:50:20 +00:00
|
|
|
|
2022-06-23 15:47:57 +00:00
|
|
|
def normalize_urls_runtime(body, v):
|
|
|
|
|
|
|
|
if v:
|
|
|
|
body = body.replace("https://old.reddit.com/r/", f'https://{v.reddit}/r/')
|
|
|
|
|
|
|
|
if v.nitter: body = twitter_to_nitter_regex.sub(r'https://nitter.net/\1', body)
|
|
|
|
|
|
|
|
return body
|
|
|
|
|
2022-06-22 19:50:20 +00:00
|
|
|
def sort_comments(sort, comments):
|
|
|
|
|
|
|
|
if sort == 'new':
|
2022-06-22 20:30:45 +00:00
|
|
|
return comments.order_by(Comment.id.desc())
|
2022-06-22 19:50:20 +00:00
|
|
|
elif sort == 'old':
|
2022-06-22 20:30:45 +00:00
|
|
|
return comments.order_by(Comment.id)
|
2022-06-22 19:50:20 +00:00
|
|
|
elif sort == 'controversial':
|
2022-06-22 20:30:45 +00:00
|
|
|
return comments.order_by((Comment.upvotes+1)/(Comment.downvotes+1) + (Comment.downvotes+1)/(Comment.upvotes+1), Comment.downvotes.desc(), Comment.id.desc())
|
2022-06-22 19:50:20 +00:00
|
|
|
elif sort == "bottom":
|
2022-06-22 20:30:45 +00:00
|
|
|
return comments.order_by(Comment.realupvotes, Comment.id.desc())
|
2022-06-22 19:50:20 +00:00
|
|
|
else:
|
2022-06-22 20:30:45 +00:00
|
|
|
return comments.order_by(Comment.realupvotes.desc(), Comment.id.desc())
|
2022-06-22 19:50:20 +00:00
|
|
|
|
|
|
|
|
2021-09-19 18:22:57 +00:00
|
|
|
class Comment(Base):
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
__tablename__ = "comments"
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
author_id = Column(Integer, ForeignKey("users.id"))
|
|
|
|
parent_submission = Column(Integer, ForeignKey("submissions.id"))
|
2022-02-13 01:08:12 +00:00
|
|
|
created_utc = Column(Integer)
|
2021-07-21 01:12:26 +00:00
|
|
|
edited_utc = Column(Integer, default=0)
|
|
|
|
is_banned = Column(Boolean, default=False)
|
2022-02-15 22:54:17 +00:00
|
|
|
ghost = Column(Boolean, default=False)
|
2022-06-25 00:11:00 +00:00
|
|
|
bannedfor = Column(String)
|
2021-07-21 01:12:26 +00:00
|
|
|
distinguish_level = Column(Integer, default=0)
|
|
|
|
deleted_utc = Column(Integer, default=0)
|
2022-02-13 11:02:44 +00:00
|
|
|
is_approved = Column(Integer, ForeignKey("users.id"))
|
2022-04-04 01:41:20 +00:00
|
|
|
level = Column(Integer, default=1)
|
2021-07-21 01:12:26 +00:00
|
|
|
parent_comment_id = Column(Integer, ForeignKey("comments.id"))
|
2021-12-05 02:21:38 +00:00
|
|
|
top_comment_id = Column(Integer)
|
2021-07-21 01:12:26 +00:00
|
|
|
over_18 = Column(Boolean, default=False)
|
|
|
|
is_bot = Column(Boolean, default=False)
|
2022-05-26 23:08:23 +00:00
|
|
|
stickied = Column(String)
|
|
|
|
stickied_utc = Column(Integer)
|
2022-02-12 23:10:29 +00:00
|
|
|
sentto = Column(Integer, ForeignKey("users.id"))
|
2021-07-25 23:49:53 +00:00
|
|
|
app_id = Column(Integer, ForeignKey("oauth_apps.id"))
|
2021-10-29 16:22:51 +00:00
|
|
|
upvotes = Column(Integer, default=1)
|
2021-09-24 02:21:41 +00:00
|
|
|
downvotes = Column(Integer, default=0)
|
2021-11-30 14:18:16 +00:00
|
|
|
realupvotes = Column(Integer, default=1)
|
2021-11-06 17:30:39 +00:00
|
|
|
body = Column(String)
|
|
|
|
body_html = Column(String)
|
2021-10-06 22:38:15 +00:00
|
|
|
ban_reason = Column(String)
|
2022-01-22 13:04:30 +00:00
|
|
|
slots_result = Column(String)
|
2022-01-28 08:00:15 +00:00
|
|
|
blackjack_result = Column(String)
|
2022-02-14 01:39:48 +00:00
|
|
|
wordle_result = Column(String)
|
2022-01-29 04:48:30 +00:00
|
|
|
treasure_amount = Column(String)
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2022-02-12 23:10:29 +00:00
|
|
|
oauth_app = relationship("OauthApp", viewonly=True)
|
2021-09-22 14:51:36 +00:00
|
|
|
post = relationship("Submission", viewonly=True)
|
2021-09-22 15:01:18 +00:00
|
|
|
author = relationship("User", primaryjoin="User.id==Comment.author_id")
|
2021-10-06 04:57:45 +00:00
|
|
|
senttouser = relationship("User", primaryjoin="User.id==Comment.sentto", viewonly=True)
|
2021-09-22 14:51:36 +00:00
|
|
|
parent_comment = relationship("Comment", remote_side=[id], viewonly=True)
|
2022-04-03 23:44:54 +00:00
|
|
|
child_comments = relationship("Comment", lazy="dynamic", remote_side=[parent_comment_id], viewonly=True)
|
2022-06-25 20:33:51 +00:00
|
|
|
awards = relationship("AwardRelationship", order_by="AwardRelationship.awarded_utc.desc()", viewonly=True)
|
2021-12-17 17:55:11 +00:00
|
|
|
reports = relationship("CommentFlag", viewonly=True)
|
2021-10-20 14:37:46 +00:00
|
|
|
|
2021-07-21 01:12:26 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
2022-03-01 00:06:50 +00:00
|
|
|
if "created_utc" not in kwargs:
|
|
|
|
kwargs["created_utc"] = int(time.time())
|
2021-07-21 01:12:26 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
|
|
|
|
return f"<Comment(id={self.id})>"
|
|
|
|
|
2022-01-11 04:30:08 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def top_comment(self):
|
2022-06-18 00:57:23 +00:00
|
|
|
return g.db.get(Comment, self.top_comment_id)
|
2022-01-11 04:30:08 +00:00
|
|
|
|
2021-11-05 21:30:54 +00:00
|
|
|
@lazy
|
2022-04-15 16:28:08 +00:00
|
|
|
def flags(self, v):
|
|
|
|
flags = g.db.query(CommentFlag).filter_by(comment_id=self.id).order_by(CommentFlag.created_utc).all()
|
2022-06-12 06:11:59 +00:00
|
|
|
if not (v and (v.shadowbanned or v.admin_level >= 2)):
|
2022-04-15 16:28:08 +00:00
|
|
|
for flag in flags:
|
|
|
|
if flag.user.shadowbanned:
|
|
|
|
flags.remove(flag)
|
|
|
|
return flags
|
2021-11-05 21:30:54 +00:00
|
|
|
|
2022-06-26 01:12:31 +00:00
|
|
|
@lazy
|
|
|
|
def poll_voted(self, v):
|
|
|
|
if v:
|
|
|
|
vote = g.db.query(CommentVote.vote_type).filter_by(user_id=v.id, comment_id=self.id).one_or_none()
|
|
|
|
if vote: return vote[0]
|
|
|
|
return None
|
|
|
|
|
2021-10-07 04:06:21 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def options(self):
|
2022-05-25 17:01:29 +00:00
|
|
|
return self.child_comments.filter_by(author_id=AUTOPOLLER_ID).order_by(Comment.id).all()
|
2021-10-07 04:06:21 +00:00
|
|
|
|
2022-02-07 11:39:26 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def choices(self):
|
2022-05-25 17:01:29 +00:00
|
|
|
return self.child_comments.filter_by(author_id=AUTOCHOICE_ID).order_by(Comment.id).all()
|
2022-03-04 21:23:33 +00:00
|
|
|
|
2022-06-23 16:36:39 +00:00
|
|
|
@lazy
|
2021-11-05 20:10:51 +00:00
|
|
|
def total_poll_voted(self, v):
|
|
|
|
if v:
|
|
|
|
for option in self.options:
|
2022-06-26 01:12:31 +00:00
|
|
|
if option.poll_voted(v): return True
|
2021-11-05 20:10:51 +00:00
|
|
|
return False
|
|
|
|
|
2022-06-23 16:36:39 +00:00
|
|
|
@lazy
|
2022-02-07 11:39:26 +00:00
|
|
|
def total_choice_voted(self, v):
|
|
|
|
if v:
|
2022-06-26 01:25:17 +00:00
|
|
|
return g.db.query(CommentVote.comment_id).filter(CommentVote.user_id == v.id, CommentVote.comment_id.in_([x.id for x in self.choices])).first()
|
2022-02-07 11:39:26 +00:00
|
|
|
return False
|
|
|
|
|
2021-12-27 02:09:06 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def controversial(self):
|
|
|
|
if self.downvotes > 5 and 0.25 < self.upvotes / self.downvotes < 4: return True
|
|
|
|
return False
|
|
|
|
|
2021-09-19 18:22:57 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def created_datetime(self):
|
|
|
|
return str(time.strftime("%d/%B/%Y %H:%M:%S UTC", time.gmtime(self.created_utc)))
|
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def age_string(self):
|
2022-03-04 23:49:38 +00:00
|
|
|
notif_utc = self.__dict__.get("notif_utc")
|
2022-03-04 17:33:58 +00:00
|
|
|
|
|
|
|
if notif_utc: timestamp = notif_utc
|
2022-03-04 16:53:28 +00:00
|
|
|
elif self.created_utc: timestamp = self.created_utc
|
2022-03-02 00:05:30 +00:00
|
|
|
else: return None
|
2021-12-20 20:03:59 +00:00
|
|
|
|
2022-03-02 00:05:30 +00:00
|
|
|
age = int(time.time()) - timestamp
|
2021-09-19 18:22:57 +00:00
|
|
|
|
|
|
|
if age < 60:
|
|
|
|
return "just now"
|
|
|
|
elif age < 3600:
|
|
|
|
minutes = int(age / 60)
|
|
|
|
return f"{minutes}m ago"
|
|
|
|
elif age < 86400:
|
|
|
|
hours = int(age / 3600)
|
|
|
|
return f"{hours}hr ago"
|
|
|
|
elif age < 2678400:
|
|
|
|
days = int(age / 86400)
|
|
|
|
return f"{days}d ago"
|
|
|
|
|
|
|
|
now = time.gmtime()
|
2022-03-02 00:05:30 +00:00
|
|
|
ctd = time.gmtime(timestamp)
|
2021-09-19 18:22:57 +00:00
|
|
|
|
|
|
|
months = now.tm_mon - ctd.tm_mon + 12 * (now.tm_year - ctd.tm_year)
|
|
|
|
if now.tm_mday < ctd.tm_mday:
|
|
|
|
months -= 1
|
|
|
|
|
|
|
|
if months < 12:
|
|
|
|
return f"{months}mo ago"
|
|
|
|
else:
|
|
|
|
years = int(months / 12)
|
|
|
|
return f"{years}yr ago"
|
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def edited_string(self):
|
|
|
|
|
|
|
|
age = int(time.time()) - self.edited_utc
|
|
|
|
|
|
|
|
if age < 60:
|
|
|
|
return "just now"
|
|
|
|
elif age < 3600:
|
|
|
|
minutes = int(age / 60)
|
|
|
|
return f"{minutes}m ago"
|
|
|
|
elif age < 86400:
|
|
|
|
hours = int(age / 3600)
|
|
|
|
return f"{hours}hr ago"
|
|
|
|
elif age < 2678400:
|
|
|
|
days = int(age / 86400)
|
|
|
|
return f"{days}d ago"
|
|
|
|
|
|
|
|
now = time.gmtime()
|
|
|
|
ctd = time.gmtime(self.edited_utc)
|
2021-12-02 20:06:55 +00:00
|
|
|
|
2021-09-19 18:22:57 +00:00
|
|
|
months = now.tm_mon - ctd.tm_mon + 12 * (now.tm_year - ctd.tm_year)
|
2021-12-02 20:06:55 +00:00
|
|
|
if now.tm_mday < ctd.tm_mday:
|
|
|
|
months -= 1
|
2021-09-19 18:22:57 +00:00
|
|
|
|
|
|
|
if months < 12:
|
|
|
|
return f"{months}mo ago"
|
|
|
|
else:
|
2021-12-02 20:06:55 +00:00
|
|
|
years = int(months / 12)
|
2021-09-19 18:22:57 +00:00
|
|
|
return f"{years}yr ago"
|
|
|
|
|
2022-02-05 17:51:42 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def score(self):
|
|
|
|
return self.upvotes - self.downvotes
|
2021-09-23 19:22:18 +00:00
|
|
|
|
2021-07-25 23:23:21 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def fullname(self):
|
2021-07-30 05:31:38 +00:00
|
|
|
return f"t3_{self.id}"
|
2021-07-25 23:23:21 +00:00
|
|
|
|
2021-07-21 01:12:26 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def parent(self):
|
|
|
|
|
2021-07-27 00:05:58 +00:00
|
|
|
if not self.parent_submission: return None
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2021-07-27 00:05:58 +00:00
|
|
|
if self.level == 1: return self.post
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2022-06-18 00:57:23 +00:00
|
|
|
else: return g.db.get(Comment, self.parent_comment_id)
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2022-06-24 19:26:18 +00:00
|
|
|
@property
|
2021-07-30 05:23:17 +00:00
|
|
|
@lazy
|
|
|
|
def parent_fullname(self):
|
2021-07-30 05:34:04 +00:00
|
|
|
if self.parent_comment_id: return f"t3_{self.parent_comment_id}"
|
|
|
|
elif self.parent_submission: return f"t2_{self.parent_submission}"
|
2021-07-30 05:23:17 +00:00
|
|
|
|
2022-06-22 19:50:20 +00:00
|
|
|
@lazy
|
2022-06-22 20:42:19 +00:00
|
|
|
def replies(self, sort=None):
|
2022-02-17 06:32:04 +00:00
|
|
|
if self.replies2 != None: return [x for x in self.replies2 if not x.author.shadowbanned]
|
2022-04-16 16:41:38 +00:00
|
|
|
if not self.parent_submission:
|
2022-05-25 17:09:26 +00:00
|
|
|
return [x for x in self.child_comments.order_by(Comment.id) if not x.author.shadowbanned]
|
2022-06-22 19:50:20 +00:00
|
|
|
|
2022-06-26 01:22:05 +00:00
|
|
|
comments = self.child_comments.filter(Comment.author_id.notin_(poll_bots))
|
2022-06-22 19:50:20 +00:00
|
|
|
comments = sort_comments(sort, comments)
|
|
|
|
return [x for x in comments if not x.author.shadowbanned]
|
2022-05-25 17:09:26 +00:00
|
|
|
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2022-06-22 19:50:20 +00:00
|
|
|
@lazy
|
|
|
|
def replies3(self, sort):
|
2022-02-04 05:42:24 +00:00
|
|
|
if self.replies2 != None: return self.replies2
|
2022-04-16 16:44:32 +00:00
|
|
|
if not self.parent_submission:
|
2022-05-25 17:01:29 +00:00
|
|
|
return self.child_comments.order_by(Comment.id).all()
|
2022-06-22 19:50:20 +00:00
|
|
|
|
2022-06-26 01:22:05 +00:00
|
|
|
comments = self.child_comments.filter(Comment.author_id.notin_(poll_bots))
|
2022-06-22 22:25:10 +00:00
|
|
|
return sort_comments(sort, comments).all()
|
2022-05-25 17:01:29 +00:00
|
|
|
|
2022-02-04 05:42:24 +00:00
|
|
|
|
2021-07-21 01:12:26 +00:00
|
|
|
@property
|
|
|
|
def replies2(self):
|
2022-03-04 23:49:38 +00:00
|
|
|
return self.__dict__.get("replies2")
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
@replies2.setter
|
|
|
|
def replies2(self, value):
|
|
|
|
self.__dict__["replies2"] = value
|
|
|
|
|
2021-09-12 06:12:44 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def shortlink(self):
|
2022-02-24 13:20:48 +00:00
|
|
|
return f"{self.post.shortlink}/{self.id}?context=8#context"
|
2021-09-12 06:12:44 +00:00
|
|
|
|
2022-01-28 21:44:32 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
2022-02-24 13:20:48 +00:00
|
|
|
def permalink(self):
|
|
|
|
return f"{SITE_FULL}{self.shortlink}"
|
2022-01-28 21:44:32 +00:00
|
|
|
|
2022-01-17 21:26:03 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
2022-02-24 13:20:48 +00:00
|
|
|
def morecomments(self):
|
|
|
|
return f"{self.post.permalink}/{self.id}#context"
|
2022-01-17 21:26:03 +00:00
|
|
|
|
2021-07-21 01:12:26 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
2022-02-17 07:12:38 +00:00
|
|
|
def author_name(self):
|
|
|
|
if self.ghost: return '👻'
|
|
|
|
else: return self.author.username
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
@property
|
2021-09-19 18:25:40 +00:00
|
|
|
@lazy
|
2021-07-21 01:12:26 +00:00
|
|
|
def json_raw(self):
|
2021-07-28 03:55:47 +00:00
|
|
|
flags = {}
|
2022-04-15 16:28:08 +00:00
|
|
|
for f in self.flags(None): flags[f.user.username] = f.reason
|
2021-07-28 03:55:47 +00:00
|
|
|
|
2021-07-21 01:12:26 +00:00
|
|
|
data= {
|
2021-07-29 05:31:57 +00:00
|
|
|
'id': self.id,
|
2021-07-21 01:12:26 +00:00
|
|
|
'level': self.level,
|
2022-01-17 23:45:23 +00:00
|
|
|
'author_name': self.author_name,
|
2021-07-21 01:12:26 +00:00
|
|
|
'body': self.body,
|
|
|
|
'body_html': self.body_html,
|
|
|
|
'is_bot': self.is_bot,
|
|
|
|
'created_utc': self.created_utc,
|
|
|
|
'edited_utc': self.edited_utc or 0,
|
|
|
|
'is_banned': bool(self.is_banned),
|
2021-07-25 21:59:20 +00:00
|
|
|
'deleted_utc': self.deleted_utc,
|
2021-07-21 01:12:26 +00:00
|
|
|
'is_nsfw': self.over_18,
|
2022-03-01 16:50:39 +00:00
|
|
|
'permalink': f'/comment/{self.id}',
|
2022-05-26 23:08:23 +00:00
|
|
|
'stickied': self.stickied,
|
2021-07-28 03:29:06 +00:00
|
|
|
'distinguish_level': self.distinguish_level,
|
2021-12-14 00:51:04 +00:00
|
|
|
'post_id': self.post.id if self.post else 0,
|
2021-09-18 19:45:17 +00:00
|
|
|
'score': self.score,
|
|
|
|
'upvotes': self.upvotes,
|
|
|
|
'downvotes': self.downvotes,
|
2021-07-28 03:39:58 +00:00
|
|
|
'is_bot': self.is_bot,
|
2021-07-28 03:55:47 +00:00
|
|
|
'flags': flags,
|
2021-07-21 01:12:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if self.ban_reason:
|
|
|
|
data["ban_reason"]=self.ban_reason
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
2022-06-23 16:36:39 +00:00
|
|
|
@lazy
|
2022-01-23 23:06:34 +00:00
|
|
|
def award_count(self, kind):
|
2021-08-23 11:05:56 +00:00
|
|
|
return len([x for x in self.awards if x.kind == kind])
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
@property
|
2021-09-19 18:25:40 +00:00
|
|
|
@lazy
|
2021-07-21 01:12:26 +00:00
|
|
|
def json_core(self):
|
|
|
|
if self.is_banned:
|
|
|
|
data= {'is_banned': True,
|
|
|
|
'ban_reason': self.ban_reason,
|
2021-07-30 05:31:38 +00:00
|
|
|
'id': self.id,
|
2021-12-14 18:35:56 +00:00
|
|
|
'post': self.post.id if self.post else 0,
|
2021-07-21 01:12:26 +00:00
|
|
|
'level': self.level,
|
2021-07-25 23:23:21 +00:00
|
|
|
'parent': self.parent_fullname
|
2021-07-21 01:12:26 +00:00
|
|
|
}
|
2022-01-12 01:19:13 +00:00
|
|
|
elif self.deleted_utc:
|
2021-07-21 01:12:26 +00:00
|
|
|
data= {'deleted_utc': self.deleted_utc,
|
2021-07-30 05:31:38 +00:00
|
|
|
'id': self.id,
|
2021-12-14 18:35:56 +00:00
|
|
|
'post': self.post.id if self.post else 0,
|
2021-07-21 01:12:26 +00:00
|
|
|
'level': self.level,
|
2021-07-25 23:23:21 +00:00
|
|
|
'parent': self.parent_fullname
|
2021-07-21 01:12:26 +00:00
|
|
|
}
|
|
|
|
else:
|
|
|
|
|
|
|
|
data=self.json_raw
|
|
|
|
|
2022-01-07 21:03:14 +00:00
|
|
|
if self.level>=2: data['parent_comment_id']= self.parent_comment_id
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
if "replies" in self.__dict__:
|
2022-06-22 20:42:19 +00:00
|
|
|
data['replies']=[x.json_core for x in self.replies(None)]
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
@property
|
|
|
|
def json(self):
|
|
|
|
|
|
|
|
data=self.json_core
|
|
|
|
|
2022-01-12 01:19:13 +00:00
|
|
|
if self.deleted_utc or self.is_banned:
|
2021-07-21 01:12:26 +00:00
|
|
|
return data
|
|
|
|
|
2022-01-22 10:14:15 +00:00
|
|
|
data["author"]='👻' if self.ghost else self.author.json_core
|
2021-12-14 02:07:50 +00:00
|
|
|
data["post"]=self.post.json_core if self.post else ''
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
if self.level >= 2:
|
|
|
|
data["parent"]=self.parent.json_core
|
|
|
|
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
2022-06-23 16:36:39 +00:00
|
|
|
@lazy
|
2021-07-21 01:12:26 +00:00
|
|
|
def realbody(self, v):
|
2022-01-19 09:07:16 +00:00
|
|
|
if self.post and self.post.club and not (v and (v.paid_dues or v.id in [self.author_id, self.post.author_id])): return f"<p>{CC} ONLY</p>"
|
2021-09-24 03:00:54 +00:00
|
|
|
|
2022-02-08 20:05:06 +00:00
|
|
|
body = self.body_html or ""
|
2021-08-21 11:06:28 +00:00
|
|
|
|
2022-02-07 15:40:08 +00:00
|
|
|
if body:
|
|
|
|
body = censor_slurs(body, v)
|
|
|
|
|
2022-06-23 15:47:57 +00:00
|
|
|
body = normalize_urls_runtime(body, v)
|
2022-02-07 15:40:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
if v and v.controversial:
|
2022-02-28 23:30:44 +00:00
|
|
|
captured = []
|
2022-02-27 22:05:51 +00:00
|
|
|
for i in controversial_regex.finditer(body):
|
2022-06-23 20:02:40 +00:00
|
|
|
if i.group(1) in captured: continue
|
|
|
|
captured.append(i.group(1))
|
2022-02-28 23:30:44 +00:00
|
|
|
|
2022-02-07 15:40:08 +00:00
|
|
|
url = i.group(1)
|
|
|
|
p = urlparse(url).query
|
|
|
|
p = parse_qs(p)
|
|
|
|
|
|
|
|
if 'sort' not in p: p['sort'] = ['controversial']
|
|
|
|
|
|
|
|
url_noquery = url.split('?')[0]
|
|
|
|
body = body.replace(url, f"{url_noquery}?{urlencode(p, True)}")
|
|
|
|
|
|
|
|
if v and v.shadowbanned and v.id == self.author_id and 86400 > time.time() - self.created_utc > 60:
|
|
|
|
ti = max(int((time.time() - self.created_utc)/60), 1)
|
2022-02-26 22:44:42 +00:00
|
|
|
maxupvotes = min(ti, 13)
|
2022-02-07 15:40:08 +00:00
|
|
|
rand = randint(0, maxupvotes)
|
|
|
|
if self.upvotes < rand:
|
2022-02-26 22:44:42 +00:00
|
|
|
amount = randint(0, 3)
|
2022-02-14 02:47:28 +00:00
|
|
|
if amount == 1:
|
|
|
|
self.upvotes += amount
|
|
|
|
g.db.add(self)
|
2021-11-13 21:07:59 +00:00
|
|
|
|
2022-02-07 11:39:26 +00:00
|
|
|
for c in self.options:
|
|
|
|
body += f'<div class="custom-control"><input type="checkbox" class="custom-control-input" id="{c.id}" name="option"'
|
2022-06-26 01:12:31 +00:00
|
|
|
if c.poll_voted(v): body += " checked"
|
2022-02-07 11:39:26 +00:00
|
|
|
if v: body += f''' onchange="poll_vote('{c.id}', '{self.id}')"'''
|
|
|
|
else: body += f''' onchange="poll_vote_no_v('{c.id}', '{self.id}')"'''
|
|
|
|
body += f'''><label class="custom-control-label" for="{c.id}">{c.body_html}<span class="presult-{self.id}'''
|
2022-06-11 09:56:16 +00:00
|
|
|
if not self.total_poll_voted(v): body += ' d-none'
|
2022-02-07 11:39:26 +00:00
|
|
|
body += f'"> - <a href="/votes?link=t3_{c.id}"><span id="poll-{c.id}">{c.upvotes}</span> votes</a></span></label></div>'
|
|
|
|
|
2022-05-04 03:33:00 +00:00
|
|
|
if self.choices:
|
|
|
|
curr = self.total_choice_voted(v)
|
2022-06-26 01:25:17 +00:00
|
|
|
if curr: curr = " value=" + str(curr.comment_id)
|
2022-05-04 03:33:00 +00:00
|
|
|
else: curr = ''
|
|
|
|
body += f'<input class="d-none" id="current-{self.id}"{curr}>'
|
2022-02-07 11:39:26 +00:00
|
|
|
|
|
|
|
for c in self.choices:
|
2022-02-07 12:55:37 +00:00
|
|
|
body += f'''<div class="custom-control"><input name="choice-{self.id}" autocomplete="off" class="custom-control-input" type="radio" id="{c.id}" onchange="choice_vote('{c.id}','{self.id}')"'''
|
2022-06-26 01:12:31 +00:00
|
|
|
if c.poll_voted(v): body += " checked "
|
2022-02-07 11:39:26 +00:00
|
|
|
body += f'''><label class="custom-control-label" for="{c.id}">{c.body_html}<span class="presult-{self.id}'''
|
2022-06-11 09:56:16 +00:00
|
|
|
if not self.total_choice_voted(v): body += ' d-none'
|
2022-02-07 11:39:26 +00:00
|
|
|
body += f'"> - <a href="/votes?link=t3_{c.id}"><span id="choice-{c.id}">{c.upvotes}</span> votes</a></span></label></div>'
|
2022-01-22 16:22:31 +00:00
|
|
|
|
2022-03-29 18:23:40 +00:00
|
|
|
if self.author.sig_html and (self.author_id == MOOSE_ID or (not self.ghost and not (v and v.sigs_disabled))):
|
2022-01-22 16:41:48 +00:00
|
|
|
body += f"<hr>{self.author.sig_html}"
|
2022-01-22 16:22:31 +00:00
|
|
|
|
2021-07-21 01:12:26 +00:00
|
|
|
return body
|
|
|
|
|
2022-06-23 16:36:39 +00:00
|
|
|
@lazy
|
2021-10-02 20:58:14 +00:00
|
|
|
def plainbody(self, v):
|
2022-01-19 09:07:16 +00:00
|
|
|
if self.post and self.post.club and not (v and (v.paid_dues or v.id in [self.author_id, self.post.author_id])): return f"<p>{CC} ONLY</p>"
|
2021-10-02 20:58:14 +00:00
|
|
|
|
|
|
|
body = self.body
|
|
|
|
|
|
|
|
if not body: return ""
|
|
|
|
|
2022-02-27 22:05:51 +00:00
|
|
|
return censor_slurs(body, v)
|
2021-10-02 20:58:14 +00:00
|
|
|
|
2021-09-19 20:06:52 +00:00
|
|
|
@lazy
|
2022-01-31 04:17:27 +00:00
|
|
|
def collapse_for_user(self, v, path):
|
2022-01-31 04:22:14 +00:00
|
|
|
if v and self.author_id == v.id: return False
|
2022-01-31 04:20:59 +00:00
|
|
|
|
2022-01-31 04:17:27 +00:00
|
|
|
if path == '/admin/removed/comments': return False
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2022-06-22 16:25:11 +00:00
|
|
|
if f'/{self.id}' in path: return False
|
|
|
|
|
2021-11-03 14:03:26 +00:00
|
|
|
if self.over_18 and not (v and v.over_18) and not (self.post and self.post.over_18): return True
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2021-11-12 14:06:22 +00:00
|
|
|
if self.is_banned: return True
|
2022-01-31 22:13:16 +00:00
|
|
|
|
2022-06-22 16:25:11 +00:00
|
|
|
if (self.slots_result or self.blackjack_result or self.wordle_result) and (not self.body or len(self.body_html) <= 100) and 9 > self.level > 1: return True
|
2022-01-31 22:13:16 +00:00
|
|
|
|
|
|
|
if v and v.filter_words and self.body and any(x in self.body for x in v.filter_words): return True
|
2022-01-31 04:04:08 +00:00
|
|
|
|
2021-07-21 01:12:26 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
2021-07-31 13:55:49 +00:00
|
|
|
def is_op(self): return self.author_id==self.post.author_id
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2021-07-27 00:16:30 +00:00
|
|
|
@lazy
|
2022-04-15 16:28:08 +00:00
|
|
|
def active_flags(self, v): return len(self.flags(v))
|
|
|
|
|
2022-03-06 03:25:23 +00:00
|
|
|
@lazy
|
|
|
|
def wordle_html(self, v):
|
|
|
|
if not self.wordle_result: return ''
|
|
|
|
|
|
|
|
split_wordle_result = self.wordle_result.split('_')
|
|
|
|
wordle_guesses = split_wordle_result[0]
|
|
|
|
wordle_status = split_wordle_result[1]
|
|
|
|
wordle_answer = split_wordle_result[2]
|
|
|
|
|
|
|
|
body = f"<span id='wordle-{self.id}' class='ml-2'><small>{wordle_guesses}</small>"
|
|
|
|
|
|
|
|
if wordle_status == 'active' and v and v.id == self.author_id:
|
|
|
|
body += f'''<input autocomplete="off" id="guess_box" type="text" name="guess" class="form-control" maxsize="4" style="width: 200px;display: initial"placeholder="5-letter guess"></input><button class="action-{self.id} btn btn-success small" style="text-transform: uppercase; padding: 2px"onclick="handle_action('wordle','{self.id}',document.getElementById('guess_box').value)">Guess</button>'''
|
|
|
|
elif wordle_status == 'won':
|
|
|
|
body += "<strong class='ml-2'>Correct!</strong>"
|
|
|
|
elif wordle_status == 'lost':
|
|
|
|
body += f"<strong class='ml-2'>Lost. The answer was: {wordle_answer}</strong>"
|
|
|
|
|
|
|
|
body += '</span>'
|
|
|
|
return body
|
|
|
|
|
|
|
|
@lazy
|
|
|
|
def blackjack_html(self, v):
|
|
|
|
if not self.blackjack_result: return ''
|
|
|
|
|
|
|
|
split_result = self.blackjack_result.split('_')
|
|
|
|
blackjack_status = split_result[3]
|
|
|
|
player_hand = split_result[0].replace('X', '10')
|
|
|
|
dealer_hand = split_result[1].split('/')[0] if blackjack_status == 'active' else split_result[1]
|
|
|
|
dealer_hand = dealer_hand.replace('X', '10')
|
2022-03-06 04:23:12 +00:00
|
|
|
wager = int(split_result[4])
|
2022-03-06 03:44:09 +00:00
|
|
|
try: kind = split_result[5]
|
|
|
|
except: kind = "coins"
|
2022-05-19 07:49:15 +00:00
|
|
|
currency_kind = "Coins" if kind == "coins" else "Marseybux"
|
2022-03-06 03:25:23 +00:00
|
|
|
|
2022-05-01 18:24:01 +00:00
|
|
|
try: is_insured = split_result[6]
|
|
|
|
except: is_insured = "0"
|
|
|
|
|
2022-03-06 03:25:23 +00:00
|
|
|
body = f"<span id='blackjack-{self.id}' class='ml-2'><em>{player_hand} vs. {dealer_hand}</em>"
|
|
|
|
|
2022-03-06 18:15:13 +00:00
|
|
|
if blackjack_status == 'active' and v and v.id == self.author_id:
|
2022-05-01 18:24:01 +00:00
|
|
|
body += f'''
|
|
|
|
<button
|
|
|
|
class="action-{self.id} btn btn-success small"
|
|
|
|
style="text-transform: uppercase; padding: 2px"
|
|
|
|
onclick="handle_action('blackjack','{self.id}','hit')">
|
|
|
|
Hit
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
class="action-{self.id} btn btn-danger small"
|
|
|
|
style="text-transform: uppercase; padding: 2px"
|
|
|
|
onclick="handle_action('blackjack','{self.id}','stay')">
|
|
|
|
Stay
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
class="action-{self.id} btn btn-secondary small"
|
|
|
|
style="text-transform: uppercase; padding: 2px"
|
|
|
|
onclick="handle_action('blackjack','{self.id}','doubledown')">
|
|
|
|
Double Down
|
|
|
|
</button>
|
|
|
|
'''
|
|
|
|
|
|
|
|
if dealer_hand[0][0] == 'A' and not is_insured == "1":
|
|
|
|
body += f'''
|
|
|
|
<button
|
|
|
|
class="action-{self.id} btn btn-secondary small"
|
|
|
|
style="text-transform: uppercase; padding: 2px"
|
|
|
|
onclick="handle_action('blackjack','{self.id}','insurance')">
|
|
|
|
Insure
|
|
|
|
</button>
|
|
|
|
'''
|
|
|
|
|
2022-03-06 03:25:23 +00:00
|
|
|
elif blackjack_status == 'push':
|
|
|
|
body += f"<strong class='ml-2'>Pushed. Refunded {wager} {currency_kind}.</strong>"
|
|
|
|
elif blackjack_status == 'bust':
|
|
|
|
body += f"<strong class='ml-2'>Bust. Lost {wager} {currency_kind}.</strong>"
|
|
|
|
elif blackjack_status == 'lost':
|
|
|
|
body += f"<strong class='ml-2'>Lost {wager} {currency_kind}.</strong>"
|
|
|
|
elif blackjack_status == 'won':
|
|
|
|
body += f"<strong class='ml-2'>Won {wager} {currency_kind}.</strong>"
|
|
|
|
elif blackjack_status == 'blackjack':
|
2022-03-06 03:55:02 +00:00
|
|
|
body += f"<strong class='ml-2'>Blackjack! Won {floor(wager * 3/2)} {currency_kind}.</strong>"
|
2022-03-06 03:25:23 +00:00
|
|
|
|
2022-05-01 18:24:01 +00:00
|
|
|
if is_insured == "1":
|
|
|
|
body += f" <em class='text-success'>Insured.</em>"
|
|
|
|
|
2022-03-06 03:25:23 +00:00
|
|
|
body += '</span>'
|
2022-06-11 09:30:46 +00:00
|
|
|
return body
|
2022-06-26 04:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
@lazy
|
|
|
|
def is_saved(self, v):
|
2022-06-26 04:57:31 +00:00
|
|
|
return g.db.query(CommentSaveRelationship).filter_by(user_id=v.id, comment_id=self.id).first()
|