Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 135 lines (122 sloc) 4.741 kb
1ecb0ff3 »
2011-10-17 added comments.rb from redis book programming examples. Still to modi…
1 # Copyright 2011 Salvatore Sanfilippo. All rights reserved.
2 #
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are met:
5 #
6 # 1. Redistributions of source code must retain the above copyright
7 # notice, this list of conditions and the following disclaimer.
8 #
9 # 2. Redistributions in binary form must reproduce the above copyright
10 # notice, this list of conditions and the following disclaimer in the
11 # documentation and/or other materials provided with the distribution.
12 #
13 # THIS SOFTWARE IS PROVIDED BY SALVATORE SANFILIPPO ''AS IS'' AND ANY EXPRESS
14 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
16 # NO EVENT SHALL SALVATORE SANFILIPPO OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
17 # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 #
24 # The views and conclusions contained in the software and documentation are
25 # those of the authors and should not be interpreted as representing official
26 # policies, either expressed or implied, of Salvaore Sanfilippo.
27
28 require 'rubygems'
29 require 'json'
30
31 class RedisComments
32 def initialize(redis,namespace,sort_proc=nil)
33 @r = redis
34 @namespace = namespace
35 @sort_proc = sort_proc
36 end
37
38 def thread_key(thread_id)
39 "thread:#{@namespace}:#{thread_id}"
40 end
1ae8534c »
2011-10-18 voting of news implemented. Comments work in progress...
41
42 def fetch(thread_id,comment_id)
43 key = thread_key(thread_id)
44 @r.hget(key,id)
45 end
1ecb0ff3 »
2011-10-17 added comments.rb from redis book programming examples. Still to modi…
46
47 def insert(thread_id,comment)
48 raise "no parent_id field" if !comment.has_key?('parent_id')
49 key = thread_key(thread_id)
1ae8534c »
2011-10-18 voting of news implemented. Comments work in progress...
50 if comment['parent_id'] != -1
51 parent = @r.hget(key,comment['parent_id'])
52 return false if !parent
53 end
1ecb0ff3 »
2011-10-17 added comments.rb from redis book programming examples. Still to modi…
54 id = @r.hincrby(key,:nextid,1)
55 @r.hset(key,id,comment.to_json)
56 return id.to_i
57 end
58
1ae8534c »
2011-10-18 voting of news implemented. Comments work in progress...
59 def edit(thread_id,comment_id,updates)
1ecb0ff3 »
2011-10-17 added comments.rb from redis book programming examples. Still to modi…
60 key = thread_key(thread_id)
61 old = @r.hget(key,id)
62 return false if !old
1ae8534c »
2011-10-18 voting of news implemented. Comments work in progress...
63 comment = JSON.parse(old).merge(updates)
1ecb0ff3 »
2011-10-17 added comments.rb from redis book programming examples. Still to modi…
64 @r.hset(key,id,comment.to_json)
65 return true
66 end
67
68 def remove_thread(thread_id)
69 @r.del(thread_key(thread_id))
70 end
71
72 def comments_in_thread(thread_id)
73 @r.hlen(thread_key(thread_id)).to_i-1
74 end
75
76 def del_comment(thread_id,comment_id)
1ae8534c »
2011-10-18 voting of news implemented. Comments work in progress...
77 self.edit(thread_id,comment_id,{"del" => 1})
1ecb0ff3 »
2011-10-17 added comments.rb from redis book programming examples. Still to modi…
78 end
79
80 def render_comments(thread_id,&block)
81 byparent = {}
82 @r.hgetall(thread_key(thread_id)).each{|id,comment|
83 next if id == "nextid"
84 c = JSON.parse(comment)
85 c['id'] = id.to_i
86 parent_id = c['parent_id'].to_i
87 byparent[parent_id] = [] if !byparent.has_key?(parent_id)
88 byparent[parent_id] << c
89 }
90 render_comments_rec(byparent,-1,0,block)
91 end
92
93 def render_comments_rec(byparent,parent_id,level,block)
94 thislevel = byparent[parent_id]
95 thislevel = @sort_proc.call(thislevel,level) if @sort_proc
96 thislevel.each{|c|
97 c['level'] = level
98 block.call(c)
99 if byparent[c['id']]
100 render_comments_rec(byparent,c['id'],level+1,block)
101 end
102 }
103 end
104 end
105
106 # In this example we want comments at top level sorted in reversed chronological
107 # order, but all the sub trees sorted in plain chronological order.
1ae8534c »
2011-10-18 voting of news implemented. Comments work in progress...
108 # comments = RedisComments.new(Redis.new,"mycomments",proc{|c,level|
109 # if level == 0
110 # c.sort {|a,b| b['ctime'] <=> a['ctime']}
111 # else
112 # c.sort {|a,b| a['ctime'] <=> b['ctime']}
113 # end
114 # })
115 #
116 # comments.remove_thread(50)
117 # first_id = comments.insert(50,
118 # {'body' => 'First comment at top level','parent_id'=>-1,'ctime'=>1000}
119 # )
120 # second_id = comments.insert(50,
121 # {'body' => 'Second comment at top level','parent_id'=>-1,'ctime'=>1001}
122 # )
123 # id = comments.insert(50,
124 # {'body' => 'reply number one','parent_id'=>second_id,'ctime'=>1002}
125 # )
126 # id = comments.insert(50,
127 # {'body' => 'reply to reply','parent_id'=>id,'ctime'=>1003}
128 # )
129 # id = comments.insert(50,
130 # {'body' => 'reply number two','parent_id'=>second_id,'ctime'=>1002}
131 # )
132 # rendered_comments = comments.render_comments(50) {|c|
133 # puts (" "*c['level']) + c['body']
134 # }
Something went wrong with that request. Please try again.