Skip to content

Commit

Permalink
more refactoring, unlimited storage, fetching of all facts for the cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandru Badiu committed Sep 28, 2016
1 parent cc4457e commit 3499582
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 74 deletions.
Binary file renamed dist/Factual.0.0.8.crx → dist/Factual.0.0.9.crx
Binary file not shown.
5 changes: 3 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Factual",
"description": "Prima extensie de fact-checking pe politicile și declarațiile publice din România.",
"version": "0.0.8",
"version": "0.0.9",
"icons": {
"16": "assets/factual_icon_16x16.png",
"32": "assets/factual_icon_32x32.png",
Expand All @@ -18,7 +18,8 @@
"permissions": [
"storage",
"tabs",
"alarms"
"alarms",
"unlimitedStorage"
],
"externally_connectable": {
"matches": []
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "factchecker-plugin-chrome",
"version": "0.0.8",
"version": "0.0.9",
"description": "Factual Chrome extension.",
"main": "index.js",
"scripts": {
Expand Down
113 changes: 113 additions & 0 deletions src/js/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import Rx from 'rx';
import config from './config';
import { encodeParams, getUrlCode, getShortUrl } from './util';
import { convertFact, convertFacts } from './fact';

let factsCache = [];

export const setFactsCache = (facts) => {
factsCache = facts;
};

export const getFactsFromCache = (url) => {
return _.filter(factsCache, { source: getShortUrl(url) });
};

export const getFacts = (url, uid, client, origin) => {
return new Promise((resolve) => {
const urlCode = getUrlCode(url);
const params = {
q: urlCode,
u: uid,
client,
origin,
};

$.ajax({
dataType: 'json',
url: `http:\/\/${config.api}?${encodeParams(params)}`,
}).then((response) => {
const facts = [];
if (response.error) {
return resolve(facts);
}

if (response.data) {
Object.keys(response.data).forEach(id => facts.push(convertFact(response.data[id])));
}

resolve(facts);
});
});
};

const getAllPage = (page, uid, client, origin) => {
const params = {
page,
q: 'all',
u: uid,
client,
origin,
};

return Rx.Observable.fromPromise(new Promise((resolve) => {
$.ajax({
dataType: 'json',
url: `http:\/\/${config.api}?${encodeParams(params)}`,
}).then((response) => {
const result = {
total_pages: 0,
current_page: 0,
data: [],
};

if (response.error) {
return resolve(result);
}

result.total_pages = response.total_pages;
result.current_page = response.current_page;

if (response.data) {
result.data = convertFacts(response.data);
}

return resolve(result);
});
}));
};

const getPagedItems = (index, uid, client, origin) => {
return getAllPage(index, uid, client, origin)
.flatMap((response) => {
const result = Rx.Observable.return(response.data);

if (response.total_pages > response.current_page) {
const nextPage = response.current_page + 1;
return result.concat(getPagedItems(nextPage, uid, client, origin));
}

return result;
});
};

export const getAllFacts = (uid, client, origin) => {
return new Promise((resolve) => {
const facts$ = getPagedItems(1, uid, client, origin);
let facts = [];

facts$
.subscribe(
(result) => {
facts = facts.concat(result);
},
(error) => {
console.log('error');
console.log(error);
},
() => {
resolve(facts);
}
);
});
};
82 changes: 11 additions & 71 deletions src/js/factual_background.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
*
* @author Alexandru Badiu <[email protected]>
*/
import config from './config';
import FactualBase from './factual_base';
import { getUserToken, encodeParams, getUrlCode, getShortUrl } from './util';
import { convertFact, convertFacts } from './fact';
import { getFacts, getAllFacts, getFactsFromCache, setFactsCache } from './api';
import { getUserToken } from './util';

require('../css/factual.scss');

Expand All @@ -25,7 +24,7 @@ class FactualBackground extends FactualBase {
uid: '',
};

chrome.alarms.clear(this.alarmName);
// chrome.alarms.clear(this.alarmName);

chrome.storage.sync.get('settings', (result) => {
if (result) {
Expand All @@ -38,19 +37,17 @@ class FactualBackground extends FactualBase {
}

this.setupEvents();
// this.setupAlarms();
this.setupAlarms();
});

chrome.storage.local.get('facts', (data) => {
this.cachedFacts = data.facts;
setFactsCache(data.facts);
});
}

updateCachedFacts(facts) {
this.cachedFacts = facts;
setFactsCache(facts);
chrome.storage.local.set({ facts: this.cachedFacts });
console.log('updated cached facts');
console.log(this.cachedFacts);
}

toolbarClicked() {
Expand Down Expand Up @@ -106,13 +103,13 @@ class FactualBackground extends FactualBase {
}

if (request.action === 'facts-get') {
const cfacts = this.getFactsFromCache(request.url);
const cfacts = getFactsFromCache(request.url);
if (cfacts.length) {
sendResponse(cfacts);
return false;
}

this.getFacts(request.url)
getFacts(request.url, this.settings.uid, 'chrome_extension', 'site')
.then((facts) => {
sendResponse(facts);
});
Expand All @@ -133,7 +130,7 @@ class FactualBackground extends FactualBase {

onAlarm(alarm) {
if (alarm.name === this.alarmName) {
this.getAllFacts()
getAllFacts(this.settings.uid, 'chrome_extension', 'site')
.then((facts) => {
this.updateCachedFacts(facts);
});
Expand All @@ -153,69 +150,12 @@ class FactualBackground extends FactualBase {
const hasAlarm = alarms.some(a => a.name === this.alarmName);
if (!hasAlarm) {
chrome.alarms.create(this.alarmName, {
delayInMinutes: 0.1,
periodInMinutes: 0.1,
delayInMinutes: 1,
periodInMinutes: 60 * 24,
});
}
});
}

getFactsFromCache(url) {
return _.filter(this.cachedFacts, { source: getShortUrl(url) });
}

getFacts(url) {
return new Promise((resolve) => {
const urlCode = getUrlCode(url);
const params = {
q: urlCode,
u: this.settings.uid,
client: 'chrome_extension',
origin: 'site',
};

$.ajax({
dataType: 'json',
url: `http:\/\/${config.api}?${encodeParams(params)}`,
}).then((response) => {
const facts = [];
if (response.error) {
return resolve(facts);
}

if (response.data) {
Object.keys(response.data).forEach(id => facts.push(convertFact(response.data[id])));
}

resolve(facts);
});
});
}

getAllFacts() {
return new Promise((resolve) => {
const params = {
q: 'all',
u: this.settings.uid,
client: 'chrome_extension',
origin: 'site',
};
$.ajax({
dataType: 'json',
url: `http:\/\/${config.api}?${encodeParams(params)}`,
}).then((response) => {
if (response.error) {
return resolve([]);
}

if (response.data) {
return resolve(convertFacts(response.data));
}

return resolve([]);
});
});
}
}

export default new FactualBackground();

0 comments on commit 3499582

Please sign in to comment.