Skip to content

Commit 503ca60

Browse files
feat: updateEmail (#101)
* Feat/createEmail * C++ docs * feat/updateMessage * deleted irrelevant * Errors Resolved * Minor Changes * Update * New Update * 201 removed * Update Email * Fix * Make Update * Makefile Update
1 parent 6ba2b75 commit 503ca60

File tree

4 files changed

+89
-5
lines changed

4 files changed

+89
-5
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,10 @@ listMessageLogs: $(SRCS) $(EXAMPLES_DIR)/messaging/messages/listMessageLogs.cpp
271271
$(CXX) $(CXXFLAGS) -o ./$(TESTS_DIR)/listMessageLogs $(SRCS) $(EXAMPLES_DIR)/messaging/messages/listMessageLogs.cpp $(LDFLAGS)
272272
deleteMessages: $(SRCS) $(EXAMPLES_DIR)/messaging/messages/deleteMessages.cpp
273273
@mkdir -p ./$(TESTS_DIR)
274-
$(CXX) $(CXXFLAGS) -o ./$(TESTS_DIR)/deleteMessages $(SRCS) $(EXAMPLES_DIR)/messaging/messages/deleteMessages.cpp $(LDFLAGS)
274+
$(CXX) $(CXXFLAGS) -o ./$(TESTS_DIR)/deleteMessages $(SRCS) $(EXAMPLES_DIR)/messaging/messages/deleteMessages.cpp $(LDFLAGS)
275+
updateEmail: $(SRCS) $(EXAMPLES_DIR)/messaging/messages/updateEmail.cpp
276+
@mkdir -p ./$(TESTS_DIR)
277+
$(CXX) $(CXXFLAGS) -o ./$(TESTS_DIR)/updateEmail $(SRCS) $(EXAMPLES_DIR)/messaging/messages/updateEmail.cpp $(LDFLAGS)
275278
listTargets: $(SRCS) $(EXAMPLES_DIR)/messaging/messages/listTargets.cpp
276279
@mkdir -p ./$(TESTS_DIR)
277280
$(CXX) $(CXXFLAGS) -o ./$(TESTS_DIR)/listTargets $(SRCS) $(EXAMPLES_DIR)/messaging/messages/listTargets.cpp $(LDFLAGS)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "Appwrite.hpp"
2+
#include <iostream>
3+
4+
int main() {
5+
std::string projectId = "<project-id>";
6+
std::string apiKey = "<api-key>";
7+
8+
Appwrite appwrite(projectId, apiKey);
9+
10+
std::string messageId = "<existing-email-message-id>";
11+
std::string subject = "Updated Subject from C++";
12+
std::string content = "Updated content of the email from C++ SDK.";
13+
14+
try {
15+
std::string response = appwrite.getMessaging().updateEmail(
16+
messageId, subject, content
17+
);
18+
std::cout << "Email Message Updated!\nResponse: " << response << std::endl;
19+
} catch (const AppwriteException &ex) {
20+
std::cerr << "Exception: " << ex.what() << std::endl;
21+
}
22+
23+
return 0;
24+
}

include/classes/Messaging.hpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class Messaging {
155155
const std::string& content,
156156
const std::vector<std::string>& topics = {},
157157
const std::vector<std::string>& targets = {});
158-
158+
159159
/**
160160
* @brief Updates an existing push notification message.
161161
*
@@ -220,7 +220,29 @@ class Messaging {
220220
* @param messageId ID of the message.
221221
* @return JSON response.
222222
*/
223+
223224
std::string deleteMessages(const std::string &messageId);
225+
226+
/**
227+
* @brief Update an email message by its ID.
228+
* @class updateEmail
229+
*
230+
* This method belongs to the updateEmail class and provides the functionality
231+
* to update the subject and content of an existing email message via the
232+
* Appwrite Messaging API.
233+
*
234+
* @param messageId Unique message identifier
235+
* @param subject New subject of the email
236+
* @param content Updated content/body of the email
237+
* @return JSON response string from the server
238+
* @throws AppwriteException if parameters are invalid or request fails
239+
*/
240+
std::string updateEmail(
241+
const std::string& messageId,
242+
const std::string& subject,
243+
const std::string& content
244+
);
245+
224246

225247
/**
226248
* @brief List all targets for a given message.

src/services/Messaging.cpp

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,41 @@ std::string Messaging::createMessage(const std::string& messageId,
468468
}
469469
}
470470

471+
std::string Messaging::updateEmail(
472+
const std::string& messageId,
473+
const std::string& subject,
474+
const std::string& content
475+
) {
476+
if (messageId.empty()) {
477+
throw AppwriteException("Missing required parameter: 'messageId'");
478+
}
479+
if (subject.empty()) {
480+
throw AppwriteException("Missing required parameter: 'subject'");
481+
}
482+
if (content.empty()) {
483+
throw AppwriteException("Missing required parameter: 'content'");
484+
}
485+
486+
std::string url = Config::API_BASE_URL + "/messaging/messages/email/" + Utils::urlEncode(messageId);
487+
488+
std::string payload = R"({"subject":")" + Utils::escapeJsonString(subject) +
489+
R"(","content":")" + Utils::escapeJsonString(content) + R"("})";
490+
491+
std::vector<std::string> headers = Config::getHeaders(projectId);
492+
headers.push_back("X-Appwrite-Key: " + apiKey);
493+
headers.push_back("Content-Type: application/json");
494+
495+
std::string response;
496+
int statusCode = Utils::patchRequest(url, payload, headers, response);
497+
498+
if (statusCode == HttpStatus::OK) {
499+
return response;
500+
} else {
501+
throw AppwriteException("Error updating message. Status code: " + std::to_string(statusCode) +
502+
"\n\nResponse: " + response);
503+
}
504+
}
505+
471506
std::string Messaging::updatePush(const std::string &messageId,
472507
const std::string &title,
473508
const std::string &body,
@@ -672,12 +707,12 @@ std::string Messaging::listTargets(const std::string &messageId,
672707
std::string response;
673708
int statusCode = Utils::getRequest(url, headers, response);
674709

710+
675711
if (statusCode == HttpStatus::OK) {
676712
return response;
677713
} else {
678-
throw AppwriteException(
679-
"Error fetching message targets. Status code: " + std::to_string(statusCode) +
680-
"\n\nResponse: " + response);
714+
throw AppwriteException("Error updating message. Status code: " + std::to_string(statusCode) +
715+
"\n\nResponse: " + response);
681716
}
682717
}
683718

0 commit comments

Comments
 (0)