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) {
utils.response_500(res, "DB Error");
} else {
utils.convertToNoteObj(entity, (n) => {
res.send({
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 key = db.key(['Note', id]);
db.get(key, (err, entity) => {
utils.convertToNoteObj(entity, (note) => {
if (!err) res.send({
status: "OK",
note: utils.convertToNoteObj(entity)
note: note
});
else utils.response_500(res, "DB Error->" + err);
});
});
}
});
});
@ -121,11 +125,18 @@ router.get('/all', (req, res) => {
notes: (err ? null : [])
});
} 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({
status: "OK",
notes: entities
notes: notes
});
}, 1000);
}
});
}
@ -157,10 +168,12 @@ router.post('/like', (req, res) => {
}
db.save(entity, (err) => {});
db.save(_entity, (err) => {});
utils.convertToNoteObj(_entity, (n) => {
res.send({
status: "OK",
user: utils.convertToUserObj(entity),
note: utils.convertToNoteObj(_entity)
note: n
});
});
}
});
@ -186,7 +199,6 @@ router.post('/getPublic', (req, res) => {
} else {
var query = db.createQuery('Note')
.filter('is_public', true);
db.runQuery(query, (err, entities) => {
if (!entities) {
res.send({
@ -195,7 +207,13 @@ router.post('/getPublic', (req, res) => {
});
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) => {
if (Number(n.owner_id) !== Number(req.body.id)) return true;
return false;
@ -232,6 +250,7 @@ router.post('/getPublic', (req, res) => {
status: "OK",
notes: notes
});
}, 1000);
});
}
});
@ -246,8 +265,7 @@ router.post('/delete', (req, res) => {
}
var key = db.key(['Note', Number(req.body.nid)]);
db.get(key, (err, entity) => {
if (!entity || Number(utils.convertToNoteObj(entity)
.owner_id) != Number(req.body.uid)) {
if (!entity || Number(entity.data.owner_id) != Number(req.body.uid)) {
utils.response_400(res, null, "The Note not exists OR he is not the owner of the note");
return;
}

View file

@ -1,3 +1,9 @@
var db = require('./DBManager');
var avatars = {};
setInterval(() => {
console.log('Clearing Cash');
avatars = {};
}, 10 * 60 * 1000);
module.exports = {
response_400: (res, example, msg) => {
res.statusCode = 400;
@ -29,8 +35,15 @@ module.exports = {
liked_notes_id: user.data.liked_notes_id
};
},
convertToNoteObj: (note) => {
return {
convertToNoteObj: (note, callback) => {
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,
title: note.data.title,
location: {
@ -44,7 +57,29 @@ module.exports = {
created_at: note.data.created_at,
likes: note.data.likes,
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]
});
}
}
};