Add update notice and require release build secrets
All checks were successful
Release App / release-app (push) Successful in 58s
All checks were successful
Release App / release-app (push) Successful in 58s
This commit is contained in:
87
src/app.cpp
87
src/app.cpp
@@ -1,5 +1,9 @@
|
||||
#include "app.h"
|
||||
|
||||
#include <array>
|
||||
#include <cstdio>
|
||||
#include <future>
|
||||
|
||||
#include <curses.h>
|
||||
|
||||
#include "build_config.h"
|
||||
@@ -14,6 +18,66 @@ bool is_truthy_env_value(const std::string &value) {
|
||||
value == "YES" || value == "on" || value == "ON";
|
||||
}
|
||||
|
||||
std::string shell_quote(const std::string &value) {
|
||||
std::string quoted = "'";
|
||||
for (char ch : value) {
|
||||
if (ch == '\'') {
|
||||
quoted += "'\\''";
|
||||
} else {
|
||||
quoted.push_back(ch);
|
||||
}
|
||||
}
|
||||
quoted.push_back('\'');
|
||||
return quoted;
|
||||
}
|
||||
|
||||
std::string run_command_capture(const std::string &command) {
|
||||
FILE *pipe = popen(command.c_str(), "r");
|
||||
if (pipe == nullptr) {
|
||||
return {};
|
||||
}
|
||||
|
||||
std::string output;
|
||||
std::array<char, 4096> buffer{};
|
||||
while (std::fgets(buffer.data(), static_cast<int>(buffer.size()), pipe) != nullptr) {
|
||||
output += buffer.data();
|
||||
}
|
||||
if (pclose(pipe) != 0) {
|
||||
return {};
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
std::optional<std::string> fetch_update_notice() {
|
||||
static constexpr const char *kLatestReleaseApiUrl =
|
||||
"https://git.mshq.dev/api/v1/repos/AxiFisk/shinoa/releases/tags/latest";
|
||||
|
||||
if (std::string(TELEGRAM_TUI_BUILD_COMMIT).empty()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
const std::string response =
|
||||
run_command_capture("curl -fsSL " + shell_quote(kLatestReleaseApiUrl) + " 2>/dev/null");
|
||||
if (response.empty()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
try {
|
||||
const json release = json::parse(response, nullptr, true, true);
|
||||
const std::string target_commit = safe_string(release, "target_commitish");
|
||||
if (target_commit.empty()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const std::string current_commit = TELEGRAM_TUI_BUILD_COMMIT;
|
||||
if (target_commit == current_commit || target_commit.rfind(current_commit, 0) == 0) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return std::string("Update available");
|
||||
} catch (const json::exception &) {
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
App::App() {
|
||||
@@ -49,6 +113,8 @@ App::App() {
|
||||
if (use_test_dc_) {
|
||||
status_line_ = "Starting TDLib in test DC mode...";
|
||||
}
|
||||
|
||||
start_update_check();
|
||||
}
|
||||
|
||||
int App::run() {
|
||||
@@ -80,6 +146,27 @@ int App::run() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void App::start_update_check() {
|
||||
update_check_future_ =
|
||||
std::async(std::launch::async, []() { return fetch_update_notice(); });
|
||||
}
|
||||
|
||||
bool App::refresh_update_check_result() {
|
||||
if (!update_check_future_.valid()) {
|
||||
return false;
|
||||
}
|
||||
if (update_check_future_.wait_for(std::chrono::seconds(0)) != std::future_status::ready) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto notice = update_check_future_.get();
|
||||
if (notice == update_notice_) {
|
||||
return false;
|
||||
}
|
||||
update_notice_ = notice.value_or(std::string());
|
||||
return true;
|
||||
}
|
||||
|
||||
std::optional<std::int64_t> App::highlighted_chat_id() const {
|
||||
if (sorted_chat_ids_.empty()) {
|
||||
return std::nullopt;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <cstdint>
|
||||
#include <chrono>
|
||||
#include <filesystem>
|
||||
#include <future>
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <tuple>
|
||||
@@ -38,6 +39,8 @@ class App {
|
||||
|
||||
void init_curses();
|
||||
void shutdown_curses();
|
||||
void start_update_check();
|
||||
bool refresh_update_check_result();
|
||||
bool process_updates();
|
||||
void handle_td_object(const json &object);
|
||||
void handle_authorization_state();
|
||||
@@ -197,6 +200,7 @@ class App {
|
||||
std::string input_prompt_;
|
||||
std::string input_buffer_;
|
||||
std::string status_line_ = "Starting TDLib...";
|
||||
std::string update_notice_;
|
||||
std::string attachment_preview_graphics_data_;
|
||||
std::string attachment_viewer_title_;
|
||||
std::string attachment_preview_signature_;
|
||||
@@ -217,6 +221,7 @@ class App {
|
||||
std::size_t attachment_viewer_frame_index_ = 0;
|
||||
std::string attachment_viewer_send_caption_;
|
||||
std::vector<std::int64_t> forward_message_ids_;
|
||||
std::future<std::optional<std::string>> update_check_future_;
|
||||
};
|
||||
|
||||
} // namespace telegram_tui
|
||||
|
||||
@@ -96,6 +96,18 @@ void App::draw() {
|
||||
mvprintw(header_y, 1, "%s", header_label.c_str());
|
||||
const std::string auth_label = authorized_ ? "ready" : current_auth_label();
|
||||
const int auth_x = std::max(1, width - static_cast<int>(auth_label.size()) - 2);
|
||||
if (!update_notice_.empty()) {
|
||||
const std::string centered_notice =
|
||||
truncate_to_width(update_notice_, std::max(1, width - 4));
|
||||
const int notice_x =
|
||||
std::max(1, (width - static_cast<int>(centered_notice.size())) / 2);
|
||||
if (notice_x > 1 + static_cast<int>(header_label.size()) &&
|
||||
notice_x + static_cast<int>(centered_notice.size()) < auth_x - 1) {
|
||||
attron(A_BOLD);
|
||||
mvprintw(header_y, notice_x, "%s", centered_notice.c_str());
|
||||
attroff(A_BOLD);
|
||||
}
|
||||
}
|
||||
mvprintw(header_y, auth_x, "%s", auth_label.c_str());
|
||||
attroff(A_REVERSE);
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ std::string format_download_progress(std::int64_t downloaded_size, std::int64_t
|
||||
} // namespace
|
||||
|
||||
bool App::process_updates() {
|
||||
bool changed = false;
|
||||
bool changed = refresh_update_check_result();
|
||||
while (true) {
|
||||
auto update = td_.receive(0.0);
|
||||
if (!update.has_value()) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#define TELEGRAM_TUI_BUILD_COMMIT "@TELEGRAM_TUI_BUILD_COMMIT@"
|
||||
#define TELEGRAM_TUI_BUILD_VERSION "@TELEGRAM_TUI_BUILD_VERSION@"
|
||||
#define TELEGRAM_TUI_BUILD_API_ID "@TELEGRAM_TUI_BUILD_API_ID@"
|
||||
#define TELEGRAM_TUI_BUILD_API_HASH "@TELEGRAM_TUI_BUILD_API_HASH@"
|
||||
|
||||
Reference in New Issue
Block a user