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 channel_details_v2(token, channel_id): ''' Given a Channel with ID channel_id that the authorised user is part of, provide basic details about the channel Arguments: auth_user_id (integer) - id of user who is part of the channel channel_id (integer) - id of channel Exceptions: InputError - Occurs when channel_id does not refer to a valid channel. AccessError - Occurs when the auth_user_id passed in is not valid - Occurs when the authorised user is not already a member of the channel Return Value: Returns {name, owner_members, all_members} ''' with open('src/data.json', 'r') as FILE: data = json.load(FILE) # check if token exists auth_user_id = False for i in range(len(data["users"])): if data["users"][i]["token"] == token: auth_user_id = data["users"][i]["u_id"] if auth_user_id is False: raise AccessError("Invalid token") auth_user_id = convert_token(token) # check if the channel_id exists if check_channel_exists(channel_id) == False: raise InputError("Invalid channel") # check whether user belongs in this channel if check_user_in_channel(auth_user_id, channel_id) == False: raise AccessError( "Authorised user needs to be a member of the channel") return { 'name': data["channels"][channel_id]['name'], 'is_public': data["channels"][channel_id]['is_public'], 'owner_members': data["channels"][channel_id]['owner_members'], 'all_members': data["channels"][channel_id]['all_members'], }
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 standup_start_v1(token, channel_id, length): ''' Starts a standup in a channel for 'length' seconds. Arguments: token (string) - token of user starting standup channel_id (integer) - id of channel standup is in length (integer) - length of the period in which standup is active Exceptions: InputError - Occurs when channel_id does not refer to a valid channel - Occurs when an active standup is already running in the channel AccessError - Occurs when the token passed in is not valid - Occurs when the authorised user is not already a member of the channel Return Value: Returns {time_finish} ''' # exception checks with open('src/data.json', 'r') as FILE: data = json.load(FILE) # check valid token u_id = None 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 if channel id is valid if check_channel_exists(channel_id) == False: raise InputError("Invalid channel") # check user is in channel if check_user_in_channel(u_id, channel_id) == False: raise AccessError("Authorised user is not part of the channel") standupActive = standup_active_v1(token, channel_id) if standupActive['is_active'] == True: raise InputError("Active standup is currently running in this channel") dateTimeObj = datetime.now() timeStampStr = ( dateTimeObj + timedelta(seconds=int(length))).strftime("%d-%b-%Y (%H:%M)") new_standup = { 'channel_id': channel_id, 'time_finish': timeStampStr, 'messages': [], } data["standups"].append(new_standup) # send all messages in standup to channel when standup ends time.sleep(length) bundledMessages = '' allMessages = '' for i in range(len(data['standups'])): if data['standups'][i]['channel_id'] == channel_id: allMessages = data['standups'][i]['messages'] for i in range(len(allMessages)): bundledMessages += allMessages[i]['name'] + ': ' + allMessages[i][ 'message'] + '\n' message_send_v2(token, channel_id, bundledMessages) with open('src/data.json', 'w') as FILE: json.dump(data, FILE, indent=4) return {'time_finish': timeStampStr}
def channel_invite_v2(token, channel_id, u_id): ''' Invites a user (with user id u_id) to join a channel with ID channel_id. Once invited the user is added to the channel immediately. Arguments: auth_user_id (integer) - id of user sending invite channel_id (integer) - id of channel being invited to u_id (integer) - id of user being invited to channel Exceptions: InputError - Occurs when channel_id does not refer to a valid channel. - Occurs when u_id does not refer to a valid user AccessError - Occurs when the auth_user_id passed in is not valid - Occurs when the authorised user is not already a member of the channel Return Value: Returns {} ''' with open('src/data.json', 'r') as FILE: data = json.load(FILE) # check that token is valid inviteid = None for i in range(len(data['users'])): if data['users'][i]['token'] == token: inviteid = data['users'][i]['u_id'] if inviteid is None: raise AccessError("Invalid token") # check if the channel_id exists if check_channel_exists(channel_id) == False: raise InputError("Invalid channel") # check if the u_id is valid if check_user_exists(u_id) == False: raise InputError("Invalid User") # checking if the person inviting is in the channel if check_user_in_channel(inviteid, channel_id) == False: raise AccessError( "Authorised user needs to be a member of the channel") invited_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': data["users"][u_id]["handle"] } # check if the user is already a member of the channel for i in range(len(data["channels"][channel_id]["all_members"])): if u_id == data["channels"][channel_id]["all_members"][i]["u_id"]: raise InputError("User already a member of channel") num_channels_joined = data["user_stats"][u_id]["stats"]["channels_joined"][ -1]["num_channels_joined"] + 1 channels_joined = { "num_channels_joined": num_channels_joined, "time_stamp": create_timestamp() } with open('src/data.json') as FILE: data2 = json.load(FILE) temp = data2["channels"][channel_id]["all_members"] y = invited_user temp.append(y) # if user invited is an owner if data2["users"][u_id]["permission_id"] is 1: temp2 = data2["channels"][channel_id]["owner_members"] y = invited_user temp2.append(y) temp3 = data2["user_stats"][u_id]["stats"]["channels_joined"] y = channels_joined temp3.append(y) with open('src/data.json', 'w') as FILE: json.dump(data2, FILE, indent=4) notif_msg = data["users"][inviteid]["handle"] + " invited you to " + data[ "channels"][channel_id]["name"] create_notifications(u_id, channel_id, -1, notif_msg) calc_involement_rate(u_id) calc_utilisation_rate() return {}
def channel_messages_v2(token, channel_id, start): ''' Given a Channel with ID channel_id that the authorised user is part of, return up to 50 messages between index "start" and "start + 50". Arguments: auth_user_id (integer) - id of user who is part of the channel channel_id (integer) - id of channel start (integer) - index indicating start of messages Exceptions: InputError - Occurs when channel_id does not refer to a valid channel. - Occurs when start is greater than the total number of messages in the channel. AccessError - Occurs when the auth_user_id passed in is not valid - Occurs when the authorised user is not already a member of the channel Return Value: Returns {messages, start, end} ''' with open('src/data.json', 'r') as FILE: data = json.load(FILE) # check that token is valid auth_user_id = None for i in range(len(data['users'])): if data['users'][i]['token'] == token: auth_user_id = data['users'][i]['u_id'] if auth_user_id is None: raise AccessError("Invalid token") # check if the channel_id exists if check_channel_exists(channel_id) == False: raise InputError("Invalid channel") # check that user belongs in the channel if check_user_in_channel(auth_user_id, channel_id) == False: raise AccessError( "Authorised user needs to be a member of the channel") end = start + 50 counter = 0 messagelist1 = [] messagelist2 = [] finallist = [] for i in range(len(data["messages"])): if data["messages"][i]["channel_id"] == channel_id: messagelist1.insert(0, data["messages"][i]) messagelist2.append(data["messages"][i]) # check for start being less than total messages in channel if start > len(messagelist1): raise InputError("Not enough messages in channel") for i in range(start, len(messagelist1)): if counter is 50: break if end > len(messagelist1): finallist.append(messagelist1[i]) else: finallist.insert(0, messagelist2[i]) counter += 1 if counter < 50: end = -1 return { 'messages': finallist, 'start': start, 'end': end, }
def message_sendlater_v1(token, channel_id, message, time_sent): ''' Send a message from authorised_user to the channel specified by channel_id automatically at a specified time in the future ''' with open('src/data.json', 'r') as FILE: data = json.load(FILE) # check for valid token and get u_id senderid = None for i in range(len(data['users'])): if data['users'][i]['token'] == token: senderid = data["users"][i]["u_id"] if senderid is None: raise AccessError("Invalid token") # check that the channel_id is valid if check_channel_exists(channel_id) == False: raise InputError("Invalid channel") # check that the message does not contain more than 1000 characters if len(message) > 1000: raise InputError("Message is longer than the 1000 character limit") # check sender is part of the channel if check_user_in_channel(senderid, channel_id) == False: raise AccessError("Authorised user is not part of the channel") now = datetime.now() if int(time_sent) < int(datetime.timestamp(now)): raise InputError("Time sent is in the past") timeout = int(time_sent) - int(datetime.timestamp(now)) time.sleep(timeout) dm_id = -1 new_message = { 'message_id': get_new_message_id(), 'u_id': senderid, 'message': message, 'time_created': time_sent, 'channel_id': channel_id, 'dm_id': dm_id, 'reacts': [{ 'react_id': 1, 'u_ids': [], 'is_this_user_reacted': False }], 'is_pinned': False } num_messages_sent = data["user_stats"][senderid]["stats"]["messages_sent"][ -1]["num_messages_sent"] + 1 messages_sent = { "num_messages_sent": num_messages_sent, "time_stamp": time_sent } data["user_stats"][senderid]["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": time_sent } data["dreams_stats"]["messages_exist"].append(dreams_messages) data["messages"].append(new_message) with open('src/data.json', 'w') as FILE: json.dump(data, FILE, indent=4) calc_involement_rate(senderid) calc_utilisation_rate() return { 'message_id': new_message["message_id"], }