Ejemplo n.º 1
0
def search(token, query_str):
    channelDatabase = Load('Channel_database.pkl')
    userDatabase = Load('User_database.pkl')
    messageDatabase = Load('Message_database.pkl')

    u_id = helper.find_id_from_token(token)

    message_dic = []

    for i in range(len(messageDatabase)):
        print(i)
        if (query_str.lower()
                in messageDatabase[i]['message'].lower()) == True:
            print("True")
            message_dic.append({
                'message_id': messageDatabase[i]['message_id'],
                'u_id': messageDatabase[i]['u_id'],
                'message': messageDatabase[i]['message'],
                'time_created': messageDatabase[i]['time'],
                'reacts': messageDatabase[i]
                ['react_id'],  #[{ 'react_id': 1, 'u_ids': [1], 'is_this_user_reacted':0 }],
                'is_pinned': messageDatabase[i]['is_pinned']
            })
    print(message_dic)
    return {'messages': message_dic}
Ejemplo n.º 2
0
def message_sendlater(token, channel_id, message, time_sent):
    channelDatabase = Load('Channel_database.pkl')
    userDatabase = Load('User_database.pkl')
    messageDatabase = Load('Message_database.pkl')

    timesent = int(time_sent)

    helper.check_message_length(message)

    if helper.check_exist(channelDatabase, int(channel_id),
                          'channelId') == False:
        error_raise.invalid_channelid()

    #check if time is in the past
    elif helper.get_time() < int(timesent):
        error_raise.time_is_in_past()

    else:
        u_id = helper.find_id_from_token(token)
        for j in range(len(channelDatabase)):
            if channelDatabase[j]['channelId'] == int(channel_id):
                if helper.check_exist(channelDatabase[j]['all_members'], u_id,
                                      'u_id') == False:
                    error_raise.unable_to_operate()

    helper.timerStart(timesent)

    while helper.timerGoing():
        print("still going")
        time.sleep(2)

    return message_send(token, channel_id, message)
Ejemplo n.º 3
0
def message_unpin(token, message_id):
    channelDatabase = Load('Channel_database.pkl')
    userDatabase = Load('User_database.pkl')
    messageDatabase = Load('Message_database.pkl')

    #Find user_id and permission_id
    u_id = helper.find_id_from_token(token)
    permission_id = helper.find_user_permission(token)

    for j in range(len(messageDatabase)):
        if (messageDatabase[j]['message_id'] == int(message_id)):
            if (permission_id != 1
                    or messageDatabase[j]['is_pinned'] == False):
                for l in range(len(channelDatabase)):
                    if channelDatabase[l]['channelId'] == int(
                            messageDatabase[j]['channel_id']):
                        helper.check_valid_member(
                            channelDatabase[l], 'all_members', 'owner_members',
                            messageDatabase[j]['is_pinned'], u_id)
                        helper.messageDatabase[j]['is_pinned'] = False
                        pickle_message(messageDatabase)
                        return {}
                helper.check_pin_status(messageDatabase[j]['is_pinned'], False)
                helper.check_if_owner(permission_id)
                messageDatabase[j]['is_pinned'] = False
                pickle_message(messageDatabase)
                return {}
            else:
                messageDatabase[j]['is_pinned'] = False
                pickle_message(messageDatabase)
                return {}

    error_raise.invalid_messageID()
Ejemplo n.º 4
0
def message_edit(token, message_id, message):
    channelDatabase = Load('Channel_database.pkl')
    userDatabase = Load('User_database.pkl')
    messageDatabase = Load('Message_database.pkl')

    #Find user_id and permission_id
    u_id = helper.find_id_from_token(token)
    permission_id = helper.find_user_permission(token)

    if helper.check_exist(messageDatabase, int(message_id),
                          'message_id') == True:
        i = helper.get_index(messageDatabase, int(message_id), 'message_id')
        for j in range(len(channelDatabase)):
            if channelDatabase[j]['channelId'] == int(
                    messageDatabase[i]['channel_id']):
                #if not sent by authorised user
                helper.check_if_authorised_toedit(
                    u_id, permission_id, messageDatabase[i]['u_id'],
                    channelDatabase[j]['owner_members'])
                messageDatabase[i]['message'] = message
                pickle_users(userDatabase)
                pickle_channels(channelDatabase)
                pickle_message(messageDatabase)
                print(message)
                return {}
Ejemplo n.º 5
0
def channel_join(token, channel_id):
  #pickle 
  channelDatabase = Load('Channel_database.pkl')
  userDatabase = Load('User_database.pkl')
  
  #check the channel we wanto leave is valid/existed
  helper.check_channel_id(channel_id)

  #check when the channel is private and i am not 
  helper.check_not_autho_join(token,channel_id)
  
  #find the u_id
  u_id = helper.find_id_from_token(token)
  
  
  for i in range(len(channelDatabase)):
     if channelDatabase[i]['channelId'] == int(channel_id):
       for j in range(len(userDatabase)):
         if userDatabase[j]['u_id'] == u_id:
          channelDatabase[i]['all_members'].append({
          'u_id':u_id, 
          'name_first':userDatabase[j]['name_first'],
          'name_last': userDatabase[j]['name_last'],
          'profile_img_url':userDatabase[j]['profile_img_url']
          })
          userDatabase[j]['channel'].append(int(channel_id))
          #rewrite the new userdatabase and channeldatabase to pkl file
          pickle_channels(channelDatabase)
          pickle_users(userDatabase)
