2020-12-10 12:37:15 +01:00
|
|
|
# ContentDB
|
2021-01-30 17:59:42 +01:00
|
|
|
# Copyright (C) 2018-21 rubenwardy
|
2020-12-10 12:37:15 +01:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2021-01-30 17:59:42 +01:00
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
2020-12-10 12:37:15 +01:00
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2021-01-30 17:59:42 +01:00
|
|
|
# GNU Affero General Public License for more details.
|
2020-12-10 12:37:15 +01:00
|
|
|
#
|
2021-01-30 17:59:42 +01:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
2020-12-10 12:37:15 +01:00
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
import enum
|
|
|
|
|
2022-11-09 21:43:31 +01:00
|
|
|
from flask import url_for
|
2020-12-10 12:37:15 +01:00
|
|
|
from flask_login import UserMixin
|
|
|
|
from sqlalchemy import desc, text
|
|
|
|
|
|
|
|
from app import gravatar
|
|
|
|
from . import db
|
|
|
|
|
|
|
|
|
|
|
|
class UserRank(enum.Enum):
|
|
|
|
BANNED = 0
|
|
|
|
NOT_JOINED = 1
|
|
|
|
NEW_MEMBER = 2
|
|
|
|
MEMBER = 3
|
|
|
|
TRUSTED_MEMBER = 4
|
2021-08-16 19:57:05 +02:00
|
|
|
APPROVER = 5
|
|
|
|
EDITOR = 6
|
|
|
|
BOT = 7
|
|
|
|
MODERATOR = 8
|
|
|
|
ADMIN = 9
|
2020-12-10 12:37:15 +01:00
|
|
|
|
2023-06-19 22:27:49 +02:00
|
|
|
def at_least(self, min):
|
2020-12-10 12:37:15 +01:00
|
|
|
return self.value >= min.value
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def get_title(self):
|
2020-12-10 12:37:15 +01:00
|
|
|
return self.name.replace("_", " ").title()
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def to_name(self):
|
2020-12-10 12:37:15 +01:00
|
|
|
return self.name.lower()
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def choices(cls):
|
2023-06-18 22:56:19 +02:00
|
|
|
return [(choice, choice.get_title()) for choice in cls]
|
2020-12-10 12:37:15 +01:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def coerce(cls, item):
|
2021-02-28 17:04:31 +01:00
|
|
|
return item if type(item) == UserRank else UserRank[item.upper()]
|
2020-12-10 12:37:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Permission(enum.Enum):
|
2023-08-16 02:00:51 +02:00
|
|
|
VIEW_PACKAGE = "VIEW_PACKAGE"
|
2020-12-10 12:37:15 +01:00
|
|
|
EDIT_PACKAGE = "EDIT_PACKAGE"
|
|
|
|
DELETE_PACKAGE = "DELETE_PACKAGE"
|
|
|
|
CHANGE_AUTHOR = "CHANGE_AUTHOR"
|
|
|
|
CHANGE_NAME = "CHANGE_NAME"
|
|
|
|
MAKE_RELEASE = "MAKE_RELEASE"
|
|
|
|
DELETE_RELEASE = "DELETE_RELEASE"
|
|
|
|
ADD_SCREENSHOTS = "ADD_SCREENSHOTS"
|
|
|
|
APPROVE_SCREENSHOT = "APPROVE_SCREENSHOT"
|
|
|
|
APPROVE_RELEASE = "APPROVE_RELEASE"
|
|
|
|
APPROVE_NEW = "APPROVE_NEW"
|
|
|
|
EDIT_TAGS = "EDIT_TAGS"
|
|
|
|
CREATE_TAG = "CREATE_TAG"
|
|
|
|
CHANGE_RELEASE_URL = "CHANGE_RELEASE_URL"
|
|
|
|
CHANGE_USERNAMES = "CHANGE_USERNAMES"
|
|
|
|
CHANGE_RANK = "CHANGE_RANK"
|
|
|
|
CHANGE_EMAIL = "CHANGE_EMAIL"
|
|
|
|
SEE_THREAD = "SEE_THREAD"
|
|
|
|
CREATE_THREAD = "CREATE_THREAD"
|
|
|
|
COMMENT_THREAD = "COMMENT_THREAD"
|
|
|
|
LOCK_THREAD = "LOCK_THREAD"
|
|
|
|
DELETE_THREAD = "DELETE_THREAD"
|
|
|
|
DELETE_REPLY = "DELETE_REPLY"
|
|
|
|
EDIT_REPLY = "EDIT_REPLY"
|
|
|
|
UNAPPROVE_PACKAGE = "UNAPPROVE_PACKAGE"
|
|
|
|
TOPIC_DISCARD = "TOPIC_DISCARD"
|
|
|
|
CREATE_TOKEN = "CREATE_TOKEN"
|
|
|
|
EDIT_MAINTAINERS = "EDIT_MAINTAINERS"
|
2022-02-09 13:47:36 +01:00
|
|
|
DELETE_REVIEW = "DELETE_REVIEW"
|
2020-12-10 12:37:15 +01:00
|
|
|
CHANGE_PROFILE_URLS = "CHANGE_PROFILE_URLS"
|
2021-02-26 00:25:33 +01:00
|
|
|
CHANGE_DISPLAY_NAME = "CHANGE_DISPLAY_NAME"
|
2023-05-12 01:55:44 +02:00
|
|
|
VIEW_AUDIT_DESCRIPTION = "VIEW_AUDIT_DESCRIPTION"
|
2023-08-14 22:48:50 +02:00
|
|
|
EDIT_COLLECTION = "EDIT_COLLECTION"
|
|
|
|
VIEW_COLLECTION = "VIEW_COLLECTION"
|
2023-10-31 19:40:01 +01:00
|
|
|
CREATE_OAUTH_CLIENT = "CREATE_OAUTH_CLIENT"
|
2020-12-10 12:37:15 +01:00
|
|
|
|
|
|
|
# Only return true if the permission is valid for *all* contexts
|
2023-06-18 22:56:19 +02:00
|
|
|
# See Package.check_perm for package-specific contexts
|
2020-12-10 12:37:15 +01:00
|
|
|
def check(self, user):
|
|
|
|
if not user.is_authenticated:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if self == Permission.APPROVE_NEW or \
|
|
|
|
self == Permission.APPROVE_RELEASE or \
|
|
|
|
self == Permission.APPROVE_SCREENSHOT or \
|
|
|
|
self == Permission.SEE_THREAD:
|
2023-06-19 22:27:49 +02:00
|
|
|
return user.rank.at_least(UserRank.APPROVER)
|
2021-08-16 19:57:05 +02:00
|
|
|
|
|
|
|
elif self == Permission.EDIT_TAGS or self == Permission.CREATE_TAG:
|
2023-06-19 22:27:49 +02:00
|
|
|
return user.rank.at_least(UserRank.EDITOR)
|
2021-08-16 19:57:05 +02:00
|
|
|
|
2020-12-10 12:37:15 +01:00
|
|
|
else:
|
2023-06-18 22:56:19 +02:00
|
|
|
raise Exception("Non-global permission checked globally. Use Package.check_perm or User.check_perm instead.")
|
2020-12-10 12:37:15 +01:00
|
|
|
|
|
|
|
@staticmethod
|
2023-06-18 22:56:19 +02:00
|
|
|
def check_perm(user, perm):
|
2020-12-10 12:37:15 +01:00
|
|
|
if type(perm) == str:
|
|
|
|
perm = Permission[perm]
|
|
|
|
elif type(perm) != Permission:
|
|
|
|
raise Exception("Unknown permission given to Permission.check")
|
|
|
|
|
|
|
|
return perm.check(user)
|
|
|
|
|
2020-12-10 17:49:37 +01:00
|
|
|
|
2020-12-10 12:37:15 +01:00
|
|
|
def display_name_default(context):
|
|
|
|
return context.get_current_parameters()["username"]
|
|
|
|
|
2020-12-10 17:49:37 +01:00
|
|
|
|
2020-12-10 12:37:15 +01:00
|
|
|
class User(db.Model, UserMixin):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
|
2021-11-24 18:35:38 +01:00
|
|
|
created_at = db.Column(db.DateTime, nullable=True, default=datetime.datetime.utcnow)
|
|
|
|
|
2020-12-10 12:37:15 +01:00
|
|
|
# User authentication information
|
|
|
|
username = db.Column(db.String(50, collation="NOCASE"), nullable=False, unique=True, index=True)
|
|
|
|
password = db.Column(db.String(255), nullable=True, server_default=None)
|
|
|
|
reset_password_token = db.Column(db.String(100), nullable=False, server_default="")
|
|
|
|
|
|
|
|
def get_id(self):
|
|
|
|
return self.username
|
|
|
|
|
2020-12-10 17:49:37 +01:00
|
|
|
rank = db.Column(db.Enum(UserRank), nullable=False)
|
2020-12-10 12:37:15 +01:00
|
|
|
|
|
|
|
# Account linking
|
|
|
|
github_username = db.Column(db.String(50, collation="NOCASE"), nullable=True, unique=True)
|
|
|
|
forums_username = db.Column(db.String(50, collation="NOCASE"), nullable=True, unique=True)
|
|
|
|
|
|
|
|
# Access token for webhook setup
|
|
|
|
github_access_token = db.Column(db.String(50), nullable=True, server_default=None)
|
|
|
|
|
|
|
|
# User email information
|
|
|
|
email = db.Column(db.String(255), nullable=True, unique=True)
|
2021-05-04 00:47:06 +02:00
|
|
|
email_confirmed_at = db.Column(db.DateTime(), nullable=True, server_default=None)
|
2020-12-10 12:37:15 +01:00
|
|
|
|
2022-01-25 02:33:13 +01:00
|
|
|
locale = db.Column(db.String(10), nullable=True, default=None)
|
2022-01-22 21:47:43 +01:00
|
|
|
|
2020-12-10 12:37:15 +01:00
|
|
|
# User information
|
|
|
|
profile_pic = db.Column(db.String(255), nullable=True, server_default=None)
|
|
|
|
is_active = db.Column("is_active", db.Boolean, nullable=False, server_default="0")
|
|
|
|
display_name = db.Column(db.String(100), nullable=False, default=display_name_default)
|
|
|
|
|
|
|
|
# Links
|
|
|
|
website_url = db.Column(db.String(255), nullable=True, default=None)
|
|
|
|
donate_url = db.Column(db.String(255), nullable=True, default=None)
|
|
|
|
|
|
|
|
# Content
|
|
|
|
notifications = db.relationship("Notification", foreign_keys="Notification.user_id",
|
2022-11-09 21:43:31 +01:00
|
|
|
order_by=desc(text("Notification.created_at")), back_populates="user", cascade="all, delete, delete-orphan")
|
2020-12-10 12:37:15 +01:00
|
|
|
caused_notifications = db.relationship("Notification", foreign_keys="Notification.causer_id",
|
2022-11-09 21:43:31 +01:00
|
|
|
back_populates="causer", cascade="all, delete, delete-orphan", lazy="dynamic")
|
2020-12-10 12:37:15 +01:00
|
|
|
notification_preferences = db.relationship("UserNotificationPreferences", uselist=False, back_populates="user",
|
2022-11-09 21:43:31 +01:00
|
|
|
cascade="all, delete, delete-orphan")
|
2020-12-10 12:37:15 +01:00
|
|
|
|
2021-01-24 13:58:29 +01:00
|
|
|
email_verifications = db.relationship("UserEmailVerification", foreign_keys="UserEmailVerification.user_id",
|
2022-11-09 21:43:31 +01:00
|
|
|
back_populates="user", cascade="all, delete, delete-orphan", lazy="dynamic")
|
2021-01-24 13:58:29 +01:00
|
|
|
|
2020-12-10 12:37:15 +01:00
|
|
|
audit_log_entries = db.relationship("AuditLogEntry", foreign_keys="AuditLogEntry.causer_id", back_populates="causer",
|
2022-11-09 21:43:31 +01:00
|
|
|
order_by=desc("audit_log_entry_created_at"), lazy="dynamic")
|
2020-12-10 12:37:15 +01:00
|
|
|
|
2021-02-02 18:09:21 +01:00
|
|
|
maintained_packages = db.relationship("Package", lazy="dynamic", secondary="maintainers", order_by=db.asc("package_title"))
|
2021-01-29 20:38:14 +01:00
|
|
|
|
2021-02-02 18:09:21 +01:00
|
|
|
packages = db.relationship("Package", back_populates="author", lazy="dynamic", order_by=db.asc("package_title"))
|
2020-12-10 12:37:15 +01:00
|
|
|
reviews = db.relationship("PackageReview", back_populates="author", order_by=db.desc("package_review_created_at"), cascade="all, delete, delete-orphan")
|
2021-08-18 23:09:41 +02:00
|
|
|
review_votes = db.relationship("PackageReviewVote", back_populates="user", cascade="all, delete, delete-orphan")
|
2020-12-10 12:37:15 +01:00
|
|
|
tokens = db.relationship("APIToken", back_populates="owner", lazy="dynamic", cascade="all, delete, delete-orphan")
|
|
|
|
threads = db.relationship("Thread", back_populates="author", lazy="dynamic", cascade="all, delete, delete-orphan")
|
2022-01-03 02:41:50 +01:00
|
|
|
replies = db.relationship("ThreadReply", back_populates="author", lazy="dynamic", cascade="all, delete, delete-orphan", order_by=db.desc("created_at"))
|
2021-08-04 22:50:35 +02:00
|
|
|
forum_topics = db.relationship("ForumTopic", back_populates="author", lazy="dynamic", cascade="all, delete, delete-orphan")
|
2023-08-14 22:48:50 +02:00
|
|
|
collections = db.relationship("Collection", back_populates="author", lazy="dynamic", cascade="all, delete, delete-orphan", order_by=db.asc("title"))
|
2023-10-31 19:40:01 +01:00
|
|
|
clients = db.relationship("OAuthClient", back_populates="owner", lazy="dynamic", cascade="all, delete, delete-orphan")
|
2020-12-10 12:37:15 +01:00
|
|
|
|
2022-02-13 11:37:54 +01:00
|
|
|
ban = db.relationship("UserBan", foreign_keys="UserBan.user_id", back_populates="user", uselist=False)
|
|
|
|
|
2022-11-09 21:43:31 +01:00
|
|
|
def get_dict(self):
|
2023-10-31 20:18:08 +01:00
|
|
|
from app.utils.flask import abs_url_for
|
2022-11-09 21:43:31 +01:00
|
|
|
return {
|
|
|
|
"username": self.username,
|
|
|
|
"display_name": self.display_name,
|
|
|
|
"rank": self.rank.name.lower(),
|
|
|
|
"profile_pic_url": self.profile_pic,
|
|
|
|
"website_url": self.website_url,
|
|
|
|
"donate_url": self.donate_url,
|
|
|
|
"connections": {
|
|
|
|
"github": self.github_username,
|
|
|
|
"forums": self.forums_username,
|
|
|
|
},
|
|
|
|
"links": {
|
2023-10-31 20:18:08 +01:00
|
|
|
"api_packages": abs_url_for("api.packages", author=self.username),
|
|
|
|
"profile": abs_url_for("users.profile", username=self.username),
|
2022-11-09 21:43:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-10 12:37:15 +01:00
|
|
|
def __init__(self, username=None, active=False, email=None, password=None):
|
|
|
|
self.username = username
|
|
|
|
self.display_name = username
|
|
|
|
self.is_active = active
|
|
|
|
self.email = email
|
|
|
|
self.password = password
|
|
|
|
self.rank = UserRank.NOT_JOINED
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def can_access_todo_list(self):
|
|
|
|
return Permission.APPROVE_NEW.check(self) or Permission.APPROVE_RELEASE.check(self)
|
2020-12-10 12:37:15 +01:00
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def get_profile_pic_url(self):
|
2020-12-10 12:37:15 +01:00
|
|
|
if self.profile_pic:
|
|
|
|
return self.profile_pic
|
2020-12-15 20:05:29 +01:00
|
|
|
elif self.rank == UserRank.BOT:
|
|
|
|
return "/static/bot_avatar.png"
|
2020-12-10 12:37:15 +01:00
|
|
|
else:
|
2021-11-25 12:00:20 +01:00
|
|
|
return gravatar(self.email or f"{self.username}@content.minetest.net")
|
2020-12-10 12:37:15 +01:00
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def check_perm(self, user, perm):
|
2020-12-10 12:37:15 +01:00
|
|
|
if not user.is_authenticated:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if type(perm) == str:
|
|
|
|
perm = Permission[perm]
|
|
|
|
elif type(perm) != Permission:
|
2023-06-18 22:56:19 +02:00
|
|
|
raise Exception("Unknown permission given to User.check_perm()")
|
2020-12-10 12:37:15 +01:00
|
|
|
|
|
|
|
# Members can edit their own packages, and editors can edit any packages
|
|
|
|
if perm == Permission.CHANGE_AUTHOR:
|
2023-06-19 22:27:49 +02:00
|
|
|
return user.rank.at_least(UserRank.EDITOR)
|
2022-01-21 23:23:24 +01:00
|
|
|
elif perm == Permission.CHANGE_USERNAMES:
|
2023-06-19 22:27:49 +02:00
|
|
|
return user.rank.at_least(UserRank.MODERATOR)
|
2022-01-21 23:23:24 +01:00
|
|
|
elif perm == Permission.CHANGE_RANK:
|
2023-06-19 22:27:49 +02:00
|
|
|
return user.rank.at_least(UserRank.MODERATOR) and not self.rank.at_least(user.rank)
|
2020-12-10 12:37:15 +01:00
|
|
|
elif perm == Permission.CHANGE_EMAIL or perm == Permission.CHANGE_PROFILE_URLS:
|
2023-06-19 22:27:49 +02:00
|
|
|
return user == self or (user.rank.at_least(UserRank.MODERATOR) and not self.rank.at_least(user.rank))
|
2021-02-26 00:25:33 +01:00
|
|
|
elif perm == Permission.CHANGE_DISPLAY_NAME:
|
2023-06-19 22:27:49 +02:00
|
|
|
return user.rank.at_least(UserRank.NEW_MEMBER if user == self else UserRank.MODERATOR)
|
2020-12-10 12:37:15 +01:00
|
|
|
elif perm == Permission.CREATE_TOKEN:
|
|
|
|
if user == self:
|
2023-06-19 22:27:49 +02:00
|
|
|
return user.rank.at_least(UserRank.NEW_MEMBER)
|
2020-12-10 12:37:15 +01:00
|
|
|
else:
|
2023-06-19 22:27:49 +02:00
|
|
|
return user.rank.at_least(UserRank.MODERATOR) and user.rank.at_least(self.rank)
|
2023-10-31 19:40:01 +01:00
|
|
|
elif perm == Permission.CREATE_OAUTH_CLIENT:
|
|
|
|
if user == self:
|
|
|
|
return user.rank.at_least(UserRank.EDITOR)
|
|
|
|
else:
|
|
|
|
return user.rank.at_least(UserRank.MODERATOR) and user.rank.at_least(self.rank)
|
2020-12-10 12:37:15 +01:00
|
|
|
else:
|
|
|
|
raise Exception("Permission {} is not related to users".format(perm.name))
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def can_comment_ratelimit(self):
|
2020-12-11 00:08:53 +01:00
|
|
|
from app.models import ThreadReply
|
|
|
|
|
2020-12-10 12:37:15 +01:00
|
|
|
factor = 1
|
2023-06-19 22:27:49 +02:00
|
|
|
if self.rank.at_least(UserRank.ADMIN):
|
2020-12-10 12:37:15 +01:00
|
|
|
return True
|
2023-06-19 22:27:49 +02:00
|
|
|
elif self.rank.at_least(UserRank.TRUSTED_MEMBER):
|
2022-08-23 03:24:12 +02:00
|
|
|
factor = 3
|
2023-06-19 22:27:49 +02:00
|
|
|
elif self.rank.at_least(UserRank.MEMBER):
|
2022-08-23 03:24:12 +02:00
|
|
|
factor = 2
|
2020-12-10 12:37:15 +01:00
|
|
|
|
|
|
|
one_min_ago = datetime.datetime.utcnow() - datetime.timedelta(minutes=1)
|
|
|
|
if ThreadReply.query.filter_by(author=self) \
|
2022-08-23 03:24:12 +02:00
|
|
|
.filter(ThreadReply.created_at > one_min_ago).count() >= 2 * factor:
|
2020-12-10 12:37:15 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
hour_ago = datetime.datetime.utcnow() - datetime.timedelta(hours=1)
|
|
|
|
if ThreadReply.query.filter_by(author=self) \
|
2022-08-23 03:24:12 +02:00
|
|
|
.filter(ThreadReply.created_at > hour_ago).count() >= 10 * factor:
|
2020-12-10 12:37:15 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def can_open_thread_ratelimit(self):
|
2020-12-11 00:08:53 +01:00
|
|
|
from app.models import Thread
|
|
|
|
|
2020-12-10 12:37:15 +01:00
|
|
|
factor = 1
|
2023-06-19 22:27:49 +02:00
|
|
|
if self.rank.at_least(UserRank.ADMIN):
|
2020-12-10 12:37:15 +01:00
|
|
|
return True
|
2023-06-19 22:27:49 +02:00
|
|
|
elif self.rank.at_least(UserRank.TRUSTED_MEMBER):
|
2022-08-23 03:24:12 +02:00
|
|
|
factor = 5
|
2023-06-19 22:27:49 +02:00
|
|
|
elif self.rank.at_least(UserRank.MEMBER):
|
2022-08-23 03:24:12 +02:00
|
|
|
factor = 2
|
2020-12-10 12:37:15 +01:00
|
|
|
|
|
|
|
hour_ago = datetime.datetime.utcnow() - datetime.timedelta(hours=1)
|
2023-06-18 22:56:19 +02:00
|
|
|
return Thread.query.filter_by(author=self)\
|
|
|
|
.filter(Thread.created_at > hour_ago).count() < 2 * factor
|
2020-12-10 12:37:15 +01:00
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def can_review_ratelimit(self):
|
2022-05-08 17:01:13 +02:00
|
|
|
from app.models import PackageReview
|
|
|
|
|
|
|
|
factor = 1
|
2023-06-19 22:27:49 +02:00
|
|
|
if self.rank.at_least(UserRank.ADMIN):
|
2022-05-08 17:01:13 +02:00
|
|
|
return True
|
2023-06-19 22:27:49 +02:00
|
|
|
elif self.rank.at_least(UserRank.TRUSTED_MEMBER):
|
2022-05-08 17:01:13 +02:00
|
|
|
factor *= 5
|
|
|
|
|
|
|
|
five_mins_ago = datetime.datetime.utcnow() - datetime.timedelta(minutes=5)
|
|
|
|
if PackageReview.query.filter_by(author=self) \
|
2022-05-09 13:40:01 +02:00
|
|
|
.filter(PackageReview.created_at > five_mins_ago).count() > 2 * factor:
|
2022-05-08 17:01:13 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
hour_ago = datetime.datetime.utcnow() - datetime.timedelta(hours=1)
|
|
|
|
return PackageReview.query.filter_by(author=self) \
|
2023-06-18 22:56:19 +02:00
|
|
|
.filter(PackageReview.created_at > hour_ago).count() < 10 * factor
|
2022-05-08 17:01:13 +02:00
|
|
|
|
2020-12-10 12:37:15 +01:00
|
|
|
def __eq__(self, other):
|
|
|
|
if other is None:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if not self.is_authenticated or not other.is_authenticated:
|
|
|
|
return False
|
|
|
|
|
|
|
|
assert self.id > 0
|
|
|
|
return self.id == other.id
|
|
|
|
|
|
|
|
def can_see_edit_profile(self, current_user):
|
2023-06-18 22:56:19 +02:00
|
|
|
return self.check_perm(current_user, Permission.CHANGE_USERNAMES) or \
|
|
|
|
self.check_perm(current_user, Permission.CHANGE_EMAIL) or \
|
|
|
|
self.check_perm(current_user, Permission.CHANGE_RANK)
|
2020-12-10 12:37:15 +01:00
|
|
|
|
|
|
|
def can_delete(self):
|
2020-12-11 00:08:53 +01:00
|
|
|
from app.models import ForumTopic
|
2023-08-10 17:31:16 +02:00
|
|
|
return self.packages.count() == 0 and \
|
|
|
|
ForumTopic.query.filter_by(author=self).count() == 0 and \
|
|
|
|
self.rank != UserRank.BANNED
|
2020-12-10 12:37:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
class UserEmailVerification(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
2020-12-10 17:49:37 +01:00
|
|
|
user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
|
|
|
|
email = db.Column(db.String(100), nullable=False)
|
|
|
|
token = db.Column(db.String(32), nullable=True)
|
2021-01-24 13:58:29 +01:00
|
|
|
user = db.relationship("User", foreign_keys=[user_id], back_populates="email_verifications")
|
2020-12-10 12:37:15 +01:00
|
|
|
is_password_reset = db.Column(db.Boolean, nullable=False, default=False)
|
2021-11-24 18:35:38 +01:00
|
|
|
created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow)
|
2020-12-10 12:37:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
class EmailSubscription(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
email = db.Column(db.String(100), nullable=False, unique=True)
|
|
|
|
blacklisted = db.Column(db.Boolean, nullable=False, default=False)
|
|
|
|
token = db.Column(db.String(32), nullable=True, default=None)
|
|
|
|
|
|
|
|
def __init__(self, email):
|
|
|
|
self.email = email
|
|
|
|
self.blacklisted = False
|
|
|
|
self.token = None
|
|
|
|
|
2022-02-12 15:06:04 +01:00
|
|
|
@property
|
|
|
|
def url(self):
|
2023-06-19 20:32:36 +02:00
|
|
|
from app.utils import abs_url_for
|
2022-02-12 15:06:04 +01:00
|
|
|
return abs_url_for('users.unsubscribe', token=self.token)
|
|
|
|
|
2020-12-10 12:37:15 +01:00
|
|
|
|
|
|
|
class NotificationType(enum.Enum):
|
|
|
|
# Package / release / etc
|
|
|
|
PACKAGE_EDIT = 1
|
|
|
|
|
|
|
|
# Approval review actions
|
|
|
|
PACKAGE_APPROVAL = 2
|
|
|
|
|
|
|
|
# New thread
|
|
|
|
NEW_THREAD = 3
|
|
|
|
|
|
|
|
# New Review
|
|
|
|
NEW_REVIEW = 4
|
|
|
|
|
|
|
|
# Posted reply to subscribed thread
|
|
|
|
THREAD_REPLY = 5
|
|
|
|
|
2020-12-15 23:00:46 +01:00
|
|
|
# A bot notification
|
|
|
|
BOT = 6
|
|
|
|
|
2020-12-10 12:37:15 +01:00
|
|
|
# Added / removed as maintainer
|
2020-12-15 23:00:46 +01:00
|
|
|
MAINTAINER = 7
|
2020-12-10 12:37:15 +01:00
|
|
|
|
|
|
|
# Editor misc
|
2020-12-15 23:00:46 +01:00
|
|
|
EDITOR_ALERT = 8
|
2020-12-10 12:37:15 +01:00
|
|
|
|
|
|
|
# Editor misc
|
2020-12-15 23:00:46 +01:00
|
|
|
EDITOR_MISC = 9
|
2020-12-10 12:37:15 +01:00
|
|
|
|
|
|
|
# Any other
|
|
|
|
OTHER = 0
|
|
|
|
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def get_title(self):
|
2020-12-10 12:37:15 +01:00
|
|
|
return self.name.replace("_", " ").title()
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def to_name(self):
|
2020-12-10 12:37:15 +01:00
|
|
|
return self.name.lower()
|
|
|
|
|
|
|
|
def get_description(self):
|
|
|
|
if self == NotificationType.PACKAGE_EDIT:
|
|
|
|
return "When another user edits your packages, releases, etc."
|
|
|
|
elif self == NotificationType.PACKAGE_APPROVAL:
|
|
|
|
return "Notifications from editors related to the package approval process."
|
|
|
|
elif self == NotificationType.NEW_THREAD:
|
|
|
|
return "When a thread is created on your package."
|
|
|
|
elif self == NotificationType.NEW_REVIEW:
|
|
|
|
return "When a user posts a review on your package."
|
|
|
|
elif self == NotificationType.THREAD_REPLY:
|
|
|
|
return "When someone replies to a thread you're watching."
|
2020-12-15 23:00:46 +01:00
|
|
|
elif self == NotificationType.BOT:
|
|
|
|
return "From a bot - for example, update notifications."
|
2020-12-10 12:37:15 +01:00
|
|
|
elif self == NotificationType.MAINTAINER:
|
|
|
|
return "When your package's maintainers change."
|
|
|
|
elif self == NotificationType.EDITOR_ALERT:
|
|
|
|
return "For editors: Important alerts."
|
|
|
|
elif self == NotificationType.EDITOR_MISC:
|
|
|
|
return "For editors: Minor notifications, including new threads."
|
|
|
|
elif self == NotificationType.OTHER:
|
|
|
|
return "Minor notifications not important enough for a dedicated category."
|
|
|
|
else:
|
|
|
|
return ""
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
def __lt__(self, other):
|
|
|
|
return self.value < other.value
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def choices(cls):
|
2023-06-18 22:56:19 +02:00
|
|
|
return [(choice, choice.get_title()) for choice in cls]
|
2020-12-10 12:37:15 +01:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def coerce(cls, item):
|
2021-02-28 17:04:31 +01:00
|
|
|
return item if type(item) == NotificationType else NotificationType[item.upper()]
|
2020-12-10 12:37:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Notification(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
|
|
|
|
user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
|
|
|
|
user = db.relationship("User", foreign_keys=[user_id], back_populates="notifications")
|
|
|
|
|
|
|
|
causer_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
|
|
|
|
causer = db.relationship("User", foreign_keys=[causer_id], back_populates="caused_notifications")
|
|
|
|
|
|
|
|
type = db.Column(db.Enum(NotificationType), nullable=False, default=NotificationType.OTHER)
|
|
|
|
|
|
|
|
emailed = db.Column(db.Boolean(), nullable=False, default=False)
|
|
|
|
|
|
|
|
title = db.Column(db.String(100), nullable=False)
|
|
|
|
url = db.Column(db.String(200), nullable=True)
|
|
|
|
|
|
|
|
package_id = db.Column(db.Integer, db.ForeignKey("package.id"), nullable=True)
|
2020-12-10 17:49:37 +01:00
|
|
|
package = db.relationship("Package", foreign_keys=[package_id], back_populates="notifications")
|
2020-12-10 12:37:15 +01:00
|
|
|
|
2020-12-10 17:49:37 +01:00
|
|
|
created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow)
|
2020-12-10 12:37:15 +01:00
|
|
|
|
|
|
|
def __init__(self, user, causer, type, title, url, package=None):
|
|
|
|
if len(title) > 100:
|
|
|
|
title = title[:99] + "…"
|
|
|
|
|
|
|
|
self.user = user
|
|
|
|
self.causer = causer
|
|
|
|
self.type = type
|
|
|
|
self.title = title
|
|
|
|
self.url = url
|
|
|
|
self.package = package
|
|
|
|
|
|
|
|
def can_send_email(self):
|
|
|
|
prefs = self.user.notification_preferences
|
|
|
|
return prefs and self.user.email and prefs.get_can_email(self.type)
|
|
|
|
|
|
|
|
def can_send_digest(self):
|
|
|
|
prefs = self.user.notification_preferences
|
|
|
|
return prefs and self.user.email and prefs.get_can_digest(self.type)
|
|
|
|
|
|
|
|
|
|
|
|
class UserNotificationPreferences(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
|
|
|
|
user = db.relationship("User", back_populates="notification_preferences")
|
|
|
|
|
|
|
|
# 2 = immediate emails
|
|
|
|
# 1 = daily digest emails
|
|
|
|
# 0 = no emails
|
|
|
|
|
|
|
|
pref_package_edit = db.Column(db.Integer, nullable=False)
|
|
|
|
pref_package_approval = db.Column(db.Integer, nullable=False)
|
|
|
|
pref_new_thread = db.Column(db.Integer, nullable=False)
|
|
|
|
pref_new_review = db.Column(db.Integer, nullable=False)
|
|
|
|
pref_thread_reply = db.Column(db.Integer, nullable=False)
|
2020-12-15 23:00:46 +01:00
|
|
|
pref_bot = db.Column(db.Integer, nullable=False)
|
2020-12-10 12:37:15 +01:00
|
|
|
pref_maintainer = db.Column(db.Integer, nullable=False)
|
|
|
|
pref_editor_alert = db.Column(db.Integer, nullable=False)
|
|
|
|
pref_editor_misc = db.Column(db.Integer, nullable=False)
|
|
|
|
pref_other = db.Column(db.Integer, nullable=False)
|
|
|
|
|
|
|
|
def __init__(self, user):
|
|
|
|
self.user = user
|
|
|
|
self.pref_package_edit = 1
|
|
|
|
self.pref_package_approval = 1
|
|
|
|
self.pref_new_thread = 1
|
|
|
|
self.pref_new_review = 1
|
|
|
|
self.pref_thread_reply = 2
|
2020-12-15 23:00:46 +01:00
|
|
|
self.pref_bot = 1
|
2020-12-10 12:37:15 +01:00
|
|
|
self.pref_maintainer = 1
|
|
|
|
self.pref_editor_alert = 1
|
|
|
|
self.pref_editor_misc = 0
|
|
|
|
self.pref_other = 0
|
|
|
|
|
|
|
|
def get_can_email(self, notification_type):
|
2023-06-18 22:56:19 +02:00
|
|
|
return getattr(self, "pref_" + notification_type.to_name()) == 2
|
2020-12-10 12:37:15 +01:00
|
|
|
|
|
|
|
def set_can_email(self, notification_type, value):
|
|
|
|
value = 2 if value else 0
|
2023-06-18 22:56:19 +02:00
|
|
|
setattr(self, "pref_" + notification_type.to_name(), value)
|
2020-12-10 12:37:15 +01:00
|
|
|
|
|
|
|
def get_can_digest(self, notification_type):
|
2023-06-18 22:56:19 +02:00
|
|
|
return getattr(self, "pref_" + notification_type.to_name()) >= 1
|
2020-12-10 12:37:15 +01:00
|
|
|
|
|
|
|
def set_can_digest(self, notification_type, value):
|
|
|
|
if self.get_can_email(notification_type):
|
|
|
|
return
|
|
|
|
|
|
|
|
value = 1 if value else 0
|
2023-06-18 22:56:19 +02:00
|
|
|
setattr(self, "pref_" + notification_type.to_name(), value)
|
2022-02-13 11:37:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
class UserBan(db.Model):
|
|
|
|
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)
|
|
|
|
user = db.relationship("User", foreign_keys=[user_id], back_populates="ban")
|
|
|
|
|
|
|
|
message = db.Column(db.UnicodeText, nullable=False)
|
|
|
|
|
|
|
|
banned_by_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
|
|
|
|
banned_by = db.relationship("User", foreign_keys=[banned_by_id])
|
|
|
|
|
|
|
|
created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow)
|
|
|
|
|
|
|
|
expires_at = db.Column(db.DateTime, nullable=True, default=None)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def has_expired(self):
|
|
|
|
return self.expires_at and datetime.datetime.now() > self.expires_at
|
2023-10-31 19:40:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
class OAuthClient(db.Model):
|
|
|
|
__tablename__ = "oauth_client"
|
|
|
|
|
|
|
|
id = db.Column(db.String(24), primary_key=True)
|
|
|
|
title = db.Column(db.String(64), unique=True)
|
|
|
|
secret = db.Column(db.String(32))
|
|
|
|
redirect_url = db.Column(db.String(128))
|
|
|
|
|
|
|
|
owner_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
|
|
|
|
owner = db.relationship("User", foreign_keys=[owner_id], back_populates="clients")
|
|
|
|
|
|
|
|
tokens = db.relationship("APIToken", back_populates="client", lazy="dynamic", cascade="all, delete, delete-orphan")
|
|
|
|
|
|
|
|
created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow)
|