2012-10-18 20:29:16 +02:00
var compose = ( function ( ) {
2012-10-03 22:53:15 +02:00
2012-10-18 20:29:16 +02:00
var exports = { } ;
2017-04-14 16:26:00 +02:00
2016-12-02 15:16:33 +01:00
/ * T r a c k t h e s t a t e o f t h e @ a l l w a r n i n g . T h e u s e r m u s t a c k n o w l e d g e t h a t t h e y a r e s p a m m i n g t h e e n t i r e
stream before the warning will go away . If they try to send before explicitly dismissing the
warning , they will get an error message too .
undefined : no @ all / @ everyone in message ;
false : user typed @ all / @ everyone ;
true : user clicked YES * /
2016-11-09 19:34:07 +01:00
2016-06-04 04:31:35 +02:00
var user _acknowledged _all _everyone ;
2017-12-18 16:09:41 +01:00
var user _acknowledged _announce ;
2016-06-04 04:31:35 +02:00
2016-11-09 19:34:07 +01:00
exports . all _everyone _warn _threshold = 15 ;
2017-12-18 16:09:41 +01:00
exports . announce _warn _threshold = 60 ;
2016-11-09 19:34:07 +01:00
2017-11-28 06:26:39 +01:00
exports . uploads _domain = document . location . protocol + '//' + document . location . host ;
exports . uploads _path = '/user_uploads' ;
exports . uploads _re = new RegExp ( "\\]\\(" + exports . uploads _domain + "(" + exports . uploads _path + "[^\\)]+)\\)" , 'g' ) ;
2013-10-23 16:46:18 +02:00
function make _uploads _relative ( content ) {
// Rewrite uploads in markdown links back to domain-relative form
2017-11-28 06:26:39 +01:00
return content . replace ( exports . uploads _re , "]($1)" ) ;
2013-10-23 16:46:18 +02:00
}
2016-06-04 04:31:35 +02:00
function show _all _everyone _warnings ( ) {
2017-06-29 22:54:14 +02:00
var stream _count = stream _data . get _subscriber _count ( compose _state . stream _name ( ) ) || 0 ;
2016-06-04 04:31:35 +02:00
var all _everyone _template = templates . render ( "compose_all_everyone" , { count : stream _count } ) ;
var error _area _all _everyone = $ ( "#compose-all-everyone" ) ;
// only show one error for any number of @all or @everyone mentions
if ( ! error _area _all _everyone . is ( ':visible' ) ) {
error _area _all _everyone . append ( all _everyone _template ) ;
}
error _area _all _everyone . show ( ) ;
user _acknowledged _all _everyone = false ;
}
2017-04-14 16:26:00 +02:00
exports . clear _all _everyone _warnings = function ( ) {
2016-06-04 04:31:35 +02:00
$ ( "#compose-all-everyone" ) . hide ( ) ;
$ ( "#compose-all-everyone" ) . empty ( ) ;
2017-11-26 19:58:36 +01:00
$ ( "#compose-send-status" ) . hide ( ) ;
2017-04-14 16:26:00 +02:00
} ;
2016-06-04 04:31:35 +02:00
2018-01-04 22:28:25 +01:00
function show _sending _indicator ( whats _happening ) {
if ( whats _happening === undefined ) {
whats _happening = 'Sending...' ;
}
$ ( "#sending-indicator" ) . html ( i18n . t ( whats _happening ) ) ;
$ ( "#sending-indicator" ) . show ( ) ;
}
2017-12-18 16:09:41 +01:00
function show _announce _warnings ( ) {
var stream _count = stream _data . get _subscriber _count ( compose _state . stream _name ( ) ) || 0 ;
var announce _template = templates . render ( "compose_announce" , { count : stream _count } ) ;
var error _area _announce = $ ( "#compose-announce" ) ;
if ( ! error _area _announce . is ( ':visible' ) ) {
error _area _announce . append ( announce _template ) ;
}
error _area _announce . show ( ) ;
user _acknowledged _announce = false ;
}
exports . clear _announce _warnings = function ( ) {
$ ( "#compose-announce" ) . hide ( ) ;
$ ( "#compose-announce" ) . empty ( ) ;
$ ( "#compose-send-status" ) . hide ( ) ;
} ;
2017-04-14 16:26:00 +02:00
exports . clear _invites = function ( ) {
2013-09-16 21:13:11 +02:00
$ ( "#compose_invite_users" ) . hide ( ) ;
$ ( "#compose_invite_users" ) . empty ( ) ;
2017-04-14 15:09:13 +02:00
} ;
2017-10-14 15:18:21 +02:00
exports . clear _private _stream _alert = function ( ) {
$ ( "#compose_private_stream_alert" ) . hide ( ) ;
$ ( "#compose_private_stream_alert" ) . empty ( ) ;
} ;
2017-04-14 16:26:00 +02:00
exports . reset _user _acknowledged _all _everyone _flag = function ( ) {
2016-06-04 04:31:35 +02:00
user _acknowledged _all _everyone = undefined ;
2017-04-14 16:26:00 +02:00
} ;
2013-07-12 23:26:18 +02:00
2017-12-18 16:09:41 +01:00
exports . reset _user _acknowledged _announce _flag = function ( ) {
user _acknowledged _announce = undefined ;
} ;
2017-04-14 16:27:27 +02:00
exports . clear _preview _area = function ( ) {
2017-11-26 20:37:44 +01:00
$ ( "#compose-textarea" ) . show ( ) ;
2016-08-29 22:37:27 +02:00
$ ( "#undo_markdown_preview" ) . hide ( ) ;
$ ( "#preview_message_area" ) . hide ( ) ;
2016-09-26 22:03:42 +02:00
$ ( "#preview_content" ) . empty ( ) ;
2016-08-29 22:37:27 +02:00
$ ( "#markdown_preview" ) . show ( ) ;
2017-04-14 16:27:27 +02:00
} ;
2016-08-29 22:37:27 +02:00
2016-12-05 07:02:18 +01:00
function update _fade ( ) {
2017-04-15 01:15:59 +02:00
if ( ! compose _state . composing ( ) ) {
2013-08-01 17:47:48 +02:00
return ;
}
2013-03-12 21:46:54 +01:00
2017-04-15 01:15:59 +02:00
var msg _type = compose _state . get _message _type ( ) ;
2013-08-11 23:54:50 +02:00
compose _fade . set _focused _recipient ( msg _type ) ;
2013-08-11 21:21:47 +02:00
compose _fade . update _faded _messages ( ) ;
2013-03-12 21:46:54 +01:00
}
2017-04-14 16:33:00 +02:00
exports . abort _xhr = function ( ) {
2017-06-30 00:57:46 +02:00
$ ( "#compose-send-button" ) . prop ( "disabled" , false ) ;
2013-03-27 18:49:28 +01:00
var xhr = $ ( "#compose" ) . data ( "filedrop_xhr" ) ;
if ( xhr !== undefined ) {
xhr . abort ( ) ;
$ ( "#compose" ) . removeData ( "filedrop_xhr" ) ;
}
2017-04-14 16:33:00 +02:00
} ;
2013-03-27 18:49:28 +01:00
2016-08-27 04:47:53 +02:00
exports . empty _topic _placeholder = function ( ) {
2016-10-30 06:27:53 +01:00
return i18n . t ( "(no topic)" ) ;
2013-05-20 22:52:03 +02:00
} ;
2013-04-17 19:34:20 +02:00
function create _message _object ( ) {
2013-05-20 22:52:03 +02:00
// Subjects are optional, and we provide a placeholder if one isn't given.
2017-04-15 01:15:59 +02:00
var subject = compose _state . subject ( ) ;
2013-05-20 22:52:03 +02:00
if ( subject === "" ) {
2016-08-27 04:47:53 +02:00
subject = compose . empty _topic _placeholder ( ) ;
2013-05-20 22:52:03 +02:00
}
2013-10-23 16:46:18 +02:00
2017-04-15 01:15:59 +02:00
var content = make _uploads _relative ( compose _state . message _content ( ) ) ;
2013-10-23 16:46:18 +02:00
2013-12-19 17:03:08 +01:00
// Changes here must also be kept in sync with echo.try_deliver_locally
2017-02-17 22:15:38 +01:00
var message = {
2017-04-24 20:35:26 +02:00
type : compose _state . get _message _type ( ) ,
2017-02-17 22:15:38 +01:00
content : content ,
sender _id : page _params . user _id ,
2017-04-24 21:40:16 +02:00
queue _id : page _params . queue _id ,
2017-02-23 04:08:05 +01:00
stream : '' ,
subject : '' ,
2017-02-17 22:15:38 +01:00
} ;
2013-04-17 19:34:20 +02:00
if ( message . type === "private" ) {
// TODO: this should be collapsed with the code in composebox_typeahead.js
2017-03-18 18:48:43 +01:00
var recipient = compose _state . recipient ( ) ;
2017-02-24 23:51:23 +01:00
var emails = util . extract _pm _recipients ( recipient ) ;
message . to = emails ;
2017-02-24 16:20:25 +01:00
message . reply _to = recipient ;
message . private _message _recipient = recipient ;
2017-02-24 23:51:23 +01:00
message . to _user _ids = people . email _list _to _user _ids _string ( emails ) ;
2013-04-17 19:34:20 +02:00
} else {
2017-04-15 01:15:59 +02:00
var stream _name = compose _state . stream _name ( ) ;
2017-02-23 04:08:05 +01:00
message . to = stream _name ;
message . stream = stream _name ;
var sub = stream _data . get _sub ( stream _name ) ;
if ( sub ) {
message . stream _id = sub . stream _id ;
}
message . subject = subject ;
2013-04-17 19:34:20 +02:00
}
return message ;
}
2017-03-29 08:54:26 +02:00
// Export for testing
exports . create _message _object = create _message _object ;
2013-04-05 16:44:46 +02:00
2013-02-01 17:04:23 +01:00
function compose _error ( error _text , bad _input ) {
2017-11-26 19:58:36 +01:00
$ ( '#compose-send-status' ) . removeClass ( common . status _classes )
2013-02-01 17:04:23 +01:00
. addClass ( 'alert-error' )
. stop ( true ) . fadeTo ( 0 , 1 ) ;
2017-11-26 20:03:46 +01:00
$ ( '#compose-error-msg' ) . html ( error _text ) ;
2017-06-30 00:57:46 +02:00
$ ( "#compose-send-button" ) . prop ( 'disabled' , false ) ;
2013-03-06 17:40:51 +01:00
$ ( "#sending-indicator" ) . hide ( ) ;
2013-11-21 00:48:03 +01:00
if ( bad _input !== undefined ) {
bad _input . focus ( ) . select ( ) ;
}
2013-02-01 17:04:23 +01:00
}
2017-12-11 11:39:49 +01:00
function nonexistent _stream _reply _error ( ) {
$ ( "#nonexistent_stream_reply_error" ) . show ( ) ;
$ ( "#compose-reply-error-msg" ) . html ( "There are no messages to reply to yet." ) ;
setTimeout ( function ( ) {
$ ( "#nonexistent_stream_reply_error" ) . hide ( ) ;
} , 5000 ) ;
}
2018-04-04 19:38:09 +02:00
function compose _not _subscribed _error ( error _text , bad _input ) {
$ ( '#compose-send-status' ) . removeClass ( common . status _classes )
. addClass ( 'home-error-bar' )
. stop ( true ) . fadeTo ( 0 , 1 ) ;
$ ( '#compose-error-msg' ) . html ( error _text ) ;
$ ( "#compose-send-button" ) . prop ( 'disabled' , false ) ;
$ ( "#sending-indicator" ) . hide ( ) ;
$ ( ".compose-send-status-close" ) . hide ( ) ;
if ( bad _input !== undefined ) {
bad _input . focus ( ) . select ( ) ;
}
}
2017-12-11 11:39:49 +01:00
exports . nonexistent _stream _reply _error = nonexistent _stream _reply _error ;
2013-12-03 20:59:23 +01:00
function clear _compose _box ( ) {
2017-11-26 20:37:44 +01:00
$ ( "#compose-textarea" ) . val ( '' ) . focus ( ) ;
2017-02-22 02:34:05 +01:00
drafts . delete _draft _after _send ( ) ;
2017-04-23 08:51:26 +02:00
compose _ui . autosize _textarea ( ) ;
2017-11-26 19:58:36 +01:00
$ ( "#compose-send-status" ) . hide ( 0 ) ;
2017-06-30 00:57:46 +02:00
$ ( "#compose-send-button" ) . prop ( 'disabled' , false ) ;
2013-12-03 20:59:23 +01:00
$ ( "#sending-indicator" ) . hide ( ) ;
2014-03-13 19:03:31 +01:00
resize . resize _bottom _whitespace ( ) ;
2013-12-03 20:59:23 +01:00
}
sending messages: Extract sent_messages.js.
This commit extract send_messages.js to clean up code related
to the following things:
* sending data to /json/report_send_time
* restarting the event loop if events don't arrive on time
The code related to /json/report changes the following ways:
* We track the state almost completely in the new
send_messages.js module, with other modules just
making one-line calls.
* We no longer send "displayed" times to the servers, since
we were kind of lying about them anyway.
* We now explicitly track the state of each single sent
message in its own object.
* We now look up data related to the messages by local_id,
instead of message_id. The problem with message_id was
that is was mutable. Now we use local_id, and we extend
the local_id concept to messages that don't get rendered
client side. We no longer need to react to the
'message_id_changed' event to change our hash key.
* The code used to live in many places:
* various big chunks were scattered among compose.js,
and those were all moved or reduced to one-line
calls into the new module
* echo.js continues to make basically one-line calls,
but it no longer calls compose.report_as_received(),
nor does it set the "start" time.
* message_util.js used to report received events, but
only when they finally got drawn in the home view;
this code is gone now
The code related to restarting the event loop if events don't arrive
changes as follows:
* The timer now gets set up from within
send_messages.message_state.report_server_ack,
where we can easily inspect the current state of the
possibly-still-in-flight message.
* The code to confirm that an event was received happens now
in server_events.js, rather than later, so that we don't
falsely blame the event loop for a downstream bug. (Plus
it's easier to just do it one place.)
This change removes a fair amount of code from our node tests. Some
of the removal is good stuff related to us completing killing off
unnecessary code. Other removals are more expediency-driven, and
we should make another sweep at ramping up our coverage on compose.js,
with possibly a little more mocking of the new `send_messages` code
layer, since it's now abstracted better.
There is also some minor cleanup to echo.resend_message() in this
commit.
See #5968 for a detailed breakdown of the changes.
2017-07-30 12:56:46 +02:00
exports . send _message _success = function ( local _id , message _id , locally _echoed ) {
2017-05-09 19:34:42 +02:00
if ( ! locally _echoed ) {
2013-12-19 17:03:08 +01:00
clear _compose _box ( ) ;
}
2017-07-14 19:30:23 +02:00
echo . reify _message _id ( local _id , message _id ) ;
} ;
2017-07-13 15:01:02 +02:00
2017-07-06 19:50:52 +02:00
exports . send _message = function send _message ( request ) {
2013-10-23 17:39:05 +02:00
if ( request === undefined ) {
request = create _message _object ( ) ;
}
2013-09-11 20:42:12 +02:00
if ( request . type === "private" ) {
request . to = JSON . stringify ( request . to ) ;
} else {
request . to = JSON . stringify ( [ request . to ] ) ;
}
2013-12-19 17:03:08 +01:00
var local _id ;
sending messages: Extract sent_messages.js.
This commit extract send_messages.js to clean up code related
to the following things:
* sending data to /json/report_send_time
* restarting the event loop if events don't arrive on time
The code related to /json/report changes the following ways:
* We track the state almost completely in the new
send_messages.js module, with other modules just
making one-line calls.
* We no longer send "displayed" times to the servers, since
we were kind of lying about them anyway.
* We now explicitly track the state of each single sent
message in its own object.
* We now look up data related to the messages by local_id,
instead of message_id. The problem with message_id was
that is was mutable. Now we use local_id, and we extend
the local_id concept to messages that don't get rendered
client side. We no longer need to react to the
'message_id_changed' event to change our hash key.
* The code used to live in many places:
* various big chunks were scattered among compose.js,
and those were all moved or reduced to one-line
calls into the new module
* echo.js continues to make basically one-line calls,
but it no longer calls compose.report_as_received(),
nor does it set the "start" time.
* message_util.js used to report received events, but
only when they finally got drawn in the home view;
this code is gone now
The code related to restarting the event loop if events don't arrive
changes as follows:
* The timer now gets set up from within
send_messages.message_state.report_server_ack,
where we can easily inspect the current state of the
possibly-still-in-flight message.
* The code to confirm that an event was received happens now
in server_events.js, rather than later, so that we don't
falsely blame the event loop for a downstream bug. (Plus
it's easier to just do it one place.)
This change removes a fair amount of code from our node tests. Some
of the removal is good stuff related to us completing killing off
unnecessary code. Other removals are more expediency-driven, and
we should make another sweep at ramping up our coverage on compose.js,
with possibly a little more mocking of the new `send_messages` code
layer, since it's now abstracted better.
There is also some minor cleanup to echo.resend_message() in this
commit.
See #5968 for a detailed breakdown of the changes.
2017-07-30 12:56:46 +02:00
var locally _echoed ;
2017-05-09 19:34:42 +02:00
local _id = echo . try _deliver _locally ( request ) ;
sending messages: Extract sent_messages.js.
This commit extract send_messages.js to clean up code related
to the following things:
* sending data to /json/report_send_time
* restarting the event loop if events don't arrive on time
The code related to /json/report changes the following ways:
* We track the state almost completely in the new
send_messages.js module, with other modules just
making one-line calls.
* We no longer send "displayed" times to the servers, since
we were kind of lying about them anyway.
* We now explicitly track the state of each single sent
message in its own object.
* We now look up data related to the messages by local_id,
instead of message_id. The problem with message_id was
that is was mutable. Now we use local_id, and we extend
the local_id concept to messages that don't get rendered
client side. We no longer need to react to the
'message_id_changed' event to change our hash key.
* The code used to live in many places:
* various big chunks were scattered among compose.js,
and those were all moved or reduced to one-line
calls into the new module
* echo.js continues to make basically one-line calls,
but it no longer calls compose.report_as_received(),
nor does it set the "start" time.
* message_util.js used to report received events, but
only when they finally got drawn in the home view;
this code is gone now
The code related to restarting the event loop if events don't arrive
changes as follows:
* The timer now gets set up from within
send_messages.message_state.report_server_ack,
where we can easily inspect the current state of the
possibly-still-in-flight message.
* The code to confirm that an event was received happens now
in server_events.js, rather than later, so that we don't
falsely blame the event loop for a downstream bug. (Plus
it's easier to just do it one place.)
This change removes a fair amount of code from our node tests. Some
of the removal is good stuff related to us completing killing off
unnecessary code. Other removals are more expediency-driven, and
we should make another sweep at ramping up our coverage on compose.js,
with possibly a little more mocking of the new `send_messages` code
layer, since it's now abstracted better.
There is also some minor cleanup to echo.resend_message() in this
commit.
See #5968 for a detailed breakdown of the changes.
2017-07-30 12:56:46 +02:00
if ( local _id ) {
// We are rendering this message locally with an id
// like 92l99.01 that corresponds to a reasonable
// approximation of the id we'll get from the server
// in terms of sorting messages.
locally _echoed = true ;
} else {
// We are not rendering this message locally, but we
// track the message's life cycle with an id like
// loc-1, loc-2, loc-3,etc.
locally _echoed = false ;
local _id = sent _messages . get _new _local _id ( ) ;
2013-12-19 17:03:08 +01:00
}
2017-07-14 19:30:23 +02:00
sending messages: Extract sent_messages.js.
This commit extract send_messages.js to clean up code related
to the following things:
* sending data to /json/report_send_time
* restarting the event loop if events don't arrive on time
The code related to /json/report changes the following ways:
* We track the state almost completely in the new
send_messages.js module, with other modules just
making one-line calls.
* We no longer send "displayed" times to the servers, since
we were kind of lying about them anyway.
* We now explicitly track the state of each single sent
message in its own object.
* We now look up data related to the messages by local_id,
instead of message_id. The problem with message_id was
that is was mutable. Now we use local_id, and we extend
the local_id concept to messages that don't get rendered
client side. We no longer need to react to the
'message_id_changed' event to change our hash key.
* The code used to live in many places:
* various big chunks were scattered among compose.js,
and those were all moved or reduced to one-line
calls into the new module
* echo.js continues to make basically one-line calls,
but it no longer calls compose.report_as_received(),
nor does it set the "start" time.
* message_util.js used to report received events, but
only when they finally got drawn in the home view;
this code is gone now
The code related to restarting the event loop if events don't arrive
changes as follows:
* The timer now gets set up from within
send_messages.message_state.report_server_ack,
where we can easily inspect the current state of the
possibly-still-in-flight message.
* The code to confirm that an event was received happens now
in server_events.js, rather than later, so that we don't
falsely blame the event loop for a downstream bug. (Plus
it's easier to just do it one place.)
This change removes a fair amount of code from our node tests. Some
of the removal is good stuff related to us completing killing off
unnecessary code. Other removals are more expediency-driven, and
we should make another sweep at ramping up our coverage on compose.js,
with possibly a little more mocking of the new `send_messages` code
layer, since it's now abstracted better.
There is also some minor cleanup to echo.resend_message() in this
commit.
See #5968 for a detailed breakdown of the changes.
2017-07-30 12:56:46 +02:00
request . local _id = local _id ;
sent _messages . start _tracking _message ( {
local _id : local _id ,
locally _echoed : locally _echoed ,
} ) ;
2013-12-19 17:03:08 +01:00
2017-07-17 16:52:57 +02:00
request . locally _echoed = locally _echoed ;
2013-11-06 18:57:38 +01:00
function success ( data ) {
sending messages: Extract sent_messages.js.
This commit extract send_messages.js to clean up code related
to the following things:
* sending data to /json/report_send_time
* restarting the event loop if events don't arrive on time
The code related to /json/report changes the following ways:
* We track the state almost completely in the new
send_messages.js module, with other modules just
making one-line calls.
* We no longer send "displayed" times to the servers, since
we were kind of lying about them anyway.
* We now explicitly track the state of each single sent
message in its own object.
* We now look up data related to the messages by local_id,
instead of message_id. The problem with message_id was
that is was mutable. Now we use local_id, and we extend
the local_id concept to messages that don't get rendered
client side. We no longer need to react to the
'message_id_changed' event to change our hash key.
* The code used to live in many places:
* various big chunks were scattered among compose.js,
and those were all moved or reduced to one-line
calls into the new module
* echo.js continues to make basically one-line calls,
but it no longer calls compose.report_as_received(),
nor does it set the "start" time.
* message_util.js used to report received events, but
only when they finally got drawn in the home view;
this code is gone now
The code related to restarting the event loop if events don't arrive
changes as follows:
* The timer now gets set up from within
send_messages.message_state.report_server_ack,
where we can easily inspect the current state of the
possibly-still-in-flight message.
* The code to confirm that an event was received happens now
in server_events.js, rather than later, so that we don't
falsely blame the event loop for a downstream bug. (Plus
it's easier to just do it one place.)
This change removes a fair amount of code from our node tests. Some
of the removal is good stuff related to us completing killing off
unnecessary code. Other removals are more expediency-driven, and
we should make another sweep at ramping up our coverage on compose.js,
with possibly a little more mocking of the new `send_messages` code
layer, since it's now abstracted better.
There is also some minor cleanup to echo.resend_message() in this
commit.
See #5968 for a detailed breakdown of the changes.
2017-07-30 12:56:46 +02:00
exports . send _message _success ( local _id , data . id , locally _echoed ) ;
2013-12-19 17:03:08 +01:00
}
2013-11-04 23:58:51 +01:00
2013-12-19 17:03:08 +01:00
function error ( response ) {
// If we're not local echo'ing messages, or if this message was not
// locally echoed, show error in compose box
sending messages: Extract sent_messages.js.
This commit extract send_messages.js to clean up code related
to the following things:
* sending data to /json/report_send_time
* restarting the event loop if events don't arrive on time
The code related to /json/report changes the following ways:
* We track the state almost completely in the new
send_messages.js module, with other modules just
making one-line calls.
* We no longer send "displayed" times to the servers, since
we were kind of lying about them anyway.
* We now explicitly track the state of each single sent
message in its own object.
* We now look up data related to the messages by local_id,
instead of message_id. The problem with message_id was
that is was mutable. Now we use local_id, and we extend
the local_id concept to messages that don't get rendered
client side. We no longer need to react to the
'message_id_changed' event to change our hash key.
* The code used to live in many places:
* various big chunks were scattered among compose.js,
and those were all moved or reduced to one-line
calls into the new module
* echo.js continues to make basically one-line calls,
but it no longer calls compose.report_as_received(),
nor does it set the "start" time.
* message_util.js used to report received events, but
only when they finally got drawn in the home view;
this code is gone now
The code related to restarting the event loop if events don't arrive
changes as follows:
* The timer now gets set up from within
send_messages.message_state.report_server_ack,
where we can easily inspect the current state of the
possibly-still-in-flight message.
* The code to confirm that an event was received happens now
in server_events.js, rather than later, so that we don't
falsely blame the event loop for a downstream bug. (Plus
it's easier to just do it one place.)
This change removes a fair amount of code from our node tests. Some
of the removal is good stuff related to us completing killing off
unnecessary code. Other removals are more expediency-driven, and
we should make another sweep at ramping up our coverage on compose.js,
with possibly a little more mocking of the new `send_messages` code
layer, since it's now abstracted better.
There is also some minor cleanup to echo.resend_message() in this
commit.
See #5968 for a detailed breakdown of the changes.
2017-07-30 12:56:46 +02:00
if ( ! locally _echoed ) {
2017-11-26 20:37:44 +01:00
compose _error ( response , $ ( '#compose-textarea' ) ) ;
2013-12-19 17:03:08 +01:00
return ;
2013-12-03 20:59:23 +01:00
}
2013-10-31 15:56:30 +01:00
2014-01-02 17:34:16 +01:00
echo . message _send _error ( local _id , response ) ;
2013-09-11 20:42:12 +02:00
}
2018-02-20 13:08:50 +01:00
transmit . send _message ( request , success , error ) ;
2014-01-30 20:29:00 +01:00
server _events . assert _get _events _running ( "Restarting get_events because it was not running during send" ) ;
2013-12-03 20:59:23 +01:00
2017-05-09 19:34:42 +02:00
if ( locally _echoed ) {
2013-12-03 20:59:23 +01:00
clear _compose _box ( ) ;
}
2017-07-06 19:50:52 +02:00
} ;
2012-10-29 18:06:53 +01:00
2018-01-12 23:13:56 +01:00
exports . deferred _message _types = {
scheduled : {
delivery _type : 'send_later' ,
test : /^\/schedule/ ,
slash _command : '/schedule' ,
} ,
reminders : {
delivery _type : 'remind' ,
test : /^\/remind/ ,
slash _command : '/remind' ,
} ,
} ;
function is _deferred _delivery ( message _content ) {
var reminders _test = exports . deferred _message _types . reminders . test ;
var scheduled _test = exports . deferred _message _types . scheduled . test ;
return ( reminders _test . test ( message _content ) ||
scheduled _test . test ( message _content ) ) ;
2018-01-04 21:31:23 +01:00
}
function patch _request _for _scheduling ( request ) {
var new _request = request ;
var raw _message = request . content . split ( '\n' ) ;
var command _line = raw _message [ 0 ] ;
var message = raw _message . slice ( 1 ) . join ( '\n' ) ;
2018-01-12 23:13:56 +01:00
var deferred _message _type = _ . filter ( exports . deferred _message _types , function ( props ) {
return command _line . match ( props . test ) !== null ;
} ) [ 0 ] ;
var command = command _line . match ( deferred _message _type . test ) [ 0 ] ;
var deliver _at = command _line . slice ( command . length + 1 ) ;
if ( message . trim ( ) === '' || deliver _at . trim ( ) === '' ||
command _line . slice ( command . length , command . length + 1 ) !== ' ' ) {
2018-01-04 21:31:23 +01:00
$ ( "#compose-textarea" ) . attr ( 'disabled' , false ) ;
2018-01-12 23:13:56 +01:00
if ( command _line . slice ( command . length , command . length + 1 ) !== ' ' ) {
2018-01-04 21:31:23 +01:00
compose _error ( i18n . t ( 'Invalid slash command. Check if you are missing a space after the command.' ) , $ ( '#compose-textarea' ) ) ;
} else if ( deliver _at . trim ( ) === '' ) {
compose _error ( i18n . t ( 'Please specify time for your reminder.' ) , $ ( '#compose-textarea' ) ) ;
} else {
compose _error ( i18n . t ( 'Your reminder note is empty!' ) , $ ( '#compose-textarea' ) ) ;
}
return ;
}
new _request . content = message ;
new _request . deliver _at = deliver _at ;
2018-01-12 23:13:56 +01:00
new _request . delivery _type = deferred _message _type . delivery _type ;
2018-01-04 21:31:23 +01:00
new _request . tz _guess = moment . tz . guess ( ) ;
return new _request ;
}
2018-01-16 00:45:17 +01:00
exports . schedule _message = function schedule _message ( request , success , error ) {
2018-01-04 21:31:23 +01:00
if ( request === undefined ) {
request = create _message _object ( ) ;
}
if ( request . type === "private" ) {
request . to = JSON . stringify ( request . to ) ;
} else {
request . to = JSON . stringify ( [ request . to ] ) ;
}
2018-01-16 00:45:17 +01:00
/ * s u c c e s s a n d e r r o r c a l l b a c k s a r e k i n d o f a p a c k a g e d e a l h e r e . W h e n s c h e d u l i n g
a message either by means of slash command or from message feed , if we need to do
something special on success then we will also need to know if our request errored
and do something appropriate . Therefore we just check if success callback is not
defined and just assume request to be coming from compose box . This is correct
because we won ' t ever actually have success operate in different context than error . * /
if ( success === undefined ) {
success = function ( data ) {
notifications . notify _above _composebox ( 'Scheduled your Message to be delivered at: ' + data . deliver _at ) ;
$ ( "#compose-textarea" ) . attr ( 'disabled' , false ) ;
clear _compose _box ( ) ;
} ;
error = function ( response ) {
$ ( "#compose-textarea" ) . attr ( 'disabled' , false ) ;
compose _error ( response , $ ( '#compose-textarea' ) ) ;
} ;
/ * W e a r e a d d i n g a d i s a b l e o n c o m p o s e u n d e r t h i s b l o c k s i n c e i t a c t u a l l y
has its place with the branch of code which does stuff when slash command
is incoming from compose _box * /
$ ( "#compose-textarea" ) . attr ( 'disabled' , true ) ;
2018-01-04 21:31:23 +01:00
}
request = patch _request _for _scheduling ( request ) ;
if ( request === undefined ) {
return ;
}
2018-02-20 13:08:50 +01:00
transmit . send _message ( request , success , error ) ;
2018-01-04 21:31:23 +01:00
} ;
2017-02-19 07:43:02 +01:00
exports . enter _with _preview _open = function ( ) {
2017-04-14 16:27:27 +02:00
exports . clear _preview _area ( ) ;
2017-02-19 07:43:02 +01:00
if ( page _params . enter _sends ) {
2017-07-07 19:31:32 +02:00
// If enter_sends is enabled, we attempt to send the message
exports . finish ( ) ;
2017-02-19 07:43:02 +01:00
} else {
// Otherwise, we return to the compose box and focus it
2017-11-26 20:37:44 +01:00
$ ( "#compose-textarea" ) . focus ( ) ;
2017-02-19 07:43:02 +01:00
}
} ;
2012-10-18 20:16:56 +02:00
exports . finish = function ( ) {
2017-04-14 16:26:00 +02:00
exports . clear _invites ( ) ;
2017-10-14 15:18:21 +02:00
exports . clear _private _stream _alert ( ) ;
2018-01-04 21:31:23 +01:00
notifications . clear _compose _notifications ( ) ;
2013-09-16 21:13:11 +02:00
2012-10-29 22:37:34 +01:00
if ( ! compose . validate ( ) ) {
return false ;
}
2018-01-04 21:31:23 +01:00
var message _content = compose _state . message _content ( ) ;
2018-01-12 23:13:56 +01:00
if ( is _deferred _delivery ( message _content ) ) {
2018-01-04 21:31:23 +01:00
exports . schedule _message ( ) ;
} else {
exports . send _message ( ) ;
}
2017-04-14 16:27:27 +02:00
exports . clear _preview _area ( ) ;
2012-11-07 19:59:35 +01:00
// TODO: Do we want to fire the event even if the send failed due
// to a server-side error?
2013-07-25 22:48:55 +02:00
$ ( document ) . trigger ( $ . Event ( 'compose_finished.zulip' ) ) ;
2012-10-29 22:37:34 +01:00
return true ;
2012-10-18 20:16:56 +02:00
} ;
2017-02-10 20:03:23 +01:00
exports . update _email = function ( user _id , new _email ) {
2017-04-15 01:15:59 +02:00
var reply _to = compose _state . recipient ( ) ;
2017-02-10 20:03:23 +01:00
if ( ! reply _to ) {
return ;
}
reply _to = people . update _email _in _reply _to ( reply _to , user _id , new _email ) ;
2017-04-15 01:15:59 +02:00
compose _state . recipient ( reply _to ) ;
2017-02-10 20:03:23 +01:00
} ;
2013-10-10 16:43:49 +02:00
2017-03-22 14:28:16 +01:00
exports . get _invalid _recipient _emails = function ( ) {
var private _recipients = util . extract _pm _recipients ( compose _state . recipient ( ) ) ;
2017-10-26 18:45:08 +02:00
var invalid _recipients = _ . reject ( private _recipients , people . is _valid _email _for _compose ) ;
2017-03-22 14:28:16 +01:00
return invalid _recipients ;
} ;
2017-06-28 13:17:10 +02:00
function check _unsubscribed _stream _for _send ( stream _name , autosubscribe ) {
2017-02-12 05:42:47 +01:00
var stream _obj = stream _data . get _sub ( stream _name ) ;
var result ;
if ( ! stream _obj ) {
return "does-not-exist" ;
2017-04-28 23:47:35 +02:00
}
2017-02-12 05:42:47 +01:00
if ( ! autosubscribe ) {
return "not-subscribed" ;
}
// In the rare circumstance of the autosubscribe option, we
// *Synchronously* try to subscribe to the stream before sending
// the message. This is deprecated and we hope to remove it; see
// #4650.
2017-04-28 23:47:35 +02:00
channel . post ( {
url : "/json/subscriptions/exists" ,
2017-02-12 05:42:47 +01:00
data : { stream : stream _name , autosubscribe : true } ,
2017-04-28 23:47:35 +02:00
async : false ,
success : function ( data ) {
if ( data . subscribed ) {
result = "subscribed" ;
} else {
result = "not-subscribed" ;
}
} ,
error : function ( xhr ) {
if ( xhr . status === 404 ) {
result = "does-not-exist" ;
} else {
result = "error" ;
}
} ,
} ) ;
return result ;
2012-10-03 22:53:15 +02:00
}
2017-01-05 12:05:51 +01:00
function validate _stream _message _mentions ( stream _name ) {
2017-06-29 15:54:31 +02:00
var stream _count = stream _data . get _subscriber _count ( stream _name ) || 0 ;
2016-11-09 19:34:07 +01:00
2016-06-04 04:31:35 +02:00
// check if @all or @everyone is in the message
2017-04-15 01:15:59 +02:00
if ( util . is _all _or _everyone _mentioned ( compose _state . message _content ( ) ) &&
2016-11-09 19:34:07 +01:00
stream _count > compose . all _everyone _warn _threshold ) {
2016-06-04 04:31:35 +02:00
if ( user _acknowledged _all _everyone === undefined ||
user _acknowledged _all _everyone === false ) {
// user has not seen a warning message yet if undefined
show _all _everyone _warnings ( ) ;
2016-11-09 19:34:07 +01:00
2017-06-30 00:57:46 +02:00
$ ( "#compose-send-button" ) . prop ( 'disabled' , false ) ;
2016-11-09 19:34:07 +01:00
$ ( "#sending-indicator" ) . hide ( ) ;
2016-06-04 04:31:35 +02:00
return false ;
}
} else {
// the message no longer contains @all or @everyone
2017-04-14 16:26:00 +02:00
exports . clear _all _everyone _warnings ( ) ;
2016-06-04 04:31:35 +02:00
}
// at this point, the user has either acknowledged the warning or removed @all / @everyone
user _acknowledged _all _everyone = undefined ;
2017-01-05 12:05:51 +01:00
return true ;
}
2017-12-18 16:09:41 +01:00
function validate _stream _message _announce ( stream _name ) {
var stream _count = stream _data . get _subscriber _count ( stream _name ) || 0 ;
if ( stream _name === "announce" &&
stream _count > compose . announce _warn _threshold ) {
if ( user _acknowledged _announce === undefined ||
user _acknowledged _announce === false ) {
// user has not seen a warning message yet if undefined
show _announce _warnings ( ) ;
$ ( "#compose-send-button" ) . prop ( 'disabled' , false ) ;
$ ( "#sending-indicator" ) . hide ( ) ;
return false ;
}
} else {
exports . clear _announce _warnings ( ) ;
}
// at this point, the user has acknowledged the warning
user _acknowledged _announce = undefined ;
return true ;
}
2018-01-23 21:06:47 +01:00
exports . validation _error = function ( error _type , stream _name ) {
2013-02-01 17:04:23 +01:00
var response ;
2018-02-05 09:21:58 +01:00
var context = { } ;
context . stream _name = Handlebars . Utils . escapeExpression ( stream _name ) ;
2018-01-23 21:06:47 +01:00
switch ( error _type ) {
2017-06-28 13:14:09 +02:00
case "does-not-exist" :
2018-02-05 09:21:58 +01:00
response = i18n . t ( "<p>The stream <b>__stream_name__</b> does not exist.</p><p>Manage your subscriptions <a href='#streams/all'>on your Streams page</a>.</p>" , context ) ;
2017-06-28 13:14:09 +02:00
compose _error ( response , $ ( '#stream' ) ) ;
return false ;
case "error" :
compose _error ( i18n . t ( "Error checking subscription" ) , $ ( "#stream" ) ) ;
return false ;
case "not-subscribed" :
2018-04-04 19:38:09 +02:00
var new _row = templates . render ( "compose_not_subscribed" ) ;
compose _not _subscribed _error ( new _row , $ ( '#stream' ) ) ;
2017-06-28 13:14:09 +02:00
return false ;
2012-10-03 23:13:38 +02:00
}
2017-06-28 18:34:39 +02:00
return true ;
2017-06-28 12:40:53 +02:00
} ;
2017-01-05 12:05:51 +01:00
2018-01-23 21:06:47 +01:00
exports . validate _stream _message _address _info = function ( stream _name ) {
if ( stream _data . is _subscribed ( stream _name ) ) {
return true ;
}
var autosubscribe = page _params . narrow _stream !== undefined ;
var error _type = check _unsubscribed _stream _for _send ( stream _name , autosubscribe ) ;
return exports . validation _error ( error _type , stream _name ) ;
} ;
2017-01-05 12:05:51 +01:00
function validate _stream _message ( ) {
2017-04-15 01:15:59 +02:00
var stream _name = compose _state . stream _name ( ) ;
2017-01-05 12:05:51 +01:00
if ( stream _name === "" ) {
compose _error ( i18n . t ( "Please specify a stream" ) , $ ( "#stream" ) ) ;
return false ;
}
2017-04-20 07:50:34 +02:00
if ( page _params . realm _mandatory _topics ) {
2017-04-15 01:15:59 +02:00
var topic = compose _state . subject ( ) ;
2017-01-05 12:05:51 +01:00
if ( topic === "" ) {
compose _error ( i18n . t ( "Please specify a topic" ) , $ ( "#subject" ) ) ;
return false ;
}
}
2017-12-18 16:09:41 +01:00
// If both `@all` is mentioned and it's in `#announce`, just validate
// for `@all`. Users shouldn't have to hit "yes" more than once.
if ( util . is _all _or _everyone _mentioned ( compose _state . message _content ( ) ) &&
stream _name === "announce" ) {
if ( ! exports . validate _stream _message _address _info ( stream _name ) ||
! validate _stream _message _mentions ( stream _name ) ) {
return false ;
}
// If either criteria isn't met, just do the normal validation.
} else {
if ( ! exports . validate _stream _message _address _info ( stream _name ) ||
! validate _stream _message _mentions ( stream _name ) ||
! validate _stream _message _announce ( stream _name ) ) {
return false ;
}
2017-01-05 12:05:51 +01:00
}
return true ;
}
2017-03-22 14:28:16 +01:00
2016-12-02 15:16:33 +01:00
// The function checks whether the recipients are users of the realm or cross realm users (bots
// for now)
2012-11-08 00:38:21 +01:00
function validate _private _message ( ) {
2018-03-06 15:07:55 +01:00
if ( compose _state . recipient ( ) . length === 0 ) {
compose _error ( i18n . t ( "Please specify at least one valid recipient" ) , $ ( "#private_message_recipient" ) ) ;
2012-10-03 22:39:05 +02:00
return false ;
2017-04-20 08:03:44 +02:00
} else if ( page _params . realm _is _zephyr _mirror _realm ) {
2016-08-11 00:35:52 +02:00
// For Zephyr mirroring realms, the frontend doesn't know which users exist
return true ;
2016-12-02 21:34:35 +01:00
}
2017-03-22 14:28:16 +01:00
var invalid _recipients = exports . get _invalid _recipient _emails ( ) ;
2016-12-02 21:34:35 +01:00
var context = { } ;
if ( invalid _recipients . length === 1 ) {
2016-12-03 03:08:47 +01:00
context = { recipient : invalid _recipients . join ( ) } ;
2017-03-27 23:25:43 +02:00
compose _error ( i18n . t ( "The recipient __recipient__ is not valid" , context ) , $ ( "#private_message_recipient" ) ) ;
2016-12-02 21:34:35 +01:00
return false ;
} else if ( invalid _recipients . length > 1 ) {
2016-12-03 03:08:47 +01:00
context = { recipients : invalid _recipients . join ( ) } ;
2017-03-27 23:25:43 +02:00
compose _error ( i18n . t ( "The recipients __recipients__ are not valid" , context ) , $ ( "#private_message_recipient" ) ) ;
2016-12-02 21:34:35 +01:00
return false ;
2016-06-10 19:46:53 +02:00
}
2016-12-02 21:34:35 +01:00
return true ;
2012-10-03 22:39:05 +02:00
}
2012-10-03 23:13:38 +02:00
2012-10-18 20:29:16 +02:00
exports . validate = function ( ) {
2012-10-31 21:17:07 +01:00
$ ( "#compose-send-button" ) . attr ( 'disabled' , 'disabled' ) . blur ( ) ;
2018-01-04 21:31:23 +01:00
var message _content = compose _state . message _content ( ) ;
2018-01-12 23:13:56 +01:00
if ( is _deferred _delivery ( message _content ) ) {
2018-01-04 21:31:23 +01:00
show _sending _indicator ( 'Scheduling...' ) ;
} else {
show _sending _indicator ( ) ;
}
2012-10-03 23:13:38 +02:00
2018-01-04 21:31:23 +01:00
if ( /^\s*$/ . test ( message _content ) ) {
2017-11-26 20:37:44 +01:00
compose _error ( i18n . t ( "You have nothing to send!" ) , $ ( "#compose-textarea" ) ) ;
2012-10-31 22:18:48 +01:00
return false ;
}
2013-11-21 00:48:03 +01:00
if ( $ ( "#zephyr-mirror-error" ) . is ( ":visible" ) ) {
2016-06-13 08:40:35 +02:00
compose _error ( i18n . t ( "You need to be running Zephyr mirroring in order to send messages!" ) ) ;
2013-11-21 00:48:03 +01:00
return false ;
}
2017-04-24 20:35:26 +02:00
if ( compose _state . get _message _type ( ) === 'private' ) {
2012-11-08 00:38:21 +01:00
return validate _private _message ( ) ;
2012-10-03 23:13:38 +02:00
}
2016-12-02 21:34:35 +01:00
return validate _stream _message ( ) ;
2012-10-18 20:29:16 +02:00
} ;
2017-12-06 14:46:23 +01:00
exports . handle _keydown = function ( event ) {
var code = event . keyCode || event . which ;
var textarea = $ ( "#compose-textarea" ) ;
var range = textarea . range ( ) ;
2018-01-24 12:10:52 +01:00
var isBold = code === 66 ;
2018-02-01 19:20:24 +01:00
var isItalic = code === 73 && ! event . shiftKey ;
2018-01-24 12:10:52 +01:00
var isLink = code === 76 && event . shiftKey ;
2017-12-06 14:46:23 +01:00
2018-02-17 17:21:45 +01:00
// detect command and ctrl key
var isCmdOrCtrl = /Mac/i . test ( navigator . userAgent ) ? event . metaKey : event . ctrlKey ;
if ( ( isBold || isItalic || isLink ) && isCmdOrCtrl ) {
2017-12-06 14:46:23 +01:00
function add _markdown ( markdown ) {
var textarea = $ ( "#compose-textarea" ) ;
var range = textarea . range ( ) ;
if ( ! document . execCommand ( 'insertText' , false , markdown ) ) {
textarea . range ( range . start , range . end ) . range ( markdown ) ;
}
event . preventDefault ( ) ;
}
2018-01-24 12:10:52 +01:00
if ( isBold ) {
2017-12-06 14:46:23 +01:00
// ctrl + b: Convert selected text to bold text
add _markdown ( "**" + range . text + "**" ) ;
if ( ! range . length ) {
textarea . caret ( textarea . caret ( ) - 2 ) ;
}
}
2018-01-24 12:10:52 +01:00
if ( isItalic ) {
2017-12-06 14:46:23 +01:00
// ctrl + i: Convert selected text to italic text
add _markdown ( "*" + range . text + "*" ) ;
if ( ! range . length ) {
textarea . caret ( textarea . caret ( ) - 1 ) ;
}
}
2018-01-24 12:10:52 +01:00
if ( isLink ) {
2017-12-06 14:46:23 +01:00
// ctrl + l: Insert a link to selected text
add _markdown ( "[" + range . text + "](url)" ) ;
var position = textarea . caret ( ) ;
var txt = document . getElementById ( "compose-textarea" ) ;
// Include selected text in between [] parantheses and insert '(url)'
// where "url" should be automatically selected.
// Position of cursor depends on whether browser supports exec
// command or not. So set cursor position accrodingly.
2018-02-01 19:09:37 +01:00
if ( range . length > 0 ) {
if ( document . queryCommandEnabled ( 'insertText' ) ) {
txt . selectionStart = position - 4 ;
txt . selectionEnd = position - 1 ;
} else {
txt . selectionStart = position + range . length + 3 ;
txt . selectionEnd = position + range . length + 6 ;
}
2017-12-06 14:46:23 +01:00
} else {
2018-02-01 19:09:37 +01:00
textarea . caret ( textarea . caret ( ) - 6 ) ;
2017-12-06 14:46:23 +01:00
}
}
compose _ui . autosize _textarea ( ) ;
return ;
}
} ;
2017-06-26 21:29:08 +02:00
exports . initialize = function ( ) {
2017-07-07 00:59:09 +02:00
$ ( '#stream,#subject,#private_message_recipient' ) . on ( 'keyup' , update _fade ) ;
$ ( '#stream,#subject,#private_message_recipient' ) . on ( 'change' , update _fade ) ;
2017-12-06 14:46:23 +01:00
$ ( '#compose-textarea' ) . on ( 'keydown' , exports . handle _keydown ) ;
2017-06-26 21:29:08 +02:00
$ ( "#compose form" ) . on ( "submit" , function ( e ) {
e . preventDefault ( ) ;
compose . finish ( ) ;
} ) ;
2017-11-26 20:37:44 +01:00
resize . watch _manual _resize ( "#compose-textarea" ) ;
2013-05-03 22:16:52 +02:00
2018-02-13 23:26:19 +01:00
upload . feature _check ( $ ( "#compose #attach_files" ) ) ;
2013-08-26 19:00:38 +02:00
// Lazy load the Dropbox script, since it can slow our page load
// otherwise, and isn't enabled for all users. Also, this Dropbox
// script isn't under an open source license, so we can't (for legal
// reasons) minify it with our own code.
if ( feature _flags . dropbox _integration ) {
LazyLoad . js ( 'https://www.dropbox.com/static/api/1/dropins.js' , function ( ) {
// Successful load. We should now have window.Dropbox.
if ( ! _ . has ( window , 'Dropbox' ) ) {
blueslip . error ( 'Dropbox script reports loading but window.Dropbox undefined' ) ;
} else if ( Dropbox . isBrowserSupported ( ) ) {
Dropbox . init ( { appKey : window . dropboxAppKey } ) ;
$ ( "#compose #attach_dropbox_files" ) . removeClass ( "notdisplayed" ) ;
}
} ) ;
2013-08-11 19:57:44 +02:00
}
2013-09-16 21:13:11 +02:00
// Show a warning if a user @-mentions someone who will not receive this message
$ ( document ) . on ( 'usermention_completed.zulip' , function ( event , data ) {
2017-04-15 01:15:59 +02:00
if ( compose _state . get _message _type ( ) !== 'stream' ) {
return ;
}
2016-07-27 01:45:29 +02:00
// Disable for Zephyr mirroring realms, since we never have subscriber lists there
2017-04-20 08:03:44 +02:00
if ( page _params . realm _is _zephyr _mirror _realm ) {
2013-09-16 21:13:11 +02:00
return ;
}
if ( data !== undefined && data . mentioned !== undefined ) {
var email = data . mentioned . email ;
2016-06-04 04:31:35 +02:00
// warn if @all or @everyone is mentioned
if ( data . mentioned . full _name === 'all' || data . mentioned . full _name === 'everyone' ) {
return ; // don't check if @all or @everyone is subscribed to a stream
}
2013-10-18 16:51:26 +02:00
if ( compose _fade . would _receive _message ( email ) === false ) {
2013-09-16 21:13:11 +02:00
var error _area = $ ( "#compose_invite_users" ) ;
2017-07-08 23:29:59 +02:00
var existing _invites _area = $ ( '#compose_invite_users .compose_invite_user' ) ;
2013-09-16 21:13:11 +02:00
2017-07-08 23:29:59 +02:00
var existing _invites = _ . map ( $ ( existing _invites _area ) , function ( user _row ) {
2013-09-16 21:13:11 +02:00
return $ ( user _row ) . data ( 'useremail' ) ;
} ) ;
if ( existing _invites . indexOf ( email ) === - 1 ) {
2017-07-09 14:13:42 +02:00
var context = { email : email , name : data . mentioned . full _name } ;
var new _row = templates . render ( "compose-invite-users" , context ) ;
2013-09-16 21:13:11 +02:00
error _area . append ( new _row ) ;
}
error _area . show ( ) ;
}
}
2016-06-04 04:31:35 +02:00
2017-11-08 09:05:13 +01:00
// User group mentions will fall through here. In the future,
// we may want to add some sort of similar warning for cases
// where nobody in the group is subscribed, but that decision
// can wait on user feedback.
2016-06-04 04:31:35 +02:00
} ) ;
$ ( "#compose-all-everyone" ) . on ( 'click' , '.compose-all-everyone-confirm' , function ( event ) {
event . preventDefault ( ) ;
$ ( event . target ) . parents ( '.compose-all-everyone' ) . remove ( ) ;
user _acknowledged _all _everyone = true ;
2017-04-14 16:26:00 +02:00
exports . clear _all _everyone _warnings ( ) ;
2016-11-09 19:34:07 +01:00
compose . finish ( ) ;
2013-09-16 21:13:11 +02:00
} ) ;
2017-12-18 16:09:41 +01:00
$ ( "#compose-announce" ) . on ( 'click' , '.compose-announce-confirm' , function ( event ) {
event . preventDefault ( ) ;
$ ( event . target ) . parents ( '.compose-announce' ) . remove ( ) ;
user _acknowledged _announce = true ;
exports . clear _announce _warnings ( ) ;
compose . finish ( ) ;
} ) ;
2018-04-04 19:38:09 +02:00
$ ( "#compose-send-status" ) . on ( 'click' , '.sub_unsub_button' , function ( event ) {
event . preventDefault ( ) ;
var stream _name = $ ( '#stream' ) . val ( ) ;
if ( stream _name === undefined ) {
return ;
}
var sub = stream _data . get _sub ( stream _name ) ;
subs . sub _or _unsub ( sub ) ;
$ ( "#compose-send-status" ) . hide ( ) ;
} ) ;
2018-04-07 04:53:32 +02:00
$ ( "#compose-send-status" ) . on ( 'click' , '#compose_not_subscribed_close' , function ( event ) {
event . preventDefault ( ) ;
$ ( "#compose-send-status" ) . hide ( ) ;
} ) ;
2013-09-16 21:13:11 +02:00
$ ( "#compose_invite_users" ) . on ( 'click' , '.compose_invite_link' , function ( event ) {
event . preventDefault ( ) ;
var invite _row = $ ( event . target ) . parents ( '.compose_invite_user' ) ;
var email = $ ( invite _row ) . data ( 'useremail' ) ;
2017-03-04 21:42:17 +01:00
if ( email === undefined ) {
return ;
}
2013-09-16 21:13:11 +02:00
2017-03-04 23:02:59 +01:00
function success ( ) {
2017-03-04 21:42:17 +01:00
var all _invites = $ ( "#compose_invite_users" ) ;
invite _row . remove ( ) ;
2013-09-16 21:13:11 +02:00
2017-03-04 21:42:17 +01:00
if ( all _invites . children ( ) . length === 0 ) {
all _invites . hide ( ) ;
}
2017-03-04 23:02:59 +01:00
}
function failure ( ) {
2017-03-04 21:42:17 +01:00
var error _msg = invite _row . find ( '.compose_invite_user_error' ) ;
error _msg . show ( ) ;
$ ( event . target ) . attr ( 'disabled' , true ) ;
2017-03-04 23:02:59 +01:00
}
2017-04-15 01:15:59 +02:00
var stream _name = compose _state . stream _name ( ) ;
2017-03-04 23:02:59 +01:00
var sub = stream _data . get _sub ( stream _name ) ;
if ( ! sub ) {
// This should only happen if a stream rename occurs
// before the user clicks. We could prevent this by
// putting a stream id in the link.
blueslip . warn ( 'Stream no longer exists: ' + stream _name ) ;
failure ( ) ;
return ;
}
2017-04-24 04:11:25 +02:00
stream _edit . invite _user _to _stream ( email , sub , success , failure ) ;
2013-09-16 21:13:11 +02:00
} ) ;
$ ( "#compose_invite_users" ) . on ( 'click' , '.compose_invite_close' , function ( event ) {
var invite _row = $ ( event . target ) . parents ( '.compose_invite_user' ) ;
var all _invites = $ ( "#compose_invite_users" ) ;
invite _row . remove ( ) ;
if ( all _invites . children ( ) . length === 0 ) {
all _invites . hide ( ) ;
}
} ) ;
2017-10-14 15:18:21 +02:00
// Show a warning if a private stream is linked
$ ( document ) . on ( 'streamname_completed.zulip' , function ( event , data ) {
2017-11-27 18:13:57 +01:00
// For PMs, we don't warn about links to private streams, since
// you are often specifically encouraging somebody to subscribe
// to the stream over PMs.
2017-10-14 15:18:21 +02:00
if ( compose _state . get _message _type ( ) !== 'stream' ) {
return ;
}
2017-11-27 18:13:57 +01:00
if ( data === undefined || data . stream === undefined ) {
blueslip . error ( 'Invalid options passed into handler.' ) ;
return ;
}
2017-10-14 15:18:21 +02:00
2018-03-09 15:15:54 +01:00
var compose _stream = stream _data . get _sub ( compose _state . stream _name ( ) ) ;
if ( compose _stream . subscribers && data . stream . subscribers ) {
var compose _stream _sub = compose _stream . subscribers . keys ( ) ;
var mentioned _stream _sub = data . stream . subscribers . keys ( ) ;
// Don't warn if subscribers list of current compose_stream is a subset of
// mentioned_stream subscribers list.
if ( _ . difference ( compose _stream _sub , mentioned _stream _sub ) . length === 0 ) {
return ;
}
}
2017-11-27 18:13:57 +01:00
// data.stream refers to the stream we're linking to in
// typeahead. If it's not invite-only, then it's public, and
// there is no need to warn about it, since all users can already
// see all the public streams.
if ( ! data . stream . invite _only ) {
return ;
2017-10-14 15:18:21 +02:00
}
2017-11-27 18:13:57 +01:00
var stream _name = data . stream . name ;
var warning _area = $ ( "#compose_private_stream_alert" ) ;
var context = { stream _name : stream _name } ;
var new _row = templates . render ( "compose_private_stream_alert" , context ) ;
warning _area . append ( new _row ) ;
warning _area . show ( ) ;
2017-10-14 15:18:21 +02:00
} ) ;
$ ( "#compose_private_stream_alert" ) . on ( 'click' , '.compose_private_stream_alert_close' , function ( event ) {
var stream _alert _row = $ ( event . target ) . parents ( '.compose_private_stream_alert' ) ;
var stream _alert = $ ( "#compose_private_stream_alert" ) ;
stream _alert _row . remove ( ) ;
if ( stream _alert . children ( ) . length === 0 ) {
stream _alert . hide ( ) ;
}
} ) ;
2013-08-11 19:57:44 +02:00
// Click event binding for "Attach files" button
// Triggers a click on a hidden file input field
$ ( "#compose" ) . on ( "click" , "#attach_files" , function ( e ) {
e . preventDefault ( ) ;
$ ( "#compose #file_input" ) . trigger ( "click" ) ;
2017-10-06 21:36:39 +02:00
} ) ;
2013-08-11 19:57:44 +02:00
2018-01-21 19:27:36 +01:00
// content is passed to check for status messages ("/me ...")
// and will be undefined in case of errors
function show _preview ( rendered _content , content ) {
2017-08-27 18:46:27 +02:00
var preview _html ;
2018-01-21 19:27:36 +01:00
if ( content !== undefined && markdown . is _status _message ( content , rendered _content ) ) {
2017-08-27 18:46:27 +02:00
// Handle previews of /me messages
preview _html = "<strong>" + page _params . full _name + "</strong> " + rendered _content . slice ( 4 + 3 , - 4 ) ;
} else {
preview _html = rendered _content ;
}
2018-01-15 19:36:32 +01:00
2017-08-27 18:46:27 +02:00
$ ( "#preview_content" ) . html ( preview _html ) ;
2018-01-02 13:02:13 +01:00
if ( page _params . emojiset === "text" ) {
2017-10-23 13:54:20 +02:00
$ ( "#preview_content" ) . find ( ".emoji" ) . replaceWith ( function ( ) {
var text = $ ( this ) . attr ( "title" ) ;
return ":" + text + ":" ;
} ) ;
}
2017-08-27 18:46:27 +02:00
}
2016-08-29 22:37:27 +02:00
2017-08-11 01:51:48 +02:00
$ ( '#compose' ) . on ( 'click' , '#video_link' , function ( e ) {
e . preventDefault ( ) ;
2018-04-03 01:46:55 +02:00
if ( page _params . jitsi _server _url === null ) {
return ;
}
2017-08-11 01:51:48 +02:00
var video _call _id = util . random _int ( 100000000000000 , 999999999999999 ) ;
2018-04-03 01:46:55 +02:00
var video _call _link = page _params . jitsi _server _url + "/" + video _call _id ;
2017-08-11 01:51:48 +02:00
var video _call _link _text = '[' + _ ( 'Click to join video call' ) + '](' + video _call _link + ')' ;
2017-11-09 16:57:58 +01:00
compose _ui . insert _syntax _and _focus ( video _call _link _text ) ;
2017-08-11 01:51:48 +02:00
} ) ;
2016-08-29 22:37:27 +02:00
$ ( "#compose" ) . on ( "click" , "#markdown_preview" , function ( e ) {
e . preventDefault ( ) ;
2017-11-26 20:37:44 +01:00
var content = $ ( "#compose-textarea" ) . val ( ) ;
$ ( "#compose-textarea" ) . hide ( ) ;
2016-08-29 22:37:27 +02:00
$ ( "#markdown_preview" ) . hide ( ) ;
$ ( "#undo_markdown_preview" ) . show ( ) ;
$ ( "#preview_message_area" ) . show ( ) ;
2017-08-27 18:27:50 +02:00
if ( content . length === 0 ) {
2017-08-27 18:46:27 +02:00
show _preview ( i18n . t ( "Nothing to preview" ) ) ;
2016-08-29 22:37:27 +02:00
} else {
2017-08-27 18:27:50 +02:00
if ( markdown . contains _backend _only _syntax ( content ) ) {
2016-09-26 22:03:42 +02:00
var spinner = $ ( "#markdown_preview_spinner" ) . expectOne ( ) ;
loading . make _indicator ( spinner ) ;
2016-09-24 12:16:42 +02:00
} else {
// For messages that don't appear to contain
// bugdown-specific syntax not present in our
// marked.js frontend processor, we render using the
// frontend markdown processor message (but still
// render server-side to ensure the preview is
2017-07-29 02:51:33 +02:00
// accurate; if the `markdown.contains_backend_only_syntax` logic is
2016-09-24 12:16:42 +02:00
// incorrect wrong, users will see a brief flicker).
2017-08-27 18:26:02 +02:00
var message _obj = {
2017-08-27 18:27:50 +02:00
raw _content : content ,
2017-08-27 18:26:02 +02:00
} ;
2017-08-27 18:46:27 +02:00
markdown . apply _markdown ( message _obj ) ;
2016-09-24 12:16:42 +02:00
}
2017-01-13 01:10:19 +01:00
channel . post ( {
2016-09-24 12:16:42 +02:00
url : '/json/messages/render' ,
idempotent : true ,
2017-08-27 18:27:50 +02:00
data : { content : content } ,
2016-09-24 12:16:42 +02:00
success : function ( response _data ) {
2017-08-27 18:27:50 +02:00
if ( markdown . contains _backend _only _syntax ( content ) ) {
2016-09-26 22:03:42 +02:00
loading . destroy _indicator ( $ ( "#markdown_preview_spinner" ) ) ;
}
2018-01-21 19:27:36 +01:00
show _preview ( response _data . rendered , content ) ;
2016-09-24 12:16:42 +02:00
} ,
error : function ( ) {
2017-08-27 18:27:50 +02:00
if ( markdown . contains _backend _only _syntax ( content ) ) {
2016-09-26 22:03:42 +02:00
loading . destroy _indicator ( $ ( "#markdown_preview_spinner" ) ) ;
}
2017-08-27 18:46:27 +02:00
show _preview ( i18n . t ( "Failed to generate preview" ) ) ;
2017-01-12 00:17:43 +01:00
} ,
2016-09-24 12:16:42 +02:00
} ) ;
2016-08-29 22:37:27 +02:00
}
} ) ;
$ ( "#compose" ) . on ( "click" , "#undo_markdown_preview" , function ( e ) {
e . preventDefault ( ) ;
2017-04-14 16:27:27 +02:00
exports . clear _preview _area ( ) ;
2016-08-29 22:37:27 +02:00
} ) ;
2013-08-11 19:57:44 +02:00
$ ( "#compose" ) . on ( "click" , "#attach_dropbox_files" , function ( e ) {
e . preventDefault ( ) ;
var options = {
// Required. Called when a user selects an item in the Chooser.
success : function ( files ) {
2017-11-26 20:37:44 +01:00
var textbox = $ ( "#compose-textarea" ) ;
2013-08-11 19:57:44 +02:00
var links = _ . map ( files , function ( file ) { return '[' + file . name + '](' + file . link + ')' ; } )
. join ( ' ' ) + ' ' ;
textbox . val ( textbox . val ( ) + links ) ;
} ,
// Optional. A value of false (default) limits selection to a single file, while
// true enables multiple file selection.
2013-08-14 16:31:12 +02:00
multiselect : true ,
2017-01-12 00:17:43 +01:00
iframe : true ,
2013-08-11 19:57:44 +02:00
} ;
Dropbox . choose ( options ) ;
} ) ;
2018-02-13 22:41:46 +01:00
$ ( "#compose" ) . filedrop (
2018-02-13 22:48:48 +01:00
upload . options ( {
mode : 'compose' ,
} )
2018-02-13 22:41:46 +01:00
) ;
2014-01-14 17:21:41 +01:00
if ( page _params . narrow !== undefined ) {
2014-01-17 00:05:35 +01:00
if ( page _params . narrow _topic !== undefined ) {
2017-03-18 17:41:47 +01:00
compose _actions . start ( "stream" , { subject : page _params . narrow _topic } ) ;
2014-01-17 00:05:35 +01:00
} else {
2017-03-18 17:41:47 +01:00
compose _actions . start ( "stream" , { } ) ;
2014-01-17 00:05:35 +01:00
}
2014-01-14 17:21:41 +01:00
}
2017-06-26 21:29:08 +02:00
} ;
2012-10-24 04:02:39 +02:00
2012-10-18 20:29:16 +02:00
return exports ;
} ( ) ) ;
2016-12-29 12:23:07 +01:00
if ( typeof module !== 'undefined' ) {
module . exports = compose ;
}