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 cc5afedcc..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', + :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/people_controller.rb b/app/controllers/people_controller.rb index 7b8b74df1..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").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", :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/controllers/users_controller.rb b/app/controllers/users_controller.rb index cc8d482f3..86e8f35c9 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -141,4 +141,14 @@ class UsersController < ApplicationController tar_path = PhotoMover::move_photos(current_user) 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 + else + @token = "No token created" + end + render :text => @token + end end diff --git a/app/models/activity_streams/photo.rb b/app/models/activity_streams/photo.rb new file mode 100644 index 000000000..8fd691656 --- /dev/null +++ b/app/models/activity_streams/photo.rb @@ -0,0 +1,38 @@ +# 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::Photo < Post + include Diaspora::Socketable + + 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] + 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["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 + + def activity_streams?; true; 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/app/views/shared/_stream_element.html.haml b/app/views/shared/_stream_element.html.haml index ad7e06344..6f074b1f0 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.respond_to?(:activity_streams?) + = image_tag post.image_url + - else + = render 'status_messages/status_message', :post => post, :photos => post.photos .info - if post.public? diff --git a/app/views/shared/_stream_element.mobile.haml b/app/views/shared/_stream_element.mobile.haml index 82a342790..54f9e6ab7 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.respond_to?(:activity_streams?) + = 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/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..9951299c9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -59,6 +59,16 @@ 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' + + # 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') 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/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/schema.rb b/db/schema.rb index b11a750c6..4237ec91d 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 => 20110518222303) do create_table "aspect_memberships", :force => true do |t| t.integer "aspect_id", :null => false @@ -225,12 +225,12 @@ ActiveRecord::Schema.define(:version => 20110518010050) 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,6 +242,12 @@ ActiveRecord::Schema.define(:version => 20110518010050) do t.datetime "updated_at" t.string "mongo_id" t.string "unprocessed_image" + 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" @@ -361,8 +367,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..160e438dd 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -138,4 +138,22 @@ describe UsersController do assigns[:email_prefs]['mentioned'].should be_false end end -end \ No newline at end of file + + 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' + }.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 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 <