Improvements to user drafts (#6226)

* drafts in user profile: only show to user herself (not to admins), use avatar replying to (instead of topic OP), add keyboard shortcut for drafts, simplify display labels

* use JSON when testing Draft.stream
This commit is contained in:
Penar Musaraj 2018-08-01 17:41:27 -04:00 committed by Sam
parent 8147130412
commit 4a872823e7
12 changed files with 34 additions and 54 deletions

View File

@ -89,7 +89,7 @@ export default Ember.Component.extend(LoadMore, {
const stream = this.get("stream");
Draft.clear(draft.draft_key, draft.sequence)
.then(() => {
stream.load(this.site);
stream.remove(draft);
})
.catch(error => {
popupAjaxError(error);

View File

@ -62,9 +62,9 @@ export default Ember.Controller.extend(CanCheckEmails, {
return viewingSelf || isAdmin;
},
@computed("viewingSelf", "currentUser.admin")
showDrafts(viewingSelf, isAdmin) {
return viewingSelf || isAdmin;
@computed("viewingSelf")
showDrafts(viewingSelf) {
return viewingSelf;
},
@computed("viewingSelf", "currentUser.admin")

View File

@ -31,6 +31,7 @@ const bindings = {
"g b": { path: "/bookmarks" },
"g p": { path: "/my/activity" },
"g m": { path: "/my/messages" },
"g d": { path: "/my/activity/drafts" },
home: { handler: "goToFirstPost", anonymous: true },
"command+up": { handler: "goToFirstPost", anonymous: true },
j: { handler: "selectDown", anonymous: true },

View File

@ -31,17 +31,15 @@ export default RestModel.extend({
);
},
@computed("draft_key", "post_number")
draftType(draftKey, postNumber) {
@computed("draft_key")
draftType(draftKey) {
switch (draftKey) {
case NEW_TOPIC_KEY:
return I18n.t("drafts.new_topic");
case NEW_PRIVATE_MESSAGE_KEY:
return I18n.t("drafts.new_private_message");
default:
return postNumber
? I18n.t("drafts.post_reply", { postNumber })
: I18n.t("drafts.topic_reply");
return false;
}
}
});

View File

@ -14,6 +14,7 @@
{{#if siteSettings.enable_personal_messages}}
<li>{{{i18n 'keyboard_shortcuts_help.jump_to.messages'}}}</li>
{{/if}}
<li>{{{i18n 'keyboard_shortcuts_help.jump_to.drafts'}}}</li>
</ul>
<h4>{{i18n 'keyboard_shortcuts_help.navigation.title'}}</h4>
<ul>

View File

@ -16,29 +16,29 @@ class DraftsController < ApplicationController
limit: params[:limit]
}
guardian.ensure_can_see_drafts!(user)
stream = Draft.stream(opts)
stream.each do |d|
parsed_data = JSON.parse(d.data)
if parsed_data
if parsed_data['reply']
d.raw = parsed_data['reply']
end
if parsed_data['categoryId'].present? && !d.category_id.present?
d.category_id = parsed_data['categoryId']
help_key = "user_activity.no_drafts"
if user == current_user
stream = Draft.stream(opts)
stream.each do |d|
parsed_data = JSON.parse(d.data)
if parsed_data
if parsed_data['reply']
d.raw = parsed_data['reply']
end
if parsed_data['categoryId'].present? && !d.category_id.present?
d.category_id = parsed_data['categoryId']
end
end
end
end
help_key = "user_activity.no_drafts"
if user == current_user
help_key += ".self"
else
help_key += ".others"
end
render json: {
drafts: serialize_data(stream, DraftSerializer),
drafts: stream ? serialize_data(stream, DraftSerializer) : [],
no_results_help: I18n.t(help_key)
}

View File

@ -58,13 +58,15 @@ class Draft < ActiveRecord::Base
pu.username, pu.name, pu.id user_id, pu.uploaded_avatar_id, pu.username_lower,
du.username draft_username, NULL as raw, NULL as cooked, NULL as post_number
FROM drafts d
LEFT JOIN LATERAL json_extract_path_text (d.data::json, 'postId') postId ON TRUE
LEFT JOIN posts p ON postId :: BIGINT = p.id
LEFT JOIN topics t ON
CASE
WHEN d.draft_key LIKE '%' || '#{EXISTING_TOPIC}' || '%'
THEN CAST(replace(d.draft_key, '#{EXISTING_TOPIC}', '') AS INT)
ELSE 0
END = t.id
JOIN users pu on pu.id = COALESCE(t.user_id, d.user_id)
JOIN users pu on pu.id = COALESCE(p.user_id, t.user_id, d.user_id)
JOIN users du on du.id = #{user_id}
/*where*/
/*order_by*/

View File

@ -288,12 +288,11 @@ en:
confirm_clear: "Are you sure you want to clear all the bookmarks from this topic?"
drafts:
resume: "Resume Draft"
remove: "Remove Draft"
resume: "Resume"
remove: "Remove"
new_topic: "New topic draft"
new_private_message: "New private message draft"
topic_reply: "Draft reply"
post_reply: "Draft reply to #{{postNumber}}"
topic_count_latest:
one: "See {{count}} new or updated topic"
@ -2565,6 +2564,7 @@ en:
bookmarks: '<b>g</b>, <b>b</b> Bookmarks'
profile: '<b>g</b>, <b>p</b> Profile'
messages: '<b>g</b>, <b>m</b> Messages'
drafts: '<b>g</b>, <b>d</b> Drafts'
navigation:
title: 'Navigation'
jump: '<b>#</b> Go to post #'

View File

@ -778,7 +778,7 @@ en:
others: "No replies."
no_drafts:
self: "You have no drafts."
others: "No drafts."
others: "You do not have permission to see drafts for this user."
topic_flag_types:
spam:

View File

@ -30,10 +30,6 @@ module UserGuardian
is_me?(user) || is_admin?
end
def can_see_drafts?(user)
is_me?(user) || is_admin?
end
def can_silence_user?(user)
user && is_staff? && not(user.staff?)
end

View File

@ -2350,24 +2350,6 @@ describe Guardian do
end
end
describe "can_see_drafts?" do
it "won't allow a non-logged in user to see a user's drafts" do
expect(Guardian.new.can_see_drafts?(user)).to be_falsey
end
it "won't allow a user to see another user's drafts" do
expect(Guardian.new(coding_horror).can_see_drafts?(user)).to be_falsey
end
it "will allow user to see own drafts" do
expect(Guardian.new(user).can_see_drafts?(user)).to be_truthy
end
it "will allow an admin to see a user's drafts" do
expect(Guardian.new(admin).can_see_drafts?(user)).to be_truthy
end
end
describe "can_edit_email?" do
context 'when allowed in settings' do
before do

View File

@ -73,19 +73,19 @@ describe Draft do
end
it "should include the correct number of drafts in the stream" do
Draft.set(@user, "test", 0, "first")
Draft.set(@user, "test2", 0, "second")
Draft.set(@user, "test", 0, '{"reply":"hey.","action":"createTopic","title":"Hey"}')
Draft.set(@user, "test2", 0, '{"reply":"howdy"}')
expect(stream.count).to eq(2)
end
it "should include the right topic id in a draft reply in the stream" do
Draft.set(@user, "topic_#{public_topic.id}", 0, "hey")
Draft.set(@user, "topic_#{public_topic.id}", 0, '{"reply":"hi"}')
draft_row = stream.first
expect(draft_row.topic_id).to eq(public_topic.id)
end
it "should include the right draft username in the stream" do
Draft.set(@user, "topic_#{public_topic.id}", 0, "hey")
Draft.set(@user, "topic_#{public_topic.id}", 0, '{"reply":"hey"}')
draft_row = stream.first
expect(draft_row.draft_username).to eq(@user.username)
end