Skip to content

Instantly share code, notes, and snippets.

@samsoft00
Last active June 8, 2022 14:54
Show Gist options
  • Save samsoft00/73783c6b633f11f515e9577c364602b3 to your computer and use it in GitHub Desktop.
Save samsoft00/73783c6b633f11f515e9577c364602b3 to your computer and use it in GitHub Desktop.
exports.getLists = async (query) => {
const sort = { _id: -1 }
// Max of 30 records
query.limit = parseInt(query.limit || 10)
const limit = query.limit > 30 ? 30 : query.limit
let hasNext = false
let hasPrev = false
const q = {
domain: query.domain,
archived: { $exists: false }
}
if (query.previous_cursor) {
q._id = { $gt: dbo.id(query.previous_cursor) }
sort._id = 1
} else if (query.next_cursor) {
q._id = { $lt: dbo.id(query.next_cursor) }
}
const r = await dbo.db().collection('lists').find(q, {
sort,
limit
}).toArray()
if (query.previous_cursor) r.reverse()
if (r.length) {
q._id = {
$lt: dbo.id(r[r.length - 1]._id)
}
hasNext = !!await dbo.db().collection('lists').findOne(q)
q._id = {
$gt: dbo.id(r[0]._id)
}
hasPrev = !!await dbo.db().collection('lists').findOne(q)
}
const o = {
data: r.map(l => new ListResponse(l))
}
if (hasNext) {
o.next_cursor = r[r.length - 1]._id + ''
}
if (hasPrev) {
o.previous_cursor = r[0]._id + ''
}
return o
}
exports.avatar = (email, uid, s) => {
const initials = email ? email[0] : 'oo'
const backup = `https://tiley.herokuapp.com/avatar/${uid}/${initials}.png`
// const backup = `https://avatar.tobi.sh/${uid}`
if (!email) {
return backup
}
email = email.trim().toLowerCase()
if (!s) {
s = 200
}
const hash = crypto.createHash('md5').update(email).digest('hex').toString()
return `https://www.gravatar.com/avatar/${hash}?s=${s}&d=${encodeURIComponent(backup)}`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment