diff --git a/Makefile b/Makefile index c151b0c..e8ad965 100644 --- a/Makefile +++ b/Makefile @@ -265,7 +265,10 @@ listMessageLogs: $(SRCS) $(EXAMPLES_DIR)/messaging/messages/listMessageLogs.cpp $(CXX) $(CXXFLAGS) -o ./$(TESTS_DIR)/listMessageLogs $(SRCS) $(EXAMPLES_DIR)/messaging/messages/listMessageLogs.cpp $(LDFLAGS) deleteMessages: $(SRCS) $(EXAMPLES_DIR)/messaging/messages/deleteMessages.cpp @mkdir -p ./$(TESTS_DIR) - $(CXX) $(CXXFLAGS) -o ./$(TESTS_DIR)/deleteMessages $(SRCS) $(EXAMPLES_DIR)/messaging/messages/deleteMessages.cpp $(LDFLAGS) + $(CXX) $(CXXFLAGS) -o ./$(TESTS_DIR)/deleteMessages $(SRCS) $(EXAMPLES_DIR)/messaging/messages/deleteMessages.cpp $(LDFLAGS) +updateEmail: $(SRCS) $(EXAMPLES_DIR)/messaging/messages/updateEmail.cpp + @mkdir -p ./$(TESTS_DIR) + $(CXX) $(CXXFLAGS) -o ./$(TESTS_DIR)/updateEmail $(SRCS) $(EXAMPLES_DIR)/messaging/messages/updateEmail.cpp $(LDFLAGS) listTargets: $(SRCS) $(EXAMPLES_DIR)/messaging/messages/listTargets.cpp @mkdir -p ./$(TESTS_DIR) $(CXX) $(CXXFLAGS) -o ./$(TESTS_DIR)/listTargets $(SRCS) $(EXAMPLES_DIR)/messaging/messages/listTargets.cpp $(LDFLAGS) diff --git a/examples/messaging/messages/updateEmail.cpp b/examples/messaging/messages/updateEmail.cpp new file mode 100644 index 0000000..65d1bab --- /dev/null +++ b/examples/messaging/messages/updateEmail.cpp @@ -0,0 +1,24 @@ +#include "Appwrite.hpp" +#include + +int main() { + std::string projectId = ""; + std::string apiKey = ""; + + Appwrite appwrite(projectId, apiKey); + + std::string messageId = ""; + std::string subject = "Updated Subject from C++"; + std::string content = "Updated content of the email from C++ SDK."; + + try { + std::string response = appwrite.getMessaging().updateEmail( + messageId, subject, content + ); + std::cout << "Email Message Updated!\nResponse: " << response << std::endl; + } catch (const AppwriteException &ex) { + std::cerr << "Exception: " << ex.what() << std::endl; + } + + return 0; +} diff --git a/include/classes/Messaging.hpp b/include/classes/Messaging.hpp index 480aab2..f53d4ea 100644 --- a/include/classes/Messaging.hpp +++ b/include/classes/Messaging.hpp @@ -155,7 +155,7 @@ class Messaging { const std::string& content, const std::vector& topics = {}, const std::vector& targets = {}); - + /** * @brief Updates an existing push notification message. * @@ -201,7 +201,29 @@ class Messaging { * @param messageId ID of the message. * @return JSON response. */ + std::string deleteMessages(const std::string &messageId); + + /** + * @brief Update an email message by its ID. + * @class updateEmail + * + * This method belongs to the updateEmail class and provides the functionality + * to update the subject and content of an existing email message via the + * Appwrite Messaging API. + * + * @param messageId Unique message identifier + * @param subject New subject of the email + * @param content Updated content/body of the email + * @return JSON response string from the server + * @throws AppwriteException if parameters are invalid or request fails + */ + std::string updateEmail( + const std::string& messageId, + const std::string& subject, + const std::string& content + ); + /** * @brief List all targets for a given message. diff --git a/src/services/Messaging.cpp b/src/services/Messaging.cpp index 6dfc1b2..d6e9a6d 100644 --- a/src/services/Messaging.cpp +++ b/src/services/Messaging.cpp @@ -468,6 +468,41 @@ std::string Messaging::createMessage(const std::string& messageId, } } +std::string Messaging::updateEmail( + const std::string& messageId, + const std::string& subject, + const std::string& content +) { + if (messageId.empty()) { + throw AppwriteException("Missing required parameter: 'messageId'"); + } + if (subject.empty()) { + throw AppwriteException("Missing required parameter: 'subject'"); + } + if (content.empty()) { + throw AppwriteException("Missing required parameter: 'content'"); + } + + std::string url = Config::API_BASE_URL + "/messaging/messages/email/" + Utils::urlEncode(messageId); + + std::string payload = R"({"subject":")" + Utils::escapeJsonString(subject) + + R"(","content":")" + Utils::escapeJsonString(content) + R"("})"; + + std::vector headers = Config::getHeaders(projectId); + headers.push_back("X-Appwrite-Key: " + apiKey); + headers.push_back("Content-Type: application/json"); + + std::string response; + int statusCode = Utils::patchRequest(url, payload, headers, response); + + if (statusCode == HttpStatus::OK) { + return response; + } else { + throw AppwriteException("Error updating message. Status code: " + std::to_string(statusCode) + + "\n\nResponse: " + response); + } +} + std::string Messaging::updatePush(const std::string &messageId, const std::string &title, const std::string &body, @@ -623,12 +658,12 @@ std::string Messaging::listTargets(const std::string &messageId, std::string response; int statusCode = Utils::getRequest(url, headers, response); + if (statusCode == HttpStatus::OK) { return response; } else { - throw AppwriteException( - "Error fetching message targets. Status code: " + std::to_string(statusCode) + - "\n\nResponse: " + response); + throw AppwriteException("Error updating message. Status code: " + std::to_string(statusCode) + + "\n\nResponse: " + response); } }