about summary refs log tree commit diff
path: root/app
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2016-12-26 21:52:03 +0100
committerEugen Rochko <eugen@zeonfederated.com>2016-12-26 21:52:03 +0100
commit2146ac91a004bad2a6c4dc1d01599a85515928f5 (patch)
tree0e65a4537c5c33f9e273d8646f1eded0eeb1e38c /app
parent3689c119f0ad8d523ab8deb3c2c8ed0a9c84db6e (diff)
Follow requests send e-mail notifications, but are excluded from notifications API
Better initial state for unlisted/nsfw toggles
Diffstat (limited to 'app')
-rw-r--r--app/assets/javascripts/components/features/compose/components/compose_form.jsx5
-rw-r--r--app/assets/javascripts/components/features/follow_requests/components/account_authorize.jsx2
-rw-r--r--app/controllers/api/v1/notifications_controller.rb2
-rw-r--r--app/mailers/notification_mailer.rb9
-rw-r--r--app/models/follow_request.rb2
-rw-r--r--app/models/notification.rb18
-rw-r--r--app/models/user.rb2
-rw-r--r--app/services/follow_service.rb7
-rw-r--r--app/services/notify_service.rb5
-rw-r--r--app/views/notification_mailer/follow_request.text.erb5
10 files changed, 45 insertions, 12 deletions
diff --git a/app/assets/javascripts/components/features/compose/components/compose_form.jsx b/app/assets/javascripts/components/features/compose/components/compose_form.jsx
index 760b0efd1..012e39c91 100644
--- a/app/assets/javascripts/components/features/compose/components/compose_form.jsx
+++ b/app/assets/javascripts/components/features/compose/components/compose_form.jsx
@@ -20,6 +20,7 @@ const messages = defineMessages({
 const ComposeForm = React.createClass({
 
   propTypes: {
+    intl: React.PropTypes.object.isRequired,
     text: React.PropTypes.string.isRequired,
     suggestion_token: React.PropTypes.string,
     suggestions: ImmutablePropTypes.list,
@@ -129,7 +130,7 @@ const ComposeForm = React.createClass({
           <span style={{ display: 'inline-block', verticalAlign: 'middle', marginBottom: '14px', marginLeft: '8px', color: '#9baec8' }}><FormattedMessage id='compose_form.private' defaultMessage='Mark as private' /></span>
         </label>
 
-        <Motion defaultStyle={{ opacity: 100, height: 39.5 }} style={{ opacity: spring(this.props.private ? 0 : 100), height: spring(this.props.private ? 0 : 39.5) }}>
+        <Motion defaultStyle={{ opacity: this.props.private ? 0 : 100, height: this.props.private ? 39.5 : 0 }} style={{ opacity: spring(this.props.private ? 0 : 100), height: spring(this.props.private ? 0 : 39.5) }}>
           {({ opacity, height }) =>
             <label style={{ display: 'block', lineHeight: '24px', verticalAlign: 'middle', height: `${height}px`, overflow: 'hidden', opacity: opacity / 100 }}>
               <Toggle checked={this.props.unlisted} onChange={this.handleChangeListability} />
@@ -138,7 +139,7 @@ const ComposeForm = React.createClass({
           }
         </Motion>
 
-        <Motion defaultStyle={{ opacity: 100, height: 39.5 }} style={{ opacity: spring(this.props.media_count === 0 ? 0 : 100), height: spring(this.props.media_count === 0 ? 0 : 39.5) }}>
+        <Motion defaultStyle={{ opacity: 0, height: 0 }} style={{ opacity: spring(this.props.media_count === 0 ? 0 : 100), height: spring(this.props.media_count === 0 ? 0 : 39.5) }}>
           {({ opacity, height }) =>
             <label style={{ display: 'block', lineHeight: '24px', verticalAlign: 'middle', height: `${height}px`, overflow: 'hidden', opacity: opacity / 100 }}>
               <Toggle checked={this.props.sensitive} onChange={this.handleChangeSensitivity} />
diff --git a/app/assets/javascripts/components/features/follow_requests/components/account_authorize.jsx b/app/assets/javascripts/components/features/follow_requests/components/account_authorize.jsx
index c16488aea..0d41d192f 100644
--- a/app/assets/javascripts/components/features/follow_requests/components/account_authorize.jsx
+++ b/app/assets/javascripts/components/features/follow_requests/components/account_authorize.jsx
@@ -35,7 +35,7 @@ const AccountAuthorize = ({ intl, account, onAuthorize, onReject }) => {
   return (
     <div>
       <div style={outerStyle}>
-        <Permalink href={account.get('url')} className='detailed-status__display-name' style={{ display: 'block', overflow: 'hidden', marginBottom: '15px' }}>
+        <Permalink href={account.get('url')} to={`/accounts/${account.get('id')}`} className='detailed-status__display-name' style={{ display: 'block', overflow: 'hidden', marginBottom: '15px' }}>
           <div style={{ float: 'left', marginRight: '10px' }}><Avatar src={account.get('avatar')} size={48} /></div>
           <DisplayName account={account} />
         </Permalink>
diff --git a/app/controllers/api/v1/notifications_controller.rb b/app/controllers/api/v1/notifications_controller.rb
index a24e0beb7..c8f162cb0 100644
--- a/app/controllers/api/v1/notifications_controller.rb
+++ b/app/controllers/api/v1/notifications_controller.rb
@@ -7,7 +7,7 @@ class Api::V1::NotificationsController < ApiController
   respond_to :json
 
   def index
-    @notifications = Notification.where(account: current_account).paginate_by_max_id(20, params[:max_id], params[:since_id])
+    @notifications = Notification.where(account: current_account).browserable.paginate_by_max_id(20, params[:max_id], params[:since_id])
     @notifications = cache_collection(@notifications, Notification)
     statuses       = @notifications.select { |n| !n.target_status.nil? }.map(&:target_status)
 
diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb
index 7b2cac7f3..a1b084682 100644
--- a/app/mailers/notification_mailer.rb
+++ b/app/mailers/notification_mailer.rb
@@ -40,4 +40,13 @@ class NotificationMailer < ApplicationMailer
       mail to: @me.user.email, subject: I18n.t('notification_mailer.reblog.subject', name: @account.acct)
     end
   end
+
+  def follow_request(recipient, notification)
+    @me      = recipient
+    @account = notification.from_account
+
+    I18n.with_locale(@me.user.locale || I18n.default_locale) do
+      mail to: @me.user.email, subject: I18n.t('notification_mailer.follow_request.subject', name: @account.acct)
+    end
+  end
 end
diff --git a/app/models/follow_request.rb b/app/models/follow_request.rb
index b46065d53..8eef3abf4 100644
--- a/app/models/follow_request.rb
+++ b/app/models/follow_request.rb
@@ -6,6 +6,8 @@ class FollowRequest < ApplicationRecord
   belongs_to :account
   belongs_to :target_account, class_name: 'Account'
 
+  has_one :notification, as: :activity, dependent: :destroy
+
   validates :account, :target_account, presence: true
   validates :account_id, uniqueness: { scope: :target_account_id }
 
diff --git a/app/models/notification.rb b/app/models/notification.rb
index 9d076ad41..c0b5c45a8 100644
--- a/app/models/notification.rb
+++ b/app/models/notification.rb
@@ -8,16 +8,18 @@ class Notification < ApplicationRecord
   belongs_to :from_account, class_name: 'Account'
   belongs_to :activity, polymorphic: true
 
-  belongs_to :mention,   foreign_type: 'Mention',   foreign_key: 'activity_id'
-  belongs_to :status,    foreign_type: 'Status',    foreign_key: 'activity_id'
-  belongs_to :follow,    foreign_type: 'Follow',    foreign_key: 'activity_id'
-  belongs_to :favourite, foreign_type: 'Favourite', foreign_key: 'activity_id'
+  belongs_to :mention,        foreign_type: 'Mention',       foreign_key: 'activity_id'
+  belongs_to :status,         foreign_type: 'Status',        foreign_key: 'activity_id'
+  belongs_to :follow,         foreign_type: 'Follow',        foreign_key: 'activity_id'
+  belongs_to :follow_request, foreign_type: 'FollowRequest', foreign_key: 'activity_id'
+  belongs_to :favourite,      foreign_type: 'Favourite',     foreign_key: 'activity_id'
 
   validates :account_id, uniqueness: { scope: [:activity_type, :activity_id] }
 
   STATUS_INCLUDES = [:account, :stream_entry, :media_attachments, :tags, mentions: :account, reblog: [:stream_entry, :account, :media_attachments, :tags, mentions: :account]].freeze
 
   scope :cache_ids, -> { select(:id, :updated_at, :activity_type, :activity_id) }
+  scope :browserable, -> { where.not(activity_type: ['FollowRequest']) }
 
   cache_associated :from_account, status: STATUS_INCLUDES, mention: [status: STATUS_INCLUDES], favourite: [:account, status: STATUS_INCLUDES], follow: :account
 
@@ -30,7 +32,7 @@ class Notification < ApplicationRecord
     when 'Status'
       :reblog
     else
-      activity_type.downcase.to_sym
+      activity_type.underscore.to_sym
     end
   end
 
@@ -43,6 +45,10 @@ class Notification < ApplicationRecord
     end
   end
 
+  def browserable?
+    type != :follow_request
+  end
+
   class << self
     def reload_stale_associations!(cached_items)
       account_ids = cached_items.map(&:from_account_id).uniq
@@ -61,7 +67,7 @@ class Notification < ApplicationRecord
 
   def set_from_account
     case activity_type
-    when 'Status', 'Follow', 'Favourite'
+    when 'Status', 'Follow', 'Favourite', 'FollowRequest'
       self.from_account_id = activity(false)&.account_id
     when 'Mention'
       self.from_account_id = activity(false)&.status&.account_id
diff --git a/app/models/user.rb b/app/models/user.rb
index 3fc028a6a..d5a52da06 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -15,7 +15,7 @@ class User < ApplicationRecord
   scope :admins,   -> { where(admin: true) }
 
   has_settings do |s|
-    s.key :notification_emails, defaults: { follow: false, reblog: false, favourite: false, mention: false }
+    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
 
diff --git a/app/services/follow_service.rb b/app/services/follow_service.rb
index a73ec344d..555f01b6d 100644
--- a/app/services/follow_service.rb
+++ b/app/services/follow_service.rb
@@ -20,7 +20,12 @@ class FollowService < BaseService
   private
 
   def request_follow(source_account, target_account)
-    FollowRequest.create!(account: source_account, target_account: target_account)
+    return unless target_account.local?
+
+    follow_request = FollowRequest.create!(account: source_account, target_account: target_account)
+    NotifyService.new.call(target_account, follow_request)
+
+    follow_request
   end
 
   def direct_follow(source_account, target_account)
diff --git a/app/services/notify_service.rb b/app/services/notify_service.rb
index 8263c4376..2fb1d3919 100644
--- a/app/services/notify_service.rb
+++ b/app/services/notify_service.rb
@@ -32,6 +32,10 @@ class NotifyService < BaseService
     false
   end
 
+  def blocked_follow_request?
+    false
+  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
@@ -45,6 +49,7 @@ class NotifyService < BaseService
 
   def create_notification
     @notification.save!
+    return unless @notification.browserable?
     FeedManager.instance.broadcast(@recipient.id, type: 'notification', message: FeedManager.instance.inline_render(@recipient, 'api/v1/notifications/show', @notification))
   end
 
diff --git a/app/views/notification_mailer/follow_request.text.erb b/app/views/notification_mailer/follow_request.text.erb
new file mode 100644
index 000000000..c0d38ec67
--- /dev/null
+++ b/app/views/notification_mailer/follow_request.text.erb
@@ -0,0 +1,5 @@
+<%= display_name(@me) %>,
+
+<%= t('notification_mailer.follow_request.body', name: @account.acct) %>
+
+<%= web_url("follow_requests") %>