def edit_msg_react_in_list(msg, uid, method): """Interate the messages list by its id, return the message after edit.""" # get the channels channels = data.return_channels() messages = data.return_messages() # modify in channel for i in channels: if i['channel_id'] == msg['channel_id']: for temp in i['message']: if temp['message_id'] == msg['message_id']: if method == 'add': temp['reacts'][0]["u_ids"].append(int(uid)) elif method == 'delete': temp['reacts'][0]["u_ids"].remove(int(uid)) # modify in msg.json for temp in messages: if temp['message_id'] == msg['message_id']: if method == 'add': temp['reacts'][0]["u_ids"].append(int(uid)) elif method == 'delete': temp['reacts'][0]["u_ids"].remove(int(uid)) # add it to memory data.replace_channels(channels) data.replace_messages(messages)
def send_random_messages(channel_id, num): ''' send a number of random messages''' # characters to use in the message characters = string.ascii_letters + string.digits + string.punctuation + string.whitespace # find which channels there are focus_channel = None channels = data.return_channels() for channel in channels: if channel['channel_id'] == channel_id: focus_channel = channel break # add random messages to list messages = [] for i in range(num): msg = create_random_message(characters, i) return_message = { 'message_id': i, 'u_id': str(random.choice(focus_channel['all_members'])['u_id']), 'message': msg, 'time_created': i, } messages.append(return_message) # add the message to channel for i in channels: if i['channel_id'] == channel_id: i['messages'] = messages break # add that to persistent storage data.replace_channels(channels) return messages
def add_owner_in_channel(channel_id, owners): """Add a member into the owner list.""" channels = data.return_channels() for users in channels: if users['channel_id'] == channel_id: users['owner_members'].append(owners) data.replace_channels(channels)
def remove_whole_channel(channel_id): """If no owner exist, remove the whole channel.""" channels = data.return_channels() for chan in channels: if chan['channel_id'] == channel_id: channels.remove(chan) data.replace_channels(channels)
def rm_owner_in_channel(channel_id, owners): """Remove a member from the owner list.""" channels = data.return_channels() for users in channels: if users['channel_id'] == channel_id: for onrs in users['owner_members']: if onrs['u_id'] == owners: users['owner_members'].remove(onrs) data.replace_channels(channels)
def remove_a_member_in_channel(u_id, channel_id): """Remove the member by user if from the channel.""" channels = data.return_channels() for users in channels: if users['channel_id'] == channel_id: for member in users['all_members']: if member['u_id'] == u_id: users['all_members'].remove(member) data.replace_channels(channels)
def adding_message(return_message, channel_id): """Adding given return_message in the whole list.""" # get the channels channels = data.return_channels() # add user into memory for i in channels: if i['channel_id'] == channel_id: i['message'].insert(0, return_message) # add it to memory data.replace_channels(channels) data.insert_messages(return_message)
def add_one_in_channel(channel_id, user): """Adding a member into the channel.""" # get the channels channels = data.return_channels() # add user into memory for i in channels: if i['channel_id'] == channel_id: i['all_members'].append(user) if check_permission(user['u_id']) == 1: i['owner_members'].append(user) # add it to memory data.replace_channels(channels)
def delete_msg_in_list(msg): """Interate the messages list by its id, return the message we need.""" # get the channels channels = data.return_channels() messages = data.return_messages() # deleting message from memory for i in channels: if i['channel_id'] == msg['channel_id']: i['message'].remove(msg) messages.remove(msg) # add it to memory data.replace_channels(channels) data.replace_messages(messages)
def change_msg_pin(msg, sign): """Interate the messages list by its id, return the message after edit.""" # get the channels channels = data.return_channels() messages = data.return_messages() # deleting message from memory for i in channels: if i['channel_id'] == msg['channel_id']: for temp in i['message']: if temp['message_id'] == msg['message_id']: temp['is_pinned'] = sign for temp in messages: if temp['message_id'] == msg['message_id']: temp['is_pinned'] = sign # add it to memory data.replace_channels(channels) data.replace_messages(messages)
def edit_msg_in_list(msg, text): """Interate the messages list by its id, return the message after edit.""" # get the channels channels = data.return_channels() messages = data.return_messages() # deleting message from memory for i in channels: if i['channel_id'] == msg['channel_id']: for temp in i['message']: if temp['message_id'] == msg['message_id']: temp['message'] = text for temp in messages: if temp['message_id'] == msg['message_id']: temp['message'] = text # add it to memory data.replace_channels(channels) data.replace_messages(messages)