Ejemplo n.º 6
0
def message_send(token, channel_id, message):
    channelDatabase = Load('Channel_database.pkl')
    userDatabase = Load('User_database.pkl')
    messageDatabase = Load('Message_database.pkl')

    #check if message is more than 1000 characters
    helper.check_message_length(message)

    u_id = helper.find_id_from_token(token)

    for j in range(len(channelDatabase)):
        if channelDatabase[j]['channelId'] == int(channel_id):
            if helper.check_exist(channelDatabase[j]['all_members'], u_id,
                                  'u_id') == True:
                message_id = helper.create_messageid(int(channel_id), message)
                print(messageDatabase)
                #if helper.check_exist(channelDatabase[j]['messagelist'], message_id, 'message_id') == False:
                #if not any(d['messagelist'] == message_id for d in channelDatabase):
                if message_id not in channelDatabase[j]['messagelist']:
                    channelDatabase[j]['messagelist'].append(message_id)
                messageDatabase.append({
                    'message_id':
                    message_id,
                    'u_id':
                    u_id,
                    'channel_id':
                    channel_id,
                    'message':
                    message,
                    'time':
                    helper.get_time(),
                    'react_id': [{
                        'react_id': 1,
                        'u_ids': [],
                        'is_this_user_reacted': bool(False)
                    }],
                    'is_pinned':
                    False
                })
                pickle_users(userDatabase)
                pickle_channels(channelDatabase)
                pickle_message(messageDatabase)

                return {'message_id': message_id}
            else:
                error_raise.user_not_in_channel()
Ejemplo n.º 7
0
def message_remove(token, message_id):
    channelDatabase = Load('Channel_database.pkl')
    userDatabase = Load('User_database.pkl')
    messageDatabase = Load('Message_database.pkl')
    #Find user_id and permission_id
    u_id = helper.find_id_from_token(token)
    permission_id = helper.find_user_permission(token)

    #check if message exists in database
    if helper.check_exist(messageDatabase, int(message_id),
                          'message_id') == False:
        error_raise.invalid_messageID()

    #check raise error when non authorised user or the authorised user is not a admin or owner
    message_index = helper.get_index(messageDatabase, int(message_id),
                                     'message_id')

    channel_id = messageDatabase[message_index]['channel_id']

    if helper.check_exist(channelDatabase, int(channel_id),
                          'channelId') == False:
        error_raise.invalid_channelid()

    channel_index = helper.get_index(channelDatabase, int(channel_id),
                                     'channelId')

    print(u_id)
    print(type(u_id))

    print(permission_id)
    print(type(permission_id))

    print(channel_index)
    print(type(channel_index))

    helper.check_if_authorised_toedit(
        u_id, permission_id, messageDatabase[message_index]['u_id'],
        channelDatabase[channel_index]['owner_members'])
    channelDatabase[channel_index]['messagelist'].remove(int(message_id))
    a = {}
    a = messageDatabase.pop(message_index)
    pickle_channels(channelDatabase)
    pickle_message(messageDatabase)
    return {}
Ejemplo n.º 8
0
def message_unreact(token, message_id, react_id):
    channelDatabase = Load('Channel_database.pkl')
    userDatabase = Load('User_database.pkl')
    messageDatabase = Load('Message_database.pkl')

    #check if react_id is valid and find u_id
    helper.check_react_id(int(react_id))
    u_id = helper.find_id_from_token(token)

    if helper.check_exist(messageDatabase, int(message_id),
                          'message_id') == True:
        index_d = helper.get_index(messageDatabase, int(message_id),
                                   'message_id')
        if helper.check_exist(channelDatabase,
                              int(messageDatabase[index_d]['channel_id']),
                              'channelId') == True:
            index_e = helper.get_index(
                channelDatabase, int(messageDatabase[index_d]['channel_id']),
                'channelId')
            if helper.check_exist(channelDatabase[index_e]['all_members'],
                                  u_id, 'u_id') == True:
                index_f = helper.get_index(
                    messageDatabase[index_d]['react_id'], 1, 'react_id')
                for key, value in messageDatabase[index_d]['react_id'][
                        index_f].items():
                    if key == 'is_this_user_reacted' and value == False:
                        error_raise.already_unreacted()
                helper.change_react_remove(
                    messageDatabase[index_d]['react_id'][index_f], 'u_ids',
                    'is_this_user_reacted', u_id)
                pickle_message(messageDatabase)
            else:
                error_raise.message_invalid()

# pickle_channels(channelDatabase)
#pickle_message(messageDatabase)
    return {}