Skip to content
This repository was archived by the owner on Jan 2, 2024. It is now read-only.

Commit 5ca29d5

Browse files
committed
send follow message.
1 parent 75a202e commit 5ca29d5

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

config/config.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
require './lib/observer'
22
Mongoid.configure.load!("./config/mongoid.yml")
3-
Mongoid.observers = BlockObserver,BlockLinkObserver
3+
Mongoid.observers = BlockObserver,BlockLinkObserver,WatchObserver
44
Mongoid.instantiate_observers

controllers/controller_for_views.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@
144144
r=Redis.new
145145
id=@current_user.Id
146146
while v=r.rpop(id) do
147-
@msgs << v
147+
@msgs << Marshal.load(v)
148148
end
149149
r.del("#{id}_count")
150150
@msg_count = 0

lib/observer.rb

+13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1+
# encoding: utf-8
2+
13
class BlockObserver < Mongoid::Observer
4+
def after_create(doc)
5+
end
26
end
37

48
class BlockLinkObserver < Mongoid::Observer
59
end
10+
11+
class WatchObserver < Mongoid::Observer
12+
def after_create(doc)
13+
if doc.WatchType=="user"
14+
user=User.where(:Id=>doc.UserId).first
15+
send_message(doc.WatchedId,"<a href='/user/#{user.Id}'>#{user.Name}</a> now follow you.")
16+
end
17+
end
18+
end

lib/utils.rb

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# encoding: utf-8
2+
13
def user_avatar(id)
24
user=User.where(:Id=>id).first
35
if user
@@ -26,4 +28,10 @@ def get_msg_count(user)
2628

2729
def is_followed(from_id,to_id)
2830
return Watch.where(:UserId=>from_id,:WatchedId=>to_id,:WatchType=>'user').first
31+
end
32+
33+
def send_message(user_id,msg)
34+
r=Redis.new
35+
r.rpush(user_id,Marshal.dump(msg))
36+
r.incr("#{user_id}_count")
2937
end

0 commit comments

Comments
 (0)