def run(sc, parameters): user_id = sc_to_user_id[sc] uid = parameters[0] accepted = parameters[1] c = database.get_cursor() sql="SELECT 1 from friends where from_user_id=%d and to_user_id=%d and accepted=0"%(uid, user_id) c.execute(sql) rows = c.fetchall() if len(rows) == 0: return if not accepted: c = database.get_cursor() spl="delete from friends where from_user_id=%d and to_user_id=%d and accepted=0"%(uid,user_id) c.execute(sql) return if accepted: c = database.get_cursor() sql="update friends set accepted=1 where from_user_id=%d and to_user_id=%d and accepted=0"%(uid, user_id) c.execute(sql) c = database.get_cursor() sql="insert into friends (from_user_id,to_user_id,accepted) values (%d,%d,1)"%(user_id, uid) c.execute(sql) sc.send(MessageType.contact_info, add_target_type(database.get_user(uid), 0)) if uid in user_id_to_sc: user_id_to_sc[uid].send(MessageType.contact_info, add_target_type(database.get_user(user_id), 0))
def run(sc, parameters): # pprint(parameters) user_id = sc_to_user_id[sc] sender = database.get_user(user_id) # target_id在后面填入,对于发送方和接收方不一样 message = { "message": parameters['message'], 'sender_id': user_id, 'sender_name': sender['username'], 'target_type': parameters['target_type'], 'time': int(round(time.time() * 1000)) } if parameters['target_type'] == 0: # 私聊 if not database.is_friend_with(user_id, parameters['target_id']): sc.send(MessageType.general_failure, '还不是好友') return # 给发送方发回执 message['target_id'] = parameters['target_id'] user_id_to_sc[user_id].send(MessageType.on_new_message, message) database.add_to_chat_history(user_id, message['target_id'], message['target_type'], _serialize_dict(message), True) # 给接收方发消息,存入聊天记录 message['target_id'] = user_id sent = False if parameters['target_id'] in user_id_to_sc: sent = True user_id_to_sc[parameters['target_id']].send( MessageType.on_new_message, message) database.add_to_chat_history(parameters['target_id'], message['target_id'], message['target_type'], _serialize_dict(message), sent) if parameters['target_type'] == 1: # 群聊 message['target_id'] = parameters['target_id'] if not database.in_room(user_id, parameters['target_id']): sc.send(MessageType.general_failure, '还没有加入该群') return users_id = database.get_room_members_id(parameters['target_id']) for user_id in users_id: sent = False if user_id in user_id_to_sc: user_id_to_sc[user_id].send(MessageType.on_new_message, message) sent = True database.add_to_chat_history(user_id, message['target_id'], message['target_type'], _serialize_dict(message), sent)
def run(sc, parameters): user_id = sc_to_user_id[sc] # parameters = username c = database.get_cursor() username = parameters.strip().lower() # print(user_id) r = c.execute('SELECT id from users where username=?', [username]).fetchall() #print(r[0][0]) #异常条件判断 if len(r) == 0: sc.send(MessageType.del_friend_result, [False, '用户名不存在']) return uid = r[0][0] if uid == user_id: sc.send(MessageType.del_friend_result, [False, '不能删除自己']) return c = database.get_cursor() r = c.execute( 'SELECT 1 from friends where from_user_id=? and to_user_id=? and accepted=1', [user_id, uid]).fetchall() #判断对方是否已经是自己的好友了 if len(r) == 0: sc.send(MessageType.del_friend_result, [False, '该用户还不是您的好友']) return #对方是你的好友 if len(r) != 0: #删除操作 c = database.get_cursor() c.execute( 'delete from friends where from_user_id=? and to_user_id=? and accepted=1', [uid, user_id]).fetchall() c.execute( 'delete from friends where from_user_id=? and to_user_id=? and accepted=1', [user_id, uid]).fetchall() sc.send(MessageType.del_friend_result, [True, '']) sc.send(MessageType.del_info, database.get_user(uid)) #如果要删除的好友在线,将他的好友列表中将我删除的操作码发送给服务器 if uid in user_id_to_sc: user_id_to_sc[uid].send(MessageType.del_info, database.get_user(user_id)) return
def run(sc, parameters): user_id = sc_to_user_id[sc] uid = parameters[0] accepted = parameters[1] c = database.get_cursor() r = c.execute( 'SELECT 1 from friends where from_user_id=? and to_user_id=? and accepted=0', [uid, user_id]) rows = r.fetchall() if len(rows) == 0: return if not accepted: c = database.get_cursor() c.execute( 'delete from friends where from_user_id=? and to_user_id=? and accepted=0', [uid, user_id]) return if accepted: c = database.get_cursor() c.execute( 'update friends set accepted=1 where from_user_id=? and to_user_id=? and accepted=0', [uid, user_id]) c = database.get_cursor() c.execute( 'insert into friends (from_user_id,to_user_id,accepted) values (?,?,1)', [user_id, uid]) sc.send(MessageType.contact_info, add_target_type(database.get_user(uid), 0)) if uid in user_id_to_sc: user_id_to_sc[uid].send( MessageType.contact_info, add_target_type(database.get_user(user_id), 0))
def run(sc, parameters): user_id = sc_to_user_id[sc] # parameters = username c = database.get_cursor() username = parameters.strip().lower() sql = "SELECT id from users where username='******'" % username #r = c.execute('SELECT id from users where username=?', [username]).fetchall() c.execute(sql) r = c.fetchall() if len(r) == 0: sc.send(MessageType.add_friend_result, [False, '用户名不存在']) return uid = r[0][0] if uid == user_id: sc.send(MessageType.add_friend_rescult, [False, '不能加自己为好友']) return c = database.get_cursor() sql = "SELECT 1 from friends where from_user_id=%d and to_user_id=%d" % ( user_id, uid) c.execute(sql) r = c.fetchall() # r = c.execute('SELECT 1 from friends where from_user_id=? and to_user_id=?', [user_id, uid]).fetchall() if len(r) != 0: sc.send(MessageType.add_friend_result, [False, '已经是好友/已经发送过好友请求']) return c = database.get_cursor() sql = "insert into friends (from_user_id,to_user_id,accepted) values (%d,%d,0)" % ( user_id, uid) c.execute(sql) r = c.fetchall() #c.execute('insert into friends (from_user_id,to_user_id,accepted) values (?,?,0)', [user_id, uid]).fetchall() sc.send(MessageType.add_friend_result, [True, '']) if uid in user_id_to_sc: user_id_to_sc[uid].send(MessageType.incoming_friend_request, database.get_user(user_id))
def run(sc, parameters): parameters[0] = parameters[0].strip().lower() c = database.get_cursor() sql = "SELECT id,username from users where username='******' and password='******'" % ( parameters[0], md5(parameters[1])) print(sql) #r = c.execute("SELECT id,username from users where username='******' and password='******'", (parameters[0], md5(parameters[1]))) c.execute(sql) rows = c.fetchall() if len(rows) == 0: sc.send(MessageType.login_failed) return user_id = rows[0][0] # 已经登入,踢下线 if user_id in user_id_to_sc: sc_old = user_id_to_sc[user_id] sc_old.send(MessageType.server_kick) sc_old.close() remove_sc_from_socket_mapping(sc_old) sc_to_user_id[sc] = user_id user_id_to_sc[user_id] = sc user = database.get_user(user_id) sc.send(MessageType.login_successful, user) login_bundle = {} # 发送群列表 rms = database.get_user_rooms(user_id) login_bundle['rooms'] = list(map(lambda x: add_target_type(x, 1), rms)) # for rm in rms: # sc.send(MessageType.contact_info, add_target_type(rm, 1)) # 发送好友请求 frs = database.get_pending_friend_request(user_id) for fr in frs: sc.send(MessageType.incoming_friend_request, fr) # 发送好友列表 frs = database.get_friends(user_id) login_bundle['friends'] = list(map(lambda x: add_target_type(x, 0), frs)) for fr in frs: # sc.send(MessageType.contact_info, add_target_type(fr, 0)) # 通知他的好友他上线了 if fr['id'] in user_id_to_sc: user_id_to_sc[fr['id']].send(MessageType.friend_on_off_line, [True, user_id]) # 通知群聊里的人他上线了 # [room_id, user_id, online] rooms_id = database.get_user_rooms_id(user_id) for room_id in rooms_id: users_id = database.get_room_members_id(room_id) for _user_id in users_id: if _user_id in user_id_to_sc and user_id != _user_id: user_id_to_sc[_user_id].send(MessageType.room_user_on_off_line, [room_id, user_id, True]) login_bundle['messages'] = database.get_chat_history(user_id) sc.send(MessageType.login_bundle, login_bundle)