Reintroduce New Member rank

Fixes #183
This commit is contained in:
rubenwardy 2022-08-23 02:24:12 +01:00
parent 1d36f7d12b
commit b4208f2dda
8 changed files with 54 additions and 20 deletions

@ -119,7 +119,7 @@ def check_for_ban():
logout_user() logout_user()
return redirect(url_for("users.login")) return redirect(url_for("users.login"))
elif current_user.rank == models.UserRank.NOT_JOINED: elif current_user.rank == models.UserRank.NOT_JOINED:
current_user.rank = models.UserRank.MEMBER current_user.rank = models.UserRank.NEW_MEMBER
models.db.session.commit() models.db.session.commit()

@ -14,7 +14,7 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
import datetime
import os import os
from typing import List from typing import List
@ -25,7 +25,7 @@ from sqlalchemy import or_, and_
from app.logic.game_support import GameSupportResolver from app.logic.game_support import GameSupportResolver
from app.models import PackageRelease, db, Package, PackageState, PackageScreenshot, MetaPackage, User, \ from app.models import PackageRelease, db, Package, PackageState, PackageScreenshot, MetaPackage, User, \
NotificationType, PackageUpdateConfig, License, UserRank, PackageType, PackageGameSupport NotificationType, PackageUpdateConfig, License, UserRank, PackageType, PackageGameSupport, ThreadReply
from app.tasks.emails import send_pending_digests from app.tasks.emails import send_pending_digests
from app.tasks.forumtasks import importTopicList, checkAllForumAccounts from app.tasks.forumtasks import importTopicList, checkAllForumAccounts
from app.tasks.importtasks import importRepoScreenshot, checkZipRelease, check_for_updates, updateAllGameSupport from app.tasks.importtasks import importRepoScreenshot, checkZipRelease, check_for_updates, updateAllGameSupport
@ -335,3 +335,14 @@ def detect_game_support():
@action("Send pending notif digests") @action("Send pending notif digests")
def do_send_pending_digests(): def do_send_pending_digests():
send_pending_digests.delay() send_pending_digests.delay()
@action("Set users to new member")
def set_new_members():
threshold = datetime.datetime.now() - datetime.timedelta(days=7)
User.query.filter(User.rank == UserRank.MEMBER,
~User.replies.any(ThreadReply.created_at < threshold),
~User.packages.any(Package.approved_at < threshold)).update({"rank": UserRank.NEW_MEMBER}, synchronize_session=False)
db.session.commit()

@ -666,14 +666,14 @@ class Package(db.Model):
isApprover = user.rank.atLeast(UserRank.APPROVER) isApprover = user.rank.atLeast(UserRank.APPROVER)
if perm == Permission.CREATE_THREAD: if perm == Permission.CREATE_THREAD:
return user.rank.atLeast(UserRank.MEMBER) return user.rank.atLeast(UserRank.NEW_MEMBER)
# Members can edit their own packages, and editors can edit any packages # Members can edit their own packages, and editors can edit any packages
elif perm == Permission.MAKE_RELEASE or perm == Permission.ADD_SCREENSHOTS: elif perm == Permission.MAKE_RELEASE or perm == Permission.ADD_SCREENSHOTS:
return isMaintainer return isMaintainer
elif perm == Permission.EDIT_PACKAGE: elif perm == Permission.EDIT_PACKAGE:
return isMaintainer and user.rank.atLeast(UserRank.MEMBER if self.approved else UserRank.NEW_MEMBER) return isMaintainer and user.rank.atLeast(UserRank.NEW_MEMBER)
elif perm == Permission.APPROVE_RELEASE: elif perm == Permission.APPROVE_RELEASE:
return (isMaintainer or isApprover) and user.rank.atLeast(UserRank.MEMBER if self.approved else UserRank.NEW_MEMBER) return (isMaintainer or isApprover) and user.rank.atLeast(UserRank.MEMBER if self.approved else UserRank.NEW_MEMBER)
@ -688,7 +688,7 @@ class Package(db.Model):
elif perm == Permission.APPROVE_SCREENSHOT: elif perm == Permission.APPROVE_SCREENSHOT:
return (isMaintainer or isApprover) and \ return (isMaintainer or isApprover) and \
user.rank.atLeast(UserRank.TRUSTED_MEMBER if self.approved else UserRank.NEW_MEMBER) user.rank.atLeast(UserRank.MEMBER if self.approved else UserRank.NEW_MEMBER)
elif perm == Permission.EDIT_MAINTAINERS or perm == Permission.DELETE_PACKAGE: elif perm == Permission.EDIT_MAINTAINERS or perm == Permission.DELETE_PACKAGE:
return isOwner or user.rank.atLeast(UserRank.EDITOR) return isOwner or user.rank.atLeast(UserRank.EDITOR)

@ -153,7 +153,7 @@ class ThreadReply(db.Model):
raise Exception("Unknown permission given to ThreadReply.checkPerm()") raise Exception("Unknown permission given to ThreadReply.checkPerm()")
if perm == Permission.EDIT_REPLY: if perm == Permission.EDIT_REPLY:
return user.rank.atLeast(UserRank.MEMBER if user == self.author else UserRank.MODERATOR) and not self.thread.locked return user.rank.atLeast(UserRank.NEW_MEMBER if user == self.author else UserRank.MODERATOR) and not self.thread.locked
elif perm == Permission.DELETE_REPLY: elif perm == Permission.DELETE_REPLY:
return user.rank.atLeast(UserRank.MODERATOR) and self.thread.replies[0] != self return user.rank.atLeast(UserRank.MODERATOR) and self.thread.replies[0] != self

@ -227,7 +227,7 @@ class User(db.Model, UserMixin):
elif perm == Permission.CHANGE_EMAIL or perm == Permission.CHANGE_PROFILE_URLS: elif perm == Permission.CHANGE_EMAIL or perm == Permission.CHANGE_PROFILE_URLS:
return user == self or (user.rank.atLeast(UserRank.MODERATOR) and not self.rank.atLeast(user.rank)) return user == self or (user.rank.atLeast(UserRank.MODERATOR) and not self.rank.atLeast(user.rank))
elif perm == Permission.CHANGE_DISPLAY_NAME: elif perm == Permission.CHANGE_DISPLAY_NAME:
return user.rank.atLeast(UserRank.MEMBER if user == self else UserRank.MODERATOR) return user.rank.atLeast(UserRank.NEW_MEMBER if user == self else UserRank.MODERATOR)
elif perm == Permission.CREATE_TOKEN: elif perm == Permission.CREATE_TOKEN:
if user == self: if user == self:
return user.rank.atLeast(UserRank.MEMBER) return user.rank.atLeast(UserRank.MEMBER)
@ -243,16 +243,18 @@ class User(db.Model, UserMixin):
if self.rank.atLeast(UserRank.ADMIN): if self.rank.atLeast(UserRank.ADMIN):
return True return True
elif self.rank.atLeast(UserRank.TRUSTED_MEMBER): elif self.rank.atLeast(UserRank.TRUSTED_MEMBER):
factor *= 2 factor = 3
elif self.rank.atLeast(UserRank.MEMBER):
factor = 2
one_min_ago = datetime.datetime.utcnow() - datetime.timedelta(minutes=1) one_min_ago = datetime.datetime.utcnow() - datetime.timedelta(minutes=1)
if ThreadReply.query.filter_by(author=self) \ if ThreadReply.query.filter_by(author=self) \
.filter(ThreadReply.created_at > one_min_ago).count() >= 3 * factor: .filter(ThreadReply.created_at > one_min_ago).count() >= 2 * factor:
return False return False
hour_ago = datetime.datetime.utcnow() - datetime.timedelta(hours=1) hour_ago = datetime.datetime.utcnow() - datetime.timedelta(hours=1)
if ThreadReply.query.filter_by(author=self) \ if ThreadReply.query.filter_by(author=self) \
.filter(ThreadReply.created_at > hour_ago).count() >= 20 * factor: .filter(ThreadReply.created_at > hour_ago).count() >= 10 * factor:
return False return False
return True return True
@ -264,7 +266,9 @@ class User(db.Model, UserMixin):
if self.rank.atLeast(UserRank.ADMIN): if self.rank.atLeast(UserRank.ADMIN):
return True return True
elif self.rank.atLeast(UserRank.TRUSTED_MEMBER): elif self.rank.atLeast(UserRank.TRUSTED_MEMBER):
factor *= 5 factor = 5
elif self.rank.atLeast(UserRank.MEMBER):
factor = 2
hour_ago = datetime.datetime.utcnow() - datetime.timedelta(hours=1) hour_ago = datetime.datetime.utcnow() - datetime.timedelta(hours=1)
return Thread.query.filter_by(author=self) \ return Thread.query.filter_by(author=self) \

@ -76,7 +76,7 @@ CELERYBEAT_SCHEDULE = {
}, },
'check_for_updates': { 'check_for_updates': {
'task': 'app.tasks.importtasks.check_for_updates', 'task': 'app.tasks.importtasks.check_for_updates',
'schedule': crontab(minute=10, hour=1), # 0110 'schedule': crontab(minute=10, hour=2), # 0210
}, },
'send_pending_notifications': { 'send_pending_notifications': {
'task': 'app.tasks.emails.send_pending_notifications', 'task': 'app.tasks.emails.send_pending_notifications',
@ -90,6 +90,10 @@ CELERYBEAT_SCHEDULE = {
'task': 'app.tasks.usertasks.delete_inactive_users', 'task': 'app.tasks.usertasks.delete_inactive_users',
'schedule': crontab(minute=15), # every hour at quarter past 'schedule': crontab(minute=15), # every hour at quarter past
}, },
'upgrade_new_members': {
'task': 'app.tasks.usertasks.upgrade_new_members',
'schedule': crontab(minute=10, hour=3), # 0310
},
} }
celery.conf.beat_schedule = CELERYBEAT_SCHEDULE celery.conf.beat_schedule = CELERYBEAT_SCHEDULE

@ -16,16 +16,31 @@
import datetime import datetime
from app.models import User, db, UserRank
from sqlalchemy import or_, and_
from app.models import User, db, UserRank, ThreadReply, Package
from app.tasks import celery from app.tasks import celery
@celery.task() @celery.task()
def delete_inactive_users(): def delete_inactive_users():
threshold = datetime.datetime.now() - datetime.timedelta(hours=5) threshold = datetime.datetime.now() - datetime.timedelta(hours=5)
users = User.query.filter(User.is_active==False, User.packages==None, User.forum_topics==None, User.created_at<=threshold, User.rank==UserRank.NOT_JOINED).all() users = User.query.filter(User.is_active == False, User.packages == None, User.forum_topics == None,
for user in users: User.created_at <= threshold, User.rank == UserRank.NOT_JOINED).all()
db.session.delete(user) for user in users:
db.session.delete(user)
db.session.commit() db.session.commit()
@celery.task()
def upgrade_new_members():
threshold = datetime.datetime.now() - datetime.timedelta(days=7)
User.query.filter(and_(User.rank == UserRank.NEW_MEMBER, or_(
User.replies.any(ThreadReply.created_at < threshold),
User.packages.any(Package.approved_at < threshold)))).update({"rank": UserRank.MEMBER})
db.session.commit()

@ -59,7 +59,7 @@ def post_login(user: User, next_url):
def login_user_set_active(user: User, next_url: str = None, *args, **kwargs): def login_user_set_active(user: User, next_url: str = None, *args, **kwargs):
if user.rank == UserRank.NOT_JOINED and user.email is None: if user.rank == UserRank.NOT_JOINED and user.email is None:
user.rank = UserRank.MEMBER user.rank = UserRank.NEW_MEMBER
user.notification_preferences = UserNotificationPreferences(user) user.notification_preferences = UserNotificationPreferences(user)
user.is_active = True user.is_active = True
db.session.commit() db.session.commit()