def banUser(qwirkGroup, user, parameters): if len(parameters) > 0: if user.qwirkuser in qwirkGroup.admins.all(): try: qwirkUserToBan = QwirkUser.objects.get( user__username=parameters[0]) qwirkUserToBan.qwirkGroups.remove(qwirkGroup) qwirkGroup.blockedUsers.add(qwirkUserToBan) sendSomething( qwirkGroup, user, "User " + str(parameters[0]) + " has been kicked") utils.sendMessageToAllUserGroup( qwirkGroup, user, parameters[0] + " has been banned by " + user.username, "informations") textUserLeave = json.dumps({ "action": "userLeave", "username": user.username }) utils.sendToQwirkGroup(qwirkGroup, user, textUserLeave) except QwirkUser.DoesNotExist: sendSomething(qwirkGroup, user, "User not in group") else: sendSomething(qwirkGroup, user, "You are not an admin you can't do that !")
def quitGroup(request): if request.user is not None: groupName = request.data['groupName'] qwirkGroup = QwirkGroup.objects.get(name=groupName) if request.user.qwirkuser in qwirkGroup.admins.all(): qwirkGroup.admins.remove(request.user.qwirkuser) if len(qwirkGroup.admins.all()) <= 0: # qwirkUserDefaultAdmin = QwirkUser.objects.filter(qwirkGroup=qwirkGroup).latest() try: qwirkUserDefaultAdmin = qwirkGroup.qwirkuser_set.earliest( 'id') qwirkGroup.admins.add(qwirkUserDefaultAdmin) utils.sendMessageToAllUserGroup( qwirkGroup, request.user, "new administrator: " + qwirkUserDefaultAdmin.user.username, "informations") except ObjectDoesNotExist: qwirkGroup.delete() groupOrChannel = "channel" if qwirkGroup.isPrivate: groupOrChannel = "group" utils.sendMessageToAllUserGroup( qwirkGroup, request.user, request.user.username + " leave this " + groupOrChannel, "informations") textUserLeave = json.dumps({ "action": "userLeave", "username": request.user.username }) utils.sendToQwirkGroup(qwirkGroup, request.user, textUserLeave) request.user.qwirkuser.qwirkGroups.remove(qwirkGroup) return HttpResponse(status=204) else: return HttpResponse(status=401)
def banUser(request): if request.user is not None: groupName = request.data['groupName'] userToBanUsername = request.data['username'] qwirkGroup = QwirkGroup.objects.get(name=groupName) if request.user.qwirkuser in qwirkGroup.admins.all(): userToBan = QwirkUser.objects.get(user__username=userToBanUsername) qwirkGroup.blockedUsers.add(userToBan) userToBan.qwirkGroups.remove(qwirkGroup) text = json.dumps({ "action": "removeGroup", "groupName": groupName }) Group("user" + userToBanUsername).send({ "text": text, }) utils.sendMessageToAllUserGroup( qwirkGroup, request.user, userToBanUsername + " has been banned by " + request.user.username, "informations") return HttpResponse(status=200) else: jsonResponse = JSONRenderer().render({ 'status': 'error', 'name': "NoAdminRight", 'message': 'You need to be admin for banuser' }) return HttpResponse(jsonResponse, status=403) else: return HttpResponse(status=401)
def addUserToGroup(request): if request.user is not None: print(request.data) groupName = request.data['groupName'] userName = request.data['username'] isAdmin = request.data['isAdmin'] qwirkGroup = QwirkGroup.objects.get(name=groupName) userToAdd = User.objects.get(username=userName) if qwirkGroup in request.user.qwirkuser.qwirkGroups.all(): if userToAdd.qwirkuser not in qwirkGroup.blockedUsers.all(): if userToAdd.qwirkuser not in qwirkGroup.qwirkuser_set.all(): userToAdd.qwirkuser.qwirkGroups.add(qwirkGroup) if isAdmin: qwirkGroup.admins.add(userToAdd) groupOrChannel = "channel" if qwirkGroup.isPrivate: groupOrChannel = "group" qwirkGroupSerializer = QwirkGroupSerializer(qwirkGroup) text = json.dumps({ "action": "newGroup", "qwirkGroup": qwirkGroupSerializer.data, }) Group("user" + userName).send({ "text": text, }) textMessage = request.user.username + " has invited " + userToAdd.username + " to join the " + groupOrChannel utils.sendMessageToAllUserGroup(qwirkGroup, request.user, textMessage, "informations") qwirkUserSerializer = QwirkUserSerializer( userToAdd.qwirkuser) textNewUser = json.dumps({ "action": "newUser", "qwirkUser": qwirkUserSerializer.data }) utils.sendToQwirkGroup(qwirkGroup, request.user, textNewUser) qwirkUserSerializer = QwirkUserSerializer( userToAdd.qwirkuser) jsonResponse = JSONRenderer().render({ 'status': 'success', 'qwirkUser': qwirkUserSerializer.data }) return HttpResponse(jsonResponse, status=200) else: jsonResponse = JSONRenderer().render({ 'status': 'error', 'name': 'UserAlreadyInGroup', 'message': 'This user is already in this group' }) return HttpResponse(jsonResponse, status=400) else: print("user blocked") jsonResponse = JSONRenderer().render({ 'status': 'error', 'name': 'UserBlocked', 'message': 'This user is bocked for this group' }) return HttpResponse(jsonResponse, status=403) else: print("user not authorized to add someone") jsonResponse = JSONRenderer().render({ 'status': 'error', "name": "NoAdminRight", 'message': 'You need to be admin for add an user to the group' }) return HttpResponse(jsonResponse, status=403) else: return HttpResponse(status=401)
def sendSomething(qwirkGroup, user, *args): if len(args) > 0: utils.sendMessageToAllUserGroup(qwirkGroup, user, args[0], "botResponse")
def receive(self, content, **kwargs): print("receive Chat json :") print(content) if "groupname" in kwargs: goodTokenAndUserInGroup, user = checkToken(kwargs, False) if goodTokenAndUserInGroup: qwirkGroup = QwirkGroup.objects.get( name=kwargs["groupname"] ) # TODO check with exist or with try catch but not sur because check in connect need to be tested if content["action"] == "message": utils.sendMessageToAllUserGroup(qwirkGroup, user, content["content"]["text"], "message") self.chatbot(qwirkGroup, user, text=content["content"]["text"]) elif content["action"] == "call": Group(kwargs["groupname"]).send({ "text": json.dumps(content), }) elif content["action"] == "get-message": try: Notification.objects.filter( message__qwirkGroup__name=kwargs["groupname"], qwirkUser=user.qwirkuser).delete() except Notification.DoesNotExist: pass messages = Message.objects.filter( qwirkGroup__name=kwargs["groupname"]).order_by( "-dateTime" )[int(content["content"]["startMessage"] ):int(content["content"]["endMessage"])] messageToSend = list() for message in messages: messageToSend.append(MessageSerializer(message).data) # print(messageToSend) text = { "action": "saved-messages", "content": json.dumps(messageToSend) } self.send(text) elif content["action"] == "get-group-informations": groupInfo = dict() groupInfo["isPrivate"] = qwirkGroup.isPrivate groupInfo["isContactGroup"] = qwirkGroup.isContactGroup if user.qwirkuser in qwirkGroup.admins.all(): groupInfo["isAdmin"] = True else: groupInfo["isAdmin"] = False groupInfo["qwirkUsers"] = list() if groupInfo["isContactGroup"]: contacts = qwirkGroup.contact_set.all() for contact in contacts: if contact.qwirkUser.user.username != user.username: groupInfo[ "titleGroupName"] = contact.qwirkUser.user.username groupInfo["qwirkUsers"].append( QwirkUserSerializerSimple( contact.qwirkUser).data) for qwirkUser in groupInfo["qwirkUsers"]: qwirkUser["isAdmin"] = True groupInfo["statusContact"] = contact.status else: qwirkUsers = qwirkGroup.qwirkuser_set.all() for qwirkUser in qwirkUsers: groupInfo["qwirkUsers"].append( QwirkUserSerializerSimple(qwirkUser).data) for qwirkUser in groupInfo["qwirkUsers"]: qwirkUser[ "isAdmin"] = qwirkUser in qwirkGroup.admins.all( ) groupInfo["titleGroupName"] = qwirkGroup.name text = { "action": "group-informations", "content": json.dumps(groupInfo) } self.send(text) elif content["action"] == "accept-contact-request": if qwirkGroup.isContactGroup: contacts = qwirkGroup.contact_set.all() for contact in contacts: contact.status = "Friend" contact.save() requestMessage = qwirkGroup.message_set.get( type='requestMessage') requestMessage.type = "acceptMessage" requestMessage.save() utils.sendFriendshipResponse(qwirkGroup, user, "acceptMessage", requestMessage) elif content["action"] == "decline-contact-request": if qwirkGroup.isContactGroup: contacts = qwirkGroup.contact_set.all() for contact in contacts: contact.status = "Refuse" contact.save() requestMessage = qwirkGroup.message_set.get( type='requestMessage') requestMessage.type = "refuseMessage" requestMessage.save() utils.sendFriendshipResponse(qwirkGroup, user, "refuseMessage", requestMessage) elif content["action"] == "remove-contact": qwirkGroup.delete() elif content["action"] == "block-contact": if qwirkGroup.isContactGroup: for contact in qwirkGroup.contact_set.all(): contact.status = "Block" contact.save() requestMessage = qwirkGroup.message_set.get( type='requestMessage') requestMessage.type = "blockMessage" requestMessage.save() utils.sendFriendshipResponse(qwirkGroup, user, "blockMessage", requestMessage) else: username = content["content"]["username"] userToBlock = QwirkUser.objects.get( user__username=username) qwirkGroup.blockedUsers.add(userToBlock) textMessage = user.username + " has blocked " + userToBlock.user.username utils.sendMessageToAllUserGroup( qwirkGroup, user, textMessage, "informations") else: print("no groupname in url")