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 {
res.send({
status: "OK",
note: utils.convertToNoteObj(entity)
utils.convertToNoteObj(entity, (n) => {
res.send({
status: "OK",
note: n
});
});
}
});
@ -92,11 +94,13 @@ 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) => {
if (!err) res.send({
status: "OK",
note: utils.convertToNoteObj(entity)
utils.convertToNoteObj(entity, (note) => {
if (!err) res.send({
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 : [])
});
} else {
entities = entities.map(utils.convertToNoteObj);
res.send({
status: "OK",
notes: entities
});
var notes = [];
for (var i = 0; i < entities.length; i++) {
utils.convertToNoteObj(entities[i], (n) => {
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) => {});
res.send({
status: "OK",
user: utils.convertToUserObj(entity),
note: utils.convertToNoteObj(_entity)
utils.convertToNoteObj(_entity, (n) => {
res.send({
status: "OK",
user: utils.convertToUserObj(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,43 +207,50 @@ router.post('/getPublic', (req, res) => {
});
return;
}
var notes = entities.map(utils.convertToNoteObj);
notes = notes.filter((n) => {
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;
});
}
var notes = [];
for (var i = 0; i < entities.length; i++) {
utils.convertToNoteObj(entities[i], (n) => {
notes.push(n);
});
}
res.send({
status: "OK",
notes: notes
});
setTimeout(() => {
notes = notes.filter((n) => {
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)]);
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,22 +35,51 @@ module.exports = {
liked_notes_id: user.data.liked_notes_id
};
},
convertToNoteObj: (note) => {
return {
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: note.data.avatar
};
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: {
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: 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]
});
}
}
};