def user_profile_sethandle(token, handle_str): ''' Update the authorised user's handle. ''' #Check if token valid and get u_id if helper_functions.check_token(token).get('token_status'): raise error.AccessError(description="Token invalid") u_id = helper_functions.check_token(token).get('u_id') #If handle_str valid if len(handle_str) < 3 or len(handle_str) > 20: raise error.InputError(description="Handle_str invalid") #If handle_str already being used for item in auth.registered_users: if item.get("handle") == handle_str: raise error.InputError(description="Handle_str being used") #Update handle_str for item in auth.registered_users: if item.get("u_id") == u_id: item["handle"] = handle_str break return {}
def channel_leave(token, channel_id): ''' When a user leaves the channel. ''' # user token is invalid if helper_functions.check_token(token).get('token_status'): raise error.InputError(description="Token invalid") user_id = helper_functions.check_token(token).get("u_id") #Checked invalid channel if helper_functions.check_channelid_valid(channel_id): raise error.InputError("Channel not valid") #User already in channel if helper_functions.check_u_id_in_channel(user_id, channel_id): raise error.AccessError("Already in channel") for current_channel in channels.channel_data: if current_channel.get("channel_id") == channel_id: for members in current_channel.get("member_ids"): if user_id == members: current_channel["member_ids"].remove(user_id) break break return {}
def channel_invite(token, channel_id, u_id): ''' Inviting a user to join the channel. ''' #Checked invalid channel if helper_functions.check_channelid_valid(channel_id): raise error.InputError("Channel not valid") # user id is not valid if helper_functions.check_uid_valid(u_id): raise error.InputError("User not valid") # user token is invalid if helper_functions.check_token(token).get('token_status'): raise error.InputError(description="Token invalid") auth_id = helper_functions.check_token(token).get('u_id') #If token is in channel if helper_functions.check_u_id_in_channel(auth_id, channel_id): raise error.AccessError("Authorised user not in channel") #User already in channel if helper_functions.check_u_id_in_channel(u_id, channel_id) == False: raise error.AccessError("Already in channel") # add to user to channel if everything is valid # details_user = {u_id, email, name_first, name_last, handle_str} for user in channels.channel_data: if user.get("channel_id") == channel_id: user["member_ids"].append(u_id) break return {}
def standup_send(token, channel_id, message): ''' Send message in standup ''' # Checking is the token exist if helper_functions.check_token(token).get('token_status'): raise error.AccessError(description="Token invalid") u_id = helper_functions.check_token(token).get('u_id') #If channel valid if helper_functions.check_channelid_valid(channel_id): raise error.InputError(description="Channel_id invalid") #If startup is active in channel if standup_active(token, channel_id)['is_active'] == False: raise error.InputError(description="Startup is not currently active") #If message is more than 1000 characters if len(message) > 1000: raise error.InputError(description="Message length too long") #Make a new message for user in auth.registered_users: if user['u_id'] == u_id: handle_str = user['handle'] break string = str(handle_str) + ": " + str(message) #Append message to CHANNELSMSG CHANNELSMSG[channel_id].append(string) return {}
def channel_addowner(token, channel_id, u_id): ''' When a user is added as an owner to the channel. ''' #Checked invalid channel if helper_functions.check_channelid_valid(channel_id): raise error.InputError("Channel not valid") # user token is invalid if helper_functions.check_token(token).get('token_status'): raise error.InputError(description="Token invalid") u_id_for_token = helper_functions.check_token(token).get("u_id") #Check if they are an owner if helper_functions.check_uid_owner_in_channel(u_id_for_token, channel_id): raise error.AccessError( "Authorised user not in channel or flockr owner") #If u_id already owner if helper_functions.check_uid_owner_in_channel(u_id, channel_id) == False: raise error.InputError("u_id already owner") #Append u_id to list of owners for user in channels.channel_data: if user.get("channel_id") == channel_id: user.get("owner_ids").append(u_id) break return {}
def user_profile_setname(token, name_first, name_last): ''' Updates the authorised user's first and last name. ''' # Checking is the token exist then getting their u_id if helper_functions.check_token(token).get('token_status'): raise error.AccessError(description="Token invalid") user_id = helper_functions.check_token(token).get('u_id') #If first_name is not 1 < first_name < 50 if (len(name_first) < 1) or (len(name_first) > 50): raise error.InputError( "First name invalid, needs to be between 1 and 50 characters") #If last_name is not 1 < last_name < 50 if (len(name_last) < 1) or (len(name_last) > 50): raise error.InputError( "Last name invalid, needs to be between 1 and 50 characters") # new value for keys to update name new_name = {'first_name': name_first, 'last_name': name_last} # loops through users in list for users in auth.registered_users: if users['u_id'] == user_id: # gets rid of existing value and replaces it with new value users.update(new_name) break return {}
def channel_details(token, channel_id): ''' Presenting the details of the channel. ''' #Checked invalid channel if helper_functions.check_channelid_valid(channel_id): raise error.InputError("Channel not valid") # user token is invalid if helper_functions.check_token(token).get('token_status'): raise error.InputError(description="Token invalid") user_id = helper_functions.check_token(token).get('u_id') #User already in channel if helper_functions.check_u_id_in_channel(user_id, channel_id): raise error.AccessError("Not in channel") #return details about channel channel_detail = { 'name': 'name', 'owner_members': [], 'all_members': [], } for curr_channels in channels.list_of_all_channels: if curr_channels.get('channel_id') == channel_id: channel_detail['name'] = curr_channels['name'] break for curr_channels in channels.channel_data: if curr_channels.get('channel_id') == channel_id: user_ids = curr_channels.get("member_ids") for user in user_ids: for user_detail in auth.registered_users: if user_detail['u_id'] == user: member_details = { 'u_id': user, 'name_first': user_detail['first_name'], 'name_last': user_detail['last_name'], 'profile_img_url': user_detail['profile_img_url'], } channel_detail['all_members'].append(member_details) for curr_channels in channels.channel_data: if curr_channels.get('channel_id') == channel_id: user_ids = curr_channels.get("owner_ids") for user in user_ids: for user_detail in auth.registered_users: if user_detail['u_id'] == user: owner_details = { 'u_id': user, 'name_first': user_detail['first_name'], 'name_last': user_detail['last_name'], 'profile_img_url': user_detail['profile_img_url'], } channel_detail['owner_members'].append(owner_details) return channel_detail
def search(token, query_str): ''' Searches all channel messages and uses regex to find similar messages. ''' # Checking is the token exist then getting their u_id if helper_functions.check_token(token).get('token_status'): raise error.AccessError(description="Token invalid") u_id = helper_functions.check_token(token).get('u_id') messages = [] #return dictionary of details of query string for chan in channels.channel_data: for user in chan.get("member_ids"): if user == u_id: for line in chan.get("messages"): if line.get('message_sent') is None: break #Seeing if user has reacted is_this_user_reacted = False if u_id in line.get('reacts')[0]['u_ids']: is_this_user_reacted = True if re.search(str(query_str), line.get('message_sent')): msg = { 'message_id': line.get('message_id'), 'u_id': line.get('user_id'), 'message': line.get('message_sent'), 'time_created': line.get('time_created'), 'reacts': [{ 'react_id': line.get('reacts')[0].get('react_id'), 'u_ids': line.get('reacts')[0].get('u_ids'), 'is_this_user_reacted': is_this_user_reacted, }], 'is_pinned': line.get('is_pinned'), } messages.append(msg) return {'messages': messages}
def channels_create(token, name, is_public): ''' Creates a new channel that is either public or private. ''' # Checking for a valid token if helper_functions.check_token(token).get('token_status'): raise error.InputError(description="Token invalid") user_id = helper_functions.check_token(token).get('u_id') # Error is the channel is greater than 20 if len(name) > 20: raise error.InputError( "The channel name you have entered is greater than 20 characters.") # Error if theres no channel name if len(name) == 0: raise error.InputError("No channel name entered.") # Assigning the channel id as the number of channels number_of_channels = len(list_of_all_channels) channel_id = number_of_channels + 1 channels_details = {'channel_id': channel_id, 'name': name} channel_data_base = { 'owner_ids': [], 'member_ids': [], 'channel_id': channel_id, 'is_public': True, 'messages': [], 'name': name } # Assigning whether the channels is public or private channel_data_base['is_public'] = is_public # Adding the data of the channel for user in auth.registered_tokens: if token == user.get("token"): user_id = user.get("u_id") channel_data_base['owner_ids'].append(user_id) channel_data_base['member_ids'].append(user_id) # Adding the newly created channel into the list channel_data.append(channel_data_base) list_of_all_channels.append(channels_details) return {'channel_id': channel_id}
def standup_start(token, channel_id, length): ''' Begin the standup ''' # Checking is the token exist then getting their u_id if helper_functions.check_token(token).get('token_status'): raise error.AccessError(description="Token invalid") #If channel valid if helper_functions.check_channelid_valid(channel_id): raise error.InputError(description="Channel_id invalid") #If startup is active in channel if standup_active(token, channel_id)['is_active'] == True: raise error.InputError(description="Startup is currently active") #Get the time it will end dt_finish = datetime.now() + timedelta(seconds=length) time_finish = dt_finish.timestamp() #Append startup to STANDUPS STANDUPS.append({ 'channel_id': channel_id, 'time_finish': int(time_finish) }) #Append to CHANNELMSGS CHANNELSMSG[channel_id] = [] t = threading.Timer(int(length), helper_send_message, args=[token, channel_id]) t.start() return {'time_finish': int(time_finish)}
def message_react(token, message_id, react_id): ''' Given a message, allow the user to add a react to it. ''' # Takes in react_id as a param --> need to be generated when the message is sent # Checking if token is valid if helper_functions.check_token(token).get('token_status'): raise error.AccessError(description="Token invalid") user_id = helper_functions.check_token(token).get('u_id') # Checking if message ID is valid invalid_m_id = True for data in channels.channel_data: for msg in data['messages']: if msg.get('message_id') == message_id: #channel_id = data.get('channel_id') invalid_m_id = False break if invalid_m_id: raise error.InputError('You have entered an invalid message ID') for user in auth.registered_tokens: if user.get("token") == token: user_id = user.get('u_id') break already_reacted = True if react_id != 1: raise error.InputError('Invalid react_id entered') for data in channels.channel_data: for message_data in data.get('messages'): if message_id == message_data.get('message_id'): for react_data in message_data.get('reacts'): if react_id == 1: if user_id not in react_data['u_ids']: react_data['u_ids'].append(user_id) already_reacted = False break if already_reacted: raise error.InputError('You have already reacted to this message') return {}
def message_unpin(token, message_id): # check input error # message_id invalid: #Invalid token test if helper_functions.check_token(token).get('token_status'): raise error.AccessError(description="Token invalid") user_id = helper_functions.check_token(token).get('u_id') channel_id = "" invalid_msg_id = True for data in channels.channel_data: for msg in data['messages']: if msg.get('message_id') == message_id: channel_id = int(data.get('channel_id')) invalid_msg_id = False break if invalid_msg_id: raise error.InputError("You have entered an invalid message ID") # check access error # not member of channel user_id = "" for user in auth.registered_tokens: if user.get("token") == token: user_id = int(user.get('u_id')) break # not owner / flockr owner for channel_datas in channels.channel_data: if channel_datas.get("channel_id") == channel_id: if user_id not in channel_datas['owner_ids']: raise error.AccessError("You are not in this channel / not an owner") else: break # message already pinned? if not, pin. for data in channels.channel_data: for message_data in data.get('messages'): if message_data.get("message_id") == message_id: if message_data.get("is_pinned") == False: raise error.InputError("This message is already unpinned") else: message_data['is_pinned'] = False return {}
def admin_userpermission_change(token, u_id, permission_id): ''' Changes the permissions of the u_id entered. ''' #If permission_id invalid if permission_id < 1 or permission_id > 2: raise error.InputError(description="permission_id invalid") # check for valid user: # Checking is the token exist then getting their u_id if helper_functions.check_token(token).get('token_status'): raise error.AccessError(description="Token invalid") token_uid = helper_functions.check_token(token).get('u_id') #If u_id valid if helper_functions.check_uid_valid(u_id): raise error.InputError(description="Invalid u_id") #If token is not owner not_owner = True for user in auth.registered_users: if user.get("u_id") == token_uid: if user.get("permissions") == 1: not_owner = False break if not_owner: raise error.AccessError("Authorised user is not owner") #Update u_id permissions for user in auth.registered_users: if user.get("u_id") == u_id: user["permissions"] = permission_id break #If member in channel also add to owner if permission_id == 1: for data in channels.channel_data: if u_id in data.get('member_ids'): if u_id not in data.get('owner_ids'): data.get('owner_ids').append(u_id) return {}
def channel_join(token, channel_id): ''' When a user joins the channel. ''' # user token is invalid if helper_functions.check_token(token).get('token_status'): raise error.InputError(description="Token invalid") user_id = helper_functions.check_token(token).get("u_id") #Checked invalid channel if helper_functions.check_channelid_valid(channel_id): raise error.InputError("Channel not valid") # channel is private channel_is_public = False for chan_status in channels.channel_data: if chan_status['channel_id'] == channel_id: if chan_status['is_public'] == True: channel_is_public = True break if channel_is_public == False: raise error.AccessError("Channel is private") #If flock owner add to owner for user in auth.registered_users: if user.get('u_id') == user_id: if user.get('permissions') == 1: #Append u_id to list of owners for user in channels.channel_data: if user.get("channel_id") == channel_id: user.get("owner_ids").append(user_id) user.get("member_ids").append(user_id) return {} for current_channel in channels.channel_data: if channel_id == current_channel['channel_id']: current_channel["member_ids"].append(user_id) return {}
def user_profile_setemail(token, email): ''' Updates the authorised user's email. ''' #Use regex to check if email is valid regex = r'^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$' # Checking is the token exist then getting their u_id if helper_functions.check_token(token).get('token_status'): raise error.AccessError(description="Token invalid") u_id = helper_functions.check_token(token).get('u_id') # Testing for an invalid email invalid_email = False for user in auth.registered_tokens: if user.get("email") == email: invalid_email = True break if invalid_email: raise error.InputError("You have entered an invalid email") #If email is invalid if not re.search(regex, str(email)): raise error.InputError(description="Email Invalid") # Testing for email that is already being used for data in auth.registered_users: if data.get('email') == email: raise error.InputError("Email is already being used") # Updating the user's email for users in auth.registered_users: if users.get('u_id') == u_id: users['email'] = email break return {}
def message_edit(token, message_id, message): ''' Given a message id and user token edit the message. ''' #Token invalid check if helper_functions.check_token(token).get('token_status'): raise error.AccessError(description="Token invalid") user_id = helper_functions.check_token(token).get('u_id') # Error message too long if len(message) > 1000: raise error.InputError("You have message longer than 1000 words") invalid_messages_id = True # Testing if the message id is valid for data in channels.channel_data: for msg in data['messages']: if msg.get('message_id') == message_id: channel_id = data.get("channel_id") invalid_messages_id = False # Raising the error if invalid_messages_id: raise error.InputError("You have entered an invalid message id") # Editing the message for data in channels.channel_data: for message_data in data.get('messages'): if message_data.get("message_id") == message_id: #check if owner if helper_functions.check_uid_owner_in_channel(user_id, channel_id): if message_data.get('user_id') != user_id: raise error.AccessError("Not an owner or not user who sent msg") message_data['message_sent'] = message break return {}
def message_remove(token, message_id): ''' Removing a message that is requested by the user. ''' #If token invalid if helper_functions.check_token(token).get('token_status'): raise error.AccessError(description="Token invalid") user_id = helper_functions.check_token(token).get('u_id') # Checking if message ID is valid invalid_m_id = True for data in channels.channel_data: for msg in data['messages']: if msg.get('message_id') == message_id: channel_id = data.get('channel_id') invalid_m_id = False break if invalid_m_id: raise error.InputError("You have entered an invalid message ID") #Check if message is from the authorised user # Go inside message to check if message_id is the same in order to remove the message # Return error if message no longer existing for data in channels.channel_data: for message_data in data.get('messages'): if message_data.get('message_id') == message_id: #check if owner if helper_functions.check_uid_owner_in_channel(user_id, channel_id): if message_data.get('user_id') != user_id: raise error.AccessError("Not an owner or not user who sent msg") if msg['message_sent'] == None: raise error.InputError("Message no longer exists") else: data.get('messages').remove(message_data) break return {}
def message_sendlater(token, channel_id, message, time_sent): ''' Given a message and user token send the message later. ''' #Token invalid test if helper_functions.check_token(token).get('token_status'): raise error.AccessError(description="Token invalid") user_id = helper_functions.check_token(token).get('u_id') if len(message) > 1000: raise error.InputError("You have message longer than 1000 words") if message == '': raise error.InputError("no messages") invalid_channel_id = True not_in_channel = True # Checking is the token exist then getting their u_id for channel_datas in channels.channel_data: if channel_datas.get("channel_id") == channel_id: invalid_channel_id = False if user_id in channel_datas['member_ids']: not_in_channel = False if invalid_channel_id: raise error.InputError("You have entered an invalid channel id.") if not_in_channel: raise error.AccessError("You are not in this channel.") time_create_date = datetime.now().replace(microsecond=0) time_create = time_create_date.timestamp() if time_sent < time_create: raise error.InputError("Can not send message to that time.") time_to_be_sent = time_sent - time_create time.sleep(time_to_be_sent) return message_send(token, channel_id, message)
def channels_list(token): ''' Provides a list of all the channels and their details that the authorised user is part of. ''' # Checking is the token exist then getting their u_id if helper_functions.check_token(token).get('token_status'): raise error.InputError(description="Token invalid") user_id = helper_functions.check_token(token).get('u_id') # List of channels that the user belongs to user_channel = [] # Checking if the user is in the channel then append that channel to the user channel list for channel in list_of_all_channels: channel_id = channel.get("channel_id") for data in channel_data: if data['channel_id'] == channel_id: if user_id in data['member_ids']: user_channel.append(channel) return {'channels': user_channel}
def user_profile_uploadphoto(token, img_url, x_start, y_start, x_end, y_end): ''' Update the authorised user's profile. ''' # Checking is the token exist then getting their u_id if helper_functions.check_token(token).get('token_status'): raise error.AccessError(description="Token invalid") u_id = helper_functions.check_token(token).get('u_id') try: urllib.request.urlretrieve(img_url, f"src/static/{u_id}.jpg") except: raise error.InputError(description="Url can not be opened") opened_img = Image.open(f"src/static/{u_id}.jpg") if opened_img.format != "JPEG": raise error.InputError(description="The image is not JPEG type") width, height = opened_img.size if x_start < 0 or y_start < 0 or x_end > width or y_end > height: raise error.InputError( description="The crop bound is greater than the orginal photo.") new_image = opened_img.crop((x_start, y_start, x_end, y_end)) new_image.save(f"src/static/{u_id}.jpg") profile_img_url = url_for('static', filename=f'{u_id}.jpg', _external=True) for users in auth.registered_users: if users['u_id'] == u_id: users['profile_img_url'] = profile_img_url break return {}
def channel_removeowner(token, channel_id, u_id): ''' When a user is removed as owner from the channel. ''' #Checked invalid channel if helper_functions.check_channelid_valid(channel_id): raise error.InputError("Channel not valid") # user token is invalid if helper_functions.check_token(token).get('token_status'): raise error.InputError(description="Token invalid") u_id_for_token = helper_functions.check_token(token).get("u_id") #If user is an owner if helper_functions.check_uid_owner_in_channel(u_id_for_token, channel_id): raise error.AccessError( "Authorised user not in channel or flockr owner") #Check if u_id is an owner. If so then remove u_id_not_owner = True for user in channels.channel_data: if user.get('channel_id') == channel_id: #check if u_id is owner for owner in user.get('owner_ids'): if owner == u_id: u_id_not_owner = False user['owner_ids'].remove(owner) break if u_id_not_owner == False: break if u_id_not_owner == True: raise error.InputError("U_id not an owner") return {}
def auth_login(email, password): ''' Check if input matches with data ''' #If email is invalid if not re.search(REGEX, email): raise error.InputError(description="Email Invalid") #Email does not belong to a user user_not_found = True for user in registered_users: if user.get('email') == email: u_id = user.get("u_id") user_not_found = False break if user_not_found: raise error.InputError(description="Email not registered") #Incorrect password booleon incorrect_password = True #Check if encrypted passwords are the same encrypted_password = hashlib.sha256(password.encode()).hexdigest() for user in registered_users: if user.get('email') == email: if user.get('password') == encrypted_password: incorrect_password = False if incorrect_password: raise error.InputError(description="Password is invalid") #Token will be hashed u_id token = str(jwt.encode({'u_id': u_id}, SECRET, algorithm='HS256')) verification = { 'u_id': u_id, 'token': token, } #See if user is already logged in if helper_functions.check_token(token).get('token_status') == False: raise error.InputError(description="User already logged in") #Append to registered_tokens registered_tokens.append(verification) return verification
def users_all(token): ''' Returns a list of all users and their associated details ''' # Checking is the token exist then getting their u_id if helper_functions.check_token(token).get('token_status'): raise error.AccessError(description="Token invalid") # Returning all users and their details all_user_detail = [] for users in auth.registered_users: user_detail = { 'u_id': users.get('u_id'), 'email': users.get('email'), 'name_first': users.get('first_name'), 'name_last': users.get('last_name'), 'handle_str': users.get('handle'), 'profile_img_url': users.get('profile_img_url'), } all_user_detail.append(user_detail) return {'users': all_user_detail}
def channels_listall(token): ''' Provides a list of all the channels and their details. ''' #Checking is the token exist then getting their u_id if helper_functions.check_token(token).get('token_status'): raise error.InputError(description="Token invalid") # Returning the list of channels # output = {'channels': []} # for channels_d in channel_data: # if channels_d['is_public'] == True: # detail = { # 'channel_id': channels_d['channel_id'], # 'name': channels_d['name'] # } # output['channels'].append(detail) return {'channels': list_of_all_channels}
def standup_active(token, channel_id): ''' Check if standup is active ''' # Checking is the token exist if helper_functions.check_token(token).get('token_status'): raise error.AccessError(description="Token invalid") #If channel valid if helper_functions.check_channelid_valid(channel_id): raise error.InputError(description="Channel_id invalid") #Check if standup active in channel is_active = False time_finish = None for channel in STANDUPS: if channel['channel_id'] == channel_id: is_active = True time_finish = channel['time_finish'] return {'is_active': is_active, 'time_finish': time_finish}
def user_profile(token, u_id): ''' Returns profile details for a valid user. ''' # Test for an invalid token if helper_functions.check_token(token).get('token_status'): raise error.AccessError(description="Token invalid") # Testing for an invalid u_id if helper_functions.check_uid_valid(u_id): raise error.InputError("You have entered an invalid user id") # Getting all the user details for users in auth.registered_users: if users['u_id'] == u_id and users.get('profile_img_url') == None: user_detail = { 'user': { 'u_id': users.get('u_id'), 'email': users.get('email'), 'name_first': users.get('first_name'), 'name_last': users.get('last_name'), 'handle_str': users.get('handle'), } } if users['u_id'] == u_id and users.get('profile_img_url') != None: user_detail = { 'user': { 'u_id': users.get('u_id'), 'email': users.get('email'), 'name_first': users.get('first_name'), 'name_last': users.get('last_name'), 'handle_str': users.get('handle'), 'profile_img_url': users.get('profile_img_url') } } break return user_detail
def channel_messages(token, channel_id, start): ''' Return up to 50 messages. ''' #Checked invalid channel if helper_functions.check_channelid_valid(channel_id): raise error.InputError("Channel not valid") # user token is invalid if helper_functions.check_token(token).get('token_status'): raise error.InputError(description="Token invalid") u_id = helper_functions.check_token(token).get("u_id") #User already in channel if helper_functions.check_u_id_in_channel(u_id, channel_id): raise error.AccessError("Not in channel") #return messages for channel in channels.channel_data: if channel.get("channel_id") == channel_id: messages = channel.get("messages") messages = list(reversed(messages)) num_messages = len(messages) if num_messages == 0 and start == 0: return {"messages": [], "start": start, "end": -1} #If start is larger than number of items in messages if start >= num_messages: raise error.InputError("Start value older than latest message") #Append message to a list index_message = start end = int(index_message) + 50 counter = 0 return_messages = [] while counter < 50: get_index = start + counter if get_index >= end or get_index >= num_messages: break #Seeing if user has reacted is_this_user_reacted = False if u_id in messages[get_index].get('reacts')[0]['u_ids']: is_this_user_reacted = True newmsg = { 'message_id': messages[get_index].get('message_id'), 'u_id': messages[get_index].get('user_id'), 'message': messages[get_index].get('message_sent'), 'time_created': messages[get_index].get('time_created'), 'reacts': [{ 'react_id': messages[get_index].get('reacts')[0].get('react_id'), 'u_ids': messages[get_index].get('reacts')[0].get('u_ids'), 'is_this_user_reacted': is_this_user_reacted, }], 'is_pinned': messages[get_index].get('is_pinned') } return_messages.append(newmsg) counter += 1 if counter < 50: end = -1 return {"messages": return_messages, "start": start, "end": end}
def message_send(token, channel_id, message): ''' Grabbing a message and sending it. ''' if len(message) > 1000: raise error.InputError("Messages can't have more than 1000 characters") #Check token valid if helper_functions.check_token(token).get('token_status'): raise error.AccessError(description="Token invalid") user_id = helper_functions.check_token(token).get('u_id') invalid_channel_id = True not_in_channel = True # Checking is the token exist then getting their u_id for channel_datas in channels.channel_data: if channel_datas.get("channel_id") == channel_id: invalid_channel_id = False if user_id in channel_datas['member_ids']: not_in_channel = False # If token is invalid return an error if invalid_channel_id: raise error.AccessError("You have entered an invalid channel id.") if not_in_channel: raise error.AccessError("You are not in this channel.") time_create_date = datetime.now().replace(microsecond=0) time_create = time_create_date.timestamp() # Generate message ID gen_id = len(message_ids) + 1 message_id = { 'message_id': gen_id, } react_info = [{ 'react_id': 1, 'u_ids': [], }] msg = { 'message_sent': message, 'message_id': gen_id, 'user_id': user_id, 'time_created': time_create, 'reacts': react_info, 'is_pinned': False } message_ids.append(message_id) for channels_data in channels.channel_data: if channels_data.get('channel_id') == channel_id: channels_data['messages'].append(msg) # Hang man option if "/hangman" in message: hangman.start_hangman(token, channel_id) if "/guess" in message: hangman.hangman_guess(token, channel_id, message[7]) if "/stop" in message: hangman.hangman_stop(token, channel_id) if "/reveal" == message: hangman.hangman_reveal(token, channel_id) return message_id