Skip to content
This repository was archived by the owner on Dec 21, 2023. It is now read-only.

Commit 9676175

Browse files
authored
Add duration parameter to muting. (mastodon#13831)
* Adding duration to muting. * Remove useless checks
1 parent f54ca3d commit 9676175

File tree

18 files changed

+124
-14
lines changed

18 files changed

+124
-14
lines changed

app/controllers/api/v1/accounts_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def block
4242
end
4343

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

app/controllers/api/v1/mutes_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Api::V1::MutesController < Api::BaseController
77

88
def index
99
@accounts = load_accounts
10-
render json: @accounts, each_serializer: REST::AccountSerializer
10+
render json: @accounts, each_serializer: REST::MutedAccountSerializer
1111
end
1212

1313
private

app/javascript/mastodon/actions/accounts.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,11 @@ export function unblockAccountFail(error) {
257257
};
258258

259259

260-
export function muteAccount(id, notifications) {
260+
export function muteAccount(id, notifications, duration=0) {
261261
return (dispatch, getState) => {
262262
dispatch(muteAccountRequest(id));
263263

264-
api(getState).post(`/api/v1/accounts/${id}/mute`, { notifications }).then(response => {
264+
api(getState).post(`/api/v1/accounts/${id}/mute`, { notifications, duration }).then(response => {
265265
// Pass in entire statuses map so we can use it to filter stuff in different parts of the reducers
266266
dispatch(muteAccountSuccess(response.data, getState().get('statuses')));
267267
}).catch(error => {

app/javascript/mastodon/actions/mutes.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const MUTES_EXPAND_FAIL = 'MUTES_EXPAND_FAIL';
1313

1414
export const MUTES_INIT_MODAL = 'MUTES_INIT_MODAL';
1515
export const MUTES_TOGGLE_HIDE_NOTIFICATIONS = 'MUTES_TOGGLE_HIDE_NOTIFICATIONS';
16+
export const MUTES_CHANGE_DURATION = 'MUTES_CHANGE_DURATION';
1617

1718
export function fetchMutes() {
1819
return (dispatch, getState) => {
@@ -104,3 +105,12 @@ export function toggleHideNotifications() {
104105
dispatch({ type: MUTES_TOGGLE_HIDE_NOTIFICATIONS });
105106
};
106107
}
108+
109+
export function changeMuteDuration(duration) {
110+
return dispatch => {
111+
dispatch({
112+
type: MUTES_CHANGE_DURATION,
113+
duration,
114+
});
115+
};
116+
}

app/javascript/mastodon/components/account.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import IconButton from './icon_button';
88
import { defineMessages, injectIntl } from 'react-intl';
99
import ImmutablePureComponent from 'react-immutable-pure-component';
1010
import { me } from '../initial_state';
11+
import RelativeTimestamp from './relative_timestamp';
1112

1213
const messages = defineMessages({
1314
follow: { id: 'account.follow', defaultMessage: 'Follow' },
@@ -107,11 +108,17 @@ class Account extends ImmutablePureComponent {
107108
}
108109
}
109110

111+
let mute_expires_at;
112+
if (account.get('mute_expires_at')) {
113+
mute_expires_at = <div><RelativeTimestamp timestamp={account.get('mute_expires_at')} futureDate /></div>;
114+
}
115+
110116
return (
111117
<div className='account'>
112118
<div className='account__wrapper'>
113119
<Permalink key={account.get('id')} className='account__display-name' title={account.get('acct')} href={account.get('url')} to={`/accounts/${account.get('id')}`}>
114120
<div className='account__avatar-wrapper'><Avatar account={account} size={36} /></div>
121+
{mute_expires_at}
115122
<DisplayName account={account} />
116123
</Permalink>
117124

app/javascript/mastodon/features/ui/components/mute_modal.js

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
import React from 'react';
22
import { connect } from 'react-redux';
33
import PropTypes from 'prop-types';
4-
import { injectIntl, FormattedMessage } from 'react-intl';
4+
import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
55
import Toggle from 'react-toggle';
66
import Button from '../../../components/button';
77
import { closeModal } from '../../../actions/modal';
88
import { muteAccount } from '../../../actions/accounts';
9-
import { toggleHideNotifications } from '../../../actions/mutes';
9+
import { toggleHideNotifications, changeMuteDuration } from '../../../actions/mutes';
1010

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

1217
const mapStateToProps = state => {
1318
return {
1419
account: state.getIn(['mutes', 'new', 'account']),
1520
notifications: state.getIn(['mutes', 'new', 'notifications']),
21+
muteDuration: state.getIn(['mutes', 'new', 'duration']),
1622
};
1723
};
1824

1925
const mapDispatchToProps = dispatch => {
2026
return {
21-
onConfirm(account, notifications) {
22-
dispatch(muteAccount(account.get('id'), notifications));
27+
onConfirm(account, notifications, muteDuration) {
28+
dispatch(muteAccount(account.get('id'), notifications, muteDuration));
2329
},
2430

2531
onClose() {
@@ -29,6 +35,10 @@ const mapDispatchToProps = dispatch => {
2935
onToggleNotifications() {
3036
dispatch(toggleHideNotifications());
3137
},
38+
39+
onChangeMuteDuration(e) {
40+
dispatch(changeMuteDuration(e.target.value));
41+
},
3242
};
3343
};
3444

@@ -43,6 +53,8 @@ class MuteModal extends React.PureComponent {
4353
onConfirm: PropTypes.func.isRequired,
4454
onToggleNotifications: PropTypes.func.isRequired,
4555
intl: PropTypes.object.isRequired,
56+
muteDuration: PropTypes.number.isRequired,
57+
onChangeMuteDuration: PropTypes.func.isRequired,
4658
};
4759

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

5264
handleClick = () => {
5365
this.props.onClose();
54-
this.props.onConfirm(this.props.account, this.props.notifications);
66+
this.props.onConfirm(this.props.account, this.props.notifications, this.props.muteDuration);
5567
}
5668

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

81+
changeMuteDuration = (e) => {
82+
this.props.onChangeMuteDuration(e);
83+
}
84+
6985
render () {
70-
const { account, notifications } = this.props;
86+
const { account, notifications, muteDuration, intl } = this.props;
7187

7288
return (
7389
<div className='modal-root__modal mute-modal'>
@@ -91,6 +107,21 @@ class MuteModal extends React.PureComponent {
91107
<FormattedMessage id='mute_modal.hide_notifications' defaultMessage='Hide notifications from this user?' />
92108
</label>
93109
</div>
110+
<div>
111+
<span><FormattedMessage id='mute_modal.duration' defaultMessage='Duration' />: </span>
112+
113+
{/* eslint-disable-next-line jsx-a11y/no-onchange */}
114+
<select value={muteDuration} onChange={this.changeMuteDuration}>
115+
<option value={0}>{intl.formatMessage({ id: 'mute_modal.indefinite' })}</option>
116+
<option value={300}>{intl.formatMessage(messages.minutes, { number: 5 })}</option>
117+
<option value={1800}>{intl.formatMessage(messages.minutes, { number: 30 })}</option>
118+
<option value={3600}>{intl.formatMessage(messages.hours, { number: 1 })}</option>
119+
<option value={21600}>{intl.formatMessage(messages.hours, { number: 6 })}</option>
120+
<option value={86400}>{intl.formatMessage(messages.days, { number: 1 })}</option>
121+
<option value={259200}>{intl.formatMessage(messages.days, { number: 3 })}</option>
122+
<option value={604800}>{intl.formatMessage(messages.days, { number: 7 })}</option>
123+
</select>
124+
</div>
94125
</div>
95126

96127
<div className='mute-modal__action-bar'>

app/javascript/mastodon/locales/en.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@
276276
"missing_indicator.label": "Not found",
277277
"missing_indicator.sublabel": "This resource could not be found",
278278
"mute_modal.hide_notifications": "Hide notifications from this user?",
279+
"mute_modal.duration": "Duration",
280+
"mute_modal.indefinite": "Indefinite",
279281
"navigation_bar.apps": "Mobile apps",
280282
"navigation_bar.blocks": "Blocked users",
281283
"navigation_bar.bookmarks": "Bookmarks",

app/javascript/mastodon/locales/ja.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@
268268
"missing_indicator.label": "見つかりません",
269269
"missing_indicator.sublabel": "見つかりませんでした",
270270
"mute_modal.hide_notifications": "このユーザーからの通知を隠しますか?",
271+
"mute_modal.duration": "ミュートする期間",
272+
"mute_modal.indefinite": "無期限",
271273
"navigation_bar.apps": "アプリ",
272274
"navigation_bar.blocks": "ブロックしたユーザー",
273275
"navigation_bar.bookmarks": "ブックマーク",

app/javascript/mastodon/reducers/mutes.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import Immutable from 'immutable';
33
import {
44
MUTES_INIT_MODAL,
55
MUTES_TOGGLE_HIDE_NOTIFICATIONS,
6+
MUTES_CHANGE_DURATION,
67
} from '../actions/mutes';
78

89
const initialState = Immutable.Map({
910
new: Immutable.Map({
1011
account: null,
1112
notifications: true,
13+
duration: 0,
1214
}),
1315
});
1416

@@ -21,6 +23,8 @@ export default function mutes(state = initialState, action) {
2123
});
2224
case MUTES_TOGGLE_HIDE_NOTIFICATIONS:
2325
return state.updateIn(['new', 'notifications'], (old) => !old);
26+
case MUTES_CHANGE_DURATION:
27+
return state.setIn(['new', 'duration'], Number(action.duration));
2428
default:
2529
return state;
2630
}

app/javascript/styles/mastodon-light/diff.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,3 +759,8 @@ html {
759759
.compose-form .compose-form__warning {
760760
box-shadow: none;
761761
}
762+
763+
.mute-modal select {
764+
border: 1px solid lighten($ui-base-color, 8%);
765+
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;
766+
}

app/javascript/styles/mastodon/components.scss

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5074,6 +5074,22 @@ a.status-card.compact:hover {
50745074
}
50755075
}
50765076
}
5077+
5078+
select {
5079+
appearance: none;
5080+
box-sizing: border-box;
5081+
font-size: 14px;
5082+
color: $inverted-text-color;
5083+
display: inline-block;
5084+
width: auto;
5085+
outline: 0;
5086+
font-family: inherit;
5087+
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;
5088+
border: 1px solid darken($simple-background-color, 14%);
5089+
border-radius: 4px;
5090+
padding: 6px 10px;
5091+
padding-right: 30px;
5092+
}
50775093
}
50785094

50795095
.confirmation-modal__container,

app/models/concerns/account_interactions.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,12 @@ def block!(other_account, uri: nil)
131131
.find_or_create_by!(target_account: other_account)
132132
end
133133

134-
def mute!(other_account, notifications: nil)
134+
def mute!(other_account, notifications: nil, duration: 0)
135135
notifications = true if notifications.nil?
136-
mute = mute_relationships.create_with(hide_notifications: notifications).find_or_create_by!(target_account: other_account)
136+
mute = mute_relationships.create_with(hide_notifications: notifications).find_or_initialize_by(target_account: other_account)
137+
mute.expires_in = duration.zero? ? nil : duration
138+
mute.save!
139+
137140
remove_potential_friendship(other_account)
138141

139142
# 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.

app/models/mute.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
# account_id :bigint(8) not null
1010
# target_account_id :bigint(8) not null
1111
# hide_notifications :boolean default(TRUE), not null
12+
# expires_at :datetime
1213
#
1314

1415
class Mute < ApplicationRecord
1516
include Paginable
1617
include RelationshipCacheable
18+
include Expireable
1719

1820
belongs_to :account
1921
belongs_to :target_account, class_name: 'Account'
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
3+
class REST::MutedAccountSerializer < REST::AccountSerializer
4+
attribute :mute_expires_at
5+
6+
def mute_expires_at
7+
mute = current_user.account.mute_relationships.find_by(target_account_id: object.id)
8+
mute && !mute.expired? ? mute.expires_at : nil
9+
end
10+
end

app/services/mute_service.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
# frozen_string_literal: true
22

33
class MuteService < BaseService
4-
def call(account, target_account, notifications: nil)
4+
def call(account, target_account, notifications: nil, duration: 0)
55
return if account.id == target_account.id
66

7-
mute = account.mute!(target_account, notifications: notifications)
7+
mute = account.mute!(target_account, notifications: notifications, duration: duration)
88

99
if mute.hide_notifications?
1010
BlockWorker.perform_async(account.id, target_account.id)
1111
else
1212
MuteWorker.perform_async(account.id, target_account.id)
1313
end
1414

15+
DeleteMuteWorker.perform_at(duration.seconds, mute.id) if duration != 0
16+
1517
mute
1618
end
1719
end

app/workers/delete_mute_worker.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
3+
class DeleteMuteWorker
4+
include Sidekiq::Worker
5+
6+
def perform(mute_id)
7+
mute = Mute.find_by(id: mute_id)
8+
UnmuteService.new.call(mute.account, mute.target_account) if mute&.expired?
9+
end
10+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddExpiresAtToMutes < ActiveRecord::Migration[5.2]
2+
def change
3+
add_column :mutes, :expires_at, :datetime
4+
end
5+
end

db/schema.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@
545545
t.boolean "hide_notifications", default: true, null: false
546546
t.bigint "account_id", null: false
547547
t.bigint "target_account_id", null: false
548+
t.datetime "expires_at"
548549
t.index ["account_id", "target_account_id"], name: "index_mutes_on_account_id_and_target_account_id", unique: true
549550
t.index ["target_account_id"], name: "index_mutes_on_target_account_id"
550551
end

0 commit comments

Comments
 (0)