diff --git a/files/classes/__init__.py b/files/classes/__init__.py index 15c93d1ea..d03ccd68b 100644 --- a/files/classes/__init__.py +++ b/files/classes/__init__.py @@ -15,6 +15,7 @@ from .mod_logs import * from .award import * from .marsey import * from .sub_block import * +from .sub_subscription import * from .saves import * from .views import * from .notifications import * diff --git a/files/classes/sub_subscription.py b/files/classes/sub_subscription.py new file mode 100644 index 000000000..fbb1b0b20 --- /dev/null +++ b/files/classes/sub_subscription.py @@ -0,0 +1,12 @@ +from sqlalchemy import * +from sqlalchemy.orm import relationship +from files.__main__ import Base + +class SubSubscription(Base): + + __tablename__ = "sub_subscriptions" + user_id = Column(Integer, ForeignKey("users.id"), primary_key=True) + sub = Column(String, ForeignKey("subs.name"), primary_key=True) + + def __repr__(self): + return f"" \ No newline at end of file diff --git a/files/classes/user.py b/files/classes/user.py index 369193d71..5de8565da 100644 --- a/files/classes/user.py +++ b/files/classes/user.py @@ -17,6 +17,7 @@ from .mod_logs import * from .mod import * from .exiles import * from .sub_block import * +from .sub_subscription import * from files.__main__ import Base, cache from files.helpers.security import * import random @@ -26,6 +27,9 @@ defaulttheme = environ.get("DEFAULT_THEME", "midnight").strip() defaulttimefilter = environ.get("DEFAULT_TIME_FILTER", "all").strip() cardview = bool(int(environ.get("CARD_VIEW", 1))) +if SITE_NAME == 'Drama': defaultsubs = 1 +else: defaultsubs = 2 + class User(Base): __tablename__ = "users" @@ -127,6 +131,7 @@ class User(Base): original_username = deferred(Column(String)) referred_by = Column(Integer, ForeignKey("users.id")) subs_created = Column(Integer, default=0) + subs = Column(Integer, default=defaultsubs) badges = relationship("Badge", viewonly=True) subscriptions = relationship("Subscription", viewonly=True) @@ -163,10 +168,19 @@ class User(Base): def all_blocks(self): return [x[0] for x in g.db.query(SubBlock.sub).filter_by(user_id=self.id).all()] + @property + @lazy + def subbed_subs(self): + return [x[0] for x in g.db.query(SubSubscription.sub).filter_by(user_id=self.id).all()] + @lazy def blocks(self, sub): return g.db.query(SubBlock).filter_by(user_id=self.id, sub=sub).one_or_none() + @lazy + def subscribed_to(self, sub): + return g.db.query(SubSubscription).filter_by(user_id=self.id, sub=sub).one_or_none() + @lazy def mod_date(self, sub): if self.id == AEVANN_ID: return 1 diff --git a/files/routes/front.py b/files/routes/front.py index bf0332e10..06ca4c30b 100644 --- a/files/routes/front.py +++ b/files/routes/front.py @@ -167,29 +167,27 @@ def front_all(v, sub=None, subdomain=None): if sort == 'bump': t='all' - if request.host == 'rdrama.net': defaultsubs = 'Exclude subs' - else: defaultsubs = 'Include subs' - - if v: subs=session.get('subs', defaultsubs) - else: subs=defaultsubs - try: gt=int(request.values.get("utc_greater_than", 0)) except: gt=0 try: lt=int(request.values.get("utc_less_than", 0)) except: lt=0 + if SITE_NAME == 'Drama': defaultsubs = 1 + else: defaultsubs = 2 + subs = v.subs if v else defaultsubs + ids, next_exists = frontlist(sort=sort, page=page, t=t, v=v, ccmode=ccmode, - subs=subs, filter_words=v.filter_words if v else [], gt=gt, lt=lt, sub=sub, site=SITE, + subs=subs ) posts = get_posts(ids, v=v) @@ -270,22 +268,28 @@ def front_all(v, sub=None, subdomain=None): g.db.commit() if request.headers.get("Authorization"): return {"data": [x.json for x in posts], "next_exists": next_exists} - return render_template("home.html", v=v, listing=posts, next_exists=next_exists, sort=sort, t=t, page=page, ccmode=ccmode, sub=sub, subs=subs, home=True) + return render_template("home.html", v=v, listing=posts, next_exists=next_exists, sort=sort, t=t, page=page, ccmode=ccmode, sub=sub, home=True) @cache.memoize(timeout=86400) -def frontlist(v=None, sort="hot", page=1, t="all", ids_only=True, ccmode="false", subs='Include subs', filter_words='', gt=0, lt=0, sub=None, site=None): +def frontlist(v=None, sort="hot", page=1, t="all", ids_only=True, ccmode="false", filter_words='', gt=0, lt=0, sub=None, site=None, subs=None): posts = g.db.query(Submission) - if sub: posts = posts.filter_by(sub=sub.name) - elif subs == "View subs only": - posts = posts.filter(Submission.sub != None) - if v and v.all_blocks: posts = posts.filter(Submission.sub.notin_(v.all_blocks)) - elif subs == "Include subs": - if v and v.all_blocks: posts = posts.filter(or_(Submission.sub == None, Submission.sub.notin_(v.all_blocks))) - else: posts = posts.filter(Submission.sub == None) + if sub: + posts = posts.filter_by(sub=sub.name) + elif not v: + posts = posts.filter(Submission.sub == None) + elif v.subs == 1: + posts = posts.filter(or_(Submission.sub == None, Submission.sub.in_(v.subbed_subs))) + elif v.subs == 2: + posts = posts.filter(or_(Submission.sub == None, Submission.sub.notin_(v.all_blocks))) + elif v.subs == 3: + posts = posts.filter(Submission.sub.in_(v.subbed_subs)) + elif v.subs == 4: + posts = posts.filter(Submission.sub != None, Submission.sub.notin_(v.all_blocks)) + if gt: posts = posts.filter(Submission.created_utc > gt) if lt: posts = posts.filter(Submission.created_utc < lt) @@ -352,13 +356,18 @@ def frontlist(v=None, sort="hot", page=1, t="all", ids_only=True, ccmode="false" if (sort == "hot" or (v and v.id == Q_ID)) and page == 1 and ccmode == "false" and not gt and not lt: pins = g.db.query(Submission).filter(Submission.stickied != None, Submission.is_banned == False) - if sub: pins = pins.filter_by(sub=sub.name) - elif subs == "View subs only": - pins = pins.filter(Submission.sub != None) - if v and v.all_blocks: pins = pins.filter(Submission.sub.notin_(v.all_blocks)) - elif subs == "Include subs": - if v and v.all_blocks: pins = pins.filter(or_(Submission.sub == None, Submission.sub.notin_(v.all_blocks))) - else: pins = pins.filter(Submission.sub == None) + if sub: + pins = pins.filter_by(sub=sub.name) + elif not v: + pins = pins.filter(Submission.sub == None) + elif v.subs == 1: + pins = pins.filter(or_(Submission.sub == None, Submission.sub.in_(v.subbed_subs))) + elif v.subs == 2: + pins = pins.filter(or_(Submission.sub == None, Submission.sub.notin_(v.all_blocks))) + elif v.subs == 3: + pins = pins.filter(Submission.sub.in_(v.subbed_subs)) + else: + pins = pins.filter(Submission.sub.notin_(v.all_blocks)) if v and v.admin_level < 2: pins = pins.filter(Submission.author_id.notin_(v.userblocks)) diff --git a/files/routes/subs.py b/files/routes/subs.py index 19e6f07d1..201b42adb 100644 --- a/files/routes/subs.py +++ b/files/routes/subs.py @@ -6,6 +6,50 @@ from .front import frontlist +@app.post("/s//subscribe") +@auth_required +def subscribe_sub(v, sub): + sub = g.db.query(Sub).filter_by(name=sub.strip().lower()).one_or_none() + if not sub: abort(404) + sub = sub.name + + existing = g.db.query(SubSubscription).filter_by(user_id=v.id, sub=sub).one_or_none() + + if not existing: + subscribe = SubSubscription(user_id=v.id, sub=sub) + g.db.add(subscribe) + g.db.commit() + cache.delete_memoized(frontlist) + + return {"message": "Subscribed to sub!"} + + +@app.post("/s//unsubscribe") +@auth_required +def unsubscribe_sub(v, sub): + sub = g.db.query(Sub).filter_by(name=sub.strip().lower()).one_or_none() + if not sub: abort(404) + sub = sub.name + + subscribe = g.db.query(SubSubscription).filter_by(user_id=v.id, sub=sub).one_or_none() + + if subscribe: + g.db.delete(subscribe) + g.db.commit() + cache.delete_memoized(frontlist) + + return {"message": "Unsubscribed from sub!"} + + +@app.get("/s//subscribers") +@auth_required +def subscribers(v, sub): + sub = g.db.query(Sub).filter_by(name=sub.strip().lower()).one_or_none() + if not sub: abort(404) + + users = g.db.query(User).join(SubSubscription, SubSubscription.user_id==User.id).filter_by(sub=sub.name).all() + + return render_template("sub/subscribers.html", v=v, sub=sub, users=users) @@ -130,7 +174,7 @@ def block_sub(v, sub): if not sub: abort(404) sub = sub.name - # if v.mods(sub): return {"error": "You can't block subs you mod!"} + if v.mods(sub): return {"error": "You can't block subs you mod!"} existing = g.db.query(SubBlock).filter_by(user_id=v.id, sub=sub).one_or_none() @@ -450,8 +494,15 @@ def sub_sidebar(v, sub): @app.get("/sub_toggle/") -def sub_toggle(mode): - if mode in ('Exclude subs', 'Include subs', 'View subs only'): session["subs"] = mode +@auth_required +def sub_toggle(mode, v): + try: mode = int(mode) + except: abort(400) + + if mode in (1,2,3,4) and v.subs != mode: + v.subs = mode + g.db.add(v) + g.db.commit() if request.referrer and len(request.referrer) > 1 and request.referrer.startswith(SITE_FULL): return redirect(request.referrer) diff --git a/files/templates/home.html b/files/templates/home.html index 12e97bb06..6829fe2f1 100644 --- a/files/templates/home.html +++ b/files/templates/home.html @@ -63,12 +63,21 @@ {% endif %} diff --git a/files/templates/sidebar_Drama.html b/files/templates/sidebar_Drama.html index 1b7878ef5..f3b3d85c6 100644 --- a/files/templates/sidebar_Drama.html +++ b/files/templates/sidebar_Drama.html @@ -13,17 +13,22 @@ {% if sub.sidebar_html %}
{{sub.sidebar_html|safe}}
{% endif %} - {% if v and v.id in (AEVANN_ID,CARP_ID) %} - CREATE SUB - {% endif %} - {% if v and v.mods(sub.name) %} - SUB SETTINGS - {% endif %} {% if v %} + SUBSCRIBE + UNSUBSCRIBE + BLOCK SUB UNBLOCK SUB + + {% if v.id in (AEVANN_ID,CARP_ID) %} + CREATE SUB + {% endif %} + {% if v.mods(sub.name) %} + SUB SETTINGS + {% endif %} {% endif %} MODS + SUBSCRIBERS EXILEES BLOCKERS {% else %} diff --git a/files/templates/sidebar_PCM.html b/files/templates/sidebar_PCM.html index 81993dc09..f2f6379dc 100644 --- a/files/templates/sidebar_PCM.html +++ b/files/templates/sidebar_PCM.html @@ -4,19 +4,24 @@ {% if sub.sidebar_html %}
{{sub.sidebar_html|safe}}
{% endif %} - {% if v and v.mods(sub.name) %} - SUB SETTINGS - {% endif %} {% if v %} + SUBSCRIBE + UNSUBSCRIBE + BLOCK SUB UNBLOCK SUB + + CREATE SUB + {% if v.mods(sub.name) %} + SUB SETTINGS + {% endif %} {% endif %} MODS + SUBSCRIBERS EXILEES BLOCKERS {% endif %} -CREATE SUB STREAM LIST BUGS/SUGGESTIONS MEGATHREAD OFFICIAL CONSPIRACY THEORY THREAD