How can I programatically trigger topic watch for the current user?
-
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.
-
@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.