Getting New Avatar (Every 10 Mins)

This commit is contained in:
Sagi Dayan 2016-07-08 01:42:39 +03:00
parent b2808ef65e
commit aa20f92f88
2 changed files with 125 additions and 72 deletions

View file

@ -46,9 +46,11 @@ router.post('/upsert', (req, res) => {
if (err) { if (err) {
utils.response_500(res, "DB Error"); utils.response_500(res, "DB Error");
} else { } else {
res.send({ utils.convertToNoteObj(entity, (n) => {
status: "OK", res.send({
note: utils.convertToNoteObj(entity) status: "OK",
note: n
});
}); });
} }
}); });
@ -92,11 +94,13 @@ router.post('/upsert', (req, res) => {
var id = Number(response.mutationResults[0].key.path[0].id); var id = Number(response.mutationResults[0].key.path[0].id);
var key = db.key(['Note', id]); var key = db.key(['Note', id]);
db.get(key, (err, entity) => { db.get(key, (err, entity) => {
if (!err) res.send({ utils.convertToNoteObj(entity, (note) => {
status: "OK", if (!err) res.send({
note: utils.convertToNoteObj(entity) status: "OK",
note: note
});
else utils.response_500(res, "DB Error->" + err);
}); });
else utils.response_500(res, "DB Error->" + err);
}); });
} }
}); });
@ -121,11 +125,18 @@ router.get('/all', (req, res) => {
notes: (err ? null : []) notes: (err ? null : [])
}); });
} else { } else {
entities = entities.map(utils.convertToNoteObj); var notes = [];
res.send({ for (var i = 0; i < entities.length; i++) {
status: "OK", utils.convertToNoteObj(entities[i], (n) => {
notes: entities notes.push(n);
}); });
}
setTimeout(() => {
res.send({
status: "OK",
notes: notes
});
}, 1000);
} }
}); });
} }
@ -157,10 +168,12 @@ router.post('/like', (req, res) => {
} }
db.save(entity, (err) => {}); db.save(entity, (err) => {});
db.save(_entity, (err) => {}); db.save(_entity, (err) => {});
res.send({ utils.convertToNoteObj(_entity, (n) => {
status: "OK", res.send({
user: utils.convertToUserObj(entity), status: "OK",
note: utils.convertToNoteObj(_entity) user: utils.convertToUserObj(entity),
note: n
});
}); });
} }
}); });
@ -186,7 +199,6 @@ router.post('/getPublic', (req, res) => {
} else { } else {
var query = db.createQuery('Note') var query = db.createQuery('Note')
.filter('is_public', true); .filter('is_public', true);
db.runQuery(query, (err, entities) => { db.runQuery(query, (err, entities) => {
if (!entities) { if (!entities) {
res.send({ res.send({
@ -195,43 +207,50 @@ router.post('/getPublic', (req, res) => {
}); });
return; return;
} }
var notes = entities.map(utils.convertToNoteObj); var notes = [];
notes = notes.filter((n) => { for (var i = 0; i < entities.length; i++) {
if (Number(n.owner_id) !== Number(req.body.id)) return true; utils.convertToNoteObj(entities[i], (n) => {
return false; notes.push(n);
}); });
if (req.body.filter) {
if (req.body.filter.location) {
notes = notes.filter((note) => {
if (geolib.getDistance({
latitude: note.location.lat,
longitude: note.location.lng
}, {
latitude: req.body.filter.location.lat,
longitude: req.body.filter.location.lng
}) <= req.body.filter.location.distance) return true;
return false;
});
}
if (req.body.filter.tags) {
notes = notes.filter((note) => {
for (var i = 0; i < req.body.filter.tags.length; i++) {
if (note.tags.indexOf(req.body.filter.tags[i]) >= 0) return true;
}
return false;
});
}
if (req.body.filter.since) {
notes = notes.filter((note) => {
if (note.created_at > req.body.filter.since) return true;
return false;
});
}
} }
res.send({ setTimeout(() => {
status: "OK", notes = notes.filter((n) => {
notes: notes if (Number(n.owner_id) !== Number(req.body.id)) return true;
}); return false;
});
if (req.body.filter) {
if (req.body.filter.location) {
notes = notes.filter((note) => {
if (geolib.getDistance({
latitude: note.location.lat,
longitude: note.location.lng
}, {
latitude: req.body.filter.location.lat,
longitude: req.body.filter.location.lng
}) <= req.body.filter.location.distance) return true;
return false;
});
}
if (req.body.filter.tags) {
notes = notes.filter((note) => {
for (var i = 0; i < req.body.filter.tags.length; i++) {
if (note.tags.indexOf(req.body.filter.tags[i]) >= 0) return true;
}
return false;
});
}
if (req.body.filter.since) {
notes = notes.filter((note) => {
if (note.created_at > req.body.filter.since) return true;
return false;
});
}
}
res.send({
status: "OK",
notes: notes
});
}, 1000);
}); });
} }
}); });
@ -246,8 +265,7 @@ router.post('/delete', (req, res) => {
} }
var key = db.key(['Note', Number(req.body.nid)]); var key = db.key(['Note', Number(req.body.nid)]);
db.get(key, (err, entity) => { db.get(key, (err, entity) => {
if (!entity || Number(utils.convertToNoteObj(entity) if (!entity || Number(entity.data.owner_id) != Number(req.body.uid)) {
.owner_id) != Number(req.body.uid)) {
utils.response_400(res, null, "The Note not exists OR he is not the owner of the note"); utils.response_400(res, null, "The Note not exists OR he is not the owner of the note");
return; return;
} }

View file

@ -1,3 +1,9 @@
var db = require('./DBManager');
var avatars = {};
setInterval(() => {
console.log('Clearing Cash');
avatars = {};
}, 10 * 60 * 1000);
module.exports = { module.exports = {
response_400: (res, example, msg) => { response_400: (res, example, msg) => {
res.statusCode = 400; res.statusCode = 400;
@ -29,22 +35,51 @@ module.exports = {
liked_notes_id: user.data.liked_notes_id liked_notes_id: user.data.liked_notes_id
}; };
}, },
convertToNoteObj: (note) => { convertToNoteObj: (note, callback) => {
return { if (!avatars[note.data.owner_id]) {
id: note.key.id, console.log('Avatar Not In Cash... Getting new one');
title: note.data.title, var key = db.key(['User', Number(note.data.owner_id)]);
location: { db.get(key, (err, entity) => {
lat: note.data.lat, if (!err) {
lng: note.data.lng, avatars[note.data.owner_id] = entity.data.avatar;
address: note.data.address console.log('Saved Avatar');
}, callback({
body: note.data.body, id: note.key.id,
owner_id: note.data.owner_id, title: note.data.title,
is_public: note.data.is_public, location: {
created_at: note.data.created_at, lat: note.data.lat,
likes: note.data.likes, lng: note.data.lng,
tags: note.data.tags, address: note.data.address
avatar: note.data.avatar },
}; body: note.data.body,
owner_id: note.data.owner_id,
is_public: note.data.is_public,
created_at: note.data.created_at,
likes: note.data.likes,
tags: note.data.tags,
avatar: entity.data.avatar
});
}
});
} else {
console.log('Avatar found In Cash');
callback({
id: note.key.id,
title: note.data.title,
location: {
lat: note.data.lat,
lng: note.data.lng,
address: note.data.address
},
body: note.data.body,
owner_id: note.data.owner_id,
is_public: note.data.is_public,
created_at: note.data.created_at,
likes: note.data.likes,
tags: note.data.tags,
avatar: avatars[note.data.owner_id]
});
}
} }
}; };