XF 2.3 No thread alert with new posts when user has not visited their thread

Edman

Member
When posting a news article on my site's CMS, I create a new thread for that article, with the userid of that user that was posting the news item.

The code to do this is this:

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 then create a thread watch

Code:
        $threadid = $thread->thread_id;

        $state = $user->Option->getValue('creation_watch_state');
        
        $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) {}

Everything appears to work correctly, and there is an entry in xf_thread_watch, but, when alerts are set to on, the user is not getting an alert.

However, if the user visits the thread (manually), and then somebody makes a post in it, an alert will appear without making any other changes.

It looks like alerts do not appear, because the user in who's name that thread was created, has not seen that thread. I do not believe this is normal behaviour.

Is there any way to programmatically make it look like the user has visited the thread at the same time the thread was made?

Thanks
 
For my blogs addon I handle it the same way XFRM does with the below

PHP:
    protected function afterResourceThreadCreated(Thread $thread)
    {
        $this->repository('XF:Thread')->markThreadReadByVisitor($thread);
        $this->repository('XF:ThreadWatch')->autoWatchThread($thread, \XF::visitor(), true);
    }
 
Thanks, this was helpful, I have made some changes and seems like it works. It is just strange that after creating a thread you have to mark it read for alerts to appear.
 
Back
Top Bottom