added tag follow feature, mention page feature. is now pretty easy to add new types of streams, but some more refactoring could make it even nicer

This commit is contained in:
Maxwell Salzberg 2011-09-27 23:48:07 -07:00
parent b5c3f2c615
commit 429ac42502
13 changed files with 151 additions and 16 deletions

View file

@ -0,0 +1,14 @@
require File.join(Rails.root, '/lib/mention_stream')
class MentionsController < ApplicationController
before_filter :authenticate_user!
def index
@stream = MentionStream.new(current_user, :max_time => params[:max_time])
if params[:only_posts]
render :partial => 'shared/stream', :locals => {:posts => @stream.posts}
else
render 'aspects/index'
end
end
end

View file

@ -3,10 +3,15 @@ class TagFollowingsController < ApplicationController
before_filter :authenticate_user! before_filter :authenticate_user!
def index def index
@stream = TagStream.new(current_user) @stream = TagStream.new(current_user, :max_time => params[:max_time])
render 'aspects/index', :locals => {:posts => @stream.posts} if params[:only_posts]
render :partial => 'shared/stream', :locals => {:posts => @stream.posts}
else
render 'aspects/index'
end
end end
# POST /tag_followings # POST /tag_followings
# POST /tag_followings.xml # POST /tag_followings.xml
def create def create

View file

@ -12,6 +12,8 @@ module StreamHelper
person_path(@person, :max_time => @posts.last.created_at.to_i) person_path(@person, :max_time => @posts.last.created_at.to_i)
elsif controller.instance_of?(TagFollowingsController) elsif controller.instance_of?(TagFollowingsController)
tag_followings_path(:max_time => time_for_scroll(opts[:ajax_stream], @stream)) tag_followings_path(:max_time => time_for_scroll(opts[:ajax_stream], @stream))
elsif controller.instance_of?(MentionsController)
mentions_path(:max_time => time_for_scroll(opts[:ajax_stream], @stream))
elsif controller.instance_of?(AspectsController) elsif controller.instance_of?(AspectsController)
aspects_path(:max_time => time_for_scroll(opts[:ajax_stream], @stream), :a_ids => @stream.aspect_ids) aspects_path(:max_time => time_for_scroll(opts[:ajax_stream], @stream), :a_ids => @stream.aspect_ids)
else else

View file

@ -34,6 +34,10 @@ class Post < ActiveRecord::Base
scope :all_public, where(:public => true, :pending => false) scope :all_public, where(:public => true, :pending => false)
def self.for_a_stream(max_time, order)
where("#{order} < ?", max_time).order("#{order} desc").includes({:author => :profile}, :mentions).limit(15)
end
def diaspora_handle def diaspora_handle
read_attribute(:diaspora_handle) || self.author.diaspora_handle read_attribute(:diaspora_handle) || self.author.diaspora_handle
end end

View file

@ -20,7 +20,11 @@
= render 'aspects/no_contacts_message' = render 'aspects/no_contacts_message'
#main_stream.stream{:data => {:guids => stream.aspect_ids.join(',')}} #main_stream.stream{:data => {:guids => stream.aspect_ids.join(',')}}
<<<<<<< HEAD
- if stream.ajax_stream? - if stream.ajax_stream?
=======
- if stream.ajax_posts?
>>>>>>> added tag follow feature, mention page feature. is now pretty easy to add new types of streams, but some more refactoring could make it even nicer
#pagination #pagination
=link_to(t('more'), next_page_path(:ajax_stream => true), :class => 'paginate') =link_to(t('more'), next_page_path(:ajax_stream => true), :class => 'paginate')

View file

@ -7,12 +7,7 @@
- if @stream.people.size > 0 - if @stream.people.size > 0
- for person in @stream.people.sample(15) - for person in @stream.people.sample(15)
= person_image_link(person) = person_image_link(person)
= link_to t('.view_all_contacts'), @stream.contacts_link, :id => "view_all_contacts_link"
- if @stream.for_all_aspects? || @stream.aspect_ids.size > 1
= link_to t('.view_all_contacts'), contacts_link, :id => "view_all_contacts_link"
- else
= link_to t('.view_all_contacts'), contacts_path(:a_id => @stream.aspect.id), :id => "view_all_contacts_link"
- else - else
= t('.no_contacts') = t('.no_contacts')
= link_to t('.manage_your_aspects'), contacts_link = link_to t('.manage_your_aspects'), contacts_link

View file

@ -21,6 +21,9 @@
.section .section
= render 'aspects/aspect_listings' = render 'aspects/aspect_listings'
.section
= link_to "Mentions", mentions_path
.section#followed_tags_listing .section#followed_tags_listing
= render 'tags/followed_tags_listings' = render 'tags/followed_tags_listings'

View file

@ -54,6 +54,7 @@ Diaspora::Application.routes.draw do
get "tag_followings" => "tag_followings#index", :as => 'tag_followings' get "tag_followings" => "tag_followings#index", :as => 'tag_followings'
resources :mentions, :only => [:index]
get 'tags/:name' => 'tags#show', :as => 'tag' get 'tags/:name' => 'tags#show', :as => 'tag'

View file

@ -94,4 +94,12 @@ class AspectStream
"#{self.aspect.name}(#{self.people.size})" "#{self.aspect.name}(#{self.people.size})"
end end
end end
def contacts_link
if for_all_aspects? || aspect_ids.size > 1
Rails.application.routes.url_helpers.contacts_path
else
Rails.application.routes.url_helpers.contacts_path(:a_id => @stream.aspect.id)
end
end
end end

72
lib/mention_stream.rb Normal file
View file

@ -0,0 +1,72 @@
# Copyright (c) 2010-2011, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
class MentionStream
attr_reader :max_time, :order
# @param user [User]
# @param inputted_aspect_ids [Array<Integer>] Ids of aspects for given stream
# @param aspect_ids [Array<Integer>] Aspects this stream is responsible for
# @opt max_time [Integer] Unix timestamp of stream's post ceiling
# @opt order [String] Order of posts (i.e. 'created_at', 'updated_at')
# @return [void]
def initialize(user, opts={})
@user = user
set_max_time(opts[:max_time])
@order = opts[:order] || 'created_at'
end
def set_max_time(time_string)
@max_time = Time.at(time_string.to_i) unless time_string.blank?
@max_time ||= (Time.now + 1)
end
def link(opts={})
Rails.application.routes.url_helpers.mentions_path(opts)
end
def title
"Your Mentions"
end
# @return [ActiveRecord::Association<Post>] AR association of posts
def posts
@posts ||= Post.joins(:mentions).where(:mentions => {:person_id => @user.person.id}).for_a_stream(@max_time, @order)
end
# @return [ActiveRecord::Association<Person>] AR association of people within stream's given aspects
def people
@people ||= posts.map{|p| p.author}.uniq
end
def for_all_aspects?
false
end
def ajax_posts?
false
end
def aspects
[]
end
def aspect
nil
end
def contacts_title
"People who mentioned you"
end
def contacts_link
'#'
end
def aspect_ids
[]
end
end

View file

@ -14,10 +14,16 @@ class TagStream
# @return [void] # @return [void]
def initialize(user, opts={}) def initialize(user, opts={})
@tags = user.followed_tags @tags = user.followed_tags
@tag_string = @tags.join(', '){|tag| tag.name} @tag_string = @tags.join(', '){|tag| tag.name}.to_s
@user = user @user = user
@max_time = opts[:max_time] set_max_time(opts[:max_time])
@order = opts[:order]
@order = opts[:order] || 'created_at'
end
def set_max_time(time_string)
@max_time = Time.at(time_string.to_i) unless time_string.blank?
@max_time ||= (Time.now + 1)
end end
def link(opts={}) def link(opts={})
@ -25,14 +31,12 @@ class TagStream
end end
def title def title
"Tag Stream" @tag_string.titleize.split(',').to_sentence
end end
# @return [ActiveRecord::Association<Post>] AR association of posts # @return [ActiveRecord::Association<Post>] AR association of posts
def posts def posts
@posts ||= StatusMessage.tagged_with([@tag_string], :any => true) @posts ||= StatusMessage.tagged_with([@tag_string], :any => true).for_a_stream(@max_time, @order)
end end
# @return [ActiveRecord::Association<Person>] AR association of people within stream's given aspects # @return [ActiveRecord::Association<Person>] AR association of people within stream's given aspects
@ -44,6 +48,10 @@ class TagStream
false false
end end
def ajax_posts?
false
end
def aspects def aspects
[] []
end end
@ -56,8 +64,11 @@ class TagStream
"People who like #{@tag_string}" "People who like #{@tag_string}"
end end
def contacts_link
'#'
end
def aspect_ids def aspect_ids
[] []
end end
end end

View file

@ -0,0 +1,8 @@
Diaspora.Pages.MentionsIndex = function() {
var self = this;
this.subscribe("page/ready", function(evt, document) {
self.stream = self.instantiate("Stream", document.find("#aspect_stream_container"));
self.infiniteScroll = self.instantiate("InfiniteScroll");
});
};

View file

@ -0,0 +1,8 @@
Diaspora.Pages.TagFollowingsIndex = function() {
var self = this;
this.subscribe("page/ready", function(evt, document) {
self.stream = self.instantiate("Stream", document.find("#aspect_stream_container"));
self.infiniteScroll = self.instantiate("InfiniteScroll");
});
};