Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Account temporary muting.
  • Loading branch information
aquarla committed Mar 31, 2020
1 parent 0c53bcf commit a69f523
Show file tree
Hide file tree
Showing 18 changed files with 144 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app/controllers/api/v1/accounts_controller.rb
Expand Up @@ -44,7 +44,7 @@ def block
end

def mute
MuteService.new.call(current_user.account, @account, notifications: truthy_param?(:notifications))
MuteService.new.call(current_user.account, @account, notifications: truthy_param?(:notifications), duration: (params[:duration] || 0))
render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
end

Expand Down
4 changes: 2 additions & 2 deletions app/javascript/mastodon/actions/accounts.js
Expand Up @@ -290,11 +290,11 @@ export function unblockAccountFail(error) {
};


export function muteAccount(id, notifications) {
export function muteAccount(id, notifications, duration=0) {
return (dispatch, getState) => {
dispatch(muteAccountRequest(id));

api(getState).post(`/api/v1/accounts/${id}/mute`, { notifications }).then(response => {
api(getState).post(`/api/v1/accounts/${id}/mute`, { notifications, duration }).then(response => {
// Pass in entire statuses map so we can use it to filter stuff in different parts of the reducers
dispatch(muteAccountSuccess(response.data, getState().get('statuses')));
}).catch(error => {
Expand Down
10 changes: 10 additions & 0 deletions app/javascript/mastodon/actions/mutes.js
Expand Up @@ -13,6 +13,7 @@ export const MUTES_EXPAND_FAIL = 'MUTES_EXPAND_FAIL';

export const MUTES_INIT_MODAL = 'MUTES_INIT_MODAL';
export const MUTES_TOGGLE_HIDE_NOTIFICATIONS = 'MUTES_TOGGLE_HIDE_NOTIFICATIONS';
export const MUTES_CHANGE_DURATION = 'MUTES_CHANGE_DURATION';

export function fetchMutes() {
return (dispatch, getState) => {
Expand Down Expand Up @@ -104,3 +105,12 @@ export function toggleHideNotifications() {
dispatch({ type: MUTES_TOGGLE_HIDE_NOTIFICATIONS });
};
}

export function changeMuteDuration(duration) {
return dispatch => {
dispatch({
type: MUTES_CHANGE_DURATION,
duration,
});
};
}
7 changes: 7 additions & 0 deletions app/javascript/mastodon/components/account.js
Expand Up @@ -8,6 +8,7 @@ import IconButton from './icon_button';
import { defineMessages, injectIntl } from 'react-intl';
import ImmutablePureComponent from 'react-immutable-pure-component';
import { me } from '../initial_state';
import RelativeTimestamp from './relative_timestamp';

const messages = defineMessages({
follow: { id: 'account.follow', defaultMessage: 'Follow' },
Expand Down Expand Up @@ -107,11 +108,17 @@ class Account extends ImmutablePureComponent {
}
}

let mute_expires_at;
if (account.get('mute_expires_at')) {
mute_expires_at = <div><RelativeTimestamp timestamp={account.get('mute_expires_at')} futureDate /></div>;
}

return (
<div className='account'>
<div className='account__wrapper'>
<Permalink key={account.get('id')} className='account__display-name' title={account.get('acct')} href={account.get('url')} to={`/accounts/${account.get('id')}`}>
<div className='account__avatar-wrapper'><Avatar account={account} size={36} /></div>
{mute_expires_at}
<DisplayName account={account} />
</Permalink>

Expand Down
41 changes: 35 additions & 6 deletions app/javascript/mastodon/features/ui/components/mute_modal.js
@@ -1,25 +1,31 @@
import React from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { injectIntl, FormattedMessage } from 'react-intl';
import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
import Toggle from 'react-toggle';
import Button from '../../../components/button';
import { closeModal } from '../../../actions/modal';
import { muteAccount } from '../../../actions/accounts';
import { toggleHideNotifications } from '../../../actions/mutes';
import { toggleHideNotifications, changeMuteDuration } from '../../../actions/mutes';

const messages = defineMessages({
minutes: { id: 'intervals.full.minutes', defaultMessage: '{number, plural, one {# minute} other {# minutes}}' },
hours: { id: 'intervals.full.hours', defaultMessage: '{number, plural, one {# hour} other {# hours}}' },
days: { id: 'intervals.full.days', defaultMessage: '{number, plural, one {# day} other {# days}}' },
});

const mapStateToProps = state => {
return {
account: state.getIn(['mutes', 'new', 'account']),
notifications: state.getIn(['mutes', 'new', 'notifications']),
muteDuration: state.getIn(['mutes', 'new', 'duration']),
};
};

const mapDispatchToProps = dispatch => {
return {
onConfirm(account, notifications) {
dispatch(muteAccount(account.get('id'), notifications));
onConfirm(account, notifications, muteDuration) {
dispatch(muteAccount(account.get('id'), notifications, muteDuration));
},

onClose() {
Expand All @@ -29,6 +35,10 @@ const mapDispatchToProps = dispatch => {
onToggleNotifications() {
dispatch(toggleHideNotifications());
},

onChangeMuteDuration(e) {
dispatch(changeMuteDuration(e.target.value));
},
};
};

Expand All @@ -43,6 +53,8 @@ class MuteModal extends React.PureComponent {
onConfirm: PropTypes.func.isRequired,
onToggleNotifications: PropTypes.func.isRequired,
intl: PropTypes.object.isRequired,
muteDuration: PropTypes.number.isRequired,
onChangeMuteDuration: PropTypes.func.isRequired,
};

componentDidMount() {
Expand All @@ -51,7 +63,7 @@ class MuteModal extends React.PureComponent {

handleClick = () => {
this.props.onClose();
this.props.onConfirm(this.props.account, this.props.notifications);
this.props.onConfirm(this.props.account, this.props.notifications, this.props.muteDuration);
}

handleCancel = () => {
Expand All @@ -66,8 +78,12 @@ class MuteModal extends React.PureComponent {
this.props.onToggleNotifications();
}

changeMuteDuration = (e) => {
this.props.onChangeMuteDuration(e);
}

render () {
const { account, notifications } = this.props;
const { account, notifications, muteDuration, intl } = this.props;

return (
<div className='modal-root__modal mute-modal'>
Expand All @@ -91,6 +107,19 @@ class MuteModal extends React.PureComponent {
<FormattedMessage id='mute_modal.hide_notifications' defaultMessage='Hide notifications from this user?' />
</label>
</div>
<div>
<span><FormattedMessage id='mute_modal.duration' defaultMessage='Duration' />: </span>
<select value={muteDuration} onChange={this.changeMuteDuration}>
<option value={0}>{intl.formatMessage({id: "mute_modal.unlimited"})}</option>
<option value={300}>{intl.formatMessage(messages.minutes, { number: 5 })}</option>
<option value={1800}>{intl.formatMessage(messages.minutes, { number: 30 })}</option>
<option value={3600}>{intl.formatMessage(messages.hours, { number: 1 })}</option>
<option value={21600}>{intl.formatMessage(messages.hours, { number: 6 })}</option>
<option value={86400}>{intl.formatMessage(messages.days, { number: 1 })}</option>
<option value={259200}>{intl.formatMessage(messages.days, { number: 3 })}</option>
<option value={604800}>{intl.formatMessage(messages.days, { number: 7 })}</option>
</select>
</div>
</div>

<div className='mute-modal__action-bar'>
Expand Down
2 changes: 2 additions & 0 deletions app/javascript/mastodon/locales/en.json
Expand Up @@ -258,6 +258,8 @@
"missing_indicator.label": "Not found",
"missing_indicator.sublabel": "This resource could not be found",
"mute_modal.hide_notifications": "Hide notifications from this user?",
"mute_modal.duration": "Duration",
"mute_modal.unlimited": "Unlimited",
"navigation_bar.apps": "Mobile apps",
"navigation_bar.blocks": "Blocked users",
"navigation_bar.bookmarks": "Bookmarks",
Expand Down
2 changes: 2 additions & 0 deletions app/javascript/mastodon/locales/ja.json
Expand Up @@ -259,6 +259,8 @@
"missing_indicator.label": "見つかりません",
"missing_indicator.sublabel": "見つかりませんでした",
"mute_modal.hide_notifications": "このユーザーからの通知を隠しますか?",
"mute_modal.duration": "ミュートする期間",
"mute_modal.unlimited": "無期限",
"navigation_bar.apps": "アプリ",
"navigation_bar.blocks": "ブロックしたユーザー",
"navigation_bar.bookmarks": "ブックマーク",
Expand Down
4 changes: 4 additions & 0 deletions app/javascript/mastodon/reducers/mutes.js
Expand Up @@ -3,12 +3,14 @@ import Immutable from 'immutable';
import {
MUTES_INIT_MODAL,
MUTES_TOGGLE_HIDE_NOTIFICATIONS,
MUTES_CHANGE_DURATION,
} from '../actions/mutes';

const initialState = Immutable.Map({
new: Immutable.Map({
account: null,
notifications: true,
duration: 0,
}),
});

Expand All @@ -21,6 +23,8 @@ export default function mutes(state = initialState, action) {
});
case MUTES_TOGGLE_HIDE_NOTIFICATIONS:
return state.updateIn(['new', 'notifications'], (old) => !old);
case MUTES_CHANGE_DURATION:
return state.updateIn(['new', 'duration'], (old) => Number(action.duration));
default:
return state;
}
Expand Down
5 changes: 5 additions & 0 deletions app/javascript/styles/mastodon-light/diff.scss
Expand Up @@ -774,3 +774,8 @@ html {
.audio-player .video-player__time-total {
color: $primary-text-color;
}

.mute-modal select {
border: 1px solid lighten($ui-base-color, 8%);
background: $simple-background-color url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 14.933 18.467' height='19.698' width='15.929'><path d='M3.467 14.967l-3.393-3.5H14.86l-3.392 3.5c-1.866 1.925-3.666 3.5-4 3.5-.335 0-2.135-1.575-4-3.5zm.266-11.234L7.467 0 11.2 3.733l3.733 3.734H0l3.733-3.734z' fill='#{hex-color(lighten($ui-base-color, 8%))}'/></svg>") no-repeat right 8px center / auto 16px;
}
16 changes: 16 additions & 0 deletions app/javascript/styles/mastodon/components.scss
Expand Up @@ -4994,6 +4994,22 @@ a.status-card.compact:hover {
}
}
}

select {
appearance: none;
box-sizing: border-box;
font-size: 14px;
color: $inverted-text-color;
display: inline-block;
width: auto;
outline: 0;
font-family: inherit;
background: $simple-background-color url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 14.933 18.467' height='19.698' width='15.929'><path d='M3.467 14.967l-3.393-3.5H14.86l-3.392 3.5c-1.866 1.925-3.666 3.5-4 3.5-.335 0-2.135-1.575-4-3.5zm.266-11.234L7.467 0 11.2 3.733l3.733 3.734H0l3.733-3.734z' fill='#{hex-color(darken($simple-background-color, 14%))}'/></svg>") no-repeat right 8px center / auto 16px;
border: 1px solid darken($simple-background-color, 14%);
border-radius: 4px;
padding: 6px 10px;
padding-right: 30px;
}
}

.confirmation-modal__container,
Expand Down
12 changes: 10 additions & 2 deletions app/models/concerns/account_interactions.rb
Expand Up @@ -78,6 +78,7 @@ def follow_mapping(query, field)
has_many :blocked_by, -> { order('blocks.id desc') }, through: :blocked_by_relationships, source: :account

# Mute relationships
has_many :mutes
has_many :mute_relationships, class_name: 'Mute', foreign_key: 'account_id', dependent: :destroy
has_many :muting, -> { order('mutes.id desc') }, through: :mute_relationships, source: :target_account
has_many :muted_by_relationships, class_name: 'Mute', foreign_key: :target_account_id, dependent: :destroy
Expand Down Expand Up @@ -105,9 +106,16 @@ def block!(other_account, uri: nil)
.find_or_create_by!(target_account: other_account)
end

def mute!(other_account, notifications: nil)
def mute!(other_account, notifications: nil, duration: 0)
notifications = true if notifications.nil?
mute = mute_relationships.create_with(hide_notifications: notifications).find_or_create_by!(target_account: other_account)
mute = mutes.find_by(target_account: other_account)
expires_at = (duration.zero? ? nil : (Time.current + duration.seconds))
if mute
mute.update!(expires_at: expires_at)
else
mute = mute_relationships.create_with(hide_notifications: notifications, expires_at: expires_at).find_or_create_by!(target_account: other_account)
end

remove_potential_friendship(other_account)

# When toggling a mute between hiding and allowing notifications, the mute will already exist, so the find_or_create_by! call will return the existing Mute without updating the hide_notifications attribute. Therefore, we check that hide_notifications? is what we want and set it if it isn't.
Expand Down
8 changes: 8 additions & 0 deletions app/models/mute.rb
Expand Up @@ -9,6 +9,8 @@
# account_id :bigint(8) not null
# target_account_id :bigint(8) not null
# hide_notifications :boolean default(TRUE), not null
# expires_at :datetime
# unmute_jid :string
#

class Mute < ApplicationRecord
Expand All @@ -22,9 +24,15 @@ class Mute < ApplicationRecord

after_commit :remove_blocking_cache

before_destroy :delete_unmute_job

private

def remove_blocking_cache
Rails.cache.delete("exclude_account_ids_for:#{account_id}")
end

def delete_unmute_job
Sidekiq::ScheduledSet.new.find_job(unmute_jid)&.delete if unmute_jid
end
end
10 changes: 10 additions & 0 deletions app/serializers/rest/account_serializer.rb
Expand Up @@ -7,6 +7,8 @@ class REST::AccountSerializer < ActiveModel::Serializer
:note, :url, :avatar, :avatar_static, :header, :header_static,
:followers_count, :following_count, :statuses_count, :last_status_at

attribute :mute_expires_at, if: :current_user?

has_one :moved_to_account, key: :moved, serializer: REST::AccountSerializer, if: :moved_and_not_nested?
has_many :emojis, serializer: REST::CustomEmojiSerializer

Expand All @@ -20,6 +22,10 @@ def value

has_many :fields

def current_user?
defined?(current_user) && !current_user.nil?
end

def id
object.id.to_s
end
Expand Down Expand Up @@ -59,4 +65,8 @@ def moved_and_not_nested?
def last_status_at
object.last_status_at&.to_date&.iso8601
end

def mute_expires_at
current_user.account&.mute_relationships&.find_by(target_account_id: object.id)&.expires_at
end
end
9 changes: 7 additions & 2 deletions app/services/mute_service.rb
@@ -1,17 +1,22 @@
# frozen_string_literal: true

class MuteService < BaseService
def call(account, target_account, notifications: nil)
def call(account, target_account, notifications: nil, duration: 0)
return if account.id == target_account.id

mute = account.mute!(target_account, notifications: notifications)
mute = account.mute!(target_account, notifications: notifications, duration: duration)

if mute.hide_notifications?
BlockWorker.perform_async(account.id, target_account.id)
else
MuteWorker.perform_async(account.id, target_account.id)
end

if duration != 0
jid = DeleteMuteWorker.perform_at(duration.seconds, account.id, target_account.id)
mute.update!(unmute_jid: jid)
end

mute
end
end
13 changes: 13 additions & 0 deletions app/workers/delete_mute_worker.rb
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class DeleteMuteWorker
include Sidekiq::Worker

def perform(account_id, target_account_id)
account = Account.find(account_id)
target_account = Account.find(target_account_id)
if account && target_account
UnmuteService.new.call(account, target_account)
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20200317021758_add_expires_at_to_mutes.rb
@@ -0,0 +1,5 @@
class AddExpiresAtToMutes < ActiveRecord::Migration[5.2]
def change
add_column :mutes, :expires_at, :datetime
end
end
5 changes: 5 additions & 0 deletions db/migrate/20200318064228_add_unmute_jid_to_mute.rb
@@ -0,0 +1,5 @@
class AddUnmuteJidToMute < ActiveRecord::Migration[5.2]
def change
add_column :mutes, :unmute_jid, :string
end
end
2 changes: 2 additions & 0 deletions db/schema.rb
Expand Up @@ -483,6 +483,8 @@
t.boolean "hide_notifications", default: true, null: false
t.bigint "account_id", null: false
t.bigint "target_account_id", null: false
t.datetime "expires_at"
t.string "unmute_jid"
t.index ["account_id", "target_account_id"], name: "index_mutes_on_account_id_and_target_account_id", unique: true
t.index ["target_account_id"], name: "index_mutes_on_target_account_id"
end
Expand Down

0 comments on commit a69f523

Please sign in to comment.