i did the thingy ride my dick now

This commit is contained in:
i-am-called-glitchy 2025-06-06 20:36:23 +00:00
parent c3a1ce74af
commit 2ad548e0b3
3 changed files with 31 additions and 3 deletions

20
app.py
View file

@ -9,10 +9,13 @@ from flask_limiter import Limiter
from flask_limiter.util import get_remote_address from flask_limiter.util import get_remote_address
import time import time
import re import re
from flask_socketio import SocketIO, emit
dotenv.load_dotenv() dotenv.load_dotenv()
app = Flask(__name__) app = Flask(__name__)
socketio = SocketIO(app)
app.secret_key = os.getenv("SECRET") app.secret_key = os.getenv("SECRET")
USERS_FILE = "users.txt" USERS_FILE = "users.txt"
DATA_DIR = "notes" DATA_DIR = "notes"
@ -127,6 +130,7 @@ def public_board(username):
return f.read(), 200 return f.read(), 200
if request.method == "POST": if request.method == "POST":
if user != username: if user != username:
return "Forbidden", 403 return "Forbidden", 403
data = request.get_json() data = request.get_json()
@ -135,24 +139,34 @@ def public_board(username):
os.makedirs(os.path.dirname(path), exist_ok=True) os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w", encoding="utf-8") as f: with open(path, "w", encoding="utf-8") as f:
f.write(data["content"]) f.write(data["content"])
socketio.emit('board_update', {'content': data["content"]})
return jsonify({"status": "saved"}) return jsonify({"status": "saved"})
@socketio.on('connect')
def handle_connect():
print('Client connected')
@socketio.on('disconnect')
def handle_disconnect():
print('Client disconnected')
# When content is updated via POST /boardapi
@app.route("/boardapi", methods=["GET", "POST"]) @app.route("/boardapi", methods=["GET", "POST"])
def shared_board(): def shared_board():
path = os.path.join(DATA_DIR, "__shared_board.txt") path = os.path.join(DATA_DIR, "__shared_board.txt")
if request.method == "GET": if request.method == "GET":
if not os.path.exists(path): if not os.path.exists(path):
return "", 200 return "", 200
with open(path, "r", encoding="utf-8") as f: with open(path, "r", encoding="utf-8") as f:
return f.read(), 200 return f.read(), 200
elif request.method == "POST":
if request.method == "POST":
data = request.get_json() data = request.get_json()
if not data or "content" not in data: if not data or "content" not in data:
return "Bad request", 400 return "Bad request", 400
with open(path, "w", encoding="utf-8") as f: with open(path, "w", encoding="utf-8") as f:
f.write(data["content"]) f.write(data["content"])
# Broadcast new content to all clients
socketio.emit('board_update', {'content': data["content"]})
return jsonify({"status": "saved"}) return jsonify({"status": "saved"})

View file

@ -79,3 +79,15 @@ textarea.addEventListener("input", () => {
} }
}, 500); }, 500);
}); });
const socket = io();
// Listen for updates
socket.on('board_update', data => {
if (true && data.content !== last) {
textarea.value = data.content;
last = data.content;
status.textContent = "Updated from server";
}
});

View file

@ -16,7 +16,9 @@
<script> <script>
const noteName = "{{ note|e }}"; const noteName = "{{ note|e }}";
</script> </script>
<script src="https://cdn.socket.io/4.4.1/socket.io.min.js"></script>
<script src="{{ url_for('static', filename='note.js') }}"></script> <script src="{{ url_for('static', filename='note.js') }}"></script>
</body> </body>
</html> </html>