0

I followed the GoRails tutorial on using @mentions in ActionText. I want to automatically email any @mentioned user to alert them of the conversation. I would assume this to be trivial in Rails, but can't find any documentation on how to do it.

Anybody know how this would be accomplished?

GoRails: https://gorails.com/episodes/at-mentions-with-actiontext

hellion
  • 4,602
  • 6
  • 38
  • 77

4 Answers4

2

I'm not 100% but here's an idea you can play with. Since the tutorial adds embeded objects to the rich text object, you could do something like this in your model:

has_rich_text :content

after_create do
  content.embeds.each do |embed|
    # now you have each embeded object, I guess you could use that sgid that
    # he name on the tutorial a few times to find if the embeded object is a 
    # user or something else, and then fire the email. I leave this part to 
    # you since I didn't actually tried ActionText yet, I just saw the 
    # tutorial and read parts of the code ;P
  end
end
arieljuod
  • 15,460
  • 2
  • 25
  • 36
0

Based on your description, I believe that you'd like to have a comprehensive feature that will perform the following actions:

  1. detect @ + username in a text area (- you need a text area keyboard typing event listener and special char pattern condition recognising for @ + username)
  2. list all users that are relative to your @ + username fuzzy search (- you need create an users list API with support of searching by username, and let F/E requests to this API whenever typing in the text area with @ + username)
  3. be able to select an user from @mention dropdown list (- a pure F/E behaviour, just need another event listener to detect user selecting and inject the selected username to that text area.
  4. Auto alert the @mention users after commenting the conversation (- A B/E function is needed here. Your server will have this conversation data after commenting, and server needs to check if the conversation contains any @mention section, if there is one, then find the user related to that @mention, and send email to the user by directly using Mailer or indirectly using a mail Worker.)

This is just one simple way to achieve this feature without using any third party interface. Even if you are going to use ActionText in Trix edior, you need at least to have abilities of

  1. providing an API to return searched user list results
  2. and sending mails for those @mention users in B/E at least.

Let me know if you need any more details if any part of my comment is not clear here.

QuintinX
  • 11
  • 2
  • Since the emailing needs to happen on the backend anyway, there's no need to preoccupy oneself with all these frontend bells and whistles (autocompletion, etc.). They're just for UX. And core functionality is as simple as this: "when text is being saved to the database, scan it for `@mentions` and enqueue emails to users with found handles". And if we put it this way, then actiontext/trix becomes irrelevant. Backend is not concerned with what editor was used to compose the text. – Sergio Tulentsev Jun 02 '19 at 12:27
0

There is a GoRails tutorial on this topic: https://gorails.com/episodes/notifications-action-text-user-mentions

Looking at the source code it seems

after_create :send_notifications

def send_notifications
  users = user_mentions
  users.each do |user|
    PostMailer.user_mention(self, user).deliver_now
  end
end

def user_mentions
  @users ||= body.body.attachments.select{ |a| a.attachable.class == User }.map(&:attachable).uniq
end
daibhin
  • 3
  • 1
  • 5
0

I integrated ActionText @mentions with the noticed gem to send emails or text messages to users who are mentioned. Seems to work pretty well. As discussed above, I pulled out the users mentioned in each post with:

body.body.attachments.select{|a| a.attachable.class==User}.map(&:attachable).uniq
aboger
  • 2,214
  • 6
  • 33
  • 47
  • Do you know of a way to go the other direction... get a User, and find all of the posts/Actiontexts they are mentioned in? – mungojerie Jun 28 '22 at 18:07