Skip to content

Commit

Permalink
refactor: adapt eventId to API v2
Browse files Browse the repository at this point in the history
  • Loading branch information
wyq777x authored and Serein207 committed Oct 24, 2024
1 parent 969d8fb commit fd68dc2
Show file tree
Hide file tree
Showing 18 changed files with 108 additions and 65 deletions.
2 changes: 1 addition & 1 deletion src/Controller/Convert.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ slint::SharedString firstUnicode(const std::string& str) {
EventStruct from(const EventEntity& entity) {
boost::algorithm::trim(entity.summary);
return {
.id = entity.id,
.id = slint::SharedString(entity.id),
.summary = slint::SharedString(entity.summary),
.summary_abbr = details::firstUnicode(entity.summary),
.description = slint::SharedString(entity.description),
Expand Down
46 changes: 39 additions & 7 deletions src/Controller/Core/AccountManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,19 +179,51 @@ void AccountManager::loadConfig() {
setLoginState(false);
auto [year, month, day] = evento::account.expire.date;
auto [hour, minute, second, _] = evento::account.expire.time;
std::tm t = {year, month, day, hour, minute, second};
spdlog::info("Loading config with date: {}-{}-{} and time: {}:{}:{}",
year,
month,
day,
hour,
minute,
second);

if (year < 1900 || month < 1 || month > 12 || day < 1 || day > 31 || hour < 0 || hour > 23
|| minute < 0 || minute > 59 || second < 0 || second > 60) {
spdlog::error("Invalid date or time values");
return;
}
std::tm t = {};
t.tm_year = year - 1900;
t.tm_mon = month - 1;
t.tm_mday = day;
t.tm_hour = hour;
t.tm_min = minute;
t.tm_sec = second;
t.tm_isdst = -1;

time_t time = std::mktime(&t);
if (time == -1) {
spdlog::error("Failed to convert time to time_t");
return;
}

_expiredTime = std::chrono::system_clock::from_time_t(std::mktime(&t));
_expiredTime = std::chrono::system_clock::from_time_t(time);
_userInfo.linkId = evento::account.userId;
}

void AccountManager::saveConfig() {
auto expire = std::chrono::system_clock::to_time_t(_expiredTime);
auto expireTm = *std::localtime(&expire);
evento::account.expire
= toml::date_time{toml::date{expireTm.tm_year + 1900, expireTm.tm_mon + 1, expireTm.tm_mday},
toml::time{expireTm.tm_hour, expireTm.tm_min, expireTm.tm_sec}};
evento::account.userId = _userInfo.linkId;
if (auto expireTm = std::localtime(&expire)) {
evento::account.expire = toml::date_time{toml::date{expireTm->tm_year + 1900,
expireTm->tm_mon + 1,
expireTm->tm_mday},
toml::time{expireTm->tm_hour,
expireTm->tm_min,
expireTm->tm_sec}};
evento::account.userId = _userInfo.linkId;
} else {
spdlog::error("Failed to convert time to time_t");
}
}

void AccountManager::setKeychainRefreshToken(const std::string& refreshToken) const {
Expand Down
24 changes: 12 additions & 12 deletions src/Controller/View/DetailPage.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "app.h"
#include <Controller/AsyncExecutor.hh>
#include <Controller/Convert.h>
#include <Controller/UiBridge.h>
Expand All @@ -19,13 +18,14 @@ void DetailPage::onCreate() {
self->on_load_feedback([&self = *this]() { self.loadFeedback(); });
self->on_load_event([&self = *this]() { self.loadEvent(); });
self->on_feedback([&self = *this](int rate, slint::SharedString content) {
self.feedbackEvent(self->get_event_model().id, rate, content.data());
self.feedbackEvent(self->get_event_model().id.data(), rate, content.data());
});
self->on_check_in([&self = *this](slint::SharedString checkInCode) {
self.checkIn(self->get_event_model().id, checkInCode.data());
self.checkIn(self->get_event_model().id.data(), checkInCode.data());
});
self->on_subscribe([&self = *this](bool subscribe) {
self.subscribe(self->get_event_model().id.data(), subscribe);
});
self->on_subscribe(
[&self = *this](bool subscribe) { self.subscribe(self->get_event_model().id, subscribe); });
}

void DetailPage::onShow() {
Expand All @@ -39,7 +39,7 @@ void DetailPage::onShow() {

void DetailPage::loadEvent() {
auto& self = *this;
executor()->asyncExecute(networkClient()->getEventById(self->get_event_model().id),
executor()->asyncExecute(networkClient()->getEventById(self->get_event_model().id.data()),
[&self = *this](Result<EventQueryRes> result) {
if (result.isErr()) {
self.bridge.getMessageManager()
Expand All @@ -61,7 +61,7 @@ void DetailPage::loadEvent() {
void DetailPage::loadFeedback() {
auto& self = *this;
self->set_state(PageState::Loading);
executor()->asyncExecute(networkClient()->getUserFeedback(self->get_event_model().id),
executor()->asyncExecute(networkClient()->getUserFeedback(self->get_event_model().id.data()),
[&self = *this](Result<std::optional<FeedbackEntity>> result) {
if (result.isErr()) {
self->set_state(PageState::Error);
Expand All @@ -75,7 +75,7 @@ void DetailPage::loadFeedback() {
});
}

void DetailPage::checkIn(int eventId, std::string checkInCode) {
void DetailPage::checkIn(eventId_t eventId, std::string checkInCode) {
auto& self = *this;
executor()->asyncExecute(
networkClient()->checkInEvent(eventId, checkInCode), [&self = *this](Result<bool> result) {
Expand Down Expand Up @@ -103,9 +103,9 @@ void DetailPage::checkIn(int eventId, std::string checkInCode) {
});
}

void DetailPage::subscribe(int eventId, bool subscribe) {
void DetailPage::subscribe(std::string eventId_t, bool subscribe) {
auto& self = *this;
executor()->asyncExecute(networkClient()->subscribeEvent(eventId, subscribe),
executor()->asyncExecute(networkClient()->subscribeEvent(eventId_t, subscribe),
[&self = *this, subscribe](Result<bool> result) {
if (result.isErr()) {
self.bridge.getMessageManager()
Expand All @@ -130,10 +130,10 @@ void DetailPage::subscribe(int eventId, bool subscribe) {
});
}

void DetailPage::feedbackEvent(int eventId, int rate, std::string content) {
void DetailPage::feedbackEvent(std::string eventId_t, int rate, std::string content) {
auto& self = *this;
executor()
->asyncExecute(networkClient()->addUserFeedback(eventId, rate, content),
->asyncExecute(networkClient()->addUserFeedback(eventId_t, rate, content),
[&self = *this, rate, content](Result<bool> result) {
if (result.isErr()) {
self.bridge.getMessageManager().showMessage(result.unwrapErr().what(),
Expand Down
6 changes: 3 additions & 3 deletions src/Controller/View/DetailPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class DetailPage : public BasicView, private GlobalAgent<DetailPageBridge> {

void loadEvent();
void loadFeedback();
void checkIn(int eventId, std::string checkInCode);
void subscribe(int eventId, bool subscribe);
void feedbackEvent(int eventId, int rate, std::string content);
void checkIn(eventId_t eventId, std::string checkInCode);
void subscribe(eventId_t eventId, bool subscribe);
void feedbackEvent(eventId_t eventId, int rate, std::string content);
};

EVENTO_UI_END
6 changes: 3 additions & 3 deletions src/Controller/View/HistoryPage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ void HistoryPage::onCreate() {

self->on_load_events(
[this](int pageIndex, int size) { loadHistoryEvents(pageIndex + 1, size); });
self->on_comment([this](int eventId, int rating, slint::SharedString content) {
feedbackEvent(eventId, rating, content.data());
self->on_comment([this](slint::SharedString eventId, int rating, slint::SharedString content) {
feedbackEvent(eventId.data(), rating, content.data());
});
self->on_navigate_to_detail([this](EventStruct eventStruct) {
spdlog::debug("navigate to DetailPage, current event is {}", eventStruct.summary.data());
Expand Down Expand Up @@ -79,7 +79,7 @@ Task<Result<std::vector<EventFeedbackStruct>>> HistoryPage::loadHistoryEventsTas
co_return res;
}

void HistoryPage::feedbackEvent(int eventId, int rating, std::string content) {
void HistoryPage::feedbackEvent(eventId_t eventId, int rating, std::string content) {
auto& self = *this;
auto trans = [](const auto& e) { return e.id; };
executor()
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/View/HistoryPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class HistoryPage : public BasicView, private GlobalAgent<HistoryPageBridge> {

Task<Result<std::vector<EventFeedbackStruct>>> loadHistoryEventsTask(int page, int size);

void feedbackEvent(int eventId, int rating, std::string content);
void feedbackEvent(eventId_t eventId, int rating, std::string content);

std::vector<EventFeedbackStruct> eventFeedbacks;
};
Expand Down
6 changes: 3 additions & 3 deletions src/Controller/View/MyEventPage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void MyEventPage::refreshUiModel(Result<EventQueryRes> result) {
if (evento::settings.noticeBegin)
for (auto const& entity : models[(int) EventState::SigningUp]) {
auto time = parseIso8601Utc(entity.start.c_str());
ipc()->showOrUpdateMessage(entity.id,
ipc()->showOrUpdateMessage(entity.id.data(),
std::format("活动 {} 还有 15 分钟就要开始了,记得参加哦",
entity.summary),
std::chrono::system_clock::from_time_t(time) - 15min);
Expand All @@ -88,14 +88,14 @@ void MyEventPage::refreshUiModel(Result<EventQueryRes> result) {
if (evento::settings.noticeEnd)
for (auto const& entity : models[(int) EventState::Active]) {
auto time = parseIso8601Utc(entity.end.c_str());
ipc()->showOrUpdateMessage(entity.id,
ipc()->showOrUpdateMessage(entity.id.data(),
std::format("活动 {} 结束了,记得反馈哦", entity.summary),
std::chrono::system_clock::from_time_t(time));
}

for (auto const& entity : models[(int) EventState::Cancelled]) {
auto time = parseIso8601Utc(entity.start.c_str());
ipc()->cancelMessage(entity.id);
ipc()->cancelMessage(entity.id.data());
}

self->set_not_started_model(convert::from(models[(int) EventState::SigningUp]));
Expand Down
8 changes: 4 additions & 4 deletions src/Infrastructure/IPC/SocketClient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ void SocketClient::exitTray() {
spdlog::info("Tray exited with code: {}", exit_code);
}

void SocketClient::showOrUpdateMessage(int messageId,
void SocketClient::showOrUpdateMessage(messageId_t messageId,
std::string const& message,
std::chrono::system_clock::time_point const& time) {
if (messageId == 0) {
if (messageId.empty()) {
spdlog::warn("Invalid message id");
return;
}
Expand All @@ -94,8 +94,8 @@ void SocketClient::showOrUpdateMessage(int messageId,
AsyncExecutor::Delay | AsyncExecutor::Once);
}

void SocketClient::cancelMessage(int messageId) {
if (messageId == 0) {
void SocketClient::cancelMessage(messageId_t messageId) {
if (messageId.empty()) {
spdlog::warn("Invalid message id");
return;
}
Expand Down
7 changes: 4 additions & 3 deletions src/Infrastructure/IPC/SocketClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace evento {

using messageId_t = std::string;
namespace net = boost::asio;

class SocketClient {
Expand All @@ -20,11 +21,11 @@ class SocketClient {
void startTray();
void exitTray();

void showOrUpdateMessage(int messageId,
void showOrUpdateMessage(messageId_t messageId,
std::string const& message,
std::chrono::system_clock::time_point const& time);

void cancelMessage(int messageId);
void cancelMessage(messageId_t messageId);
void deleteAllMessage();

struct MessageType {
Expand All @@ -48,7 +49,7 @@ class SocketClient {
std::unique_ptr<net::ip::tcp::socket> _socket;
std::unordered_map<std::string_view, std::function<void()>> _actions;

std::unordered_map<int, std::string> _messageMap;
std::unordered_map<messageId_t, std::string> _messageMap;
};

SocketClient* ipc();
Expand Down
18 changes: 10 additions & 8 deletions src/Infrastructure/Network/NetworkClient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ Task<Result<EventQueryRes>> NetworkClient::getDepartmentEventList(
co_return Ok(entity);
}

Task<Result<EventQueryRes>> NetworkClient::getEventById(int eventId) {
Task<Result<EventQueryRes>> NetworkClient::getEventById(eventId_t eventId) {
auto result = co_await this->request<api::Evento>(http::verb::get,
endpoint("/v2/client/event/query",
{{"id", std::to_string(eventId)}}),
{{"id", eventId}}),
{},
0min);
if (result.isErr())
Expand Down Expand Up @@ -205,7 +205,7 @@ Task<Result<EventQueryRes>> NetworkClient::getEventList(
co_return Ok(entity);
}

Task<Result<AttachmentEntity>> NetworkClient::getAttachment(int eventId) {
Task<Result<AttachmentEntity>> NetworkClient::getAttachment(eventId_t eventId) {
auto result = co_await this
->request<api::Evento>(http::verb::get,
endpoint(std::format("/v2/client/event/{}/attachments",
Expand All @@ -224,7 +224,7 @@ Task<Result<AttachmentEntity>> NetworkClient::getAttachment(int eventId) {
}

Task<Result<std::optional<FeedbackEntity>>> NetworkClient::getUserFeedback(
int eventId, std::chrono::steady_clock::duration cacheTtl) {
eventId_t eventId, std::chrono::steady_clock::duration cacheTtl) {
auto result = co_await this
->request<api::Evento>(http::verb::get,
endpoint(std::format("/v2/client/event/{}/feedback",
Expand All @@ -249,7 +249,9 @@ Task<Result<std::optional<FeedbackEntity>>> NetworkClient::getUserFeedback(
co_return Ok(entity);
}

Task<Result<bool>> NetworkClient::addUserFeedback(int eventId, int rating, std::string content) {
Task<Result<bool>> NetworkClient::addUserFeedback(eventId_t eventId,
int rating,
std::string content) {
auto result = co_await this->request<api::Evento>(
http::verb::post,
endpoint(std::format("/v2/client/event/{}/feedback", eventId),
Expand All @@ -264,7 +266,7 @@ Task<Result<bool>> NetworkClient::addUserFeedback(int eventId, int rating, std::
co_return Err(Error(Error::Data, "response data type error"));
}

Task<Result<bool>> NetworkClient::checkInEvent(int eventId, std::string code) {
Task<Result<bool>> NetworkClient::checkInEvent(eventId_t eventId, std::string code) {
auto result = co_await this->request<api::Evento>(
http::verb::post,
endpoint(std::format("/v2/client/event/{}/check-in", eventId), {{"code", code}}));
Expand All @@ -278,7 +280,7 @@ Task<Result<bool>> NetworkClient::checkInEvent(int eventId, std::string code) {
co_return Err(Error(Error::Data, "response data type error"));
}

Task<Result<bool>> NetworkClient::subscribeEvent(int eventId, bool subscribe) {
Task<Result<bool>> NetworkClient::subscribeEvent(eventId_t eventId, bool subscribe) {
std::string subscribeStr = subscribe ? "true" : "false";
auto result = co_await this
->request<api::Evento>(http::verb::post,
Expand Down Expand Up @@ -374,7 +376,7 @@ Task<Result<SlideEntityList>> NetworkClient::getHomeSlide(
}

Task<Result<SlideEntityList>> NetworkClient::getEventSlide(
int eventId, std::chrono::steady_clock::duration cacheTtl) {
eventId_t eventId, std::chrono::steady_clock::duration cacheTtl) {
auto result = co_await this->request<api::Evento>(http::verb::get,
endpoint(
std::format("/v2/client/event/{}/slide",
Expand Down
14 changes: 7 additions & 7 deletions src/Infrastructure/Network/NetworkClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,21 @@ class NetworkClient {
int size = 10,
std::chrono::steady_clock::duration cacheTtl = 1min);

Task<Result<EventQueryRes>> getEventById(int eventId);
Task<Result<EventQueryRes>> getEventById(eventId_t eventId);

Task<Result<EventQueryRes>> getEventList(std::initializer_list<urls::param> params,
std::chrono::steady_clock::duration cacheTtl = 1min);

Task<Result<AttachmentEntity>> getAttachment(int eventId);
Task<Result<AttachmentEntity>> getAttachment(eventId_t eventId);

Task<Result<std::optional<FeedbackEntity>>> getUserFeedback(
int eventId, std::chrono::steady_clock::duration cacheTtl = 1min);
eventId_t eventId, std::chrono::steady_clock::duration cacheTtl = 1min);

Task<Result<bool>> addUserFeedback(int eventId, int rating, std::string content);
Task<Result<bool>> addUserFeedback(eventId_t eventId, int rating, std::string content);

Task<Result<bool>> checkInEvent(int eventId, std::string code);
Task<Result<bool>> checkInEvent(eventId_t eventId, std::string code);

Task<Result<bool>> subscribeEvent(int eventId, bool subscribe);
Task<Result<bool>> subscribeEvent(eventId_t eventId, bool subscribe);

Task<Result<bool>> subscribeDepartment(std::string larkDepartment, bool subscribe);

Expand All @@ -95,7 +95,7 @@ class NetworkClient {

Task<Result<SlideEntityList>> getHomeSlide(std::chrono::steady_clock::duration cacheTtl = 1min);

Task<Result<SlideEntityList>> getEventSlide(int eventId,
Task<Result<SlideEntityList>> getEventSlide(eventId_t eventId,
std::chrono::steady_clock::duration cacheTtl = 1min);

Task<Result<DepartmentEntityList>> getDepartmentList(
Expand Down
2 changes: 1 addition & 1 deletion src/Infrastructure/Network/Response/AttachmentEntity.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace evento {

struct AttachmentEntity {
int id;
int eventId;
std::string eventId;
std::string url;

NLOHMANN_DEFINE_TYPE_INTRUSIVE(AttachmentEntity, id, eventId, url);
Expand Down
4 changes: 3 additions & 1 deletion src/Infrastructure/Network/Response/EventEntity.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
#include <string>
namespace evento {

using eventId_t = std::string;

struct EventEntity {
int id;
eventId_t id;
mutable std::string summary;
std::string description;
std::string start;
Expand Down
Loading

0 comments on commit fd68dc2

Please sign in to comment.