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 {
utils.convertToNoteObj(entity, (n) => {
res.send({ res.send({
status: "OK", status: "OK",
note: utils.convertToNoteObj(entity) note: n
});
}); });
} }
}); });
@ -92,12 +94,14 @@ 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) => {
utils.convertToNoteObj(entity, (note) => {
if (!err) res.send({ if (!err) res.send({
status: "OK", status: "OK",
note: utils.convertToNoteObj(entity) 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 = [];
for (var i = 0; i < entities.length; i++) {
utils.convertToNoteObj(entities[i], (n) => {
notes.push(n);
});
}
setTimeout(() => {
res.send({ res.send({
status: "OK", status: "OK",
notes: entities 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) => {});
utils.convertToNoteObj(_entity, (n) => {
res.send({ res.send({
status: "OK", status: "OK",
user: utils.convertToUserObj(entity), user: utils.convertToUserObj(entity),
note: utils.convertToNoteObj(_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,7 +207,13 @@ router.post('/getPublic', (req, res) => {
}); });
return; return;
} }
var notes = entities.map(utils.convertToNoteObj); var notes = [];
for (var i = 0; i < entities.length; i++) {
utils.convertToNoteObj(entities[i], (n) => {
notes.push(n);
});
}
setTimeout(() => {
notes = notes.filter((n) => { notes = notes.filter((n) => {
if (Number(n.owner_id) !== Number(req.body.id)) return true; if (Number(n.owner_id) !== Number(req.body.id)) return true;
return false; return false;
@ -232,6 +250,7 @@ router.post('/getPublic', (req, res) => {
status: "OK", status: "OK",
notes: notes 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,8 +35,15 @@ 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]) {
console.log('Avatar Not In Cash... Getting new one');
var key = db.key(['User', Number(note.data.owner_id)]);
db.get(key, (err, entity) => {
if (!err) {
avatars[note.data.owner_id] = entity.data.avatar;
console.log('Saved Avatar');
callback({
id: note.key.id, id: note.key.id,
title: note.data.title, title: note.data.title,
location: { location: {
@ -44,7 +57,29 @@ module.exports = {
created_at: note.data.created_at, created_at: note.data.created_at,
likes: note.data.likes, likes: note.data.likes,
tags: note.data.tags, tags: note.data.tags,
avatar: note.data.avatar 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]
});
}
} }
}; };