diff --git a/files/classes/chats.py b/files/classes/chats.py index d7790d79c..fb44bc507 100644 --- a/files/classes/chats.py +++ b/files/classes/chats.py @@ -2,7 +2,7 @@ import time from flask import g from sqlalchemy import Column, ForeignKey -from sqlalchemy.orm import relationship +from sqlalchemy.orm import deferred, relationship from sqlalchemy.sql.sqltypes import * from files.classes import Base @@ -14,6 +14,7 @@ class Chat(Base): id = Column(Integer, primary_key=True) name = Column(String) created_utc = Column(Integer) + css = deferred(Column(String)) @property @lazy diff --git a/files/routes/chats.py b/files/routes/chats.py index 3b160df67..dae1a923b 100644 --- a/files/routes/chats.py +++ b/files/routes/chats.py @@ -112,9 +112,9 @@ def chat(v, chat_id): orgy = get_running_orgy(v, chat_id) if orgy: orgies = g.db.query(Orgy).filter_by(chat_id=chat_id).order_by(Orgy.start_utc).all() - return render_template("orgy.html", v=v, messages=displayed_messages, chat=chat, sorted_memberships=sorted_memberships, muting_chat=muting_chat, orgy=orgy, orgies=orgies, membership=membership) + return render_template("orgy.html", v=v, messages=displayed_messages, chat=chat, sorted_memberships=sorted_memberships, muting_chat=muting_chat, orgy=orgy, orgies=orgies, membership=membership, chat_css=chat.css) - return render_template("chat.html", v=v, messages=displayed_messages, chat=chat, sorted_memberships=sorted_memberships, muting_chat=muting_chat, membership=membership) + return render_template("chat.html", v=v, messages=displayed_messages, chat=chat, sorted_memberships=sorted_memberships, muting_chat=muting_chat, membership=membership, chat_css=chat.css) @app.post("/chat//name") @@ -188,6 +188,63 @@ def mute_chat(v, chat_id): return {"message": msg} +@app.get("/chat//custom_css") +@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400) +@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400, key_func=get_ID) +@auth_required +def chat_custom_css_get(v, chat_id): + chat = g.db.get(Chat, chat_id) + if not chat: + abort(404, "Chat not found!") + + if v.id != chat.owner_id: + abort(403, "Only the chat owner can change its custom css!") + + return render_template("chat_css.html", v=v, chat=chat) + +@app.post("/chat//custom_css") +@limiter.limit('1/second', scope=rpath) +@limiter.limit('1/second', scope=rpath, key_func=get_ID) +@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400) +@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400, key_func=get_ID) +@auth_required +def chat_custom_css_post(v, chat_id): + chat = g.db.get(Chat, chat_id) + if not chat: + abort(404, "Chat not found!") + + if v.id != chat.owner_id: + abort(403, "Only the chat owner can change its custom css!") + + if v.shadowbanned: abort(400) + + css = request.values.get('css', '').strip() + + if len(css) > CSS_LENGTH_LIMIT: + abort(400, f"Chat CSS is too long (max {CSS_LENGTH_LIMIT} characters)") + + valid, error = validate_css(css) + if not valid: + abort(400, error) + + chat.css = css + g.db.add(chat) + + return {"message": "Chat Custom CSS successfully updated!"} + + +@app.get("/chat//css") +@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400) +@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400, key_func=get_ID) +@auth_required +def chat_css(v, chat_id): + chat = g.db.query(Chat.css).filter_by(id=chat_id).one_or_none() + if not chat: + abort(404, "Chat not found!") + resp = make_response(chat.css or "") + resp.headers.add("Content-Type", "text/css") + return resp + @app.get("/chat//orgies") @limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400) @limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400, key_func=get_ID) diff --git a/files/templates/chat.html b/files/templates/chat.html index 2ca4467c1..733e3730c 100644 --- a/files/templates/chat.html +++ b/files/templates/chat.html @@ -17,8 +17,7 @@ {{chat.name}} {% if v.id == chat.owner_id %} -
@@ -27,7 +26,9 @@
- + + + {% else %}