imalsogreg / reffit

Community mini-reviews for scientific papers
reffit.com
GNU General Public License v3.0
33 stars 3 forks source link

Rendering user reputation in title bar leading to lots of code duplication #18

Open imalsogreg opened 10 years ago

imalsogreg commented 10 years ago

Working on displaying logged-in-user's reputation next to his/her name in the top nav bar. This is requiring modification of every Handler that renders a page, and passing the User and all Documents to all these handlers (because the reputation-computing function needs that info).

Snap.Snaplet.Auth gives the splice , which is automatically spliced out of every AuthManager'ed template.

Can I possibly do something similar? Snaplet.ReffitUser that automatically handles loggedInUser's reputation printing?

Also, printing the reputations of comment authors and discussion point writers is making for a lot of duplicate code. Is there a way around it?

BlairArchibald commented 10 years ago

We should definitely use a similar model to that used in Snap.Snaplet.Auth by modifying the heist state at startup. This way we keep the Handlers separate from any rendering logic and should be able to just duplicate the tags in the html (which is much nicer than duplicating code within Handlers).

We should probably look to refactor more of the handlers in a similar fashion!

You would need a function i.e addReffitHesitConfig h. Which will be called within the main reffit application initializer function.

imalsogreg commented 10 years ago

Thanks, that helped me think about this. Right now reputation is never stored in the database - it's always recomputed when its needed (every page load). The situation will be different when we move to SQL b/c we don't want to do all those database queries for every page render. So this particular bit of ugliness, I will wait to fix until I see how the integration of Persist shakes out (unless you see a trivial way to get the fix into the current code - which you might).

I'm still trying to understand snaplets & heist better so I can answer questions like: "could I make a spice available by a similar mechanism to Snap.Snaplet.Auth's, throughout the site, even though computing it requires at least one database lookup per page load? Or is it better to accomplish that in Site.hs with something looking like this (prob doesn't compile - just laying out general idea):

wrapsite $ \site -> do
  docs <- query QueryAllDocs
  myUser <- lookupReffitUser =<< currentUser
  modifyHeistState . bindSplices $ reputationSplices docs myUser

" The alternative under Persistent would look similar, except I would be making some query into a ReffitUsers table, and the reputation would be stored right in that user's row.