Unread Topics - View

Solved Plugin Development
  • Hi all,

    I am trying to develop a plugin that helps keep a topic on "Unread" section indefinitely in spite of users reading it.

    The following are my progress/attempts so far,

    1. I inserted the required topic ID via the hook filter:topics.getUnreadTids. It works but as soon as all topics have been read, this hook isn't fired any more and therefore, the topic IDs I manually insert do not pop appear in the Unread list.

    2. I removed the topic IDs (tids I want to not to go away from Unread view even after they're read) from hookData.tid array when action:topics.markAsRead is fired. No results

    3. I ran the topics.markAsUnreadForAll(tid) function to mark topics as unread.

    None of my attempts have been successful so far.

    I appreciate any direction anyone can give me about how to make this possible.

    TIA!

  • yasasY yasas marked this topic as a question on
  • You should be able to do this, the function topics.markAsUnreadForAll(tid) (https://github.com/NodeBB/NodeBB/blob/master/src/topics/unread.js#L262-L264) is not named correctly, all it does it mark the category of the topic unread.

    The code to mark a topic unread is here https://github.com/NodeBB/NodeBB/blob/master/src/topics/unread.js#L365-L372

    So you can try using that in the hook action:topics.markAsRead

    myPlugin.actionTopicsMarkAsRead = async (hookData) => {
      await Promise.all(hookData.tids.map(async (tid) => {
         if (tid === 'sometopicid') {
            await topics.markUnread(tid, hookData.uid);  
         }
      }));
      topics.pushUnreadCount(hookData.uid);
    };
    

    Essentially as soon as someone marks a topic read you mark it unread if it matches your criteria.

  • FWIW, I think filter:topics.getUnreadTids should still be fired even if no tids are returned.

    The relevant block in src/topics/unread.js:

    const data = await getTids(params);
    if (uid <= 0 || !data.tids || !data.tids.length) {
    	return data;
    }
    
    const result = await plugins.hooks.fire('filter:topics.getUnreadTids', {
    	uid: uid,
    	tids: data.tids,
    	counts: data.counts,
    	tidsByFilter: data.tidsByFilter,
    	cid: params.cid,
    	filter: params.filter,
    	query: params.query || {},
    });
    return result;
    

    Could just remove || !data.tids || !data.tids.length from the conditional and we'd be ok. The uid check just leaves out bots/spiders.

  • Hi @baris
    Understood. My bad.. I should've paid more attention.Thanks, that's very helpful! I was able to perform the task following this suggestion..

    Hi @julian
    Thanks for that! I could use @baris 's suggestion and get my task performed but I think I should take some time and change the conditionals to check if I can get it to work as well. I have a couple more tasks to complete right before the Unread view is loaded. So definitely this needs to be further explored. I will get back to you or baris in case I am unable to find a solution. However, for now, I think I am set.

    Again, thanks very much you two! (As always)
    And Happy New Year!

  • yasasY yasas has marked this topic as solved on
  • Hi @julian
    I could follow your suggestion and get it to work. Removing !data.tids || !data.tids.length) { did the trick.
    Will update it here if I find a way to do it without changing the original code.

    Thanks!

  • I added this change https://github.com/NodeBB/NodeBB/commit/b81fd81af639509cc280d616d5c46a0b11564bff so it will be available in core ­čöť

  • Hi @baris
    I missed this somehow.

    Thank you! This is great news. I was creating a custom page but this solves the issue. Thanks very much!


Suggested Topics