var db = require('./DBManager'); var avatars = {}; setInterval(() => { console.log('Clearing Cash'); avatars = {}; }, 3 * 60 * 1000); module.exports = { clearCash: () => { avatars = {}; }, sort: (a, b) => { return b.created_at - a.created_at; }, response_400: (res, example, msg) => { res.statusCode = 400; var r = { example_body: example, status: 'Error', code: 400, message: 'Bad request, ' + (msg || 'see the example for more information') }; res.send(r); }, response_500: (res, msg) => { res.statusCode = 500; var r = { status: 'Error', code: 500, message: msg }; res.send(r); }, convertToUserObj: (user) => { return { id: user.key.id, username: user.data.username, password: user.data.password, email: user.data.email, creation_time: user.data.creation_time, avatar: user.data.avatar, liked_notes_id: user.data.liked_notes_id }; }, 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] }); } } };