Skip to content
Extraits de code Groupes Projets
Valider ab6696e8 rédigé par Eugen Rochko's avatar Eugen Rochko
Parcourir les fichiers

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
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Affichage de
avec 124 ajouts et 34 suppressions
......@@ -27,6 +27,9 @@ gem 'ostatus2'
gem 'goldfinger'
gem 'devise'
gem 'rails_autolink'
gem 'doorkeeper'
gem 'rabl'
gem 'oj'
group :development, :test do
gem 'rspec-rails'
......
......@@ -74,8 +74,10 @@ GEM
warden (~> 1.2.3)
diff-lcs (1.2.5)
docile (1.1.5)
domain_name (0.5.20160128)
domain_name (0.5.20160216)
unf (>= 0.0.5, < 1.0.0)
doorkeeper (3.1.0)
railties (>= 3.2)
dotenv (2.1.0)
dotenv-rails (2.1.0)
dotenv (= 2.1.0)
......@@ -90,7 +92,7 @@ GEM
ruby-progressbar (~> 1.4)
globalid (0.3.6)
activesupport (>= 4.1.0)
goldfinger (1.0.1)
goldfinger (1.0.2)
addressable (~> 2.4)
http (~> 1.0)
nokogiri (~> 1.6)
......@@ -139,6 +141,7 @@ GEM
multi_json (1.11.2)
nokogiri (1.6.7.2)
mini_portile2 (~> 2.0.0.rc2)
oj (2.14.5)
orm_adapter (0.5.0)
ostatus2 (0.1.1)
addressable (~> 2.4)
......@@ -165,6 +168,8 @@ GEM
puma (2.16.0)
quiet_assets (1.1.0)
railties (>= 3.1, < 5.0)
rabl (0.12.0)
activesupport (>= 2.3.14)
rack (1.6.4)
rack-test (0.6.3)
rack (>= 1.0)
......@@ -300,6 +305,7 @@ DEPENDENCIES
binding_of_caller
coffee-rails (~> 4.1.0)
devise
doorkeeper
dotenv-rails
fabrication
font-awesome-sass
......@@ -310,6 +316,7 @@ DEPENDENCIES
jbuilder (~> 2.0)
jquery-rails
nokogiri
oj
ostatus2
paperclip (~> 4.3)
paranoia (~> 2.0)
......@@ -317,6 +324,7 @@ DEPENDENCIES
pry-rails
puma
quiet_assets
rabl
rails (= 4.2.5.1)
rails_12factor
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
before_action :set_account
before_action :set_webfinger_header
before_action :authenticate_user!, only: [:follow, :unfollow]
def show
@statuses = @account.statuses.order('id desc').includes(thread: [:account], reblog: [:account], stream_entry: [])
......@@ -14,16 +13,6 @@ class AccountsController < ApplicationController
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
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
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
......@@ -3,8 +3,6 @@ class StreamEntriesController < ApplicationController
before_action :set_account
before_action :set_stream_entry
before_action :authenticate_user!, only: [:reblog, :favourite]
before_action :only_statuses!, only: [:reblog, :favourite]
def show
@type = @stream_entry.activity_type.downcase
......@@ -15,16 +13,6 @@ class StreamEntriesController < ApplicationController
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
def set_account
......@@ -34,8 +22,4 @@ class StreamEntriesController < ApplicationController
def set_stream_entry
@stream_entry = @account.stream_entries.find(params[:id])
end
def only_statuses!
redirect_to root_url unless @stream_entry.activity_type == 'Status'
end
end
module Api::AccountsHelper
end
module Api::FollowsHelper
end
module Api::StatusesHelper
end
......@@ -31,10 +31,10 @@ module StreamEntriesHelper
end
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
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
......@@ -24,7 +24,7 @@ class Account < ActiveRecord::Base
MENTION_RE = /(?:^|\W)@([a-z0-9_]+(?:@[a-z0-9\.\-]+)?)/i
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
def unfollow!(other_account)
......@@ -59,6 +59,14 @@ class Account < ActiveRecord::Base
!(self.secret.blank? || self.verify_token.blank?)
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
self.private_key.nil? ? OpenSSL::PKey::RSA.new(self.public_key) : OpenSSL::PKey::RSA.new(self.private_key)
end
......
......@@ -5,11 +5,12 @@ class FollowService < BaseService
def call(source_account, 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)
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
private
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter