subs toggle

master
Aevann1 2022-03-06 01:53:43 +02:00
parent c39c3fe146
commit fd5a0ae9ca
8 changed files with 146 additions and 40 deletions

View File

@ -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 *

View File

@ -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"<SubSubscription(user_id={self.user_id}, sub={self.sub})>"

View File

@ -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

View File

@ -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))

View File

@ -6,6 +6,50 @@ from .front import frontlist
@app.post("/s/<sub>/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/<sub>/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/<sub>/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/<mode>")
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)

View File

@ -63,12 +63,21 @@
<div class="dropdown dropdown-actions mx-2 ddd">
<button class="btn btn-secondary dropdown-toggle text-small-m ddd" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="fas fa-user-group mr-2 text-small-m"></i>
{{subs | capitalize}}
{% if v.subs == 1 %}
Include subscribed subs
{% elif v.subs == 2 %}
Include all subs
{% elif v.subs == 3 %}
View subscribed subs only
{% elif v.subs == 4 %}
View subs only
{% endif %}
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton" x-placement="bottom-start" style="position: absolute; will-change: transform; top: 0px; left: 0px; transform: translate3d(0px, 31px, 0px);">
{% if subs != "Exclude subs" %}<a class="dropdown-item text-small-m" href="/sub_toggle/Exclude%20subs"><i class="fas fa-user-group mr-2 text-small-m"></i>Exclude subs</a>{% endif %}
{% if subs != "Include subs" %}<a class="dropdown-item text-small-m" href="/sub_toggle/Include%20subs"><i class="fas fa-user-group mr-2 text-small-m"></i>Include subs</a>{% endif %}
{% if subs != "View subs only" %}<a class="dropdown-item text-small-m" href="/sub_toggle/View%20subs%20only"><i class="fas fa-user-group mr-2 text-small-m"></i>View subs only</a>{% endif %}
{% if subs != 1 %}<a class="dropdown-item text-small-m" href="/sub_toggle/1"><i class="fas fa-user-group mr-2 text-small-m"></i>Include subscribed subs</a>{% endif %}
{% if subs != 2 %}<a class="dropdown-item text-small-m" href="/sub_toggle/2"><i class="fas fa-user-group mr-2 text-small-m"></i>Include all subs</a>{% endif %}
{% if subs != 3 %}<a class="dropdown-item text-small-m" href="/sub_toggle/3"><i class="fas fa-user-group mr-2 text-small-m"></i>View subscribed subs only</a>{% endif %}
{% if subs != 4 %}<a class="dropdown-item text-small-m" href="/sub_toggle/4"><i class="fas fa-user-group mr-2 text-small-m"></i>View subs only</a>{% endif %}
</div>
</div>
{% endif %}

View File

@ -13,17 +13,22 @@
{% if sub.sidebar_html %}
<div class="mb-4">{{sub.sidebar_html|safe}}</div>
{% endif %}
{% if v and v.id in (AEVANN_ID,CARP_ID) %}
<a class="btn btn-primary btn-block" href="/create_sub">CREATE SUB</a>
{% endif %}
{% if v and v.mods(sub.name) %}
<a class="btn btn-primary btn-block" href="/s/{{sub.name}}/settings">SUB SETTINGS</a>
{% endif %}
{% if v %}
<a class="btn btn-primary btn-block {% if v.subscribed_to(sub.name) %}d-none{% endif %}" onclick="post_toast(this,'/s/{{sub.name}}/subscribe','subscribe-sub','unsubscribe-sub');this.classList.toggle('d-none');nextElementSibling.classList.toggle('d-none')">SUBSCRIBE</a>
<a class="btn btn-primary btn-block {% if not v.subscribed_to(sub.name) %}d-none{% endif %}" onclick="post_toast(this,'/s/{{sub.name}}/unsubscribe','subscribe-sub','unsubscribe-sub');this.classList.toggle('d-none');previousElementSibling.classList.toggle('d-none')">UNSUBSCRIBE</a>
<a class="btn btn-primary btn-block {% if v.blocks(sub.name) %}d-none{% endif %}" onclick="post_toast(this,'/s/{{sub.name}}/block','block-sub','unblock-sub');this.classList.toggle('d-none');nextElementSibling.classList.toggle('d-none')">BLOCK SUB</a>
<a class="btn btn-primary btn-block {% if not v.blocks(sub.name) %}d-none{% endif %}" onclick="post_toast(this,'/s/{{sub.name}}/unblock','block-sub','unblock-sub');this.classList.toggle('d-none');previousElementSibling.classList.toggle('d-none')">UNBLOCK SUB</a>
{% if v.id in (AEVANN_ID,CARP_ID) %}
<a class="btn btn-primary btn-block" href="/create_sub">CREATE SUB</a>
{% endif %}
{% if v.mods(sub.name) %}
<a class="btn btn-primary btn-block" href="/s/{{sub.name}}/settings">SUB SETTINGS</a>
{% endif %}
{% endif %}
<a class="btn btn-primary btn-block" href="/s/{{sub.name}}/mods">MODS</a>
<a class="btn btn-primary btn-block" href="/s/{{sub.name}}/subscribers">SUBSCRIBERS</a>
<a class="btn btn-primary btn-block" href="/s/{{sub.name}}/exilees">EXILEES</a>
<a class="btn btn-primary btn-block" href="/s/{{sub.name}}/blockers">BLOCKERS</a>
{% else %}

