We've moved discussions to Discord

Sending notifcations when mentioned

Joshua LeBlanc
Poking around actiontext, but it's abysmally documented. 

I figure I can just wire up an after_save callback, and look at the mentions to send notifications - However I can't seem to get a list of the mentions.

Has anyone done this, or have any pointers?
Nicholas B
Maybe this part of the documentation can help:
- https://jumpstartrails.com/docs/notifications

What I'm thinking is that maybe everyone mentioned in the comment can also have this method ran on their user.
NewComment.with(comment: @comment).deliver_later(@comment.post.owner)


Also,  Chris Oliver mentioned that he is working on this feature for new jumpstart:
- https://jumpstartrails.com/discussions/175 (before last comment)

and here is a related thread:
- https://jumpstartrails.com/discussions/224
Chris Oliver
You'll have to loop through the ActionText content attachments and filter only the User attachments.

Here's an example with a Post model that has_rich_text :body on it to find all the User's mentioned.

Post.last.body.body.attachments.select{ |a| a.attachable.class == User }

Then you can use that array of users to notify them.
Chris Oliver
I'll probably build a concern so you can add this functionality easily to your models that use ActionText.

Maybe something like this?
include Jumpstart::Mentions
has_mentions :user, rich_text: :body, class: User

# defines a method called "user_mentions" that searches the "body" rich text for User attachments.
 
def notify
  user_mentions #=> [<User>, <User>]
end
Chris Oliver
Just added Jumpstart::Mentions as a concern so you can include it in your own models. I'll add docs for it soon, but it's all defined in the repo.

https://gitlab.com/gorails/jumpstart-pro/-/blob/master/lib/jumpstart/lib/jumpstart/mentions.rb
Chris Oliver
Refactored to be more flexible and intuitive. 👍

class Post
  has_rich_text :body
  has_rich_text_mentions :body
end

@post.body_mentions #=> [User, User]
@post.body_mentions(Photo) #=> [Photo, Photo]

Brandon Brown
Thanks  Chris Oliver , you should really add this helpful info to the docs!

Related question, how can I convert the post body to plain text (to include the mention) so that I can send in a mailer? If i do something like to_plain_text(), it will just strip the mention. Any ideas?
Chris Oliver
Brandon Brown

Made a pull request to fix that: https://github.com/jumpstart-pro/jumpstart-pro/pull/114
Brandon Brown
Excellent, thank you sir!
Notifications
You’re not receiving notifications from this thread.