diff --git a/chat/src/hooks/useEmojis.ts b/chat/src/hooks/useEmojis.ts index d97c1500c..a3670cfec 100644 --- a/chat/src/hooks/useEmojis.ts +++ b/chat/src/hooks/useEmojis.ts @@ -33,7 +33,7 @@ export function useEmojis() { // Retrieve the list. useEffect(() => { - fetch("/marsey_list.json") + fetch("/emojis") .then((res) => res.json()) .then(setEmojis) .catch(setError); diff --git a/files/assets/js/emoji_modal.js b/files/assets/js/emoji_modal.js index aaeef18db..9daeb4b09 100644 --- a/files/assets/js/emoji_modal.js +++ b/files/assets/js/emoji_modal.js @@ -187,10 +187,9 @@ const emojisSearchDictionary = { // get public emojis list const emojiRequest = new XMLHttpRequest(); -emojiRequest.open("GET", '/marsey_list.json'); +emojiRequest.open("GET", '/emojis'); emojiRequest.setRequestHeader('xhr', 'xhr'); emojiRequest.onload = async (e) => { - console.log("HERE") let emojis = JSON.parse(emojiRequest.response); if(! (emojis instanceof Array )) throw new TypeError("[EMOJI DIALOG] rDrama's server should have sent a JSON-coded Array!"); diff --git a/files/helpers/const.py b/files/helpers/const.py index 2d19150e8..f76149704 100644 --- a/files/helpers/const.py +++ b/files/helpers/const.py @@ -63,6 +63,9 @@ IS_LOCALHOST = SITE == "localhost" or SITE == "127.0.0.1" or SITE.startswith("19 if IS_LOCALHOST: SITE_FULL = 'http://' + SITE else: SITE_FULL = 'https://' + SITE +REDDIT_NOTIFS_CACHE_KEY = "reddit_notifications" +MARSEYS_CACHE_KEY = "marseys" +EMOJIS_CACHE_KEY = "emojis" if SITE_NAME == 'PCM': CC = "SPLASH MOUNTAIN" else: CC = "COUNTRY CLUB" @@ -283,6 +286,7 @@ PERMS = { # Minimum admin_level to perform action. } FEATURES = { + 'MARSEYS': True, 'MARSEYBUX': True, 'AWARDS': True, 'CHAT': True, @@ -372,7 +376,6 @@ ERROR_MARSEYS = { 500: "marseycarp3", } -EMOJI_MARSEYS = True EMOJI_SRCS = ['files/assets/emojis.json'] PIN_LIMIT = 3 diff --git a/files/helpers/get.py b/files/helpers/get.py index a2c1d8a9b..646d00351 100644 --- a/files/helpers/get.py +++ b/files/helpers/get.py @@ -95,6 +95,21 @@ def get_account(id:Union[str, int], v:Optional[User]=None, graceful=False, inclu return user +def get_accounts_dict(ids:Union[Iterable[str], Iterable[int]], v:Optional[User]=None, graceful=False, include_shadowbanned=True) -> Optional[dict[int, User]]: + if not ids: return {} + try: + ids = [int(id) for id in ids] + except: + if graceful: return None + abort(404) + + users = g.db.query(User).filter(any_(ids)) + if not (include_shadowbanned or (v and v.can_see_shadowbanned)): + users = users.filter(User.shadowbanned == None) + users = users.all() + if len(users) != len(ids) and not graceful: abort(404) + return {u.id:u for u in users} + def get_post(i:Union[str, int], v:Optional[User]=None, graceful=False) -> Optional[Submission]: try: i = int(i) except: diff --git a/files/helpers/offsitementions.py b/files/helpers/offsitementions.py index 701ca6979..c5004b0bd 100644 --- a/files/helpers/offsitementions.py +++ b/files/helpers/offsitementions.py @@ -40,12 +40,11 @@ def offsite_mentions_task(cache:Cache): notify_mentions([send_user], user_mentions, mention_str='mention of you') def get_mentions(cache:Cache, queries:Iterable[str], reddit_notifs_users=False): - CACHE_KEY = 'reddit_notifications' kinds = ['submission', 'comment'] mentions = [] exclude_subreddits = ['PokemonGoRaids', 'SubSimulatorGPT2', 'SubSimGPT2Interactive'] try: - after = int(cache.get(CACHE_KEY) or time.time()) + after = int(cache.get(const.REDDIT_NOTIFS_CACHE_KEY) or time.time()) except: print("Failed to retrieve last mention time from cache") after = time.time() diff --git a/files/routes/asset_submissions.py b/files/routes/asset_submissions.py index 620cf1348..20605c855 100644 --- a/files/routes/asset_submissions.py +++ b/files/routes/asset_submissions.py @@ -9,7 +9,6 @@ from files.helpers.const import * from files.helpers.get import * from files.helpers.media import * from files.helpers.useractions import * -from files.routes.static import marsey_list from files.routes.wrappers import * from files.__main__ import app, cache, limiter @@ -145,7 +144,8 @@ def approve_marsey(v, name): else: badge_grant(badge_id=17, user=author) purge_files_in_cache(f"https://{SITE}/e/{marsey.name}/webp") - cache.delete_memoized(marsey_list) + cache.delete(EMOJIS_CACHE_KEY) + cache.delete(MARSEYS_CACHE_KEY) move(f"/asset_submissions/marseys/{name}.webp", f"files/assets/images/emojis/{marsey.name}.webp") highquality = f"/asset_submissions/marseys/{name}" diff --git a/files/routes/static.py b/files/routes/static.py index 975b7d820..4ee8cd977 100644 --- a/files/routes/static.py +++ b/files/routes/static.py @@ -26,51 +26,39 @@ def rdrama(id, title): @app.get("/marseys") @auth_required def marseys(v): - if SITE == 'rdrama.net': - marseys = g.db.query(Marsey, User).join(User, Marsey.author_id == User.id).filter(Marsey.submitter_id==None) - sort = request.values.get("sort", "usage") - if sort == "usage": - marseys = marseys.order_by(Marsey.count.desc(), User.username).all() - elif sort == "added": - marseys = marseys.order_by(nullslast(Marsey.created_utc.desc()), User.username).all() - else: # implied sort == "author" - marseys = marseys.order_by(User.username, Marsey.count.desc()).all() - - original = os.listdir("/asset_submissions/marseys/original") - for marsey, user in marseys: - for x in IMAGE_FORMATS: + marseys = get_marseys(g.db) + authors = get_accounts_dict([m.author_id for m in marseys], include_shadowbanned=False) + original = os.listdir("/asset_submissions/marseys/original") + for marsey in marseys: + marsey.user = authors.get(marsey.author_id) + for x in IMAGE_FORMATS: if f'{marsey.name}.{x}' in original: marsey.og = f'{marsey.name}.{x}' break - else: - marseys = g.db.query(Marsey).filter(Marsey.submitter_id==None).order_by(Marsey.count.desc()) - return render_template("marseys.html", v=v, marseys=marseys) -@app.get("/marsey_list.json") -@cache.memoize(timeout=600) +@app.get("/emojis") def marsey_list(): - emojis = [] + return jsonify(get_emojis(g.db)) - # From database - if EMOJI_MARSEYS: - emojis = [{ - "name": emoji.name, - "author": author if SITE == 'rdrama.net' or author == "anton-d" else None, - # yikes, I don't really like this DB schema. Next time be better - "tags": emoji.tags.split(" ") + [emoji.name[len("marsey"):] \ - if emoji.name.startswith("marsey") else emoji.name], - "count": emoji.count, - "class": "Marsey" - } for emoji, author in g.db.query(Marsey, User.username).join(User, Marsey.author_id == User.id).filter(Marsey.submitter_id==None) \ - .order_by(Marsey.count.desc())] +@cache.cached(timeout=86400, key_prefix=MARSEYS_CACHE_KEY) +def get_marseys(db:scoped_session): + if not FEATURES['MARSEYS']: return [] + marseys = [] + for marsey, author in db.query(Marsey, User).join(User, Marsey.author_id == User.id).filter(Marsey.submitter_id == None).order_by(Marsey.count.desc()): + marsey.author = author.username if FEATURES['ASSET_SUBMISSIONS'] or author == "anton-d" else None + marsey.tags = marsey.tags.split(" ") + [emoji.name[len("marsey"):]] + setattr(marsey, "class", "Marsey") + marseys.append(marsey) + return marseys - # Static shit +@cache.cached(timeout=600, key_prefix=EMOJIS_CACHE_KEY) +def get_emojis(db:scoped_session): + emojis = get_marseys(db) for src in EMOJI_SRCS: with open(src, "r", encoding="utf-8") as f: emojis = emojis + json.load(f) - - return jsonify(emojis) + return emojis @app.get('/sidebar') @auth_desired diff --git a/files/templates/marseys.html b/files/templates/marseys.html index 9d76c2852..abd0c9a0e 100644 --- a/files/templates/marseys.html +++ b/files/templates/marseys.html @@ -5,15 +5,15 @@
- - + + - + {% if SITE == 'rdrama.net' %} - - - + {% endif %} + + @@ -43,8 +43,7 @@ {% endfor %} {% endif %} -
#Name#Name MarseyUsageUsageAuthorAdded onOriginal FileAuthorAdded onOriginal File
- + {% endblock %}