FIX: PM should never be allowed to have a category
FIX: TL3 should not be allowed to muck with PM titles
This commit is contained in:
parent
f95611aba1
commit
0f585bcdbe
14 changed files with 47 additions and 19 deletions
app
db/migrate
lib/guardian
spec
|
@ -115,7 +115,8 @@ class TopicsController < ApplicationController
|
||||||
|
|
||||||
success = false
|
success = false
|
||||||
Topic.transaction do
|
Topic.transaction do
|
||||||
success = topic.save && topic.change_category_to_id(params[:category_id].to_i)
|
success = topic.save
|
||||||
|
success &= topic.change_category_to_id(params[:category_id].to_i) unless topic.private_message?
|
||||||
end
|
end
|
||||||
|
|
||||||
# this is used to return the title to the client as it may have been changed by "TextCleaner"
|
# this is used to return the title to the client as it may have been changed by "TextCleaner"
|
||||||
|
|
|
@ -513,6 +513,8 @@ class Topic < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def change_category_to_id(category_id)
|
def change_category_to_id(category_id)
|
||||||
|
return false if private_message?
|
||||||
|
|
||||||
# If the category name is blank, reset the attribute
|
# If the category name is blank, reset the attribute
|
||||||
if (category_id.nil? || category_id.to_i == 0)
|
if (category_id.nil? || category_id.to_i == 0)
|
||||||
cat = Category.find_by(id: SiteSetting.uncategorized_category_id)
|
cat = Category.find_by(id: SiteSetting.uncategorized_category_id)
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
class PrivateMessageShaveNoCategoryId < ActiveRecord::Migration
|
||||||
|
def up
|
||||||
|
execute "UPDATE topics SET category_id = NULL WHERE category_id IS NOT NULL AND archetype = \'private_message\'"
|
||||||
|
execute "ALTER TABLE topics ADD CONSTRAINT pm_has_no_category CHECK (category_id IS NULL OR archetype <> 'private_message')"
|
||||||
|
end
|
||||||
|
def down
|
||||||
|
raise ActiveRecord::IrreversibleMigration
|
||||||
|
end
|
||||||
|
end
|
|
@ -28,7 +28,7 @@ module TopicGuardian
|
||||||
# Editing Method
|
# Editing Method
|
||||||
def can_edit_topic?(topic)
|
def can_edit_topic?(topic)
|
||||||
return false if Discourse.static_doc_topic_ids.include?(topic.id) && !is_admin?
|
return false if Discourse.static_doc_topic_ids.include?(topic.id) && !is_admin?
|
||||||
return true if is_staff? || user.has_trust_level?(TrustLevel[3])
|
return true if is_staff? || (!topic.private_message? && user.has_trust_level?(TrustLevel[3]))
|
||||||
return false if topic.archived
|
return false if topic.archived
|
||||||
is_my_own?(topic)
|
is_my_own?(topic)
|
||||||
end
|
end
|
||||||
|
|
|
@ -258,7 +258,7 @@ describe ComposerMessagesFinder do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "doesn't notify you in a private message" do
|
it "doesn't notify you in a private message" do
|
||||||
topic.update_column(:archetype, Archetype.private_message)
|
topic.update_columns(category_id: nil, archetype: Archetype.private_message)
|
||||||
finder.check_dominating_topic.should be_blank
|
finder.check_dominating_topic.should be_blank
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -853,6 +853,13 @@ describe Guardian do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'private message' do
|
||||||
|
it 'returns false at trust level 3' do
|
||||||
|
topic.archetype = 'private_message'
|
||||||
|
Guardian.new(trust_level_3).can_edit?(topic).should eq(false)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'archived' do
|
context 'archived' do
|
||||||
it 'returns true as a moderator' do
|
it 'returns true as a moderator' do
|
||||||
Guardian.new(moderator).can_edit?(build(:topic, user: user, archived: true)).should == true
|
Guardian.new(moderator).can_edit?(build(:topic, user: user, archived: true)).should == true
|
||||||
|
|
|
@ -17,6 +17,7 @@ end
|
||||||
|
|
||||||
Fabricator(:private_message_topic, from: :topic) do
|
Fabricator(:private_message_topic, from: :topic) do
|
||||||
user
|
user
|
||||||
|
category_id { nil }
|
||||||
title { sequence(:title) { |i| "This is a private message #{i}" } }
|
title { sequence(:title) { |i| "This is a private message #{i}" } }
|
||||||
archetype "private_message"
|
archetype "private_message"
|
||||||
topic_allowed_users{|t| [
|
topic_allowed_users{|t| [
|
||||||
|
|
|
@ -39,7 +39,7 @@ describe Draft do
|
||||||
it 'nukes new topic draft after a topic is created' do
|
it 'nukes new topic draft after a topic is created' do
|
||||||
u = Fabricate(:user)
|
u = Fabricate(:user)
|
||||||
Draft.set(u, Draft::NEW_TOPIC, 0, 'my draft')
|
Draft.set(u, Draft::NEW_TOPIC, 0, 'my draft')
|
||||||
t = Fabricate(:topic, user: u)
|
_t = Fabricate(:topic, user: u)
|
||||||
s = DraftSequence.current(u, Draft::NEW_TOPIC)
|
s = DraftSequence.current(u, Draft::NEW_TOPIC)
|
||||||
Draft.get(u, Draft::NEW_TOPIC, s).should be_nil
|
Draft.get(u, Draft::NEW_TOPIC, s).should be_nil
|
||||||
end
|
end
|
||||||
|
@ -47,7 +47,7 @@ describe Draft do
|
||||||
it 'nukes new pm draft after a pm is created' do
|
it 'nukes new pm draft after a pm is created' do
|
||||||
u = Fabricate(:user)
|
u = Fabricate(:user)
|
||||||
Draft.set(u, Draft::NEW_PRIVATE_MESSAGE, 0, 'my draft')
|
Draft.set(u, Draft::NEW_PRIVATE_MESSAGE, 0, 'my draft')
|
||||||
t = Fabricate(:topic, user: u, archetype: Archetype.private_message)
|
t = Fabricate(:topic, user: u, archetype: Archetype.private_message, category_id: nil)
|
||||||
s = DraftSequence.current(t.user, Draft::NEW_PRIVATE_MESSAGE)
|
s = DraftSequence.current(t.user, Draft::NEW_PRIVATE_MESSAGE)
|
||||||
Draft.get(u, Draft::NEW_PRIVATE_MESSAGE, s).should be_nil
|
Draft.get(u, Draft::NEW_PRIVATE_MESSAGE, s).should be_nil
|
||||||
end
|
end
|
||||||
|
@ -55,7 +55,7 @@ describe Draft do
|
||||||
it 'does not nuke new topic draft after a pm is created' do
|
it 'does not nuke new topic draft after a pm is created' do
|
||||||
u = Fabricate(:user)
|
u = Fabricate(:user)
|
||||||
Draft.set(u, Draft::NEW_TOPIC, 0, 'my draft')
|
Draft.set(u, Draft::NEW_TOPIC, 0, 'my draft')
|
||||||
t = Fabricate(:topic, user: u, archetype: Archetype.private_message)
|
t = Fabricate(:topic, user: u, archetype: Archetype.private_message, category_id: nil)
|
||||||
s = DraftSequence.current(t.user, Draft::NEW_TOPIC)
|
s = DraftSequence.current(t.user, Draft::NEW_TOPIC)
|
||||||
Draft.get(u, Draft::NEW_TOPIC, s).should == 'my draft'
|
Draft.get(u, Draft::NEW_TOPIC, s).should == 'my draft'
|
||||||
end
|
end
|
||||||
|
|
|
@ -108,17 +108,18 @@ describe Invite do
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'an existing user' do
|
context 'an existing user' do
|
||||||
let(:topic) { Fabricate(:topic, archetype: Archetype.private_message) }
|
let(:topic) { Fabricate(:topic, category_id: nil, archetype: 'private_message') }
|
||||||
let(:coding_horror) { Fabricate(:coding_horror) }
|
let(:coding_horror) { Fabricate(:coding_horror) }
|
||||||
let!(:invite) { topic.invite_by_email(topic.user, coding_horror.email) }
|
let!(:invite) { topic.invite_by_email(topic.user, coding_horror.email) }
|
||||||
|
|
||||||
it "doesn't create an invite" do
|
it "works" do
|
||||||
|
# doesn't create an invite
|
||||||
invite.should be_blank
|
invite.should be_blank
|
||||||
end
|
|
||||||
|
|
||||||
it "gives the user permission to access the topic" do
|
# gives the user permission to access the topic
|
||||||
topic.allowed_users.include?(coding_horror).should be_true
|
topic.allowed_users.include?(coding_horror).should be_true
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
context '.redeem' do
|
context '.redeem' do
|
||||||
|
@ -254,7 +255,7 @@ describe Invite do
|
||||||
let!(:user) { invite.redeem }
|
let!(:user) { invite.redeem }
|
||||||
|
|
||||||
let(:coding_horror) { User.find_by(username: "CodingHorror") }
|
let(:coding_horror) { User.find_by(username: "CodingHorror") }
|
||||||
let(:another_topic) { Fabricate(:topic, archetype: "private_message", user: coding_horror) }
|
let(:another_topic) { Fabricate(:topic, category_id: nil, archetype: "private_message", user: coding_horror) }
|
||||||
|
|
||||||
it 'adds the user to the topic_users of the first topic' do
|
it 'adds the user to the topic_users of the first topic' do
|
||||||
topic.allowed_users.include?(user).should be_true
|
topic.allowed_users.include?(user).should be_true
|
||||||
|
|
|
@ -66,7 +66,7 @@ describe PostAlertObserver do
|
||||||
let(:mention_post) { Fabricate(:post, user: user, raw: 'Hello @eviltrout')}
|
let(:mention_post) { Fabricate(:post, user: user, raw: 'Hello @eviltrout')}
|
||||||
let(:topic) do
|
let(:topic) do
|
||||||
topic = mention_post.topic
|
topic = mention_post.topic
|
||||||
topic.update_column :archetype, Archetype.private_message
|
topic.update_columns archetype: Archetype.private_message, category_id: nil
|
||||||
topic
|
topic
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -217,7 +217,7 @@ http://b.com/#{'a'*500}
|
||||||
|
|
||||||
describe 'internal link from pm' do
|
describe 'internal link from pm' do
|
||||||
it 'works' do
|
it 'works' do
|
||||||
pm = Fabricate(:topic, user: user, archetype: 'private_message')
|
pm = Fabricate(:topic, user: user, category_id: nil, archetype: 'private_message')
|
||||||
pm.posts.create(user: user, raw: "some content")
|
pm.posts.create(user: user, raw: "some content")
|
||||||
|
|
||||||
url = "http://#{test_uri.host}/t/topic-slug/#{topic.id}"
|
url = "http://#{test_uri.host}/t/topic-slug/#{topic.id}"
|
||||||
|
|
|
@ -7,7 +7,8 @@ describe TopicParticipantsSummary do
|
||||||
let(:topic) do
|
let(:topic) do
|
||||||
Fabricate(:topic,
|
Fabricate(:topic,
|
||||||
user: topic_creator,
|
user: topic_creator,
|
||||||
archetype: Archetype::private_message
|
archetype: Archetype::private_message,
|
||||||
|
category_id: nil
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -801,16 +801,22 @@ describe Topic do
|
||||||
context 'changing category' do
|
context 'changing category' do
|
||||||
let(:category) { Fabricate(:category) }
|
let(:category) { Fabricate(:category) }
|
||||||
|
|
||||||
before do
|
it "creates a new revision" do
|
||||||
topic.change_category_to_id(category.id)
|
topic.change_category_to_id(category.id)
|
||||||
|
post.revisions.size.should == 1
|
||||||
end
|
end
|
||||||
|
|
||||||
it "creates a new revision" do
|
it "does nothing for private messages" do
|
||||||
post.revisions.size.should == 1
|
topic.archetype = "private_message"
|
||||||
|
topic.category_id = nil
|
||||||
|
|
||||||
|
topic.change_category_to_id(category.id)
|
||||||
|
topic.category_id.should == nil
|
||||||
end
|
end
|
||||||
|
|
||||||
context "removing a category" do
|
context "removing a category" do
|
||||||
before do
|
before do
|
||||||
|
topic.change_category_to_id(category.id)
|
||||||
topic.change_category_to_id(nil)
|
topic.change_category_to_id(nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@ describe UserAction do
|
||||||
let(:private_post) { Fabricate(:post) }
|
let(:private_post) { Fabricate(:post) }
|
||||||
let(:private_topic) do
|
let(:private_topic) do
|
||||||
topic = private_post.topic
|
topic = private_post.topic
|
||||||
topic.update_column(:archetype, Archetype::private_message)
|
topic.update_columns(category_id: nil, archetype: Archetype::private_message)
|
||||||
topic
|
topic
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -136,7 +136,7 @@ describe UserAction do
|
||||||
context "liking a private message" do
|
context "liking a private message" do
|
||||||
|
|
||||||
before do
|
before do
|
||||||
post.topic.update_column(:archetype, Archetype::private_message)
|
post.topic.update_columns(category_id: nil, archetype: Archetype::private_message)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "doesn't add the entry to the stream" do
|
it "doesn't add the entry to the stream" do
|
||||||
|
|
Reference in a new issue