From 2f4c5f504fa5edc2a826afa9645371f529ae904d Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Sat, 9 Dec 2017 01:32:29 +0100 Subject: Limit users to 50 lists, remove pagination from lists API (#5933) --- app/controllers/api/v1/lists_controller.rb | 38 +----------------------------- 1 file changed, 1 insertion(+), 37 deletions(-) (limited to 'app/controllers/api') diff --git a/app/controllers/api/v1/lists_controller.rb b/app/controllers/api/v1/lists_controller.rb index 9437373bd..180a91d81 100644 --- a/app/controllers/api/v1/lists_controller.rb +++ b/app/controllers/api/v1/lists_controller.rb @@ -1,18 +1,14 @@ # frozen_string_literal: true class Api::V1::ListsController < Api::BaseController - LISTS_LIMIT = 50 - before_action -> { doorkeeper_authorize! :read }, only: [:index, :show] before_action -> { doorkeeper_authorize! :write }, except: [:index, :show] before_action :require_user! before_action :set_list, except: [:index, :create] - after_action :insert_pagination_headers, only: :index - def index - @lists = List.where(account: current_account).paginate_by_max_id(limit_param(LISTS_LIMIT), params[:max_id], params[:since_id]) + @lists = List.where(account: current_account).all render json: @lists, each_serializer: REST::ListSerializer end @@ -44,36 +40,4 @@ class Api::V1::ListsController < Api::BaseController def list_params params.permit(:title) end - - def insert_pagination_headers - set_pagination_headers(next_path, prev_path) - end - - def next_path - if records_continue? - api_v1_lists_url pagination_params(max_id: pagination_max_id) - end - end - - def prev_path - unless @lists.empty? - api_v1_lists_url pagination_params(since_id: pagination_since_id) - end - end - - def pagination_max_id - @lists.last.id - end - - def pagination_since_id - @lists.first.id - end - - def records_continue? - @lists.size == limit_param(LISTS_LIMIT) - end - - def pagination_params(core_params) - params.permit(:limit).merge(core_params) - end end -- cgit From 99242b92bcc1965b2a72c01216bce7c232322f15 Mon Sep 17 00:00:00 2001 From: abcang Date: Sat, 9 Dec 2017 10:31:37 +0900 Subject: Keep WebPush settings (#5879) --- .../api/web/push_subscriptions_controller.rb | 2 + .../mastodon/actions/push_notifications.js | 11 +++-- app/javascript/mastodon/web_push_subscription.js | 51 ++++++++++++++++++++-- .../api/web/push_subscriptions_controller_spec.rb | 17 ++++++++ 4 files changed, 74 insertions(+), 7 deletions(-) (limited to 'app/controllers/api') diff --git a/app/controllers/api/web/push_subscriptions_controller.rb b/app/controllers/api/web/push_subscriptions_controller.rb index d66237feb..52e250d02 100644 --- a/app/controllers/api/web/push_subscriptions_controller.rb +++ b/app/controllers/api/web/push_subscriptions_controller.rb @@ -28,6 +28,8 @@ class Api::Web::PushSubscriptionsController < Api::BaseController }, } + data.deep_merge!(params[:data]) if params[:data] + web_subscription = ::Web::PushSubscription.create!( endpoint: params[:subscription][:endpoint], key_p256dh: params[:subscription][:keys][:p256dh], diff --git a/app/javascript/mastodon/actions/push_notifications.js b/app/javascript/mastodon/actions/push_notifications.js index 55661d2b0..cfe419888 100644 --- a/app/javascript/mastodon/actions/push_notifications.js +++ b/app/javascript/mastodon/actions/push_notifications.js @@ -1,4 +1,5 @@ import axios from 'axios'; +import { setSettingsToLocalStorage } from '../web_push_subscription'; export const SET_BROWSER_SUPPORT = 'PUSH_NOTIFICATIONS_SET_BROWSER_SUPPORT'; export const SET_SUBSCRIPTION = 'PUSH_NOTIFICATIONS_SET_SUBSCRIPTION'; @@ -42,11 +43,15 @@ export function saveSettings() { const state = getState().get('push_notifications'); const subscription = state.get('subscription'); const alerts = state.get('alerts'); + const data = { alerts }; axios.put(`/api/web/push_subscriptions/${subscription.get('id')}`, { - data: { - alerts, - }, + data, + }).then(() => { + const me = getState().getIn(['meta', 'me']); + if (me) { + setSettingsToLocalStorage(me, data); + } }); }; } diff --git a/app/javascript/mastodon/web_push_subscription.js b/app/javascript/mastodon/web_push_subscription.js index 3dbed09ea..114d9c3b3 100644 --- a/app/javascript/mastodon/web_push_subscription.js +++ b/app/javascript/mastodon/web_push_subscription.js @@ -35,16 +35,35 @@ const subscribe = (registration) => const unsubscribe = ({ registration, subscription }) => subscription ? subscription.unsubscribe().then(() => registration) : registration; -const sendSubscriptionToBackend = (subscription) => - axios.post('/api/web/push_subscriptions', { - subscription, - }).then(response => response.data); +const sendSubscriptionToBackend = (subscription) => { + const params = { subscription }; + + const me = store.getState().getIn(['meta', 'me']); + if (me) { + const data = getSettingsFromLocalStorage(me); + if (data) { + params.data = data; + } + } + + return axios.post('/api/web/push_subscriptions', params).then(response => response.data); +}; // Last one checks for payload support: https://web-push-book.gauntface.com/chapter-06/01-non-standards-browsers/#no-payload const supportsPushNotifications = ('serviceWorker' in navigator && 'PushManager' in window && 'getKey' in PushSubscription.prototype); +const SUBSCRIPTION_DATA_STORAGE_KEY = 'mastodon_push_notification_data'; + export function register () { store.dispatch(setBrowserSupport(supportsPushNotifications)); + const me = store.getState().getIn(['meta', 'me']); + + if (me && !getSettingsFromLocalStorage(me)) { + const alerts = store.getState().getIn(['push_notifications', 'alerts']); + if (alerts) { + setSettingsToLocalStorage(me, { alerts: alerts }); + } + } if (supportsPushNotifications) { if (!getApplicationServerKey()) { @@ -79,6 +98,9 @@ export function register () { // it means that the backend subscription is valid (and was set during hydration) if (!(subscription instanceof PushSubscription)) { store.dispatch(setSubscription(subscription)); + if (me) { + setSettingsToLocalStorage(me, { alerts: subscription.alerts }); + } } }) .catch(error => { @@ -90,6 +112,9 @@ export function register () { // Clear alerts and hide UI settings store.dispatch(clearSubscription()); + if (me) { + removeSettingsFromLocalStorage(me); + } try { getRegistration() @@ -103,3 +128,21 @@ export function register () { console.warn('Your browser does not support Web Push Notifications.'); } } + +export function setSettingsToLocalStorage(id, data) { + try { + localStorage.setItem(`${SUBSCRIPTION_DATA_STORAGE_KEY}_${id}`, JSON.stringify(data)); + } catch (e) {} +} + +export function getSettingsFromLocalStorage(id) { + try { + return JSON.parse(localStorage.getItem(`${SUBSCRIPTION_DATA_STORAGE_KEY}_${id}`)); + } catch (e) {} + + return null; +} + +export function removeSettingsFromLocalStorage(id) { + localStorage.removeItem(`${SUBSCRIPTION_DATA_STORAGE_KEY}_${id}`); +} diff --git a/spec/controllers/api/web/push_subscriptions_controller_spec.rb b/spec/controllers/api/web/push_subscriptions_controller_spec.rb index 7e83b801d..bbf94c5c6 100644 --- a/spec/controllers/api/web/push_subscriptions_controller_spec.rb +++ b/spec/controllers/api/web/push_subscriptions_controller_spec.rb @@ -48,6 +48,23 @@ describe Api::Web::PushSubscriptionsController do expect(push_subscription['key_p256dh']).to eq(create_payload[:subscription][:keys][:p256dh]) expect(push_subscription['key_auth']).to eq(create_payload[:subscription][:keys][:auth]) end + + context 'with initial data' do + it 'saves alert settings' do + sign_in(user) + + stub_request(:post, create_payload[:subscription][:endpoint]).to_return(status: 200) + + post :create, format: :json, params: create_payload.merge(alerts_payload) + + push_subscription = Web::PushSubscription.find_by(endpoint: create_payload[:subscription][:endpoint]) + + expect(push_subscription.data['follow']).to eq(alerts_payload[:data][:follow]) + expect(push_subscription.data['favourite']).to eq(alerts_payload[:data][:favourite]) + expect(push_subscription.data['reblog']).to eq(alerts_payload[:data][:reblog]) + expect(push_subscription.data['mention']).to eq(alerts_payload[:data][:mention]) + end + end end describe 'PUT #update' do -- cgit From fcc0795a4013db355c629da071d909ceda1f37b1 Mon Sep 17 00:00:00 2001 From: abcang Date: Sun, 10 Dec 2017 07:37:31 +0900 Subject: Remove unused function (#5950) --- app/controllers/api/base_controller.rb | 15 --------------- 1 file changed, 15 deletions(-) (limited to 'app/controllers/api') diff --git a/app/controllers/api/base_controller.rb b/app/controllers/api/base_controller.rb index 7cfe8fe71..5983c0fbe 100644 --- a/app/controllers/api/base_controller.rb +++ b/app/controllers/api/base_controller.rb @@ -72,19 +72,4 @@ class Api::BaseController < ApplicationController def render_empty render json: {}, status: 200 end - - def set_maps(statuses) # rubocop:disable Style/AccessorMethodName - if current_account.nil? - @reblogs_map = {} - @favourites_map = {} - @mutes_map = {} - return - end - - status_ids = statuses.compact.flat_map { |s| [s.id, s.reblog_of_id] }.uniq - conversation_ids = statuses.compact.map(&:conversation_id).compact.uniq - @reblogs_map = Status.reblogs_map(status_ids, current_account) - @favourites_map = Status.favourites_map(status_ids, current_account) - @mutes_map = Status.mutes_map(conversation_ids, current_account) - end end -- cgit From 1356ed72cd4f595e93a5fa23fd8d7459fc8f81b3 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Tue, 12 Dec 2017 03:55:39 +0100 Subject: Fix #5953 - Add GET /api/v1/accounts/:id/lists (#5983) --- .../api/v1/accounts/lists_controller.rb | 20 +++++++++++++++++++ config/routes.rb | 1 + .../api/v1/accounts/lists_controller_spec.rb | 23 ++++++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 app/controllers/api/v1/accounts/lists_controller.rb create mode 100644 spec/controllers/api/v1/accounts/lists_controller_spec.rb (limited to 'app/controllers/api') diff --git a/app/controllers/api/v1/accounts/lists_controller.rb b/app/controllers/api/v1/accounts/lists_controller.rb new file mode 100644 index 000000000..a7ba89ce2 --- /dev/null +++ b/app/controllers/api/v1/accounts/lists_controller.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +class Api::V1::Accounts::ListsController < Api::BaseController + before_action -> { doorkeeper_authorize! :read } + before_action :require_user! + before_action :set_account + + respond_to :json + + def index + @lists = @account.lists.where(account: current_account) + render json: @lists, each_serializer: REST::ListSerializer + end + + private + + def set_account + @account = Account.find(params[:account_id]) + end +end diff --git a/config/routes.rb b/config/routes.rb index 6313a355d..467849c03 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -269,6 +269,7 @@ Rails.application.routes.draw do resources :statuses, only: :index, controller: 'accounts/statuses' resources :followers, only: :index, controller: 'accounts/follower_accounts' resources :following, only: :index, controller: 'accounts/following_accounts' + resources :lists, only: :index, controller: 'accounts/lists' member do post :follow diff --git a/spec/controllers/api/v1/accounts/lists_controller_spec.rb b/spec/controllers/api/v1/accounts/lists_controller_spec.rb new file mode 100644 index 000000000..0a372f65b --- /dev/null +++ b/spec/controllers/api/v1/accounts/lists_controller_spec.rb @@ -0,0 +1,23 @@ +require 'rails_helper' + +describe Api::V1::Accounts::ListsController do + render_views + + let(:user) { Fabricate(:user, account: Fabricate(:account, username: 'alice')) } + let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'read') } + let(:account) { Fabricate(:account) } + let(:list) { Fabricate(:list, account: user.account) } + + before do + allow(controller).to receive(:doorkeeper_token) { token } + user.account.follow!(account) + list.accounts << account + end + + describe 'GET #index' do + it 'returns http success' do + get :index, params: { account_id: account.id } + expect(response).to have_http_status(:success) + end + end +end -- cgit