Skip to content

Commit

Permalink
Added: 你可能会关注的人或者话题
Browse files Browse the repository at this point in the history
  • Loading branch information
nowa committed Apr 21, 2011
1 parent 82bc4ef commit 3b0d096
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 15 deletions.
18 changes: 18 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,24 @@ def unread_notifies

[notifies, notifications]
end

# 推荐给我的人或者话题
def related_items
# related_people = self.following.inject([]) do |memo, person|
# memo += person.following
# end
related_people = self.followed_topics.inject([]) do |memo, topic|
memo += topic.followers
end.uniq
related_people = related_people - self.following - [self] if related_people

related_topics = self.following.inject([]) do |memo, person|
memo += person.followed_topics
end.uniq
related_topics -= self.followed_topics if related_topics

return related_people + related_topics
end

protected

Expand Down
35 changes: 27 additions & 8 deletions app/views/asks/_sidebar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,32 @@
</ul>
</div>
<% if current_user %>
<div class="box">
<h2>邀请好友</h2>
<ul class="sections">
<li>
<a href="<%= new_user_invitation_path %>">邀请你的朋友注册</a>
</li>
</ul>
</div>
<div class="box">
<h2>邀请好友</h2>
<ul class="sections">
<li>
<a href="<%= new_user_invitation_path %>">邀请你的朋友注册</a>
</li>
</ul>
</div>

<%
related = current_user.related_items

if related.size > 0
%>
<div class="box">
<h2>你可能会关注的人或话题</h2><br>
<ul class="followed-item listing">
<% for item in related.random_pick(6) %>
<% if item.is_a?(User) %>
<%= render "home/recommended_user", :follower => item %>
<% elsif item.is_a?(Topic) %>
<%= render "home/recommended_topic", :topic => item %>
<% end %>
<% end %>
</ul>
</div>
<% end %>
<% end %>
<% end %>
14 changes: 14 additions & 0 deletions app/views/home/_recommended_topic.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<li>
<div class="item">
<a href="<%= topic_path(topic.name) %>">#<%= topic.name %></a>
</div>
<% if current_user %>
<div class="action">
<% if current_user.topic_followed?(topic) %>
<a class="flat_button small" href="#" onclick="return Topics.unfollow(this, '<%= topic.name %>','small');">取消关注</a>
<% else %>
<a class="green_button small" href="#" onclick="return Topics.follow(this, '<%= topic.name %>','small');">关注</a>
<% end %>
</div>
<% end %>
</li>
22 changes: 22 additions & 0 deletions app/views/home/_recommended_user.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<li>
<div class="item">
<div class="left">
<a href="/users/<%= follower.slug %>" title="<%= follower.name %>">
<%= user_avatar_tag(follower, :small) %>
</a>
</div>
<div class="right">
<p><a href="<%= user_path(follower.slug) %>"><%= follower.name %></a></p>
<p class="tagline"><%= truncate(follower.tagline,:length => 9, :truncate_string =>"…") %></p>
</div>
</div>
<% if current_user %>
<div class="action">
<% if current_user.followed?(follower) %>
<a class="flat_button small" href="#" onclick="return Users.unfollow(this, '<%= follower.slug %>','small');">取消关注</a>
<% else %>
<a class="green_button small" href="#" onclick="return Users.follow(this, '<%= follower.slug %>','small');">关注</a>
<% end %>
</div>
<% end %>
</li>
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<% if current_user %>
window.WEB_SOCKET_SWF_LOCATION = "http://<%= @zomet_config["server"] %>/WebSocketMain.swf";
$(document).ready(function() {
var jug = new Juggernaut({host: '<%= @zomet_config["server"] %>', port: <%= @zomet_config["port"] || 80 %>});
var jug = new Juggernaut({host: '<%= @zomet_config["host"] %>', port: <%= @zomet_config["port"] || 80 %>});
jug.subscribe("/notifications/<%= current_user.slug %>", function(data){
$('#notify_badge').removeClass("force-hide");
if ($(document).attr("title").indexOf("(新) ") < 0) {
Expand Down
7 changes: 4 additions & 3 deletions app/views/logs/_log.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ action = log.action.gsub("ADDTOPIC", "ADD_TOPIC").gsub("DELTOPIC", "DEL_TOPIC")
case log._type
when "AskLog"
item = log.ask
if !item.blank?
if !item.blank? and item.spams_count < Setting.ask_spam_max
%>
<div class="ask" ask-id="<%= log.id %>">
<div class="savatar">
Expand Down Expand Up @@ -82,7 +82,8 @@ when "UserLog"
elsif ["THANK_ANSWER"].include?(action)
item = Answer.first(:conditions => {:id => log.target_id})
end
if !item.nil?

if !item.nil? or (item.is_a?(Ask) and item.spams_count < Setting.ask_spam_max)
%>
<div class="ask" ask-id="<%= log.id %>">
<% if ["FOLLOW_TOPIC", "UNFOLLOW_TOPIC"].include?(action) %>
Expand Down Expand Up @@ -141,7 +142,7 @@ when "UserLog"
when "AnswerLog"
if log.answer
item = log.answer.ask
if !item.nil?
if !item.nil? and item.spams_count < Setting.ask_spam_max
%>
<div class="ask" ask-id="<%= item.id %>">
<div class="savatar">
Expand Down
1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ class Application < Rails::Application
end

require "string_extensions"
require "array_extensions"
require "zomet"
8 changes: 8 additions & 0 deletions lib/array_extensions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module ArrayExtensions
# 随机从数组里取出N个元素
def random_pick(number)
sort_by{ rand }.slice(0...number)
end
end

Array.send :include,ArrayExtensions
4 changes: 3 additions & 1 deletion lib/zomet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ def use_zomet(options = nil)
module InstanceMethods
def load_zomet_config
@zomet_config = YAML.load_file("#{Rails.root}/config/zomet.yml")[Rails.env]
@zomet_config["port"] ||= URI.parse("http://#{@zomet_config["server"]}").port
uri = URI.parse("http://#{@zomet_config["server"]}")
@zomet_config["host"] ||= uri.host
@zomet_config["port"] ||= uri.port
end

def pub_to_browser(options)
Expand Down
2 changes: 1 addition & 1 deletion public/stylesheets/front.css
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ body { text-align:center; }

.sidebar .sections { font-size:14px; }
.sidebar .sections li { display:inline; }
.sidebar .sections li a { display:block; padding:5px 18px; margin-bottom:4px;
.sidebar .sections li a { display:block; padding:3px 18px; margin-bottom:3px;
background:url(/images/section_arrow.gif) left center no-repeat; }
.sidebar .sections li a:hover,
.sidebar .sections li a.active { background-color: #DFEAF4; text-decoration: none; }
Expand Down
2 changes: 1 addition & 1 deletion public/stylesheets/users.css
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
border-bottom: 1px dotted #C8C8C8;
}

.topic-listing li.last, .followers-listing li.last {
.topic-listing li.last, .followers-listing li.last, .followed-item li.first {
border: none;
}

Expand Down

0 comments on commit 3b0d096

Please sign in to comment.