2022-05-04 23:09:46 +00:00
|
|
|
import time
|
2022-11-15 09:19:08 +00:00
|
|
|
|
|
|
|
from sqlalchemy import Column, ForeignKey
|
|
|
|
from sqlalchemy.orm import relationship
|
|
|
|
from sqlalchemy.sql.sqltypes import *
|
2024-02-11 17:19:18 +00:00
|
|
|
from flask import g
|
2024-03-05 19:18:13 +00:00
|
|
|
import bleach
|
|
|
|
from bleach.linkifier import LinkifyFilter
|
|
|
|
import functools
|
2022-11-15 09:19:08 +00:00
|
|
|
|
|
|
|
from files.classes import Base
|
2022-12-11 23:44:34 +00:00
|
|
|
from files.helpers.config.const import *
|
2022-11-15 09:19:08 +00:00
|
|
|
from files.helpers.lazy import lazy
|
2023-09-22 17:01:30 +00:00
|
|
|
from files.helpers.slurs_and_profanities import censor_slurs_profanities
|
2022-10-15 18:11:43 +00:00
|
|
|
from files.helpers.sorting_and_time import make_age_string
|
2024-03-05 19:18:13 +00:00
|
|
|
from files.helpers.regex import sanitize_url_regex
|
|
|
|
|
|
|
|
def allowed_attributes_notes(tag, name, value):
|
|
|
|
if tag == 'a':
|
|
|
|
if name == 'href' and '\\' not in value and 'xn--' not in value:
|
|
|
|
return True
|
|
|
|
if name == 'rel' and value == 'nofollow noopener': return True
|
|
|
|
|
|
|
|
if tag == 'img':
|
|
|
|
if name == 'src':
|
|
|
|
if '\\' in value: return False
|
|
|
|
if value.startswith('/') : return True
|
|
|
|
if value.startswith(f'{SITE_FULL_IMAGES}/') : return True
|
|
|
|
if name == 'loading' and value == 'lazy': return True
|
|
|
|
if name == 'data-bs-toggle' and value == 'tooltip': return True
|
|
|
|
if name in {'alt','title'}: return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def bleach_log_note(note):
|
|
|
|
note = note.replace("\n", "").replace("\r", "").replace("\t", "")
|
|
|
|
|
|
|
|
note = bleach.Cleaner(
|
|
|
|
tags=['a','img'],
|
|
|
|
attributes=allowed_attributes_notes,
|
|
|
|
protocols=['http','https'],
|
|
|
|
filters=[
|
|
|
|
functools.partial(
|
|
|
|
LinkifyFilter,
|
|
|
|
skip_tags=["pre","code"],
|
|
|
|
parse_email=False,
|
|
|
|
url_re=sanitize_url_regex
|
|
|
|
)
|
|
|
|
]
|
|
|
|
).clean(note)
|
|
|
|
|
|
|
|
note = note.replace('\n','').strip()
|
|
|
|
|
|
|
|
return note
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
class ModAction(Base):
|
|
|
|
__tablename__ = "modactions"
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
user_id = Column(Integer, ForeignKey("users.id"))
|
|
|
|
kind = Column(String)
|
|
|
|
target_user_id = Column(Integer, ForeignKey("users.id"))
|
2023-06-07 23:26:32 +00:00
|
|
|
target_post_id = Column(Integer, ForeignKey("posts.id"))
|
2022-05-04 23:09:46 +00:00
|
|
|
target_comment_id = Column(Integer, ForeignKey("comments.id"))
|
2023-08-23 21:57:39 +00:00
|
|
|
_note = Column(String)
|
2022-09-19 20:40:33 +00:00
|
|
|
created_utc = Column(Integer)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-07-02 06:48:04 +00:00
|
|
|
user = relationship("User", primaryjoin="User.id==ModAction.user_id")
|
|
|
|
target_user = relationship("User", primaryjoin="User.id==ModAction.target_user_id")
|
2023-06-07 23:26:32 +00:00
|
|
|
target_post = relationship("Post")
|
2023-08-05 14:08:14 +00:00
|
|
|
target_comment = relationship("Comment")
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-09-19 20:40:33 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
2024-03-05 19:18:13 +00:00
|
|
|
if "created_utc" not in kwargs:
|
|
|
|
kwargs["created_utc"] = int(time.time())
|
|
|
|
|
|
|
|
if "_note" in kwargs:
|
|
|
|
kwargs["_note"] = bleach_log_note(kwargs["_note"])
|
|
|
|
|
2022-09-19 20:40:33 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
def __repr__(self):
|
2022-11-29 21:02:38 +00:00
|
|
|
return f"<{self.__class__.__name__}(id={self.id})>"
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def age_string(self):
|
2022-10-15 18:11:43 +00:00
|
|
|
return make_age_string(self.created_utc)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def note(self):
|
2023-08-05 14:12:25 +00:00
|
|
|
if self.kind == "ban_user":
|
|
|
|
if self.target_post_id:
|
|
|
|
return f'for <a href="{self.target_post.permalink}">post</a>'
|
|
|
|
elif self.target_comment_id:
|
|
|
|
return f'for <a href="{self.target_comment.permalink}">comment</a>'
|
|
|
|
else:
|
|
|
|
return self._note
|
2022-05-04 23:09:46 +00:00
|
|
|
else:
|
|
|
|
return self._note or ""
|
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def string(self):
|
2022-12-11 23:44:34 +00:00
|
|
|
output = MODACTION_TYPES[self.kind]["str"].format(self=self)
|
2022-05-04 23:09:46 +00:00
|
|
|
if self.note: output += f" <i>({self.note})</i>"
|
|
|
|
return output
|
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def target_link(self):
|
2023-08-05 14:12:25 +00:00
|
|
|
if self.target_user_id:
|
|
|
|
return f'<a href="{self.target_user.url}">@{self.target_user.username}</a>'
|
|
|
|
elif self.target_post_id:
|
2024-02-11 17:19:18 +00:00
|
|
|
return censor_slurs_profanities(f'<a href="{self.target_post.permalink}">{self.target_post.title_html}</a>', g.v)
|
2023-08-05 14:12:25 +00:00
|
|
|
elif self.target_comment_id:
|
|
|
|
return f'<a href="{self.target_comment.permalink}">comment</a>'
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def icon(self):
|
2022-12-11 23:44:34 +00:00
|
|
|
return MODACTION_TYPES[self.kind]['icon']
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def color(self):
|
2022-12-11 23:44:34 +00:00
|
|
|
return MODACTION_TYPES[self.kind]['color']
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def permalink(self):
|
2022-09-29 12:43:14 +00:00
|
|
|
return f"{SITE_FULL}/log/{self.id}"
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-12-11 23:44:34 +00:00
|
|
|
from files.helpers.config.modaction_types import MODACTION_TYPES, MODACTION_TYPES_FILTERED
|