drafts: Move snapshot_message from compose to drafts

Previously drafts called compose.snapshot_message which would then
get the message object from compose.create_message_object. This method often
checked for the validity of stream/user recipients which would often cause tracebacks.

The new method in drafts.snapshot message just gets the data from the fields and
stores them in the draft model without any additional checking.
This commit is contained in:
Sampriti Panda 2017-03-29 12:24:26 +05:30 committed by Tim Abbott
parent 400a2e0ff1
commit 32e76c2c60
4 changed files with 64 additions and 14 deletions

View File

@ -79,7 +79,7 @@ people.add(bob);
};
var message = compose.snapshot_message();
var message = compose.create_message_object();
assert.equal(message.to, 'social');
assert.equal(message.subject, 'lunch');
assert.equal(message.content, 'burrito');
@ -87,7 +87,7 @@ people.add(bob);
global.compose_state.composing = function () {
return 'private';
};
message = compose.snapshot_message();
message = compose.create_message_object();
assert.deepEqual(message.to, ['alice@example.com', 'bob@example.com']);
assert.equal(message.to_user_ids, '31,32');
assert.equal(message.content, 'burrito');

View File

@ -20,6 +20,8 @@ set_global('localStorage', {
ls_container = {};
},
});
set_global('compose', {});
set_global('compose_state', {});
function stub_timestamp(timestamp, func) {
var original_func = Date.prototype.getTime;
@ -38,6 +40,7 @@ var draft_1 = {
};
var draft_2 = {
private_message_recipient: "aaron@zulip.com",
reply_to: "aaron@zulip.com",
type: "private",
content: "Test Private Message",
};
@ -93,3 +96,32 @@ var draft_2 = {
assert.deepEqual(ls.get("drafts"), {});
}());
}());
(function test_snapshot_message() {
function stub_draft(draft) {
global.compose_state.composing = function () {
return draft.type;
};
global.compose.message_content = function () {
return draft.content;
};
global.compose_state.recipient = function () {
return draft.private_message_recipient;
};
global.compose.stream_name = function () {
return draft.stream;
};
global.compose.subject = function () {
return draft.subject;
};
}
stub_draft(draft_1);
assert.deepEqual(drafts.snapshot_message(), draft_1);
stub_draft(draft_2);
assert.deepEqual(drafts.snapshot_message(), draft_2);
stub_draft({});
assert.equal(drafts.snapshot_message(), undefined);
}());

View File

@ -356,17 +356,8 @@ function create_message_object() {
}
return message;
}
exports.snapshot_message = function () {
if (!compose_state.composing() || (exports.message_content() === "")) {
// If you aren't in the middle of composing the body of a
// message, don't try to snapshot.
return;
}
// Save what we can.
return create_message_object();
};
// Export for testing
exports.create_message_object = create_message_object;
function compose_error(error_text, bad_input) {
$('#send-status').removeClass(status_classes)

View File

@ -63,8 +63,35 @@ var draft_model = (function () {
exports.draft_model = draft_model;
exports.snapshot_message = function () {
if (!compose_state.composing() || (compose.message_content() === "")) {
// If you aren't in the middle of composing the body of a
// message, don't try to snapshot.
return;
}
// Save what we can.
var message = {
type: compose_state.composing(),
content: compose.message_content(),
};
if (message.type === "private") {
var recipient = compose_state.recipient();
message.reply_to = recipient;
message.private_message_recipient = recipient;
} else {
message.stream = compose.stream_name();
var subject = compose.subject();
if (subject === "") {
subject = compose.empty_topic_placeholder();
}
message.subject = subject;
}
return message;
};
exports.update_draft = function () {
var draft = compose.snapshot_message();
var draft = drafts.snapshot_message();
var draft_id = $("#new_message_content").data("draft-id");
if (draft_id !== undefined) {