potential fix for favorites in production, let's try
This commit is contained in:
		@@ -201,49 +201,65 @@ export default {
 | 
			
		||||
      tmp
 | 
			
		||||
    };
 | 
			
		||||
    return data;
 | 
			
		||||
  },
 | 
			
		||||
  getRandom: async (o = ({ user, tag, mime, mode, session })) => {
 | 
			
		||||
    const user    = o.user ? decodeURI(o.user) : null;
 | 
			
		||||
    const tag     = lib.parseTag(o.tag ?? null);
 | 
			
		||||
    const mime    = (o.mime ?? "");
 | 
			
		||||
    const smime   = cfg.allowedMimes.includes(mime) ? mime + "/%" : mime === "" ? "%" : "%";
 | 
			
		||||
 | 
			
		||||
  },getRandom: async (o = ({ user, tag, mime, mode, fav, session })) => {
 | 
			
		||||
    const user = o.user ? decodeURI(o.user) : null;
 | 
			
		||||
    const tag = lib.parseTag(o.tag ?? null);
 | 
			
		||||
    const mime = (o.mime ?? "");
 | 
			
		||||
    const smime = cfg.allowedMimes.includes(mime) ? mime + "/%" : mime === "" ? "%" : "%";
 | 
			
		||||
  
 | 
			
		||||
    const modequery = mime == "audio" ? lib.getMode(0) : lib.getMode(o.mode ?? 0);
 | 
			
		||||
 | 
			
		||||
    const item = await db`
 | 
			
		||||
      select
 | 
			
		||||
        items.id
 | 
			
		||||
      from items
 | 
			
		||||
      left join tags_assign on tags_assign.item_id = items.id
 | 
			
		||||
      left join tags on tags.id = tags_assign.tag_id
 | 
			
		||||
      left join favorites on favorites.item_id = items.id
 | 
			
		||||
      left join "user" on "user".id = favorites.user_id
 | 
			
		||||
      where
 | 
			
		||||
        ${ db.unsafe(modequery) }
 | 
			
		||||
        and items.active = 'true'
 | 
			
		||||
        ${ tag ? db`and tags.normalized ilike '%' || slugify(${tag}) || '%'` : db`` }
 | 
			
		||||
        ${ o.fav ? db`and "user".user ilike ${'%'+user+'%'}` : db`` }
 | 
			
		||||
        ${ user ? db`and items.username ilike ${'%'+user+'%'}` : db`` }
 | 
			
		||||
        ${ mime ? db`and items.mime ilike ${smime}` : db`` }
 | 
			
		||||
        ${ !o.session && globalfilter ? db`and items.id not in (select item_id from tags_assign where item_id = items.id and (${db.unsafe(globalfilter)}))` : db`` }
 | 
			
		||||
      group by items.id, tags.tag
 | 
			
		||||
      order by random()
 | 
			
		||||
      limit 1
 | 
			
		||||
    `;
 | 
			
		||||
 | 
			
		||||
    if(item.length === 0) {
 | 
			
		||||
  
 | 
			
		||||
    let item;
 | 
			
		||||
  
 | 
			
		||||
    if (o.fav && user) {
 | 
			
		||||
      // Special case: random from user's favorites
 | 
			
		||||
      item = await db`
 | 
			
		||||
        select
 | 
			
		||||
          items.id
 | 
			
		||||
        from favorites
 | 
			
		||||
        inner join items on favorites.item_id = items.id
 | 
			
		||||
        inner join "user" on "user".id = favorites.user_id
 | 
			
		||||
        where
 | 
			
		||||
          "user".user ilike ${'%' + user + '%'}
 | 
			
		||||
          and items.active = 'true'
 | 
			
		||||
          ${mime ? db`and items.mime ilike ${smime}` : db``}
 | 
			
		||||
        order by random()
 | 
			
		||||
        limit 1
 | 
			
		||||
      `;
 | 
			
		||||
    } else {
 | 
			
		||||
      // Normal random logic
 | 
			
		||||
      item = await db`
 | 
			
		||||
        select
 | 
			
		||||
          items.id
 | 
			
		||||
        from items
 | 
			
		||||
        left join tags_assign on tags_assign.item_id = items.id
 | 
			
		||||
        left join tags on tags.id = tags_assign.tag_id
 | 
			
		||||
        where
 | 
			
		||||
          ${db.unsafe(modequery)}
 | 
			
		||||
          and items.active = 'true'
 | 
			
		||||
          ${tag ? db`and tags.normalized ilike '%' || slugify(${tag}) || '%'` : db``}
 | 
			
		||||
          ${user ? db`and items.username ilike ${'%' + user + '%'}` : db``}
 | 
			
		||||
          ${mime ? db`and items.mime ilike ${smime}` : db``}
 | 
			
		||||
          ${!o.session && globalfilter ? db`and items.id not in (select item_id from tags_assign where item_id = items.id and (${db.unsafe(globalfilter)}))` : db``}
 | 
			
		||||
        group by items.id, tags.tag
 | 
			
		||||
        order by random()
 | 
			
		||||
        limit 1
 | 
			
		||||
      `;
 | 
			
		||||
    }
 | 
			
		||||
  
 | 
			
		||||
    if (item.length === 0) {
 | 
			
		||||
      return {
 | 
			
		||||
        success: false,
 | 
			
		||||
        message: "no f0cks found :("
 | 
			
		||||
      };
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  
 | 
			
		||||
    const link = lib.genLink({ user, tag, mime, type: o.fav ? 'favs' : 'f0cks' });
 | 
			
		||||
 | 
			
		||||
  
 | 
			
		||||
    return {
 | 
			
		||||
      success: true,
 | 
			
		||||
      link: link,
 | 
			
		||||
      itemid: item[0].id
 | 
			
		||||
    };
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
  };
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,4 @@
 | 
			
		||||
import cfg from "../../inc/config.mjs";
 | 
			
		||||
import lib from "../lib.mjs";
 | 
			
		||||
import f0cklib from "../routeinc/f0cklib.mjs";
 | 
			
		||||
 | 
			
		||||
export default (router, tpl) => {
 | 
			
		||||
@@ -7,12 +6,14 @@ export default (router, tpl) => {
 | 
			
		||||
    let referer = req.headers.referer ?? '';
 | 
			
		||||
    let opts = {};
 | 
			
		||||
 | 
			
		||||
    if(referer.match(new RegExp(cfg.main.url.regex))) { // parse referer
 | 
			
		||||
    if (referer.match(new RegExp(cfg.main.url.regex))) { // parse referer
 | 
			
		||||
      referer = referer.split(cfg.main.url.domain)[1];
 | 
			
		||||
      console.log("referer: ", referer);
 | 
			
		||||
 | 
			
		||||
      const tmp = referer.match(/^\/?(?:\/tag\/(?<tag>.+?))?(?:\/user\/(?<user>.+?)\/(?<mode>f0cks|favs))?(?:\/(?<mime>image|audio|video))?(?:\/p\/(?<page>\d+))?(?:\/(?<itemid>\d+))?$/);
 | 
			
		||||
      console.log("tmp: ", tmp);
 | 
			
		||||
      if(tmp)
 | 
			
		||||
 | 
			
		||||
      if (tmp && tmp.groups)
 | 
			
		||||
        opts = tmp.groups;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -21,14 +22,14 @@ export default (router, tpl) => {
 | 
			
		||||
      tag: opts.tag,
 | 
			
		||||
      mime: opts.mime,
 | 
			
		||||
      page: opts.page,
 | 
			
		||||
      fav: opts.mode == 'favs',
 | 
			
		||||
      fav: opts.mode === 'favs',
 | 
			
		||||
      mode: req.session.mode,
 | 
			
		||||
      session: !!req.session
 | 
			
		||||
    });
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    console.log("data", data);
 | 
			
		||||
 | 
			
		||||
    if(!data.success) {
 | 
			
		||||
    if (!data.success) {
 | 
			
		||||
      return res.reply({
 | 
			
		||||
        code: 404,
 | 
			
		||||
        body: tpl.render('error', {
 | 
			
		||||
@@ -40,5 +41,6 @@ export default (router, tpl) => {
 | 
			
		||||
 | 
			
		||||
    res.redirect(encodeURI(`${data.link.main}${data.link.path}${data.itemid}`));
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  return router;
 | 
			
		||||
};
 | 
			
		||||
};
 | 
			
		||||
		Reference in New Issue
	
	Block a user