mirror of
https://github.com/onionshare/onionshare.git
synced 2025-01-10 03:37:28 -03:00
Removed room from chat
- Uses the global room instead of adding and leaving room for users - Removes the joining event and triggers connection status from server as soon as a connection event is received in server side
This commit is contained in:
parent
62396e66b8
commit
f4ade1ba8d
3 changed files with 19 additions and 27 deletions
|
@ -56,7 +56,7 @@ class ModeSettings:
|
|||
"disable_files": False,
|
||||
},
|
||||
"website": {"disable_csp": False, "filenames": []},
|
||||
"chat": {"room": "default"},
|
||||
"chat": {},
|
||||
}
|
||||
self._settings = {}
|
||||
|
||||
|
|
|
@ -11,29 +11,23 @@ $(function () {
|
|||
// Store current username received from app context
|
||||
var current_username = $('#username').val();
|
||||
|
||||
// On browser connect, emit a socket event to be added to
|
||||
// room and assigned random username
|
||||
socket.on('connect', function () {
|
||||
socket.emit('joined', {});
|
||||
});
|
||||
|
||||
// Triggered on any status change by any user, such as some
|
||||
// user joined, or changed username, or left, etc.
|
||||
socket.on('status', function (data) {
|
||||
addMessageToRoom(data, current_username, 'status');
|
||||
addMessageToPanel(data, current_username, 'status');
|
||||
console.log(data, current_username);
|
||||
});
|
||||
|
||||
// Triggered when message is received from a user. Even when sent
|
||||
// by self, it get triggered after the server sends back the emit.
|
||||
socket.on('message', function (data) {
|
||||
addMessageToRoom(data, current_username, 'chat');
|
||||
addMessageToPanel(data, current_username, 'chat');
|
||||
console.log(data, current_username);
|
||||
});
|
||||
|
||||
// Triggered when disconnected either by server stop or timeout
|
||||
socket.on('disconnect', function (data) {
|
||||
addMessageToRoom({ 'msg': 'The chat server is disconnected.' }, current_username, 'status');
|
||||
addMessageToPanel({ 'msg': 'The chat server is disconnected.' }, current_username, 'status');
|
||||
})
|
||||
socket.on('connect_error', function (error) {
|
||||
console.log("error");
|
||||
|
@ -66,7 +60,7 @@ $(function () {
|
|||
});
|
||||
});
|
||||
|
||||
var addMessageToRoom = function (data, current_username, messageType) {
|
||||
var addMessageToPanel = function (data, current_username, messageType) {
|
||||
var scrollDiff = getScrollDiffBefore();
|
||||
if (messageType === 'status') {
|
||||
addStatusMessage(data.msg);
|
||||
|
|
|
@ -19,7 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
"""
|
||||
|
||||
from flask import request, render_template, make_response, jsonify, session
|
||||
from flask_socketio import emit, join_room, leave_room
|
||||
from flask_socketio import emit
|
||||
|
||||
|
||||
class ChatModeWeb:
|
||||
|
@ -33,7 +33,7 @@ class ChatModeWeb:
|
|||
|
||||
self.web = web
|
||||
|
||||
# This tracks users in the room
|
||||
# This tracks users in the server
|
||||
self.connected_users = []
|
||||
|
||||
# This tracks the history id
|
||||
|
@ -61,7 +61,6 @@ class ChatModeWeb:
|
|||
if session.get("name")
|
||||
else 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},
|
||||
|
@ -111,12 +110,11 @@ class ChatModeWeb:
|
|||
)
|
||||
return r
|
||||
|
||||
@self.web.socketio.on("joined", namespace="/chat")
|
||||
def joined(message):
|
||||
@self.web.socketio.on("connect", namespace="/chat")
|
||||
def server_connect():
|
||||
"""Sent by clients when they enter a room.
|
||||
A status message is broadcast to all people in the room."""
|
||||
self.connected_users.append(session.get("name"))
|
||||
join_room(session.get("room"))
|
||||
emit(
|
||||
"status",
|
||||
{
|
||||
|
@ -125,23 +123,23 @@ class ChatModeWeb:
|
|||
"connected_users": self.connected_users,
|
||||
"user": session.get("name"),
|
||||
},
|
||||
room=session.get("room"),
|
||||
broadcast=True,
|
||||
)
|
||||
|
||||
@self.web.socketio.on("text", namespace="/chat")
|
||||
def text(message):
|
||||
"""Sent by a client when the user entered a new message.
|
||||
The message is sent to all people in the room."""
|
||||
The message is sent to all people in the server."""
|
||||
emit(
|
||||
"message",
|
||||
{"username": session.get("name"), "msg": message["msg"]},
|
||||
room=session.get("room"),
|
||||
broadcast=True,
|
||||
)
|
||||
|
||||
@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."""
|
||||
The message is sent to all people in the server."""
|
||||
current_name = session.get("name")
|
||||
if message.get("username", ""):
|
||||
session["name"] = message["username"]
|
||||
|
@ -158,20 +156,20 @@ class ChatModeWeb:
|
|||
"old_name": current_name,
|
||||
"new_name": session.get("name"),
|
||||
},
|
||||
room=session.get("room"),
|
||||
broadcast=True,
|
||||
)
|
||||
|
||||
@self.web.socketio.on("disconnect", namespace="/chat")
|
||||
def disconnect():
|
||||
"""Sent by clients when they disconnect from a room.
|
||||
A status message is broadcast to all people in the room."""
|
||||
self.connected_users.remove(session.get("name"))
|
||||
leave_room(session.get("room"))
|
||||
"""Sent by clients when they disconnect.
|
||||
A status message is broadcast to all people in the server."""
|
||||
if session.get("name") in self.connected_users:
|
||||
self.connected_users.remove(session.get("name"))
|
||||
emit(
|
||||
"status",
|
||||
{
|
||||
"msg": "{} has left the room.".format(session.get("name")),
|
||||
"connected_users": self.connected_users,
|
||||
},
|
||||
room=session.get("room"),
|
||||
broadcast=True,
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue