From 9b941e6520b3364879a4155b8fa7ffa972141373 Mon Sep 17 00:00:00 2001 From: danielgrippi Date: Wed, 18 May 2011 16:57:23 -0700 Subject: [PATCH] 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