2021-08-15 19:17:19 +00:00
|
|
|
import re
|
|
|
|
from urllib.parse import urlencode, urlparse, parse_qs
|
2021-07-21 01:12:26 +00:00
|
|
|
from flask import *
|
|
|
|
from sqlalchemy import *
|
2021-09-23 15:43:09 +00:00
|
|
|
from sqlalchemy.orm import relationship, deferred
|
2021-08-04 15:35:10 +00:00
|
|
|
from files.helpers.lazy import lazy
|
2021-09-28 03:59:33 +00:00
|
|
|
from files.helpers.const import SLURS
|
2021-08-04 15:35:10 +00:00
|
|
|
from files.__main__ import Base
|
2021-07-31 13:55:49 +00:00
|
|
|
from .flags import CommentFlag
|
2021-09-12 06:12:44 +00:00
|
|
|
from os import environ
|
2021-09-19 18:28:31 +00:00
|
|
|
import time
|
2021-09-12 06:12:44 +00:00
|
|
|
|
|
|
|
site = environ.get("DOMAIN").strip()
|
2021-07-21 01:12:26 +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"))
|
|
|
|
created_utc = Column(Integer, default=0)
|
|
|
|
edited_utc = Column(Integer, default=0)
|
|
|
|
is_banned = Column(Boolean, default=False)
|
2021-09-10 05:36:31 +00:00
|
|
|
removed_by = Column(Integer)
|
2021-08-11 22:28:11 +00:00
|
|
|
bannedfor = Column(Boolean)
|
2021-07-21 01:12:26 +00:00
|
|
|
distinguish_level = Column(Integer, default=0)
|
|
|
|
deleted_utc = Column(Integer, default=0)
|
|
|
|
is_approved = Column(Integer, default=0)
|
|
|
|
level = Column(Integer, default=0)
|
|
|
|
parent_comment_id = Column(Integer, ForeignKey("comments.id"))
|
|
|
|
over_18 = Column(Boolean, default=False)
|
|
|
|
is_bot = Column(Boolean, default=False)
|
2021-09-30 19:40:33 +00:00
|
|
|
is_pinned = Column(String(25))
|
2021-07-25 23:49:53 +00:00
|
|
|
sentto=Column(Integer)
|
2021-09-27 21:50:35 +00:00
|
|
|
notifiedto=Column(Integer)
|
2021-07-25 23:49:53 +00:00
|
|
|
app_id = Column(Integer, ForeignKey("oauth_apps.id"))
|
2021-09-22 14:51:36 +00:00
|
|
|
oauth_app = relationship("OauthApp", viewonly=True)
|
2021-09-24 02:21:41 +00:00
|
|
|
upvotes = Column(Integer, default=1)
|
|
|
|
downvotes = Column(Integer, default=0)
|
2021-09-24 19:41:08 +00:00
|
|
|
body = deferred(Column(String(10000)))
|
|
|
|
body_html = deferred(Column(String(20000)))
|
2021-09-24 02:21:41 +00:00
|
|
|
ban_reason = Column(String(256))
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2021-09-22 14:51:36 +00:00
|
|
|
post = relationship("Submission", viewonly=True)
|
|
|
|
flags = relationship("CommentFlag", lazy="dynamic", viewonly=True)
|
2021-09-22 15:01:18 +00:00
|
|
|
author = relationship("User", primaryjoin="User.id==Comment.author_id")
|
2021-09-22 14:51:36 +00:00
|
|
|
parent_comment = relationship("Comment", remote_side=[id], viewonly=True)
|
|
|
|
child_comments = relationship("Comment", remote_side=[parent_comment_id], viewonly=True)
|
|
|
|
awards = relationship("AwardRelationship", viewonly=True)
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
|
|
|
if "created_utc" not in kwargs:
|
|
|
|
kwargs["created_utc"] = int(time.time())
|
|
|
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
|
|
|
|
return f"<Comment(id={self.id})>"
|
|
|
|
|
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):
|
|
|
|
|
|
|
|
age = int(time.time()) - self.created_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.created_utc)
|
|
|
|
|
|
|
|
# compute number of months
|
|
|
|
months = now.tm_mon - ctd.tm_mon + 12 * (now.tm_year - ctd.tm_year)
|
|
|
|
# remove a month count if current day of month < creation day of month
|
|
|
|
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):
|
|
|
|
|
|
|
|
if not self.edited_utc:
|
|
|
|
return "never"
|
|
|
|
|
|
|
|
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)
|
|
|
|
months = now.tm_mon - ctd.tm_mon + 12 * (now.tm_year - ctd.tm_year)
|
|
|
|
|
|
|
|
if months < 12:
|
|
|
|
return f"{months}mo ago"
|
|
|
|
else:
|
|
|
|
years = now.tm_year - ctd.tm_year
|
|
|
|
return f"{years}yr ago"
|
|
|
|
|
2021-09-23 19:22:18 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def score(self):
|
|
|
|
return self.upvotes - self.downvotes
|
|
|
|
|
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
|
|
|
|
2021-07-27 00:05:58 +00:00
|
|
|
else: return g.db.query(Comment).get(self.parent_comment_id)
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2021-07-30 05:23:17 +00:00
|
|
|
@property
|
|
|
|
@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
|
|
|
|
2021-07-21 01:12:26 +00:00
|
|
|
@property
|
|
|
|
def replies(self):
|
2021-08-23 18:54:57 +00:00
|
|
|
r = self.__dict__.get("replies", None)
|
|
|
|
if r: r = [x for x in r if not x.author.shadowbanned]
|
2021-08-23 18:48:11 +00:00
|
|
|
if not r and r != []: r = sorted([x for x in self.child_comments if not x.author.shadowbanned], key=lambda x: x.score, reverse=True)
|
2021-07-21 01:12:26 +00:00
|
|
|
return r
|
|
|
|
|
|
|
|
@replies.setter
|
|
|
|
def replies(self, value):
|
|
|
|
self.__dict__["replies"] = value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def replies2(self):
|
|
|
|
return self.__dict__.get("replies2", [])
|
|
|
|
|
|
|
|
@replies2.setter
|
|
|
|
def replies2(self, value):
|
|
|
|
self.__dict__["replies2"] = value
|
|
|
|
|
2021-08-23 18:52:56 +00:00
|
|
|
@property
|
|
|
|
def replies3(self):
|
|
|
|
r = self.__dict__.get("replies", None)
|
|
|
|
if not r and r != []: r = sorted([x for x in self.child_comments], key=lambda x: x.score, reverse=True)
|
|
|
|
return r
|
|
|
|
|
2021-09-12 06:12:44 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def shortlink(self):
|
|
|
|
return f"https://{site}/comment/{self.id}"
|
|
|
|
|
2021-07-21 01:12:26 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def permalink(self):
|
2021-09-12 08:02:04 +00:00
|
|
|
if self.post and self.post.club: return f"/comment/{self.id}/"
|
|
|
|
|
2021-07-21 01:12:26 +00:00
|
|
|
if self.post: return f"{self.post.permalink}/{self.id}/"
|
|
|
|
else: return f"/comment/{self.id}/"
|
|
|
|
|
|
|
|
@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 = {}
|
|
|
|
for f in self.flags: flags[f.user.username] = f.reason
|
|
|
|
|
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,
|
2021-07-25 22:11:26 +00:00
|
|
|
'author_name': self.author.username,
|
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,
|
|
|
|
'permalink': self.permalink,
|
2021-07-28 03:29:06 +00:00
|
|
|
'is_pinned': self.is_pinned,
|
|
|
|
'distinguish_level': self.distinguish_level,
|
2021-07-30 05:31:38 +00:00
|
|
|
'post_id': self.post.id,
|
2021-09-18 19:45:17 +00:00
|
|
|
'score': self.score,
|
|
|
|
'upvotes': self.upvotes,
|
|
|
|
'downvotes': self.downvotes,
|
2021-07-25 01:17:28 +00:00
|
|
|
#'award_count': self.award_count,
|
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
|
|
|
|
|
2021-08-23 11:05:56 +00:00
|
|
|
def award_count(self, kind) -> int:
|
|
|
|
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,
|
|
|
|
'post': self.post.id,
|
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
|
|
|
}
|
|
|
|
elif self.deleted_utc > 0:
|
|
|
|
data= {'deleted_utc': self.deleted_utc,
|
2021-07-30 05:31:38 +00:00
|
|
|
'id': self.id,
|
|
|
|
'post': self.post.id,
|
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
|
|
|
|
|
2021-07-30 05:31:38 +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__:
|
|
|
|
data['replies']=[x.json_core for x in self.replies]
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
@property
|
2021-09-19 18:25:40 +00:00
|
|
|
@lazy
|
2021-07-21 01:12:26 +00:00
|
|
|
def json(self):
|
|
|
|
|
|
|
|
data=self.json_core
|
|
|
|
|
|
|
|
if self.deleted_utc > 0 or self.is_banned:
|
|
|
|
return data
|
|
|
|
|
|
|
|
data["author"]=self.author.json_core
|
|
|
|
data["post"]=self.post.json_core
|
|
|
|
|
|
|
|
if self.level >= 2:
|
|
|
|
data["parent"]=self.parent.json_core
|
|
|
|
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
def realbody(self, v):
|
2021-09-17 14:07:05 +00:00
|
|
|
if self.post and self.post.club and not (v and v.paid_dues): return "<p>COUNTRY CLUB ONLY</p>"
|
2021-09-24 03:00:54 +00:00
|
|
|
|
2021-09-22 23:42:43 +00:00
|
|
|
body = self.body_html
|
2021-08-21 11:06:28 +00:00
|
|
|
|
2021-09-24 03:02:14 +00:00
|
|
|
if not body: return ""
|
2021-09-24 03:03:31 +00:00
|
|
|
|
2021-09-28 03:59:33 +00:00
|
|
|
if not v or v.slurreplacer:
|
|
|
|
for s, r in SLURS.items(): body = body.replace(s, r)
|
2021-08-21 11:06:28 +00:00
|
|
|
|
2021-07-21 01:12:26 +00:00
|
|
|
if v and not v.oldreddit: body = body.replace("old.reddit.com", "reddit.com")
|
2021-08-11 18:57:12 +00:00
|
|
|
|
2021-09-13 12:06:22 +00:00
|
|
|
if v and v.nitter: body = body.replace("www.twitter.com", "nitter.net").replace("twitter.com", "nitter.net")
|
|
|
|
|
2021-08-15 22:37:35 +00:00
|
|
|
if v and v.controversial:
|
2021-08-15 19:17:19 +00:00
|
|
|
for i in re.finditer('(/comments/.*?)"', body):
|
|
|
|
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)}")
|
|
|
|
|
2021-07-21 01:12:26 +00:00
|
|
|
return body
|
|
|
|
|
2021-09-19 20:06:52 +00:00
|
|
|
@lazy
|
2021-07-21 01:12:26 +00:00
|
|
|
def collapse_for_user(self, v):
|
|
|
|
|
|
|
|
if self.over_18 and not (v and v.over_18) and not self.post.over_18:
|
|
|
|
return True
|
|
|
|
|
|
|
|
if not v:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if any([x in self.body for x in v.filter_words]):
|
|
|
|
return True
|
|
|
|
|
|
|
|
if self.is_banned: return True
|
|
|
|
|
|
|
|
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
|
|
|
@property
|
|
|
|
@lazy
|
2021-08-06 12:57:37 +00:00
|
|
|
def active_flags(self): return self.flags.count()
|
2021-07-31 13:55:49 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
2021-07-31 14:11:11 +00:00
|
|
|
def ordered_flags(self): return self.flags.order_by(CommentFlag.id).all()
|
2021-07-31 13:55:49 +00:00
|
|
|
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Notification(Base):
|
|
|
|
|
|
|
|
__tablename__ = "notifications"
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
user_id = Column(Integer, ForeignKey("users.id"))
|
|
|
|
comment_id = Column(Integer, ForeignKey("comments.id"))
|
|
|
|
read = Column(Boolean, default=False)
|
2021-07-25 23:49:53 +00:00
|
|
|
followsender = Column(Integer)
|
|
|
|
unfollowsender = Column(Integer)
|
2021-09-28 20:54:33 +00:00
|
|
|
removefollowsender = Column(Integer)
|
2021-07-25 23:49:53 +00:00
|
|
|
blocksender = Column(Integer)
|
|
|
|
unblocksender = Column(Integer)
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2021-09-22 14:51:36 +00:00
|
|
|
comment = relationship("Comment", viewonly=True)
|
|
|
|
user = relationship("User", viewonly=True)
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
|
2021-08-15 19:17:19 +00:00
|
|
|
return f"<Notification(id={self.id})>"
|