Automatically create the default journals if there are none.

master
Tom Hacohen 6 years ago
parent 1e1033fdf1
commit 73897700d9

@ -17,7 +17,7 @@ import * as EteSync from './api/EteSync';
import { CURRENT_VERSION } from './api/Constants'; import { CURRENT_VERSION } from './api/Constants';
import { store, JournalsType, EntriesType, StoreState, CredentialsData, UserInfoType } from './store'; import { store, JournalsType, EntriesType, StoreState, CredentialsData, UserInfoType } from './store';
import { fetchAll, fetchUserInfo, createUserInfo } from './store/actions'; import { createJournal, fetchAll, fetchEntries, fetchUserInfo, createUserInfo } from './store/actions';
export interface SyncInfoJournal { export interface SyncInfoJournal {
journal: EteSync.Journal; journal: EteSync.Journal;
@ -101,7 +101,25 @@ class SyncGate extends React.PureComponent {
componentDidMount() { componentDidMount() {
const me = this.props.etesync.credentials.email; const me = this.props.etesync.credentials.email;
const syncAll = () => { const syncAll = () => {
store.dispatch(fetchAll(this.props.etesync, this.props.entries)); store.dispatch<any>(fetchAll(this.props.etesync, this.props.entries)).then((haveJournals: boolean) => {
if (haveJournals) {
return;
}
['ADDRESS_BOOK', 'CALENDAR'].forEach((collectionType) => {
const collection = new EteSync.CollectionInfo();
collection.uid = EteSync.genUid();
collection.type = collectionType;
collection.displayName = 'Default';
const journal = new EteSync.Journal();
const cryptoManager = new EteSync.CryptoManager(this.props.etesync.encryptionKey, collection.uid);
journal.setInfo(cryptoManager, collection);
store.dispatch<any>(createJournal(this.props.etesync, journal)).then(() => {
store.dispatch(fetchEntries(this.props.etesync, collection.uid));
});
});
});
}; };
const sync = () => { const sync = () => {

@ -26,6 +26,11 @@ export function deriveKey(salt: string, password: string): string {
return sjcl.codec.base64.fromBits((sjcl.misc as any).scrypt(password, salt, 16384, 8, 1, keySize)); return sjcl.codec.base64.fromBits((sjcl.misc as any).scrypt(password, salt, 16384, 8, 1, keySize));
} }
export function genUid() {
const rand = sjcl.random.randomWords(4);
return sjcl.codec.hex.fromBits(hmac256(rand, rand));
}
function hmac256(salt: sjcl.BitArray, key: sjcl.BitArray) { function hmac256(salt: sjcl.BitArray, key: sjcl.BitArray) {
let hmac = new sjcl.misc.hmac(salt); let hmac = new sjcl.misc.hmac(salt);
return hmac.encrypt(key); return hmac.encrypt(key);

@ -7,7 +7,7 @@ import * as fetch from 'isomorphic-fetch';
import { byte, base64, stringToByteArray } from './Helpers'; import { byte, base64, stringToByteArray } from './Helpers';
import { CryptoManager, AsymmetricKeyPair, HMAC_SIZE_BYTES } from './Crypto'; import { CryptoManager, AsymmetricKeyPair, HMAC_SIZE_BYTES } from './Crypto';
export { CryptoManager, AsymmetricCryptoManager, AsymmetricKeyPair, deriveKey } from './Crypto'; export { CryptoManager, AsymmetricCryptoManager, AsymmetricKeyPair, deriveKey, genUid } from './Crypto';
class ExtendableError extends Error { class ExtendableError extends Error {
constructor(message: any) { constructor(message: any) {

@ -54,6 +54,20 @@ export const { fetchJournals } = createActions({
}, },
}); });
export const createJournal = createAction(
'CREATE_JOURNAL',
(etesync: CredentialsData, journal: EteSync.Journal) => {
const creds = etesync.credentials;
const apiBase = etesync.serviceApiUrl;
let journalManager = new EteSync.JournalManager(creds, apiBase);
return journalManager.create(journal);
},
(etesync: CredentialsData, journal: EteSync.Journal) => {
return { journal };
},
);
export const { fetchEntries, createEntries } = createActions({ export const { fetchEntries, createEntries } = createActions({
FETCH_ENTRIES: [ FETCH_ENTRIES: [
(etesync: CredentialsData, journalUid: string, prevUid: string | null) => { (etesync: CredentialsData, journalUid: string, prevUid: string | null) => {

@ -138,31 +138,62 @@ export const entries = handleAction(
Map({}) Map({})
); );
const journals = handleAction( const journals = handleActions(
actions.fetchJournals, {
(state: JournalsTypeImmutable, action: any) => { [actions.fetchJournals.toString()]: (state: JournalsTypeImmutable, action: any) => {
const newState = fetchTypeIdentityReducer(state, action); const newState = fetchTypeIdentityReducer(state, action);
// Compare the states and see if they are really different // Compare the states and see if they are really different
const oldJournals = state.get('value', null); const oldJournals = state.get('value', null);
const newJournals = newState.get('value', null); const newJournals = newState.get('value', null);
if (!oldJournals || !newJournals || (oldJournals.size !== newJournals.size)) { if (!oldJournals || !newJournals || (oldJournals.size !== newJournals.size)) {
return newState; return newState;
} }
let oldJournalHash = {}; let oldJournalHash = {};
oldJournals.forEach((x) => { oldJournals.forEach((x) => {
oldJournalHash[x.uid] = x.serialize(); oldJournalHash[x.uid] = x.serialize();
}); });
if (newJournals.every((journal: EteSync.Journal) => ( if (newJournals.every((journal: EteSync.Journal) => (
(journal.uid in oldJournalHash) && (journal.uid in oldJournalHash) &&
(journal.serialize().content === oldJournalHash[journal.uid].content) (journal.serialize().content === oldJournalHash[journal.uid].content)
))) { ))) {
return state; return state;
} else { } else {
return newState; return newState;
} }
},
[actions.createJournal.toString()]: (state: JournalsTypeImmutable, _action: any) => {
const action = { ..._action };
if (action.payload) {
action.payload = (action.meta === undefined) ? action.payload : action.meta.journal;
action.payload = [ action.payload ];
}
const newState = fetchTypeIdentityReducer(state, action, true);
// Compare the states and see if they are really different
const oldJournals = state.get('value', null);
const newJournals = newState.get('value', null);
if (!oldJournals || !newJournals || (oldJournals.size !== newJournals.size)) {
return newState;
}
let oldJournalHash = {};
oldJournals.forEach((x) => {
oldJournalHash[x.uid] = x.serialize();
});
if (newJournals.every((journal: EteSync.Journal) => (
(journal.uid in oldJournalHash) &&
(journal.serialize().content === oldJournalHash[journal.uid].content)
))) {
return state;
} else {
return newState;
}
},
}, },
new JournalsFetchRecord(), new JournalsFetchRecord(),
); );

Loading…
Cancel
Save