def user_profile_setname_v1(token, name_first, name_last): if check_token_valid(token) is False: raise AccessError("Invalid Token") # checks if first and last names are between 1-50 characters long if len(name_first) < 1 or len(name_first) > 50: raise InputError("Name must be between 1-50 characters") if len(name_last) < 1 or len(name_last) > 50: raise InputError("Name must be between 1-50 characters") with open('src/data.json', 'r') as FILE: data = json.load(FILE) for i in range(len(data['users'])): if data['users'][i]['token'] == token: data['users'][i]['name_first'] = name_first data['users'][i]['name_last'] = name_last with open('src/data.json', 'w') as FILE: json.dump(data,FILE, indent = 4) return { }
def user_profile_sethandle_v1(token, handle_str): # token check if check_token_valid(token) is False: raise AccessError("Invalid Token") handle = ''.join(handle_str.split()) handle = handle.replace("@","") handle = handle.lower() with open('src/data.json', 'r') as FILE: data = json.load(FILE) for i in range(len(data['users'])): if data['users'][i]['handle'] == handle: raise InputError("Handle already in use") for j in range(len(data['users'])): if data['users'][j]['token'] == token: if len(handle) < 3 or len(handle) > 20: raise InputError("Invalid handle") data['users'][j]['handle'] = handle with open('src/data.json', 'w') as FILE: json.dump(data,FILE, indent = 4) return { }
def admin_userpermission_change_v1(token, u_id, permission_id): # token check if check_token_valid(token) is False: raise AccessError("Invalid Token") if permission_id is not 1 and permission_id is not 2: raise InputError("Invalid permission_id") if not check_user_exists(u_id): raise InputError("invalid u_id") with open('src/data.json', 'r') as FILE: data = json.load(FILE) owner = False owner_id = convert_token(token) if data['users'][owner_id]['permission_id'] == 1: owner = True if owner == True: for i in range(len(data['users'])): if u_id == data['users'][i]['u_id']: data['users'][i]['permission_id'] = permission_id else: raise AccessError("Unauthorised user, user is not an owner") with open ('src/data.json', 'w') as FILE: json.dump(data, FILE, indent = 4) return { }
def notifications_get_v1(token): ''' channel invite, channel addowner, channel removeowner message send, message share dm create, dm invite, senddm, admins ''' # token check if check_token_valid(token) is False: raise AccessError("Invalid Token") with open ("src/data.json") as FILE: data = json.load(FILE) user_id = convert_token(token) notifications = [] count = 0 for i in range(len(data["notifications"])): if count < 20: if data["notifications"][i]["u_id"] == user_id: notification = { "channel_id": data["notifications"][i]["channel_id"], "dm_id": data["notifications"][i]["dm_id"], "notification_message": data["notifications"][i]["notification_message"] } notifications.insert(0,notification) else: break count += 1 return { "notifications": notifications }
def search_v2(token, query_str): # token check if check_token_valid(token) is False: raise AccessError("Invalid Token") messages = [] # append to list all messages that match query string from # channels/DM that user has joined with open ("src/data.json") as FILE: data = json.load(FILE) u_id = convert_token(token) for i in range(len(data['messages'])): dm_id = data['messages'][i]['dm_id'] channel_id = data['messages'][i]['channel_id'] if dm_id == -1: if check_user_in_channel(u_id, channel_id): if query_str.lower() in data['messages'][i]['message'].lower(): messages.append(data['messages'][i]) else: if check_user_in_dm(u_id, dm_id): if query_str.lower() in data['messages'][i]['message'].lower(): messages.append(data['messages'][i]) print(messages) return { 'messages': messages }
def users_stats_v1(token): # token check if check_token_valid(token) is False: raise AccessError("Invalid Token") with open('src/data.json', 'r') as FILE: data = json.load(FILE) return { "dreams_stats": data["dreams_stats"] }
def user_stats_v1(token): # token check if check_token_valid(token) is False: raise AccessError("Invalid Token") with open('src/data.json', 'r') as FILE: data = json.load(FILE) u_id = convert_token(token) return { "user_stats": data["user_stats"][u_id]['stats'] }
def admin_user_remove_v1(token, u_id): # token check if check_token_valid(token) is False: raise AccessError("Invalid Token") with open ("src/data.json") as FILE: data = json.load(FILE) if not check_user_exists(u_id): raise InputError("invalid u_id") owner_count = 0 for i in range(len(data['users'])): if data['users'][i]['permission_id'] == 1: owner_count += 1 if owner_count == 1: raise InputError("user is the only owner") with open('src/data.json', 'r') as FILE: data = json.load(FILE) owner = False owner_id = convert_token(token) if data['users'][owner_id]['permission_id'] == 1: owner = True if owner == True: for i in range(len(data['users'])): if data['users'][i]['u_id'] == u_id: data['users'][i]['name_first'] = "Removed user" data['users'][i]['name_last'] = "Removed user" for i in range(len(data['messages'])): if data['messages'][i]['u_id'] == u_id: data['messages'][i]['message'] = "Removed user" else: raise AccessError("Unauthorised user, user is not an owner") with open ('src/data.json', 'w') as FILE: json.dump(data, FILE, indent = 4) return {}
def user_profile_v1(token, u_id): with open('src/data.json', 'r') as FILE: data = json.load(FILE) if check_token_valid(token) is False: raise AccessError("Invalid Token") if check_user_exists(u_id) is False: raise InputError("Invalid user") return { 'user': { 'u_id': u_id, 'email': data['users'][u_id]['email'], 'name_first': data['users'][u_id]['name_first'], 'name_last': data['users'][u_id]['name_last'], 'handle_str': data['users'][u_id]['handle'], }, }
def user_profile_setemail_v1(token, email): # token check if check_token_valid(token) is False: raise AccessError("Invalid Token") email_syntax = re.compile('^[a-zA-Z0-9]+[\\._]?[a-zA-Z0-9]+[@]\\w+[.]\\w{2,3}$') if not email_syntax.match(email): raise InputError("Invalid email") with open('src/data.json', 'r') as FILE: data = json.load(FILE) for i in range(len(data['users'])): if data['users'][i]['token'] == token: data['users'][i]['email'] = email with open('src/data.json', 'w') as FILE: json.dump(data,FILE, indent = 4) return { }
def users_all_v1(token): # token check if check_token_valid(token) is False: raise AccessError("Invalid Token") with open('src/data.json', 'r') as FILE: data = json.load(FILE) users = [] for i in range(len(data['users'])): user = { 'u_id': data['users'][i]['u_id'], 'email': data['users'][i]['email'], 'name_first': data['users'][i]['name_first'], 'name_last': data['users'][i]['name_last'], 'handle_str': data['users'][i]['handle'], } users.append(user) return { "users": users }