Skip to content
This repository was archived by the owner on Aug 2, 2020. It is now read-only.

Commit ff805c4

Browse files
committed
Fix lifecycle meta event bug
1 parent 16bad5f commit ff805c4

File tree

5 files changed

+70
-28
lines changed

5 files changed

+70
-28
lines changed

src/cqhttp/core/event.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,22 @@ namespace cqhttp {
3333
emit_event(event, data, &Application::on_meta_event);
3434
}
3535

36-
void emit_lifecycle_meta_event(const MetaEvent::SubType sub_type) {
36+
void emit_lifecycle_meta_event(const MetaEvent::SubType sub_type,
37+
const LifecycleMetaEvent::_PostMethod post_method) {
3738
LifecycleMetaEvent e;
3839
e.sub_type = sub_type;
40+
switch (sub_type) {
41+
case MetaEvent::LIFECYCLE_ENABLE:
42+
case MetaEvent::LIFECYCLE_DISABLE:
43+
e._post_method = LifecycleMetaEvent::_PostMethod::HTTP;
44+
break;
45+
case MetaEvent::LIFECYCLE_CONNECT:
46+
e._post_method = LifecycleMetaEvent::_PostMethod::WEBSOCKET;
47+
break;
48+
default:
49+
e._post_method = post_method;
50+
break;
51+
}
3952
emit_event(e);
4053
}
4154

src/cqhttp/core/event.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ namespace cqhttp {
4040

4141
struct LifecycleMetaEvent final : MetaEvent {
4242
LifecycleMetaEvent() : MetaEvent() { meta_event_type = LIFECYCLE; }
43+
44+
enum class _PostMethod : int { ALL, HTTP, WEBSOCKET, NONE };
45+
_PostMethod _post_method = _PostMethod::ALL;
4346
};
4447

4548
inline void to_json(json &j, const LifecycleMetaEvent &e) {
@@ -60,6 +63,7 @@ namespace cqhttp {
6063
{"post_type", "meta_event"},
6164
{"meta_event_type", e.meta_event_type},
6265
{"sub_type", sub_type_str},
66+
{"_post_method", static_cast<int>(e._post_method)},
6367
};
6468
}
6569

@@ -89,6 +93,8 @@ namespace cqhttp {
8993
emit_event(event, json(event));
9094
}
9195

92-
void emit_lifecycle_meta_event(const MetaEvent::SubType sub_type);
96+
void emit_lifecycle_meta_event(
97+
const MetaEvent::SubType sub_type,
98+
const LifecycleMetaEvent::_PostMethod post_method = LifecycleMetaEvent::_PostMethod::ALL);
9399
void emit_heartbeat_meta_event(const json status, const int64_t interval);
94100
} // namespace cqhttp

src/cqhttp/plugins/web/http.cpp

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -323,40 +323,49 @@ namespace cqhttp::plugins {
323323
}
324324

325325
void Http::hook_after_event(EventContext<cq::Event> &ctx) {
326-
if (!post_url_.empty()) {
327-
logging::debug(TAG, u8"开始通过 HTTP 上报事件");
328-
const auto resp = post_json(post_url_, ctx.data, secret_, post_timeout_);
326+
if (post_url_.empty()) {
327+
ctx.next();
328+
return;
329+
}
330+
if (ctx.data["post_type"] == "meta_event" && ctx.data["meta_event_type"] == "lifecycle"
331+
&& ctx.data["_post_method"] != static_cast<int>(LifecycleMetaEvent::_PostMethod::ALL)
332+
&& ctx.data["_post_method"] != static_cast<int>(LifecycleMetaEvent::_PostMethod::HTTP)) {
333+
ctx.next();
334+
return;
335+
}
336+
337+
logging::debug(TAG, u8"开始通过 HTTP 上报事件");
338+
const auto resp = post_json(post_url_, ctx.data, secret_, post_timeout_);
329339

330-
if (resp.status_code == 0) {
331-
logging::warning(TAG, u8"HTTP 上报地址 " + post_url_ + u8" 无法访问");
340+
if (resp.status_code == 0) {
341+
logging::warning(TAG, u8"HTTP 上报地址 " + post_url_ + u8" 无法访问");
342+
} else {
343+
const auto log_msg = u8"通过 HTTP 上报数据到 " + post_url_ + (resp.ok() ? u8" 成功" : u8" 失败")
344+
+ u8",状态码:" + to_string(resp.status_code);
345+
if (resp.ok()) {
346+
logging::info_success(TAG, log_msg);
332347
} else {
333-
const auto log_msg = u8"通过 HTTP 上报数据到 " + post_url_ + (resp.ok() ? u8" 成功" : u8" 失败")
334-
+ u8",状态码:" + to_string(resp.status_code);
335-
if (resp.ok()) {
336-
logging::info_success(TAG, log_msg);
337-
} else {
338-
logging::warning(TAG, log_msg);
339-
}
348+
logging::warning(TAG, log_msg);
340349
}
350+
}
341351

342-
if (resp.ok() && !resp.body.empty()) {
343-
logging::debug(TAG, u8"收到响应 " + resp.body);
352+
if (resp.ok() && !resp.body.empty()) {
353+
logging::debug(TAG, u8"收到响应 " + resp.body);
344354

345-
const auto resp_payload = resp.get_json();
346-
if (resp_payload.is_object()) {
347-
const utils::JsonEx params = resp_payload;
355+
const auto resp_payload = resp.get_json();
356+
if (resp_payload.is_object()) {
357+
const utils::JsonEx params = resp_payload;
348358

349-
// note here that the ctx.data object was processed by backward_compatibility plugin,
350-
// but now that the ".handle_quick_operation" action can handle legacy data format,
351-
// it's ok here to use ctx.data directly
352-
call_action(".handle_quick_operation", {{"context", ctx.data}, {"operation", params.raw}});
359+
// note here that the ctx.data object was processed by backward_compatibility plugin,
360+
// but now that the ".handle_quick_operation" action can handle legacy data format,
361+
// it's ok here to use ctx.data directly
362+
call_action(".handle_quick_operation", {{"context", ctx.data}, {"operation", params.raw}});
353363

354-
if (params.get_bool("block", false)) {
355-
ctx.event.block();
356-
}
357-
} else {
358-
logging::debug(TAG, u8"上报响应不是有效的 JSON,已忽略");
364+
if (params.get_bool("block", false)) {
365+
ctx.event.block();
359366
}
367+
} else {
368+
logging::debug(TAG, u8"上报响应不是有效的 JSON,已忽略");
360369
}
361370
}
362371

src/cqhttp/plugins/web/websocket.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ namespace cqhttp::plugins {
9292
}
9393

9494
void WebSocket::hook_after_event(EventContext<cq::Event> &ctx) {
95+
if (ctx.data["post_type"] == "meta_event" && ctx.data["meta_event_type"] == "lifecycle"
96+
&& ctx.data["_post_method"] != static_cast<int>(LifecycleMetaEvent::_PostMethod::ALL)
97+
&& ctx.data["_post_method"] != static_cast<int>(LifecycleMetaEvent::_PostMethod::WEBSOCKET)) {
98+
ctx.next();
99+
return;
100+
}
101+
95102
static const auto path_regex = regex("^(/|/event/?)$");
96103
if (started_) {
97104
logging::debug(TAG, u8"开始通过 WebSocket 服务端推送事件");

src/cqhttp/plugins/web/websocket_reverse.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ namespace cqhttp::plugins {
113113
}
114114

115115
void WebSocketReverse::hook_after_event(EventContext<cq::Event> &ctx) {
116+
if (ctx.data["post_type"] == "meta_event" && ctx.data["meta_event_type"] == "lifecycle"
117+
&& ctx.data["_post_method"] != static_cast<int>(LifecycleMetaEvent::_PostMethod::ALL)
118+
&& ctx.data["_post_method"] != static_cast<int>(LifecycleMetaEvent::_PostMethod::WEBSOCKET)) {
119+
ctx.next();
120+
return;
121+
}
122+
116123
if (event_) {
117124
event_->push_event(ctx.data);
118125
}

0 commit comments

Comments
 (0)