-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #222 from Retrospring/feature/muting
- Loading branch information
Showing
18 changed files
with
337 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
class Ajax::MuteRuleController < AjaxController | ||
def create | ||
params.require :muted_phrase | ||
|
||
unless user_signed_in? | ||
@response[:status] = :noauth | ||
@response[:message] = I18n.t('messages.noauth') | ||
return | ||
end | ||
|
||
rule = MuteRule.create!(user: current_user, muted_phrase: params[:muted_phrase]) | ||
@response[:status] = :okay | ||
@response[:success] = true | ||
@response[:message] = "Rule added successfully." | ||
@response[:id] = rule.id | ||
end | ||
|
||
def update | ||
params.require :id | ||
params.require :muted_phrase | ||
|
||
unless user_signed_in? | ||
@response[:status] = :noauth | ||
@response[:message] = I18n.t('messages.noauth') | ||
return | ||
end | ||
|
||
rule = MuteRule.find(params[:id]) | ||
|
||
if rule.user_id != current_user.id | ||
@response[:status] = :nopriv | ||
@response[:message] = "Can't edit other people's rules" | ||
return | ||
end | ||
|
||
rule.muted_phrase = params[:muted_phrase] | ||
rule.save! | ||
|
||
@response[:status] = :okay | ||
@response[:message] = "Rule updated successfully." | ||
@response[:success] = true | ||
end | ||
|
||
def destroy | ||
params.require :id | ||
|
||
unless user_signed_in? | ||
@response[:status] = :noauth | ||
@response[:message] = I18n.t('messages.noauth') | ||
return | ||
end | ||
|
||
rule = MuteRule.find(params[:id]) | ||
|
||
if rule.user_id != current_user.id | ||
@response[:status] = :nopriv | ||
@response[:message] = "Can't edit other people's rules" | ||
return | ||
end | ||
|
||
rule.destroy! | ||
|
||
@response[:status] = :okay | ||
@response[:message] = "Rule deleted successfully." | ||
@response[:success] = true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import {createDeleteEvent, createSubmitEvent} from "retrospring/features/settings/mute"; | ||
|
||
export default (): void => { | ||
const submit: HTMLButtonElement = document.getElementById('new-rule-submit') as HTMLButtonElement; | ||
if (submit.classList.contains('js-initialized')) return; | ||
|
||
const rulesList = document.querySelector<HTMLDivElement>('.js-rules-list'); | ||
rulesList.querySelectorAll<HTMLDivElement>('.form-group:not(.js-initalized)').forEach(entry => { | ||
const button = entry.querySelector('button') | ||
button.onclick = createDeleteEvent(entry, button) | ||
}); | ||
const textEntry: HTMLButtonElement = document.getElementById('new-rule-text') as HTMLButtonElement; | ||
const template: HTMLTemplateElement = document.getElementById('rule-template') as HTMLTemplateElement; | ||
|
||
submit.form.onsubmit = createSubmitEvent(submit, rulesList, textEntry, template) | ||
|
||
submit.classList.add('js-initialized') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import Rails from '@rails/ujs'; | ||
|
||
export function createSubmitEvent( | ||
submit: HTMLButtonElement, | ||
rulesList: HTMLDivElement, | ||
textEntry: HTMLButtonElement, | ||
template: HTMLTemplateElement | ||
): (event: Event) => void { | ||
return (event) => { | ||
event.preventDefault(); | ||
submit.disabled = true; | ||
|
||
Rails.ajax({ | ||
url: '/ajax/mute', | ||
type: 'POST', | ||
dataType: 'json', | ||
data: new URLSearchParams({muted_phrase: textEntry.value}).toString(), | ||
success: (data) => { | ||
submit.disabled = false; | ||
if (!data.success) return; | ||
|
||
const newEntryFragment = template.content.cloneNode(true) as Element; | ||
newEntryFragment.querySelector<HTMLInputElement>('input').value = textEntry.value; | ||
const newDeleteButton = newEntryFragment.querySelector<HTMLButtonElement>('button') | ||
newDeleteButton.dataset.id = String(data.id); | ||
newDeleteButton.onclick = createDeleteEvent( | ||
newEntryFragment.querySelector('.form-group'), | ||
newDeleteButton | ||
) | ||
|
||
rulesList.appendChild(newEntryFragment) | ||
|
||
textEntry.value = '' | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
export function createDeleteEvent( | ||
entry: HTMLDivElement, | ||
deleteButton: HTMLButtonElement | ||
): () => void { | ||
return () => { | ||
deleteButton.disabled = true; | ||
|
||
Rails.ajax({ | ||
url: '/ajax/mute/' + deleteButton.dataset.id, | ||
type: 'DELETE', | ||
dataType: 'json', | ||
success: (data) => { | ||
if (data.success) { | ||
entry.parentElement.removeChild(entry) | ||
} else { | ||
deleteButton.disabled = false; | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class MuteRule < ApplicationRecord | ||
belongs_to :user | ||
|
||
def applies_to?(post) | ||
!!(post.content =~ /\b#{Regexp.escape(muted_phrase)}\b/i) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
.card | ||
.card-body | ||
%h2 Muted words | ||
%p Muting words (or longer phrases) will prevent questions containing those to appear in your inbox. | ||
%p Note: Filtered questions are discarded completely from your inbox, and won't reappear if you remove a filter later on. | ||
.js-rules-list | ||
- @rules.each do |rule| | ||
.form-group | ||
.input-group | ||
%input.form-control{ disabled: true, value: rule.muted_phrase } | ||
.input-group-append | ||
%button.btn.btn-danger{ type: 'button', data: { id: rule.id } } Remove | ||
.form-group | ||
%form | ||
.input-group | ||
%input.form-control#new-rule-text{ placeholder: 'Add a new muted word...' } | ||
.input-group-append | ||
%button.btn.btn-primary#new-rule-submit{ type: 'submit' } Add | ||
%template#rule-template | ||
.form-group | ||
.input-group | ||
%input.form-control{ disabled: true } | ||
.input-group-append | ||
%button.btn.btn-danger{ type: 'button' } Remove |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
= render 'settings/muted' | ||
|
||
- provide(:title, generate_title('Muted words')) | ||
- parent_layout 'user/settings' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class CreateMuteRules < ActiveRecord::Migration[5.2] | ||
def change | ||
create_table :mute_rules do |t| | ||
t.references :user, foreign_key: true | ||
t.string :muted_phrase | ||
|
||
t.timestamps | ||
end | ||
|
||
change_column(:mute_rules, :id, :bigint, default: -> { "gen_timestamp_id('mute_rules'::text)" }) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
require 'rails_helper' | ||
|
||
describe Ajax::MuteRuleController, :ajax_controller, type: :controller do | ||
|
||
describe "#create" do | ||
subject { post(:create, params: params) } | ||
|
||
context "when user is signed in" do | ||
before(:each) { sign_in(user) } | ||
|
||
let(:params) { { muted_phrase: 'test' } } | ||
let(:expected_response) do | ||
{ | ||
"success" => true, | ||
"status" => "okay", | ||
"id" => MuteRule.last.id, | ||
"message" => "Rule added successfully.", | ||
} | ||
end | ||
|
||
it "creates a mute rule" do | ||
expect { subject }.to change { MuteRule.count }.by(1) | ||
expect(response).to have_http_status(:success) | ||
|
||
rule = MuteRule.first | ||
expect(rule.user_id).to eq(user.id) | ||
expect(rule.muted_phrase).to eq('test') | ||
end | ||
|
||
include_examples "returns the expected response" | ||
end | ||
end | ||
|
||
describe "#update" do | ||
subject { post(:update, params: params) } | ||
|
||
context "when user is signed in" do | ||
before(:each) { sign_in(user) } | ||
|
||
let(:rule) { MuteRule.create(user: user, muted_phrase: 'test') } | ||
let(:params) { { id: rule.id, muted_phrase: 'dogs' } } | ||
let(:expected_response) do | ||
{ | ||
"success" => true, | ||
"status" => "okay", | ||
"message" => "Rule updated successfully." | ||
} | ||
end | ||
|
||
it "updates a mute rule" do | ||
subject | ||
expect(response).to have_http_status(:success) | ||
|
||
expect(rule.reload.muted_phrase).to eq('dogs') | ||
end | ||
|
||
include_examples "returns the expected response" | ||
end | ||
end | ||
|
||
describe "#destroy" do | ||
subject { delete(:destroy, params: params) } | ||
|
||
context "when user is signed in" do | ||
before(:each) { sign_in(user) } | ||
|
||
let(:rule) { MuteRule.create(user: user, muted_phrase: 'test') } | ||
let(:params) { { id: rule.id } } | ||
let(:expected_response) do | ||
{ | ||
"success" => true, | ||
"status" => "okay", | ||
"message" => "Rule deleted successfully." | ||
} | ||
end | ||
|
||
it "deletes a mute rule" do | ||
subject | ||
expect(response).to have_http_status(:success) | ||
|
||
expect(MuteRule.exists?(rule.id)).to eq(false) | ||
|
||
end | ||
|
||
include_examples "returns the expected response" | ||
end | ||
end | ||
|
||
end |
Oops, something went wrong.