diff --git a/docker-compose.yml b/docker-compose.yml index ae466bd6b..4ae6de3c9 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -16,7 +16,7 @@ services: - SITE_NAME=Drama - CLOUDFLARE_ZONE=vcxvdfgfc6r554etrgd - CLOUDFLARE_KEY=vcxvdfgfc6r554etrgd - - TENOR_KEY=vcxvdfgfc6r554etrgd + - GIPHY_KEY=vcxvdfgfc6r554etrgd - MAILGUN_KEY=vcxvdfgfc6r554etrgd - MAILGUN_DOMAIN=rdrama.net - FORCE_HTTPS=0 diff --git a/files/__main__.py b/files/__main__.py index 1ddfbd0e8..1eb2518c5 100644 --- a/files/__main__.py +++ b/files/__main__.py @@ -37,7 +37,7 @@ app.config['DATABASE_URL'] = environ.get("DATABASE_CONNECTION_POOL_URL",environ. app.config['SECRET_KEY'] = environ.get('MASTER_KEY') app.config["SERVER_NAME"] = environ.get("DOMAIN").strip() -app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 60*10 +app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 31536000 app.config["SESSION_COOKIE_NAME"] = "session_" + environ.get("SITE_NAME").strip().lower() app.config["VERSION"] = "1.0.0" @@ -87,8 +87,6 @@ app.config["CACHE_OPTIONS"]={'connection_pool':redispool} if app.config["CACHE_T app.config["READ_ONLY"]=bool(int(environ.get("READ_ONLY", "0"))) app.config["BOT_DISABLE"]=bool(int(environ.get("BOT_DISABLE", False))) -app.config["TENOR_KEY"]=environ.get("TENOR_KEY",'').strip() - Markdown(app) cache = Cache(app) @@ -261,7 +259,7 @@ def after_request(response): response.headers.add("Referrer-Policy", "same-origin") response.headers.add("Feature-Policy", "geolocation 'none'; midi 'none'; notifications 'none'; push 'none'; sync-xhr 'none'; microphone 'none'; camera 'none'; magnetometer 'none'; gyroscope 'none'; vibrate 'none'; fullscreen 'none'; payment 'none';") - if not request.path.startswith("/embed/"): response.headers.add("X-Frame-Options", "deny") + response.headers.add("X-Frame-Options", "deny") return response diff --git a/files/assets/js/all_js.js b/files/assets/js/all_js.js index 4ff17c425..34a90ef42 100644 --- a/files/assets/js/all_js.js +++ b/files/assets/js/all_js.js @@ -91,134 +91,158 @@ document.body.addEventListener('keydown', function(event) { else { document.getElementById('gifSearch').value = null; } - - // load more gifs div - var loadGIFs = document.getElementById('gifs-load-more'); - - // error message div - var noGIFs = document.getElementById('no-gifs-found'); - - // categories div - var cats = document.getElementById('GIFcats'); - - // container div - var container = document.getElementById('GIFs'); - - // modal body div - var modalBody = document.getElementById('gif-modal-body') - - // UI buttons - var backBtn = document.getElementById('gifs-back-btn'); - var cancelBtn = document.getElementById('gifs-cancel-btn'); - - container.innerHTML = ''; - - if (searchTerm == undefined) { - container.innerHTML = '
Agree
Laugh
Confused
Sad
Happy
Awesome
Yes
No
Love
Please
Scared
Angry
Awkward
Cringe
OMG
Why
Gross
Meh
' - backBtn.innerHTML = null; - cancelBtn.innerHTML = null; - noGIFs.innerHTML = null; - loadGIFs.innerHTML = null; - } else { - backBtn.innerHTML = ''; - - cancelBtn.innerHTML = ''; - - let gifs = []; - let apiKey = tenor_api_key(); - let lmt = 25; - let url = "https://g.tenor.com/v1/search/?q=" + searchTerm + "&key=" + apiKey + "&limit=" + lmt; - fetch(url) - .then(response => { - return response.json(); - }) - .then(json => { - let results = json.results.map(function(obj) { - return { - id: obj.id, - preview: obj.media[0].tinygif.url, - url: obj.media[0].gif.url, - source: obj.url, - bgColor: obj.bg_color - } - }); - - gifs = results - - // loop for fetching mutliple GIFs and creating the card divs - if (gifs.length) { - for (var i = 0; i < gifs.length; i++) { - container.innerHTML += ('
'); - noGIFs.innerHTML = null; - loadGIFs.innerHTML = '

Thou've reached the end of the list!

'; - } - } else { - noGIFs.innerHTML = '

Aw shucks. No GIFs found...

'; - container.innerHTML = null; - loadGIFs.innerHTML = null; - } - }) - .catch(err => alert(err)); - }; - } - // Insert GIF markdown into comment box function - - function insertGIF(url,form) { - - var gif = "![](" + url +")"; - - var commentBox = document.getElementById(form); - - var old = commentBox.value; - - commentBox.value = old + gif; - - } - - // When GIF keyboard is hidden, hide all GIFs - - $('#gifModal').on('hidden.bs.modal', function (e) { - - document.getElementById('gifSearch').value = null; - + // load more gifs div - + var loadGIFs = document.getElementById('gifs-load-more'); - - // no GIFs div - + + // error message div + var noGIFs = document.getElementById('no-gifs-found'); - + + // categories div + + var cats = document.getElementById('GIFcats'); + // container div - + var container = document.getElementById('GIFs'); - + + // modal body div + + var modalBody = document.getElementById('gif-modal-body') + // UI buttons - + var backBtn = document.getElementById('gifs-back-btn'); - + var cancelBtn = document.getElementById('gifs-cancel-btn'); - + + container.innerHTML = ''; + + if (searchTerm == undefined) { + container.innerHTML = '
Agree
Laugh
Confused
Sad
Happy
Awesome
Yes
No
Love
Please
Scared
Angry
Awkward
Cringe
OMG
Why
Gross
Meh
' + + backBtn.innerHTML = null; + + cancelBtn.innerHTML = null; + + noGIFs.innerHTML = null; + + loadGIFs.innerHTML = null; + } + else { + backBtn.innerHTML = ''; + + cancelBtn.innerHTML = ''; + + $.ajax({ + url: "/giphy?searchTerm=" + searchTerm + "&limit=48", + type: "GET", + success: function(response) { + var max = response.data.length - 1 //length of response, minus 1 (cuz array starts at index 0) + var randomNumber = Math.round(Math.random() * 6) //random number between 0 and max -1 + // GIF array + var gifURL = []; + + // loop for fetching mutliple GIFs and creating the card divs + if (max < 48 && max > 0) { + for (var i = 0; i <= max; i++) { + gifURL[i] = "https://media.giphy.com/media/" + response.data[i].id + "/200w_d.gif"; + if (response.data[i].username==''){ + container.innerHTML += ('
'); + } + else { + container.innerHTML += ('
'); + } + noGIFs.innerHTML = null; + loadGIFs.innerHTML = '

Thou've reached the end of the list!

'; + } + } + else if (max <= 0) { + noGIFs.innerHTML = '

Aw shucks. No GIFs found...

'; + container.innerHTML = null; + loadGIFs.innerHTML = null; + } + else { + for (var i = 0; i <= 48; i++) { + gifURL[i] = "https://media.giphy.com/media/" + response.data[i].id + "/200w_d.gif"; + if (response.data[i].username==''){ + container.innerHTML += ('
'); + } + else { + container.innerHTML += ('
'); + } + noGIFs.innerHTML = null; + loadGIFs.innerHTML = '

Thou've reached the end of the list!

'; + } + } + }, + error: function(e) { + alert(e); + } + }); + }; + } + + // Insert GIF markdown into comment box function + + function insertGIF(url,form) { + + var gif = "![](" + url +")"; + + var commentBox = document.getElementById(form); + + var old = commentBox.value; + + commentBox.value = old + gif; + + } + + // When GIF keyboard is hidden, hide all GIFs + + $('#gifModal').on('hidden.bs.modal', function (e) { + + document.getElementById('gifSearch').value = null; + + // load more gifs div + + var loadGIFs = document.getElementById('gifs-load-more'); + + // no GIFs div + + var noGIFs = document.getElementById('no-gifs-found'); + + // container div + + var container = document.getElementById('GIFs'); + + // UI buttons + + var backBtn = document.getElementById('gifs-back-btn'); + + var cancelBtn = document.getElementById('gifs-cancel-btn'); + // Remove inner HTML from container var - - container.innerHTML = '
Agree
Laugh
Confused
Sad
Happy
Awesome
Yes
No
Love
Please
Scared
Angry
Awkward
Cringe
OMG
Why
Gross
Meh
' - + + container.innerHTML = '
Agree
Laugh
Confused
Sad
Happy
Awesome
Yes
No
Love
Please
Scared
Angry
Awkward
Cringe
OMG
Why
Gross
Meh
' + // Hide UI buttons - + backBtn.innerHTML = null; - + cancelBtn.innerHTML = null; - + // Remove inner HTML from no gifs div - + noGIFs.innerHTML = null; - + // Hide no more gifs div - + loadGIFs.innerHTML = null; - - }); - + + }); + //iOS webapp stuff diff --git a/files/helpers/jinja2.py b/files/helpers/jinja2.py index 8c6e63e09..c9c21ffa4 100644 --- a/files/helpers/jinja2.py +++ b/files/helpers/jinja2.py @@ -13,11 +13,11 @@ def app_config(x): return app.config.get(x) @app.template_filter("post_embed") -def crosspost_embed(id): +def post_embed(id, v): - id = int(id) + try: id = int(id) + except: return None p = get_post(id, graceful=True) - if hasattr(g, 'v') and g.v: return render_template("submission_listing.html", listing=[p], v=g.v) - else: return render_template("submission_listing.html", listing=[p], postembed=True) \ No newline at end of file + return render_template("submission_listing.html", listing=[p], v=v) \ No newline at end of file diff --git a/files/routes/__init__.py b/files/routes/__init__.py index bf40d037f..cbddc7e96 100644 --- a/files/routes/__init__.py +++ b/files/routes/__init__.py @@ -13,4 +13,5 @@ from .static import * from .users import * from .votes import * from .feeds import * -from .awards import * \ No newline at end of file +from .awards import * +from .giphy import * \ No newline at end of file diff --git a/files/routes/comments.py b/files/routes/comments.py index 3c2e74cee..955fe2911 100644 --- a/files/routes/comments.py +++ b/files/routes/comments.py @@ -35,8 +35,8 @@ def post_pid_comment_cid(cid, pid=None, anything=None, v=None): if not v and "logged_out" not in request.path: return redirect(f"/logged_out/comment/{cid}") - if v and "logged_out" in request.full_path: return redirect(f"/comment/{cid}") - + if v and "logged_out" in request.full_path: v = None + try: cid = int(cid) except: try: cid = int(cid, 36) diff --git a/files/routes/front.py b/files/routes/front.py index 50f860687..75d69e477 100644 --- a/files/routes/front.py +++ b/files/routes/front.py @@ -191,7 +191,7 @@ def front_all(v): if not v and request.path == "/": return redirect("/logged_out") - if v and "logged_out" in request.full_path: return redirect("/") + if v and "logged_out" in request.full_path: v = None try: page = int(request.args.get("page") or 1) except: abort(400) diff --git a/files/routes/giphy.py b/files/routes/giphy.py new file mode 100644 index 000000000..28b2bfadf --- /dev/null +++ b/files/routes/giphy.py @@ -0,0 +1,22 @@ +from flask import * +from os import environ +import requests + +from files.__main__ import app + +GIPHY_KEY = environ.get('GIPHY_KEY').rstrip() + + +@app.route("/giphy", methods=["GET"]) +@app.route("/giphy", methods=["GET"]) +def giphy(): + + searchTerm = request.args.get("searchTerm", "") + limit = int(request.args.get("limit", "")) + if searchTerm and limit: + url = f"https://api.giphy.com/v1/gifs/search?q={searchTerm}&api_key={GIPHY_KEY}&limit={limit}" + elif searchTerm and not limit: + url = f"https://api.giphy.com/v1/gifs/search?q={searchTerm}&api_key={GIPHY_KEY}&limit=48" + else: + url = f"https://api.giphy.com/v1/gifs?api_key={GIPHY_KEY}&limit=48" + return jsonify(requests.get(url).json()) \ No newline at end of file diff --git a/files/routes/login.py b/files/routes/login.py index 8d4da04e6..4cc84cda8 100644 --- a/files/routes/login.py +++ b/files/routes/login.py @@ -125,10 +125,7 @@ def login_post(): # check for previous page redir = request.form.get("redirect", "/") - if redir: - return redirect(redir) - else: - return redirect(account.url) + return redirect(redir.replace("/logged_out", "")) @app.get("/me") diff --git a/files/routes/posts.py b/files/routes/posts.py index 1cb9a5ee2..76f309e7d 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -54,7 +54,7 @@ def post_id(pid, anything=None, v=None): if not v and "logged_out" not in request.path: return redirect(f"/logged_out/post/{pid}") - if v and "logged_out" in request.full_path: return redirect(f"/post/{pid}") + if v and "logged_out" in request.full_path: v = None try: pid = int(pid) except Exception as e: pass diff --git a/files/routes/users.py b/files/routes/users.py index 632ca2f0c..bf930aa9e 100644 --- a/files/routes/users.py +++ b/files/routes/users.py @@ -281,7 +281,7 @@ def u_username(username, v=None): if not v and "logged_out" not in request.path: return redirect(f"/logged_out/@{username}") - if v and "logged_out" in request.full_path: return redirect(f"/@{username}") + if v and "logged_out" in request.full_path: v = None # username is unique so at most this returns one result. Otherwise 404 @@ -402,7 +402,7 @@ def u_username_comments(username, v=None): if not v and "logged_out" not in request.path: return redirect(f"/logged_out/@{username}/comments") - if v and "logged_out" in request.full_path: return redirect(f"/@{username}/comments") + if v and "logged_out" in request.full_path: v = None # username is unique so at most this returns one result. Otherwise 404 diff --git a/files/templates/errors/502.html b/files/templates/errors/502.html index 8cd53dd8f..c1cb02cf5 100644 --- a/files/templates/errors/502.html +++ b/files/templates/errors/502.html @@ -37,7 +37,7 @@