def message_edit_v2(token, message_id, message): with open('src/data.json', 'r') as FILE: data = json.load(FILE) editorid = None for i in range(len(data['users'])): if data['users'][i]['token'] == token: editorid = data["users"][i]["u_id"] if editorid is None: raise AccessError("Invalid token") if len(message) > 1000: raise InputError("Message is longer than the 1000 character limit") # check to see if message exists if check_message_exists(message_id) == False: raise InputError("Message does not exist") # check to see if message is being edited by authorised user valid = True if check_message_sent_by_user(editorid, message_id) == False: valid = False # check to see if token belongs to the owner of channel being deleted for i in range(len(data["messages"])): if data["messages"][i]["message_id"] == message_id: channel_id = data["messages"][i]["channel_id"] if channel_id is not -1: for i in range(len(data["messages"])): if data["messages"][i]["message_id"] == message_id: channel_id = data["messages"][i]["channel_id"] for i in range(len(data["channels"][channel_id]["owner_members"])): if data["channels"][channel_id]["owner_members"][i][ "u_id"] == editorid: valid = True if valid == False: raise AccessError( "Authorised User is not the owner of this channel and did not send this message" ) else: if valid == False: raise AccessError( "Authorised User is not the owner of this dm and did not send this message" ) with open('src/data.json') as FILE: data2 = json.load(FILE) for i in range(len(data2["messages"]) - 1): if data2["messages"][i]["message_id"] == message_id: if message is '': del data2["messages"][i] else: data2["messages"][i]["message"] = message with open('src/data.json', 'w') as FILE: json.dump(data2, FILE, indent=4) return {}
def message_react_v1(token, message_id, react_id): ''' Given a message within a channel the authorised user is part of, add a "react" to that particular message ''' with open('src/data.json', 'r') as FILE: data = json.load(FILE) # check for valid token and get u_id reactorid = None for i in range(len(data['users'])): if data['users'][i]['token'] == token: reactorid = data["users"][i]["u_id"] if reactorid is None: raise AccessError("Invalid token") # check message exists if check_message_exists(message_id) == False: raise InputError("Message does not exist") # check whether user is within channel/Dm which the message is in for i in range(len(data["messages"])): if data["messages"][i]["message_id"] == message_id: channel_id = data["messages"][i]["channel_id"] dm_id = data["messages"][i]["dm_id"] if channel_id == -1: if check_user_in_dm(reactorid, dm_id) == False: raise AccessError("Authorised user is not part of the dm") if dm_id == -1: if check_user_in_channel(reactorid, channel_id) == False: raise AccessError("Authorised user is not part of the channel") # check react_id is valid if react_id != 1: raise InputError("Invalid react_id") reacts_list = data["messages"][message_id]["reacts"] # check if react_id already exists if reactorid in reacts_list[int(react_id) - 1]['u_ids']: raise InputError("Message already contains this react_id") # adding the u_id of the person making a react reacts_list[int(react_id) - 1]['u_ids'].append(reactorid) reacts_list[int(react_id) - 1]['is_this_user_reacted'] = True with open('src/data.json', 'w') as FILE: json.dump(data, FILE, indent=4) return {}
def message_unpin_v1(token, message_id): ''' Given a message within a channel, mark it as "pinned" to be given special display treatment by the frontend ''' with open('src/data.json', 'r') as FILE: data = json.load(FILE) # check for valid token and get u_id pinnerid = None for i in range(len(data['users'])): if data['users'][i]['token'] == token: pinnerid = data["users"][i]["u_id"] if pinnerid is None: raise AccessError("Invalid token") # check that message_id is valid if check_message_exists(message_id) == False: raise InputError("Message does not exist") for i in range(len(data["messages"])): if data["messages"][i]["message_id"] == message_id: channel_id = data["messages"][i]["channel_id"] dm_id = data["messages"][i]["dm_id"] # check whether user is within channel/Dm which the message is in if channel_id == -1: if check_user_in_dm(pinnerid, dm_id) == False: raise AccessError("Authorised user is not part of the dm") if check_owner_of_dm(pinnerid, dm_id) == False: raise AccessError("Authorised user is not an owner of the dm") if dm_id == -1: if check_user_in_channel(pinnerid, channel_id) == False: raise AccessError("Authorised user is not part of the channel") if check_owner_of_channel(pinnerid, channel_id) == False: raise AccessError("Authorised user is not an owner of the channel") # check whether the message is already unpinned if data["messages"][message_id]['is_pinned'] == False: raise InputError("Message is already unpinned") # unpin the message data["messages"][message_id]['is_pinned'] = False with open('src/data.json', 'w') as FILE: json.dump(data, FILE, indent=4) return {}
def message_remove_v1(token, message_id): with open('src/data.json', 'r') as FILE: data = json.load(FILE) # check to see if message exists if check_message_exists(message_id) == False: raise InputError("Message does not exist") u_id = None # token check for i in range(len(data['users'])): if data['users'][i]['token'] == token: u_id = data["users"][i]["u_id"] if u_id is None: raise AccessError("Invalid token") # check message was sent by the authorised user making the request valid = True if check_message_sent_by_user(u_id, message_id) == False: valid = False # check to see if token belongs to the owner of message being deleted for i in range(len(data["messages"])): if data["messages"][i]["message_id"] == message_id: channel_id = data["messages"][i]["channel_id"] if channel_id is not -1: for i in range(len(data["channels"][channel_id]["owner_members"])): if data["channels"][channel_id]["owner_members"][i][ "u_id"] == u_id: valid = True if valid == False: raise AccessError( "Authorised User is not the owner of this channel and did not send this message" ) else: if valid == False: raise AccessError( "Authorised User is not the owner of this dm and did not send this message" ) num_messages_sent = data["user_stats"][u_id]["stats"]["messages_sent"][-1][ "num_messages_sent"] - 1 messages_sent = { "num_messages_sent": num_messages_sent, "time_stamp": create_timestamp() } data["user_stats"][u_id]["stats"]["messages_sent"].append(messages_sent) num_messages = data["dreams_stats"]["messages_exist"][-1][ "num_messages_exist"] - 1 dreams_messages = { "num_messages_exist": num_messages, "time_stamp": create_timestamp() } data["dreams_stats"]["messages_exist"].append(dreams_messages) for i in range(len(data["messages"])): if data["messages"][i]["message_id"] == message_id: del data["messages"][i] break with open('src/data.json', 'w') as FILE: json.dump(data, FILE, indent=4) calc_involement_rate(u_id) calc_utilisation_rate() return {}