在 Rails 中实现通知

发布于 2024-11-25 03:35:51 字数 467 浏览 2 评论 0原文

在我的应用程序中,当评论或帖子中提到用户时,我想通知他/她。
用户句柄是@user_name,类似于Facebook
提及的数据库表看起来像:

Mention
  mentioned_by: user_id (foreign key)
  user_mentioned: user_id (foreign key)
  comment_id: (foreign key)
  post_id: (foreign key)

但我无法找到实现它的方法。 Facebook/Twitter 是如何做到的?

我决定使用 ActiveRecord 回调/观察者设计模式,每当将新评论/帖子保存到数据库时,我就可以浏览帖子/评论的内容,并查找任何提及,然后根据需要执行通知。

我感觉有一些缺失的部分,而且我没有得到正确的结果。
这是最好的方法吗?

In my application, I want to notify a user, when he/she is mentioned in a comment or a post.
The user handle is @user_name, similar to Facebook.
The database table for mentions looks like:

Mention
  mentioned_by: user_id (foreign key)
  user_mentioned: user_id (foreign key)
  comment_id: (foreign key)
  post_id: (foreign key)

I can't figure out a way to implement it though. How do Facebook / Twitter do it?

What I decided to go with, was use ActiveRecord callbacks/ Observer design pattern and whenever a new comment/post is saved to database, I can go through the contents of the post/comment, and look out for any mentions and then execute the notifications as required.

I get the feeling that there are some missing pieces and I am not getting it right.
Is this the best way of doing it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

流年里的时光 2024-12-02 03:35:52

您可以在保存记录时使用 ActiveRecord 回调(例如 before_save、after_save 或 before_create、after_create)来浏览评论内容,查找并创建所有提及模型并将它们保存到数据库。

You can use ActiveRecord callbacks while record is saved (like before_save, after_save or before_create, after_create) to go through comment content, find and create all mentions models and save them to db.

夕色琉璃 2024-12-02 03:35:52

事实上,我本人对这个问题的具体答案很感兴趣。我不知道 Facebook 和 Twitter 是如何做到这一点的,但我确实从一般搜索中知道,gem 充当可标记的角色可以完成这项工作。查看 https://github.com/mbleigh/acts-as-taggable-on< /a>.

另外,stackoverflow 上的这个问题也可能为您提供一些信息: Implementing twitter-like hashtag on祝你

好运。我鼓励您尝试更多地关注这个问题并获得比我所说的更可靠的答案。 :]

I actually am interested in a concrete answer to this myself. I don't know how Facebook and Twitter do it, but I do know from general searches that the gem acts-as-taggable-on could get the job done. Check out https://github.com/mbleigh/acts-as-taggable-on.

Also, this question on stackoverflow might also provide you with some info: Implementing twitter-like hashtag on rails

Good luck. I encourage you to try to get more attention to this question and get a more solid answer than what I've said. :]

盛夏尉蓝 2024-12-02 03:35:52

我相信 Tumblr 使用 Redis 队列系统(如 Resque)来处理数据量。

进行回调(正如您提到的)并将其交给 Resque。 (最近有一个关于 Resuqe 的 Railscasts

Tumblr uses a Redis queuing system (like Resque) I believe to handle the volume.

Do a callback (as you mentioned) and hand it off to Resque. (There was a Railscasts about Resuqe recently)

不念旧人 2024-12-02 03:35:52

对此没有单一的推荐方法。在超级级别,您可能需要查看“Comet 编程”,轮询和 WebSockets [HTML5],然后选择正确的组合。有一些很棒的实现可以管理 Rails 中的推送通知。 Orbited、Juggernaut、PusherApp、Faye 等。您必须深入挖掘才能找出其中哪些使用网络套接字和网络套接字。并回退到闪存选项来处理全面支持。

Faye 也提供了 Node.js 配置,但我不确定其他配置。

这些步骤暂时类似于:

  1. 保存内容 - 将其排队到解析器

  2. 解析内容以找出涉及的用户 - 使用 Nokogiri 或等效工具。

  3. 如果您正在考虑类似 Twitter 的方法,请将 Comet/Poll 作为一个单独的进程来参与 current_session 中的用户。

  4. //用Post记录做其他事情

  5. 向相关用户推送通知,并在他们稍后上线时销毁()。

    希望能给一些方向。

There is no single recommended approach for this. At an uber level, you may want to look at 'Comet programming', Polling and WebSockets [HTML5] and then choose the right combination. There are a couple of great implementations to manage push notifications in rails. Orbited, Juggernaut, PusherApp, Faye etc. You'll have to dig deep to figure out which of 'em use web-sockets & and fall-back to flash option to handle full support.

Faye gives a Node.js configuration also, but I am not sure about others.

Tentatively the steps would look something like:

  1. Save the content - queue it to parser

  2. Parse the content to find out involved users - Use Nokogiri or equivalent.

  3. Comet/Poll it to involved users in current_session as a separate process if you're looking at Twitter like approach.

  4. //Do other things with Post record

  5. Push notifications to involved users and destroy() when they come online later.

    Hope that gives some direction.

腹黑女流氓 2024-12-02 03:35:52

我知道这个问题已经过时了,但我最近向 ruby​​gems.org 发布了一个 MentionSystem gem,它允许在被提及者对象和提及者之间创建提及,它还允许您检测 facebook / twitter styler 中的提及,例如 @username1 和 #hashtag,以便创建的提及。

gem 托管在 github 上: https://github.com/pmviva/mention_system

假设您有一个可以以@用户名形式提及用户的帖子。

你有这个类

class Post < ActiveRecord::Base
  act_as_mentioner
end

class User < ActiveRecord::Base
  act_as_mentionee
end

然后你定义一个自定义提及处理器:

class PostMentionProcessor < MentionSystem::MentionProcessor
  def extract_mentioner_content(post)
    return post.content
  end

  def find_mentionees_by_handles(*handles)
    User.where(username: handles)
  end
end

然后在你的帖子控制器中创建你有的操作:

def create
  @post = Post.new(params[:post])
  if @post.save
    m = PostMentionProcessor.new
    m.add_after_callback Proc.new { |post, user| UserMailer.notify_mention(post, user) }
    m.process_mentions(post)
  end

  respond_with @post
end

如果你的帖子内容中有@user1,@user2和@user3,提及处理器将解析user1,user2,user3,将查找用户名 [user1, user2, user3] 的用户,然后在数据库中创建提及,在每次提及之后,它将运行 after 回调,在示例中将发送一封电子邮件,通知帖子和用户之间的提及。

I know this question is outdated but I released a MentionSystem gem recently to rubygems.org that allows to create mentions between mentionee objects and mentioners, it also allows you to detect mentions in facebook / twitter styler like @username1 and #hashtag in order to create the mentions.

The gem is hosted at github: https://github.com/pmviva/mention_system

Lets say you have a Post that can mention users in the form of @username.

you have the class

class Post < ActiveRecord::Base
  act_as_mentioner
end

and

class User < ActiveRecord::Base
  act_as_mentionee
end

Then you define a custom mention processor:

class PostMentionProcessor < MentionSystem::MentionProcessor
  def extract_mentioner_content(post)
    return post.content
  end

  def find_mentionees_by_handles(*handles)
    User.where(username: handles)
  end
end

Then in your Posts controller create action you have:

def create
  @post = Post.new(params[:post])
  if @post.save
    m = PostMentionProcessor.new
    m.add_after_callback Proc.new { |post, user| UserMailer.notify_mention(post, user) }
    m.process_mentions(post)
  end

  respond_with @post
end

If your post has @user1, @user2 and @user3 in its content the mention processor will parse user1, user2, user3, will find users with username [user1, user2, user3] and then create the mentions in the database, after each of the mentions it will run the after callback that in the example will send an email notifying the mention between post and user.

情场扛把子 2024-12-02 03:35:51

Facebook 和 Twitter 不是中型 Rails 应用程序。他们是公司。运行它们的技术是分布式的,而且大多是定制的,尤其是在 Facebook 的情况下。

您似乎要掌握的部分是他们如何以高性能和可扩展的方式确定通知谁。这就是狗屎变得真实的地方。您可以找到很多关于它们背后的架构的信息,并且肯定有很多很棒的东西可以帮助您思考这些事情,但最终它们都不会成为您在任何应用程序中实现的东西。重新建设。

http://www.quora.com/What-is-Facebooks-architecture

Facebook 架构

http://www.infoq.com/news/2009/06/Twitter-Architecture< /a>

http://engineering.twitter.com/2010/10/twitters-new-search-architecture.html

Quora 上有更多有趣的细节。


不幸的是,这些都不能让你更接近你的目标。我认为您开始做的最现实的事情就是简单地将 Pusher 之类的服务绑定到无需担心即可向客户端发送消息,请使用 ActiveRecord Observer 将通知添加到后台队列,其中工作人员实际上将这些通知发送给 Pusher。这是一天或更少的工作,并且应该可以很好地扩展到每天至少 10k 条通知。一旦开始出现性能问题,就放弃观察者和推送者,转而使用像 Goliath 这样可以在本地处理这两项工作的东西。

最重要的是,尽可能了解已经通过测试的大型且经验丰富的系统,但主要只是看看他们遇到了哪些问题以及他们如何解决这些问题。即使在大公司中,这些方法也不尽相同,并且它们会因每个实现而异。

希望这能帮助您指明一个好的方向。 :)

Facebook and Twitter are not mid-size Rails applications. They are companies. The tech that runs them is distributed and mostly custom, especially in the case of Facebook.

The part that you seem to be grasping for is how they determine who to notify in a performant and scalable way. This is where shit gets real. You can find a lot of information about the architecture behind each on of them, and there is certainly a lot of great stuff to help you think about these things, but ultimately none of it is going to be something you implement into whatever application you're building.

http://www.quora.com/What-is-Facebooks-architecture

Facebook Architecture

http://www.infoq.com/news/2009/06/Twitter-Architecture

http://engineering.twitter.com/2010/10/twitters-new-search-architecture.html

Plenty more juicy details over at Quora.


Unfortunately, none of this gets you closer to your goal. I think the most realistic thing for you to do to start out with woud be to simply tie in a service like Pusher to send messages to clients without worrying about it, use an ActiveRecord Observer to add notifications to a background queue where the workers actually send those notifications to Pusher. This is a day or less of work and it should scale well to at least 10k notifications a day. Once you start having performance problems, ditch the Observer and Pusher for something like Goliath that can handle both of the jobs locally.

Bottom line, learn what you can about large and experienced systems that have been put through the paces, but mainly just to see what problems they ran into and how they solved them. These methods aren't the same among the big guys even, and they are going to vary for each implementation.

Hopefully that helps point you in a good direction. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文