def channel_addowner(token, channel_id, u_id): DATA = load() channelDict = DATA['channelDict'] if channel_id_check(channel_id) == False: raise ValueError("channel_id is invalid") # already an owner getUserFromToken(token) u_id = int(u_id) channel_id = int(channel_id) for parts in channelDict: if parts['channel_id'] == channel_id and u_id in parts['channel_owner']: raise ValueError("user is already an owner in the channel") if not if_User_Owner(token, channel_id): raise AccessError( "the authorised user is not an owner of the slackr, or an owner of this channel" ) if u_id not in channelDict[channel_id - 1]['channel_member']: raise AccessError("the user is not a member of this channel") for parts in channelDict: if parts['channel_id'] == channel_id: parts['channel_owner'].append(u_id) parts['channel_member'].remove(u_id) DATA['channelDict'] = channelDict save(DATA) return {}
def channels_listall(token): DATA = load() channelDict = DATA['channelDict'] getUserFromToken(token) channel = [] for cha in channelDict: dic = {'channel_id': int(cha['channel_id']), 'name': cha['name']} channel.append(dic) return {'channels': channel}
def message_react(token, message_id, react_id): u_id = getUserFromToken(token) u_id = int(u_id) message_id = int(message_id) react_id = int(react_id) DATA = load() messDict = DATA['messDict'] if react_id != 1: raise ValueError('React_id is not a valid React ID') is_mess = False for mess in messDict: if mess['message_id'] == message_id: channelID = mess['channel_id'] message = mess is_mess = True break if not is_mess: raise ValueError('invalid message_id') if not is_in_channel(u_id, channelID): raise ValueError( 'message_id is not a valid message within a channel that the authorised user has joined' ) if u_id in message['reacts'][0]['u_ids']: raise ValueError( f'Message with ID {message_id} already contains an active React with ID {react_id}' ) else: message['reacts'][0]['react_id'] = react_id message['reacts'][0]['u_ids'].append(u_id) DATA['messDict'] = messDict save(DATA) return {}
def message_pin(token, message_id): u_id = getUserFromToken(token) u_id = int(u_id) message_id = int(message_id) DATA = load() messDict = DATA['messDict'] found = False for mess in messDict: if mess['message_id'] == message_id: if mess['is_pinned']: raise ValueError('already pinned') channelID = mess['channel_id'] message = mess found = True break if not found: raise ValueError("Invalid message_id") if not if_User_Owner(token, channelID): raise ValueError('The authorised user is not an admin') if not is_in_channel(u_id, channelID): raise AccessError( 'message_id is not a valid message within a channel that the authorised user has joined' ) message['is_pinned'] = True DATA['messDict'] = messDict save(DATA) return {}
def channel_join(token, channel_id): DATA = load() channelDict = DATA['channelDict'] if channel_id_check(channel_id) == False: raise ValueError("channel_id is invalid") id = getUserFromToken(token) id = int(id) # private channel if channel_property_check(channel_id) == False: if channel_admin_check(token) == False: raise AccessError( "authorised user is not an admin when channel is private") else: for parts in channelDict: if parts['channel_id'] == int(channel_id): parts['channel_owner'].append(id) # public channel else: for parts in channelDict: if parts['channel_id'] == int(channel_id): if channel_admin_check(token) == False: if parts['channel_member'] == []: parts['channel_member'] = [id] else: parts['channel_member'].append(id) else: parts['channel_owner'].append(id) DATA['channelDict'] = channelDict save(DATA)
def admin_userpermission_change(token, u_id, permission_id): fl = 1 u_id = int(u_id) permission_id = int(permission_id) data = load() userDict = data['userDict'] for user in userDict: if u_id == user['u_id']: fl = 0 if fl == 1: raise ValueError('Wrong user id') if permission_id not in range(1, 4): raise ValueError('Unmatch permission id') opid = getUserFromToken(token) for user in userDict: if user['u_id'] == opid: if user['permission_id'] == 2 and permission_id == 1: raise AccessError( 'Permission Denied: Trying to give owner by admin') if user['permission_id'] == 3: raise AccessError('Permission Denied : member can not do this') for sb in userDict: if sb['u_id'] == u_id: if sb['permission_id'] == 1: raise AccessError( 'Permission Denied: Trying to change owner permission') sb['permission_id'] = permission_id data['userDict'] = userDict save(data) pass
def channels_messages(token, channel_id, start): u_id = getUserFromToken(token) u_id = int(u_id) DATA = load() messDict = DATA['messDict'] start = int(start) if channel_id_check(channel_id) == False: raise ValueError("channel_id is invalid") if auth_id_check(token, channel_id) == False: raise AccessError("Auth user is not a member of channnel") dic = {'messages': [], 'start': start, 'end': None} lis = [] for parts in messDict: if int(parts['channel_id']) == int(channel_id): lis.append(int(parts['message_id'])) if len(lis) != 1: lis = lis[::-1] lis = get_messages(u_id, lis) if len(lis) < int(start): raise ValueError( "start is greater than the total number of messages in the channel" ) if int(start) + 50 >= len(lis): for parts in lis[int(start):len(lis)]: dic['messages'].append(parts) dic['end'] = -1 else: for parts in lis[int(start):int(start) + 50]: dic['messages'].append(parts) dic['end'] = start + 50 return dic
def standup_send(token, channel_id, message): channel_id = int(channel_id) data = load() channelDict = data['channelDict'] userDict = data['userDict'] opid = getUserFromToken(token) for user in userDict: if int(user['u_id']) == int(opid): name = user['first_name'] break if len(message) > 1000: raise ValueError("Message too long") for ch in channelDict: if int(channel_id) == int(ch['channel_id']): if opid not in ch['channel_member'] and opid not in ch[ 'channel_owner']: raise AccessError('You are not a member of this channel') if ch['standUp'] != True: raise ValueError( 'An active standup is not currently running in this channel' ) append = str(name) + ': ' + str(message) if not ch['standlist']: ch['standlist'] = append data['channelDict'] = channelDict save(data) return {} else: ch['standlist'] += '\r\n' + append data['channelDict'] = channelDict save(data) return {} raise ValueError('Channel ID is not a valid channel')
def user_profile(token, u_id): getUserFromToken(token) DATA = load() userdict = DATA['userDict'] for user in userdict: if int(user['u_id']) == int(u_id): dic = { 'u_id': (user['u_id']), 'email': (user['email']), 'name_first': (user['first_name']), 'name_last': (user['last_name']), 'handle_str': (user['handle']), 'profile_img_url': (user['profile_img_url']) } return dic raise ValueError('u_id was incorrect')
def user_profiles_uploadphoto(token, img_url, x_start, y_start, x_end, y_end): response = requests.get(img_url) if response.status_code != 200: raise ValueError('url corrupted') img = Image.open(urllib. request. urlopen(img_url)) width, height = img.size if img == -1: raise ValueError("image does not exist") if x_end == x_start or y_end == y_start: raise ValueError("incorrect range") if int(x_end) > width or int(y_end) > height or int(x_start) > width or int(y_start) > height: raise ValueError('Out of bound') if int(x_end) < 0 or int(y_end) < 0 or int(x_start) < 0 or int(y_start) < 0: raise ValueError('Out of bound') if img.format != "JPEG" and img.format != "JPG": raise ValueError("Image uploaded is not a JPG") cropped = img.crop((int(x_start), int(y_start), int(x_end), int(y_end))) uid = getUserFromToken(token) cropped = cropped.save('frontend/prebundle/profile_image/' + str(uid) + '.jpg') DATA = load() userDict = DATA['userDict'] # port = request.host # port = request.url_root for user in userDict: if int(uid) == int(user['u_id']): # user['profile_img_url'] = str(port) + "frontend/prebundle/static/" + str(id) + '.jpg' user['profile_img_url'] = 'frontend/prebundle/profile_image/' + str(uid) + '.jpg' print(user['profile_img_url']) DATA['userDict'] = userDict save(DATA) return {}
def message_send(token, channel_id, message): u_id = getUserFromToken(token) DATA = load() messDict = DATA['messDict'] channelDict = DATA['channelDict'] if len(message) > 1000: raise ValueError("Message is more than 1000 characters") for cha in channelDict: if cha['channel_id'] == channel_id: if u_id not in cha['channel_owner'] and u_id not in cha[ 'channel_member']: raise AccessError( "The authorised user has not joined the channel they are trying to post to" ) DATA['messID'] += 1 mess = { 'channel_id': int(channel_id), 'message_id': int(DATA['messID']), 'u_id': int(u_id), 'message': message, 'time_created': int(datetime.now().timestamp()), 'reacts': [{ 'react_id': None, 'u_ids': [], 'is_this_user_reacted': False }], 'is_pinned': False } mess_id = int(mess['message_id']) messDict.append(mess) DATA['messDict'] = messDict save(DATA) return int(mess_id)
def channel_admin_check(token): DATA = load() userDict = DATA['userDict'] id = getUserFromToken(token) # if the user is slacker owner or admin for parts in userDict: if (parts['u_id'] == int(id) and (parts['permission_id'] == 1 or parts['permission_id'] == 2)): return True return False
def if_slackr_owner(token): DATA = load() userDict = DATA['userDict'] # get the user id from token u_id = getUserFromToken(token) # slacker owner or admin for parts in userDict: if (parts['u_id'] == u_id and (parts['permission_id'] == 1 or parts['permission_id'] == 2)): return True return False
def users_all(token): getUserFromToken(token) data = load() userDict = data['userDict'] lis = [] for user in userDict: dic = { 'u_id': user['u_id'], 'email': (user['email']), 'name_first': (user['first_name']), 'name_last': (user['last_name']), 'handle_str': (user['handle']), 'profile_img_url': user['profile_img_url'] } lis.append(dic) return { 'users': list(lis) }
def channels_list(token): DATA = load() channelDict = DATA['channelDict'] lis = [] uid = getUserFromToken(token) for parts in channelDict: if (uid in parts['channel_member'] or uid in parts['channel_owner']): lis.append(parts['channel_id']) lis = get_channels(lis) return {'channels': lis}
def is_sender(token, message_id): u_id = getUserFromToken(token) u_id = int(u_id) message_id = int(message_id) data = load() messDict = data['messDict'] for mess in messDict: if mess['message_id'] == message_id: if mess['u_id'] == u_id: return True return False
def auth_id_check(token, channel_id): DATA = load() channelDict = DATA['channelDict'] userDict = DATA['userDict'] # find the channel's member and owner uid = getUserFromToken(token) # if the user is slacker owner or admin for parts in userDict: if (int(parts['u_id']) == int(uid) and (int(parts['permission_id']) == 1 or int(parts['permission_id']) == 2)): return True for elements in channelDict: if int(elements['channel_id']) == int(channel_id): return bool(int(uid) in elements['channel_member'] or int(uid) in elements['channel_owner']) return False
def channel_leave(token, channel_id): id = getUserFromToken(token) id = int(id) channel_id = int(channel_id) if channel_id_check(channel_id) == False: raise ValueError("channel_id is invalid") if not is_in_channel(id, channel_id): raise ValueError("user is not a member of channel") DATA = load() channelDict = DATA['channelDict'] for cha in channelDict: if cha['channel_id'] == channel_id: if id in cha['channel_owner']: cha['channel_owner'].remove(id) else: cha['channel_member'].remove(id) DATA['channelDict'] = channelDict save(DATA)
def if_User_Owner(token, channel_id): channel_id = int(channel_id) DATA = load() channelDict = DATA['channelDict'] userDict = DATA['userDict'] # get the user id from token id = getUserFromToken(token) id = int(id) # slacker owner or admin for parts in userDict: if (parts['u_id'] == id and (parts['permission_id'] == 1 or parts['permission_id'] == 2)): return True # find the channel and serach the owner for elements in channelDict: if elements['channel_id'] == channel_id: if id in elements['channel_owner']: return True return False
def user_profile_sethandle(token, handle_str): opid = getUserFromToken(token) DATA = load() userDict = DATA['userDict'] if len(handle_str) <= 3: raise ValueError('handle too short') if len(handle_str) >= 20: raise ValueError('handle too long') for user in userDict: if user['handle'] == handle_str: raise ValueError('handle already used') for user in userDict: if opid == user['u_id']: user['handle'] = handle_str DATA['userDict'] = userDict save(DATA) return
def channels_create(token, name, is_public): DATA = load() channelDict = DATA['channelDict'] if len(name) > 20: raise ValueError("Name is more than 20 characters long") user_id = getUserFromToken(token) if channelDict == []: dic = { 'channel_id': 1, 'name': name, 'channel_creater': int(user_id), 'channel_member': [], 'channel_owner': [int(user_id)], 'is_public': is_public, 'standUp': False, 'standlist': '', 'standtime': None } channelDict.append(dic) DATA['channelDict'] = channelDict save(DATA) return dic['channel_id'] else: # if same name for parts in channelDict: if parts['name'] == name: raise ValueError("this name was already used") count = len(channelDict) + 1 dic = { 'channel_id': int(count), 'name': name, 'channel_creater': int(user_id), 'channel_member': [], 'channel_owner': [int(user_id)], 'is_public': is_public, 'standUp': False, 'standlist': '', 'standtime': None } channelDict.append(dic) DATA['channelDict'] = channelDict save(DATA) return int(dic['channel_id'])
def user_profile_setmail(token, email): opid = getUserFromToken(token) DATA = load() userDict = DATA['userDict'] regex = '^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$' if re.search(regex, email): pass else: raise ValueError("Invalid Email") for user in userDict: if user['email'] == email: raise ValueError("Email address is already used bt another user.") for user in userDict: if opid == user['u_id']: user['email'] = email DATA['userDict'] = userDict save(DATA) return
def user_profile_setname(token, name_first, name_last): opid = getUserFromToken(token) DATA = load() userDict = DATA['userDict'] if len(name_first) > 50: raise ValueError('First name too long') if len(name_last) > 50: raise ValueError('Last name too long') if len(name_first) < 1: raise ValueError('First name too short') if len(name_last) < 1: raise ValueError('Last name too short') for user in userDict: if opid == user['u_id']: user['first_name'] = name_first user['last_name'] = name_last DATA['userDict'] = userDict save(DATA) return
def message_edit(token, message_id, message): u_id = getUserFromToken(token) u_id = int(u_id) message_id = int(message_id) DATA = load() messDict = DATA['messDict'] for mess in messDict: if mess['message_id'] == message_id: channelID = mess['channel_id'] mess_dict = mess break if not is_owner(token, channelID) and not is_sender(token, message_id): raise AccessError('Unauthorised edit !') if len(str(message)) == 0: DATA['messDict'].remove(mess_dict) else: mess_dict['message'] = message DATA['messDict'] = messDict save(DATA) return {}
def message_unpin(token, message_id): u_id = getUserFromToken(token) u_id = int(u_id) message_id = int(message_id) DATA = load() messDict = DATA['messDict'] channelDict = DATA['channelDict'] found = False for mess in messDict: if mess['message_id'] == message_id: if not mess['is_pinned']: raise ValueError('already unpinned') channelID = mess['channel_id'] message = mess found = True break if not found: raise ValueError("Invalid message_id") if not if_User_Owner(token, channelID): raise ValueError('The authorised user is not an admin') for chan in channelDict: if chan['channel_id'] == channelID: if int(u_id) in chan['channel_member']: pass elif u_id in chan['channel_owner']: pass else: raise AccessError( 'message_id is not a valid message within a channel that the authorised user has joined' ) message['is_pinned'] = False DATA['messDict'] = messDict DATA['channelDict'] = channelDict save(DATA) return {}
def message_unreact(token, message_id, react_id): u_id = getUserFromToken(token) u_id = int(u_id) message_id = int(message_id) react_id = int(react_id) DATA = load() messDict = DATA['messDict'] if react_id != 1: raise ValueError('React_id is not a valid React ID') is_mess = False for mess in messDict: if mess['message_id'] == message_id: if mess['reacts'][0]['react_id'] == None: raise ValueError( 'Message with ID message_id does not contain an active React' ) if mess['reacts'][0]['react_id'] != react_id: raise ValueError('React_id is not a valid React ID') channelID = mess['channel_id'] message = mess is_mess = True break if not is_mess: raise ValueError('invalid message_id') if not is_in_channel(u_id, channelID): raise ValueError( 'message_id is not a valid message within a channel that the authorised user has joined' ) if u_id in message['reacts'][0]['u_ids']: message['reacts'][0]['u_ids'].remove(u_id) if len(message['reacts'][0]['u_ids']) == 0: message['reacts'][0]['react_id'] = None DATA['messDict'] = messDict save(DATA) return {}
def standup_start(token, channel_id, length): channel_id = int(channel_id) data = load() channelDict = data['channelDict'] opid = getUserFromToken(token) for ch in channelDict: if int(channel_id) == ch['channel_id']: if opid not in ch['channel_member'] and opid not in ch[ 'channel_owner']: raise AccessError('You are not a member of this channel') if ch['standUp'] == True: raise ValueError('this channel is already in standup') ch['standUp'] = True ch['standtime'] = showtime(length) time = ch['standtime'] data['channelDict'] = channelDict save(data) timer = threading.Timer(int(length), send, [channel_id, token]) timer.start() return {'time_finish': time} raise ValueError('incorrect channel id')