Ejemplo n.º 1
0
    def on_delete_project_chat_group(data):
        if data["projectId"] in rooms() and current_user.is_project_owner_of(
                data["projectId"]):

            project_chat_group = ProjectChatGroup.query.filter_by(
                project_id=data["projectId"]).first()

            members = project_chat_group.get_members_with_only_id_entities()

            ProjectChatGroupMsgStatus.query.filter_by(
                project_chat_group_id=project_chat_group.id).delete()
            ProjectChatGroupMsg.query.filter_by(
                project_chat_group_id=project_chat_group.id).delete()
            project_chat_group.member_links.delete()
            db.session.delete(project_chat_group)

            db.session.commit()

            emit("successfully_deleted_project_chat_group", room=request.sid)

            for member in members:
                emit("chat_group_removed", {
                    "id": project_chat_group.id,
                    "type": "projectChatGroup"
                },
                     room=member.user_id,
                     namespace="/")
Ejemplo n.º 2
0
    def on_edit_project_name_and_desc(data):
        if data["id"] in rooms() and current_user.is_project_owner_of(
                data["id"]):
            project = Project.query.filter_by(id=data["id"]).first()

            regexexp = re.compile("^[a-zA-Z0-9_-]*$")
            if 3 <= len(data["name"]) <= 32 and data[
                    "name"] != project.name and regexexp.search(data["name"]):

                if Project.query.filter(
                        Project.owner_name == project.owner_name,
                        Project.name.ilike(data["name"])).scalar():

                    emit(
                        "edit_project_name_and_desc_error_owner_has_already_project_with_that_name",
                        room=request.sid)
                    return

                if data["name"] in invalid_names():
                    return

                project.name = data["name"]

                ProjectChatGroup.query.filter_by(project_id=project.id)\
                    .update({ProjectChatGroup.name: f"{project.owner_name}/{project.name}"})

            if len(data["projectDesc"]
                   ) <= 128 and data["projectDesc"] != project.project_desc:
                project.project_desc = data["projectDesc"]

            db.session.commit()

            project_data = {
                "projectId": project.id,
                "name": project.name,
                "ownerName": project.owner_name,
                "projectDesc": project.project_desc
            }

            emit("edit_project_name_and_desc_successful", room=request.sid)

            for collab in db.session.query(
                    ProjectCollaboratorLink.user_id).filter_by(
                        project_id=project.id).all():
                emit("update_project_attributes",
                     project_data,
                     room=collab.user_id,
                     namespace="/")

            emit("update_project_attributes",
                 project_data,
                 room=current_user.id,
                 namespace="/")
Ejemplo n.º 3
0
    def on_change_project_visibility(data):
        if data["projectId"] in rooms() and current_user.is_project_owner_of(
                data["projectId"]):
            Project.query.filter_by(id=data["projectId"])\
                .update({Project.visibility: data["value"]})
            db.session.commit()

            emit("successfully_changed_project_visibility", room=request.sid)

            emit("update_project_visibility",
                 data["value"],
                 room=data["projectId"])
Ejemplo n.º 4
0
    def on_change_user_role(data):
        if current_user.is_project_owner_of(data["projectId"]) \
                and data["newRole"] == "admin" or data["newRole"] == "access-only":

            collab_link = ProjectCollaboratorLink.query.filter_by(project_id=data["projectId"], user_id=data["userId"])\
                .first()

            if not collab_link:
                return

            collab_link.user_role = data["newRole"]
            db.session.commit()

            emit("successfully_changed_user_role", room=request.sid)
            emit("force_reload", namespace="/", room=int(data["userId"]))
Ejemplo n.º 5
0
    def on_change_project_owner(data):
        if data["projectId"] in rooms() and current_user.is_project_owner_of(
                data["projectId"]):
            project = Project.query.filter_by(id=data["projectId"]).first()

            new_owner_user = User.get_by_name_or_email(data["nameOrEmail"])

            if not new_owner_user or not new_owner_user.is_project_collaborator_of(data["projectId"]) \
                    or Project.query.filter_by(owner_name=new_owner_user.name).scalar():
                emit("change_project_owner_error_invalid_target",
                     room=request.sid)
                return

            ProjectCollaboratorLink.query.filter_by(
                project_id=data["projectId"],
                user_id=new_owner_user.id).delete()
            project.owner_name = new_owner_user.name
            db.session.add(
                ProjectCollaboratorLink(data["projectId"], current_user.id,
                                        "admin"))
            db.session.commit()

            project_data = {
                "projectId": project.id,
                "name": project.name,
                "ownerName": project.owner_name,
                "projectDesc": project.project_desc
            }

            for collab in db.session.query(
                    ProjectCollaboratorLink.user_id).filter_by(
                        project_id=project.id).all():
                emit("update_project_attributes",
                     project_data,
                     room=collab.user_id,
                     namespace="/")

            emit("update_project_attributes",
                 project_data,
                 room=new_owner_user.id,
                 namespace="/")

            emit("force_reload", room=current_user.id, namespace="/")
            emit("force_reload", room=new_owner_user.id, namespace="/")
