def getChats(): """ returns the messages that belongs to a channel or dm if it is valid """ channel = request.form.get("channel") ispublic = request.form.get("ispublic") if ispublic == "true": # get the channel information if the it exists otherwise fail channel = PublicChannel.objects(name=channel).first() if channel: # set the last channel to the channel just accessed current_user.lastchannel = channel.name current_user.save() messages = channel.getChats() else: return jsonify({"success": False}) else: # fetch a Pair's messages if the pair exists else fail otheruser = current_user.getotherperson(channel) otheruser = User.objects(username=otheruser).first() if otheruser: user = User.objects(username=current_user.username).get() pair = Pair.getAPair(user, otheruser) if pair: messages = pair.getChats() current_user.lastchannel = pair.pairname current_user.save() else: return jsonify({"success": False}) # send the messages return jsonify({"success": True, "messages":messages})
def addMessage(data): """ adds a message that was just typed by a user to the channel its on and emits it to be shown only to users currently on that channel """ sender = data["sender"] message = data["message"] channel = data["channel"] typeOfchannel = data["type"] if typeOfchannel == "private": otheruser = current_user.getotherperson(channel) otheruser = User.objects(username=otheruser).first() if otheruser: user = User.objects(username=current_user.username).get() pair = Pair.getAPair(otheruser, user) pair.addChat(sender=sender, message=message) emit("show message", pair.getlastchat(), room=pair.pairname) else: channel = PublicChannel.objects(name=channel).first() if channel: # add chat to channel channel.addChat(sender=sender, message=message) emit("show message", channel.getlastchat(), room=channel.name)