def message_pin(token, message_id): # 1. authorise user should be in the channel # 2. the message should be valid # 3. the message should be unpinned check_tokenlogin(token) auser_dic = get_user_from_token(token) auid = auser_dic['u_id'] m_c_dic = get_message_from_mid(message_id) check_validmessage(m_c_dic) message_dic = m_c_dic['message_dic'] channel_dic = m_c_dic['channel_dic'] #check if the authorise user is in the channel check_auser_not_in_channel(auid, channel_dic['all_members']) check_user_admin(auser_dic) # cannot pin the message which is pinned if message_dic['is_pinned']: raise ValueError("Message with ID message_id is already pinned.") message_dic['is_pinned'] = True return {}
def message_edit(token, message_id, message): check_tokenlogin(token) auser_dic = get_user_from_token(token) # only the user who sent this message can edit the message check_message_length(message) m_c_dic = get_message_from_mid(message_id) check_validmessage(m_c_dic) message_dic = m_c_dic['message_dic'] channel_dic = m_c_dic['channel_dic'] if not helper_check_user_admin(auser_dic): check_permission_of_message(message_dic, channel_dic, auser_dic['u_id']) # edit the message # if the message is empty, delete this message if message == '': channel_dic['messages'].remove(message_dic) return {} message_dic['message'] = message return {}
def user_profiles_uploadphoto(token, img_url, x_start, y_start, x_end, y_end, port): auser_dic = get_user_from_token(token) if not helper_check_HTTPstatus(img_url): raise ValueError("img_url is returns an HTTP status other than 200.") # check whether exists this folder path = "static/images/" # if not exists path, then mkdir if not os.path.isdir(path): os.mkdir(path) filename = str(auser_dic['handle'] + ".jpg") urllib.request.urlretrieve(img_url, path+filename) if img_url.rsplit('.', 1)[1].lower() != "jpg" and img_url.rsplit('.', 1)[1].lower() != "jpeg": # the photo uploaded should be jpg raise ValueError("Image uploaded is not a JPG.") image = Image.open(path+filename) if not helper_check_validdimensions(path+filename, x_start, y_start, x_end, y_end): # x_start, y_start, x_end, y_end should be within the dimensions of the image raise ValueError("any of x_start, y_start, x_end, y_end are not within the dimensions of the image at the URL.") cropped = image.crop((x_start, y_start, x_end, y_end)) cropped.save(path+filename) auser_dic['profile_img_url'] = "http://localhost:" + str(port) + "/static/images/" + filename # update the user profile print(path + filename) return {}
def channels_create(token, name, is_public): check_tokenlogin(token) data = getdata() auser_dic = get_user_from_token(token) auid = auser_dic['u_id'] # check if the channel name valid if len(name) > 20: raise ValueError('channel name not valid') channel_id = -1 for channel in data['channels']: channel_id = channel['channel_id'] # channel id will be automaticlly updated with every creation channel_id = channel_id + 1 channel_dic = { 'channel_id': channel_id, 'name': name, 'is_public': is_public, 'standup_messages': '', 'standup_is_active': False, 'standup_time_finish': None, 'owner_members': [auid], 'all_members': [auid], # the one who created this channel will be the owner of the channel 'messages': [] } # channel will be added into the channels list after creation data['channels'].append(channel_dic) return {'channel_id': channel_id}
def user_profile_sethandle(token, handle_str): check_tokenlogin(token) auser_dic = get_user_from_token(token) check_validhandle(handle_str) # the handle should be valid if not handle_is_unique(handle_str): # this handle has been used raise ValueError('handle is already used by another user') auser_dic['handle'] = handle_str return {}
def user_profile_setname(token, name_first, name_last): check_tokenlogin(token) # check the validation of the token auser_dic = get_user_from_token(token) # check if the name is valid check_validname(name_first, name_last) auser_dic['name_first'] = name_first auser_dic['name_last'] = name_last return {}
def search(token, query_str): check_tokenlogin(token) channels = getdata()['channels'] auser_dic = get_user_from_token(token) auid = auser_dic['u_id'] collection = [] for channel in channels: # firstly search fot all the channels the user is in if auid in channel['all_members']: for message in channel['messages']: #then search for all the messages this user sent in a channel if query_str in message['message']: # lastly append the message to the collection collection.append(message) return {'messages': collection}
def message_send(token, channel_id, message): # only the user who is in the channel can send message check_tokenlogin(token) channel_dic = get_channel_from_cid(channel_id) auser_dic = get_user_from_token(token) auid = auser_dic['u_id'] check_validchannel(channel_dic) check_auser_not_in_channel(auid, channel_dic['all_members']) check_message_length(message) c_messages = channel_dic['messages'] m_id = helper_send_message(c_messages, message, auid) # append the m_id into the message list after sending return {'message_id': m_id}
def standup_active(token, channel_id): # 1. the channel should be valid # 2. authorised user should be in the channel auser_dic = get_user_from_token(token) auid = auser_dic['u_id'] channel_dic = get_channel_from_cid(channel_id) check_validchannel(channel_dic) check_auser_not_in_channel(auid, channel_dic['all_members']) if not channel_dic['standup_is_active']: return {'is_active': False, 'time_finish': None} return { 'is_active': True, 'time_finish': channel_dic['standup_time_finish'] }
def channels_list(token): check_tokenlogin(token) data = getdata() auser_dic = get_user_from_token(token) auid = auser_dic['u_id'] channels = [] # get the list of channels, which the token user in for channel in data['channels']: if auid in channel['all_members']: channels.append({ 'channel_id': channel['channel_id'], 'name': channel['name'] }) return {'channels': channels}
def channel_invite(token, channel_id, u_id): check_tokenlogin(token) channel_dic = get_channel_from_cid(channel_id) auser_dic = get_user_from_token(token) check_validchannel(channel_dic) members = channel_dic['all_members'] check_auser_not_in_channel(auser_dic['u_id'], members) uid_dic = get_user_from_uid(u_id) # add this user in the channel if this user is not a member of the channel check_validuid(uid_dic) if u_id in members: raise ValueError("the invited user already in channel") members.append(u_id) return {}
def message_remove(token, message_id): check_tokenlogin(token) auser_dic = get_user_from_token(token) m_c_dic = get_message_from_mid(message_id) check_validmessage(m_c_dic) message_dic = m_c_dic['message_dic'] channel_dic = m_c_dic['channel_dic'] if not helper_check_user_admin(auser_dic): check_permission_of_message(message_dic, channel_dic, auser_dic['u_id']) # remove the message channel_dic['messages'].remove(message_dic) return {}
def channel_removeowner(token, channel_id, u_id): check_tokenlogin(token) channel_dic = get_channel_from_cid(channel_id) auser_dic = get_user_from_token(token) check_validchannel(channel_dic) check_auser_permission_of_channel(auser_dic, channel_dic) # only the owner can remove an owner, remove oneself is okay if u_id not in channel_dic['owner_members']: raise ValueError("The user with uid is not a owner of this channel") check_only_owner(channel_dic) # add uid to the owner of the channel channel_dic['owner_members'].remove(u_id) return {}
def message_react(token, message_id, react_id): # 1. authorise user should be in the channel # 2. the message should be valid # 3. the message should be unreacted check_tokenlogin(token) #get the information of the user auser_dic = get_user_from_token(token) auid = auser_dic['u_id'] m_c_dic = get_message_from_mid(message_id) check_validmessage(m_c_dic) # check if the message is valid then get the message message_dic = m_c_dic['message_dic'] channel_dic = m_c_dic['channel_dic'] check_uid_not_in_channel(auid, channel_dic) check_validreact(react_id) m_reacts = message_dic['reacts'] this_react = helper_check_react_exists(m_reacts, react_id) user_reacted = helper_is_the_user_reacted(message_dic, auid) # if there is no this react in the message # add a react if not this_react: m_reacts.append({ 'react_id': react_id, 'u_ids': [auid], 'is_this_user_reacted': user_reacted }) return {} # if there is this react in the message if auid in this_react['u_ids']: raise ValueError( "Message already contains an active React with react_id from user." ) this_react['u_ids'].append(auid) if not user_reacted: this_react['is_this_user_reacted'] = True return {}
def channel_messages(token, channel_id, start): check_tokenlogin(token) #set up the initial value max = 50 returned_least_recent = -1 channel_dic = get_channel_from_cid(channel_id) auser_dic = get_user_from_token(token) check_validchannel(channel_dic) check_auser_not_in_channel(auser_dic['u_id'], channel_dic['all_members']) # check if the start is valid total_messages = len(channel_dic['messages']) return_messages = [] if total_messages == 0: return {'messages': return_messages, 'start': start, 'end': -1} if start > total_messages: raise ValueError( 'Start is greater than the total number of messages in the channel' ) counter = start end = start + max # get the messages between start and end # if end > total, just get the messages between start and last message while counter < end and counter < total_messages: return_messages.append(channel_dic['messages'][counter]) counter = counter + 1 # if this function has returned the least recent messages in the channel # returns -1 in "end" to indicate there are no more messages to load after this return. if start == total_messages: end = returned_least_recent # else return the counter of last message in the list of returned messages else: end = counter return {'messages': return_messages, 'start': start, 'end': end}
def channel_addowner(token, channel_id, u_id): check_tokenlogin(token) channel_dic = get_channel_from_cid(channel_id) auser_dic = get_user_from_token(token) check_validchannel(channel_dic) check_auser_permission_of_channel(auser_dic, channel_dic) check_uid_not_in_channel(u_id, channel_dic) if u_id in channel_dic['owner_members']: raise ValueError( "The user with uid is already a owner of this channel") # add uid to the owner of the channel channel_dic['owner_members'].append(u_id) return {}
def user_profile_setemail(token, email): check_tokenlogin(token) users = getdata()['users'] auser_dic = get_user_from_token(token) check_validemail(email) # check if the email is valid for user in users: if user['email'] == email: raise ValueError("email has been used.") if auser_dic['email'] == email: raise ValueError('No new email entered') auser_dic['email'] = email return {}
def channel_join(token, channel_id): check_tokenlogin(token) channel_dic = get_channel_from_cid(channel_id) auser_dic = get_user_from_token(token) # check error check_validchannel(channel_dic) auid = auser_dic['u_id'] check_auser_in_channel(auid, channel_dic['all_members']) # when channel_id refers to a channel that is private # when the authorised user is not an admin if not channel_dic['is_public'] and not helper_check_user_admin(auser_dic): raise AccessError("channel is private") # add the auser to channel channel_dic['all_members'].append(auid) return {}
def channel_leave(token, channel_id): check_tokenlogin(token) channel_dic = get_channel_from_cid(channel_id) auser_dic = get_user_from_token(token) # check error check_validchannel(channel_dic) auser_uid = auser_dic['u_id'] channel_all_members = channel_dic['all_members'] check_auser_not_in_channel(auser_uid, channel_all_members) # the auser leave the channel channel_all_members.remove(auser_uid) owners = channel_dic['owner_members'] # if auser is a owner of channel, remove auser from owner list of this channel if auser_uid in owners: check_only_owner(channel_dic) owners.remove(auser_uid) return {}
def message_sendlater(token, channel_id, message, time_send): check_tokenlogin(token) channel_dic = get_channel_from_cid(channel_id) auser_dic = get_user_from_token(token) auid = auser_dic['u_id'] check_validchannel(channel_dic) check_auser_not_in_channel(auid, channel_dic['all_members']) check_message_length(message) second = (convert_date(time_send) - datetime.utcnow()).total_seconds() if second < 0: raise ValueError("Time sent is a time in the past.") timer = CustomTimer(second, helper_send_message, (channel_dic['messages'], message, auid)) # timer = threading.Timer(second, message_send(token, channel_id, message)) timer.start() m_id = timer.join() print(m_id) return {'message_id': m_id}
def standup_start(token, channel_id, length): # 1. the authorised user should be in this channel # 2. the channel should be valid # 3. there can be only one standup at the same time in one channel auser_dic = get_user_from_token(token) auid = auser_dic['u_id'] channel_dic = get_channel_from_cid(channel_id) check_validchannel(channel_dic) check_auser_not_in_channel(auid, channel_dic['all_members']) if channel_dic['standup_is_active']: raise ValueError( "An active standup is currently running in this channel") channel_dic['standup_is_active'] = True finish_time = int(datetime.datetime.now().strftime("%s")) + length channel_dic['standup_time_finish'] = finish_time timer = CustomTimer(length, helper_standup, (channel_dic, auid)) timer.start() return {'time_finish': finish_time}
def standup_send(token, channel_id, message): # 1. the channel should be valid # 2. authorised user should be in the channel # 3. the message should be valid auser_dic = get_user_from_token(token) auid = auser_dic['u_id'] auser_firstname = auser_dic['name_first'] channel_dic = get_channel_from_cid(channel_id) check_message_length(message) check_validchannel(channel_dic) check_auser_not_in_channel(auid, channel_dic['all_members']) if not channel_dic['standup_is_active']: raise ValueError( "An active standup is not currently running in this channel") channel_dic['standup_messages'] = str( channel_dic['standup_messages']) + str(auser_firstname) + " : " + str( message) + "\n" return {}
def admin_userpermission_change(token, u_id, permission_id): check_tokenlogin(token) permission_ids = get_permission_ids() auser_dic = get_user_from_token(token) user_dic = get_user_from_uid(u_id) check_validuid(user_dic) # 3 types of permissions: 1. owner 2. admin 3. member # only 1 and 2 can change permission owner_id = permission_ids['Owner'] admin_id = permission_ids['Admin'] member_id = permission_ids['Member'] if permission_id not in (owner_id, admin_id, member_id): raise ValueError("permission_id does not refer to a value permission") if not helper_check_user_admin(auser_dic): raise AccessError("the authorised user is not an admin or owner") user_dic['permission_id'] = permission_id return {}
def message_unreact(token, message_id, react_id): # 1. authorise user should be in the channel # 2. the message should be valid # 3. the message should be reacted check_tokenlogin(token) auser_dic = get_user_from_token(token) auid = auser_dic['u_id'] m_c_dic = get_message_from_mid(message_id) check_validmessage(m_c_dic) message_dic = m_c_dic['message_dic'] channel_dic = m_c_dic['channel_dic'] # when the user not in this channel check_uid_not_in_channel(auid, channel_dic) check_validreact(react_id) m_reacts = message_dic['reacts'] this_react = helper_check_react_exists(m_reacts, react_id) # when the user didn't react to this message if not this_react or auid not in this_react['u_ids']: raise ValueError( "Message with message_id does not contain an active React with react_id from user." ) user_reacted = helper_is_the_user_reacted(message_dic, auid) this_react['u_ids'].remove(auid) if not user_reacted: this_react['is_this_user_reacted'] = False return {}