From 7951e7ffd5cf5932f7206b52cd85f602abd9b25d Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Wed, 11 Jan 2017 15:39:31 +0100 Subject: Add ruby version to Gemfile, move devDependencies in package.json to dependencies, fix bug in process feed service --- Gemfile.lock | 3 +++ 1 file changed, 3 insertions(+) (limited to 'Gemfile.lock') diff --git a/Gemfile.lock b/Gemfile.lock index 2467b76cc..2408df68d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -463,5 +463,8 @@ DEPENDENCIES webmock will_paginate +RUBY VERSION + ruby 2.3.1p112 + BUNDLED WITH 1.13.6 -- cgit From b11fdc3ae3f90731c01149a5a36dc64e065d4ea2 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Thu, 12 Jan 2017 20:46:24 +0100 Subject: Migrate from ledermann/rails-settings to rails-settings-cached which allows global settings with YAML-defined defaults. Add admin page for editing global settings. Add "site_description" setting that would show as a paragraph on the frontpage --- Gemfile | 3 ++- Gemfile.lock | 10 ++++--- app/assets/javascripts/application_public.js | 5 ++++ app/assets/stylesheets/tables.scss | 4 +++ app/controllers/about_controller.rb | 4 +-- app/controllers/admin/settings_controller.rb | 25 +++++++++++++++++ app/controllers/settings/preferences_controller.rb | 20 ++++++++------ app/helpers/settings_helper.rb | 4 +++ app/lib/hash_object.rb | 10 +++++++ app/models/setting.rb | 31 ++++++++++++++++++++++ app/models/user.rb | 7 ++--- app/services/notify_service.rb | 16 +++++------ app/views/about/index.html.haml | 5 +++- app/views/admin/settings/index.html.haml | 22 +++++++++++++++ app/views/settings/preferences/show.html.haml | 4 +-- config/navigation.rb | 1 + config/routes.rb | 1 + config/settings.yml | 23 ++++++++++++++++ db/migrate/20170112154826_migrate_settings.rb | 19 +++++++++++++ db/schema.rb | 10 +++---- 20 files changed, 189 insertions(+), 35 deletions(-) create mode 100644 app/controllers/admin/settings_controller.rb create mode 100644 app/lib/hash_object.rb create mode 100644 app/models/setting.rb create mode 100644 app/views/admin/settings/index.html.haml create mode 100644 config/settings.yml create mode 100644 db/migrate/20170112154826_migrate_settings.rb (limited to 'Gemfile.lock') diff --git a/Gemfile b/Gemfile index 590fb2124..6b8519369 100644 --- a/Gemfile +++ b/Gemfile @@ -17,6 +17,7 @@ gem 'pg' gem 'pghero' gem 'dotenv-rails' gem 'font-awesome-rails' +gem 'best_in_place', '~> 3.0.1' gem 'paperclip', '~> 5.0' gem 'paperclip-av-transcoder' @@ -43,7 +44,7 @@ gem 'will_paginate' gem 'rack-attack' gem 'rack-cors', require: 'rack/cors' gem 'sidekiq' -gem 'ledermann-rails-settings' +gem 'rails-settings-cached' gem 'pg_search' gem 'simple-navigation' diff --git a/Gemfile.lock b/Gemfile.lock index 2408df68d..2c009955e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -60,6 +60,9 @@ GEM babel-source (>= 4.0, < 6) execjs (~> 2.0) bcrypt (3.1.11) + best_in_place (3.0.3) + actionpack (>= 3.2) + railties (>= 3.2) better_errors (2.1.1) coderay (>= 1.0.0) erubis (>= 2.6.6) @@ -172,8 +175,6 @@ GEM json (1.8.3) launchy (2.4.3) addressable (~> 2.3) - ledermann-rails-settings (2.4.2) - activerecord (>= 3.1) letter_opener (1.4.1) launchy (~> 2.2) link_header (0.0.8) @@ -259,6 +260,8 @@ GEM nokogiri (~> 1.6.0) rails-html-sanitizer (1.0.3) loofah (~> 2.0) + rails-settings-cached (0.6.5) + rails (>= 4.2.0) rails_12factor (0.0.3) rails_serve_static_assets rails_stdout_logging @@ -405,6 +408,7 @@ DEPENDENCIES addressable autoprefixer-rails aws-sdk (>= 2.0) + best_in_place (~> 3.0.1) better_errors binding_of_caller browserify-rails @@ -426,7 +430,6 @@ DEPENDENCIES i18n-tasks (~> 0.9.6) jbuilder (~> 2.0) jquery-rails - ledermann-rails-settings letter_opener link_header lograge @@ -445,6 +448,7 @@ DEPENDENCIES rack-cors rack-timeout-puma rails (~> 5.0.1.0) + rails-settings-cached rails_12factor rails_autolink react-rails diff --git a/app/assets/javascripts/application_public.js b/app/assets/javascripts/application_public.js index f131a267a..9626c5dae 100644 --- a/app/assets/javascripts/application_public.js +++ b/app/assets/javascripts/application_public.js @@ -1,3 +1,8 @@ //= require jquery //= require jquery_ujs //= require extras +//= require best_in_place + +$(function () { + $(".best_in_place").best_in_place(); +}); diff --git a/app/assets/stylesheets/tables.scss b/app/assets/stylesheets/tables.scss index a37870786..279cc3069 100644 --- a/app/assets/stylesheets/tables.scss +++ b/app/assets/stylesheets/tables.scss @@ -36,6 +36,10 @@ text-decoration: none; } } + + strong { + font-weight: 500; + } } samp { diff --git a/app/controllers/about_controller.rb b/app/controllers/about_controller.rb index 7df58444f..84e5fbbd9 100644 --- a/app/controllers/about_controller.rb +++ b/app/controllers/about_controller.rb @@ -4,10 +4,10 @@ class AboutController < ApplicationController before_action :set_body_classes def index + @description = Setting.site_description end - def terms - end + def terms; end private diff --git a/app/controllers/admin/settings_controller.rb b/app/controllers/admin/settings_controller.rb new file mode 100644 index 000000000..af0be8823 --- /dev/null +++ b/app/controllers/admin/settings_controller.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +class Admin::SettingsController < ApplicationController + before_action :require_admin! + + layout 'admin' + + def index + @settings = Setting.all_as_records + end + + def update + @setting = Setting.where(var: params[:id]).first_or_initialize(var: params[:id]) + + if @setting.value != params[:setting][:value] + @setting.value = params[:setting][:value] + @setting.save + end + + respond_to do |format| + format.html { redirect_to admin_settings_path } + format.json { respond_with_bip(@setting) } + end + end +end diff --git a/app/controllers/settings/preferences_controller.rb b/app/controllers/settings/preferences_controller.rb index 3b6d109a6..f273b5f21 100644 --- a/app/controllers/settings/preferences_controller.rb +++ b/app/controllers/settings/preferences_controller.rb @@ -8,14 +8,18 @@ class Settings::PreferencesController < ApplicationController def show; end def update - current_user.settings(:notification_emails).follow = user_params[:notification_emails][:follow] == '1' - current_user.settings(:notification_emails).follow_request = user_params[:notification_emails][:follow_request] == '1' - current_user.settings(:notification_emails).reblog = user_params[:notification_emails][:reblog] == '1' - current_user.settings(:notification_emails).favourite = user_params[:notification_emails][:favourite] == '1' - current_user.settings(:notification_emails).mention = user_params[:notification_emails][:mention] == '1' - - current_user.settings(:interactions).must_be_follower = user_params[:interactions][:must_be_follower] == '1' - current_user.settings(:interactions).must_be_following = user_params[:interactions][:must_be_following] == '1' + current_user.settings['notification_emails'] = { + follow: user_params[:notification_emails][:follow] == '1', + follow_request: user_params[:notification_emails][:follow_request] == '1', + reblog: user_params[:notification_emails][:reblog] == '1', + favourite: user_params[:notification_emails][:favourite] == '1', + mention: user_params[:notification_emails][:mention] == '1', + } + + current_user.settings['interactions'] = { + must_be_follower: user_params[:interactions][:must_be_follower] == '1', + must_be_following: user_params[:interactions][:must_be_following] == '1', + } if current_user.update(user_params.except(:notification_emails, :interactions)) redirect_to settings_preferences_path, notice: I18n.t('generic.changes_saved_msg') diff --git a/app/helpers/settings_helper.rb b/app/helpers/settings_helper.rb index fa569e73a..aed8770c8 100644 --- a/app/helpers/settings_helper.rb +++ b/app/helpers/settings_helper.rb @@ -14,4 +14,8 @@ module SettingsHelper def human_locale(locale) HUMAN_LOCALES[locale] end + + def hash_to_object(hash) + HashObject.new(hash) + end end diff --git a/app/lib/hash_object.rb b/app/lib/hash_object.rb new file mode 100644 index 000000000..274c020ad --- /dev/null +++ b/app/lib/hash_object.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +class HashObject + def initialize(hash) + hash.each do |k, v| + instance_variable_set("@#{k}", v) + self.class.send(:define_method, k, proc { instance_variable_get("@#{k}") }) + end + end +end diff --git a/app/models/setting.rb b/app/models/setting.rb new file mode 100644 index 000000000..0a429a62b --- /dev/null +++ b/app/models/setting.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +class Setting < RailsSettings::Base + source Rails.root.join('config/settings.yml') + namespace Rails.env + + def to_param + var + end + + class << self + def all_as_records + vars = thing_scoped + records = vars.map { |r| [r.var, r] }.to_h + + default_settings.each do |key, default_value| + next if records.key?(key) || default_value.is_a?(Hash) + records[key] = Setting.new(var: key, value: default_value) + end + + records + end + + private + + def default_settings + return {} unless RailsSettings::Default.enabled? + RailsSettings::Default.instance + end + end +end diff --git a/app/models/user.rb b/app/models/user.rb index d5a52da06..bf7d04d7c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class User < ApplicationRecord + include RailsSettings::Extend + devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable belongs_to :account, inverse_of: :user @@ -14,11 +16,6 @@ class User < ApplicationRecord scope :recent, -> { order('id desc') } scope :admins, -> { where(admin: true) } - has_settings do |s| - s.key :notification_emails, defaults: { follow: false, reblog: false, favourite: false, mention: false, follow_request: true } - s.key :interactions, defaults: { must_be_follower: false, must_be_following: false } - end - def send_devise_notification(notification, *args) devise_mailer.send(notification, self, *args).deliver_later end diff --git a/app/services/notify_service.rb b/app/services/notify_service.rb index 2fb1d3919..2eb0f417d 100644 --- a/app/services/notify_service.rb +++ b/app/services/notify_service.rb @@ -37,13 +37,13 @@ class NotifyService < BaseService end def blocked? - blocked = @recipient.suspended? # Skip if the recipient account is suspended anyway - blocked ||= @recipient.id == @notification.from_account.id # Skip for interactions with self - blocked ||= @recipient.blocking?(@notification.from_account) # Skip for blocked accounts - blocked ||= (@notification.from_account.silenced? && !@recipient.following?(@notification.from_account)) # Hellban - blocked ||= (@recipient.user.settings(:interactions).must_be_follower && !@notification.from_account.following?(@recipient)) # Options - blocked ||= (@recipient.user.settings(:interactions).must_be_following && !@recipient.following?(@notification.from_account)) # Options - blocked ||= send("blocked_#{@notification.type}?") # Type-dependent filters + blocked = @recipient.suspended? # Skip if the recipient account is suspended anyway + blocked ||= @recipient.id == @notification.from_account.id # Skip for interactions with self + blocked ||= @recipient.blocking?(@notification.from_account) # Skip for blocked accounts + blocked ||= (@notification.from_account.silenced? && !@recipient.following?(@notification.from_account)) # Hellban + blocked ||= (@recipient.user.settings.interactions['must_be_follower'] && !@notification.from_account.following?(@recipient)) # Options + blocked ||= (@recipient.user.settings.interactions['must_be_following'] && !@recipient.following?(@notification.from_account)) # Options + blocked ||= send("blocked_#{@notification.type}?") # Type-dependent filters blocked end @@ -58,6 +58,6 @@ class NotifyService < BaseService end def email_enabled? - @recipient.user.settings(:notification_emails).send(@notification.type) + @recipient.user.settings.notification_emails[@notification.type] end end diff --git a/app/views/about/index.html.haml b/app/views/about/index.html.haml index 0c6516036..a593ff578 100644 --- a/app/views/about/index.html.haml +++ b/app/views/about/index.html.haml @@ -8,7 +8,7 @@ %meta{ property: 'og:site_name', content: 'Mastodon' }/ %meta{ property: 'og:type', content: 'website' }/ %meta{ property: 'og:title', content: Rails.configuration.x.local_domain }/ - %meta{ property: 'og:description', content: "Mastodon is a free, open-source social network server. A decentralized alternative to commercial platforms, it avoids the risks of a single company monopolizing your communication. Anyone can run Mastodon and participate in the social network seamlessly" }/ + %meta{ property: 'og:description', content: @description.blank? ? "Mastodon is a free, open-source social network server. A decentralized alternative to commercial platforms, it avoids the risks of a single company monopolizing your communication. Anyone can run Mastodon and participate in the social network seamlessly" : strip_tags(@description) }/ %meta{ property: 'og:image', content: asset_url('mastodon_small.jpg') }/ %meta{ property: 'og:image:width', content: '400' }/ %meta{ property: 'og:image:height', content: '400' }/ @@ -24,6 +24,9 @@ .screenshot= image_tag 'screenshot.png' + - unless @description.blank? + %p= @description.html_safe + .actions .info = link_to t('about.terms'), terms_path diff --git a/app/views/admin/settings/index.html.haml b/app/views/admin/settings/index.html.haml new file mode 100644 index 000000000..b8ca3a7a4 --- /dev/null +++ b/app/views/admin/settings/index.html.haml @@ -0,0 +1,22 @@ +- content_for :page_title do + Site Settings + +%table.table + %colgroup + %col{ width: '35%' }/ + %thead + %tr + %th Setting + %th Click to edit + %tbody + %tr + %td + %strong Site description + %br/ + Displayed as a paragraph on the frontpage and used as a meta tag. + %br/ + You can use HTML tags, in particular + %code= '' + and + %code= '' + %td= best_in_place @settings['site_description'], :value, as: :textarea, url: admin_setting_path(@settings['site_description']) diff --git a/app/views/settings/preferences/show.html.haml b/app/views/settings/preferences/show.html.haml index a0860c94b..a9a1d21ac 100644 --- a/app/views/settings/preferences/show.html.haml +++ b/app/views/settings/preferences/show.html.haml @@ -6,14 +6,14 @@ = f.input :locale, collection: I18n.available_locales, wrapper: :with_label, include_blank: false, label_method: lambda { |locale| human_locale(locale) } - = f.simple_fields_for :notification_emails, current_user.settings(:notification_emails) do |ff| + = f.simple_fields_for :notification_emails, hash_to_object(current_user.settings.notification_emails) do |ff| = ff.input :follow, as: :boolean, wrapper: :with_label = ff.input :follow_request, as: :boolean, wrapper: :with_label = ff.input :reblog, as: :boolean, wrapper: :with_label = ff.input :favourite, as: :boolean, wrapper: :with_label = ff.input :mention, as: :boolean, wrapper: :with_label - = f.simple_fields_for :interactions, current_user.settings(:interactions) do |ff| + = f.simple_fields_for :interactions, hash_to_object(current_user.settings.interactions) do |ff| = ff.input :must_be_follower, as: :boolean, wrapper: :with_label = ff.input :must_be_following, as: :boolean, wrapper: :with_label diff --git a/config/navigation.rb b/config/navigation.rb index 1b6615ed0..9aaa12b0b 100644 --- a/config/navigation.rb +++ b/config/navigation.rb @@ -7,5 +7,6 @@ SimpleNavigation::Configuration.run do |navigation| primary.item :domain_blocks, safe_join([fa_icon('lock fw'), 'Domain Blocks']), admin_domain_blocks_url primary.item :sidekiq, safe_join([fa_icon('diamond fw'), 'Sidekiq']), sidekiq_url primary.item :pghero, safe_join([fa_icon('database fw'), 'PgHero']), pghero_url + primary.item :settings, safe_join([fa_icon('cogs fw'), 'Site Settings']), admin_settings_url end end diff --git a/config/routes.rb b/config/routes.rb index dd6944b29..c0262e933 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -58,6 +58,7 @@ Rails.application.routes.draw do namespace :admin do resources :pubsubhubbub, only: [:index] resources :domain_blocks, only: [:index, :create] + resources :settings, only: [:index, :update] resources :accounts, only: [:index, :show, :update] do member do diff --git a/config/settings.yml b/config/settings.yml new file mode 100644 index 000000000..2e309e46e --- /dev/null +++ b/config/settings.yml @@ -0,0 +1,23 @@ +# config/app.yml for rails-settings-cached +defaults: &defaults + site_description: '' + site_contact_username: '' + site_contact_email: '' + notification_emails: + follow: false + reblog: false + favourite: false + mention: false + follow_request: true + interactions: + must_be_follower: false + must_be_following: false + +development: + <<: *defaults + +test: + <<: *defaults + +production: + <<: *defaults diff --git a/db/migrate/20170112154826_migrate_settings.rb b/db/migrate/20170112154826_migrate_settings.rb new file mode 100644 index 000000000..f6f6ed531 --- /dev/null +++ b/db/migrate/20170112154826_migrate_settings.rb @@ -0,0 +1,19 @@ +class MigrateSettings < ActiveRecord::Migration + def up + remove_index :settings, [:target_type, :target_id, :var] + rename_column :settings, :target_id, :thing_id + rename_column :settings, :target_type, :thing_type + change_column :settings, :thing_id, :integer, null: true, default: nil + change_column :settings, :thing_type, :string, null: true, default: nil + add_index :settings, [:thing_type, :thing_id, :var], unique: true + end + + def down + remove_index :settings, [:thing_type, :thing_id, :var] + rename_column :settings, :thing_id, :target_id + rename_column :settings, :thing_type, :target_type + change_column :settings, :target_id, :integer, null: false + change_column :settings, :target_type, :string, null: false, default: '' + add_index :settings, [:target_type, :target_id, :var], unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 5a5dd83c7..1cd1258db 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: 20170109120109) do +ActiveRecord::Schema.define(version: 20170112154826) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -238,13 +238,13 @@ ActiveRecord::Schema.define(version: 20170109120109) do end create_table "settings", force: :cascade do |t| - t.string "var", null: false + t.string "var", null: false t.text "value" - t.string "target_type", null: false - t.integer "target_id", null: false + t.string "thing_type" + t.integer "thing_id" t.datetime "created_at" t.datetime "updated_at" - t.index ["target_type", "target_id", "var"], name: "index_settings_on_target_type_and_target_id_and_var", unique: true, using: :btree + t.index ["thing_type", "thing_id", "var"], name: "index_settings_on_thing_type_and_thing_id_and_var", unique: true, using: :btree end create_table "statuses", force: :cascade do |t| -- cgit From 306eb6e9c90295dcdff2a0094066542a46a8e634 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Wed, 18 Jan 2017 23:44:29 +0100 Subject: Add optional StatsD performance tracking --- Gemfile | 1 + Gemfile.lock | 2 ++ app/lib/statsd_monitor.rb | 11 +++++++++++ config/application.rb | 2 ++ config/environments/production.rb | 4 ++++ config/initializers/inflections.rb | 7 +++---- config/initializers/statsd.rb | 20 ++++++++++++++++++++ 7 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 app/lib/statsd_monitor.rb create mode 100644 config/initializers/statsd.rb (limited to 'Gemfile.lock') diff --git a/Gemfile b/Gemfile index 6b8519369..1341e45de 100644 --- a/Gemfile +++ b/Gemfile @@ -47,6 +47,7 @@ gem 'sidekiq' gem 'rails-settings-cached' gem 'pg_search' gem 'simple-navigation' +gem 'statsd-instrument' gem 'react-rails' gem 'browserify-rails' diff --git a/Gemfile.lock b/Gemfile.lock index 2c009955e..7214d21eb 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -370,6 +370,7 @@ GEM actionpack (>= 4.0) activesupport (>= 4.0) sprockets (>= 3.0.0) + statsd-instrument (2.1.2) temple (0.7.7) term-ansicolor (1.4.0) tins (~> 1.0) @@ -463,6 +464,7 @@ DEPENDENCIES simple-navigation simple_form simplecov + statsd-instrument uglifier (>= 1.3.0) webmock will_paginate diff --git a/app/lib/statsd_monitor.rb b/app/lib/statsd_monitor.rb new file mode 100644 index 000000000..e48ce6541 --- /dev/null +++ b/app/lib/statsd_monitor.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class StatsDMonitor + def initialize(app) + @app = app + end + + def call(env) + @app.call(env) + end +end diff --git a/config/application.rb b/config/application.rb index e561d0473..e97fb165b 100644 --- a/config/application.rb +++ b/config/application.rb @@ -30,6 +30,8 @@ module Mastodon config.active_job.queue_adapter = :sidekiq + config.middleware.insert(0, 'StatsDMonitor') + config.middleware.insert_before 0, Rack::Cors do allow do origins '*' diff --git a/config/environments/production.rb b/config/environments/production.rb index 8b8d974b3..30170e810 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -104,4 +104,8 @@ Rails.application.configure do config.react.variant = :production config.active_record.logger = nil + + config.to_prepare do + StatsD.backend = StatsD::Instrument::Backends::NullBackend if ENV['STATSD_ADDR'].blank? + end end diff --git a/config/initializers/inflections.rb b/config/initializers/inflections.rb index ac033bf9d..8fd1ae72c 100644 --- a/config/initializers/inflections.rb +++ b/config/initializers/inflections.rb @@ -10,7 +10,6 @@ # inflect.uncountable %w( fish sheep ) # end -# These inflection rules are supported but not enabled by default: -# ActiveSupport::Inflector.inflections(:en) do |inflect| -# inflect.acronym 'RESTful' -# end +ActiveSupport::Inflector.inflections(:en) do |inflect| + inflect.acronym 'StatsD' +end diff --git a/config/initializers/statsd.rb b/config/initializers/statsd.rb new file mode 100644 index 000000000..c9c754e7f --- /dev/null +++ b/config/initializers/statsd.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +StatsD.prefix = 'mastodon' +StatsD.default_sample_rate = 1 + +StatsDMonitor.extend(StatsD::Instrument) +StatsDMonitor.statsd_measure(:call, 'request.duration') + +STATSD_REQUEST_METRICS = { + 'request.status.success' => 200, + 'request.status.not_found' => 404, + 'request.status.too_many_requests' => 429, + 'request.status.internal_server_error' => 500, +}.freeze + +STATSD_REQUEST_METRICS.each do |name, code| + StatsDMonitor.statsd_count_if(:call, name) do |status, _env, _body| + status.to_i == code + end +end -- cgit From f051c2e8131616018486cfb9f4a80a120ca71f6e Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Thu, 19 Jan 2017 09:37:07 +0100 Subject: Fix statsd null backend not being initialized properly --- Gemfile | 2 +- Gemfile.lock | 7 +++---- app/models/account.rb | 3 ++- config/environments/production.rb | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) (limited to 'Gemfile.lock') diff --git a/Gemfile b/Gemfile index 1341e45de..aa149c61e 100644 --- a/Gemfile +++ b/Gemfile @@ -19,7 +19,7 @@ gem 'dotenv-rails' gem 'font-awesome-rails' gem 'best_in_place', '~> 3.0.1' -gem 'paperclip', '~> 5.0' +gem 'paperclip', '~> 5.1' gem 'paperclip-av-transcoder' gem 'aws-sdk', '>= 2.0' diff --git a/Gemfile.lock b/Gemfile.lock index 7214d21eb..9b33580fc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -76,8 +76,7 @@ GEM bullet (5.3.0) activesupport (>= 3.0.0) uniform_notifier (~> 1.10.0) - climate_control (0.0.3) - activesupport (>= 3.0) + climate_control (0.1.0) cocaine (0.5.8) climate_control (>= 0.0.3, < 1.0) coderay (1.1.1) @@ -89,7 +88,7 @@ GEM execjs coffee-script-source (1.10.0) colorize (0.8.1) - concurrent-ruby (1.0.3) + concurrent-ruby (1.0.4) connection_pool (2.2.1) crack (0.4.3) safe_yaml (~> 1.0.0) @@ -437,7 +436,7 @@ DEPENDENCIES nokogiri oj ostatus2 - paperclip (~> 5.0) + paperclip (~> 5.1) paperclip-av-transcoder pg pg_search diff --git a/app/models/account.rb b/app/models/account.rb index 621a66e24..c2a41c4c6 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -126,7 +126,8 @@ class Account < ApplicationRecord def save_with_optional_avatar! save! rescue ActiveRecord::RecordInvalid - self.avatar = nil + self.avatar = nil + self[:avatar_remote_url] = '' save! end diff --git a/config/environments/production.rb b/config/environments/production.rb index 30170e810..1572eaf6c 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -106,6 +106,6 @@ Rails.application.configure do config.active_record.logger = nil config.to_prepare do - StatsD.backend = StatsD::Instrument::Backends::NullBackend if ENV['STATSD_ADDR'].blank? + StatsD.backend = StatsD::Instrument::Backends::NullBackend.new if ENV['STATSD_ADDR'].blank? end end -- cgit From f0de621e76b5a5ba3f7e67bd88c0183aac22b985 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Fri, 20 Jan 2017 01:00:14 +0100 Subject: Fix #463 - Fetch and display previews of URLs using OpenGraph tags --- Gemfile | 1 + Gemfile.lock | 2 + .../javascripts/components/actions/cards.jsx | 40 +++++++++ .../javascripts/components/actions/statuses.jsx | 2 + .../components/features/status/components/card.jsx | 96 ++++++++++++++++++++++ .../features/status/components/detailed_status.jsx | 3 + .../features/status/containers/card_container.jsx | 8 ++ .../javascripts/components/reducers/cards.jsx | 14 ++++ .../javascripts/components/reducers/index.jsx | 4 +- app/assets/stylesheets/components.scss | 6 ++ app/controllers/api/v1/statuses_controller.rb | 8 +- app/lib/statsd_monitor.rb | 11 --- app/models/preview_card.rb | 20 +++++ app/models/status.rb | 1 + app/services/fetch_link_card_service.rb | 33 ++++++++ app/services/post_status_service.rb | 1 + app/views/api/v1/statuses/card.rabl | 5 ++ app/workers/link_crawl_worker.rb | 13 +++ config/application.rb | 3 +- config/initializers/inflections.rb | 1 + config/routes.rb | 3 +- db/migrate/20170119214911_create_preview_cards.rb | 17 ++++ db/schema.rb | 16 +++- lib/statsd_monitor.rb | 11 +++ spec/fabricators/preview_card_fabricator.rb | 5 ++ spec/models/preview_card_spec.rb | 5 ++ spec/models/subscription_spec.rb | 2 +- 27 files changed, 313 insertions(+), 18 deletions(-) create mode 100644 app/assets/javascripts/components/actions/cards.jsx create mode 100644 app/assets/javascripts/components/features/status/components/card.jsx create mode 100644 app/assets/javascripts/components/features/status/containers/card_container.jsx create mode 100644 app/assets/javascripts/components/reducers/cards.jsx delete mode 100644 app/lib/statsd_monitor.rb create mode 100644 app/models/preview_card.rb create mode 100644 app/services/fetch_link_card_service.rb create mode 100644 app/views/api/v1/statuses/card.rabl create mode 100644 app/workers/link_crawl_worker.rb create mode 100644 db/migrate/20170119214911_create_preview_cards.rb create mode 100644 lib/statsd_monitor.rb create mode 100644 spec/fabricators/preview_card_fabricator.rb create mode 100644 spec/models/preview_card_spec.rb (limited to 'Gemfile.lock') diff --git a/Gemfile b/Gemfile index aa149c61e..bab7cebb5 100644 --- a/Gemfile +++ b/Gemfile @@ -48,6 +48,7 @@ gem 'rails-settings-cached' gem 'pg_search' gem 'simple-navigation' gem 'statsd-instrument' +gem 'ruby-oembed', require: 'oembed' gem 'react-rails' gem 'browserify-rails' diff --git a/Gemfile.lock b/Gemfile.lock index 9b33580fc..20ea37fcc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -334,6 +334,7 @@ GEM rainbow (>= 1.99.1, < 3.0) ruby-progressbar (~> 1.7) unicode-display_width (~> 1.0, >= 1.0.1) + ruby-oembed (0.10.1) ruby-progressbar (1.8.1) safe_yaml (1.0.4) sass (3.4.22) @@ -457,6 +458,7 @@ DEPENDENCIES rspec-rails rspec-sidekiq rubocop + ruby-oembed sass-rails (~> 5.0) sdoc (~> 0.4.0) sidekiq diff --git a/app/assets/javascripts/components/actions/cards.jsx b/app/assets/javascripts/components/actions/cards.jsx new file mode 100644 index 000000000..808f1835b --- /dev/null +++ b/app/assets/javascripts/components/actions/cards.jsx @@ -0,0 +1,40 @@ +import api from '../api'; + +export const STATUS_CARD_FETCH_REQUEST = 'STATUS_CARD_FETCH_REQUEST'; +export const STATUS_CARD_FETCH_SUCCESS = 'STATUS_CARD_FETCH_SUCCESS'; +export const STATUS_CARD_FETCH_FAIL = 'STATUS_CARD_FETCH_FAIL'; + +export function fetchStatusCard(id) { + return (dispatch, getState) => { + dispatch(fetchStatusCardRequest(id)); + + api(getState).get(`/api/v1/statuses/${id}/card`).then(response => { + dispatch(fetchStatusCardSuccess(id, response.data)); + }).catch(error => { + dispatch(fetchStatusCardFail(id, error)); + }); + }; +}; + +export function fetchStatusCardRequest(id) { + return { + type: STATUS_CARD_FETCH_REQUEST, + id + }; +}; + +export function fetchStatusCardSuccess(id, card) { + return { + type: STATUS_CARD_FETCH_SUCCESS, + id, + card + }; +}; + +export function fetchStatusCardFail(id, error) { + return { + type: STATUS_CARD_FETCH_FAIL, + id, + error + }; +}; diff --git a/app/assets/javascripts/components/actions/statuses.jsx b/app/assets/javascripts/components/actions/statuses.jsx index 21a56381e..9ac215727 100644 --- a/app/assets/javascripts/components/actions/statuses.jsx +++ b/app/assets/javascripts/components/actions/statuses.jsx @@ -1,6 +1,7 @@ import api from '../api'; import { deleteFromTimelines } from './timelines'; +import { fetchStatusCard } from './cards'; export const STATUS_FETCH_REQUEST = 'STATUS_FETCH_REQUEST'; export const STATUS_FETCH_SUCCESS = 'STATUS_FETCH_SUCCESS'; @@ -31,6 +32,7 @@ export function fetchStatus(id) { api(getState).get(`/api/v1/statuses/${id}`).then(response => { dispatch(fetchStatusSuccess(response.data, skipLoading)); dispatch(fetchContext(id)); + dispatch(fetchStatusCard(id)); }).catch(error => { dispatch(fetchStatusFail(id, error, skipLoading)); }); diff --git a/app/assets/javascripts/components/features/status/components/card.jsx b/app/assets/javascripts/components/features/status/components/card.jsx new file mode 100644 index 000000000..7161de364 --- /dev/null +++ b/app/assets/javascripts/components/features/status/components/card.jsx @@ -0,0 +1,96 @@ +import PureRenderMixin from 'react-addons-pure-render-mixin'; +import ImmutablePropTypes from 'react-immutable-proptypes'; + +const outerStyle = { + display: 'flex', + cursor: 'pointer', + fontSize: '14px', + border: '1px solid #363c4b', + borderRadius: '4px', + color: '#616b86', + marginTop: '14px', + textDecoration: 'none', + overflow: 'hidden' +}; + +const contentStyle = { + flex: '2', + padding: '8px', + paddingLeft: '14px' +}; + +const titleStyle = { + display: 'block', + fontWeight: '500', + marginBottom: '5px', + color: '#d9e1e8' +}; + +const descriptionStyle = { + color: '#d9e1e8' +}; + +const imageOuterStyle = { + flex: '1', + background: '#373b4a' +}; + +const imageStyle = { + display: 'block', + width: '100%', + height: 'auto', + margin: '0', + borderRadius: '4px 0 0 4px' +}; + +const hostStyle = { + display: 'block', + marginTop: '5px', + fontSize: '13px' +}; + +const getHostname = url => { + const parser = document.createElement('a'); + parser.href = url; + return parser.hostname; +}; + +const Card = React.createClass({ + propTypes: { + card: ImmutablePropTypes.map + }, + + mixins: [PureRenderMixin], + + render () { + const { card } = this.props; + + if (card === null) { + return null; + } + + let image = ''; + + if (card.get('image')) { + image = ( +
+ {card.get('title')} +
+ ); + } + + return ( +
+ {image} + +
+ {card.get('title')} +

{card.get('description')}

+ {getHostname(card.get('url'))} +
+
+ ); + } +}); + +export default Card; diff --git a/app/assets/javascripts/components/features/status/components/detailed_status.jsx b/app/assets/javascripts/components/features/status/components/detailed_status.jsx index 14a504c7c..f2d6ae48a 100644 --- a/app/assets/javascripts/components/features/status/components/detailed_status.jsx +++ b/app/assets/javascripts/components/features/status/components/detailed_status.jsx @@ -7,6 +7,7 @@ import MediaGallery from '../../../components/media_gallery'; import VideoPlayer from '../../../components/video_player'; import { Link } from 'react-router'; import { FormattedDate, FormattedNumber } from 'react-intl'; +import CardContainer from '../containers/card_container'; const DetailedStatus = React.createClass({ @@ -42,6 +43,8 @@ const DetailedStatus = React.createClass({ } else { media = ; } + } else { + media = ; } if (status.get('application')) { diff --git a/app/assets/javascripts/components/features/status/containers/card_container.jsx b/app/assets/javascripts/components/features/status/containers/card_container.jsx new file mode 100644 index 000000000..5c8bfeec2 --- /dev/null +++ b/app/assets/javascripts/components/features/status/containers/card_container.jsx @@ -0,0 +1,8 @@ +import { connect } from 'react-redux'; +import Card from '../components/card'; + +const mapStateToProps = (state, { statusId }) => ({ + card: state.getIn(['cards', statusId], null) +}); + +export default connect(mapStateToProps)(Card); diff --git a/app/assets/javascripts/components/reducers/cards.jsx b/app/assets/javascripts/components/reducers/cards.jsx new file mode 100644 index 000000000..3c9395011 --- /dev/null +++ b/app/assets/javascripts/components/reducers/cards.jsx @@ -0,0 +1,14 @@ +import { STATUS_CARD_FETCH_SUCCESS } from '../actions/cards'; + +import Immutable from 'immutable'; + +const initialState = Immutable.Map(); + +export default function cards(state = initialState, action) { + switch(action.type) { + case STATUS_CARD_FETCH_SUCCESS: + return state.set(action.id, Immutable.fromJS(action.card)); + default: + return state; + } +}; diff --git a/app/assets/javascripts/components/reducers/index.jsx b/app/assets/javascripts/components/reducers/index.jsx index 80c913d2d..0798116c4 100644 --- a/app/assets/javascripts/components/reducers/index.jsx +++ b/app/assets/javascripts/components/reducers/index.jsx @@ -13,6 +13,7 @@ import search from './search'; import notifications from './notifications'; import settings from './settings'; import status_lists from './status_lists'; +import cards from './cards'; export default combineReducers({ timelines, @@ -28,5 +29,6 @@ export default combineReducers({ relationships, search, notifications, - settings + settings, + cards }); diff --git a/app/assets/stylesheets/components.scss b/app/assets/stylesheets/components.scss index fedf73b1d..7e61323ab 100644 --- a/app/assets/stylesheets/components.scss +++ b/app/assets/stylesheets/components.scss @@ -680,3 +680,9 @@ button.active i.fa-retweet { transition-duration: 0.9s; background-position: 0 -209px; } + +.status-card { + &:hover { + background: #363c4b; + } +} diff --git a/app/controllers/api/v1/statuses_controller.rb b/app/controllers/api/v1/statuses_controller.rb index c661d81c1..37ed5e6dd 100644 --- a/app/controllers/api/v1/statuses_controller.rb +++ b/app/controllers/api/v1/statuses_controller.rb @@ -3,8 +3,8 @@ class Api::V1::StatusesController < ApiController before_action -> { doorkeeper_authorize! :read }, except: [:create, :destroy, :reblog, :unreblog, :favourite, :unfavourite] before_action -> { doorkeeper_authorize! :write }, only: [:create, :destroy, :reblog, :unreblog, :favourite, :unfavourite] - before_action :require_user!, except: [:show, :context, :reblogged_by, :favourited_by] - before_action :set_status, only: [:show, :context, :reblogged_by, :favourited_by] + before_action :require_user!, except: [:show, :context, :card, :reblogged_by, :favourited_by] + before_action :set_status, only: [:show, :context, :card, :reblogged_by, :favourited_by] respond_to :json @@ -21,6 +21,10 @@ class Api::V1::StatusesController < ApiController set_counters_maps(statuses) end + def card + @card = PreviewCard.find_by!(status: @status) + end + def reblogged_by results = @status.reblogs.paginate_by_max_id(DEFAULT_ACCOUNTS_LIMIT, params[:max_id], params[:since_id]) accounts = Account.where(id: results.map(&:account_id)).map { |a| [a.id, a] }.to_h diff --git a/app/lib/statsd_monitor.rb b/app/lib/statsd_monitor.rb deleted file mode 100644 index e48ce6541..000000000 --- a/app/lib/statsd_monitor.rb +++ /dev/null @@ -1,11 +0,0 @@ -# frozen_string_literal: true - -class StatsDMonitor - def initialize(app) - @app = app - end - - def call(env) - @app.call(env) - end -end diff --git a/app/models/preview_card.rb b/app/models/preview_card.rb new file mode 100644 index 000000000..e59b05eb8 --- /dev/null +++ b/app/models/preview_card.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +class PreviewCard < ApplicationRecord + IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze + + belongs_to :status + + has_attached_file :image, styles: { original: '120x120#' }, convert_options: { all: '-quality 80 -strip' } + + validates :url, presence: true + validates_attachment_content_type :image, content_type: IMAGE_MIME_TYPES + validates_attachment_size :image, less_than: 1.megabytes + + def save_with_optional_image! + save! + rescue ActiveRecord::RecordInvalid + self.image = nil + save! + end +end diff --git a/app/models/status.rb b/app/models/status.rb index 5710f9cca..d5f52b55c 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -23,6 +23,7 @@ class Status < ApplicationRecord has_and_belongs_to_many :tags has_one :notification, as: :activity, dependent: :destroy + has_one :preview_card, dependent: :destroy validates :account, presence: true validates :uri, uniqueness: true, unless: 'local?' diff --git a/app/services/fetch_link_card_service.rb b/app/services/fetch_link_card_service.rb new file mode 100644 index 000000000..2779b79b5 --- /dev/null +++ b/app/services/fetch_link_card_service.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +class FetchLinkCardService < BaseService + def call(status) + # Get first URL + url = URI.extract(status.text).reject { |uri| (uri =~ /\Ahttps?:\/\//).nil? }.first + + return if url.nil? + + response = http_client.get(url) + + return if response.code != 200 + + page = Nokogiri::HTML(response.to_s) + card = PreviewCard.where(status: status).first_or_initialize(status: status, url: url) + + card.title = meta_property(page, 'og:title') || page.at_xpath('//title')&.content + card.description = meta_property(page, 'og:description') || meta_property(page, 'description') + card.image = URI.parse(meta_property(page, 'og:image')) if meta_property(page, 'og:image') + + card.save_with_optional_image! + end + + private + + def http_client + HTTP.timeout(:per_operation, write: 10, connect: 10, read: 10).follow + end + + def meta_property(html, property) + html.at_xpath("//meta[@property=\"#{property}\"]")&.attribute('content')&.value || html.at_xpath("//meta[@name=\"#{property}\"]")&.attribute('content')&.value + end +end diff --git a/app/services/post_status_service.rb b/app/services/post_status_service.rb index af31c923f..8765ef5e3 100644 --- a/app/services/post_status_service.rb +++ b/app/services/post_status_service.rb @@ -22,6 +22,7 @@ class PostStatusService < BaseService process_mentions_service.call(status) process_hashtags_service.call(status) + LinkCrawlWorker.perform_async(status.id) DistributionWorker.perform_async(status.id) Pubsubhubbub::DistributionWorker.perform_async(status.stream_entry.id) diff --git a/app/views/api/v1/statuses/card.rabl b/app/views/api/v1/statuses/card.rabl new file mode 100644 index 000000000..8ba8dcbb1 --- /dev/null +++ b/app/views/api/v1/statuses/card.rabl @@ -0,0 +1,5 @@ +object @card + +attributes :url, :title, :description + +node(:image) { |card| card.image? ? full_asset_url(card.image.url(:original)) : nil } diff --git a/app/workers/link_crawl_worker.rb b/app/workers/link_crawl_worker.rb new file mode 100644 index 000000000..af3394b8b --- /dev/null +++ b/app/workers/link_crawl_worker.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +class LinkCrawlWorker + include Sidekiq::Worker + + sidekiq_options retry: false + + def perform(status_id) + FetchLinkCardService.new.call(Status.find(status_id)) + rescue ActiveRecord::RecordNotFound + true + end +end diff --git a/config/application.rb b/config/application.rb index e97fb165b..d0b06bf95 100644 --- a/config/application.rb +++ b/config/application.rb @@ -3,6 +3,7 @@ require_relative 'boot' require 'rails/all' require_relative '../app/lib/exceptions' +require_relative '../lib/statsd_monitor' # Require the gems listed in Gemfile, including any gems # you've limited to :test, :development, or :production. @@ -30,7 +31,7 @@ module Mastodon config.active_job.queue_adapter = :sidekiq - config.middleware.insert(0, 'StatsDMonitor') + config.middleware.insert(0, ::StatsDMonitor) config.middleware.insert_before 0, Rack::Cors do allow do diff --git a/config/initializers/inflections.rb b/config/initializers/inflections.rb index 8fd1ae72c..b5e43e705 100644 --- a/config/initializers/inflections.rb +++ b/config/initializers/inflections.rb @@ -12,4 +12,5 @@ ActiveSupport::Inflector.inflections(:en) do |inflect| inflect.acronym 'StatsD' + inflect.acronym 'OEmbed' end diff --git a/config/routes.rb b/config/routes.rb index 42de503f0..4606c663a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -86,6 +86,7 @@ Rails.application.routes.draw do resources :statuses, only: [:create, :show, :destroy] do member do get :context + get :card get :reblogged_by get :favourited_by @@ -146,7 +147,7 @@ Rails.application.routes.draw do get '/about', to: 'about#index' get '/about/more', to: 'about#more' get '/terms', to: 'about#terms' - + root 'home#index' match '*unmatched_route', via: :all, to: 'application#raise_not_found' diff --git a/db/migrate/20170119214911_create_preview_cards.rb b/db/migrate/20170119214911_create_preview_cards.rb new file mode 100644 index 000000000..70ed91bbd --- /dev/null +++ b/db/migrate/20170119214911_create_preview_cards.rb @@ -0,0 +1,17 @@ +class CreatePreviewCards < ActiveRecord::Migration[5.0] + def change + create_table :preview_cards do |t| + t.integer :status_id + t.string :url, null: false, default: '' + + # OpenGraph + t.string :title, null: true + t.string :description, null: true + t.attachment :image + + t.timestamps + end + + add_index :preview_cards, :status_id, unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 37da0c44e..abe6f1bfe 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: 20170114203041) do +ActiveRecord::Schema.define(version: 20170119214911) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -157,6 +157,20 @@ ActiveRecord::Schema.define(version: 20170114203041) do t.index ["uid"], name: "index_oauth_applications_on_uid", unique: true, using: :btree end + create_table "preview_cards", force: :cascade do |t| + t.integer "status_id" + t.string "url", default: "", null: false + t.string "title" + t.string "description" + t.string "image_file_name" + t.string "image_content_type" + t.integer "image_file_size" + t.datetime "image_updated_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["status_id"], name: "index_preview_cards_on_status_id", unique: true, using: :btree + end + create_table "pubsubhubbub_subscriptions", force: :cascade do |t| t.string "topic", default: "", null: false t.string "callback", default: "", null: false diff --git a/lib/statsd_monitor.rb b/lib/statsd_monitor.rb new file mode 100644 index 000000000..e48ce6541 --- /dev/null +++ b/lib/statsd_monitor.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class StatsDMonitor + def initialize(app) + @app = app + end + + def call(env) + @app.call(env) + end +end diff --git a/spec/fabricators/preview_card_fabricator.rb b/spec/fabricators/preview_card_fabricator.rb new file mode 100644 index 000000000..448a94e7e --- /dev/null +++ b/spec/fabricators/preview_card_fabricator.rb @@ -0,0 +1,5 @@ +Fabricator(:preview_card) do + status_id 1 + url "MyString" + html "MyText" +end diff --git a/spec/models/preview_card_spec.rb b/spec/models/preview_card_spec.rb new file mode 100644 index 000000000..14ef23923 --- /dev/null +++ b/spec/models/preview_card_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe PreviewCard, type: :model do + +end diff --git a/spec/models/subscription_spec.rb b/spec/models/subscription_spec.rb index d40bf0b44..9cb3d41ce 100644 --- a/spec/models/subscription_spec.rb +++ b/spec/models/subscription_spec.rb @@ -1,5 +1,5 @@ require 'rails_helper' RSpec.describe Subscription, type: :model do - pending "add some examples to (or delete) #{__FILE__}" + end -- cgit From 5fef55c11a7ea855e7b43ed274558a0893d0dc16 Mon Sep 17 00:00:00 2001 From: Effy Elden Date: Sat, 21 Jan 2017 20:22:49 +1100 Subject: Set remote dev environments to use letter_opener_web, add default .env.vagrant variables --- .env.vagrant | 1 + Gemfile | 1 + Gemfile.lock | 7 ++++++- config/environments/development.rb | 10 +++++++++- config/routes.rb | 6 ++++++ 5 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 .env.vagrant (limited to 'Gemfile.lock') diff --git a/.env.vagrant b/.env.vagrant new file mode 100644 index 000000000..0ab0552c9 --- /dev/null +++ b/.env.vagrant @@ -0,0 +1 @@ +VAGRANT=true \ No newline at end of file diff --git a/Gemfile b/Gemfile index bab7cebb5..ebee7e35f 100644 --- a/Gemfile +++ b/Gemfile @@ -73,6 +73,7 @@ group :development do gem 'better_errors' gem 'binding_of_caller' gem 'letter_opener' + gem 'letter_opener_web' gem 'bullet' gem 'active_record_query_trace' end diff --git a/Gemfile.lock b/Gemfile.lock index 20ea37fcc..0e720d7fb 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -176,6 +176,10 @@ GEM addressable (~> 2.3) letter_opener (1.4.1) launchy (~> 2.2) + letter_opener_web (1.3.0) + actionmailer (>= 3.2) + letter_opener (~> 1.0) + railties (>= 3.2) link_header (0.0.8) lograge (0.4.1) actionpack (>= 4, < 5.1) @@ -432,6 +436,7 @@ DEPENDENCIES jbuilder (~> 2.0) jquery-rails letter_opener + letter_opener_web link_header lograge nokogiri @@ -474,4 +479,4 @@ RUBY VERSION ruby 2.3.1p112 BUNDLED WITH - 1.13.6 + 1.13.7 diff --git a/config/environments/development.rb b/config/environments/development.rb index 829edcf04..476f3d74c 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -62,7 +62,15 @@ Rails.application.configure do # routes, locales, etc. This feature depends on the listen gem. # config.file_watcher = ActiveSupport::EventedFileUpdateChecker - config.action_mailer.delivery_method = :letter_opener + # If usng a Heroku, Vagrant or generic remote development environment, + # use letter_opener_web, accessible at /letter_opener. + # + # Otherwise, use letter_opener, which launches a browser window to view sent mail. + if (ENV['HEROKU'] || ENV['VAGRANT'] || ENV['REMOTE_DEV']) + config.action_mailer.delivery_method = :letter_opener_web + else + config.action_mailer.delivery_method = :letter_opener + end config.after_initialize do Bullet.enable = true diff --git a/config/routes.rb b/config/routes.rb index 4606c663a..0fcbc266d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,6 +3,12 @@ require 'sidekiq/web' Rails.application.routes.draw do + + # Development-only routes + if Rails.env.development? + mount LetterOpenerWeb::Engine, at: "/letter_opener" + end + mount ActionCable.server, at: 'cable' authenticate :user, lambda { |u| u.admin? } do -- cgit From d4d56b8af0e85e328eb1a323816d734ca16a4176 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Mon, 23 Jan 2017 14:45:09 +0100 Subject: Fix #365, 1/2 of #408 - replace rails_autolink with URI.regexp, run link_hashtags on simplified_format --- Gemfile | 1 - Gemfile.lock | 3 --- app/lib/formatter.rb | 10 ++++++++-- 3 files changed, 8 insertions(+), 6 deletions(-) (limited to 'Gemfile.lock') diff --git a/Gemfile b/Gemfile index ebee7e35f..7fb3ab91d 100644 --- a/Gemfile +++ b/Gemfile @@ -31,7 +31,6 @@ gem 'link_header' gem 'ostatus2' gem 'goldfinger' gem 'devise' -gem 'rails_autolink' gem 'doorkeeper' gem 'rabl' gem 'oj' diff --git a/Gemfile.lock b/Gemfile.lock index 0e720d7fb..12f6679c7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -268,8 +268,6 @@ GEM rails_12factor (0.0.3) rails_serve_static_assets rails_stdout_logging - rails_autolink (1.1.6) - rails (> 3.1) rails_serve_static_assets (0.0.5) rails_stdout_logging (0.0.5) railties (5.0.1) @@ -456,7 +454,6 @@ DEPENDENCIES rails (~> 5.0.1.0) rails-settings-cached rails_12factor - rails_autolink react-rails redis (~> 3.2) redis-rails diff --git a/app/lib/formatter.rb b/app/lib/formatter.rb index 04386d295..3565611bc 100644 --- a/app/lib/formatter.rb +++ b/app/lib/formatter.rb @@ -32,6 +32,7 @@ class Formatter html = encode(account.note) html = link_urls(html) + html = link_hashtags(html) html.html_safe # rubocop:disable Rails/OutputSafety end @@ -43,8 +44,8 @@ class Formatter end def link_urls(html) - auto_link(html, link: :urls, html: { rel: 'nofollow noopener', target: '_blank' }) do |text| - truncate(text.gsub(/\Ahttps?:\/\/(www\.)?/, ''), length: 30) + html.gsub(URI.regexp(%w(http https))) do |match| + link_html(match) end end @@ -63,6 +64,11 @@ class Formatter end end + def link_html(url) + link_text = truncate(url.gsub(/\Ahttps?:\/\/(www\.)?/, ''), length: 30) + "#{link_text}" + end + def hashtag_html(match) prefix, affix = match.split('#') "#{prefix}##{affix}" -- cgit