-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtasks_peer.cpp
366 lines (311 loc) · 12.2 KB
/
tasks_peer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include "tasks_peer.h"
#include "tasks_log.h"
#include "join_string_values.h"
#include "transform_container.h"
#include "Ditto.h"
#include <iostream>
#include <mutex>
#include <sstream>
#include <stdexcept>
#include <thread>
using namespace std;
using json = nlohmann::json;
namespace {
/// Extract a Task object from a QueryResultItem.
Task task_from(const ditto::QueryResultItem &item) {
return json::parse(item.json_string()).template get<Task>();
}
/// Convert a QueryResult to a collection of JSON objects.
vector<string> tasks_json_from(const ditto::QueryResult &result) {
const auto item_count = result.item_count();
vector<string> tasks;
tasks.reserve(item_count);
for (size_t i = 0; i < item_count; ++i) {
tasks.emplace_back(result.get_item(i).json_string());
}
return tasks;
}
/// Initialize a Ditto instance.
unique_ptr<ditto::Ditto> init_ditto(JNIEnv *env,
jobject context,
string app_id,
string online_playground_token,
bool enable_cloud_sync,
string persistence_dir,
bool is_running_on_emulator,
string custom_url,
const string& websocket_url) {
try {
// TODO UPDATE TO USE CUSTOM URL AND WEBSOCKET
// Docs: https://docs.ditto.live/sdk/latest/install-guides/cpp#importing-and-initializing-ditto
const auto identity = ditto::Identity::OnlinePlayground(
std::move(app_id),
std::move(online_playground_token),
enable_cloud_sync,
std::move(custom_url)
);
auto ditto =
make_unique<ditto::Ditto>(identity, std::move(persistence_dir));
if (is_running_on_emulator) {
// Some transports don't work correctly on emulator, so disable them.
ditto->update_transport_config([websocket_url](ditto::TransportConfig &config) {
config.enable_all_peer_to_peer();
config.connect.websocket_urls.insert(websocket_url);
});
}
ditto->set_android_context(env, context);
// Required for compatibility with DQL.
ditto->disable_sync_with_v3();
return ditto;
} catch (const exception &err) {
throw runtime_error(string("unable to initialize Ditto: ") + err.what());
}
}
} // end anonymous namespace
// Private implementation of the TasksPeer class.
class TasksPeer::Impl { // NOLINT(cppcoreguidelines-special-member-functions)
private:
unique_ptr<mutex> mtx;
unique_ptr<ditto::Ditto> ditto;
shared_ptr<ditto::SyncSubscription> tasks_subscription;
public:
Impl(JNIEnv *env,
jobject context,
string app_id,
string online_playground_token,
bool enable_cloud_sync,
string persistence_dir,
bool is_running_on_emulator,
string custom_auth_url,
const string& websocket_url)
: mtx(new mutex()),
ditto(init_ditto(
env,
context,
std::move(app_id),
std::move(online_playground_token),
enable_cloud_sync,
std::move(persistence_dir),
is_running_on_emulator,
std::move(custom_auth_url),
websocket_url
)) {}
~Impl() noexcept {
try {
stop_sync();
} catch (const exception &err) {
cerr << "Failed to destroy tasks peer instance: " +
string(err.what())
<< endl;
}
}
void start_sync() {
if (is_sync_active()) {
return;
}
ditto->start_sync();
tasks_subscription =
ditto->sync().register_subscription("SELECT * FROM tasks");
}
void stop_sync() {
if (!is_sync_active()) {
return;
}
tasks_subscription->cancel();
tasks_subscription.reset();
ditto->stop_sync();
}
bool is_sync_active() const { return ditto->get_is_sync_active(); }
string add_task(const string &title, bool done) {
try {
const json task_args = {
{"title", title},
{"done", done},
{"deleted", false}};
const auto command = "INSERT INTO tasks DOCUMENTS (:newTask)";
const auto result =
ditto->get_store().execute(command, {{"newTask", task_args}});
auto task_id = result.mutated_document_ids()[0].to_string();
log_debug("Added task: " + task_id);
return task_id;
} catch (const exception &err) {
log_error("Failed to add task: " + string(err.what()));
throw runtime_error("unable to add task: " + string(err.what()));
}
}
Task get_task(const string &task_id) {
try {
lock_guard<mutex> lock(*mtx);
if (task_id.empty()) {
throw invalid_argument("task_id must not be empty");
}
const auto query = "SELECT * FROM tasks WHERE _id = :id AND NOT deleted";
const auto result = ditto->get_store().execute(query, {{"id", task_id}});
const auto item_count = result.item_count();
if (item_count == 0) {
throw runtime_error(string("no tasks found with id \"") + task_id +
"\"");
} else if (item_count > 1) {
throw runtime_error("more than one task found with id \"" + task_id +
"\"");
}
const auto task = task_from(result.get_item(0));
log_debug("Retrieved task with _id " + task_id);
return task;
} catch (const exception &err) {
log_error("Failed to get task with _id " + task_id + ": " +
string(err.what()));
throw runtime_error("unable to retrieve task: " + string(err.what()));
}
}
void update_task(const Task &task) {
try {
lock_guard<mutex> lock(*mtx);
const auto stmt = "UPDATE tasks SET"
" title = :title,"
" done = :done,"
" deleted = :deleted"
" WHERE _id = :id";
const auto result =
ditto->get_store().execute(stmt, {{"title", task.title},
{"done", task.done},
{"deleted", task.deleted},
{"id", task._id}});
if (result.mutated_document_ids().empty()) {
throw runtime_error("task not found with ID: " + task._id);
}
log_debug("Updated task: " + task._id);
} catch (const exception &err) {
log_error("Failed to update task: " + string(err.what()));
throw runtime_error("unable to update task: " + string(err.what()));
}
}
void mark_task_complete(const string &task_id, bool done) {
try {
lock_guard<mutex> lock(*mtx);
if (task_id.empty()) {
throw invalid_argument("task ID must not be empty");
}
const auto stmt = "UPDATE tasks SET done = :done WHERE _id = :id";
const auto result =
ditto->get_store().execute(stmt, {{"done", done},
{"id", task_id}});
log_debug("Marked task " + task_id +
(done ? " complete" : " incomplete"));
} catch (const exception &err) {
log_error("Failed to mark task complete: " + string(err.what()));
throw runtime_error("unable to mark task complete: " +
string(err.what()));
}
}
void delete_task(const string &task_id) {
try {
lock_guard<mutex> lock(*mtx);
if (task_id.empty()) {
throw invalid_argument("task ID must not be empty");
}
const auto stmt = "UPDATE tasks SET deleted = true WHERE _id = :id";
const auto result = ditto->get_store().execute(stmt, {{"id", task_id}});
if (result.mutated_document_ids().empty()) {
throw runtime_error("task not found with ID: " + task_id);
}
log_debug("Deleted task: " + task_id);
} catch (const exception &err) {
log_error("Failed to delete task: " + string(err.what()));
throw runtime_error("unable to evict task: " + string(err.what()));
}
}
shared_ptr<ditto::StoreObserver> register_tasks_observer(
function<void(const vector<string> &)> callback) {
try {
const auto observer = ditto->get_store().register_observer(
"SELECT * FROM tasks WHERE NOT deleted ORDER BY _id",
[callback = std::move(callback)](const ditto::QueryResult &result) {
const auto item_count = result.item_count();
log_debug("Tasks collection updated; count=" +
to_string(item_count));
const auto tasks = tasks_json_from(result);
try {
log_debug("Invoking observer callback");
callback(tasks);
log_debug("Observer callback completed");
} catch (const exception &err) {
log_error("Error in observer callback: " + string(err.what()));
}
});
log_debug("Registered tasks observer");
return observer;
} catch (const exception &err) {
log_error("Failed to register observer: " + string(err.what()));
throw runtime_error("unable to register observer: " + string(err.what()));
}
}
void insert_initial_tasks() {
try {
lock_guard<mutex> lock(*mtx);
vector<Task> initial_tasks = {
{"50191411-4C46-4940-8B72-5F8017A04FA7", "Buy groceries"},
{"6DA283DA-8CFE-4526-A6FA-D385089364E5", "Clean the kitchen"},
{"5303DDF8-0E72-4FEB-9E82-4B007E5797F0",
"Schedule dentist appointment"},
{"38411F1B-6B49-4346-90C3-0B16CE97E174", "Pay bills"}};
for (const auto &task: initial_tasks) {
const json task_args = {{"_id", task._id},
{"title", task.title},
{"done", task.done},
{"deleted", task.deleted}};
const auto command = "INSERT INTO tasks INITIAL DOCUMENTS (:newTask)";
ditto->get_store().execute(command, {{"newTask", task_args}});
}
} catch (const exception &err) {
log_error("Failed to insert initial tasks: " + string(err.what()));
throw runtime_error("unable to insert initial tasks: " +
string(err.what()));
}
}
vector<string> missing_permissions() const {
auto result = ditto->missing_permissions();
log_warning("Missing permissions: " + join_string_values<>(result));
return result;
}
jobjectArray missing_permissions_jni_array() const {
return ditto->missing_permissions_jni_array();
}
}; // class TasksPeer::Impl
TasksPeer::TasksPeer(JNIEnv *env, jobject context, string app_id, string online_playground_token,
bool enable_cloud_sync, string persistence_dir, bool is_running_on_emulator,
string custom_auth_url, const string& websocket_url)
: impl(make_unique<Impl>(env, context, std::move(app_id), std::move(online_playground_token),
enable_cloud_sync, std::move(persistence_dir),
is_running_on_emulator, std::move(custom_auth_url), std::move(websocket_url))) {}
TasksPeer::~TasksPeer() noexcept {
try {
log_debug("Destroying TasksPeer instance");
} catch (const exception &err) {
log_error(string("exception in TasksPeer destructor: ") + err.what());
}
}
void TasksPeer::start_sync() { impl->start_sync(); }
void TasksPeer::stop_sync() { impl->stop_sync(); }
bool TasksPeer::is_sync_active() const { return impl->is_sync_active(); }
string TasksPeer::add_task(const string &title, bool done) {
return impl->add_task(title, done);
}
Task TasksPeer::get_task(const string &task_id) {
return impl->get_task(task_id);
}
void TasksPeer::update_task(const Task &task) { impl->update_task(task); }
void TasksPeer::mark_task_complete(const string &task_id, bool done) {
impl->mark_task_complete(task_id, done);
}
void TasksPeer::delete_task(const string &task_id) {
impl->delete_task(task_id);
}
shared_ptr<ditto::StoreObserver> TasksPeer::register_tasks_observer(
function<void(const vector<string> &)> callback) {
return impl->register_tasks_observer(std::move(callback));
}
string TasksPeer::get_ditto_sdk_version() {
return ditto::Ditto::get_sdk_version();
}
void TasksPeer::insert_initial_tasks() { impl->insert_initial_tasks(); }