Linux + iPhone Continuity / iMessage / SMS
feat: "Copy Code" OTP notification marks message as read
Zack Bartelc7387cf8a576
diff --git a/src/core/include/tether/bluetooth/connection.hpp b/src/core/include/tether/bluetooth/connection.hppindex 0568bb5..2ad645d 100644--- a/src/core/include/tether/bluetooth/connection.hpp+++ b/src/core/include/tether/bluetooth/connection.hpp@@ -13,6 +13,7 @@#include <mutex>#include <nlohmann/json.hpp>#include <string>+#include <vector>namespace tether::bluetooth {@@ -100,10 +101,13 @@ namespace tether::bluetooth {MessageStore& message_store();std::mutex& message_store_mutex();- // Marks a message read locally, and on the phone when MAP is currently- // serving it. Returns false only when the handle is unknown.+ // Marks a message as read locally and on the phone (when MAP is up)+ // Returns false only when the handle is unknown.bool mark_message_read(const std::string& handle, bool read, std::string& err_out, bool* synced_out = nullptr);+ // Marks a batch read off the calling thread.+ void mark_messages_read_async(std::vector<std::string> handles, bool read);+// Sends a reply to a one-to-one conversation. Returns false with err_out set// when MAP is down, the thread cannot be replied to, or the phone refused.bool send_message(const std::string& thread_key, const std::string& body, Message& sent_out, std::string& err_out);diff --git a/src/core/src/bluetooth/connection.cpp b/src/core/src/bluetooth/connection.cppindex e225f99..1ed0e8a 100644--- a/src/core/src/bluetooth/connection.cpp+++ b/src/core/src/bluetooth/connection.cpp@@ -9,6 +9,7 @@#include "tether/bluetooth/pairing.hpp"#include "tether/bluetooth/pbap_session.hpp"#include "tether/log.hpp"+#include "tether/net.hpp"#include "tether/secret_store.hpp"#include "tether/session_lock.hpp"#include <tether/i18n.hpp>@@ -240,6 +241,36 @@ namespace tether::bluetooth {return true;}+ void mark_messages_read_async(std::vector<std::string> handles, bool read) {+ if (handles.empty())+ return;++ std::thread([handles = std::move(handles), read]() {+ int changed = 0;+ int synced = 0;+ std::string last_err;+ for (const auto& handle : handles) {+ std::string err;+ bool handle_synced = false;+ if (mark_message_read(handle, read, err, &handle_synced))+ ++changed;+ if (handle_synced)+ ++synced;+ if (!err.empty())+ last_err = err;+ }+ nlohmann::json event;+ event["command"] = "bt_message_read";+ event["handles"] = handles;+ event["read"] = read;+ event["success"] = changed > 0;+ event["synced"] = synced;+ if (!last_err.empty())+ event["message"] = last_err;+ broadcast_local_event(event.dump());+ }).detach();+ }+namespace {constexpr const char* BLUEZ_NAME = "org.bluez";diff --git a/src/core/src/net.cpp b/src/core/src/net.cppindex ff3a2ac..df91c30 100644--- a/src/core/src/net.cpp+++ b/src/core/src/net.cpp@@ -1210,34 +1210,7 @@ namespace tether {} else if (j["handle"].is_string()) {handles.push_back(j["handle"].get<std::string>());}- const bool read = j.value("read", true);-- if (!handles.empty()) {- std::thread([handles, read]() {- int changed = 0;- int synced = 0;- std::string last_err;- for (const auto& handle : handles) {- std::string err;- bool handle_synced = false;- if (bluetooth::mark_message_read(handle, read, err, &handle_synced))- ++changed;- if (handle_synced)- ++synced;- if (!err.empty())- last_err = err;- }- nlohmann::json event;- event["command"] = "bt_message_read";- event["handles"] = handles;- event["read"] = read;- event["success"] = changed > 0;- event["synced"] = synced;- if (!last_err.empty())- event["message"] = last_err;- broadcast_local_event(event.dump());- }).detach();- }+ bluetooth::mark_messages_read_async(std::move(handles), j.value("read", true));} else if (j.contains("command") && j["command"] == "bt_send_message" && j.contains("thread") &&j.contains("body")) {std::string thread = j["thread"];diff --git a/src/daemon/main.cpp b/src/daemon/main.cppindex 3954455..9408e0d 100644--- a/src/daemon/main.cpp+++ b/src/daemon/main.cpp@@ -2,10 +2,10 @@#include <csignal>#include <cstdio>#include <cstdlib>-#include <string_view>#include <ctime>#include <mutex>#include <nlohmann/json.hpp>+#include <string_view>#include <sys/timerfd.h>#include <tether/audio.hpp>#include <tether/bluetooth/airpods.hpp>@@ -162,7 +162,8 @@ int main(int argc, char** argv) {// app can still mislabel binary as text/plain. Don't abort the daemon.tether::broadcast_message(j.dump(-1, ' ', false, nlohmann::json::error_handler_t::replace));});- wayland_srv.set_clipboard_image_callback([](const std::string& png) { tether::broadcast_clipboard_image(png); });+ wayland_srv.set_clipboard_image_callback(+ [](const std::string& png) { tether::broadcast_clipboard_image(png); });}tether::FileReceiveManager file_mgr;@@ -178,12 +179,16 @@ int main(int argc, char** argv) {});tether::g_file_manager = &file_mgr;- notifier.set_copy_handler([&loop](const std::string& code) {+ notifier.set_copy_handler([&loop](const std::string& code, const std::string& handle) {// libnotify dispatches actions on its own thread; the clipboard belongs to the loop.loop.post([code] {if (tether::g_wayland)tether::g_wayland->copy_to_clipboard(code);});++ // copying the code is having read the message+ if (!handle.empty())+ tether::bluetooth::mark_messages_read_async({handle}, true);});if (notifier_ready) {@@ -254,7 +259,9 @@ int main(int argc, char** argv) {tether::bluetooth::ancs::icon_candidates(as_notification),false,repliable ? message.thread_key : std::string{},- otp});+ otp,+ "",+ message.handle});});auto bt_config = tether::bluetooth::load_config();diff --git a/src/daemon/notification.cpp b/src/daemon/notification.cppindex 906ac1d..0b66c78 100644--- a/src/daemon/notification.cpp+++ b/src/daemon/notification.cpp@@ -101,10 +101,11 @@ namespace tether {struct NotificationActionData {std::string payload;+ std::string handle;};// Set once during startup, before any notification exists.- std::function<void(const std::string&)> g_copy_handler;+ std::function<void(const std::string&, const std::string&)> g_copy_handler;void free_request(gpointer data) { delete static_cast<NotificationRequest*>(data); }@@ -149,7 +150,7 @@ namespace tether {void on_copy_code_action(NotifyNotification*, char*, gpointer user_data) {auto* action = static_cast<NotificationActionData*>(user_data);if (action && g_copy_handler)- g_copy_handler(action->payload);+ g_copy_handler(action->payload, action->handle);}void on_reply_action(NotifyNotification*, char*, gpointer user_data) {@@ -323,7 +324,7 @@ namespace tether {"copy-code",_("Copy Code"),on_copy_code_action,- new NotificationActionData{spec->otp_code},+ new NotificationActionData{spec->otp_code, spec->read_handle},free_action_data);}@@ -408,7 +409,8 @@ namespace tether {return true;}- void DesktopNotifier::set_copy_handler(std::function<void(const std::string&)> handler) {+ void DesktopNotifier::set_copy_handler(+ std::function<void(const std::string& code, const std::string& handle)> handler) {g_copy_handler = std::move(handler);}diff --git a/src/daemon/notification.hpp b/src/daemon/notification.hppindex 66cb882..4ddc8b3 100644--- a/src/daemon/notification.hpp+++ b/src/daemon/notification.hpp@@ -22,6 +22,9 @@ namespace tether {std::string otp_code;// Originating iPhone bundle id.std::string app_id;+ // MAP handle to mark read when the code is copied. Empty for+ // notifications with no message behind them.+ std::string read_handle;};class DesktopNotifier {@@ -34,9 +37,10 @@ namespace tether {bool init();- // Where the "Copy Code" action sends the code. Called on the notifier's- // own thread, so the handler is responsible for any thread hop it needs.- void set_copy_handler(std::function<void(const std::string&)> handler);+ // Where the "Copy Code" action sends the code, along with the spec's+ // read_handle. Called on the notifier's own thread, so the handler is+ // responsible for any thread hop it needs.+ void set_copy_handler(std::function<void(const std::string& code, const std::string& handle)> handler);void notify_file_arrived(const std::filesystem::path& path);