ruby-china 代码研究

内容比较丑陋,算是个抛砖引玉吧,如果不是新手,就走开吧。最近发现rc的源代码的资料比较高,网上搜了一遍,也没有发现类似面向新手的分析性文章。

redis使用计数器

    topicController:        @topic.hits.incr(1)    Topic        include Redis::Objects        counter :hits, :default => 0    end

消息系统:采用faye:参考资料:http://railscasts.com/episodes/260-messaging-with-faye?view=asciicast

当用户A回复用户B的评论时,rc会使用消息系统将该回复通知到用户B:1 当create一个新的Reply时,会调用Reply的回调方法:after_create

class Reply  after_create do    Reply.delay.send_topic_reply_notification(self.id)  end

2 创建Notification::TopicReply

def self.send_topic_reply_notification(reply_id)    reply = Reply.find_by_id(reply_id)    return if reply.blank?    topic = reply.topic    return if topic.blank?    # 给发帖人发回帖通知    if reply.user_id != topic.user_id && !reply.mentioned_user_ids.include?(topic.user_id)      Notification::TopicReply.create :user_id => topic.user_id, :reply_id => reply.id      reply.notified_user_ids << topic.user_id    end    # 给关注者发通知    topic.follower_ids.each do |uid|      # 排除同一个回复过程中已经提醒过的人      next if reply.notified_user_ids.include?(uid)      # 排除回帖人      next if uid == reply.user_id      Notification::TopicReply.create :user_id => uid, :reply_id => reply.id    end    true  end

3 Notification::TopicReply的基类是Notification::Base

class Notification::TopicReply < Notification::Base  belongs_to :reply  delegate :body, :to => :reply, :prefix => true, :allow_nil => true  def notify_hash    return "" if self.reply.blank?    {       :title => "关注的话题有了新回复:",       :content => self.reply_body[0,30],      :content_path => self.content_path    }   end  def content_path    return "" if self.reply.blank?    url_helpers.topic_path(self.reply.topic_id)  endend

4Notification::Base的create方法有个回调:realtime_push_to_client,在该回调里会将通知广播出去Notification::Base

after_create :realtime_push_to_clientdef realtime_push_to_client    if self.user      hash = self.notify_hash      hash[:count] = self.user.notifications.unread.count      FayeClient.send("/notifications_count/#{self.user.temp_access_token}", hash)    end  endclass FayeClient  def self.send(channel, params)    Thread.new {      params[:token] = Setting.faye_token      message = {:channel => channel, :data => params}      uri = URI.parse(Setting.faye_server)      Net::HTTP.post_form(uri, :message => message.to_json)    }  endend
ruby-china 代码研究

相关文章:

你感兴趣的文章:

标签云: