def channel_join(token, channel_id): # check validity of inputs check_token(token) check_channel_id(channel_id) # get data user = get_user_from_token(token) channel = get_channel(channel_id) # create a dict of type member member_details = get_member(token) # check if user is authorised to join if channel["is_public"] is False and user["permission_id"] == 3: raise AccessError("Channel is private") # check if user is already in the channel if member_details in channel["all_members"]: return {} # Add user to channel channel["all_members"].append(member_details) # Add user to owner list if they have perms if user["permission_id"] != 3: channel["owner_members"].append(member_details) save() return {}
def standup_send(token, channel_id, message): # Check validity of inputs check_token(token) check_channel_id(channel_id) # Check the message is less than 1000 characters if len(message) > 1000: raise ValueError("Message can't be more than 1000 characters") # get the channel from channel_id channel = get_channel(channel_id) # Access error if user is not in the channel if get_member(token) not in get_channel(channel_id)["all_members"]: raise AccessError("User is not part of the channel") # Check if a standup is already running if channel["active_standup"] is False: raise ValueError("Standup is not running") # get the handle from token user = get_user_from_token(token) handle = user["handle_str"] # add handle to message string = handle + ": " + message.strip() + "\n" # add string to the standup message channel["standup_message"]["message"] += string save() return {}
def channel_leave(token, channel_id): # Check validity of inputs check_token(token) check_channel_id(channel_id) # create a dict of type member member_details = get_member(token) # Remove user from a channel channel = get_channel(channel_id) if member_details in channel["all_members"]: channel["all_members"].remove(member_details) if member_details in channel["owner_members"]: channel["owner_members"].remove(member_details) save() return {}
def channels_create(token, name, is_public): # Validate values check_token(token) check_channel_name(name) # Create a dict of type member member_details = get_member(token) # Create a new channel new_channel = { "channel_id": len(data["channels"]) + 1, "name": name, "is_public": is_public, "owner_members": [member_details], "all_members": [member_details], "messages": [], "future_messages": [], "standup_message": None, "active_standup": False } data["channels"].append(new_channel) save() return {"channel_id": new_channel["channel_id"]}
def message_sendlater(token, channel_id, message, time_sent): # Check validity of inputs check_token(token) check_channel_id(channel_id) # Check that message is less than 1000 characters if len(message) > 1000: raise ValueError("Message can't be more than 1000 characters") # Check that the time sent is not the past time_now = datetime.utcnow() if time_sent < time_now: raise ValueError("Invalid time set") # Get channel channel = get_channel(channel_id) # Raise an access error if user not in channel if get_member(token) not in channel["all_members"]: raise AccessError("User is not a member of the channel.") # Send a message from authorised_user to a specific channel at a specified time in the future # Grab the user data user = get_user_from_token(token) # Set the message dictionary up message = { "message_id": data["n_messages"] + 1, "u_id": user["u_id"], "message": message, "time_created": time_sent, "reacts": [{ "react_id": 0, "u_ids": [] }, { "react_id": 1, "u_ids": [] }, { "react_id": 2, "u_ids": [] }], "is_pinned": False } # Reserving the unique message id. this message id is not valid until message is sent, # but the message id will be usable after it is sent data["n_messages"] += 1 future_messages = channel["future_messages"] future_messages.append(message) # sort the list by timestamp(Source: # https://stackoverflow.com/questions/72899/how-do-i-sort-a-list-of-dictionaries-by-a-value-of-the-dictionary) future_messages = sorted(future_messages, key=lambda k: k["time_created"]) # save the sorted list channel["future_messages"] = future_messages save() return {"message_id": message["message_id"]}