def toy_uploader(): to_user = request.form.get("to_user") from_user = request.form.get("user_id") friend_type = request.form.get("friend_type") to_user_name = "" if friend_type == "toy": toy = MONGODB.toys.find_one({"_id": ObjectId(to_user)}) for t in toy["friend_list"]: if t.get("friend_id") == from_user: to_user_name = t.get("friend_remark") # 消息提醒 to_user_name = text2audio(f"你有来自{to_user_name}的消息") file = request.files.get("reco") filename = f"{uuid4()}.wav" new_file_path = os.path.join(CHAT_PATH,filename) file.save(new_file_path) chat = { "from_user": from_user, "to_user": to_user, "chat": filename, "createTime": time.time() } MONGODB.chats.update_one({"user_list": {"$all": [from_user, to_user]}}, {"$push": {"chat_list": chat}}) set_chat(to_user, from_user) RET["CODE"] = 0 RET["MSG"] = "上传成功" RET["DATA"] = {"filename":to_user_name,"code":0,"friend_type":friend_type} print(RET["DATA"]) return jsonify(RET)
def app_uploader(): to_user = request.form.get("to_user") from_user = request.form.get("user_id") toy_info = MONGODB.toys.find_one({"_id":ObjectId(to_user)}) from_user_name = "" for friend in toy_info.get("friend_list"): if friend.get("friend_id") == from_user: from_user_name = friend.get("friend_remark") file = request.files.get("reco_file") new_file_path = os.path.join(CHAT_PATH,file.filename) file.save(new_file_path) os.system(f"ffmpeg -i {new_file_path} {new_file_path}.mp3") chat = { "from_user": from_user, "to_user": to_user, "chat": f"{file.filename}.mp3", "createTime": time.time() } MONGODB.chats.update_one({"user_list": {"$all": [from_user, to_user]}}, {"$push": {"chat_list": chat}}) # set_chat(to_user, from_user) new_file = text2audio(f"你有来自{from_user_name}的消息") RET["CODE"] = 0 RET["MSG"] = "上传成功" RET["DATA"] = {"filename":new_file,"friend_type":"app"} return jsonify(RET)
def recv_msg(): from_user = request.form.get("from_user") # app to_user = request.form.get("to_user") """ 我={ "你":1, "他":0, "app3":5 } """ chat_window = MONGODB.chats.find_one( {"user_list": { "$all": [from_user, to_user] }}) count = get_chat(to_user, from_user) new_list = [] print("玩具与当前用户的未读消息数量是:", count) if count: # chat = chat_window.get("chat_list")[-count:] # [-1] == [-未读消息数量:] # 如果 from_user == 自己 这条就放弃 继续下一条 re_chat_list = reversed(chat_window.get("chat_list")) for ch in re_chat_list: if ch.get("from_user") != to_user: new_list.append(ch) if len(new_list) == count: break toy_info = MONGODB.toys.find_one({"_id": ObjectId(to_user)}) for fri in toy_info.get("friend_list"): if fri.get("friend_id") == from_user: s = f"以下是来自{fri.get('friend_remark')}的消息" filename = text2audio(s) new_list.append({"chat": filename}) return jsonify(new_list)
def app_uploader(): # 收到App传来的语音文件,代表消息发送了 # 开启写入chat_list to_user = request.form.get("to_user") # 5ca17f85ea512d215cd9b079 from_user = request.form.get("user_id") # 5c9d8da3ea512d2048826260 toy_info = MONGODB.toys.find_one({"_id": ObjectId(to_user)}) from_user_name = "" for friend in toy_info.get("friend_list"): if friend.get("friend_id") == from_user: from_user_name = friend.get("friend_remark") # 利用两个用户的ID查询数据它们的chat_window # chat_window = MONGODB.chats.find_one({"user_list":{"$all":[from_user,to_user]}}) file = request.files.get("reco_file") new_file_path = os.path.join(CHAT_PATH, file.filename) file.save(new_file_path) os.system(f"ffmpeg -i {new_file_path} {new_file_path}.mp3") # os.remove(new_file_path) chat = { "from_user": from_user, "to_user": to_user, "chat": f"{file.filename}.mp3", "createTime": time.time() } # chat_window["chat_list"].append(chat) # 产生聊天记录的存放 MONGODB.chats.update_one({"user_list": { "$all": [from_user, to_user] }}, {"$push": { "chat_list": chat }}) set_chat(to_user, from_user) new_file = text2audio(f"你有来自{from_user_name}的消息") RET["CODE"] = 0 RET["MSG"] = "上传成功" RET["DATA"] = {"filename": new_file, "friend_type": "app"} return jsonify(RET)
def recv_msg(): from_user = request.form.get("from_user") to_user = request.form.get("to_user") chat_window = MONGODB.chats.find_one( {"user_list": { "$all": [from_user, to_user] }}) # count = get_chat(to_user,from_user) new_list = [] # print("玩具与当前用户的未读消息数量是:", count) # if count: # re_chat_list = reversed(chat_window.get("chat_list")) # for ch in re_chat_list: # if ch.get("from_user") != to_user: # new_list.append(ch) # if len(new_list) == count: # break toy_info = MONGODB.toys.find_one({"_id": ObjectId(to_user)}) for fri in toy_info.get("friend_list"): if fri.get("friend_id") == from_user: s = f"以下是来自{fri.get('friend_remark')}的消息" filename = text2audio(s) new_list.append({"chat": filename}) return jsonify(new_list)