def handle_leave_channel(channel, subscribe_socket, push_socket, client_id): # unsubscribe to events happening on this channel channel_id = remove_user_from_channel(g.user, channel) subscribe_socket.setsockopt(zmq.UNSUBSCRIBE, channel_id) leave_channel_event = { "action": "leave_channel", "data": { "user": { "email": g.user["email"], "gravatar": g.user["gravatar"], "name": g.user["name"], "username": g.user["email"].split("@")[0], "status": "active", }, "channel": channel, }, } # alert channel subscribers to user leaving packed_leave_channel = json.dumps(leave_channel_event) push_socket.send(" ".join([channel_id, packed_leave_channel])) self_leave_channel_event = { "action": "self_leave_channel", "data": { "client_id": client_id, "channel": channel, "channel_id": channel_id, }, } packed_self_leave_channel = json.dumps(self_leave_channel_event) push_socket.send(" ".join([str(g.user["email"]), packed_self_leave_channel]))
def handle_publish_message(data, push_socket): message = data["message"] channel = data["channel"] author = g.user["name"] email = g.user["email"] gravatar = g.user["gravatar"] # we use isoformat in json because it cant handle datetime objects time_now = datetime.datetime.utcnow() mongo_event_object = { "author": author, "message": message, "email": email, "channel": channel, "gravatar": gravatar, "datetime": time_now } # db insertion adds an _id field db.events.insert(mongo_event_object) event_object = { "action": "publish_message", "data": message_dict_from_event_object(mongo_event_object), } packed = json.dumps(event_object) # -> Everyone # prepend an identifier showing which channel the event happened on for PUB/SUB push_socket.send(" ".join([zmq_channel_key(channel), packed]))
def handle_leave_channel(channel, subscribe_socket, push_socket, client_id): # unsubscribe to events happening on this channel channel_id = remove_user_from_channel(g.user, channel) subscribe_socket.setsockopt(zmq.UNSUBSCRIBE, channel_id) leave_channel_event = { "action": "leave_channel", "data": { "user": { "email": g.user["email"], "gravatar": g.user["gravatar"], "name": g.user["name"], "username": g.user["email"].split("@")[0], "status": "active", }, "channel": channel, }, } # alert channel subscribers to user leaving packed_leave_channel = json.dumps(leave_channel_event) push_socket.send(" ".join([channel_id, packed_leave_channel])) self_leave_channel_event = { "action": "self_leave_channel", "data": { "client_id": client_id, "channel": channel, "channel_id": channel_id, }, } packed_self_leave_channel = json.dumps(self_leave_channel_event) push_socket.send(" ".join( [str(g.user["email"]), packed_self_leave_channel]))
def handle_reorder_channels(channels, push_socket, client_id): reorder_user_channels(g.user, channels) self_reorder_channels_event = { "action": "self_reorder_channels", "data": { "client_id": client_id, "channels": channels, }, } self_reorder_channels = json.dumps(self_reorder_channels_event) push_socket.send(" ".join([str(g.user["email"]), self_reorder_channels]))
def send_join_channel(channel, user, push_socket): channel_id = zmq_channel_key(channel) join_channel_event = { "action": "join_channel", "data": { "channel": channel, "user": { "email": user["email"], "gravatar": user["gravatar"], "name": user["name"], "username": user["email"].split("@")[0], "status": "active", }, }, } # alert channel subscribers to new user packed_join_channel = json.dumps(join_channel_event) push_socket.send(" ".join([channel_id, packed_join_channel]))
def handle_join_channel(channel, subscribe_socket, push_socket, client_id): add_user_to_channel(g.user, channel) set_user_channel_status(g.user, channel, "active") channel_id = zmq_channel_key(channel) send_join_channel(channel, g.user, push_socket) # subscribe to events happening on this channel subscribe_socket.setsockopt(zmq.SUBSCRIBE, channel_id) join_channel_event = { "action": "join_channel", "data": { "channel": channel, "user": { "email": g.user["email"], "gravatar": g.user["gravatar"], "name": g.user["name"], "username": g.user["email"].split("@")[0], "status": "active", }, }, } # alert channel subscribers to new user packed_join_channel = json.dumps(join_channel_event) push_socket.send(" ".join([channel_id, packed_join_channel])) # alert the user's other open clients of the change self_join_channel_event = { "action": "self_join_channel", "data": { "client_id": client_id, "channel": channel, "channel_id": channel_id, }, } packed_self_join_channel = json.dumps(self_join_channel_event) push_socket.send(" ".join([str(g.user["email"]), packed_self_join_channel]))
def handle_publish_message(data, push_socket): message = data["message"] channel = data["channel"] author = g.user["name"] email = g.user["email"] gravatar = g.user["gravatar"] # we use isoformat in json because it cant handle datetime objects time_now = datetime.datetime.utcnow() mongo_event_object = { "author": author, "message": message, "email": email, "channel": channel, "gravatar": gravatar, "datetime": time_now } # db insertion adds an _id field db.events.insert(mongo_event_object) event_object = { "action":"publish_message", "data": message_dict_from_event_object(mongo_event_object), } packed = json.dumps(event_object) # -> Everyone # prepend an identifier showing which channel the event happened on for PUB/SUB push_socket.send(" ".join([zmq_channel_key(channel), packed]))
def send_user_status_update(user, channel, push_socket, status): packed = json_user_status_event_object(user, channel, status) push_socket.send(" ".join([zmq_channel_key(channel), packed]))