forked from Sefaria/Sefaria-Mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.js
263 lines (253 loc) · 11.3 KB
/
history.js
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
'use strict';
import crashlytics from '@react-native-firebase/crashlytics';
import {FileSystem} from 'react-native-unimodules'
import AsyncStorage from '@react-native-async-storage/async-storage';
import { STATE_ACTIONS } from './StateManager';
const MAX_SYNC_HISTORY_LEN = 1000;
const History = {
saved: [],
lastPlace: [],
lastSync: [],
_hasSwipeDeleted: false,
_hasSyncedOnce: false,
migrateFromAsyncStorageHistory: async function() {
const hasMigrated = await AsyncStorage.getItem('hasMigratedToHistoryJSONFiles');
if (hasMigrated) { return; }
await FileSystem.makeDirectoryAsync(`${FileSystem.documentDirectory}history`);
try {
const lastPlace = await AsyncStorage.getItem('lastPlace');
Sefaria.history.setItem('lastPlace', lastPlace);
await AsyncStorage.removeItem('lastPlace');
} catch (e) {
crashlytics().recordError(new Error("Failed to migrate lastPlace"));
}
try {
const savedItems = await AsyncStorage.getItem('savedItems');
Sefaria.history.setItem('savedItems', savedItems);
await AsyncStorage.removeItem('savedItems');
} catch (e) {
crashlytics().recordError(new Error("Failed to migrate savedItems"));
}
try {
const lastSyncItems = await AsyncStorage.getItem('lastSyncItems');
Sefaria.history.setItem('lastSyncItems', lastSyncItems);
await AsyncStorage.removeItem('lastSyncItems');
} catch (e) {
crashlytics().recordError(new Error("Failed to migrate lastSyncItems"));
}
try {
const history = await AsyncStorage.getItem('history');
Sefaria.history.setItem('history', history)
await AsyncStorage.removeItem('history');
} catch (e) {
crashlytics().recordError(new Error("Failed to migrate history"));
}
await AsyncStorage.setItem('hasMigratedToHistoryJSONFiles', 'true');
},
getFilename: key => `${FileSystem.documentDirectory}history/${key}.json`,
getItem: key => FileSystem.readAsStringAsync(Sefaria.history.getFilename(key)).catch(e => null),
setItem: (key, value) => FileSystem.writeAsStringAsync(Sefaria.history.getFilename(key), value),
removeItem: key => FileSystem.deleteAsync(Sefaria.history.getFilename(key)).catch((e)=>{}),
historyToLastPlace: function(history_item_array) {
const seenBooks = {};
const lastPlace = [];
for (let history_item of history_item_array) {
if (seenBooks[history_item.book]) { continue; }
seenBooks[history_item.book] = true;
lastPlace.push(history_item);
}
return lastPlace;
},
historyToSaved: function(history_item_array) {
return history_item_array.filter(h => h.saved);
},
saveHistoryItem: async function(getHistoryObject, withIntent, onSave, intentTime=3000) {
// getHistoryObject: dependent on state of whatever component called this func. it is `getHistoryObject`'s responsibility to return empty object if readingHistory is off
// onSave: optional function which is called with the list of history items to actually save
let history_item = getHistoryObject();
if (!history_item.ref) { return; }
if (withIntent) {
await Sefaria.util.timeoutPromise(intentTime);
const new_history_item = getHistoryObject();
if (history_item.ref !== new_history_item.ref || !Sefaria.util.object_equals(history_item.versions, new_history_item.versions)) { return; /* didn't spend enough time reading */ }
}
history_item.time_stamp = Sefaria.util.epoch_time();
const lSync = Sefaria.history.lastSync;
// remove items that are the same and saved recently
if (
lSync.length > 0 &&
history_item.ref === lSync[0].ref &&
history_item.time_stamp - lSync[0].time_stamp <= 60
) { return; }
if (onSave) { onSave(history_item); }
Sefaria.history.lastSync = [history_item].concat(lSync);
Sefaria.history.setItem("lastSyncItems", JSON.stringify(Sefaria.history.lastSync));
if (!history_item.secondary) {
// secondary items should not be saved in lastPlace
Sefaria.history.lastPlace = Sefaria.history.historyToLastPlace([history_item].concat(Sefaria.history.lastPlace));
Sefaria.history.setItem("lastPlace", JSON.stringify(Sefaria.history.lastPlace));
}
},
getHistoryRefForTitle: function(title) {
//given an index title, return the ref of that title in Sefaria.history.
//if it doesn't exist, return null
for (let item of Sefaria.history.lastPlace) {
if (Sefaria.textTitleForRef(item.ref) === title) { return item; }
}
return null;
},
_loadHistoryItems: async function() {
await Sefaria.history.migrateFromAsyncStorageHistory();
const lastPlace = await Sefaria.history.getItem('lastPlace');
const lastSync = await Sefaria.history.getItem('lastSyncItems');
const saved = await Sefaria.history.getItem('savedItems');
const hasSwipeDeleted = await AsyncStorage.getItem("hasSwipeDeleted");
const hasSyncedOnce = await AsyncStorage.getItem('hasSyncedOnce');
try { Sefaria.history.lastPlace = JSON.parse(lastPlace) || []; }
catch(e) { Sefaria.history.lastPlace = []; }
try { Sefaria.history.lastSync = JSON.parse(lastSync) || []; }
catch(e) { Sefaria.history.lastSync = []; }
try { Sefaria.history.saved = JSON.parse(saved) || []; }
catch(e) { Sefaria.history.saved = []; }
Sefaria.history._hasSwipeDeleted = JSON.parse(hasSwipeDeleted) || false;
Sefaria.history._hasSyncedOnce = JSON.parse(hasSyncedOnce) || false;
},
syncProfile: async function(dispatch, settings, max_sync_history_len=MAX_SYNC_HISTORY_LEN) {
/*
settings is of the form
{
email_notifications: oneOf('daily', 'weekly', 'never'),
interface_language: oneOf('english', 'hebrew'),
textual_custom: oneOf('sephardi', 'ashkenazi')
}
*/
const currHistoryStr = await Sefaria.history.getItem('history') || '[]';
const lastSyncStr = await Sefaria.history.getItem('lastSyncItems') || '[]';
const settingsStr = JSON.stringify(settings);
const lastSyncItems = JSON.parse(lastSyncStr);
const lastSyncItemsToSend = lastSyncItems.slice(-max_sync_history_len);
const nextLastSyncItems = lastSyncItems.slice(0, -max_sync_history_len);
let currHistory = JSON.parse(currHistoryStr);
currHistory = lastSyncItemsToSend.filter(h => !h.action).concat(currHistory);
await Sefaria.api.getAuthToken();
if (Sefaria._auth.uid) {
try {
const lastSyncTime = await AsyncStorage.getItem('lastSyncTime') || '0';
const url = Sefaria.api._baseHost + "api/profile/sync";
const body = Sefaria.api.urlFormEncode({user_history: JSON.stringify(lastSyncItemsToSend), last_sync: lastSyncTime, settings: settingsStr});
const response = await fetch(url, {
method: "POST",
body,
headers: {
'Authorization': `Bearer ${Sefaria._auth.token}`,
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
},
}).then(res => {
if (res.status < 200 || res.status >= 300) {
console.log('Error text', res.text());
throw new Error("Bad Response Code " + res.status);
}
return res.json();
});
await AsyncStorage.setItem('lastSyncTime', '' + response.last_sync);
await Sefaria.history.setItem('lastSyncItems', JSON.stringify(nextLastSyncItems));
const currSaved = JSON.parse(await Sefaria.history.getItem('savedItems') || '[]');
const { mergedHistory, mergedSaved } = Sefaria.history.mergeHistory(currHistory, currSaved, response.user_history);
Sefaria.history.lastPlace = Sefaria.history.historyToLastPlace(mergedHistory);
Sefaria.history.saved = mergedSaved;
currHistory = mergedHistory;
await Sefaria.history.setItem('savedItems', JSON.stringify(Sefaria.history.saved));
await Sefaria.history.setItem('lastPlace', JSON.stringify(Sefaria.history.lastPlace));
await Sefaria.history.setItem('history', JSON.stringify(mergedHistory));
Sefaria.history.updateSettingsAfterSync(dispatch, response.settings);
if (!Sefaria.history._hasSyncedOnce) {
Sefaria.history._hasSyncedOnce = true;
await AsyncStorage.setItem('hasSyncedOnce', 'true');
}
} catch (e) {
// try again later
console.log('sync error', e);
}
}
return nextLastSyncItems.filter(h => !h.action).concat(currHistory);
},
syncProfileGetSaved: async (dispatch, settings) => {
await Sefaria.history.syncProfile(dispatch, settings);
return Sefaria.history.saved;
},
updateSettingsAfterSync: function(dispatch, newSettings) {
const fieldToActionMap = {
email_notifications: STATE_ACTIONS.setEmailFrequency,
interface_language: STATE_ACTIONS.setInterfaceLanguage,
textual_custom: STATE_ACTIONS.setPreferredCustom,
reading_history: STATE_ACTIONS.setReadingHistory,
};
Object.entries(newSettings).map(([key, value]) => {
if (!fieldToActionMap[key]) { return; }
dispatch({
type: fieldToActionMap[key],
value,
time: newSettings.time_stamp,
});
});
},
mergeHistory: function(currHistory, currSaved, newHistory) {
const delete_saved_set = new Set();
const newSaved = [];
const mergedHistory = newHistory
.map(h => {
// see https://codeburst.io/use-es2015-object-rest-operator-to-omit-properties-38a3ecffe90
const { delete_saved, saved, ...rest } = h;
if (delete_saved) { delete_saved_set.add(h.ref); }
if (saved) { newSaved.push(h); }
return h;
})
.concat(currHistory)
.sort((a, b) => b.time_stamp - a.time_stamp);
const mergedSaved = newSaved
.concat(currSaved)
.filter(s => !delete_saved_set.has(s.ref))
.sort((a, b) => b.time_stamp - a.time_stamp);
return {
mergedHistory,
mergedSaved,
};
},
saveSavedItem: function(item, action) {
/* action: can be either 'add_saved' or 'delete_saved'
*/
const lastSyncItem = {
...item,
action,
time_stamp: Sefaria.util.epoch_time(),
};
Sefaria.history.lastSync = [lastSyncItem].concat(Sefaria.history.lastSync);
if (action === 'add_saved') {
Sefaria.history.saved = [item].concat(Sefaria.history.saved);
} else {
Sefaria.history.saved = Sefaria.history.saved.filter(s => s.ref !== item.ref);
}
Sefaria.history.setItem("lastSyncItems", JSON.stringify(Sefaria.history.lastSync));
Sefaria.history.setItem("savedItems", JSON.stringify(Sefaria.history.saved));
},
indexOfSaved: function(ref) {
return Sefaria.history.saved.findIndex(existing => ref === existing.ref);
},
deleteHistory: async function(deleteSaved) {
await Sefaria.history.removeItem('lastPlace');
await Sefaria.history.removeItem('history');
Sefaria.history.lastPlace = [];
if (deleteSaved) {
await Sefaria.history.removeItem('savedItems');
await Sefaria.history.removeItem('lastSyncItems');
Sefaria.history.saved = [];
Sefaria.history.lastSync = [];
} else {
let lastSyncItems = JSON.parse(await Sefaria.history.getItem('lastSyncItems'));
lastSyncItems = lastSyncItems.filter(item => typeof item.saved != 'undefined' && item.action) // want to keep any items that have saved field, even if false to sync
Sefaria.history.lastSync = lastSyncItems;
await Sefaria.history.setItem('lastSyncItems', JSON.stringify(lastSyncItems));
}
},
};
export default History;