def delete_event(user_id, event_id):
    if not id_is_valid(user_id) or not id_is_valid(event_id):
        return False

    result = users_collection.update_one({'_id': ObjectId(user_id)},
                                         {'$pull': {'event_id_list': ObjectId(event_id)}})
    return result.matched_count > 0
def add_friend(user_id, friend_id):
    if not id_is_valid(user_id) or not id_is_valid(friend_id):
        return False

    result = users_collection.update_one({'_id': ObjectId(user_id)},
                                         {'$push': {'friend_id_list': ObjectId(friend_id)}})
    return result.matched_count > 0
Esempio n. 3
0
def delete_msg_from_event_chat(event_chat_id, msg_id):
    if not id_is_valid(event_chat_id) or not id_is_valid(msg_id):
        return False

    result = event_chats_collection.update_one(
        {'_id': ObjectId(event_chat_id)}, {'$pull': {
            'msg_id_list': msg_id
        }})
    return result.matched_count > 0
Esempio n. 4
0
def add_msg_to_dialog(dialog_id, msg_id):
    if not id_is_valid(dialog_id) or not id_is_valid(msg_id):
        return False

    result = dialogs_collection.update_one({'_id': ObjectId(dialog_id)},
                                           {'$push': {
                                               'msg_id_list': msg_id
                                           }})
    return result.matched_count > 0
def set_chat_id(group_event_id, chat_id):
    if not id_is_valid(group_event_id) or not id_is_valid(chat_id):
        return False

    result = group_event_collection.update_one(
        {'_id': ObjectId(group_event_id)},
        {'$set': {
            'chat_id': ObjectId(chat_id)
        }})
    return result.matched_count > 0
def delete_member(group_event_id, member_id):
    if not id_is_valid(group_event_id) or not id_is_valid(member_id):
        return False

    result = group_event_collection.update_one(
        {'_id': ObjectId(group_event_id)},
        {'$pull': {
            'member_id_list': ObjectId(member_id)
        }})
    return result.matched_count > 0
Esempio n. 7
0
def get_by_user_event(user_id, event_id):
    if not id_is_valid(user_id) or not id_is_valid(event_id):
        return None

    json = event_member_collection.find_one({
        'user_id': ObjectId(user_id),
        'event_id': ObjectId(event_id)
    })

    return create_event_from_json(json)
def leave_group_event(group_event_id, user: User):
    if group_event_id is None or not id_is_valid(group_event_id):
        return abort(400)

    group_event = group_event_dao.get(group_event_id)
    if group_event is None:
        return abort(404, "Group event is not found")

    event_member = event_member_dao.get_by_user_event(user.id, group_event_id)
    if event_member is None:
        return abort(404, "Event member is not found")

    # delete event if this member is last
    if len(group_event.member_id_list) == 1:
        if delete_group_event(group_event_id):
            return '', 204
        else:
            return abort(500, "Group event was not delete")

    if not group_event_dao.delete_member(group_event_id, event_member.id):
        return abort(500, "Event member was not delete from event")

    if not user_dao.delete_chat(event_member.user_id, group_event.chat_id):
        return abort(500, "Chat was not delete from user")
    if not user_dao.delete_event(event_member.user_id, group_event.id):
        return abort(500, "Event was not delete from user")

    event_member_dao.delete(event_member.id)
    return '', 204
Esempio n. 9
0
def delete_msg(msg_id):
    if not id_is_valid(msg_id):
        return False

    return msg_collection.delete_one({
        '_id': ObjectId(msg_id)
    }).deleted_count > 0
Esempio n. 10
0
def delete(event_member_id):
    if not id_is_valid(event_member_id):
        return False

    return event_member_collection.delete_one({
        '_id': ObjectId(event_member_id)
    }).deleted_count > 0
def delete(id):
    if not id_is_valid(id):
        return False

    return group_event_collection.delete_one({
        '_id': ObjectId(id)
    }).deleted_count > 0
Esempio n. 12
0
def delete_event_chat(event_chat_id):
    if not id_is_valid(event_chat_id):
        return False

    return event_chats_collection.delete_one({
        '_id': ObjectId(event_chat_id)
    }).deleted_count > 0
Esempio n. 13
0
def send_msg(json, user: User):
    chat_id = json.get('chat_id')
    if chat_id is None or not id_is_valid(chat_id):
        return abort(400, "Chat id is not valid")

    if not chat_dao.chat_is_exist(chat_id):
        return abort(404)

    if ObjectId(chat_id) not in user.chat_id_list:
        return abort(403)

    msg_text = json.get('text')
    if msg_text is None:
        return abort(400, "Text of message is not defined")

    msg = Message(user.id, chat_id, datetime.today(), msg_text)
    msg_id = msg_dao.save_msg(msg)
    if chat_dao.dialog_is_exist(chat_id):
        if not chat_dao.add_msg_to_dialog(chat_id, msg_id):
            return abort(500)
    if chat_dao.event_chat_is_exist(chat_id):
        if not chat_dao.add_msg_to_event_chat(chat_id, msg_id):
            return abort(500)

    return '', 204
Esempio n. 14
0
def delete_dialog(dialog_id):
    if not id_is_valid(dialog_id):
        return False

    return dialogs_collection.delete_one({
        '_id': ObjectId(dialog_id)
    }).deleted_count > 0
Esempio n. 15
0
def set_user_birthday(user_id, birthday: datetime):
    if not id_is_valid(user_id):
        return False

    result = users_collection.update_one({'_id': ObjectId(user_id)},
                                         {'$set': {'birthday': birthday}})
    return result.matched_count > 0
Esempio n. 16
0
def update_picture(user_id, picture):
    if not id_is_valid(user_id) or picture is None:
        return False

    result = users_collection.update_one({'_id': ObjectId(user_id)},
                                         {'$set': {'profile_pic': picture}})
    return result.matched_count > 0
Esempio n. 17
0
def update_text(msg_id, text):
    if not id_is_valid(msg_id):
        return False

    result = msg_collection.update_one({'_id': ObjectId(msg_id)},
                                       {'$set': {
                                           'text': text
                                       }})
    return result.matched_count > 0
def get_friend(friend_id):
    if friend_id is None or not id_is_valid(friend_id):
        return abort(400)

    friend = user_dao.get_user(friend_id)
    if friend is None:
        return abort(404)

    return json_util.dumps(friend.to_friend_json()), 200
Esempio n. 19
0
def get_dialog(dialog_id):
    if not id_is_valid(dialog_id):
        return None

    json = dialogs_collection.find_one({'_id': ObjectId(dialog_id)})
    if json is None:
        return None

    return Dialog(json['user_id_1'], json['user_id_2'], json['_id'],
                  json['msg_id_list'])
Esempio n. 20
0
def get_msg(msg_id):
    if not id_is_valid(msg_id):
        return None

    json = msg_collection.find_one({'_id': ObjectId(msg_id)})
    if json is None:
        return None

    return Message(json['user_id'], json['chat_id'], json['datetime'],
                   json['text'], json['_id'])
Esempio n. 21
0
def get(single_event_id):
    if not id_is_valid(single_event_id):
        return None

    json = single_event_collection.find_one({'_id': ObjectId(single_event_id)})
    if json is None:
        return None

    return SingleEvent(json['name'], json['is_private'], json['datetime'],
                       json['address'], json['description'], json['_id'])
Esempio n. 22
0
def set_can_delete_event(group_event_id, is_can_delete_event):
    if not id_is_valid(group_event_id):
        return 0

    result = event_member_collection.update_one(
        {'_id': ObjectId(group_event_id)},
        {'$set': {
            'is_can_delete_event': is_can_delete_event
        }})
    return result.matched_count
def get(group_event_id):
    if not id_is_valid(group_event_id):
        return None

    json = group_event_collection.find_one({'_id': ObjectId(group_event_id)})
    if json is None:
        return None

    return GroupEvent(json['name'], json['is_private'], json['datetime'],
                      json['address'], json['description'],
                      json['member_id_list'], json['chat_id'], json['_id'])
Esempio n. 24
0
def delete_msg(msg_id):
    if msg_id is None or not id_is_valid(msg_id):
        return abort(400, "Message id is not valid")

    if not msg_dao.is_exists(msg_id):
        return abort(404)

    if not msg_dao.delete_msg(msg_id):
        return abort(500)

    return '', 204
Esempio n. 25
0
def update(event: Event, collection):
    if not id_is_valid(event.id):
        return False

    result = collection.update_one({'_id': ObjectId(event.id)},
                                   {'$set': {'name': event.name,
                                             'is_private': event.is_private,
                                             'datetime': event.datetime,
                                             'address': event.address,
                                             'description': event.description}})
    return result.matched_count > 0
def add_member(event_id,
               user_id,
               is_can_invite_user=False,
               is_can_delete_user=False,
               is_can_change_event=False,
               is_can_delete_event=False):
    if not id_is_valid(event_id) or not id_is_valid(user_id):
        print("add_member - event_id or user_id is not valid")
        return None

    member = EventMember(event_id, user_id, is_can_invite_user,
                         is_can_delete_user, is_can_change_event,
                         is_can_delete_event)
    member_id = event_member_dao.save(member)
    group_event_dao.add_member(event_id, member_id)

    user_dao.add_event(user_id, event_id)
    event = group_event_dao.get(event_id)
    user_dao.add_chat(user_id, event.chat_id)

    return member_id
def send_invite(request_json, user: User):
    if request_json is None:
        return abort(400)

    invite_type = request_json.get('type')

    receiver_id = request_json.get('receiver_id')
    if receiver_id == str(user.id):
        return abort(400)

    event_id = request_json.get('event_id')
    if event_id is None:
        if invite_type == InviteType.EVENT:
            return abort(400)

        event_id = ""

    if not valid_invite_type(invite_type):
        return abort(400)

    if receiver_id is None or not id_is_valid(receiver_id):
        return abort(400)

    if event_id != "":
        if event_id is None or not id_is_valid(event_id):
            return abort(400)

        # check that user can send invite to this event
        event_member = event_member_dao.get_by_user_event(user.id, event_id)
        if event_member is None:
            return abort(404)

        if not event_member.is_can_invite_user:
            return abort(403)

    invite = Invite(user.id, receiver_id, invite_type, event_id)
    invite_id = invite_dao.save_invite(invite)
    user_dao.add_invite(receiver_id, invite_id)
    return '', 204
Esempio n. 28
0
def get_event(event_id, user: User):
    if event_id is None or not id_is_valid(event_id):
        return abort(400)

    if ObjectId(event_id) not in user.event_id_list:
        return abort(403)

    event = event_dao.get_event(event_id)
    if event is None:
        return abort(404)

    # TODO need load all members
    return json_util.dumps(event.__dict__), 200
def get_chat(chat_id, user: User):
    if chat_id is None or not id_is_valid(chat_id):
        return abort(400)

    if ObjectId(chat_id) not in user.chat_id_list:
        return abort(403)

    chat = chat_dao.get_chat(chat_id)
    if chat is None:
        return abort(404)

    chat.convert_all_msg_id_in_msg_entity()
    return json_util.dumps(chat.__dict__)
def remove_friend(friend_id, user: User):
    if friend_id is None or not id_is_valid(friend_id):
        return abort(400)

    friend = user_dao.get_user(friend_id)
    if friend is None:
        return abort(404)

    success_delete_friends = user_dao.delete_friend(user.id, friend_id)
    if not user_dao.delete_friend(friend_id, user.id):
        success_delete_friends = False

    if not success_delete_friends:
        return abort(500)

    return '', 204