Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implemented mentions (replies)

  • Loading branch information...
commit 544defafe2922cfda66ccf100768cdc1ea9a3579 1 parent 68e4b59
@danlucraft danlucraft authored
View
2  README.md
@@ -1,7 +1,7 @@
Retwis-RB
=========
-An example application using the Redis key-value database.
+An example Twitter application using the Redis key-value database.
Daniel Lucraft (dan@fluentradical.com)
View
18 app.rb
@@ -15,7 +15,6 @@ def redis
before do
keys = redis.keys("*")
- p keys
end
get '/' do
@@ -66,11 +65,16 @@ def redis
erb :profile
end
+get '/:username/mentions' do |username|
+ @user = User.find_by_username(username)
+ @posts = @user.mentions
+ erb :mentions
+end
helpers do
def link_to_user(user)
f = <<-HTML
- <a href="/#{user.username}">#{user.username}</a>
+<a href="/#{user.username}">#{user.username}</a>
HTML
end
@@ -81,6 +85,16 @@ def pluralize(singular, plural, count)
count.to_s + " " + plural
end
end
+
+ def display_post(post)
+ post.content.gsub(/@\w+/) do |mention|
+ if user = User.find_by_username(mention[1..-1])
+ "@" + link_to_user(user)
+ else
+ mention
+ end
+ end
+ end
def time_ago_in_words(time)
distance_in_seconds = (Time.now - time).round
View
16 domain.rb
@@ -96,6 +96,13 @@ def timeline(page=1)
end
end
+ def mentions(page=1)
+ from, to = (page-1)*10, page*10
+ redis.list_range("user:id:#{id}:mentions", from, to).map do |post_id|
+ Post.new(post_id)
+ end
+ end
+
def add_post(post)
redis.push_head("user:id:#{id}:posts", post.id)
redis.push_head("user:id:#{id}:timeline", post.id)
@@ -105,6 +112,10 @@ def add_timeline_post(post)
redis.push_head("user:id:#{id}:timeline", post.id)
end
+ def add_mention(post)
+ redis.push_head("user:id:#{id}:mentions", post.id)
+ end
+
def follow(user)
return if user == self
redis.set_add("user:id:#{id}:followees", user.id)
@@ -155,6 +166,11 @@ def self.create(user, content)
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
View
5 views/_posts.erb
@@ -3,10 +3,13 @@
<% @posts.each do |post| %>
<div class="post">
<strong><%= link_to_user(post.user) %></strong>
- <%= post.content %><br />
+ <%= display_post(post) %>
<div class="date">
<%= time_ago_in_words(post.created_at) %>
</div>
</div>
<% end %>
+ <% if @posts.empty? %>
+ No posts.
+ <% end %>
</div>
View
3  views/header.erb
@@ -19,7 +19,8 @@
<% if @logged_in_user %>
<br /><br />
<a href="/">home</a> |
- <%= link_to_user(@logged_in_user) %> |
+ <a href="/<%= @logged_in_user.username %>/mentions">mentions</a> |
+ <%= link_to_user(@logged_in_user) %> |
<a href="/timeline">timeline</a> |
<a href="/logout">logout</a>
<% end %>
View
19 views/mentions.erb
@@ -0,0 +1,19 @@
+
+<%= erb :header %>
+
+<div class="span-16" id="maincol">
+ <h2>Mentions of: <%= @user.username %></h2>
+ <% if @logged_in_user != @user %>
+ <div class="box">
+ <% if @logged_in_user.following?(@user) %>
+ <a href="/<%= @logged_in_user.username %>/stopfollow/<%= @user.username %>">Stop following</a>
+ <% else %>
+ <a href="/<%= @logged_in_user.username %>/follow/<%= @user.username %>">Follow</a>
+ <% end %>
+ </div>
+ <% end %>
+
+ <%= erb :_posts %>
+</div>
+
+<%= erb :footer %>
View
1  views/profile.erb
@@ -9,6 +9,7 @@
<% else %>
<a href="/<%= @logged_in_user.username %>/follow/<%= @user.username %>">Follow</a>
<% end %>
+ | <a href="/<%= @user.username %>/mentions">See mentions</a>
</div>
<% end %>
Please sign in to comment.
Something went wrong with that request. Please try again.