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
|
/*
Copyright (©) 2003-2025 Teus Benschop.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <sync/notes.h>
#include <filter/url.h>
#include <filter/roles.h>
#include <filter/string.h>
#include <filter/merge.h>
#include <filter/archive.h>
#include <tasks/logic.h>
#include <database/config/general.h>
#include <database/config/bible.h>
#include <database/logs.h>
#include <database/notes.h>
#include <database/modifications.h>
#include <database/state.h>
#include <client/logic.h>
#include <locale/translate.h>
#include <webserver/request.h>
#include <sync/logic.h>
#include <checksum/logic.h>
#include <access/bible.h>
#include <bb/logic.h>
#include <notes/logic.h>
std::string sync_notes_url ()
{
return "sync/notes";
}
std::string sync_notes (Webserver_Request& webserver_request)
{
Sync_Logic sync_logic (webserver_request);
Database_Notes database_notes (webserver_request);
Notes_Logic notes_logic (webserver_request);
if (!sync_logic.security_okay ()) {
// When the Cloud enforces https, inform the client to upgrade.
webserver_request.response_code = 426;
return std::string();
}
// Bail out if the notes databases are not available or not in good shape.
bool available = true;
if (!database_notes.healthy ()) available = false;
if (!database_notes.checksums_healthy ()) available = false;
if (!database_notes.available ()) available = false;
if (!available) {
webserver_request.response_code = 503;
return std::string();
}
// Client makes a prioritized server call: Record the client's IP address.
sync_logic.prioritized_ip_address_record ();
// What action does the client request from us?
int action = filter::strings::convert_to_int (webserver_request.post_get("a"));
// Check on the credentials when the clients sends data to the server to be stored there.
if ((action >= Sync_Logic::notes_put_create_initiate) && (action != Sync_Logic::notes_get_bulk)) {
if (!sync_logic.credentials_okay ()) return std::string();
}
// Check on username only, without password or level.
std::string user = filter::strings::hex2bin (webserver_request.post_get("u"));
if ((action == Sync_Logic::notes_get_total) || (action == Sync_Logic::notes_get_identifiers)) {
if (!webserver_request.database_users ()->usernameExists (user)) {
Database_Logs::log ("A client passes a non-existing user " + user, roles::manager);
return std::string();
}
}
webserver_request.session_logic ()->set_username (user);
// Note lower and upper limits.
int lowId = filter::strings::convert_to_int (webserver_request.post_get("l"));
int highId = filter::strings::convert_to_int (webserver_request.post_get("h"));
int identifier = filter::strings::convert_to_int (webserver_request.post_get("i"));
std::string content = webserver_request.post_get("c");
switch (action) {
case Sync_Logic::notes_get_total:
{
std::vector <std::string> bibles = access_bible::bibles (webserver_request, user);
std::vector <int> identifiers = database_notes.get_notes_in_range_for_bibles (lowId, highId, bibles, false);
// Checksum cache to speed things up in case of thousands of notes.
// Else the server would run at 100% CPU usage for some time to get the total checksums of notes.
std::string checksum = Database_State::getNotesChecksum (lowId, highId);
if (checksum.empty ()) {
checksum = database_notes.get_multiple_checksum (identifiers);
Database_State::putNotesChecksum (lowId, highId, checksum);
}
std::string response = std::to_string (identifiers.size ()) + "\n" + checksum;
return response;
}
case Sync_Logic::notes_get_identifiers:
{
std::vector <std::string> bibles = access_bible::bibles (webserver_request, user);
std::vector <int> identifiers = database_notes.get_notes_in_range_for_bibles (lowId, highId, bibles, false);
std::string response;
for (auto id : identifiers) {
if (!response.empty ()) response.append ("\n");
response.append (std::to_string (id));
response.append ("\n");
response.append (database_notes.get_checksum (id));
}
return response;
}
case Sync_Logic::notes_get_summary:
{
// Update search and checksum when the client requests the summary of a note,
// because this is the first thing a client does when it requests a full note.
// The client requests the notes in bits and pieces.
database_notes.update_search_fields (identifier);
database_notes.update_checksum (identifier);
// Return summary.
std::string summary = database_notes.get_summary (identifier);
return summary;
}
case Sync_Logic::notes_get_contents:
{
return database_notes.get_contents (identifier);
}
case Sync_Logic::notes_get_subscribers:
{
std::vector <std::string> subscribers = database_notes.get_subscribers (identifier);
return filter::strings::implode (subscribers, "\n");
}
case Sync_Logic::notes_get_assignees:
{
std::vector <std::string> assignees = database_notes.get_assignees (identifier);
return filter::strings::implode (assignees, "\n");
}
case Sync_Logic::notes_get_status:
{
return database_notes.get_raw_status (identifier);
}
case Sync_Logic::notes_get_passages:
{
// Send the raw passage contents to the client, see the client code for the reason why.
return database_notes.decode_passage (identifier);
}
case Sync_Logic::notes_get_severity:
{
return std::to_string (database_notes.get_raw_severity (identifier));
}
case Sync_Logic::notes_get_bible:
{
return database_notes.get_bible (identifier);
}
case Sync_Logic::notes_get_modified:
{
return std::to_string (database_notes.get_modified (identifier));
}
case Sync_Logic::notes_put_create_initiate:
{
// Create the note on the server.
Database_Notes::NewNote new_note {
.bible = "",
.book = 1,
.chapter = 1,
.verse = 1,
.summary = "<empty>",
.contents = "<empty>",
};
int server_id = database_notes.store_new_note (new_note);
// Update the note identifier on the server to be same as on the client.
database_notes.set_identifier (server_id, identifier);
// Update search field.
database_notes.update_search_fields (identifier);
// Done.
return std::string();
}
case Sync_Logic::notes_put_create_complete:
{
// Do notifications.
notes_logic.handlerNewNote (identifier);
// Done.
return std::string();
}
case Sync_Logic::notes_put_summary:
{
// Set the summary of the note on the server.
notes_logic.set_summary (identifier, content);
// Update search field.
database_notes.update_search_fields (identifier);
// Info.
Database_Logs::log ("Client created or updated a note on the server: " + content, roles::manager);
// Done.
return std::string();
}
case Sync_Logic::notes_put_contents:
{
// Set the note's contents on the server.
database_notes.set_contents (identifier, content);
// Update search field.
database_notes.update_search_fields (identifier);
// Done.
return std::string();
}
case Sync_Logic::notes_put_comment:
{
// Add the comment to the note on the server.
notes_logic.addComment (identifier, content);
// Update search field.
database_notes.update_search_fields (identifier);
// Info.
Database_Logs::log ("Client added comment to note on server: " + database_notes.get_summary (identifier), roles::manager);
// Notifications.
notes_logic.handlerAddComment (identifier);
// Done.
return std::string();
}
case Sync_Logic::notes_put_subscribe:
{
// Subscribe to the note on the server.
database_notes.subscribe_user (identifier, user);
// Info.
Database_Logs::log ("Client subscribed to note on server: " + database_notes.get_summary (identifier), roles::manager);
// Done.
return std::string();
}
case Sync_Logic::notes_put_unsubscribe:
{
// Unsubscribe from the note on the server.
database_notes.unsubscribe_user (identifier, user);
// Info.
Database_Logs::log ("Client unsubscribed from note on server: " + database_notes.get_summary (identifier), roles::manager);
// Done.
return std::string();
}
case Sync_Logic::notes_put_assign:
{
// Assign user to the note on the server.
notes_logic.assignUser (identifier, content);
// Info
Database_Logs::log ("Client assigned the note to a user on server: " + database_notes.get_summary (identifier), roles::manager);
// Notifications.
notes_logic.handlerAssignNote (identifier, content);
// Done.
return std::string();
}
case Sync_Logic::notes_put_unassign:
{
// Unassign the user from the note on the server.
notes_logic.unassignUser (identifier, content);
// Info.
Database_Logs::log ("Client unassigned a user from the note on server: " + database_notes.get_summary (identifier), roles::manager);
// Done.
return std::string();
}
case Sync_Logic::notes_put_status:
{
// Set the status for a note on the server.
notes_logic.setStatus (identifier, content);
// Info.
Database_Logs::log ("Client set the note status on server: " + database_notes.get_summary (identifier), roles::manager);
// Done.
return std::string();
}
case Sync_Logic::notes_put_passages:
{
// Set the passage(s) for a note on the server.
database_notes.set_raw_passage (identifier, content);
database_notes.index_raw_passage (identifier, content);
// Done.
return std::string();
}
case Sync_Logic::notes_put_severity:
{
// Set the severity for a note on the server.
notes_logic.setRawSeverity (identifier, filter::strings::convert_to_int (content));
// Info
Database_Logs::log ("Client set the severity for a note on server: " + database_notes.get_summary (identifier), roles::manager);
// Done.
return std::string();
}
case Sync_Logic::notes_put_bible:
{
// Set the Bible for a note on the server.
notes_logic.setBible (identifier, content);
// Done.
return std::string();
}
case Sync_Logic::notes_put_mark_delete:
{
// Mark note on server for deletion.
notes_logic.markForDeletion (identifier);
// Info.
Database_Logs::log ("Client marked a note on server for deletion: " + database_notes.get_summary (identifier), roles::manager);
// Notifications.
notes_logic.handlerMarkNoteForDeletion (identifier);
// Done.
return std::string();
}
case Sync_Logic::notes_put_unmark_delete:
{
// Unmark note on server for deletion.
notes_logic.unmarkForDeletion (identifier);
// Info.
Database_Logs::log ("Client unmarked a note on server for deletion: " + database_notes.get_summary (identifier), roles::manager);
// Done.
return std::string();
}
case Sync_Logic::notes_put_delete:
{
// Info to be given before the note is deleted, else the info is lost.
Database_Logs::log ("Client deleted a note on server: " + database_notes.get_summary (identifier), roles::manager);
// Notifications.
notes_logic.handlerDeleteNote (identifier);
// Delete note on server.
notes_logic.erase (identifier);
// Done.
return std::string();
}
// This method of bulk download was implemented as of September 2016.
// After a year or so, the logic for the replaced download methods can probably be removed from the Cloud.
case Sync_Logic::notes_get_bulk:
{
// Get the note identifiers the client requests.
std::vector <std::string> notes = filter::strings::explode (webserver_request.post_get("b"), '\n');
std::vector <int> identifiers;
for (auto note : notes) identifiers.push_back (filter::strings::convert_to_int (note));
// Return the JSON that contains all the requested notes.
std::string json = database_notes.get_bulk (identifiers);
return json;
}
default: {};
}
// Bad request.
// Delay a while to obstruct a flood of bad requests.
std::this_thread::sleep_for (std::chrono::seconds (1));
webserver_request.response_code = 400;
return std::string();
}
|