File: changes.cpp

package info (click to toggle)
bibledit-cloud 5.1.036-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 250,636 kB
  • sloc: xml: 915,934; ansic: 261,349; cpp: 92,628; javascript: 32,542; sh: 4,915; makefile: 586; php: 69
file content (278 lines) | stat: -rw-r--r-- 9,980 bytes parent folder | download | duplicates (2)
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
/*
 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 <sendreceive/changes.h>
#include <filter/url.h>
#include <filter/roles.h>
#include <filter/string.h>
#include <filter/date.h>
#include <tasks/logic.h>
#include <config/globals.h>
#include <database/config/general.h>
#include <database/modifications.h>
#include <database/logs.h>
#include <client/logic.h>
#include <locale/translate.h>
#include <webserver/request.h>
#include <sync/logic.h>
#include <sync/changes.h>
#include <checksum/logic.h>


int sendreceive_changes_watchdog {0};


void send_receive_changes_done ()
{
  sendreceive_changes_watchdog = 0;
  config_globals_syncing_changes = false;
}


std::string sendreceive_changes_text ()
{
  return translate("Changes") + ": ";
}


std::string sendreceive_changes_sendreceive_text ()
{
  return sendreceive_changes_text () + translate ("Send/receive");
}


std::string sendreceive_changes_up_to_date_text ()
{
  return sendreceive_changes_text () + translate ("Up to date");
}


void sendreceive_changes ()
{
  if (sendreceive_changes_watchdog) {
    int time = filter::date::seconds_since_epoch ();
    if (time < (sendreceive_changes_watchdog + 900)) {
      Database_Logs::log (sendreceive_changes_text () + translate("Still busy"), roles::translator);
      return;
    }
    Database_Logs::log (sendreceive_changes_text () + translate("Watchdog timeout"), roles::translator);
  }
  sendreceive_changes_kick_watchdog ();
  config_globals_syncing_changes = true;

  
  Database_Logs::log (sendreceive_changes_sendreceive_text (), roles::translator);
  

  Webserver_Request webserver_request;
  Sync_Logic sync_logic (webserver_request);
  
  
  if (!database::modifications::healthy ()) {
    Database_Logs::log (sendreceive_changes_text () + translate("Recreate damaged modifications database"), roles::translator);
    database::modifications::erase ();
    database::modifications::create ();
  }
  
  
  std::string response = client_logic_connection_setup ("", "");
  int iresponse = filter::strings::convert_to_int (response);
  if (iresponse < roles::guest || iresponse > roles::admin) {
    Database_Logs::log (sendreceive_changes_text () + translate("Failure to initiate connection"), roles::translator);
    send_receive_changes_done ();
    return;
  }
  
  
  // Set the correct user in the session: The sole user on the Client.
  std::vector <std::string> users = webserver_request.database_users ()->get_users ();
  if (users.empty ()) {
    Database_Logs::log (translate("No user found"), roles::translator);
    send_receive_changes_done ();
    return;
  }
  std::string user = users [0];
  webserver_request.session_logic ()->set_username (user);
  std::string password = webserver_request.database_users ()->get_md5 (user);
  
  
  // The basic request to be POSTed to the server.
  // It contains the user's credentials.
  std::map <std::string, std::string> post;
  post ["u"] = filter::strings::bin2hex (user);
  post ["p"] = password;
  post ["l"] = std::to_string (webserver_request.database_users ()->get_level (user));
  
  
  // Error variables.
  std::string error;
  bool communication_errors = false;
  
  
  // Server URL to call.
  std::string address = database::config::general::get_server_address ();
  int port = database::config::general::get_server_port ();
  std::string url = client_logic_url (address, port, sync_changes_url ());
  
  
  // Send the removed change notifications to the server.
  std::vector <int> ids = webserver_request.database_config_user ()->get_removed_changes ();
  if (!ids.empty ()) Database_Logs::log (sendreceive_changes_text () + "Sending removed notifications: " + std::to_string (ids.size()), roles::translator);
  for (auto & id : ids) {
    post ["a"] = std::to_string (Sync_Logic::changes_delete_modification);
    post ["i"] = std::to_string (id);
    response = sync_logic.post (post, url, error);
    if (!error.empty ()) {
      communication_errors = true;
      Database_Logs::log (sendreceive_changes_text () + "Failure sending removed notification: " + error, roles::translator);
    }
    else {
      webserver_request.database_config_user ()->remove_removed_change (id);
    }
  }
  
  
  if (communication_errors) {
    Database_Logs::log (sendreceive_changes_text () + translate("Not downloading change notifications due to communication error"), roles::translator);
    send_receive_changes_done ();
    return;
  }
  
  
  // Compare the total checksum for the change notifications for the active user on client and server.
  // Checksum is cached for future re-use.
  // Take actions based on that.
  std::string client_checksum = webserver_request.database_config_user ()->get_change_notifications_checksum ();
  if (client_checksum.empty ()) {
    client_checksum = Sync_Logic::changes_checksum (user);
    webserver_request.database_config_user ()->set_change_notifications_checksum (client_checksum);
  }
  std::string server_checksum;
  post ["a"] = std::to_string (Sync_Logic::changes_get_checksum);
  response = sync_logic.post (post, url, error);
  if (!error.empty ()) {
    Database_Logs::log (sendreceive_changes_text () + "Failure receiving checksum: " + error, roles::translator);
    send_receive_changes_done ();
    return;
  }
  server_checksum = response;
  if (client_checksum == server_checksum) {
    Database_Logs::log (sendreceive_changes_up_to_date_text (), roles::translator);
    send_receive_changes_done ();
    return;
  }
  
  
  // Get all identifiers for the notifications on the server for the user.
  // Get the identifiers on the client.
  std::string any_bible = "";
  std::vector <int> client_identifiers = database::modifications::getNotificationIdentifiers (user, any_bible);
  std::vector <int> server_identifiers;
  post ["a"] = std::to_string (Sync_Logic::changes_get_identifiers);
  response = sync_logic.post (post, url, error);
  if (!error.empty ()) {
    Database_Logs::log (sendreceive_changes_text () + "Failure receiving identifiers: " + error, roles::translator);
    send_receive_changes_done ();
    return;
  }
  {
    std::vector <std::string> ids2 = filter::strings::explode (response, '\n');
    for (auto & id : ids2) server_identifiers.push_back (filter::strings::convert_to_int (id));
  }

  
  // Any identifiers on the client, but not on the server, remove them from the client.
  std::vector <int> remove_identifiers = filter::strings::array_diff (client_identifiers, server_identifiers);
  for (auto & id : remove_identifiers) {
    database::modifications::deleteNotification (id);
    webserver_request.database_config_user ()->set_change_notifications_checksum ("");
    Database_Logs::log (sendreceive_changes_text () + "Removing notification: " + std::to_string (id), roles::translator);
  }

  
  // Any identifiers on the server, but not on the client, download them from the server.
  std::vector <int> download_identifiers = filter::strings::array_diff (server_identifiers, client_identifiers);
  for (auto & id : download_identifiers) {
    sendreceive_changes_kick_watchdog ();
    Database_Logs::log (sendreceive_changes_text () + "Downloading notification: " + std::to_string (id), roles::translator);
    post ["a"] = std::to_string (Sync_Logic::changes_get_modification);
    post ["i"] = std::to_string (id);
    response = sync_logic.post (post, url, error);
    if (!error.empty ()) {
      Database_Logs::log (sendreceive_changes_text () + "Failure downloading notification: " + error, roles::translator);
    }
    else {
      // The server has put all bits together, one bit per line.
      std::vector <std::string> lines = filter::strings::explode (response, '\n');
      std::string category;
      if (!lines.empty ()) {
        category = lines [0];
        lines.erase (lines.begin ());
      }
      std::string bible;
      if (!lines.empty ()) {
        bible = lines [0];
        lines.erase (lines.begin ());
      }
      int book = 0;
      if (!lines.empty ()) {
        book = filter::strings::convert_to_int (lines [0]);
        lines.erase (lines.begin ());
      }
      int chapter = 0;
      if (!lines.empty ()) {
        chapter = filter::strings::convert_to_int (lines [0]);
        lines.erase (lines.begin ());
      }
      int verse = 0;
      if (!lines.empty ()) {
        verse = filter::strings::convert_to_int (lines [0]);
        lines.erase (lines.begin ());
      }
      std::string oldtext;
      if (!lines.empty ()) {
        oldtext = lines [0];
        lines.erase (lines.begin ());
      }
      std::string modification;
      if (!lines.empty ()) {
        modification = lines [0];
        lines.erase (lines.begin ());
      }
      std::string newtext;
      if (!lines.empty ()) {
        newtext = lines [0];
        lines.erase (lines.begin ());
      }
      database::modifications::storeClientNotification (id, user, category, bible, book, chapter, verse, oldtext, modification, newtext);
      webserver_request.database_config_user ()->set_change_notifications_checksum ("");
    }
  }
  

  // Done.
  Database_Logs::log (sendreceive_changes_text () + "Ready", roles::translator);
  send_receive_changes_done ();
}


void sendreceive_changes_kick_watchdog ()
{
  sendreceive_changes_watchdog = filter::date::seconds_since_epoch ();
}