diff options
author | Saptak S <saptak013@gmail.com> | 2020-03-12 14:54:48 +0530 |
---|---|---|
committer | Saptak S <saptak013@gmail.com> | 2020-03-12 14:54:48 +0530 |
commit | c63a7605ee0cf93b04a7f00a2f047c2eb12e3616 (patch) | |
tree | 0dcc199f21b05b8d6040fd9c272faced78a9a607 /onionshare | |
parent | 7eaefd5299238ab5c9f45f36698ce680fbdc3814 (diff) | |
download | onionshare-c63a7605ee0cf93b04a7f00a2f047c2eb12e3616.tar.gz onionshare-c63a7605ee0cf93b04a7f00a2f047c2eb12e3616.zip |
Adds list of active users in the chat and allows username change
- allows users to update their username and save the new username
- runs a background thread for every user session which emits a
broadcast with the username so every user can build their list of
active users in the frontend via the socket information
- on updating username, stop the old thread and start a new thread
with the new username being emitted. The username is updated in
everyone's list along with a status message for the same.
Diffstat (limited to 'onionshare')
-rw-r--r-- | onionshare/web/chat_mode.py | 65 |
1 files changed, 55 insertions, 10 deletions
diff --git a/onionshare/web/chat_mode.py b/onionshare/web/chat_mode.py index 45f6a8cd..05014a22 100644 --- a/onionshare/web/chat_mode.py +++ b/onionshare/web/chat_mode.py @@ -1,13 +1,7 @@ -import os -import tempfile -import json -from datetime import datetime from flask import Request, request, render_template, make_response, flash, redirect, session from werkzeug.utils import secure_filename from flask_socketio import emit, join_room, leave_room -from .. import strings - class ChatModeWeb: """ @@ -37,6 +31,8 @@ class ChatModeWeb: def index(): history_id = self.cur_history_id self.cur_history_id += 1 + session["name"] = self.common.build_username() + session["room"] = self.web.settings.default_settings["chat"]["room"] self.web.add_request( request.path, {"id": history_id, "status_code": 200}, @@ -45,7 +41,9 @@ class ChatModeWeb: self.web.add_request(self.web.REQUEST_LOAD, request.path) r = make_response( render_template( - "chat.html", static_url_path=self.web.static_url_path + "chat.html", + static_url_path=self.web.static_url_path, + username=session.get("name") ) ) return self.web.add_security_headers(r) @@ -54,12 +52,15 @@ class ChatModeWeb: def joined(message): """Sent by clients when they enter a room. A status message is broadcast to all people in the room.""" - session["name"] = self.common.build_username() - session["room"] = self.web.settings.default_settings["chat"]["room"] + session["worker"] = UserListWorker(self.web.socketio) + session["thread"] = self.web.socketio.start_background_task( + session["worker"].background_thread, session["name"] + ) join_room(session.get("room")) emit( "status", - {"msg": session.get("name") + " has entered the room."}, + {"msg": session.get("name") + " has entered the room.", + "user": session.get("name")}, room=session.get("room") ) @@ -72,3 +73,47 @@ class ChatModeWeb: {"msg": session.get("name") + ": " + message["msg"]}, room=session.get("room") ) + + @self.web.socketio.on("update_username", namespace="/chat") + def update_username(message): + """Sent by a client when the user updates their username. + The message is sent to all people in the room.""" + current_name = session.get("name") + session["name"] = message["username"] + session["worker"].stop_thread() + session["worker"] = UserListWorker(self.web.socketio) + session['thread'] = self.web.socketio.start_background_task( + session["worker"].background_thread, session['name'] + ) + emit( + "status", + {"msg": current_name + " has updated their username to: " + session.get("name"), + "old_name": current_name, + "new_name": session.get("name") + }, + room=session.get("room") + ) + + + +class UserListWorker(object): + + def __init__(self, socketio): + """ + assign socketio object to emit + """ + self.socketio = socketio + self.switch = True + + def background_thread(self, name): + count = 0 + while self.switch: + self.socketio.sleep(5) + count += 1 + self.socketio.emit('update_list', + {'name': name, 'count': count}, + namespace="/chat", + broadcast=True) + + def stop_thread(self): + self.switch = False |