2022-07-09 10:32:49 +00:00
|
|
|
import time
|
2022-11-15 09:19:08 +00:00
|
|
|
|
2022-10-11 16:41:09 +00:00
|
|
|
from sqlalchemy.sql import func
|
2023-09-07 20:08:10 +00:00
|
|
|
from sqlalchemy.sql.sqltypes import *
|
2023-08-16 19:13:11 +00:00
|
|
|
from flask import g
|
2022-07-09 10:32:49 +00:00
|
|
|
|
2022-12-11 23:44:34 +00:00
|
|
|
from files.helpers.config.const import *
|
2022-11-15 09:19:08 +00:00
|
|
|
|
2023-04-29 15:45:05 +00:00
|
|
|
from files.classes.subscriptions import Subscription
|
|
|
|
from files.classes.saves import *
|
|
|
|
|
2022-10-12 08:05:26 +00:00
|
|
|
def apply_time_filter(t, objects, cls):
|
2022-07-09 10:32:49 +00:00
|
|
|
now = int(time.time())
|
2023-08-11 15:41:57 +00:00
|
|
|
|
2022-07-09 10:32:49 +00:00
|
|
|
if t == 'hour':
|
|
|
|
cutoff = now - 3600
|
|
|
|
elif t == 'day':
|
|
|
|
cutoff = now - 86400
|
|
|
|
elif t == 'week':
|
|
|
|
cutoff = now - 604800
|
|
|
|
elif t == 'month':
|
|
|
|
cutoff = now - 2592000
|
|
|
|
elif t == 'year':
|
|
|
|
cutoff = now - 31536000
|
|
|
|
else:
|
2023-08-11 15:41:57 +00:00
|
|
|
return objects
|
2022-07-09 10:32:49 +00:00
|
|
|
|
2022-10-12 08:05:26 +00:00
|
|
|
return objects.filter(cls.created_utc >= cutoff)
|
2023-09-07 15:26:31 +00:00
|
|
|
|
2022-10-12 08:05:26 +00:00
|
|
|
|
2023-02-27 15:38:12 +00:00
|
|
|
def sort_objects(sort, objects, cls):
|
2022-10-12 08:05:26 +00:00
|
|
|
if sort == 'hot':
|
2023-09-07 08:57:32 +00:00
|
|
|
if not (SITE == 'watchpeopledie.tv' and g.v and g.v.id == GTIX_ID):
|
2023-09-07 20:08:10 +00:00
|
|
|
objects = objects.order_by(func.cast(cls.is_banned, Boolean), func.cast(cls.deleted_utc, Boolean))
|
2023-09-07 08:57:32 +00:00
|
|
|
|
2022-10-12 08:05:26 +00:00
|
|
|
ti = int(time.time()) + 3600
|
2023-09-04 18:28:38 +00:00
|
|
|
metric = cls.realupvotes + 1
|
2023-06-07 23:26:32 +00:00
|
|
|
if cls.__name__ == "Post": metric += cls.comment_count/5
|
2023-09-04 18:28:38 +00:00
|
|
|
return objects.order_by(-1000000*(metric / func.power(((ti - cls.created_utc)/1000), 1.4)), cls.created_utc.desc())
|
2023-06-07 23:26:32 +00:00
|
|
|
elif sort == "views" and cls.__name__ == "Post":
|
2023-04-27 14:30:35 +00:00
|
|
|
return objects.order_by(cls.views.desc(), cls.created_utc.desc())
|
2023-07-25 22:37:54 +00:00
|
|
|
elif sort == "bump" and cls.__name__ == "Post":
|
|
|
|
return objects.filter(cls.comment_count > 1).order_by(cls.bump_utc.desc(), cls.created_utc.desc())
|
2023-06-07 23:26:32 +00:00
|
|
|
elif sort == "comments" and cls.__name__ == "Post":
|
2022-10-12 08:05:26 +00:00
|
|
|
return objects.order_by(cls.comment_count.desc(), cls.created_utc.desc())
|
2023-06-07 23:26:32 +00:00
|
|
|
elif sort == "subscriptions" and cls.__name__ == "Post":
|
|
|
|
return objects.outerjoin(Subscription, Subscription.post_id == cls.id).group_by(cls.id).order_by(func.count(Subscription.post_id).desc(), cls.created_utc.desc())
|
|
|
|
elif sort == "saves" and cls.__name__ == "Post":
|
|
|
|
return objects.outerjoin(SaveRelationship, SaveRelationship.post_id == cls.id).group_by(cls.id).order_by(func.count(SaveRelationship.post_id).desc(), cls.created_utc.desc())
|
2022-10-12 08:05:26 +00:00
|
|
|
elif sort == "new":
|
|
|
|
return objects.order_by(cls.created_utc.desc())
|
|
|
|
elif sort == "old":
|
|
|
|
return objects.order_by(cls.created_utc)
|
2023-06-07 23:26:32 +00:00
|
|
|
elif sort == "controversial" and cls.__name__ == "Post":
|
2023-05-13 03:31:45 +00:00
|
|
|
return objects.order_by((cls.upvotes+1)/(cls.downvotes+1) + (cls.downvotes+1)/(cls.upvotes+1) - cls.comment_count/500, cls.downvotes.desc(), cls.created_utc.desc())
|
2022-10-12 08:05:26 +00:00
|
|
|
elif sort == "controversial":
|
|
|
|
return objects.order_by((cls.upvotes+1)/(cls.downvotes+1) + (cls.downvotes+1)/(cls.upvotes+1), cls.downvotes.desc(), cls.created_utc.desc())
|
|
|
|
elif sort == "bottom":
|
|
|
|
return objects.order_by(cls.upvotes - cls.downvotes, cls.created_utc.desc())
|
2023-07-25 22:37:54 +00:00
|
|
|
elif sort == "random":
|
|
|
|
return objects.order_by(func.random())
|
2022-10-13 16:21:26 +00:00
|
|
|
else:
|
2022-10-12 08:05:26 +00:00
|
|
|
return objects.order_by(cls.downvotes - cls.upvotes, cls.created_utc.desc())
|
2022-10-15 18:11:43 +00:00
|
|
|
|
2023-07-30 00:42:06 +00:00
|
|
|
def make_age_string(compare):
|
2022-10-16 06:57:55 +00:00
|
|
|
if not compare or compare < 1577865600: return ""
|
2022-10-15 18:11:43 +00:00
|
|
|
age = int(time.time()) - compare
|
|
|
|
|
|
|
|
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(compare)
|
|
|
|
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"
|