View File

@ -4,19 +4,24 @@
{% if sub.sidebar_html %}
<div class="mb-4">{{sub.sidebar_html|safe}}</div>
{% endif %}
{% if v and v.mods(sub.name) %}
<a class="btn btn-primary btn-block" href="/s/{{sub.name}}/settings">SUB SETTINGS</a>
{% endif %}
{% if v %}
<a class="btn btn-primary btn-block {% if v.subscribed_to(sub.name) %}d-none{% endif %}" onclick="post_toast(this,'/s/{{sub.name}}/subscribe','subscribe-sub','unsubscribe-sub');this.classList.toggle('d-none');nextElementSibling.classList.toggle('d-none')">SUBSCRIBE</a>
<a class="btn btn-primary btn-block {% if not v.subscribed_to(sub.name) %}d-none{% endif %}" onclick="post_toast(this,'/s/{{sub.name}}/unsubscribe','subscribe-sub','unsubscribe-sub');this.classList.toggle('d-none');previousElementSibling.classList.toggle('d-none')">UNSUBSCRIBE</a>
<a class="btn btn-primary btn-block {% if v.blocks(sub.name) %}d-none{% endif %}" onclick="post_toast(this,'/s/{{sub.name}}/block','block-sub','unblock-sub');this.classList.toggle('d-none');nextElementSibling.classList.toggle('d-none')">BLOCK SUB</a>
<a class="btn btn-primary btn-block {% if not v.blocks(sub.name) %}d-none{% endif %}" onclick="post_toast(this,'/s/{{sub.name}}/unblock','block-sub','unblock-sub');this.classList.toggle('d-none');previousElementSibling.classList.toggle('d-none')">UNBLOCK SUB</a>
<a class="btn btn-primary btn-block" href="/create_sub">CREATE SUB</a>
{% if v.mods(sub.name) %}
<a class="btn btn-primary btn-block" href="/s/{{sub.name}}/settings">SUB SETTINGS</a>
{% endif %}
{% endif %}
<a class="btn btn-primary btn-block" href="/s/{{sub.name}}/mods">MODS</a>
<a class="btn btn-primary btn-block" href="/s/{{sub.name}}/subscribers">SUBSCRIBERS</a>
<a class="btn btn-primary btn-block" href="/s/{{sub.name}}/exilees">EXILEES</a>
<a class="btn btn-primary btn-block" href="/s/{{sub.name}}/blockers">BLOCKERS</a>
{% endif %}
<a class="btn btn-primary btn-block" href="/create_sub">CREATE SUB</a>
<a class="btn btn-primary btn-block mt-5" href="https://ip2.network">STREAM LIST</a>
<a class="btn btn-primary btn-block" href="/post/4103">BUGS/SUGGESTIONS MEGATHREAD</a>
<a class="btn btn-primary btn-block" href="/post/9694" >OFFICIAL CONSPIRACY THEORY THREAD</a>