XF 2.0 Proper way to use the mention system?

Jaxel

Well-known member
Has anyone added the mention system to any of their addons? Basically, I have a text field, and I want the addon to send out mention alerts.

The existing code for it in the XF base seems extremely complicated and hard to follow.
 
I think I got it...

Code:
        $media = $this->assertMediaExists($params->media_id);
        $comment = $this->em()->create('EWR\Medio:Comment');
        
        $mentions = new \XF\Str\MentionFormatter();
        $message = $mentions->getMentionsBbCode($this->plugin('XF:Editor')->fromInput('message'));
            
        $mentionUsers = $mentions->getMentionedUsers();
        $mentionUsers = $this->finder('XF:User')
            ->with(['Profile','Option'])
            ->where('user_id', array_keys($mentionUsers))
            ->fetch();
        
        $form = $this->formAction();
        $form->basicEntitySave($comment, [
            'media_id' => $media->media_id,
            'comment_message' => $message,
        ]);
        $form->run();
        
        foreach ($mentionUsers AS $user)
        {
            if ($comment->user_id != $user->user_id)
            {
                $alertRepo = \XF::repository('XF:UserAlert');
                $alertRepo->alertFromUser($user, $comment->User, 'ewr_medio_comment', $comment->comment_id, 'mention');
            }
        }
 
This:
Code:
$mentions = new \XF\Str\MentionFormatter();
Should be:
Code:
$mentions = $this->app->stringFormatter()->getMentionFormatter();

Otherwise your code isn't going to work with anything which extends MentionFormatter. For example my User Mentions Improvements add-on and User Essentials both extend the mention formatter.
 
Additionally you really want to copy how Posts, XFRM or XFMG work with using the Notifier framework to scale. These essentially allow you to use the Job system to batch send out mentions.

Otherwise it all runs in one process and can cause the process to timeout.

For example; my User Mentions Improvements add-on would cause a single user group mention to potentially expand to thousands of users. Doing that in a single request gets painful fast. Especially if the 'email on mention' function is turned on.
 
I've got no idea whats going on with this notifier framework. It's so confusing to follow.
 
For example; my User Mentions Improvements add-on would cause a single user group mention to potentially expand to thousands of users. Doing that in a single request gets painful fast. Especially if the 'email on mention' function is turned on.
[cries in Advanced User Tagging for vBulletin support tickets]

Exact same feature, since vB doesn't really have batch sending built in and I couldn't be bothered making one, I ended up just saying "Yeah don't use it on those user groups" :P


Fillip
 
  • Like
Reactions: Xon
I've got no idea whats going on with this notifier framework. It's so confusing to follow.
Have a look at XFRM\Service\ResourceUpdate\Notifier and XFRM\Notifier\ResourceUpdate\Mention for a simple implementation.

It is split into two levels;
  • a notifier service
  • Individual notifier classes.
The notifier service is a framework for loading & running the notifiers, across multiple requests, and ensures email/alerts are only sent once.

The individual notifiers are just concerned with applying the notification process to each raised user. Watching can fetch additional data to mentioned users, but is still quite simple.

[cries in Advanced User Tagging for vBulletin support tickets]

Exact same feature, since vB doesn't really have batch sending built in and I couldn't be bothered making one, I ended up just saying "Yeah don't use it on those user groups" :p
I wrote one of those for XenForo 1.x because I had threads with +2000 email subscribers getting notified at once (due to another add-on)
 
But how do I actually USE it? Thats where I'm having problems seeing it. For Watch notifications too.
 
Yeah, I just can not get any of this working, after working on it all day.

I've got watch tables, services, notifiers... and emails and alerts never get sent out.
 
Back
Top Bottom