From be662a65c6989dada094c3561b0e00d951993937 Mon Sep 17 00:00:00 2001 From: danielgrippi Date: Wed, 18 May 2011 15:09:28 -0700 Subject: [PATCH 1/6] added token authenticatable to user model --- app/controllers/users_controller.rb | 9 +++++++++ app/models/user.rb | 2 +- config/initializers/devise.rb | 2 +- config/routes.rb | 6 ++++++ .../20110518184453_add_token_auth_to_user.rb | 11 +++++++++++ db/schema.rb | 4 +++- spec/controllers/users_controller_spec.rb | 16 +++++++++++++++- 7 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20110518184453_add_token_auth_to_user.rb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index cc8d482f3..fd37f6799 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -141,4 +141,13 @@ class UsersController < ApplicationController tar_path = PhotoMover::move_photos(current_user) send_data( File.open(tar_path).read, :filename => "#{current_user.id}.tar" ) end + + def generate_new_token + if current_user.reset_authentication_token! + @token = current_user.authentication_token + else + @token = "No token created" + end + render :text => @token + end end diff --git a/app/models/user.rb b/app/models/user.rb index 5620d09dc..b695d383b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -13,7 +13,7 @@ class User < ActiveRecord::Base devise :invitable, :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, - :timeoutable + :timeoutable, :token_authenticatable before_validation :strip_and_downcase_username before_validation :set_current_language, :on => :create diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index 1e1771d3e..c55c139f0 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -113,7 +113,7 @@ Devise.setup do |config| # ==> Configuration for :token_authenticatable # Defines name of the authentication token params key - # config.token_authentication_key = :auth_token + config.token_authentication_key = :auth_token # ==> Scopes configuration # Turn scoped views on. Before rendering "sessions/new", it will first check for diff --git a/config/routes.rb b/config/routes.rb index 799cf3d01..79c1ba85c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -59,6 +59,12 @@ Diaspora::Application.routes.draw do :invitations => "invitations"} do get 'invitations/resend/:id' => 'invitations#resend', :as => 'invitation_resend' end + + # generating a new user token (for devise) + match 'users/generate_new_token' => 'users#generate_new_token' + + + get 'login' => redirect('/users/sign_in') scope 'admins', :controller => :admins do diff --git a/db/migrate/20110518184453_add_token_auth_to_user.rb b/db/migrate/20110518184453_add_token_auth_to_user.rb new file mode 100644 index 000000000..8e7831291 --- /dev/null +++ b/db/migrate/20110518184453_add_token_auth_to_user.rb @@ -0,0 +1,11 @@ +class AddTokenAuthToUser < ActiveRecord::Migration + def self.up + add_column(:users, :authentication_token, :string, :limit => 30) + add_index(:users, :authentication_token, :unique => true) + end + + def self.down + remove_index(:users, :column => :authentication_token) + remove_column(:users, :authentication_token) + end +end diff --git a/db/schema.rb b/db/schema.rb index b11a750c6..73327813c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20110518010050) do +ActiveRecord::Schema.define(:version => 20110518184453) do create_table "aspect_memberships", :force => true do |t| t.integer "aspect_id", :null => false @@ -361,8 +361,10 @@ ActiveRecord::Schema.define(:version => 20110518010050) do t.integer "invitation_limit" t.integer "invited_by_id" t.string "invited_by_type" + t.string "authentication_token", :limit => 30 end + add_index "users", ["authentication_token"], :name => "index_users_on_authentication_token", :unique => true add_index "users", ["email"], :name => "index_users_on_email" add_index "users", ["invitation_service", "invitation_identifier"], :name => "index_users_on_invitation_service_and_invitation_identifier", :unique => true add_index "users", ["invitation_token"], :name => "index_users_on_invitation_token" diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index eb8a90758..ade63a1a8 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -138,4 +138,18 @@ describe UsersController do assigns[:email_prefs]['mentioned'].should be_false end end -end \ No newline at end of file + + describe '#generate_new_token' do + it 'generates a new token for the current user' do + lambda { + get 'generate_new_token' + }.should change{ @user.reload.authentication_token } + end + + it 'displays a token' do + get 'generate_new_token' + response.body.should include(@user.reload.authentication_token) + end + end + +end From 9b941e6520b3364879a4155b8fa7ffa972141373 Mon Sep 17 00:00:00 2001 From: danielgrippi Date: Wed, 18 May 2011 16:57:23 -0700 Subject: [PATCH 2/6] Bookmark now consumes cubbi.es JSON, cubbi.es things can be saved to Diaspora, but are not displayed. --- app/controllers/bookmarks_controller.rb | 55 +++++++++++++++++++ app/models/bookmark.rb | 31 +++++++++++ config/routes.rb | 1 + .../20110518222303_add_column_for_bookmark.rb | 15 +++++ db/schema.rb | 6 +- spec/models/bookmark_spec.rb | 36 ++++++++++++ 6 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 app/controllers/bookmarks_controller.rb create mode 100644 app/models/bookmark.rb create mode 100644 db/migrate/20110518222303_add_column_for_bookmark.rb create mode 100644 spec/models/bookmark_spec.rb diff --git a/app/controllers/bookmarks_controller.rb b/app/controllers/bookmarks_controller.rb new file mode 100644 index 000000000..1635e1736 --- /dev/null +++ b/app/controllers/bookmarks_controller.rb @@ -0,0 +1,55 @@ +# Copyright (c) 2010, Diaspora Inc. This file is +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. + +class BookmarksController < ApplicationController + before_filter :authenticate_user! + skip_before_filter :verify_authenticity_token + + respond_to :json + + def create + @bookmark = Bookmark.from_activity(params[:activity]) + @bookmark.author = current_user.person + + if @bookmark.save + Rails.logger.info("event=create type=bookmark") + + current_user.add_to_streams(@bookmark, current_user.aspects) + current_user.dispatch_post(@bookmark, :url => post_url(@bookmark)) + + render :nothing => true, :status => 201 + end + end + +=begin + def destroy + if @bookmark = current_user.posts.where(:id => params[:id]).first + current_user.retract(@bookmark) + else + Rails.logger.info "event=post_destroy status=failure user=#{current_user.diaspora_handle} reason='User does not own post'" + render :nothing => true, :status => 404 + end + end + + def show + @status_message = current_user.find_visible_post_by_id params[:id] + if @status_message + @object_aspect_ids = @status_message.aspects.map{|a| a.id} + + # mark corresponding notification as read + if notification = Notification.where(:recipient_id => current_user.id, :target_id => @status_message.id).first + notification.unread = false + notification.save + end + + respond_with @status_message + else + Rails.logger.info(:event => :link_to_nonexistent_post, :ref => request.env['HTTP_REFERER'], :user_id => current_user.id, :post_id => params[:id]) + flash[:error] = I18n.t('status_messages.show.not_found') + redirect_to :back + end + end +=end + +end diff --git a/app/models/bookmark.rb b/app/models/bookmark.rb new file mode 100644 index 000000000..0a60524d6 --- /dev/null +++ b/app/models/bookmark.rb @@ -0,0 +1,31 @@ +# Copyright (c) 2010, Diaspora Inc. This file is +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. + +class Bookmark < Post + include Diaspora::Socketable + + validates_presence_of :target_url + + + def socket_to_user(user_or_id, opts={}) #adds aspect_ids to opts if they are not there + unless opts[:aspect_ids] + user_id = user_or_id.instance_of?(Fixnum) ? user_or_id : user_or_id.id + aspect_ids = AspectMembership.connection.execute( + AspectMembership.joins(:contact).where(:contacts => {:user_id => user_id, :person_id => self.author_id}).select('aspect_memberships.aspect_id').to_sql + ).map{|r| r.first} + opts.merge!(:aspect_ids => aspect_ids) + end + super(user_or_id, opts) + end + + def self.from_activity(json) + self.new( + :image_url => json["target"]["image"]["url"], + :image_height => json["target"]["image"]["height"], + :image_width => json["target"]["image"]["width"], + :target_url => json["target"]["url"] + ) + end +end + diff --git a/config/routes.rb b/config/routes.rb index 79c1ba85c..dcd1f9fe6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -63,6 +63,7 @@ Diaspora::Application.routes.draw do # generating a new user token (for devise) match 'users/generate_new_token' => 'users#generate_new_token' + resources :bookmarks, :only => :create get 'login' => redirect('/users/sign_in') diff --git a/db/migrate/20110518222303_add_column_for_bookmark.rb b/db/migrate/20110518222303_add_column_for_bookmark.rb new file mode 100644 index 000000000..8f8670be7 --- /dev/null +++ b/db/migrate/20110518222303_add_column_for_bookmark.rb @@ -0,0 +1,15 @@ +class AddColumnForBookmark < ActiveRecord::Migration + def self.up + add_column(:posts, :target_url, :string) + add_column(:posts, :image_url, :string) + add_column(:posts, :image_height, :integer) + add_column(:posts, :image_width, :integer) + end + + def self.down + remove_column(:posts, :image_width) + remove_column(:posts, :image_height) + remove_column(:posts, :image_url) + remove_column(:posts, :target_url) + end +end diff --git a/db/schema.rb b/db/schema.rb index 73327813c..136864edd 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20110518184453) do +ActiveRecord::Schema.define(:version => 20110518222303) do create_table "aspect_memberships", :force => true do |t| t.integer "aspect_id", :null => false @@ -242,6 +242,10 @@ ActiveRecord::Schema.define(:version => 20110518184453) do t.datetime "updated_at" t.string "mongo_id" t.string "unprocessed_image" + t.string "target_url" + t.string "image_url" + t.integer "image_height" + t.integer "image_width" end add_index "posts", ["author_id"], :name => "index_posts_on_person_id" diff --git a/spec/models/bookmark_spec.rb b/spec/models/bookmark_spec.rb new file mode 100644 index 000000000..80a288f38 --- /dev/null +++ b/spec/models/bookmark_spec.rb @@ -0,0 +1,36 @@ +# Copyright (c) 2010, Diaspora Inc. This file is +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. + +require 'spec_helper' + +describe Bookmark do + describe '.from_activity' do + before do + @json = { + "verb"=>"save", + "target"=> { + "url"=>"http://abcnews.go.com/US/wireStory?id=13630884", + "objectType"=>"photo", + "image"=> { + "url"=> "http://a.abcnews.com/images/Entertainment/abc_ann_wtb_blake_leo_110518_wl.jpg", + "height"=>"112", + "width"=>"200" + } + }, + "object"=> { + "url"=>"cubbi.es/daniel", + "objectType"=>"bookmark" + } + } + end + it 'marshals into a bookmark' do + bookmark = Bookmark.from_activity(@json) + bookmark.image_url.should == @json["target"]["image"]["url"] + bookmark.image_height.should == @json["target"]["image"]["height"].to_i + bookmark.image_width.should == @json["target"]["image"]["width"].to_i + bookmark.target_url.should == @json["target"]["url"] + end + + end +end From 41e1d0dd867aaa96977bba38578267aa7c7c4f09 Mon Sep 17 00:00:00 2001 From: danielgrippi Date: Wed, 18 May 2011 17:14:48 -0700 Subject: [PATCH 3/6] show photos from cubbies in the streams --- app/controllers/aspects_controller.rb | 2 +- app/controllers/bookmarks_controller.rb | 1 + app/controllers/people_controller.rb | 4 ++-- app/views/shared/_stream_element.html.haml | 6 +++++- 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/app/controllers/aspects_controller.rb b/app/controllers/aspects_controller.rb index cc5afedcc..a66a4b7de 100644 --- a/app/controllers/aspects_controller.rb +++ b/app/controllers/aspects_controller.rb @@ -31,7 +31,7 @@ class AspectsController < ApplicationController @aspect_ids = @aspects.map { |a| a.id } posts = current_user.visible_posts(:by_members_of => @aspect_ids, - :type => 'StatusMessage', + :type => ['StatusMessage','Bookmark'], :order => session[:sort_order] + ' DESC', :max_time => params[:max_time].to_i ).includes(:comments, :mentions, :likes, :dislikes) diff --git a/app/controllers/bookmarks_controller.rb b/app/controllers/bookmarks_controller.rb index 1635e1736..f3e1826cb 100644 --- a/app/controllers/bookmarks_controller.rb +++ b/app/controllers/bookmarks_controller.rb @@ -11,6 +11,7 @@ class BookmarksController < ApplicationController def create @bookmark = Bookmark.from_activity(params[:activity]) @bookmark.author = current_user.person + @bookmark.public = true if @bookmark.save Rails.logger.info("event=create type=bookmark") diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb index 7b8b74df1..78711df0b 100644 --- a/app/controllers/people_controller.rb +++ b/app/controllers/people_controller.rb @@ -86,10 +86,10 @@ class PeopleController < ApplicationController else @commenting_disabled = false end - @posts = current_user.posts_from(@person).where(:type => "StatusMessage").includes(:comments).limit(15).where(StatusMessage.arel_table[:created_at].lt(max_time)) + @posts = current_user.posts_from(@person).where(:type => ["StatusMessage", "Bookmark"]).includes(:comments).limit(15).where(StatusMessage.arel_table[:created_at].lt(max_time)) else @commenting_disabled = true - @posts = @person.posts.where(:type => "StatusMessage", :public => true).includes(:comments).limit(15).where(StatusMessage.arel_table[:created_at].lt(max_time)).order('posts.created_at DESC') + @posts = @person.posts.where(:type => ["StatusMessage", "Bookmark"], :public => true).includes(:comments).limit(15).where(StatusMessage.arel_table[:created_at].lt(max_time)).order('posts.created_at DESC') end @posts = PostsFake.new(@posts) diff --git a/app/views/shared/_stream_element.html.haml b/app/views/shared/_stream_element.html.haml index ad7e06344..d1a576947 100644 --- a/app/views/shared/_stream_element.html.haml +++ b/app/views/shared/_stream_element.html.haml @@ -22,7 +22,11 @@ .from = person_link(post.author, :class => 'author') %time.time.timeago{:datetime => post.created_at, :integer => time_for_sort(post).to_i} - = render 'status_messages/status_message', :post => post, :photos => post.photos + + - if post.model.instance_of?(Bookmark) + = image_tag post.image_url + - else + = render 'status_messages/status_message', :post => post, :photos => post.photos .info - if post.public? From dd31f37fd04b57895d28177c0f6c60856bc9fd4f Mon Sep 17 00:00:00 2001 From: danielgrippi Date: Wed, 18 May 2011 19:36:52 -0700 Subject: [PATCH 4/6] only generate new token if admin --- app/controllers/users_controller.rb | 1 + spec/models/bookmark_spec.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index fd37f6799..86e8f35c9 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -142,6 +142,7 @@ class UsersController < ApplicationController send_data( File.open(tar_path).read, :filename => "#{current_user.id}.tar" ) end + before_filter :redirect_unless_admin, :only => :generate_new_token def generate_new_token if current_user.reset_authentication_token! @token = current_user.authentication_token diff --git a/spec/models/bookmark_spec.rb b/spec/models/bookmark_spec.rb index 80a288f38..eb938773c 100644 --- a/spec/models/bookmark_spec.rb +++ b/spec/models/bookmark_spec.rb @@ -19,7 +19,7 @@ describe Bookmark do } }, "object"=> { - "url"=>"cubbi.es/daniel", + "url"=>"http://cubbi.es/daniel", "objectType"=>"bookmark" } } From c8e31825c52ba87252ab3197e828dfbb61986d8c Mon Sep 17 00:00:00 2001 From: danielgrippi Date: Thu, 19 May 2011 11:55:17 -0700 Subject: [PATCH 5/6] Bookmark -> ActivitiyStreams::Photo --- .../activity_streams/photos_controller.rb | 26 +++++++++ app/controllers/aspects_controller.rb | 2 +- app/controllers/bookmarks_controller.rb | 56 ------------------- app/controllers/people_controller.rb | 4 +- .../photo.rb} | 19 ++++--- app/views/shared/_stream_element.html.haml | 2 +- app/views/shared/_stream_element.mobile.haml | 5 +- config/routes.rb | 5 +- ...3_add_column_for_activity_streams_photo.rb | 21 +++++++ .../20110518222303_add_column_for_bookmark.rb | 15 ----- db/schema.rb | 14 +++-- spec/models/activity_streams/photo_spec.rb | 28 ++++++++++ spec/models/bookmark_spec.rb | 36 ------------ 13 files changed, 107 insertions(+), 126 deletions(-) create mode 100644 app/controllers/activity_streams/photos_controller.rb delete mode 100644 app/controllers/bookmarks_controller.rb rename app/models/{bookmark.rb => activity_streams/photo.rb} (60%) create mode 100644 db/migrate/20110518222303_add_column_for_activity_streams_photo.rb delete mode 100644 db/migrate/20110518222303_add_column_for_bookmark.rb create mode 100644 spec/models/activity_streams/photo_spec.rb delete mode 100644 spec/models/bookmark_spec.rb diff --git a/app/controllers/activity_streams/photos_controller.rb b/app/controllers/activity_streams/photos_controller.rb new file mode 100644 index 000000000..c17cea042 --- /dev/null +++ b/app/controllers/activity_streams/photos_controller.rb @@ -0,0 +1,26 @@ +# Copyright (c) 2010, Diaspora Inc. This file is +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. + +class ActivityStreams::PhotosController < ApplicationController + before_filter :authenticate_user! + before_filter :redirect_unless_admin + skip_before_filter :verify_authenticity_token + + respond_to :json + + def create + @photo = ActivityStreams::Photo.from_activity(params[:activity]) + @photo.author = current_user.person + @photo.public = true + + if @photo.save + Rails.logger.info("event=create type=activitystreams_photo") + + current_user.add_to_streams(@photo, current_user.aspects) + current_user.dispatch_post(@photo, :url => post_url(@photo)) + + render :nothing => true, :status => 201 + end + end +end diff --git a/app/controllers/aspects_controller.rb b/app/controllers/aspects_controller.rb index a66a4b7de..8b0341f0c 100644 --- a/app/controllers/aspects_controller.rb +++ b/app/controllers/aspects_controller.rb @@ -31,7 +31,7 @@ class AspectsController < ApplicationController @aspect_ids = @aspects.map { |a| a.id } posts = current_user.visible_posts(:by_members_of => @aspect_ids, - :type => ['StatusMessage','Bookmark'], + :type => ['StatusMessage','ActivityStreams::Photo'], :order => session[:sort_order] + ' DESC', :max_time => params[:max_time].to_i ).includes(:comments, :mentions, :likes, :dislikes) diff --git a/app/controllers/bookmarks_controller.rb b/app/controllers/bookmarks_controller.rb deleted file mode 100644 index f3e1826cb..000000000 --- a/app/controllers/bookmarks_controller.rb +++ /dev/null @@ -1,56 +0,0 @@ -# Copyright (c) 2010, Diaspora Inc. This file is -# licensed under the Affero General Public License version 3 or later. See -# the COPYRIGHT file. - -class BookmarksController < ApplicationController - before_filter :authenticate_user! - skip_before_filter :verify_authenticity_token - - respond_to :json - - def create - @bookmark = Bookmark.from_activity(params[:activity]) - @bookmark.author = current_user.person - @bookmark.public = true - - if @bookmark.save - Rails.logger.info("event=create type=bookmark") - - current_user.add_to_streams(@bookmark, current_user.aspects) - current_user.dispatch_post(@bookmark, :url => post_url(@bookmark)) - - render :nothing => true, :status => 201 - end - end - -=begin - def destroy - if @bookmark = current_user.posts.where(:id => params[:id]).first - current_user.retract(@bookmark) - else - Rails.logger.info "event=post_destroy status=failure user=#{current_user.diaspora_handle} reason='User does not own post'" - render :nothing => true, :status => 404 - end - end - - def show - @status_message = current_user.find_visible_post_by_id params[:id] - if @status_message - @object_aspect_ids = @status_message.aspects.map{|a| a.id} - - # mark corresponding notification as read - if notification = Notification.where(:recipient_id => current_user.id, :target_id => @status_message.id).first - notification.unread = false - notification.save - end - - respond_with @status_message - else - Rails.logger.info(:event => :link_to_nonexistent_post, :ref => request.env['HTTP_REFERER'], :user_id => current_user.id, :post_id => params[:id]) - flash[:error] = I18n.t('status_messages.show.not_found') - redirect_to :back - end - end -=end - -end diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb index 78711df0b..45779f3c3 100644 --- a/app/controllers/people_controller.rb +++ b/app/controllers/people_controller.rb @@ -86,10 +86,10 @@ class PeopleController < ApplicationController else @commenting_disabled = false end - @posts = current_user.posts_from(@person).where(:type => ["StatusMessage", "Bookmark"]).includes(:comments).limit(15).where(StatusMessage.arel_table[:created_at].lt(max_time)) + @posts = current_user.posts_from(@person).where(:type => ["StatusMessage", "ActivityStreams::Photo"]).includes(:comments).limit(15).where(StatusMessage.arel_table[:created_at].lt(max_time)) else @commenting_disabled = true - @posts = @person.posts.where(:type => ["StatusMessage", "Bookmark"], :public => true).includes(:comments).limit(15).where(StatusMessage.arel_table[:created_at].lt(max_time)).order('posts.created_at DESC') + @posts = @person.posts.where(:type => ["StatusMessage", "ActivityStreams::Photo"], :public => true).includes(:comments).limit(15).where(StatusMessage.arel_table[:created_at].lt(max_time)).order('posts.created_at DESC') end @posts = PostsFake.new(@posts) diff --git a/app/models/bookmark.rb b/app/models/activity_streams/photo.rb similarity index 60% rename from app/models/bookmark.rb rename to app/models/activity_streams/photo.rb index 0a60524d6..ace74a18c 100644 --- a/app/models/bookmark.rb +++ b/app/models/activity_streams/photo.rb @@ -2,11 +2,13 @@ # licensed under the Affero General Public License version 3 or later. See # the COPYRIGHT file. -class Bookmark < Post +class ActivityStreams::Photo < Post include Diaspora::Socketable - validates_presence_of :target_url - + validates_presence_of :image_url, + :object_url, + :provider_display_name, + :actor_url def socket_to_user(user_or_id, opts={}) #adds aspect_ids to opts if they are not there unless opts[:aspect_ids] @@ -21,10 +23,13 @@ class Bookmark < Post def self.from_activity(json) self.new( - :image_url => json["target"]["image"]["url"], - :image_height => json["target"]["image"]["height"], - :image_width => json["target"]["image"]["width"], - :target_url => json["target"]["url"] + :image_url => json["object"]["image"]["url"], + :image_height => json["object"]["image"]["height"], + :image_width => json["object"]["image"]["width"], + :object_url => json["object"]["url"], + + :provider_display_name => json["provider"]["displayName"], + :actor_url => json["actor"]["url"] ) end end diff --git a/app/views/shared/_stream_element.html.haml b/app/views/shared/_stream_element.html.haml index d1a576947..8fb22f7ba 100644 --- a/app/views/shared/_stream_element.html.haml +++ b/app/views/shared/_stream_element.html.haml @@ -23,7 +23,7 @@ = person_link(post.author, :class => 'author') %time.time.timeago{:datetime => post.created_at, :integer => time_for_sort(post).to_i} - - if post.model.instance_of?(Bookmark) + - if post.model.instance_of?(ActivityStreams::Photo) = image_tag post.image_url - else = render 'status_messages/status_message', :post => post, :photos => post.photos diff --git a/app/views/shared/_stream_element.mobile.haml b/app/views/shared/_stream_element.mobile.haml index 82a342790..d711643cf 100644 --- a/app/views/shared/_stream_element.mobile.haml +++ b/app/views/shared/_stream_element.mobile.haml @@ -9,7 +9,10 @@ .from = person_link(post.author) - = render 'status_messages/status_message', :post => post, :photos => post.photos + - if post.model.instance_of?(ActivityStreams::Photo) + = image_tag post.image_url + - else + = render 'status_messages/status_message', :post => post, :photos => post.photos .info %span.time{:integer => post.created_at.to_i} diff --git a/config/routes.rb b/config/routes.rb index dcd1f9fe6..9951299c9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -63,7 +63,10 @@ Diaspora::Application.routes.draw do # generating a new user token (for devise) match 'users/generate_new_token' => 'users#generate_new_token' - resources :bookmarks, :only => :create + # ActivityStreams routes + scope "/activity_streams", :module => "activity_streams" do + resources :photos, :controller => "photos", :only => :create, :as => "as_photos" + end get 'login' => redirect('/users/sign_in') diff --git a/db/migrate/20110518222303_add_column_for_activity_streams_photo.rb b/db/migrate/20110518222303_add_column_for_activity_streams_photo.rb new file mode 100644 index 000000000..0927da02e --- /dev/null +++ b/db/migrate/20110518222303_add_column_for_activity_streams_photo.rb @@ -0,0 +1,21 @@ +class AddColumnForActivityStreamsPhoto < ActiveRecord::Migration + def self.up + add_column(:posts, :object_url, :string) + add_column(:posts, :image_url, :string) + add_column(:posts, :image_height, :integer) + add_column(:posts, :image_width, :integer) + + add_column(:posts, :provider_display_name, :string) + add_column(:posts, :actor_url, :string) + end + + def self.down + remove_column(:posts, :actor_url) + remove_column(:posts, :provider_display_name) + + remove_column(:posts, :image_width) + remove_column(:posts, :image_height) + remove_column(:posts, :image_url) + remove_column(:posts, :object_url) + end +end diff --git a/db/migrate/20110518222303_add_column_for_bookmark.rb b/db/migrate/20110518222303_add_column_for_bookmark.rb deleted file mode 100644 index 8f8670be7..000000000 --- a/db/migrate/20110518222303_add_column_for_bookmark.rb +++ /dev/null @@ -1,15 +0,0 @@ -class AddColumnForBookmark < ActiveRecord::Migration - def self.up - add_column(:posts, :target_url, :string) - add_column(:posts, :image_url, :string) - add_column(:posts, :image_height, :integer) - add_column(:posts, :image_width, :integer) - end - - def self.down - remove_column(:posts, :image_width) - remove_column(:posts, :image_height) - remove_column(:posts, :image_url) - remove_column(:posts, :target_url) - end -end diff --git a/db/schema.rb b/db/schema.rb index 136864edd..4237ec91d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -225,12 +225,12 @@ ActiveRecord::Schema.define(:version => 20110518222303) do add_index "post_visibilities", ["post_id"], :name => "index_post_visibilities_on_post_id" create_table "posts", :force => true do |t| - t.integer "author_id", :null => false - t.boolean "public", :default => false, :null => false + t.integer "author_id", :null => false + t.boolean "public", :default => false, :null => false t.string "diaspora_handle" - t.string "guid", :null => false - t.boolean "pending", :default => false, :null => false - t.string "type", :limit => 40, :null => false + t.string "guid", :null => false + t.boolean "pending", :default => false, :null => false + t.string "type", :limit => 40, :null => false t.text "text" t.integer "status_message_id" t.text "remote_photo_path" @@ -242,10 +242,12 @@ ActiveRecord::Schema.define(:version => 20110518222303) do t.datetime "updated_at" t.string "mongo_id" t.string "unprocessed_image" - t.string "target_url" + t.string "object_url" t.string "image_url" t.integer "image_height" t.integer "image_width" + t.string "provider_display_name" + t.string "actor_url" end add_index "posts", ["author_id"], :name => "index_posts_on_person_id" diff --git a/spec/models/activity_streams/photo_spec.rb b/spec/models/activity_streams/photo_spec.rb new file mode 100644 index 000000000..4081676ec --- /dev/null +++ b/spec/models/activity_streams/photo_spec.rb @@ -0,0 +1,28 @@ +# Copyright (c) 2010, Diaspora Inc. This file is +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. + +require 'spec_helper' + +describe ActivityStreams::Photo do + describe '.from_activity' do + before do + @json = JSON.parse <"save", - "target"=> { - "url"=>"http://abcnews.go.com/US/wireStory?id=13630884", - "objectType"=>"photo", - "image"=> { - "url"=> "http://a.abcnews.com/images/Entertainment/abc_ann_wtb_blake_leo_110518_wl.jpg", - "height"=>"112", - "width"=>"200" - } - }, - "object"=> { - "url"=>"http://cubbi.es/daniel", - "objectType"=>"bookmark" - } - } - end - it 'marshals into a bookmark' do - bookmark = Bookmark.from_activity(@json) - bookmark.image_url.should == @json["target"]["image"]["url"] - bookmark.image_height.should == @json["target"]["image"]["height"].to_i - bookmark.image_width.should == @json["target"]["image"]["width"].to_i - bookmark.target_url.should == @json["target"]["url"] - end - - end -end From e7095ba788bfbf35757675221b7de6a6ce910ed4 Mon Sep 17 00:00:00 2001 From: danielgrippi Date: Thu, 19 May 2011 12:19:27 -0700 Subject: [PATCH 6/6] Tests now pass, stream_element needs de-hacking --- app/models/activity_streams/photo.rb | 2 ++ app/views/shared/_stream_element.html.haml | 2 +- app/views/shared/_stream_element.mobile.haml | 2 +- spec/controllers/users_controller_spec.rb | 4 ++++ 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/app/models/activity_streams/photo.rb b/app/models/activity_streams/photo.rb index ace74a18c..8fd691656 100644 --- a/app/models/activity_streams/photo.rb +++ b/app/models/activity_streams/photo.rb @@ -32,5 +32,7 @@ class ActivityStreams::Photo < Post :actor_url => json["actor"]["url"] ) end + + def activity_streams?; true; end end diff --git a/app/views/shared/_stream_element.html.haml b/app/views/shared/_stream_element.html.haml index 8fb22f7ba..6f074b1f0 100644 --- a/app/views/shared/_stream_element.html.haml +++ b/app/views/shared/_stream_element.html.haml @@ -23,7 +23,7 @@ = person_link(post.author, :class => 'author') %time.time.timeago{:datetime => post.created_at, :integer => time_for_sort(post).to_i} - - if post.model.instance_of?(ActivityStreams::Photo) + - if post.respond_to?(:activity_streams?) = image_tag post.image_url - else = render 'status_messages/status_message', :post => post, :photos => post.photos diff --git a/app/views/shared/_stream_element.mobile.haml b/app/views/shared/_stream_element.mobile.haml index d711643cf..54f9e6ab7 100644 --- a/app/views/shared/_stream_element.mobile.haml +++ b/app/views/shared/_stream_element.mobile.haml @@ -9,7 +9,7 @@ .from = person_link(post.author) - - if post.model.instance_of?(ActivityStreams::Photo) + - if post.respond_to?(:activity_streams?) = image_tag post.image_url - else = render 'status_messages/status_message', :post => post, :photos => post.photos diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index ade63a1a8..160e438dd 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -140,6 +140,10 @@ describe UsersController do end describe '#generate_new_token' do + before do + AppConfig[:admins] = [@user.username] + end + it 'generates a new token for the current user' do lambda { get 'generate_new_token'