jeffalo / my-ocular

backend for ocular
https://ocular.jeffalo.net/docs/about#what-iswas-my-ocular
9 stars 10 forks source link

Store userids in db for profile pictures #24

Open FunctionalMetatable opened 3 years ago

FunctionalMetatable commented 3 years ago

Less requests to scratch's servers.


// [..]
const userIds = db.get("userIds")
// [..]
app.get("/api/user/:user/picture", async (req, res) => {
  let user = await userIds.findOne({
    name: { $regex: new RegExp(`^${req.params.user}$`) }
  })
  if (!user) {
    let res = await fetch(`https://api.scratch.mit.edu/users/${req.params.user}/`)
    let { id, username } = await res.json() 

    user = await userIds.insert({ id, name: username })
  }

  res.redirect(301, `https://cdn2.scratch.mit.edu/get_image/user/${user.id}_500x500.png?my-ocular`)
})
jeffalo commented 3 years ago

this is a good idea.. but it'll break if someone changes their username. i will implement caching on the endpoint though