Skip to content

Instantly share code, notes, and snippets.

@jhjguxin
Created September 19, 2012 10:24
Show Gist options
  • Save jhjguxin/3748880 to your computer and use it in GitHub Desktop.
Save jhjguxin/3748880 to your computer and use it in GitHub Desktop.
mongo notifies demo base on bbtang.com
=begin
notify = {}
notify[:item_id][email protected]
notify[:item_type]='Note'
notify[:create_user_id] = message_box[sender_id]
notify[:event]='新站内信'
notify[:relate_user_id]=message_box[recipient_id]
notify[:read]= true || false
notify[:content][email protected]_s
notify[:content_path] = Time.now
notify[:created_at] = Time.now
notify[:updated_at] = Time.now
Notify.insert eventlog
#item is obj instance or item_id, item_type
notify = {:item => "",
:item_id => "",
:item_type => "",
:create_user_id => "",
:relate_user_id = > ""
:event => "",
:content => "",
:content_path => ""
}
u= User.find_by_email "[email protected]"
m = u.message_boxes_inbox.where(:opened => false).last
notify = {:item => m, :create_user_id => m.sender_id, :relate_user_id => m.recipient_id, :event =>"New Message", :content => "New Message from #{m.from}!", :content_path => "/" }
Notify.create(notify)# return last item or false
=end
Notify = BBTDB.collection "notifies"
def Notify.create(notify = {})
if notify.has_key? :item and notify[:item].present?
item = notify.delete(:item)
notify[:item_id] = item.id
notify[:item_type] = item.class.to_s
end
required_columns = [:item_id,:item_type, :event, :content]
if (notify.keys & required_columns).sort.eql? required_columns.sort
Notify.insert notify.merge({read: false, updated_at: Time.now, created_at: Time.now})
Notify.find_one
else
false
end
end
def Notify.hello
puts "hello mogo!"
end
#{ _id: BSON::ObjectId( "505971c508a56411d7000001" ) }
#Notify.update({ _id: BSON::ObjectId( "505971c508a56411d7000001" ) },{:read => false})
def Notify.find_by_id(id="")
if id.present?
Notify.find({_id: BSON::ObjectId(id)}).first
end
end
#Notify.update({_id: BSON::ObjectId("alksjdfklaksdljf")},{xxxx})
def Notify.update_by_id(id="",conditions={})
if id.present?
Notify.update({_id: BSON::ObjectId(id)},'$set' => conditions.merge({updated_at: Time.now}))
end
end
def Notify.read(id="")
Notify.update_by_id(id = id,{read: true})
end
def Notify.read?(id="")
notify = Notify.find({_id: BSON::ObjectId(id)}).first
true if notify.has_keys? :read and notify[:read].eql? true
end
def Notify.user_notifies?(user_id="")
notify = Notify.find({relate_user_id: user_id}).to_a
end
def Notify.user_new_notifies?(user_id="")
notify = Notify.find({relate_user_id: user_id,read: false}).to_a
end
def Notify.unread(id="")
Notify.update_by_id(id = id,{read: false})
end
def Notify.destroy(id = "")
Notify.remove({_id: BSON::ObjectId(id)})
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment