예제 #1
0
def standup_close_v1(token, channel_id, u_id):
    with open("data.json") as json_file:
        data = load(json_file)

    standup_channel = None
    for standup_channel_data in data["standups"]:
        if standup_channel_data["channel_id"] == channel_id:
            standup_channel = standup_channel_data

    if standup_channel is None:
        return

    combine_message = "\n".join(standup_channel["message"])

    time_created = datetime.now().replace(microsecond=0).timestamp()

    for channel in data["channels"]:
        if channel["channel_id"] == channel_id:
            found_channel = channel

    all_msgs = found_channel["messages"]
    # Keep track of last message id
    last_msg_id = data.get("last_message_id", 0)
    message_id = last_msg_id + 1
    # Update data
    data["last_message_id"] = message_id
    # unix timestamp
    user_stats_update(data, u_id, stat_types.MSG_SEND, time_created)
    dreams_stats_update(data, stat_types.MSG_SEND, time_created)

    new_message = {
        "message_id": message_id,
        "u_id": int(u_id),
        "message": combine_message,
        "time_created": time_created,
        "reacts": [{
            'react_id': 0,
            'u_ids': [],
            'is_this_user_reacted': False
        }],
        "react_history": [],
        "is_pinned": False,
    }
    # Append the new message to the end of the list
    all_msgs.append(new_message)

    data["standups"].remove(standup_channel)

    with open("data.json", "w") as json_file:
        dump(data, json_file, indent=4)

    return
예제 #2
0
def channels_create_v2(token, name, is_public):
    '''
    Creates a new channel with that name that is either a public or private channel
    
    Returns type:
        { channel_id }

    Input Error:
        Name is more than 20 characters long
    '''
    time_stamp = datetime.now().replace(microsecond=0).timestamp()

    with open("data.json") as json_file:
        data = load(json_file)

    # Decode token (u_id) if valid, otherwise raise AccessError
    user = token_check(token)
    # Raise error if channel name is > 20 characters
    # or if there is no name given
    if len(name) > 20:
        raise InputError("Channel name is more than 20 characters.")
    if len(name) == 0:
        raise InputError("No channel name has been entered.")

    # Ensure users sets channel to private or public
    if is_public not in (True, False):
        raise InputError("Channel privacy is undefined")

    # Makes the channel id in order from 1 as they are created
    channel_id = len(data["channels"]) + 1
    # Store data in data.py channels
    user_stats_update(data, user["u_id"], stat_types.CH_JOIN, time_stamp)
    dreams_stats_update(data, stat_types.CH_CREATE, time_stamp)

    data["channels"].append(
        {
            'channel_id': channel_id,
            'name': name,
            'owner_members': [user['u_id']],
            'all_members': [user['u_id']],
            'invites' : [],
            'is_public': is_public,
            'messages': []
        }
    )
    with open("data.json", "w") as json_file:
        dump(data, json_file, indent=4)

    return { 
        'channel_id': channel_id 
    }
예제 #3
0
def dm_remove_v1(token, dm_id):
    """
    Remove an existing DM. This can only be done by the original creator of the DM.

    Arguments:
        token (string)  - jwt encoded data structure of auth_user
        dm_id (int)    - dm_id that be removed

    Exceptions:
        InputError      -   Occurs when dm_id does not refer to a valid DM
        AccessError     -   Occurs when the user is not the original DM creator

    Return value:
        Returns user (dictionary) on success
            a dictionary with information about nothing
    """
    time_stamp = datetime.now().replace(microsecond=0).timestamp()

    with open("data.json") as json_file:
        data = load(json_file)

    auth_user_id = token_check(token)["u_id"]

    is_dm_exist = False
    for dm_data in data["dms"]:
        if dm_data["dm_id"] == dm_id:
            dm = dm_data
            is_dm_exist = True
            break
    if is_dm_exist is False:
        raise InputError("dm_id does not refer to a valid DM")

    is_auth_user_creator = False
    if auth_user_id == dm["original_creator"]:
        is_auth_user_creator = True
    if is_auth_user_creator is False:
        raise AccessError("the user is not the original DM creator")

    dreams_stats_update(data, stat_types.DM_REMOVE, time_stamp)

    for member in dm["members"]:
        user_stats_update(data, member, stat_types.DM_LEAVE, time_stamp)

    data["dms"].remove(dm)

    with open("data.json", "w") as json_file:
        dump(data, json_file, indent=4)

    return {}