def message_send(token, channel_id, message, time_sent): global GLOBAL_DATA # Send message from authorised user to channel if len(message) > 1000: raise ValueError("Message is more than 1000 characters") elif time_sent < 0: raise ValueError elif channel_id < 0 or len(GLOBAL_DATA["channels"]) <= channel_id: raise ValueError # raise AccessError when user has has not joined the channel target_channel = GLOBAL_DATA["channels"][channel_id] user_name = get_user_from_token(token) if user_name not in target_channel.members: raise AccessError("authorised user has not joined the channel they are trying to post to") #timer start now = time.time() future = now + time_sent while time.time() < future: pass new_message = Message(token, message) target_channel.messages.append(new_message) GLOBAL_DATA["messages"].append(new_message) return {"message_id": new_message.message_id}
def message_unpin(token, message_id): global GLOBAL_DATA # testing #invalid message_id if message_id < 0 or message_id >= len(GLOBAL_DATA["messages"]): raise ValueError # token is not admin if is_admin_or_owner_token(token) == False: raise ValueError target_msg = GLOBAL_DATA["messages"][message_id] # message already unpinned if target_msg.is_pinned == False: raise ValueError for Channels in GLOBAL_DATA["channels"]: for msgs in Channels.messages: if message_id == msgs.message_id: break if get_user_from_token(token) in Channels.members: target_msg.is_pinned = False return {} else: raise AccessError
def channels_list(token): name = get_user_from_token(token) print(name) channel_list = [] for channels in GLOBAL_DATA["channels"]: new_dict = {} if name in channels.members: new_dict["id"] = channels.id new_dict["name"] = channels.name #print(new_dict) channel_list.append(new_dict) return {"channels": channel_list}
def channel_join(token, Channel_id): global GLOBAL_DATA no_of_channels = len(GLOBAL_DATA["channels"]) # invalid channel_id if Channel_id > no_of_channels - 1 or Channel_id < 0: raise ValueError for channels in GLOBAL_DATA["channels"]: if Channel_id == channels.id: if channels.is_public == False: raise AccessError else: user = get_user_from_token(token) channels.members.append(user) return {}
def channel_leave(token, Channel_id): global GLOBAL_DATA user = get_user_from_token(token) print(user) no_of_channels = len(GLOBAL_DATA["channels"]) if Channel_id > no_of_channels - 1 or Channel_id < 0: raise ValueError for channels in GLOBAL_DATA["channels"]: if Channel_id == channels.id: if user in channels.members: channels.members.remove(user) return {} else: raise AccessError
def standup_send(token, channel_id, message): global GLOBAL_DATA global standup_lst if channel_id < 0 or channel_id >= len(GLOBAL_DATA["channels"]): raise ValueError if len(message) > 1000: raise ValueError target_channel = GLOBAL_DATA["channels"][channel_id] user = get_user_from_token(token) if user not in target_channel.members: raise AccessError if target_channel.standup_active == False: raise ValueError # all errors passed, then append to standup_send new_msg = {user: "******"} standup_lst.append(new_msg) return {}
def message_send(token, channel_id, message): global GLOBAL_DATA # Send message from authorised user to channel if len(message) > 1000: raise ValueError("Message is more than 1000 characters") # raise AccessError when user has has not joined the channel target_channel = GLOBAL_DATA["channels"][channel_id] user_name = get_user_from_token(token) if user_name not in target_channel.members: raise AccessError( "authorised user has not joined the channel they are trying to post to" ) # creates a new message instance and adds it to global data new_message = Message(token, message) target_channel.messages.append(new_message) GLOBAL_DATA["messages"].append(new_message) return {"message_id": new_message.message_id}
def channel_messages(token, channel_id, start): global GLOBAL_DATA # invalid channel_id no_of_channels = len(GLOBAL_DATA["channels"]) if channel_id > no_of_channels - 1 or channel_id < 0: raise ValueError #when the user is not part of the channel user_is_member = False name = get_user_from_token(token) target_channel = GLOBAL_DATA["channels"][channel_id] if name not in target_channel.members: raise AccessError target_channel.messages = [f"message{i}" for i in range(0, 20)] #invalid when start is greater than the length of messages if start > len(target_channel.messages): raise ValueError msg_lst = [] LENGTH_OF_MESSAGES = len(target_channel.messages) if LENGTH_OF_MESSAGES > 50: for i in range(0, 50): i += start msg_lst.append(target_channel.messages[i]) last = start + 50 else: print("case 2") for i in range(0, LENGTH_OF_MESSAGES): i += start msg_lst.append(target_channel.messages[i]) last = -1 return {"messages": msg_lst, "start": start, "end": last}