Skip to content
Snippets Groups Projects
Commit ab6696e8 authored by Eugen Rochko's avatar Eugen Rochko
Browse files

Adding doorkeeper, adding a REST API

POST /api/statuses                  Params: status (text contents), in_reply_to_id (optional)
GET  /api/statuses/:id
POST /api/statuses/:id/reblog

GET  /api/accounts/:id
GET  /api/accounts/:id/following
GET  /api/accounts/:id/followers
POST /api/accounts/:id/follow
POST /api/accounts/:id/unfollow

POST /api/follows                  Params: uri (e.g. user@domain)

OAuth authentication is currently disabled, but the API can be used with HTTP Auth.
parent 3824c588
No related branches found
No related tags found
No related merge requests found
Showing
with 124 additions and 34 deletions
...@@ -27,6 +27,9 @@ gem 'ostatus2' ...@@ -27,6 +27,9 @@ gem 'ostatus2'
gem 'goldfinger' gem 'goldfinger'
gem 'devise' gem 'devise'
gem 'rails_autolink' gem 'rails_autolink'
gem 'doorkeeper'
gem 'rabl'
gem 'oj'
group :development, :test do group :development, :test do
gem 'rspec-rails' gem 'rspec-rails'
......
...@@ -74,8 +74,10 @@ GEM ...@@ -74,8 +74,10 @@ GEM
warden (~> 1.2.3) warden (~> 1.2.3)
diff-lcs (1.2.5) diff-lcs (1.2.5)
docile (1.1.5) docile (1.1.5)
domain_name (0.5.20160128) domain_name (0.5.20160216)
unf (>= 0.0.5, < 1.0.0) unf (>= 0.0.5, < 1.0.0)
doorkeeper (3.1.0)
railties (>= 3.2)
dotenv (2.1.0) dotenv (2.1.0)
dotenv-rails (2.1.0) dotenv-rails (2.1.0)
dotenv (= 2.1.0) dotenv (= 2.1.0)
...@@ -90,7 +92,7 @@ GEM ...@@ -90,7 +92,7 @@ GEM
ruby-progressbar (~> 1.4) ruby-progressbar (~> 1.4)
globalid (0.3.6) globalid (0.3.6)
activesupport (>= 4.1.0) activesupport (>= 4.1.0)
goldfinger (1.0.1) goldfinger (1.0.2)
addressable (~> 2.4) addressable (~> 2.4)
http (~> 1.0) http (~> 1.0)
nokogiri (~> 1.6) nokogiri (~> 1.6)
...@@ -139,6 +141,7 @@ GEM ...@@ -139,6 +141,7 @@ GEM
multi_json (1.11.2) multi_json (1.11.2)
nokogiri (1.6.7.2) nokogiri (1.6.7.2)
mini_portile2 (~> 2.0.0.rc2) mini_portile2 (~> 2.0.0.rc2)
oj (2.14.5)
orm_adapter (0.5.0) orm_adapter (0.5.0)
ostatus2 (0.1.1) ostatus2 (0.1.1)
addressable (~> 2.4) addressable (~> 2.4)
...@@ -165,6 +168,8 @@ GEM ...@@ -165,6 +168,8 @@ GEM
puma (2.16.0) puma (2.16.0)
quiet_assets (1.1.0) quiet_assets (1.1.0)
railties (>= 3.1, < 5.0) railties (>= 3.1, < 5.0)
rabl (0.12.0)
activesupport (>= 2.3.14)
rack (1.6.4) rack (1.6.4)
rack-test (0.6.3) rack-test (0.6.3)
rack (>= 1.0) rack (>= 1.0)
...@@ -300,6 +305,7 @@ DEPENDENCIES ...@@ -300,6 +305,7 @@ DEPENDENCIES
binding_of_caller binding_of_caller
coffee-rails (~> 4.1.0) coffee-rails (~> 4.1.0)
devise devise
doorkeeper
dotenv-rails dotenv-rails
fabrication fabrication
font-awesome-sass font-awesome-sass
...@@ -310,6 +316,7 @@ DEPENDENCIES ...@@ -310,6 +316,7 @@ DEPENDENCIES
jbuilder (~> 2.0) jbuilder (~> 2.0)
jquery-rails jquery-rails
nokogiri nokogiri
oj
ostatus2 ostatus2
paperclip (~> 4.3) paperclip (~> 4.3)
paranoia (~> 2.0) paranoia (~> 2.0)
...@@ -317,6 +324,7 @@ DEPENDENCIES ...@@ -317,6 +324,7 @@ DEPENDENCIES
pry-rails pry-rails
puma puma
quiet_assets quiet_assets
rabl
rails (= 4.2.5.1) rails (= 4.2.5.1)
rails_12factor rails_12factor
rails_autolink rails_autolink
......
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
// Place all the styles related to the Api::Accounts controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
// Place all the styles related to the API::Follows controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
// Place all the styles related to the API::Statuses controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
...@@ -3,7 +3,6 @@ class AccountsController < ApplicationController ...@@ -3,7 +3,6 @@ class AccountsController < ApplicationController
before_action :set_account before_action :set_account
before_action :set_webfinger_header before_action :set_webfinger_header
before_action :authenticate_user!, only: [:follow, :unfollow]
def show def show
@statuses = @account.statuses.order('id desc').includes(thread: [:account], reblog: [:account], stream_entry: []) @statuses = @account.statuses.order('id desc').includes(thread: [:account], reblog: [:account], stream_entry: [])
...@@ -14,16 +13,6 @@ class AccountsController < ApplicationController ...@@ -14,16 +13,6 @@ class AccountsController < ApplicationController
end end
end end
def follow
current_user.account.follow!(@account)
redirect_to root_path
end
def unfollow
current_user.account.unfollow!(@account)
redirect_to root_path
end
private private
def set_account def set_account
......
class Api::AccountsController < ApiController
before_action :set_account
before_action :authenticate_user!
respond_to :json
def show
end
def following
@following = @account.following
end
def followers
@followers = @account.followers
end
def statuses
@statuses = @account.statuses
end
def follow
@follow = current_user.account.follow!(@account)
render action: :show
end
def unfollow
@unfollow = current_user.account.unfollow!(@account)
render action: :show
end
private
def set_account
@account = Account.find(params[:id])
end
end
class Api::FollowsController < ApiController
before_action :authenticate_user!
respond_to :json
def create
@follow = FollowService.new.(current_user.account, params[:uri])
render action: :show
end
end
class Api::StatusesController < ApiController
before_action :authenticate_user!
respond_to :json
def show
@status = Status.find(params[:id])
end
def create
@status = PostStatusService.new.(current_user.account, params[:status], params[:in_reply_to_id].blank? ? nil : Status.find(params[:in_reply_to_id]))
render action: :show
end
def reblog
@status = ReblogService.new.(current_user.account, Status.find(params[:id]))
render action: :show
end
end
class ApiController < ApplicationController class ApiController < ApplicationController
protect_from_forgery with: :null_session protect_from_forgery with: :null_session
protected
def current_resource_owner
User.find(doorkeeper_token.user_id) if doorkeeper_token
end
def current_user
super || current_resource_owner
end
end end
...@@ -3,8 +3,6 @@ class StreamEntriesController < ApplicationController ...@@ -3,8 +3,6 @@ class StreamEntriesController < ApplicationController
before_action :set_account before_action :set_account
before_action :set_stream_entry before_action :set_stream_entry
before_action :authenticate_user!, only: [:reblog, :favourite]
before_action :only_statuses!, only: [:reblog, :favourite]
def show def show
@type = @stream_entry.activity_type.downcase @type = @stream_entry.activity_type.downcase
...@@ -15,16 +13,6 @@ class StreamEntriesController < ApplicationController ...@@ -15,16 +13,6 @@ class StreamEntriesController < ApplicationController
end end
end end
def reblog
ReblogService.new.(current_user.account, @stream_entry.activity)
redirect_to root_path
end
def favourite
FavouriteService.new.(current_user.account, @stream_entry.activity)
redirect_to root_path
end
private private
def set_account def set_account
...@@ -34,8 +22,4 @@ class StreamEntriesController < ApplicationController ...@@ -34,8 +22,4 @@ class StreamEntriesController < ApplicationController
def set_stream_entry def set_stream_entry
@stream_entry = @account.stream_entries.find(params[:id]) @stream_entry = @account.stream_entries.find(params[:id])
end end
def only_statuses!
redirect_to root_url unless @stream_entry.activity_type == 'Status'
end
end end
module Api::AccountsHelper
end
module Api::FollowsHelper
end
module Api::StatusesHelper
end
...@@ -31,10 +31,10 @@ module StreamEntriesHelper ...@@ -31,10 +31,10 @@ module StreamEntriesHelper
end end
def reblogged_by_me_class(status) def reblogged_by_me_class(status)
user_signed_in? && (status.reblog? ? status.reblog : status).reblogs.where(account: current_user.account).count == 1 ? 'reblogged' : '' user_signed_in? && current_user.account.reblogged?(status) ? 'reblogged' : ''
end end
def favourited_by_me_class(status) def favourited_by_me_class(status)
user_signed_in? && (status.reblog? ? status.reblog : status).favourites.where(account: current_user.account).count == 1 ? 'favourited' : '' user_signed_in? && current_user.account.favourited?(status) ? 'favourited' : ''
end end
end end
...@@ -24,7 +24,7 @@ class Account < ActiveRecord::Base ...@@ -24,7 +24,7 @@ class Account < ActiveRecord::Base
MENTION_RE = /(?:^|\W)@([a-z0-9_]+(?:@[a-z0-9\.\-]+)?)/i MENTION_RE = /(?:^|\W)@([a-z0-9_]+(?:@[a-z0-9\.\-]+)?)/i
def follow!(other_account) def follow!(other_account)
self.active_relationships.first_or_create!(target_account: other_account) self.active_relationships.where(target_account: other_account).first_or_create!(target_account: other_account)
end end
def unfollow!(other_account) def unfollow!(other_account)
...@@ -59,6 +59,14 @@ class Account < ActiveRecord::Base ...@@ -59,6 +59,14 @@ class Account < ActiveRecord::Base
!(self.secret.blank? || self.verify_token.blank?) !(self.secret.blank? || self.verify_token.blank?)
end end
def favourited?(status)
(status.reblog? ? status.reblog : status).favourites.where(account: self).count == 1
end
def reblogged?(status)
(status.reblog? ? status.reblog : status).reblogs.where(account: self).count == 1
end
def keypair def keypair
self.private_key.nil? ? OpenSSL::PKey::RSA.new(self.public_key) : OpenSSL::PKey::RSA.new(self.private_key) self.private_key.nil? ? OpenSSL::PKey::RSA.new(self.public_key) : OpenSSL::PKey::RSA.new(self.private_key)
end end
......
...@@ -5,11 +5,12 @@ class FollowService < BaseService ...@@ -5,11 +5,12 @@ class FollowService < BaseService
def call(source_account, uri) def call(source_account, uri)
target_account = follow_remote_account_service.(uri) target_account = follow_remote_account_service.(uri)
return if target_account.nil? return nil if target_account.nil?
follow = source_account.follow!(target_account) follow = source_account.follow!(target_account)
send_interaction_service.(follow.stream_entry, target_account) send_interaction_service.(follow.stream_entry, target_account)
source_account.ping!(account_url(account, format: 'atom'), [Rails.configuration.x.hub_url]) source_account.ping!(account_url(source_account, format: 'atom'), [Rails.configuration.x.hub_url])
follow
end end
private private
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment