From 7e14eefc819ce27f45406452a4f8e2039d0f0886 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Mon, 26 Sep 2016 23:55:21 +0200 Subject: Replace logo, fix #57 - delete/unreblog/unfavourite API, fix #45 - app registration API --- app/assets/images/logo.png | Bin 0 -> 24218 bytes .../components/actions/interactions.jsx | 20 +++++ app/assets/stylesheets/application.scss | 82 +++++---------------- app/controllers/api/apps_controller.rb | 13 ++++ app/controllers/api/statuses_controller.rb | 17 +++++ app/controllers/api_controller.rb | 4 + app/controllers/auth/sessions_controller.rb | 6 ++ app/controllers/home_controller.rb | 2 +- app/controllers/oauth/applications_controller.rb | 18 ----- app/helpers/api/apps_helper.rb | 2 + app/helpers/oauth/applications_helper.rb | 2 - app/services/unfavourite_service.rb | 12 +++ app/views/api/apps/create.rabl | 4 + app/views/application/_logo.html.erb | 11 --- app/views/layouts/auth.html.haml | 2 +- config/application.rb | 6 -- config/initializers/doorkeeper.rb | 2 +- config/routes.rb | 13 ++-- ...20160926213048_remove_owner_from_application.rb | 7 ++ db/schema.rb | 5 +- public/favicon.ico | Bin 2319 -> 927 bytes spec/controllers/api/apps_controller_spec.rb | 26 +++++++ spec/controllers/api/statuses_controller_spec.rb | 58 +++++++++++++++ spec/controllers/auth/sessions_controller_spec.rb | 2 +- .../oauth/applications_controller_spec.rb | 18 ----- spec/helpers/api/apps_helper_spec.rb | 15 ++++ spec/helpers/oauth/applications_helper_spec.rb | 5 -- 27 files changed, 216 insertions(+), 136 deletions(-) create mode 100644 app/assets/images/logo.png create mode 100644 app/controllers/api/apps_controller.rb delete mode 100644 app/controllers/oauth/applications_controller.rb create mode 100644 app/helpers/api/apps_helper.rb delete mode 100644 app/helpers/oauth/applications_helper.rb create mode 100644 app/services/unfavourite_service.rb create mode 100644 app/views/api/apps/create.rabl delete mode 100644 app/views/application/_logo.html.erb create mode 100644 db/migrate/20160926213048_remove_owner_from_application.rb create mode 100644 spec/controllers/api/apps_controller_spec.rb delete mode 100644 spec/controllers/oauth/applications_controller_spec.rb create mode 100644 spec/helpers/api/apps_helper_spec.rb delete mode 100644 spec/helpers/oauth/applications_helper_spec.rb diff --git a/app/assets/images/logo.png b/app/assets/images/logo.png new file mode 100644 index 000000000..3a5a743c0 Binary files /dev/null and b/app/assets/images/logo.png differ diff --git a/app/assets/javascripts/components/actions/interactions.jsx b/app/assets/javascripts/components/actions/interactions.jsx index f2c615b00..3b3e2a6b6 100644 --- a/app/assets/javascripts/components/actions/interactions.jsx +++ b/app/assets/javascripts/components/actions/interactions.jsx @@ -22,6 +22,16 @@ export function reblog(status) { }; }; +export function unreblog(status) { + return (dispatch, getState) => { + api(getState).post(`/api/statuses/${status.get('id')}/unreblog`).then(response => { + // + }).catch(error => { + // + }); + }; +}; + export function reblogRequest(status) { return { type: REBLOG_REQUEST, @@ -57,6 +67,16 @@ export function favourite(status) { }; }; +export function unfavourite(status) { + return (dispatch, getState) => { + api(getState).post(`/api/statuses/${status.get('id')}/unfavourite`).then(response => { + // + }).catch(error => { + // + }); + }; +}; + export function favouriteRequest(status) { return { type: FAVOURITE_REQUEST, diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index fd0e257a7..84f15eaca 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -59,7 +59,7 @@ table { body { font-family: 'Roboto', sans-serif; - background: #282c37 image-url('background-photo.jpeg'); + background: #282c37 image-url('background-photo.jpg'); background-size: cover; background-attachment: fixed; font-size: 13px; @@ -104,10 +104,29 @@ body { font-size: 48px; font-weight: 500; + img { + display: block; + margin: 20px auto; + width: 180px; + height: 180px; + } + a { color: inherit; text-decoration: none; outline: 0; + + img { + opacity: 0.8; + transition: all 0.8s ease; + } + + &:hover { + img { + opacity: 1; + transition-duration: 0.2s; + } + } } small { @@ -313,67 +332,6 @@ body { } } -.turbolinks-progress-bar { - background-color: #2b90d9; -} - -.mastodon { - $head: #282c37; - $tusk: #d9e1e8; - $backdrop: #2b90d9; - $highlight: 18%; - - .mastodon-backdrop { - fill: $backdrop; - } - - #mastodon-ear { - fill: lighten($head, 5%); - - &:hover, &.highlight { - fill: lighten($head, $highlight); - } - } - - #mastodon-head-backdrop { - fill: darken($head, 5%); - - &:hover, &.highlight { - fill: darken($head, 5%); - } - } - - #mastodon-nose, #mastodon-cheek, #mastodon-forehead, #mastodon-backhead { - fill: $head; - - &:hover, &.highlight { - fill: lighten($head, $highlight); - } - } - - #mastodon-tusk-front { - fill: lighten($tusk, 5%); - } - - #mastodon-tusk-back { - fill: $tusk; - - &:hover, &.highlight { - fill: lighten($tusk, $highlight); - } - } - - .mastodon-shape { - transition: all 0.8s ease; - stroke: transparent; - stroke-width: 0px; - - &:hover, &.highlight { - transition-duration: 0.2s; - } - } -} - @import 'accounts'; @import 'stream_entries'; @import 'components' diff --git a/app/controllers/api/apps_controller.rb b/app/controllers/api/apps_controller.rb new file mode 100644 index 000000000..629cb2416 --- /dev/null +++ b/app/controllers/api/apps_controller.rb @@ -0,0 +1,13 @@ +class Api::AppsController < ApplicationController + respond_to :json + + def create + @app = Doorkeeper::Application.create!(app_params) + end + + private + + def app_params + params.permit(:name, :redirect_uri) + end +end diff --git a/app/controllers/api/statuses_controller.rb b/app/controllers/api/statuses_controller.rb index f68f298d8..44fb40bae 100644 --- a/app/controllers/api/statuses_controller.rb +++ b/app/controllers/api/statuses_controller.rb @@ -17,16 +17,33 @@ class Api::StatusesController < ApiController render action: :show end + def destroy + @status = Status.where(account_id: current_user.account).find(params[:id]) + RemoveStatusService.new.(@status) + render_empty + end + def reblog @status = ReblogService.new.(current_user.account, Status.find(params[:id])).reload render action: :show end + def unreblog + RemoveStatusService.new.(Status.where(account_id: current_user.account, reblog_of_id: params[:id]).first!) + @status = Status.find(params[:id]) + render action: :show + end + def favourite @status = FavouriteService.new.(current_user.account, Status.find(params[:id])).status.reload render action: :show end + def unfavourite + @status = UnfavouriteService.new.(current_user.account, Status.find(params[:id])).status.reload + render action: :show + end + def home @statuses = Feed.new(:home, current_user.account).get(20, params[:max_id]).to_a end diff --git a/app/controllers/api_controller.rb b/app/controllers/api_controller.rb index cbe7141b9..e3ac9fc67 100644 --- a/app/controllers/api_controller.rb +++ b/app/controllers/api_controller.rb @@ -27,4 +27,8 @@ class ApiController < ApplicationController def current_user super || current_resource_owner end + + def render_empty + render json: {}, status: 200 + end end diff --git a/app/controllers/auth/sessions_controller.rb b/app/controllers/auth/sessions_controller.rb index fe0adc9bb..1418ab2ca 100644 --- a/app/controllers/auth/sessions_controller.rb +++ b/app/controllers/auth/sessions_controller.rb @@ -8,4 +8,10 @@ class Auth::SessionsController < Devise::SessionsController remember_me(resource) end end + + protected + + def after_sign_in_path_for(_resource) + root_path + end end diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index f159c3df8..6b52b704d 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -7,7 +7,7 @@ class HomeController < ApplicationController @mentions = Feed.new(:mentions, current_user.account).get(20) @token = find_or_create_access_token.token end - + private def find_or_create_access_token diff --git a/app/controllers/oauth/applications_controller.rb b/app/controllers/oauth/applications_controller.rb deleted file mode 100644 index 47935bf7c..000000000 --- a/app/controllers/oauth/applications_controller.rb +++ /dev/null @@ -1,18 +0,0 @@ -class Oauth::ApplicationsController < Doorkeeper::ApplicationsController - before_action :authenticate_user! - - def index - @applications = current_user.oauth_applications - end - - def create - @application = Doorkeeper::Application.new(application_params) - @application.owner = current_user - - if @application.save - redirect_to oauth_application_url(@application) - else - render :new - end - end -end diff --git a/app/helpers/api/apps_helper.rb b/app/helpers/api/apps_helper.rb new file mode 100644 index 000000000..f6b0c6635 --- /dev/null +++ b/app/helpers/api/apps_helper.rb @@ -0,0 +1,2 @@ +module Api::AppsHelper +end diff --git a/app/helpers/oauth/applications_helper.rb b/app/helpers/oauth/applications_helper.rb deleted file mode 100644 index 2c1818055..000000000 --- a/app/helpers/oauth/applications_helper.rb +++ /dev/null @@ -1,2 +0,0 @@ -module Oauth::ApplicationsHelper -end diff --git a/app/services/unfavourite_service.rb b/app/services/unfavourite_service.rb new file mode 100644 index 000000000..2491c194f --- /dev/null +++ b/app/services/unfavourite_service.rb @@ -0,0 +1,12 @@ +class UnfavouriteService < BaseService + def call(account, status) + favourite = Favourite.find_by!(account: account, status: status) + favourite.destroy! + + unless status.local? + NotificationWorker.perform_async(favourite.stream_entry.id, status.account_id) + end + + favourite + end +end diff --git a/app/views/api/apps/create.rabl b/app/views/api/apps/create.rabl new file mode 100644 index 000000000..1ff6469a4 --- /dev/null +++ b/app/views/api/apps/create.rabl @@ -0,0 +1,4 @@ +object @app +attributes :id, :redirect_uri +node(:client_id) { |app| app.uid } +node(:client_secret) { |app| app.secret } diff --git a/app/views/application/_logo.html.erb b/app/views/application/_logo.html.erb deleted file mode 100644 index 21fa4f657..000000000 --- a/app/views/application/_logo.html.erb +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - diff --git a/app/views/layouts/auth.html.haml b/app/views/layouts/auth.html.haml index bddba570a..54aa07e2c 100644 --- a/app/views/layouts/auth.html.haml +++ b/app/views/layouts/auth.html.haml @@ -3,7 +3,7 @@ .logo-container %h1 = link_to root_path do - = render partial: 'application/logo', locals: { dim: 200 } + = image_tag 'logo.png' %small= Rails.configuration.x.local_domain .form-container diff --git a/config/application.rb b/config/application.rb index 81205de32..f6a5f6f9e 100644 --- a/config/application.rb +++ b/config/application.rb @@ -27,12 +27,6 @@ module Mastodon config.active_job.queue_adapter = :sidekiq - config.to_prepare do - # Doorkeeper::ApplicationsController.layout 'dashboard' - # Doorkeeper::AuthorizedApplicationsController.layout 'dashboard' - Doorkeeper::AuthorizationsController.layout 'auth' - end - config.middleware.use Rack::Attack config.middleware.use Rack::Deflater diff --git a/config/initializers/doorkeeper.rb b/config/initializers/doorkeeper.rb index 0d6574d9f..2c02d2b54 100644 --- a/config/initializers/doorkeeper.rb +++ b/config/initializers/doorkeeper.rb @@ -45,7 +45,7 @@ Doorkeeper.configure do # Optional parameter :confirmation => true (default false) if you want to enforce ownership of # a registered application # Note: you must also run the rails g doorkeeper:application_owner generator to provide the necessary support - enable_application_owner :confirmation => true + # enable_application_owner :confirmation => true # Define access token scopes for your provider # For more information go to diff --git a/config/routes.rb b/config/routes.rb index 51f1f86da..737dbbecf 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,9 +7,7 @@ Rails.application.routes.draw do mount Sidekiq::Web => '/sidekiq' end - use_doorkeeper do - controllers applications: 'oauth/applications' - end + use_doorkeeper get '.well-known/host-meta', to: 'xrd#host_meta', as: :host_meta get '.well-known/webfinger', to: 'xrd#webfinger', as: :webfinger @@ -31,7 +29,7 @@ Rails.application.routes.draw do resource :settings, only: [:show, :update] resources :media, only: [:show] - + namespace :api do # PubSubHubbub resources :subscriptions, only: [:show] @@ -41,7 +39,7 @@ Rails.application.routes.draw do post '/salmon/:id', to: 'salmon#update', as: :salmon # JSON / REST API - resources :statuses, only: [:create, :show] do + resources :statuses, only: [:create, :show, :destroy] do collection do get :home get :mentions @@ -51,13 +49,16 @@ Rails.application.routes.draw do get :context post :reblog + post :unreblog post :favourite + post :unfavourite end end resources :follows, only: [:create] resources :media, only: [:create] - + resources :apps, only: [:create] + resources :accounts, only: [:show] do collection do get :relationships diff --git a/db/migrate/20160926213048_remove_owner_from_application.rb b/db/migrate/20160926213048_remove_owner_from_application.rb new file mode 100644 index 000000000..a42f53a29 --- /dev/null +++ b/db/migrate/20160926213048_remove_owner_from_application.rb @@ -0,0 +1,7 @@ +class RemoveOwnerFromApplication < ActiveRecord::Migration[5.0] + def change + remove_index :oauth_applications, [:owner_id, :owner_type] + remove_column :oauth_applications, :owner_id, :integer, null: true + remove_column :oauth_applications, :owner_type, :string, null: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 712776215..f0d7cf5d3 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160920003904) do +ActiveRecord::Schema.define(version: 20160926213048) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -114,10 +114,7 @@ ActiveRecord::Schema.define(version: 20160920003904) do t.string "scopes", default: "", null: false t.datetime "created_at" t.datetime "updated_at" - t.integer "owner_id" - t.string "owner_type" t.boolean "superapp", default: false, null: false - t.index ["owner_id", "owner_type"], name: "index_oauth_applications_on_owner_id_and_owner_type", using: :btree t.index ["uid"], name: "index_oauth_applications_on_uid", unique: true, using: :btree end diff --git a/public/favicon.ico b/public/favicon.ico index 84a0b854c..3673ac0d5 100644 Binary files a/public/favicon.ico and b/public/favicon.ico differ diff --git a/spec/controllers/api/apps_controller_spec.rb b/spec/controllers/api/apps_controller_spec.rb new file mode 100644 index 000000000..ebadddde2 --- /dev/null +++ b/spec/controllers/api/apps_controller_spec.rb @@ -0,0 +1,26 @@ +require 'rails_helper' + +RSpec.describe Api::AppsController, type: :controller do + render_views + + describe 'POST #create' do + before do + post :create, params: { name: 'Test app', redirect_uri: 'urn:ietf:wg:oauth:2.0:oob' } + end + + it 'returns http success' do + expect(response).to have_http_status(:success) + end + + it 'creates an OAuth app' do + expect(Doorkeeper::Application.find_by(name: 'Test app')).to_not be nil + end + + it 'returns client ID and client secret' do + json = body_as_json + + expect(json[:client_id]).to_not be_blank + expect(json[:client_secret]).to_not be_blank + end + end +end diff --git a/spec/controllers/api/statuses_controller_spec.rb b/spec/controllers/api/statuses_controller_spec.rb index d02cc9b29..b1f0a7bcc 100644 --- a/spec/controllers/api/statuses_controller_spec.rb +++ b/spec/controllers/api/statuses_controller_spec.rb @@ -57,6 +57,22 @@ RSpec.describe Api::StatusesController, type: :controller do end end + describe 'DELETE #destroy' do + let(:status) { Fabricate(:status, account: user.account) } + + before do + post :destroy, params: { id: status.id } + end + + it 'returns http success' do + expect(response).to have_http_status(:success) + end + + it 'removes the status' do + expect(Status.find_by(id: status.id)).to be nil + end + end + describe 'POST #reblog' do let(:status) { Fabricate(:status, account: user.account) } @@ -85,6 +101,27 @@ RSpec.describe Api::StatusesController, type: :controller do end end + describe 'POST #unreblog' do + let(:status) { Fabricate(:status, account: user.account) } + + before do + post :reblog, params: { id: status.id } + post :unreblog, params: { id: status.id } + end + + it 'returns http success' do + expect(response).to have_http_status(:success) + end + + it 'updates the reblogs count' do + expect(status.reblogs_count).to eq 0 + end + + it 'updates the reblogged attribute' do + expect(user.account.reblogged?(status)).to be false + end + end + describe 'POST #favourite' do let(:status) { Fabricate(:status, account: user.account) } @@ -112,4 +149,25 @@ RSpec.describe Api::StatusesController, type: :controller do expect(hash_body[:favourited]).to be true end end + + describe 'POST #unfavourite' do + let(:status) { Fabricate(:status, account: user.account) } + + before do + post :favourite, params: { id: status.id } + post :unfavourite, params: { id: status.id } + end + + it 'returns http success' do + expect(response).to have_http_status(:success) + end + + it 'updates the favourites count' do + expect(status.favourites_count).to eq 0 + end + + it 'updates the favourited attribute' do + expect(user.account.favourited?(status)).to be false + end + end end diff --git a/spec/controllers/auth/sessions_controller_spec.rb b/spec/controllers/auth/sessions_controller_spec.rb index 48c9b34a1..9dbbb5d45 100644 --- a/spec/controllers/auth/sessions_controller_spec.rb +++ b/spec/controllers/auth/sessions_controller_spec.rb @@ -20,7 +20,7 @@ RSpec.describe Auth::SessionsController, type: :controller do post :create, params: { user: { email: user.email, password: user.password } } end - it 'redirects to home page' do + it 'redirects to home' do expect(response).to redirect_to(root_path) end diff --git a/spec/controllers/oauth/applications_controller_spec.rb b/spec/controllers/oauth/applications_controller_spec.rb deleted file mode 100644 index ecd8b16c6..000000000 --- a/spec/controllers/oauth/applications_controller_spec.rb +++ /dev/null @@ -1,18 +0,0 @@ -require 'rails_helper' - -RSpec.describe Oauth::ApplicationsController, type: :controller do - before do - sign_in Fabricate(:user), scope: :user - end - - describe 'GET #index' do - it 'returns http success' do - get :index - expect(response).to have_http_status(:success) - end - end - - describe 'POST #create' do - it 'redirects to the application page' - end -end diff --git a/spec/helpers/api/apps_helper_spec.rb b/spec/helpers/api/apps_helper_spec.rb new file mode 100644 index 000000000..e26bca2cc --- /dev/null +++ b/spec/helpers/api/apps_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the Api::AppsHelper. For example: +# +# describe Api::AppsHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe Api::AppsHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/helpers/oauth/applications_helper_spec.rb b/spec/helpers/oauth/applications_helper_spec.rb deleted file mode 100644 index 749e739db..000000000 --- a/spec/helpers/oauth/applications_helper_spec.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'rails_helper' - -RSpec.describe Oauth::ApplicationsHelper, type: :helper do - -end -- cgit