Ejemplo n.º 6
0
    def on_create_project_chat_group(data):
        if data["projectId"] not in rooms(
        ) or not current_user.is_project_owner_of(data["projectId"]):
            return

        project = Project.query.filter_by(id=data["projectId"]).first()

        project_chat_group = ProjectChatGroup(
            f"{project.owner_name}/{project.name}", project.id)
        db.session.add(project_chat_group)
        db.session.flush()

        db.session.add(
            ProjectChatGroupMemberLink(project_chat_group.id, current_user.id,
                                       "default"))

        members = {
            current_user.id: {
                "id": current_user.id,
                "role": "default",
                "name": current_user.name,
                "picUrl": current_user.small_avatar_url,
                "goToUrl": f"/{current_user.name}"
            }
        }

        for user in db.session.query(User)\
                .filter(User.id.in_(
                                    db.session.query(ProjectCollaboratorLink.user_id)
                                    .filter_by(project_id=project.id))
                        ):

            db.session.add(
                ProjectChatGroupMemberLink(project_chat_group.id, user.id,
                                           "default"))

            members[user.id] = {
                "id": user.id,
                "role": "default",
                "name": user.name,
                "picUrl": user.small_avatar_url,
                "goToUrl": f"/{user.name}"
            }

        db.session.commit()

        emit("successfully_created_project_chat_group", room=request.sid)

        for key in members.keys():
            emit("new_chat_group", {
                "id": project_chat_group.id,
                "type": "projectChatGroup",
                "name": project_chat_group.name,
                "unreadCount": 0,
                "lastMsg": "None",
                "picUrl": "/static/img/group.png",
                "roleOfCurrentUser": "******",
                "members": members,
                "msgs": []
            },
                 room=int(key),
                 namespace="/")
Ejemplo n.º 7
0
    def on_delete_project(data):
        if data["id"] in rooms() and current_user.is_project_owner_of(
                data["id"]):
            project = Project.query.filter_by(id=data["id"]).first()

            for _list in project.lists:
                for card in _list.cards:
                    for file in card.attached_files:
                        _, key = split_s3_obj_url(file.url)
                        s3_cli.delete_object(
                            Bucket=app.config["S3_BUCKET_NAME"], Key=key)
                        db.session.delete(file)

                    CardUserAssignment.query.filter_by(
                        card_id=card.id).delete()

                    db.session.delete(card)

                for file in _list.attached_files:
                    _, key = split_s3_obj_url(file.url)
                    s3_cli.delete_object(Bucket=app.config["S3_BUCKET_NAME"],
                                         Key=key)
                    db.session.delete(file)

                db.session.delete(_list)

            ProjectCollaboratorLink.query.filter_by(
                project_id=project.id).delete()

            project_chat_group = project.chat_group.first()
            if project_chat_group:
                ProjectChatGroupMsgStatus.query.filter_by(
                    project_chat_group_id=project_chat_group.id).delete()
                ProjectChatGroupMsg.query.filter_by(
                    project_chat_group_id=project_chat_group.id).delete()
                project_chat_group.member_links.delete()
                db.session.delete(project_chat_group)

            for user in db.session.query(ProjectCollaboratorLink.user_id) \
                    .filter_by(project_id=project.id):

                emit("removed_as_collab_of_project",
                     project.id,
                     room=user.id,
                     namspace="/")

                if project_chat_group:
                    emit("chat_group_removed", {
                        "type": "projectChatGroup",
                        "id": project_chat_group.id
                    },
                         room=user.id,
                         namspace="/")

            db.session.query(HistoryListCreated).filter_by(
                project_id=data["id"]).delete()
            db.session.query(HistoryListDeleted).filter_by(
                project_id=data["id"]).delete()
            db.session.query(HistoryCardCreated).filter_by(
                project_id=data["id"]).delete()
            db.session.query(HistoryCardDeleted).filter_by(
                project_id=data["id"]).delete()
            db.session.query(HistoryCardChangedList).filter_by(
                project_id=data["id"]).delete()

            db.session.delete(project)

            db.session.commit()

            emit("project_deleted", room=int(data["id"]))

            close_room(data["id"])
Ejemplo n.º 8
0
    def on_remove_user_from_project(data):
        current_user_is_project_owner = current_user.is_project_owner_of(
            data["projectId"])
        if data["projectId"] in rooms() and \
                (current_user_is_project_owner or
                 (current_user.id == data["userId"] and not current_user_is_project_owner)):

            ProjectCollaboratorLink.query.filter_by(
                project_id=data["projectId"], user_id=data["userId"]).delete()
            CardUserAssignment.query\
                .filter(CardUserAssignment.user_id == data["userId"],
                        CardUserAssignment.card_id.in_(db.session.query(Card.id)
                                                       .filter(Card.list_id.in_(db.session.query(List.id)
                                                                                .filter(List.project_id ==
                                                                                        data["projectId"])
                                                                                )
                                                               )
                                                       )
                        ).delete(synchronize_session=False)

            project_chat_group = ProjectChatGroup.query.filter_by(
                project_id=data["projectId"]).first()
            if project_chat_group:
                ProjectChatGroupMemberLink.query.filter_by(
                    project_chat_group_id=project_chat_group.id,
                    user_id=data["userId"]).delete()

                emit("chat_group_removed", {
                    "id": project_chat_group.id,
                    "type": "projectChatGroup"
                },
                     namespace="/",
                     room=int(data["userId"]))

                for member in project_chat_group.get_members_with_only_id_entities(
                ):
                    if member.user_id != data["userId"]:
                        emit("chat_group_member_removed", {
                            "chatGroupId": project_chat_group.id,
                            "chatGroupType": "projectChatGroup",
                            "userId": data["userId"]
                        },
                             namespace="/",
                             room=member.user_id)

            db.session.commit()

            if current_user.id == data["userId"]:
                emit("successfully_left_project", room=request.sid)
            else:
                emit("successfully_removed_project_collab", room=request.sid)

            emit("project_collab_removed", {"id": data["userId"]},
                 room=int(data["projectId"]))

            emit(
                "removed_as_collab_of_project",
                data["projectId"],
                room=int(data["userId"]),
                namespace="/",
            )
Ejemplo n.º 9
0
    def on_add_friend_to_project(data):
        if data["projectId"] in rooms() and current_user.is_project_owner_of(
                data["projectId"]):
            project = Project.query.filter_by(id=data["projectId"]).first()

            friend = User.get_by_name_or_email(data["friendNameOrEmail"])

            if not friend or not current_user.is_friend_with(friend.id) \
                    or friend.is_project_collaborator_of(project.id):
                emit("add_friend_to_project_error_invalid_target",
                     room=request.sid)
                return

            if data["role"] != "admin" and data["role"] != "access-only":
                return

            db.session.add(
                ProjectCollaboratorLink(data["projectId"], friend.id,
                                        data["role"]))

            project_chat_group = project.chat_group.first()
            if project_chat_group:

                member_data = {
                    friend.id: {
                        "id": friend.id,
                        "role": "default",
                        "name": friend.name,
                        "picUrl": friend.small_avatar_url,
                        "goToUrl": f"/{friend.name}"
                    }
                }

                for member_user in db.session.query(ProjectChatGroupMemberLink) \
                        .filter_by(project_chat_group_id=project_chat_group.id) \
                        .join(User, User.id == ProjectChatGroupMemberLink.user_id) \
                        .with_entities(User.id,
                                       User.name,
                                       User.small_avatar_url):

                    member_data[member_user.id] = {
                        "id": member_user.id,
                        "name": member_user.name,
                        "picUrl": member_user.small_avatar_url,
                        "goToUrl": f"/{member_user.name}",
                        "role": "default"
                    }

                    emit("new_chat_group_member", {
                        "chatGroupId": project_chat_group.id,
                        "chatGroupType": "projectChatGroup",
                        "member": {
                            "id": friend.id,
                            "name": friend.name,
                            "picUrl": friend.small_avatar_url,
                            "goToUrl": f"/{friend.name}",
                            "role": "default"
                        }
                    },
                         namespace="/",
                         room=member_user.id)

                db.session.add(
                    ProjectChatGroupMemberLink(project_chat_group.id,
                                               friend.id, "default"))

                last_msg = current_user.get_last_msg(project_chat_group.id,
                                                     "projectChatGroup")
                last_msg = {
                    "content": last_msg.content,
                    "datetime": f"{last_msg.datetime}+00:00"
                } if last_msg else "None"

                emit("new_chat_group", {
                    "id": project_chat_group.id,
                    "type": "projectChatGroup",
                    "name": project_chat_group.name,
                    "unreadCount": 0,
                    "lastMsg": last_msg,
                    "picUrl": "/static/img/group.png",
                    "roleOfCurrentUser": "******",
                    "members": member_data
                },
                     room=friend.id,
                     namespace="/")

            db.session.commit()

            emit("successfully_added_friend_to_project", room=request.sid)

            emit("new_project_collab", {
                "id": friend.id,
                "name": friend.name,
                "picUrl": friend.small_avatar_url,
                "goToUrl": f"/{friend.name}",
                "role": data["role"]
            },
                 room=data["projectId"])

            emit("now_collab_of_project", {
                "id": data["projectId"],
                "name": project.name,
                "ownerName": project.owner_name,
                "goToUrl": f"/{project.owner_name}/{project.name}"
            },
                 namespace="/",
                 room=friend.id)