【问题标题】:Is it possible to email users who are @mentioned in rails ActionText?是否可以向 Rails ActionText 中@提到的用户发送电子邮件?
【发布时间】:2019-06-01 06:02:47
【问题描述】:

我遵循了有关在 ActionText 中使用 @mentions 的 GoRails 教程。我想自动向任何@提及的用户发送电子邮件,以提醒他们对话。我认为这在 Rails 中是微不足道的,但找不到任何关于如何做到这一点的文档。

有人知道这是怎么实现的吗?

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

【问题讨论】:

    标签: ruby-on-rails trix actiontext


    【解决方案1】:

    我不是 100%,但这是一个你可以尝试的想法。由于本教程将嵌入对象添加到富文本对象中,因此您可以在模型中执行以下操作:

    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
    

    【讨论】:

    • 是的,我想应该是这样的(我也没有接触过actiontext :))
    【解决方案2】:

    根据您的描述,我相信您希望有一个全面的 将执行以下操作的功能:

    1. 在文本区域检测@ + username(- 你需要一个文本区域 键盘输入事件监听器和特殊字符模式条件 识别@ + username)
    2. 列出与您的@ + username模糊搜索相关的所有用户(-您需要创建一个支持 通过用户名搜索,并让 F/E 随时向该 API 请求 在文本区域输入@ + username)
    3. 能够从@mention下拉列表中选择一个用户(-纯F/E行为,只需要另一个事件监听器来检测用户 选择并将选定的用户名注入该文本区域。
    4. 评论对话后自动提醒@mention用户(-此处需要B / E功能。您的服务器将具有此功能 评论后的对话数据,服务器需要检查是否 对话包含任何@mention 部分,如果有,则 找到与该@mention 相关的用户,并向该用户发送电子邮件 直接使用 Mailer 或间接使用 mail Worker。)

    这只是在不使用任何第三方接口的情况下实现此功能的一种简单方法。即使你打算在Trix edior 中使用ActionText,你也需要在 至少有能力

    1. 提供 API 以返回搜索到的用户列表结果
    2. 并至少为 B/E 中的 @mention 用户发送邮件。

    如果我的评论的任何部分在此处不清楚,请告诉我您是否需要更多详细信息。

    【讨论】:

    • 由于无论如何电子邮件都需要在后端进行,因此没有必要全神贯注于所有这些前端的花里胡哨(自动完成等)。他们只是为了用户体验。核心功能就是这样简单:“当文本被保存到数据库时,扫描它以查找@mentions 并将电子邮件排入队列给找到句柄的用户”。如果我们这样说,那么 actiontext/trix 就变得无关紧要了。后端不关心用于编写文本的编辑器。
    【解决方案3】:

    有一个关于这个主题的 GoRails 教程:https://gorails.com/episodes/notifications-action-text-user-mentions

    看着source code好像

    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
    

    【讨论】:

    • 你可以做得更好body.body.attachables.select { |a| a.class == User }.uniq
    【解决方案4】:

    我将 ActionText @mentions 与被注意到的 gem 集成,以向被提及的用户发送电子邮件或短信。似乎工作得很好。如上所述,我提取了每篇文章中提到的用户:

    body.body.attachments.select{|a| a.attachable.class==User}.map(&:attachable).uniq
    

    【讨论】:

      猜你喜欢
      • 2018-09-15
      • 2013-12-03
      • 2020-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多