def send_message(user_id, user_message, talk_to): user_name = "" # Find user's name with user_id for user in server_data_container.user_list: if user['user_id'] == user_id: user_name = user['user_name'] break format_msg = Message(user_id, talk_to, user_message) # Group chat here if talk_to == 0: print('Group chat', format_msg) format_msg.content = "<%s>: %s" % (user_name, format_msg.content) for user in server_data_container.user_list: if user['user_id'] != user_id: user['message_queue'].put(format_msg) # Individual talk here else: print('Individual chat', format_msg) for user in server_data_container.user_list: if user['user_id'] == talk_to and user_id != talk_to: user['message_queue'].put(format_msg) db = MySQL() db.modify( "INSERT INTO Message (user_id, content, destination) VALUES (%s,%s,%s)", int(user_id), user_message, int(talk_to))
def regist_new_user(user_name, password): # Every user_id is unique db = MySQL() result = db.select( "SELECT * FROM User WHERE username=%s AND password=%s", user_name, password) if not result: return False user_id = result[0][0] #Regist: info_container = { 'user_id': user_id, 'user_name': user_name, 'message_queue': queue.Queue() } for user in server_data_container.user_list: if user['user_id'] == info_container['user_id']: server_data_container.user_list.remove(user) #Add to user_list server_data_container.user_list.append(info_container) #For Debug print('New User : User id:', user_id, 'User Name:', user_name) return user_id
def get_history_messages(user_id): messages = [] db = MySQL() results = db.select( "SELECT * FROM Message WHERE user_id=%s OR destination=%s", int(user_id), int(user_id)) for result in results: messages.append(Message(result[1], result[3], result[2])) return messages
def search_users(keyword): user_list = [] db = MySQL() results = db.select("SELECT * FROM User WHERE username LIKE %s", "%" + keyword + "%") for result in results: user_list.append(Contact(result[0], result[1], result[1] + ".jpg")) return user_list
def user_register(username, password, img): db = MySQL() result = db.select("SELECT * FROM User WHERE username=%s", username) if result: return False db.connect() db.modify( "INSERT INTO User (username, password, avatar) VALUES (%s,%s,%s)", username, password, img.data) return True
def get_username_by_id(user_id): db = MySQL() result = db.select("SELECT * FROM User WHERE id=%s", int(user_id)) if not result: return False return result[0][1]
def delete_history_records(user_id): db = MySQL() db.modify("DELETE FROM Message WHERE user_id=%s OR destination=%s", int(user_id), int(user_id))
def get_avatar(username): db = MySQL() result = db.select("SELECT * FROM User WHERE username=%s", username) avatar = xmlrpc.client.Binary(result[0][3]) return avatar