FEATURE: ability to hide or show specific post revisions

This commit is contained in:
David McClure
2014-10-13 01:18:49 -07:00
parent 1f8e1f8f17
commit 19d5362c6b
11 changed files with 195 additions and 39 deletions

View File

@@ -25,6 +25,20 @@ export default ObjectController.extend(ModalFunctionality, {
});
},
hide: function(postId, postVersion) {
var self = this;
Discourse.Post.hideRevision(postId, postVersion).then(function (result) {
self.refresh(postId, postVersion);
});
},
show: function(postId, postVersion) {
var self = this;
Discourse.Post.showRevision(postId, postVersion).then(function (result) {
self.refresh(postId, postVersion);
});
},
createdAtDate: function() { return moment(this.get("created_at")).format("LLLL"); }.property("created_at"),
previousVersion: function() { return this.get("version") - 1; }.property("version"),
@@ -35,6 +49,9 @@ export default ObjectController.extend(ModalFunctionality, {
displayGoToNext: function() { return this.get("version") < this.get("revisions_count"); }.property("version", "revisions_count"),
displayGoToLast: function() { return this.get("version") < this.get("revisions_count") - 1; }.property("version", "revisions_count"),
displayShow: function() { return this.get("hidden") && Discourse.User.currentProp('staff'); }.property("hidden"),
displayHide: function() { return !this.get("hidden") && Discourse.User.currentProp('staff'); }.property("hidden"),
displayingInline: Em.computed.equal("viewMode", "inline"),
displayingSideBySide: Em.computed.equal("viewMode", "side_by_side"),
displayingSideBySideMarkdown: Em.computed.equal("viewMode", "side_by_side_markdown"),
@@ -118,6 +135,9 @@ export default ObjectController.extend(ModalFunctionality, {
loadNextVersion: function() { this.refresh(this.get("post_id"), this.get("version") + 1); },
loadLastVersion: function() { this.refresh(this.get("post_id"), this.get("revisions_count")); },
hideVersion: function() { this.hide(this.get("post_id"), this.get("version")); },
showVersion: function() { this.show(this.get("post_id"), this.get("version")); },
displayInline: function() { this.set("viewMode", "inline"); },
displaySideBySide: function() { this.set("viewMode", "side_by_side"); },
displaySideBySideMarkdown: function() { this.set("viewMode", "side_by_side_markdown"); }

View File

@@ -471,6 +471,14 @@ Discourse.Post.reopenClass({
});
},
hideRevision: function(postId, version) {
return Discourse.ajax("/posts/" + postId + "/revisions/" + version + "/hide", { type: 'PUT' });
},
showRevision: function(postId, version) {
return Discourse.ajax("/posts/" + postId + "/revisions/" + version + "/show", { type: 'PUT' });
},
loadQuote: function(postId) {
return Discourse.ajax("/posts/" + postId + ".json").then(function (result) {
var post = Discourse.Post.create(result);

View File

@@ -6,6 +6,12 @@
<div id="revision-numbers" {{bind-attr class="displayRevisions::invisible"}}>{{{boundI18n revisionsTextKey previousBinding="previousVersion" currentBinding="version" totalBinding="revisions_count"}}}</div>
<button title="{{i18n post.revisions.controls.next}}" {{bind-attr class=":btn :standard displayGoToNext::invisible" disabled=loading}} {{action "loadNextVersion"}}><i class="fa fa-forward"></i></button>
<button title="{{i18n post.revisions.controls.last}}" {{bind-attr class=":btn :standard displayGoToLast::invisible" disabled=loading}} {{action "loadLastVersion"}}><i class="fa fa-fast-forward"></i></button>
{{#if displayHide}}
<button title="{{i18n post.revisions.controls.hide}}" {{bind-attr class=":btn :standard" disabled=loading}} {{action "hideVersion"}}><i class="fa fa-trash-o"></i></button>
{{/if}}
{{#if displayShow}}
<button title="{{i18n post.revisions.controls.show}}" {{bind-attr class=":btn :standard" disabled=loading}} {{action "showVersion"}}><i class="fa fa-undo"></i></button>
{{/if}}
</div>
{{#if loading}}<div id='revision-loading'><i class='fa fa-spinner fa-spin'></i>{{i18n loading}}</div>{{/if}}
<div id="display-modes">

View File

@@ -216,6 +216,20 @@ class PostsController < ApplicationController
render_json_dump(post_revision_serializer)
end
def hide_revision
post_revision = find_post_revision_from_params
guardian.ensure_can_hide_post_revision! post_revision
post_revision.hide!
render nothing: true
end
def show_revision
post_revision = find_post_revision_from_params
guardian.ensure_can_show_post_revision! post_revision
post_revision.show!
render nothing: true
end
def bookmark
post = find_post_from_params
if current_user

View File

@@ -29,8 +29,8 @@ class PostRevision < ActiveRecord::Base
end
def wiki_changes
prev = lookup("wiki", 0)
cur = lookup("wiki", 1)
prev = previous("wiki")
cur = current("wiki")
return if prev == cur
{
@@ -40,8 +40,8 @@ class PostRevision < ActiveRecord::Base
end
def post_type_changes
prev = lookup("post_type", 0)
cur = lookup("post_type", 1)
prev = previous("post_type")
cur = current("post_type")
return if prev == cur
{
@@ -75,48 +75,96 @@ class PostRevision < ActiveRecord::Base
end
def previous(field)
lookup_with_fallback(field, 0)
val = lookup(field)
if val.nil?
val = lookup_in_previous_revisions(field)
end
if val.nil?
val = lookup_in_post(field)
end
val
end
def current(field)
lookup_with_fallback(field, 1)
val = lookup_in_next_revision(field)
if val.nil?
val = lookup_in_post(field)
end
if val.nil?
val = lookup(field)
end
if val.nil?
val = lookup_in_previous_revisions(field)
end
return val
end
def previous_revisions
@previous_revs ||= PostRevision.where("post_id = ? AND number < ?", post_id, number)
@previous_revs ||= PostRevision.where("post_id = ? AND number < ? AND hidden = ?", post_id, number, false)
.order("number desc")
.to_a
end
def next_revision
@next_revision ||= PostRevision.where("post_id = ? AND number > ? AND hidden = ?", post_id, number, false)
.order("number asc")
.to_a.first
end
def has_topic_data?
post && post.post_number == 1
end
def lookup_with_fallback(field, index)
unless val = lookup(field, index)
previous_revisions.each do |v|
break if val = v.lookup(field, 1)
end
def lookup_in_previous_revisions(field)
previous_revisions.each do |v|
val = v.lookup(field)
return val unless val.nil?
end
unless val
if ["cooked", "raw"].include?(field)
val = post.send(field)
else
val = post.topic.send(field)
end
nil
end
def lookup_in_next_revision(field)
if next_revision
return next_revision.lookup(field)
end
end
def lookup_in_post(field)
if !post
return
elsif ["cooked", "raw"].include?(field)
val = post.send(field)
elsif ["title", "category_id"].include?(field)
val = post.topic.send(field)
end
val
end
def lookup(field, index)
if mod = modifications[field]
mod[index]
def lookup(field)
return nil if hidden
mod = modifications[field]
unless mod.nil?
mod[0]
end
end
def hide!
self.hidden = true
self.save!
end
def show!
self.hidden = false
self.save!
end
end
# == Schema Information

View File

@@ -12,7 +12,8 @@ class PostRevisionSerializer < ApplicationSerializer
:category_changes,
:user_changes,
:wiki_changes,
:post_type_changes
:post_type_changes,
:hidden
def include_title_changes?
object.has_topic_data?
@@ -22,6 +23,10 @@ class PostRevisionSerializer < ApplicationSerializer
object.has_topic_data?
end
def hidden
object.hidden
end
def version
object.number
end
@@ -43,7 +48,7 @@ class PostRevisionSerializer < ApplicationSerializer
end
def edit_reason
object.lookup("edit_reason", 1)
object.current("edit_reason")
end
def user_changes