XF 2.3 Changing watch state of a thread for a specific user

Edman

Member
A user has selected to watch all threads that they create and post in.

When I create a new post in a thread, the thread gets watched for that specific user

Code:
        $thread = \XF::em()->find('XF:Thread', $threadid);
        $user = \XF::em()->find('XF:User', $userid);
        $post = \XF::asVisitor($user, function() use ($thread, $message)
        {
            $creator = \XF::service('XF:Thread\Replier', $thread);
            $creator->setMessage($message);
            $creator->setIsAutomated();
            return $creator->save();
        });

But when I create a new thread, it does not get watched

Code:
        $forum = \XF::em()->find('XF:Forum', $forumid);
        $user = \XF::em()->find('XF:User', $userid);
        $thread = \XF::asVisitor($user, function() use ($forum, $title, $message)
        {
            $creator = \XF::service('XF:Thread\Creator', $forum);
            $creator->setDiscussionState('visible');
            $creator->setContent($title, $message);
            $creator->setIsAutomated();
            return $creator->save();
        });

I have searched high and low, still can't find how to change if the thread is watched or not by a specific user. Any help?
 
The auto-watch behavior is handled by calling \XF\Repository\ThreadWatchRepository::autoWatchThread for both thread creation and replies.
 
Thanks! I could not figure out how to call autoWatchThread and I couldn't find any documentation or examples of how to do it.

But I managed to add thread watch with this code

Code:
$userFinder = \XF::finder('XF:User');
$user = $userFinder->where('user_id', $userid)->fetchOne();

$threadFinder = \XF::finder('XF:Thread');
$thread = $threadFinder->where('thread_id', $threadid)->fetchOne();

$xenforo_app->repository('XF:ThreadWatch')->setWatchState($thread, $user, 'watch_email');

However, this was interfering with the code that was already there, so I just couldn't figure it out and I re-wrote it like this.

Code:
        $user = \XF::em()->find('XF:User', $userid);
        $state = $user->Option->getValue('creation_watch_state');

        if (in_array($state, array('watch_email', 'watch_no_email')))
        {
            $watch = \XF::em()->create('XF:ThreadWatch');
            $watch->thread_id = $threadid;
            $watch->user_id = $userid;
            $watch->email_subscribe = ($state == 'watch_email');;
            try
            {
                $watch->save();
            }
            catch (\XF\Db\DuplicateKeyException $e) {}
        }
 
Thanks! I could not figure out how to call autoWatchThread and I couldn't find any documentation or examples of how to do it.
It's just a repository method, you can call it freely. The arguments should be mostly self-explanatory:

PHP:
$threadWatchRepo = $this->repository(ThreadWatchRepository::class);
$threadWatchRepo->autoWatchThread($thread, $user, $onCreation);

However, this was interfering with the code that was already there, so I just couldn't figure it out and I re-wrote it like this.
I don't know what the surrounding context of your code is, but autoWatchThread is only called in a handful of places so unless you're extending existing code I'm not sure why that would be the case.
 
Back
Top Bottom