Changeset 0a75356 in Klonkt for src/routes/posts.js


Ignore:
Timestamp:
06/26/2026 11:55:09 PM (2 months ago)
Author:
Robin Genis <roboburr@…>
Branches:
main
Children:
7e66e7e
Parents:
9d34855
Message:

feat(news): feed like+boost toggle in place (fetch, no reload, no banner)

/news/like and /news/boost are now single toggling routes that return JSON {on} for a
fetch request; news.ejs posts via fetch and flips the button is-on without a reload or a
success banner. No-JS falls back to a plain redirect. getTimelineReaction reads state.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/routes/posts.js

    r9d34855 r0a75356  
    693693});
    694694
     695// Like / unlike a feed post — a toggle. Fetch request → JSON {on} (stay on the page,
     696// no banner); no-JS → redirect back.
    695697router.post('/news/like', requireSiteManager, async (req, res) => {
    696698  const site = res.locals.site;
    697699  const note = (req.body.note || '').toString();
     700  let on = false;
    698701  if (site && note) {
    699     try { await ActivityPubService.sendInteraction(site, 'like', note, (req.body.author || '').toString()); } catch (e) { /* ignore */ }
    700     ActivityPubService.markLiked(site.slug, note);
    701   }
    702   res.redirect('/news?success=' + encodeURIComponent('Geliket ⭐'));
    703 });
    704 
    705 router.post('/news/unlike', requireSiteManager, async (req, res) => {
     702    on = !ActivityPubService.getTimelineReaction(site.slug, note).liked;
     703    try { await ActivityPubService.sendInteraction(site, on ? 'like' : 'unlike', note, (req.body.author || '').toString()); } catch (e) { /* ignore */ }
     704    if (on) ActivityPubService.markLiked(site.slug, note); else ActivityPubService.unmarkLiked(site.slug, note);
     705  }
     706  if (req.get('X-Requested-With') === 'fetch') return res.json({ ok: true, on });
     707  res.redirect('/news');
     708});
     709
     710// Boost / unboost a feed post — a toggle. markBoosted also surfaces it in the Cirkel.
     711router.post('/news/boost', requireSiteManager, async (req, res) => {
    706712  const site = res.locals.site;
    707713  const note = (req.body.note || '').toString();
     714  let on = false;
    708715  if (site && note) {
    709     try { await ActivityPubService.sendInteraction(site, 'unlike', note, (req.body.author || '').toString()); } catch (e) { /* ignore */ }
    710     ActivityPubService.unmarkLiked(site.slug, note);
    711   }
    712   res.redirect('/news?success=' + encodeURIComponent('Like ingetrokken'));
    713 });
    714 
    715 router.post('/news/boost', requireSiteManager, async (req, res) => {
    716   const site = res.locals.site;
    717   const note = (req.body.note || '').toString();
    718   if (site && note) {
    719     try { await ActivityPubService.sendInteraction(site, 'boost', note, (req.body.author || '').toString()); } catch (e) { /* ignore */ }
    720     ActivityPubService.markBoosted(site.slug, note); // also surface it in the Cirkel (mixed by date)
    721   }
    722   res.redirect('/news?success=' + encodeURIComponent('Geboost 🔁'));
    723 });
    724 
    725 // Unboost (retract): send Undo(Announce) + clear the local flag.
    726 router.post('/news/unboost', requireSiteManager, async (req, res) => {
    727   const site = res.locals.site;
    728   const note = (req.body.note || '').toString();
    729   if (site && note) {
    730     try { await ActivityPubService.sendInteraction(site, 'unboost', note, (req.body.author || '').toString()); } catch (e) { /* ignore */ }
    731     ActivityPubService.unmarkBoosted(site.slug, note);
    732   }
    733   res.redirect('/news?success=' + encodeURIComponent('Boost ingetrokken'));
     716    on = !ActivityPubService.getTimelineReaction(site.slug, note).boosted;
     717    try { await ActivityPubService.sendInteraction(site, on ? 'boost' : 'unboost', note, (req.body.author || '').toString()); } catch (e) { /* ignore */ }
     718    if (on) ActivityPubService.markBoosted(site.slug, note); else ActivityPubService.unmarkBoosted(site.slug, note);
     719  }
     720  if (req.get('X-Requested-With') === 'fetch') return res.json({ ok: true, on });
     721  res.redirect('/news');
    734722});
    735723
Note: See TracChangeset for help on using the changeset viewer.