|
| 1 | +import _ from 'lodash' |
| 2 | +import moment from 'moment' |
| 3 | +import keygen from 'boost/keygen' |
| 4 | +import dataStore from 'boost/dataStore' |
| 5 | +import { request, WEB_URL } from 'boost/api' |
| 6 | + |
| 7 | +function isSameDate (a, b) { |
| 8 | + a = moment(a).utcOffset(+540).format('YYYYMMDD') |
| 9 | + b = moment(b).utcOffset(+540).format('YYYYMMDD') |
| 10 | + |
| 11 | + return a === b |
| 12 | +} |
| 13 | + |
| 14 | +export function init () { |
| 15 | + let records = getAllRecords() |
| 16 | + if (records == null) { |
| 17 | + saveAllRecords([]) |
| 18 | + } |
| 19 | + |
| 20 | + postRecords() |
| 21 | + if (window != null) { |
| 22 | + window.addEventListener('online', postRecords) |
| 23 | + window.setInterval(postRecords, 1000 * 60 * 60 * 24) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +export function getClientKey () { |
| 28 | + let clientKey = localStorage.getItem('clientKey') |
| 29 | + if (!_.isString(clientKey) || clientKey.length !== 40) { |
| 30 | + clientKey = keygen() |
| 31 | + localStorage.setItem('clientKey', clientKey) |
| 32 | + } |
| 33 | + |
| 34 | + return clientKey |
| 35 | +} |
| 36 | + |
| 37 | +export function getAllRecords () { |
| 38 | + return JSON.parse(localStorage.getItem('activityRecords')) |
| 39 | +} |
| 40 | + |
| 41 | +export function saveAllRecords (records) { |
| 42 | + localStorage.setItem('activityRecords', JSON.stringify(records)) |
| 43 | +} |
| 44 | + |
| 45 | +/* |
| 46 | +Post all records(except today) |
| 47 | +and remove all posted records |
| 48 | +*/ |
| 49 | +export function postRecords (data) { |
| 50 | + let records = getAllRecords() |
| 51 | + records = records.filter(record => { |
| 52 | + return !isSameDate(new Date(), record.date) |
| 53 | + }) |
| 54 | + |
| 55 | + if (records.length === 0) { |
| 56 | + console.log('No records to post') |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + console.log('posting...', records) |
| 61 | + let input = { |
| 62 | + clientKey: getClientKey(), |
| 63 | + records |
| 64 | + } |
| 65 | + return request.post(WEB_URL + 'apis/activity') |
| 66 | + .send(input) |
| 67 | + .then(res => { |
| 68 | + let records = getAllRecords() |
| 69 | + let todayRecord = _.find(records, record => { |
| 70 | + return isSameDate(new Date(), record.date) |
| 71 | + }) |
| 72 | + if (todayRecord != null) saveAllRecords([todayRecord]) |
| 73 | + else saveAllRecords([]) |
| 74 | + }) |
| 75 | + .catch(err => { |
| 76 | + console.error(err) |
| 77 | + }) |
| 78 | +} |
| 79 | + |
| 80 | +export function emit (type, data) { |
| 81 | + let records = getAllRecords() |
| 82 | + |
| 83 | + let index = _.findIndex(records, record => { |
| 84 | + return isSameDate(new Date(), record.date) |
| 85 | + }) |
| 86 | + |
| 87 | + let todayRecord |
| 88 | + if (index < 0) { |
| 89 | + todayRecord = {date: new Date()} |
| 90 | + records.push(todayRecord) |
| 91 | + } |
| 92 | + else todayRecord = records[index] |
| 93 | + console.log(type) |
| 94 | + switch (type) { |
| 95 | + case 'ARTICLE_CREATE': |
| 96 | + case 'ARTICLE_UPDATE': |
| 97 | + case 'ARTICLE_DESTROY': |
| 98 | + case 'FOLDER_CREATE': |
| 99 | + case 'FOLDER_UPDATE': |
| 100 | + case 'FOLDER_DESTROY': |
| 101 | + case 'FINDER_OPEN': |
| 102 | + case 'FINDER_COPY': |
| 103 | + todayRecord[type] = todayRecord[type] == null |
| 104 | + ? 1 |
| 105 | + : todayRecord[type] + 1 |
| 106 | + |
| 107 | + break |
| 108 | + } |
| 109 | + |
| 110 | + let storeData = dataStore.getData() |
| 111 | + todayRecord.FOLDER_COUNT = _.isArray(storeData.folders) ? storeData.folders.length : 0 |
| 112 | + todayRecord.ARTICLE_COUNT = _.isArray(storeData.articles) ? storeData.articles.length : 0 |
| 113 | + |
| 114 | + saveAllRecords(records) |
| 115 | +} |
| 116 | + |
| 117 | +export default { |
| 118 | + init, |
| 119 | + emit, |
| 120 | + getClientKey, |
| 121 | + postRecords |
| 122 | +} |
0 commit comments