修改了 添加好友的代码
发现了获取验证码的bug delete sri后 无报错不发数据包 还有填写验证码 错误 session删除了
This commit is contained in:
12
MS/MS.cpp
12
MS/MS.cpp
@@ -14,7 +14,10 @@ ev_pool* MS::pool = nullptr;
|
||||
management* MS::mage = nullptr;
|
||||
|
||||
MS::MS() {
|
||||
pool = new ev_pool(4);
|
||||
localEvPool();
|
||||
if (!pool) {
|
||||
pool = new ev_pool(4);
|
||||
}
|
||||
pool->add_event_base(listener);
|
||||
pool->add_event_bases(4);
|
||||
pool->run();
|
||||
@@ -97,5 +100,12 @@ void MS::event_cb(struct bufferevent *bev, short what, void *ctx) {
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
ev_pool *MS::localEvPool() {
|
||||
if (!pool) {
|
||||
pool = new ev_pool(5);
|
||||
}
|
||||
return pool;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
2
MS/MS.h
2
MS/MS.h
@@ -19,6 +19,8 @@ public:
|
||||
static void read_cb(struct bufferevent *bev, void *ctx);
|
||||
static void write_cb(struct bufferevent *bev, void *ctx);
|
||||
static void event_cb(struct bufferevent *bev, short what, void *ctx);
|
||||
static ev_pool* localEvPool();
|
||||
|
||||
private:
|
||||
static ev_pool* pool;
|
||||
static class management* mage;
|
||||
|
||||
@@ -31,15 +31,16 @@ agreement_response::~agreement_response() {}
|
||||
void agreement_response::set(mp::sri* sri, bufferevent* bev) {
|
||||
// auto resp = std::make_shared<mp::response>(mp::response());
|
||||
//
|
||||
// m_sri = resp->sri();
|
||||
// m_sri.set_subcommand(sri->subcommand());
|
||||
// m_sri.set_account(sri->account());
|
||||
// m_sri.set_username(sri->username());
|
||||
// m_sri.set_msg(sri->msg());
|
||||
// m_sri.set_token(sri->token());
|
||||
// m_sri.set_data(sri->data());
|
||||
// m_sri.set_email(sri->email());
|
||||
// m_sri.set_phone(sri->phone());
|
||||
// m_sri = ss.get();
|
||||
// m_sri->set_subcommand(sri->subcommand());
|
||||
// m_sri->set_account(sri->account());
|
||||
// m_sri->set_username(sri->username());
|
||||
// m_sri->set_msg(sri->msg());
|
||||
// m_sri->set_token(sri->token());
|
||||
// m_sri->set_data(sri->data());
|
||||
// m_sri->set_email(sri->email());
|
||||
// m_sri->set_phone(sri->phone());
|
||||
|
||||
m_sri = sri;
|
||||
m_bev = bev;
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ void handler::resp(const std::shared_ptr<agreement_request>& request,
|
||||
// response->m_sri.subcommand(), response->m_sri.username(),
|
||||
// response->m_sri.msg(), response->m_sri.token());
|
||||
|
||||
|
||||
auto resp = new Response((mp::MP_TYPE) (request->m_mph->mp_type() + 20),
|
||||
response->m_sri->subcommand(),
|
||||
response->m_sri->account(), response->m_sri->username(),
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <thread>
|
||||
#include "../MS.h"
|
||||
#include "session.h"
|
||||
|
||||
//std::map<uint64_t, userinfo*> session::user_fd;
|
||||
@@ -154,17 +155,27 @@ void session::remove_session(userinfo *user) {
|
||||
|
||||
// 时间轮思路的 定时器
|
||||
void session::timing() {
|
||||
time_wheel.run([&]() {
|
||||
auto pool = MS::localEvPool();
|
||||
pool->add_event_base([&]() {
|
||||
while (true) {
|
||||
while (!session_time_wheel.empty()) {
|
||||
auto wheel = session_time_wheel.begin();
|
||||
|
||||
// printf("session ");
|
||||
// for (const auto &item: wheel->second) {
|
||||
// remove_session(item->bev, item->key);
|
||||
// printf("bev:%p, key:%s, value:%s", item->bev, item->key.c_str(), get_session(item->bev, item->key).value().c_str());
|
||||
// }
|
||||
// printf("\n");
|
||||
|
||||
if (wheel->first > time(nullptr) + 300000) { // 5分钟
|
||||
// 超时5分钟就删除
|
||||
for (const auto &item: wheel->second) {
|
||||
remove_session(item->bev, item->key);
|
||||
}
|
||||
}
|
||||
printf("session time %ld\n", wheel->first);
|
||||
|
||||
printf("timing %ld\n", wheel->first);
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include "CodeController.h"
|
||||
|
||||
void CodeController::run(std::shared_ptr<agreement_request> request, std::shared_ptr<agreement_response> response) {
|
||||
auto sri = new mp::sri();
|
||||
auto* sri = new mp::sri();
|
||||
// 请求验证码
|
||||
if (request->m_body.subcommand() == mp::MP_SUB_TYPE::MP_CODE_EMAIL) {
|
||||
// 生成验证码
|
||||
@@ -13,19 +13,17 @@ void CodeController::run(std::shared_ptr<agreement_request> request, std::shared
|
||||
// 发送验证码至邮
|
||||
bool state = peVerifCodeService.send_email(request->m_body.account(), code);
|
||||
|
||||
// 设置session 字段
|
||||
session->set_session(request->m_bev, "code", code);
|
||||
|
||||
if (state) {
|
||||
// 设置session 字段
|
||||
session->set_session(request->m_bev, "code", code);
|
||||
sri->set_subcommand(mp::MP_SUB_TYPE::MP_CODE_SUCCESS);
|
||||
sri->set_msg("验证码已发送");
|
||||
} else {
|
||||
sri->set_subcommand(mp::MP_SUB_TYPE::MP_CODE_FAIL);
|
||||
sri->set_msg("未知问题,验证码获取失败");
|
||||
session->remove_session(request->m_bev, "code");
|
||||
}
|
||||
}
|
||||
|
||||
response->set(sri, request->m_bev);
|
||||
delete sri;
|
||||
// delete sri;
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@ void UserController::run(std::shared_ptr<agreement_request> request, std::shared
|
||||
}
|
||||
// 注册
|
||||
else if (request->m_mph->mp_type() == mp::MP_REQUEST_REGISTER) {
|
||||
|
||||
auto session_code = session->get_session(request->m_bev, "code");
|
||||
|
||||
auto sri = service.register_(request->m_body.subcommand(),request->m_body.account(),
|
||||
|
||||
@@ -66,7 +66,7 @@ bool UserDB::insert_user_friends(uint64_t account, uint64_t friends) {
|
||||
conn = LinkDB::safe_grab();
|
||||
auto q = conn->query("insert into im_user_friends (account, friends, restrictions) "
|
||||
"values (%1:account, JSON_OBJECT(%2:key, "
|
||||
"JSON_OBJECT('belong_grouping', 0, 'add_time', %3:time, 'add_source', 0)), 0)");
|
||||
"JSON_OBJECT('belong_grouping', 0, 'add_time', %3:time, 'add_source', 0)), '0')");
|
||||
|
||||
q.template_defaults[1] = account;
|
||||
q.template_defaults[2] = friends == 0 ? account : friends;
|
||||
@@ -95,24 +95,24 @@ bool UserDB::remove_user(uint64_t account) {
|
||||
}
|
||||
|
||||
|
||||
bool UserDB::test(uint64_t account) {
|
||||
// 生成附属表字段 并添加自己为好友 以后官方通知通过此接口发送
|
||||
conn = LinkDB::safe_grab();
|
||||
auto q = conn->query("insert into im_user_friends (account, friends, restrictions) "
|
||||
"values (%1:account, JSON_OBJECT(%2:key, "
|
||||
"JSON_OBJECT('belong_grouping', 0, 'add_time', %3:time, 'add_source', 0)), 0)");
|
||||
|
||||
q.template_defaults[1] = account;
|
||||
q.template_defaults[2] = account;
|
||||
q.template_defaults[3] = time(nullptr);
|
||||
q.parse();
|
||||
|
||||
auto ret = q.exec();
|
||||
|
||||
// 放回链接
|
||||
LinkDB::release(conn);
|
||||
return ret;
|
||||
}
|
||||
//bool UserDB::test(uint64_t account) {
|
||||
// // 生成附属表字段 并添加自己为好友 以后官方通知通过此接口发送
|
||||
// conn = LinkDB::safe_grab();
|
||||
// auto q = conn->query("insert into im_user_friends (account, friends, restrictions) "
|
||||
// "values (%1:account, JSON_OBJECT(%2:key, "
|
||||
// "JSON_OBJECT('belong_grouping', 0, 'add_time', %3:time, 'add_source', 0)), 0)");
|
||||
//
|
||||
// q.template_defaults[1] = account;
|
||||
// q.template_defaults[2] = account;
|
||||
// q.template_defaults[3] = time(nullptr);
|
||||
// q.parse();
|
||||
//
|
||||
// auto ret = q.exec();
|
||||
//
|
||||
// // 放回链接
|
||||
// LinkDB::release(conn);
|
||||
// return ret;
|
||||
//}
|
||||
|
||||
|
||||
// 号码池 取号
|
||||
|
||||
@@ -35,7 +35,7 @@ public:
|
||||
// 绑定手机
|
||||
bool bind_phone(uint64_t account, const std::string& phone);
|
||||
|
||||
bool test(uint64_t account);
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ char UserFriendsDB::select_add_type(uint64_t account) {
|
||||
}
|
||||
|
||||
// 搜索好友 搜索用户
|
||||
std::optional<PoUser> UserFriendsDB::select_friends(uint64_t account) {
|
||||
std::optional<PoUser> UserFriendsDB::select_user(uint64_t account) {
|
||||
conn = LinkDB::safe_grab();
|
||||
auto query = conn->query("select * from im_user where account=%1:account");
|
||||
query.template_defaults[1] = account;
|
||||
@@ -47,6 +47,29 @@ std::optional<PoUser> UserFriendsDB::select_friends(uint64_t account) {
|
||||
return user;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询 friend_key 是否 在 account内
|
||||
* @param account
|
||||
* @param friend_key
|
||||
* @return
|
||||
*/
|
||||
bool UserFriendsDB::select_friend(uint64_t account, uint64_t friend_key) {
|
||||
conn = LinkDB::safe_grab();
|
||||
auto query = conn->query("select json_extract(friends, '$.\"%2:friend_key\"') from im_user_friends where account=%1:account");
|
||||
query.template_defaults[1] = account;
|
||||
query.template_defaults[2] = friend_key;
|
||||
query.parse();
|
||||
|
||||
auto ret = query.store();
|
||||
// 放回链接
|
||||
LinkDB::release(conn);
|
||||
if (ret[0][0] == "NULL") {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// 修改好友列表数据 添加好友
|
||||
/// friends :{
|
||||
/// uid :info { }
|
||||
@@ -169,3 +192,4 @@ UserFriendsDB::select_friends_info(uint64_t account, uint64_t friends) {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -16,9 +16,8 @@ public:
|
||||
// 查询添加类型
|
||||
char select_add_type(uint64_t account);
|
||||
// 查询用户 搜索好友
|
||||
std::optional<PoUser> select_friends(uint64_t account);
|
||||
// 添加好友
|
||||
std::tuple<bool, std::string> add_friends(uint64_t source, uint64_t target);
|
||||
std::optional<PoUser> select_user(uint64_t account);
|
||||
bool select_friend(uint64_t account, uint64_t friend_key);
|
||||
// 查询全部好友 好友列表
|
||||
std::optional<std::string> select_friends_all(uint64_t account);
|
||||
std::optional<rapidjson::Document> select_friends_all_json(uint64_t account);
|
||||
@@ -26,6 +25,9 @@ public:
|
||||
std::optional<rapidjson::Document> select_friends_info(uint64_t account, uint64_t friends);
|
||||
// 添加待审核好友列表
|
||||
void insert_friends_to_be_added(uint64_t account, uint64_t friends);
|
||||
|
||||
// 添加好友
|
||||
std::tuple<bool, std::string> add_friends(uint64_t source, uint64_t target);
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ mp::sri *UserFriendsService::friendImProve(mp::body* body) {
|
||||
|
||||
// 搜索
|
||||
if (subcommand == mp::MP_SUB_TYPE::MP_SEARCH_FRIENDS_ACCOUNT) {
|
||||
FetchUser(strtol(body->account().c_str(), nullptr, 0));
|
||||
fetchUser(strtol(body->account().c_str(), nullptr, 0), strtol(body->data().c_str(), nullptr, 0));
|
||||
}
|
||||
// 添加
|
||||
else if (subcommand == mp::MP_SUB_TYPE::MP_ADD_FRIENDS_ACCOUNT) {
|
||||
@@ -31,25 +31,12 @@ mp::sri *UserFriendsService::friendImProve(mp::body* body) {
|
||||
// 获取好友列表
|
||||
else if (subcommand == mp::MP_SUB_TYPE::MP_GET_FRIENDS) {
|
||||
// 此时包传来的 是 客户端 取到用户的帐号
|
||||
FetchUserFriends(strtol(body->account().c_str(), nullptr, 0), body->data());
|
||||
fetchUserFriends(strtol(body->account().c_str(), nullptr, 0), body->data());
|
||||
}
|
||||
|
||||
return sri;
|
||||
}
|
||||
|
||||
|
||||
void UserFriendsService::add_friends(mp::body *body) {
|
||||
auto [state1, msg1] = userFriendsDb.add_friends(body->source(), body->target());
|
||||
auto [state2, msg2] = userFriendsDb.add_friends(body->target(), body->source());
|
||||
if (state1 && state2) {
|
||||
sri->set_subcommand(mp::MP_SUB_TYPE::MP_ADD_SUCCESS);
|
||||
sri->set_msg("添加成功");
|
||||
} else {
|
||||
sri->set_subcommand(mp::MP_SUB_TYPE::MP_ADD_FAIL);
|
||||
sri->set_msg("添加失败,请重试");
|
||||
}
|
||||
}
|
||||
|
||||
// 添加好友函数
|
||||
mp::sri* UserFriendsService::add_contact_person(mp::body *body) {
|
||||
// 查看添加目标的权限类型
|
||||
@@ -76,8 +63,21 @@ mp::sri* UserFriendsService::add_contact_person(mp::body *body) {
|
||||
return sri;
|
||||
}
|
||||
|
||||
// 查账户好友名单
|
||||
void UserFriendsService::FetchUserFriends(uint64_t account, const std::string &data) {
|
||||
// 直接添加好友
|
||||
void UserFriendsService::add_friends(mp::body *body) {
|
||||
auto [state1, msg1] = userFriendsDb.add_friends(body->source(), body->target());
|
||||
auto [state2, msg2] = userFriendsDb.add_friends(body->target(), body->source());
|
||||
if (state1 && state2) {
|
||||
sri->set_subcommand(mp::MP_SUB_TYPE::MP_ADD_SUCCESS);
|
||||
sri->set_msg("添加成功");
|
||||
} else {
|
||||
sri->set_subcommand(mp::MP_SUB_TYPE::MP_ADD_FAIL);
|
||||
sri->set_msg("添加失败,请重试");
|
||||
}
|
||||
}
|
||||
|
||||
// 查账户好友名单 列表
|
||||
void UserFriendsService::fetchUserFriends(uint64_t account, const std::string &data) {
|
||||
sri_clear();
|
||||
// page
|
||||
uint8_t page_begin = data.c_str()[0];
|
||||
@@ -91,11 +91,23 @@ void UserFriendsService::FetchUserFriends(uint64_t account, const std::string &d
|
||||
}
|
||||
}
|
||||
|
||||
/// MP_REQUEST_FRIENDS MP_SEARCH_FRIENDS_ACCOUNT
|
||||
// 搜索指定账户信息 搜索可添加好友
|
||||
void UserFriendsService::FetchUser(uint64_t account) {
|
||||
auto userinfo = userFriendsDb.select_friends(account);
|
||||
/**
|
||||
* MP_REQUEST_FRIENDS MP_SEARCH_FRIENDS_ACCOUNT
|
||||
* 搜索指定账户信息 搜索可添加好友
|
||||
* @param user 带查用户
|
||||
* @param account user是否为account的好友
|
||||
*/
|
||||
void UserFriendsService::fetchUser(uint64_t user, uint64_t account) {
|
||||
// 查询用户存在与否
|
||||
auto userinfo = isExistUser(user);
|
||||
auto is_friend = isExistFriend(user, account);
|
||||
|
||||
if (userinfo.has_value()) {
|
||||
if (is_friend) {
|
||||
sri->set_msg("此账户已是的好友");
|
||||
sri->set_subcommand(mp::MP_SUB_TYPE::MP_SEARCH_FAIL);
|
||||
return;
|
||||
}
|
||||
sri->set_username(userinfo.value().username);
|
||||
sri->set_account(userinfo.value().account);
|
||||
sri->set_email(userinfo.value().email);
|
||||
@@ -107,6 +119,14 @@ void UserFriendsService::FetchUser(uint64_t account) {
|
||||
}
|
||||
}
|
||||
|
||||
bool UserFriendsService::isExistFriend(uint64_t user, uint64_t account) {
|
||||
return userFriendsDb.select_friend(account, user);
|
||||
}
|
||||
|
||||
std::optional<PoUser> UserFriendsService::isExistUser(uint64_t user) {
|
||||
return userFriendsDb.select_user(user);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -16,8 +16,14 @@ public:
|
||||
public:
|
||||
// 唯一对开接口
|
||||
mp::sri* friendImProve(mp::body* body);
|
||||
void FetchUserFriends(uint64_t account, const std::string& data);
|
||||
void FetchUser(uint64_t account);
|
||||
// 获取用户的好友列表
|
||||
void fetchUserFriends(uint64_t account, const std::string& data);
|
||||
// 查询用户是否存在 并 判断是否为 已为好友
|
||||
void fetchUser(uint64_t user, uint64_t account);
|
||||
// 查看一个账户是否为此账户好友
|
||||
bool isExistFriend(uint64_t user, uint64_t account);
|
||||
// 查看一个账户是存在
|
||||
std::optional<PoUser> isExistUser(uint64_t user);
|
||||
|
||||
private:
|
||||
mp::sri* add_contact_person(mp::body* body);
|
||||
|
||||
Reference in New Issue
Block a user