forked from danlucraft/retwis-rb
-
Notifications
You must be signed in to change notification settings - Fork 2
/
domain.rb
185 lines (153 loc) · 3.75 KB
/
domain.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
class Timeline
def self.page(page)
from = (page-1)*10
to = (page)*10
post_ids = mb.list_range("timeline", from, to)
post_ids.map {|post_id| Post.new(post_id)}
end
end
class Model
def initialize(id)
@id = id
end
def ==(other)
@id.to_s == other.id.to_s
end
attr_reader :id
def self.property(name)
klass = self.name.downcase
self.class_eval <<-RUBY
def #{name}
_#{name}
end
def _#{name}
mb.get("#{klass}:id:" + id.to_s + ":#{name}")
end
def #{name}=(val)
mb.set("#{klass}:id:" + id.to_s + ":#{name}", val)
end
RUBY
end
end
class User < Model
def self.find_by_username(username)
if id = mb.get("user:username:#{username}")
User.new(id)
end
end
def self.find_by_id(id)
if mb.exists?("user:id:#{id}:username")
User.new(id)
end
end
def self.create(username, password)
user_id = mb.incr("user:uid")
salt = User.new_salt
mb.set("user:id:#{user_id}:username", username)
mb.set("user:username:#{username}", user_id)
mb.set("user:id:#{user_id}:salt", salt)
mb.set("user:id:#{user_id}:hashed_password", hash_pw(salt, password))
mb.push("users", user_id)
User.new(user_id)
end
def self.new_users
mb.list_range("users", 0, 10).map do |user_id|
User.new(user_id)
end
end
def self.new_salt
arr = %w(a b c d e f)
(0..6).to_a.map{ arr[rand(6)] }.join
end
def self.hash_pw(salt, password)
Digest::MD5.hexdigest(salt + password)
end
property :username
property :salt
property :hashed_password
def posts(page=1)
from, to = (page-1)*10, page*10
mb.list_range("user:id:#{id}:posts", from, to).map do |post_id|
Post.new(post_id)
end
end
def timeline(page=1)
from, to = (page-1)*10, page*10
mb.list_range("user:id:#{id}:timeline", from, to).map do |post_id|
Post.new(post_id)
end
end
def mentions(page=1)
from, to = (page-1)*10, page*10
mb.list_range("user:id:#{id}:mentions", from, to).map do |post_id|
Post.new(post_id)
end
end
def add_post(post)
mb.push("user:id:#{id}:posts", post.id)
mb.push("user:id:#{id}:timeline", post.id)
end
def add_timeline_post(post)
mb.push("user:id:#{id}:timeline", post.id)
end
def add_mention(post)
mb.push("user:id:#{id}:mentions", post.id)
end
def follow(user)
return if user == self
mb.set_add("user:id:#{id}:followees", user.id)
user.add_follower(self)
end
def stop_following(user)
mb.set_delete("user:id:#{id}:followees", user.id)
user.remove_follower(self)
end
def following?(user)
mb.set_member?("user:id:#{id}:followees", user.id)
end
def followers
mb.set_members("user:id:#{id}:followers").map do |user_id|
User.new(user_id)
end
end
def followees
mb.set_members("user:id:#{id}:followees").map do |user_id|
User.new(user_id)
end
end
protected
def add_follower(user)
mb.set_add("user:id:#{id}:followers", user.id)
end
def remove_follower(user)
mb.set_delete("user:id:#{id}:followers", user.id)
end
end
class Post < Model
def self.create(user, content)
post_id = mb.incr("post:uid")
post = Post.new(post_id)
post.content = content
post.user_id = user.id
post.created_at = Time.now.to_s
post.user.add_post(post)
mb.push("timeline", post_id)
post.user.followers.each do |follower|
follower.add_timeline_post(post)
end
content.scan(/@\w+/).each do |mention|
if user = User.find_by_username(mention[1..-1])
user.add_mention(post)
end
end
end
property :content
property :user_id
property :created_at
def created_at
Time.parse(_created_at)
end
def user
User.new(user_id)
end
end