示例#1
0
    def mutate_and_get_payload(root, info, **input):
        chatroom = ChatroomModel.objects.get(
            unique_identifier=input.pop('chatroom_id'))
        sent_user = info.context.user
        chatroom_membership = ChatroomMembershipModel.objects.get(
            chatroom=chatroom, user=sent_user)

        input = clean_input(input)

        new_message = MessageModel(message=input.get('message'),
                                   sender=chatroom_membership,
                                   chatroom=chatroom)
        new_message.save()

        MessageSubscriptions.broadcast(group='{}-message-subscription'.format(
            chatroom.unique_identifier),
                                       payload={
                                           "type":
                                           MutationTypes.CREATE.name,
                                           "message_id":
                                           new_message.unique_identifier
                                       })

        for membership in ChatroomMembershipModel.objects.filter(
                chatroom__unique_identifier=chatroom.unique_identifier):
            ChatroomMembershipSubscriptions.broadcast(
                group='{}-chatroom-membership-subscription'.format(
                    membership.user.username),
                payload={
                    "type": MutationTypes.UPDATE.name,
                    "chatroom_membership_id": membership.unique_identifier
                })

        return CreateMessage(message=new_message)
示例#2
0
    def mutate_and_get_payload(root, info, **input):
        chatroom = ChatroomModel.objects.get(
            unique_identifier=input.get('chatroom_id'))

        input = clean_input(input)
        chatroom.name = input.get('name')

        chatroom.save()

        ChatroomSubscriptions.broadcast(
            group='{}-chatroom-subscription'.format(
                chatroom.unique_identifier),
            payload={
                "type": MutationTypes.UPDATE.name,
                "chatroom_id": chatroom.unique_identifier
            })

        for membership in ChatroomMembershipModel.objects.filter(
                chatroom=chatroom):
            ChatroomMembershipSubscriptions.broadcast(
                group='{}-chatroom-membership-subscription'.format(
                    membership.user.username),
                payload={
                    "type": MutationTypes.UPDATE.name,
                    "chatroom_membership_id": membership.unique_identifier
                })

        return UpdateChatroom(chatroom=chatroom)
示例#3
0
    def mutate_and_get_payload(root, info, **input):
        input = clean_input(input)

        # creation of chatroom
        new_chatroom = ChatroomModel(name=input.get('name'))
        new_chatroom.save()

        # adding the creator to the chatroom
        chatroom_membership = ChatroomMembershipModel(chatroom=new_chatroom,
                                                      user=info.context.user)
        chatroom_membership.save()

        # No need to broadcase chatroom subscription since chatroom_membership handles subscription
        # ChatroomSubscriptions.broadcast(
        #     group='{}-chatroom-subscription'.format(
        #         info.context.user.username),
        #     payload={
        #         "type": MutationTypes.CREATE.name,
        #         "chatroom_id": new_chatroom.unique_identifier
        #     }
        # )

        ChatroomMembershipSubscriptions.broadcast(
            group='{}-chatroom-membership-subscription'.format(
                info.context.user.username),
            payload={
                "type": MutationTypes.CREATE.name,
                "chatroom_membership_id": chatroom_membership.unique_identifier
            })

        return CreateChatroom(chatroom=new_chatroom)
示例#4
0
    def mutate_and_get_payload(root, info, **input):        
        called_user = info.context.user
        if called_user is None or called_user.is_anonymous:
            raise GraphQLError('Not logged in.')

        filtered_input = clean_input(input)

        # used "pop" instead of "get" to get rid of the old_name argument
        updated_thread = ThreadModel.objects.get(name=filtered_input.pop('name'))
        if not is_admin(called_user, updated_thread):
            raise GraphQLError('Not an admin.')

        "Checks if user entered a new name and if the entered name is already taken"
        if ThreadModel.objects.filter(name=filtered_input.get('new_name')).first():
            raise GraphQLError('The entered name already taken.')
        
        if filtered_input.get('new_name') and ' ' in filtered_input.get('new_name'):
            raise GraphQLError('The name of the thread should not contain any spaces.')

        # remove 'new_name' attribute and set to 'name' for spread operator
        if filtered_input.get('new_name'):
            filtered_input['name'] = filtered_input.pop('new_name')

        for (key, value) in filtered_input.items():
            setattr(updated_thread, key, value)
        updated_thread.save()

        return UpdateThread(thread=updated_thread)
示例#5
0
    def mutate_and_get_payload(root, info, **input):
        # thread = ThreadModel.objects.get(name=input.get('thread_name'))
        # user   = UserModel.objects.get(username=input.get('username')) 

        "Getting the membership of the calling user"
        try:
            membership = ThreadMemberModel.objects.filter(
                user__username=clean_input(input).get('username'),
                thread__name=clean_input(input).get('thread_name')
            ).get()
        except Exception as e:
            raise GraphQLError(e)
        else:
            membership.is_admin = False
            membership.save()
        return Demote(membership=membership)
示例#6
0
    def mutate_and_get_payload(root, info, **input):
        if info.context.user.is_anonymous:
            raise GraphQLError('Not logged in.')
        updated_post = PostModel.objects.get(
            unique_identifier=input.pop('post_unique_identifier'))

        for (key, value) in clean_input(input).items():
            setattr(updated_post, key, value)
        updated_post.save()

        return UpdatePostOnThread(post=updated_post)
示例#7
0
    def mutate_and_get_payload(root, info, **input):
        "Login check"
        if not info.context.user.is_anonymous:
            raise GraphQLError('Already logged in.')
        # remove password from input to allow spaces in password
        password = input.pop('password')
        input = clean_input(input)
        "Extra username validation"
        if ' ' in input.get('username'):
            raise GraphQLError('Invalid username format.')
        "Email validation"
        try:
            validate_email(input.get('email'))
        except ValidationError:
            raise GraphQLError('Invalid email format.')
        "Checks to see if the user exists in the database"
        if UserModel.objects.filter(username=input.get('username')).first():
            raise GraphQLError('Username already taken.')
        if UserModel.objects.filter(email=input.get('email')).first():
            raise GraphQLError('Email already taken.')
        "Checks name format"
        if not validate_name(input.get('first_name')) or not validate_name(
                input.get('last_name')):
            raise GraphQLError('Invalid first/last name.')

        confirmation_link = input.pop('confirmation_link')

        "Convert username to lowercase"
        input['username'] = input.get('username').lower()

        new_user = UserModel(**input)
        new_user.set_password(password)
        new_user.save()

        # Email Authentication
        # try:
        #     server = smtplib.SMTP('smtp.gmail.com:587')
        #     server.ehlo()
        #     server.starttls()
        #     server.login("EMAIL", "PASSWORD")
        #     message = 'Subject: {}\n\n{}'.format(
        #         'Email Confirmation',
        #         'Please click the link below to confirm your email address:\n' + confirmation_link
        #     )
        #     server.sendmail("EMAIL", input.get('email'), message)
        #     server.quit()
        # except:
        #     print('Email failed to send.')

        return Register(user=new_user)
示例#8
0
 def mutate_and_get_payload(root, info, **input):
     if info.context.user.is_anonymous:
         raise GraphQLError('Not logged in.')
     comment = CommentModel.objects.get(
         unique_identifier=input.get('comment_unique_identifier'))
     try:
         new_comment = CommentModel(
             content_object=comment,
             content=clean_input(input).get('content'),
             user=info.context.user)
     except Exception as e:
         raise GraphQLError(e)
     else:
         new_comment.save()
     return CreateCommentOnComment(comment=new_comment)
示例#9
0
    def mutate_and_get_payload(root, info, **input):
        user = UserModel.objects.get(username=input.pop('username'))
        filtered_input = clean_input(input)

        if user.is_anonymous:
            raise GraphQLError('Please login first to modify profile.')

        if not validate_name(
                filtered_input.get('first_name')) or not validate_name(
                    filtered_input.get('last_name')):
            raise GraphQLError('Invalid first/last name.')

        updated_user = UserModel.objects.get(id=user.id)
        for (key, value) in filtered_input.items():
            setattr(updated_user, key, value)
        updated_user.save()

        return UpdateProfile(user=updated_user)
示例#10
0
    def mutate_and_get_payload(root, info, **input):
        message = MessageModel.objects.get(
            unique_identifier=input.pop('message_id'))

        input = clean_input(input)
        message.message = input.get('message')

        message.save()

        MessageSubscriptions.broadcast(group='{}-message-subscription'.format(
            message.chatroom.unique_identifier),
                                       payload={
                                           "type": MutationTypes.UPDATE.name,
                                           "message_id":
                                           message.unique_identifier
                                       })

        return UpdateMessage(message=message)
示例#11
0
    def mutate_and_get_payload(root, info, **input):
        if info.context.user.is_anonymous:
            raise GraphQLError('Not logged in.')

        # extracting out 'thread_name' for later object creation
        thread = ThreadModel.objects.get(name=input.pop('thread_name'))

        if not is_member(info.context.user, thread):
            raise GraphQLError('Not a member.')

        "Creating the post"
        cleaned_input = clean_input(input)
        cleaned_input = dict(cleaned_input, **{
            'user': info.context.user,
            'thread': thread
        })
        post = PostModel(**cleaned_input)
        post.save()

        return CreatePostOnThread(post=post)
示例#12
0
    def mutate_and_get_payload(root, info, **input):
        password = input.pop('password')
        cleaned_input = clean_input(input)
        cleaned_input['username'] = cleaned_input.get('username').lower()
        username = cleaned_input.get('username')

        # check if username exists
        if UserModel.objects.filter(username=username).exists():
            raise GraphQLError('Username Taken.')

        new_user = UserModel(username=username)
        new_user.set_password(password)
        new_user.save()

        UserSubscriptions.broadcast(group='users-subscription',
                                    payload={
                                        "type":
                                        SpecialMutationTypes.REGISTER.name,
                                        "username": username
                                    })

        return Register(user=new_user)
示例#13
0
    def mutate_and_get_payload(root, info, **input):
        called_user = info.context.user
        if called_user is None or called_user.is_anonymous:
            raise GraphQLError('Not logged in.')

        cleaned_input = clean_input(input)

        if ' ' in cleaned_input.get('name'):
            raise GraphQLError('The name of the thread should not contain any spaces.')

        "Checks if entered thread name already exists"
        if ThreadModel.objects.filter(name=cleaned_input.get('name')).first():
            raise GraphQLError('Thread name already taken.')

        # Creating thread
        new_thread = ThreadModel(**cleaned_input)
        new_thread.save()
        
        # Adding creator to the thread
        new_membership = ThreadMemberModel(user=called_user, thread=new_thread, is_admin=True)
        new_membership.save()

        return CreateThread(thread=new_thread)