Files
IM/MS/works/db/UserFriendsDB.cpp
dongl f66340dcc0 smtp 合并到 MS 中
添加好友 取好友列表 略有修改
接下再来 要该包类型宏  备份一下
2023-05-16 17:12:05 +08:00

143 lines
3.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// Created by dongl on 23-5-4.
//
#include "UserFriendsDB.h"
#include "linkDB.h"
// 查询好友添加权限类型
char UserFriendsDB::select_add_type(uint64_t account) {
conn = LinkDB::safe_grab();
auto query = conn->query("select restrictions from im_user_friends where account=%0:account");
query.parse();
// 查看添加授权类型 0 直接添加 1 验证问题 2 账户审核
auto ret = query.store(account)[0][0][0]; // 因为 account 唯一 所以结果 至多一个
// 放回链接
LinkDB::release(conn);
return ret;
}
// 修改好友列表数据 添加好友
/// friends {
/// uid info { }
/// }
std::tuple<bool, std::string> UserFriendsDB::add_friends(uint64_t account, uint64_t friends) {
conn = LinkDB::safe_grab();
auto q = conn->query("select username from im_user where account=%1:account");
q.template_defaults[1] = friends;
q.parse();
auto username = q.store();
if (username.num_rows() < 1) {
LinkDB::release(conn);
return {false, "未查询到账户"};
}
std::string name = username[0][0].c_str();
auto query = conn->query("update im_user_friends set friends ="
"JSON_SET(friends, '$.\"%2:friends\"', "
"JSON_OBJECT('username', '%3:username', 'belong_grouping', 1, 'add_time', 10000, 'add_source', 1)"
")"
"where account=%1:account");
query.template_defaults[1] = account;
query.template_defaults[2] = friends;
query.template_defaults[3] = name.c_str();
query.parse();
auto ret = query.exec();
// 放回链接
LinkDB::release(conn);
return {ret, "添加失败"};
}
void UserFriendsDB::insert_friends_to_be_added(uint64_t account, uint64_t friends) {
conn = LinkDB::safe_grab();
// 放回链接
LinkDB::release(conn);
}
std::optional<std::string> UserFriendsDB::select_friends_all(uint64_t account) {
conn = LinkDB::safe_grab();
auto q = conn->query("select friends from im_user_friends where account=%0:account");
q.parse();
auto ret = q.store(account);
if (ret.num_rows() < 1) {
return std::nullopt;
}
std::string friends;
ret[0][0].to_string(friends);
// for (auto mem = document.MemberBegin(); mem != document.MemberEnd(); ++mem) {
// mem->name;
// }
// 放回链接
LinkDB::release(conn);
return friends;
}
std::optional<rapidjson::Document> UserFriendsDB::select_friends_all_json(uint64_t account) {
auto temp = select_friends_all(account);
rapidjson::Document document;
if (temp.has_value()) {
document.Parse(temp.value().c_str());
return document;
} else {
return std::nullopt;
}
}
std::optional<rapidjson::Document>
UserFriendsDB::select_friends_info(uint64_t account, uint64_t friends) {
conn = LinkDB::safe_grab();
auto q = conn->query("select JSON_EXTRACT(friends, '$.\"%2:friends\"') as friend_info "
"from im_user_friends where account=%1:account");
q.template_defaults[1] = account;
q.template_defaults[2] = friends;
q.parse();
// 查库
auto ret = q.store();
printf("%zu\n", ret.num_rows());
if (ret.num_rows() < 1) {
return std::nullopt;
}
// 取json字符串
std::string friend_info;
ret[0][0].to_string(friend_info);
// 解析json
rapidjson::Document document;
document.Parse(friend_info.c_str());
// 放回链接
LinkDB::release(conn);
return document;
}