Skip to content

Instantly share code, notes, and snippets.

@punto-
Last active March 16, 2022 19:14
Show Gist options
  • Save punto-/7ec3cbc344292eb10d0abc0d46682aa0 to your computer and use it in GitHub Desktop.
Save punto-/7ec3cbc344292eb10d0abc0d46682aa0 to your computer and use it in GitHub Desktop.
function _fetch(document, field, first, count) {
var ret = [];
for (let i=first; i<first + count; i++) {
ret.push(document[field][i]);
};
return ret;
};
var dummy = {
user_feed: {
neo: [
{
id: 100,
parent: null,
author: "trinity",
content: "Hello this is a tweet",
timestamp: 1647426950,
},
{
id: 101,
parent: null,
author: "morpheus",
content: "hey check out this cool movie",
media: ["https://entradas.matrixresurrecciones.com.ar/assets/images/fullbanner.jpg"],
timestamp: 1647426950,
},
{
id: 102,
parent: null,
author: "agent_smith",
content: "hello what do you guys think of this controversial topic?",
timestamp: 1647426950,
},
]
},
user_content: {
neo: [
{
id: 106,
parent: null,
author: "neo",
content: "woah",
timestamp: 1647426950,
},
{
id: 106,
parent: null,
author: "neo",
content: "Nice day outside",
timestamp: 1647426950,
},
{
id: 107,
parent: null,
author: "neo",
content: "look at my lunch",
media: ["https://images.immediate.co.uk/production/volatile/sites/30/2013/05/HummusBowl-65a0050.jpg", "https://media-cdn.tripadvisor.com/media/photo-s/14/59/f4/d6/mahesh-lunch-home.jpg"],
timestamp: 1647426950,
},
{
id: 108,
parent: null,
author: "neo",
content: "operator I need an exit",
timestamp: 1647426950,
},
],
trinity: [
{
id: 104,
parent: 102,
author: "trinity",
content: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
timestamp: 1647426950,
},
],
},
thread: {
102: [
{
id: 102,
parent: null,
author: "agent_smith",
content: "hello what do you guys think of this controversial topic?",
timestamp: 1647426950,
},
{
id: 103,
parent: 102,
author: "neo",
content: "I think it sucks",
timestamp: 1647426950,
},
{
id: 104,
parent: 102,
author: "architect",
content: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
timestamp: 1647426950,
},
{
id: 105,
parent: 102,
author: "cypher",
content: "I think it's ok",
timestamp: 1647426950,
},
{
id: 105,
parent: 102,
author: "oracle",
content: "reported",
timestamp: 1647426950,
},
],
},
search: {
"#whatisthematrix": [
{
id: 106,
parent: null,
author: "neo",
content: "woah",
timestamp: 1647426950,
},
{
id: 105,
parent: 102,
author: "oracle",
content: "reported",
timestamp: 1647426950,
},
{
id: 103,
parent: 102,
author: "neo",
content: "I think it sucks",
timestamp: 1647426950,
},
{
id: 104,
parent: 102,
author: "architect",
content: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
timestamp: 1647426950,
},
]
},
user_info: {
neo: [{
real_name: "Thomas Anderson",
bio: "I am the one. In my free time I like to read books and hack code.",
email: "tom@deusmachina.com",
icon: "https://a.wattpad.com/useravatar/_phaedra_.256.677632.jpg",
follows: ["trinity", "agent_smith"],
}],
trinity: [{
bio: "Hello I'm a middle aged housewife or something, I like bikes",
links: {
ig: "http://www.instagram.com/trinity",
},
icon: "https://i.ebayimg.com/thumbs/images/g/Zc0AAOSwviRfj617/s-l96.jpg",
follows: ["leather_fetish"],
}],
},
user_id_default: "neo",
fetch_list : function(document, field, first, count) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
if (_global_network_fail) {
reject( { action: "fetch_list", "field": field, result: "error"} );
} else {
resolve(_fetch(document, field, first, count));
};
}, 100);
});
},
fetch_field: function(document, field) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
if (_global_network_fail) {
reject( { action: "fetch_field", "field": field, result: "error"} );
} else {
resolve(_fetch(document, field, 0, 1)[0]);
};
}, 100);
});
},
};
var db = dummy;
// this is to include the contents of db_dummy.js, works only on node, should be done in a portable way
var fs = require('fs');
eval(fs.readFileSync('db_dummy.js')+'');
var feed = {
fetch_by_user_feed: function(user_id, first, count) {
return db.fetch_list(db.user_feed, user_id, first, count);
},
fetch_by_user_content: function(user_id, first, count) {
return db.fetch_list(db.user_content, user_id, first, count);
},
fetch_by_thread: function(thread_id, first, count) {
return db.fetch_list(db.thread, thread_id, first, count);
},
fetch_by_search: function(search_term, first, count) {
return db.fetch_list(db.search, search_term, first, count);
},
user_sign_in: function() {
return db.fetch_field(db.user_info, db.user_id_default);
},
fetch_user_profile: function(user_id) {
return db.fetch_field(db.user_info, user_id);
},
post: function(item) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
if (_global_network_fail) {
reject( { action: "post", "item": item, result: "error"} );
} else {
resolve( { action: "post", "item": item, result: "ok"} );
};
}, 100);
});
},
like: function(item_id) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
if (_global_network_fail) {
reject({ action: "like", "item_id": item_id, result: "error"});
} else {
resolve({ action: "like", "item_id": item_id, result: "ok"} );
};
}, 100);
});
},
follow: function(user_id, follow) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
if (_global_network_fail) {
reject({ action: "follow", "params": [user_id, follow], result: "error"});
} else {
if (db.user_info[db.user_id_default]) {
if (follow)
db.user_info[db.user_id_default].follows.push(user_id);
else
db.user_info[db.user_id_default].follows = db.user_info[db.user_id_default].follows.filter(function(value, index, arr) {
return value != follow;
});
};
resolve({ action: "follow", "params": [user_id, follow], result: "ok"});
};
}, 100);
});
},
};
// all methods return promises
var feed = {
user_sign_in: function() { },
fetch_user_profile: function(user_id) { },
fetch_by_user_feed: function(user_id, first, count) { },
fetch_by_user_content: function(user_id, first, count) { },
fetch_by_thread: function(thread_id, first, count) { },
fetch_by_search: function(search_term, first, count) { },
post: function(item) { },
like: function(item_id) { },
// use parameter follow=false to unfollow
follow: function(user_id, follow) { },
};
var fs = require('fs');
eval(fs.readFileSync('feed_dummy.js')+'');
var _global_network_fail = false;
feed.user_sign_in().then(console.log);
feed.fetch_user_profile("trinity").then(console.log);
feed.fetch_by_user_feed("neo", 0, 10).then(console.log);
feed.fetch_by_user_content("trinity").then(console.log);
feed.fetch_by_thread(102, 0, 10).then(console.log);
feed.fetch_by_search("#whatisthematrix", 0, 10).then(console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment