How can I programatically trigger topic watch for the current user?

NodeBB Plugins
  • I am trying to add a feature to a plugin where user automatically get subscribed (watches topic) when they interact with the post. I looked at the HTML behind the watch button but could not find a clue what happens after this:

    <a href="#" role="menuitem" tabindex="-1" class="follow" title="[[topic:watch.title]]"><span>[[topic:watch]]</span> <i class="fa fa-eye"></i></a>
    

    I have the user id and the post id, I can also get the topic id. How can I automatically select watch for the current topic so user is informed of changes to that topic?

    I also looked at the wiki but could not find a hook for doing this.

  • On the server side its topics.follow. Client side you can do a socket.emit('topics.follow', tid, callback); after this commit. https://github.com/NodeBB/NodeBB/commit/e31bf9131f8e7292830c364dc0ef2a5b39a6633d

  • @baris thanks for the quick response! I have something like this now:

    socket.emit('topics.follow', post_data.tid, function(err, result) {
        if(err) {
    	console.log('plugin error: ', err);
        }
    });
    

    That seems to toggle follow or watch attribute. Is there a way to check to see if user is currently following the topic or a way to only set follow to true instead of toggling the value?

  • I found this way to work around it:

    socket.emit('topics.follow', post_data.tid, function(err, result) {
    	if(err) {
    		console.log('Error:  ', err);
    	} else if(!result) {
    		// if result is false, means we unfollow the topic
    		// so we send another request to follow
    		socket.emit('topics.follow', post_data.tid);
    	}
    });
    

    It may make two useless calls (if user is already following topic) but at least it works as expected.

  • Yeah my latest commit addresses that, topics.follow on latest will just follow the topic and there is a new function called topic.toggleFollow that does the old functionality.


Suggested Topics