2019-11-02 00:06:25 +01:00
const render _compose _all _everyone = require ( "../templates/compose_all_everyone.hbs" ) ;
const render _compose _announce = require ( "../templates/compose_announce.hbs" ) ;
const render _compose _invite _users = require ( "../templates/compose_invite_users.hbs" ) ;
const render _compose _not _subscribed = require ( "../templates/compose_not_subscribed.hbs" ) ;
const render _compose _private _stream _alert = require ( "../templates/compose_private_stream_alert.hbs" ) ;
2019-07-09 21:24:00 +02:00
2018-11-30 00:48:13 +01:00
// Docs: https://zulip.readthedocs.io/en/latest/subsystems/sending-messages.html
2012-10-03 22:53:15 +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
2019-11-02 00:06:25 +01:00
let user _acknowledged _all _everyone ;
let user _acknowledged _announce ;
2020-01-23 07:22:26 +01:00
let wildcard _mention ;
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 ( ) {
2019-11-02 00:06:25 +01:00
const stream _count = stream _data . get _subscriber _count ( compose _state . stream _name ( ) ) || 0 ;
2016-06-04 04:31:35 +02:00
2020-01-23 07:22:26 +01:00
const all _everyone _template = render _compose _all _everyone ( { count : stream _count ,
mention : wildcard _mention } ) ;
2019-11-02 00:06:25 +01:00
const error _area _all _everyone = $ ( "#compose-all-everyone" ) ;
2016-06-04 04:31:35 +02:00
// 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 ( ) {
2019-11-02 00:06:25 +01:00
const stream _count = stream _data . get _subscriber _count ( compose _state . stream _name ( ) ) || 0 ;
2017-12-18 16:09:41 +01:00
2019-11-02 00:06:25 +01:00
const announce _template = render _compose _announce ( { count : stream _count } ) ;
const error _area _announce = $ ( "#compose-announce" ) ;
2017-12-18 16:09:41 +01:00
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
2019-02-27 15:16:50 +01:00
function update _stream _button ( btn _text , title ) {
$ ( "#left_bar_compose_stream_button_big" ) . text ( btn _text ) ;
2018-08-11 23:26:00 +02:00
$ ( "#left_bar_compose_stream_button_big" ) . prop ( "title" , title ) ;
}
2019-03-06 23:46:53 +01:00
function update _conversation _button ( btn _text , title ) {
$ ( "#left_bar_compose_private_button_big" ) . text ( btn _text ) ;
$ ( "#left_bar_compose_private_button_big" ) . prop ( "title" , title ) ;
}
exports . update _closed _compose _buttons _for _private = function ( ) {
2019-11-02 00:06:25 +01:00
const text _stream = i18n . t ( "New stream message" ) ;
const title _stream = text _stream + " (c)" ;
const text _conversation = i18n . t ( "New private message" ) ;
const title _conversation = text _conversation + " (x)" ;
2019-03-06 23:46:53 +01:00
update _stream _button ( text _stream , title _stream ) ;
update _conversation _button ( text _conversation , title _conversation ) ;
2018-08-09 05:59:55 +02:00
} ;
2019-03-06 23:46:53 +01:00
exports . update _closed _compose _buttons _for _stream = function ( ) {
2019-11-02 00:06:25 +01:00
const text _stream = i18n . t ( "New topic" ) ;
const title _stream = text _stream + " (c)" ;
const text _conversation = i18n . t ( "New private message" ) ;
const title _conversation = text _conversation + " (x)" ;
2019-03-06 23:46:53 +01:00
update _stream _button ( text _stream , title _stream ) ;
update _conversation _button ( text _conversation , title _conversation ) ;
2018-08-09 05:59:55 +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
2019-11-02 00:06:25 +01:00
const msg _type = compose _state . get _message _type ( ) ;
2013-08-11 23:54:50 +02:00
compose _fade . set _focused _recipient ( msg _type ) ;
2018-04-22 16:25:46 +02:00
compose _fade . update _all ( ) ;
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 ) ;
2019-11-02 00:06:25 +01:00
const xhr = $ ( "#compose" ) . data ( "filedrop_xhr" ) ;
2013-03-27 18:49:28 +01:00
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 ( ) {
2018-11-04 17:04:17 +01:00
// Topics are optional, and we provide a placeholder if one isn't given.
2019-11-02 00:06:25 +01:00
let topic = compose _state . topic ( ) ;
2018-11-15 18:34:16 +01:00
if ( topic === "" ) {
2019-10-25 09:45:13 +02:00
topic = exports . empty _topic _placeholder ( ) ;
2013-05-20 22:52:03 +02:00
}
2013-10-23 16:46:18 +02:00
2019-11-02 00:06:25 +01:00
const 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
2019-11-02 00:06:25 +01:00
const 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 : '' ,
2017-02-17 22:15:38 +01:00
} ;
2018-12-22 18:37:48 +01:00
util . set _message _topic ( message , '' ) ;
2013-04-17 19:34:20 +02:00
if ( message . type === "private" ) {
// TODO: this should be collapsed with the code in composebox_typeahead.js
2019-12-02 17:53:55 +01:00
const recipient = compose _state . private _message _recipient ( ) ;
2019-11-02 00:06:25 +01:00
const emails = util . extract _pm _recipients ( recipient ) ;
2017-02-24 23:51:23 +01:00
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 ) ;
2019-05-23 22:18:58 +02:00
// Note: The `undefined` case is for situations like the
// is_zephyr_mirror_realm case where users may be
// automatically created when you try to send a private
// message to their email address.
if ( message . to _user _ids !== undefined ) {
2019-06-06 21:49:01 +02:00
message . to = people . user _ids _string _to _ids _array ( message . to _user _ids ) ;
2019-05-23 22:18:58 +02:00
}
2013-04-17 19:34:20 +02:00
} else {
2019-11-02 00:06:25 +01:00
const stream _name = compose _state . stream _name ( ) ;
2017-02-23 04:08:05 +01:00
message . stream = stream _name ;
2019-11-02 00:06:25 +01:00
const sub = stream _data . get _sub ( stream _name ) ;
2017-02-23 04:08:05 +01:00
if ( sub ) {
message . stream _id = sub . stream _id ;
2020-02-07 21:18:20 +01:00
message . to = sub . stream _id ;
} else {
// We should be validating streams in calling code. We'll
// try to fall back to stream_name here just in case the
// user started composing to the old stream name and
// manually entered the stream name, and it got past
// validation. We should try to kill this code off eventually.
blueslip . error ( 'Trying to send message with bad stream name: ' + stream _name ) ;
message . to = stream _name ;
2017-02-23 04:08:05 +01:00
}
2018-12-22 18:37:48 +01:00
util . set _message _topic ( message , topic ) ;
2013-04-17 19:34:20 +02:00
}
return message ;
}
2018-02-06 22:54:53 +01:00
2017-03-29 08:54:26 +02:00
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 )
2018-05-06 21:43:17 +02: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
}
2018-02-06 22:54:53 +01:00
exports . compose _error = compose _error ;
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 )
2018-05-06 21:43:17 +02:00
. addClass ( 'home-error-bar' )
. stop ( true ) . fadeTo ( 0 , 1 ) ;
2018-04-04 19:38:09 +02:00
$ ( '#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
}
2018-02-06 22:54:53 +01:00
exports . clear _compose _box = clear _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
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 ] ) ;
}
2019-11-02 00:06:25 +01:00
let local _id ;
let 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
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
2019-11-02 00:06:25 +01:00
const message _content = compose _state . message _content ( ) ;
2018-06-02 13:59:02 +02:00
// Skip normal validation for zcommands, since they aren't
// actual messages with recipients; users only send them
// from the compose box for convenience sake.
if ( zcommand . process ( message _content ) ) {
exports . do _post _send _tasks ( ) ;
clear _compose _box ( ) ;
return ;
}
2019-10-25 09:45:13 +02:00
if ( ! exports . validate ( ) ) {
2012-10-29 22:37:34 +01:00
return false ;
}
2018-01-04 21:31:23 +01:00
2018-02-06 22:54:53 +01:00
if ( reminder . is _deferred _delivery ( message _content ) ) {
reminder . schedule _message ( ) ;
2018-01-04 21:31:23 +01:00
} else {
exports . send _message ( ) ;
}
2018-06-02 13:38:40 +02:00
exports . do _post _send _tasks ( ) ;
return true ;
} ;
exports . do _post _send _tasks = function ( ) {
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-18 20:16:56 +02:00
} ;
2017-02-10 20:03:23 +01:00
exports . update _email = function ( user _id , new _email ) {
2019-12-02 17:53:55 +01:00
let reply _to = compose _state . private _message _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 ) ;
2019-12-02 17:53:55 +01:00
compose _state . private _message _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 ( ) {
2019-12-02 17:53:55 +01:00
const private _recipients = util . extract _pm _recipients (
compose _state . private _message _recipient ( ) ) ;
2019-11-02 00:06:25 +01:00
const 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 ) {
2019-11-02 00:06:25 +01:00
const stream _obj = stream _data . get _sub ( stream _name ) ;
let result ;
2017-02-12 05:42:47 +01:00
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 ) {
2019-11-02 00:06:25 +01:00
const stream _count = stream _data . get _subscriber _count ( stream _name ) || 0 ;
2020-01-23 07:22:26 +01:00
wildcard _mention = util . find _wildcard _mentions ( compose _state . message _content ( ) ) ;
2016-11-09 19:34:07 +01:00
2020-01-23 07:22:26 +01:00
// check if wildcard_mention has any mention and henceforth execute the warning message.
if ( wildcard _mention !== null && stream _count > exports . 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 ) {
2019-11-02 00:06:25 +01:00
const stream _count = stream _data . get _subscriber _count ( stream _name ) || 0 ;
2017-12-18 16:09:41 +01:00
if ( stream _name === "announce" &&
2019-10-25 09:45:13 +02:00
stream _count > exports . announce _warn _threshold ) {
2017-12-18 16:09:41 +01:00
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 ;
}
2020-02-04 21:50:55 +01:00
function validate _stream _message _post _policy ( stream _name ) {
if ( page _params . is _admin ) {
return true ;
}
const stream _post _permission _type = stream _data . stream _post _policy _values ;
const stream _post _policy = stream _data . get _stream _post _policy ( stream _name ) ;
if ( stream _post _policy === stream _post _permission _type . admins . code ) {
2018-05-14 12:06:56 +02:00
compose _error ( i18n . t ( "Only organization admins are allowed to post to this stream." ) ) ;
return false ;
}
2020-02-04 21:50:55 +01:00
2020-02-05 14:30:59 +01:00
const person = people . get _by _user _id ( page _params . user _id ) ;
2020-02-04 21:50:55 +01:00
const current _datetime = new Date ( Date . now ( ) ) ;
const person _date _joined = new Date ( person . date _joined ) ;
const days = new Date ( current _datetime - person _date _joined ) . getDate ( ) ;
let error _text ;
if ( stream _post _policy === stream _post _permission _type . non _new _members . code &&
days < page _params . realm _waiting _period _threshold ) {
error _text = i18n . t ( "New members are not allowed to post to this stream.<br>Permission will be granted in __days__ days." , { days : days } ) ;
compose _error ( error _text ) ;
return false ;
}
2018-05-14 12:06:56 +02:00
return true ;
}
2018-01-23 21:06:47 +01:00
exports . validation _error = function ( error _type , stream _name ) {
2019-11-02 00:06:25 +01:00
let response ;
2013-02-01 17:04:23 +01:00
2019-11-02 00:06:25 +01:00
const context = { } ;
2018-02-05 09:21:58 +01:00
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 ) ;
2018-11-13 17:16:02 +01:00
compose _error ( response , $ ( '#stream_message_recipient_stream' ) ) ;
2017-06-28 13:14:09 +02:00
return false ;
case "error" :
2018-11-13 17:16:02 +01:00
compose _error ( i18n . t ( "Error checking subscription" ) ,
$ ( "#stream_message_recipient_stream" ) ) ;
2017-06-28 13:14:09 +02:00
return false ;
2019-10-25 23:55:37 +02:00
case "not-subscribed" : {
2019-11-02 00:06:25 +01:00
const sub = stream _data . get _sub ( stream _name ) ;
const new _row = render _compose _not _subscribed ( {
2018-10-31 19:59:40 +01:00
should _display _sub _button : sub . should _display _subscription _button } ) ;
2018-11-13 17:16:02 +01:00
compose _not _subscribed _error ( new _row , $ ( '#stream_message_recipient_stream' ) ) ;
2017-06-28 13:14:09 +02:00
return false ;
2012-10-03 23:13:38 +02:00
}
2019-10-25 23:55:37 +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 ;
}
2019-11-02 00:06:25 +01:00
const autosubscribe = page _params . narrow _stream !== undefined ;
const error _type = check _unsubscribed _stream _for _send ( stream _name , autosubscribe ) ;
2018-01-23 21:06:47 +01:00
return exports . validation _error ( error _type , stream _name ) ;
} ;
2017-01-05 12:05:51 +01:00
function validate _stream _message ( ) {
2019-11-02 00:06:25 +01:00
const stream _name = compose _state . stream _name ( ) ;
2017-01-05 12:05:51 +01:00
if ( stream _name === "" ) {
2018-11-13 17:16:02 +01:00
compose _error ( i18n . t ( "Please specify a stream" ) , $ ( "#stream_message_recipient_stream" ) ) ;
2017-01-05 12:05:51 +01:00
return false ;
}
2017-04-20 07:50:34 +02:00
if ( page _params . realm _mandatory _topics ) {
2019-11-02 00:06:25 +01:00
const topic = compose _state . topic ( ) ;
2017-01-05 12:05:51 +01:00
if ( topic === "" ) {
2018-11-13 17:16:02 +01:00
compose _error ( i18n . t ( "Please specify a topic" ) , $ ( "#stream_message_recipient_topic" ) ) ;
2017-01-05 12:05:51 +01:00
return false ;
}
}
2020-02-04 21:50:55 +01:00
if ( ! validate _stream _message _post _policy ( stream _name ) ) {
2018-05-14 12:06:56 +02:00
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.
2020-01-23 07:22:26 +01:00
if ( wildcard _mention !== null && stream _name === "announce" ) {
2017-12-18 16:09:41 +01:00
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 {
2018-05-06 21:43:17 +02:00
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 ( ) {
2020-01-08 01:49:44 +01:00
if ( page _params . realm _private _message _policy === 2 ) {
// Frontend check for for PRIVATE_MESSAGE_POLICY_DISABLED
const user _ids = compose _pm _pill . get _user _ids ( ) ;
2020-02-05 14:30:59 +01:00
if ( user _ids . length !== 1 || ! people . get _by _user _id ( user _ids [ 0 ] ) . is _bot ) {
2020-01-08 01:49:44 +01:00
// Unless we're composing to a bot
compose _error ( i18n . t ( "Private messages are disabled in this organization." ) ,
$ ( "#private_message_recipient" ) ) ;
return false ;
}
}
2019-12-02 17:53:55 +01:00
if ( compose _state . private _message _recipient ( ) . length === 0 ) {
2018-03-06 15:07:55 +01:00
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
2019-11-02 00:06:25 +01:00
const invalid _recipients = exports . get _invalid _recipient _emails ( ) ;
2017-03-22 14:28:16 +01:00
2019-11-02 00:06:25 +01:00
let context = { } ;
2016-12-02 21:34:35 +01:00
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 ( ) ;
2019-11-02 00:06:25 +01:00
const message _content = compose _state . message _content ( ) ;
2018-02-06 22:54:53 +01:00
if ( reminder . 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
} ;
2018-07-18 05:59:55 +02:00
exports . handle _keydown = function ( event , textarea ) {
2019-11-02 00:06:25 +01:00
const code = event . keyCode || event . which ;
const isBold = code === 66 ;
const isItalic = code === 73 && ! event . shiftKey ;
const 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
2019-11-02 00:06:25 +01:00
const isCmdOrCtrl = common . has _mac _keyboard ( ) ? event . metaKey : event . ctrlKey ;
2018-02-17 17:21:45 +01:00
if ( ( isBold || isItalic || isLink ) && isCmdOrCtrl ) {
2019-11-02 00:06:25 +01:00
const range = textarea . range ( ) ;
2018-07-19 17:10:48 +02:00
function wrap _text _with _markdown ( prefix , suffix ) {
if ( ! document . execCommand ( 'insertText' , false , prefix + range . text + suffix ) ) {
textarea . range ( range . start , range . end ) . range ( prefix + range . text + suffix ) ;
2017-12-06 14:46:23 +01:00
}
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
2018-07-19 17:10:48 +02:00
wrap _text _with _markdown ( "**" , "**" ) ;
2017-12-06 14:46:23 +01:00
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
2018-07-19 17:10:48 +02:00
wrap _text _with _markdown ( "*" , "*" ) ;
2017-12-06 14:46:23 +01:00
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
2018-07-19 17:10:48 +02:00
wrap _text _with _markdown ( "[" , "](url)" ) ;
2019-11-02 00:06:25 +01:00
const position = textarea . caret ( ) ;
const txt = document . getElementById ( textarea [ 0 ] . id ) ;
2017-12-06 14:46:23 +01:00
// 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 ;
}
} ;
2018-08-11 06:43:38 +02:00
exports . handle _keyup = function ( event , textarea ) {
// Set the rtl class if the text has an rtl direction, remove it otherwise
rtl . set _rtl _class _for _textarea ( textarea ) ;
} ;
2018-04-24 22:40:00 +02:00
exports . needs _subscribe _warning = function ( email ) {
// This returns true if all of these conditions are met:
// * the user is valid
// * the stream in the compose box is valid
// * the user is not already subscribed to the stream
// * the user has no back-door way to see stream messages
2018-05-18 12:41:05 +02:00
// (i.e. bots on public/private streams)
2018-04-24 22:40:00 +02:00
//
// You can think of this as roughly answering "is there an
// actionable way to subscribe the user and do they actually
// need it?".
//
// We expect the caller to already have verified that we're
// sending to a stream and trying to mention the user.
2019-11-02 00:06:25 +01:00
const user = people . get _active _user _for _email ( email ) ;
const stream _name = compose _state . stream _name ( ) ;
2018-04-24 22:40:00 +02:00
if ( ! stream _name ) {
return false ;
}
2019-11-02 00:06:25 +01:00
const sub = stream _data . get _sub ( stream _name ) ;
2018-04-24 22:40:00 +02:00
if ( ! sub || ! user ) {
return false ;
}
2018-05-18 12:41:05 +02:00
if ( user . is _bot ) {
// Bots may receive messages on public/private streams even if they are
2018-04-24 22:40:00 +02:00
// not subscribed.
return false ;
}
if ( stream _data . is _user _subscribed ( stream _name , user . user _id ) ) {
// If our user is already subscribed
return false ;
}
return true ;
} ;
2019-01-23 08:04:53 +01:00
function insert _video _call _url ( url , target _textarea ) {
2019-11-02 00:06:25 +01:00
const video _call _link _text = '[' + _ ( 'Click to join video call' ) + '](' + url + ')' ;
2019-01-23 08:04:53 +01:00
compose _ui . insert _syntax _and _focus ( video _call _link _text , target _textarea ) ;
2018-12-28 20:45:54 +01:00
}
2018-04-24 22:40:00 +02:00
2019-02-05 15:21:30 +01:00
exports . render _and _show _preview = function ( preview _spinner , preview _content _box , content ) {
function show _preview ( rendered _content , raw _content ) {
// content is passed to check for status messages ("/me ...")
// and will be undefined in case of errors
2019-11-02 00:06:25 +01:00
let rendered _preview _html ;
2019-02-05 15:21:30 +01:00
if ( raw _content !== undefined &&
2019-12-03 15:29:44 +01:00
markdown . is _status _message ( raw _content ) ) {
2019-02-05 15:21:30 +01:00
// Handle previews of /me messages
rendered _preview _html = "<p><strong>" + page _params . full _name + "</strong>" + rendered _content . slice ( "<p>/me" . length ) ;
} else {
rendered _preview _html = rendered _content ;
}
preview _content _box . html ( rendered _preview _html ) ;
if ( page _params . emojiset === "text" ) {
preview _content _box . find ( ".emoji" ) . replaceWith ( function ( ) {
2019-11-02 00:06:25 +01:00
const text = $ ( this ) . attr ( "title" ) ;
2019-02-05 15:21:30 +01:00
return ":" + text + ":" ;
} ) ;
}
}
if ( content . length === 0 ) {
show _preview ( i18n . t ( "Nothing to preview" ) ) ;
} else {
if ( markdown . contains _backend _only _syntax ( content ) ) {
2019-11-02 00:06:25 +01:00
const spinner = preview _spinner . expectOne ( ) ;
2019-02-05 15:21:30 +01:00
loading . make _indicator ( spinner ) ;
} 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
// accurate; if the `markdown.contains_backend_only_syntax` logic is
// incorrect wrong, users will see a brief flicker).
2019-11-02 00:06:25 +01:00
const message _obj = {
2019-02-05 15:21:30 +01:00
raw _content : content ,
} ;
markdown . apply _markdown ( message _obj ) ;
}
channel . post ( {
url : '/json/messages/render' ,
idempotent : true ,
data : { content : content } ,
success : function ( response _data ) {
if ( markdown . contains _backend _only _syntax ( content ) ) {
loading . destroy _indicator ( preview _spinner ) ;
}
show _preview ( response _data . rendered , content ) ;
} ,
error : function ( ) {
if ( markdown . contains _backend _only _syntax ( content ) ) {
loading . destroy _indicator ( preview _spinner ) ;
}
show _preview ( i18n . t ( "Failed to generate preview" ) ) ;
} ,
} ) ;
}
} ;
2020-01-14 15:57:16 +01:00
exports . warn _if _private _stream _is _linked = function ( linked _stream ) {
2020-01-14 22:23:27 +01:00
// For PMs, we currently don't warn about links to private
// streams, since you are specifically sharing the existence of
// the private stream with someone. One could imagine changing
// this policy if user feedback suggested it was useful.
2020-01-14 15:57:16 +01:00
if ( compose _state . get _message _type ( ) !== 'stream' ) {
return ;
}
const compose _stream = stream _data . get _sub ( compose _state . stream _name ( ) ) ;
if ( compose _stream === undefined ) {
// We have an invalid stream name, don't warn about this here as
// we show an error to the user when they try to send the message.
return ;
}
// If the stream we're linking to is not invite-only, then it's
// public, and there is no need to warn about it, since all
2020-01-14 22:23:27 +01:00
// members can already see all the public streams.
//
// Theoretically, we could still do a warning if there are any
// guest users subscribed to the stream we're posting to; we may
// change this policy if user feedback suggests it'd be an
// improvement.
2020-01-14 15:57:16 +01:00
if ( ! linked _stream . invite _only ) {
return ;
}
refactor: Extract is_subscriber_subset().
Extracting the function makes it a bit easier to
test and use in a generic way.
Also, I wanted this to live in stream_data, so that
it's easier to find if we change how we model
subscriber data.
Finally, I use _.every to do the subset check
instead of `_.difference`, since _.difference
is actually N-squared:
_.difference = restArguments(function(array, rest) {
rest = flatten(rest, true, true);
return _.filter(array, function(value){
return !_.contains(rest, value);
});
});
And we don't actually want to build a list only
to check that it's zero vs. nonzero length.
We now do this, which short circuits as soon
as it finds any key that is only in sub1:
return _.every(sub1.subscribers.keys(), (key) => {
return sub2_set.has(key);
});
2020-01-14 19:35:33 +01:00
if ( stream _data . is _subscriber _subset ( compose _stream , linked _stream ) ) {
2020-01-14 22:23:27 +01:00
// Don't warn if subscribers list of current compose_stream is
// a subset of linked_stream's subscribers list, because
// everyone will be subscribed to the linked stream and so
// knows it exists.
refactor: Extract is_subscriber_subset().
Extracting the function makes it a bit easier to
test and use in a generic way.
Also, I wanted this to live in stream_data, so that
it's easier to find if we change how we model
subscriber data.
Finally, I use _.every to do the subset check
instead of `_.difference`, since _.difference
is actually N-squared:
_.difference = restArguments(function(array, rest) {
rest = flatten(rest, true, true);
return _.filter(array, function(value){
return !_.contains(rest, value);
});
});
And we don't actually want to build a list only
to check that it's zero vs. nonzero length.
We now do this, which short circuits as soon
as it finds any key that is only in sub1:
return _.every(sub1.subscribers.keys(), (key) => {
return sub2_set.has(key);
});
2020-01-14 19:35:33 +01:00
return ;
2020-01-14 15:57:16 +01:00
}
const stream _name = linked _stream . name ;
const warning _area = $ ( "#compose_private_stream_alert" ) ;
const context = { stream _name : stream _name } ;
const new _row = render _compose _private _stream _alert ( context ) ;
warning _area . append ( new _row ) ;
warning _area . show ( ) ;
} ;
2020-01-14 17:21:45 +01:00
exports . warn _if _mentioning _unsubscribed _user = function ( mentioned ) {
if ( compose _state . get _message _type ( ) !== 'stream' ) {
return ;
}
// Disable for Zephyr mirroring realms, since we never have subscriber lists there
if ( page _params . realm _is _zephyr _mirror _realm ) {
return ;
}
const email = mentioned . email ;
2020-01-14 18:39:10 +01:00
if ( mentioned . is _broadcast ) {
return ; // don't check if @all/@everyone/@stream
2020-01-14 17:21:45 +01:00
}
if ( exports . needs _subscribe _warning ( email ) ) {
const error _area = $ ( "#compose_invite_users" ) ;
const existing _invites _area = $ ( '#compose_invite_users .compose_invite_user' ) ;
2020-02-08 02:24:21 +01:00
const existing _invites = Array . from ( $ ( existing _invites _area ) , user _row => {
2020-01-14 17:21:45 +01:00
return $ ( user _row ) . data ( 'useremail' ) ;
} ) ;
if ( existing _invites . indexOf ( email ) === - 1 ) {
const context = {
email : email ,
name : mentioned . full _name ,
can _subscribe _other _users : page _params . can _subscribe _other _users ,
} ;
const new _row = render _compose _invite _users ( context ) ;
error _area . append ( new _row ) ;
}
error _area . show ( ) ;
}
} ;
2017-06-26 21:29:08 +02:00
exports . initialize = function ( ) {
2018-11-13 17:16:02 +01:00
$ ( '#stream_message_recipient_stream,#stream_message_recipient_topic,#private_message_recipient' ) . on ( 'keyup' , update _fade ) ;
$ ( '#stream_message_recipient_stream,#stream_message_recipient_topic,#private_message_recipient' ) . on ( 'change' , update _fade ) ;
2018-07-18 05:59:55 +02:00
$ ( '#compose-textarea' ) . on ( 'keydown' , function ( event ) {
2018-08-11 06:43:38 +02:00
exports . handle _keydown ( event , $ ( "#compose-textarea" ) . expectOne ( ) ) ;
} ) ;
$ ( '#compose-textarea' ) . on ( 'keyup' , function ( event ) {
exports . handle _keyup ( event , $ ( "#compose-textarea" ) . expectOne ( ) ) ;
2018-07-18 05:59:55 +02:00
} ) ;
2017-06-26 21:29:08 +02:00
$ ( "#compose form" ) . on ( "submit" , function ( e ) {
2018-05-06 21:43:17 +02:00
e . preventDefault ( ) ;
2019-10-25 09:45:13 +02:00
exports . finish ( ) ;
2017-06-26 21:29:08 +02:00
} ) ;
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
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 ( ) ;
2019-10-25 09:45:13 +02:00
exports . 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 ( ) ;
2019-10-25 09:45:13 +02:00
exports . finish ( ) ;
2017-12-18 16:09:41 +01:00
} ) ;
2018-04-04 19:38:09 +02:00
$ ( "#compose-send-status" ) . on ( 'click' , '.sub_unsub_button' , function ( event ) {
event . preventDefault ( ) ;
2019-11-02 00:06:25 +01:00
const stream _name = $ ( '#stream_message_recipient_stream' ) . val ( ) ;
2018-04-04 19:38:09 +02:00
if ( stream _name === undefined ) {
return ;
}
2019-11-02 00:06:25 +01:00
const sub = stream _data . get _sub ( stream _name ) ;
2018-04-04 19:38:09 +02:00
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 ( ) ;
2019-11-02 00:06:25 +01:00
const invite _row = $ ( event . target ) . parents ( '.compose_invite_user' ) ;
2013-09-16 21:13:11 +02:00
2019-11-02 00:06:25 +01:00
const 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 ( ) {
2019-11-02 00:06:25 +01:00
const all _invites = $ ( "#compose_invite_users" ) ;
2017-03-04 21:42:17 +01:00
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
}
2018-08-12 22:02:50 +02:00
function failure ( error _msg ) {
exports . clear _invites ( ) ;
compose _error ( error _msg , $ ( "#compose-textarea" ) ) ;
2017-03-04 21:42:17 +01:00
$ ( event . target ) . attr ( 'disabled' , true ) ;
2017-03-04 23:02:59 +01:00
}
2018-08-12 22:02:50 +02:00
function xhr _failure ( xhr ) {
2019-11-02 00:06:25 +01:00
const error = JSON . parse ( xhr . responseText ) ;
2018-08-12 22:02:50 +02:00
failure ( error . msg ) ;
}
2019-11-02 00:06:25 +01:00
const stream _name = compose _state . stream _name ( ) ;
const sub = stream _data . get _sub ( stream _name ) ;
2017-03-04 23:02:59 +01:00
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.
2019-11-02 00:06:25 +01:00
const error _msg = 'Stream no longer exists: ' + stream _name ;
2018-08-12 22:02:50 +02:00
blueslip . warn ( error _msg ) ;
failure ( error _msg ) ;
2017-03-04 23:02:59 +01:00
return ;
}
2018-08-12 22:02:50 +02:00
stream _edit . invite _user _to _stream ( email , sub , success , xhr _failure ) ;
2013-09-16 21:13:11 +02:00
} ) ;
$ ( "#compose_invite_users" ) . on ( 'click' , '.compose_invite_close' , function ( event ) {
2019-11-02 00:06:25 +01:00
const invite _row = $ ( event . target ) . parents ( '.compose_invite_user' ) ;
const all _invites = $ ( "#compose_invite_users" ) ;
2013-09-16 21:13:11 +02:00
invite _row . remove ( ) ;
if ( all _invites . children ( ) . length === 0 ) {
all _invites . hide ( ) ;
}
} ) ;
2017-10-14 15:18:21 +02:00
$ ( "#compose_private_stream_alert" ) . on ( 'click' , '.compose_private_stream_alert_close' , function ( event ) {
2019-11-02 00:06:25 +01:00
const stream _alert _row = $ ( event . target ) . parents ( '.compose_private_stream_alert' ) ;
const stream _alert = $ ( "#compose_private_stream_alert" ) ;
2017-10-14 15:18:21 +02:00
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
2019-01-23 08:04:53 +01:00
$ ( 'body' ) . on ( 'click' , '.video_link' , function ( e ) {
2017-08-11 01:51:48 +02:00
e . preventDefault ( ) ;
2019-11-02 00:06:25 +01:00
let target _textarea ;
2019-01-23 08:04:53 +01:00
// The data-message-id atribute is only present in the video
// call icon present in the message edit form. If present,
// the request is for the edit UI; otherwise, it's for the
// compose box.
2019-11-02 00:06:25 +01:00
const edit _message _id = $ ( e . target ) . attr ( 'data-message-id' ) ;
2019-01-23 08:04:53 +01:00
if ( edit _message _id !== undefined ) {
target _textarea = $ ( "#message_edit_content_" + edit _message _id ) ;
}
2018-04-03 01:46:55 +02:00
if ( page _params . jitsi _server _url === null ) {
return ;
}
2019-11-02 00:06:25 +01:00
let video _call _link ;
const video _call _id = util . random _int ( 100000000000000 , 999999999999999 ) ;
const available _providers = page _params . realm _available _video _chat _providers ;
2019-05-09 09:54:38 +02:00
if ( page _params . realm _video _chat _provider === available _providers . google _hangouts . id ) {
2018-04-23 14:51:30 +02:00
video _call _link = "https://hangouts.google.com/hangouts/_/" + page _params . realm _google _hangouts _domain + "/" + video _call _id ;
2019-01-23 08:04:53 +01:00
insert _video _call _url ( video _call _link , target _textarea ) ;
2019-05-09 09:54:38 +02:00
} else if ( page _params . realm _video _chat _provider === available _providers . zoom . id ) {
2018-12-28 20:45:54 +01:00
channel . get ( {
url : '/json/calls/create' ,
success : function ( response ) {
2019-01-23 08:04:53 +01:00
insert _video _call _url ( response . zoom _url , target _textarea ) ;
2018-12-28 20:45:54 +01:00
} ,
} ) ;
2018-04-23 14:51:30 +02:00
} else {
video _call _link = page _params . jitsi _server _url + "/" + video _call _id ;
2019-01-23 08:04:53 +01:00
insert _video _call _url ( video _call _link , target _textarea ) ;
2018-04-23 14:51:30 +02:00
}
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 ( ) ;
2019-11-02 00:06:25 +01:00
const content = $ ( "#compose-textarea" ) . val ( ) ;
2017-11-26 20:37:44 +01:00
$ ( "#compose-textarea" ) . hide ( ) ;
2016-08-29 22:37:27 +02:00
$ ( "#markdown_preview" ) . hide ( ) ;
$ ( "#undo_markdown_preview" ) . show ( ) ;
$ ( "#preview_message_area" ) . show ( ) ;
2019-02-05 15:21:30 +01:00
exports . render _and _show _preview ( $ ( "#markdown_preview_spinner" ) , $ ( "#preview_content" ) , content ) ;
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
} ) ;
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
2019-07-23 20:13:43 +02:00
$ ( "#compose-textarea" ) . focus ( function ( ) {
2019-11-02 00:06:25 +01:00
const opts = {
2019-07-23 20:13:43 +02:00
message _type : compose _state . get _message _type ( ) ,
stream : $ ( '#stream_message_recipient_stream' ) . val ( ) ,
topic : $ ( '#stream_message_recipient_topic' ) . val ( ) ,
private _message _recipient : compose _pm _pill . get _emails ( ) ,
} ;
compose _actions . update _placeholder _text ( opts ) ;
} ) ;
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 ) {
2018-11-15 19:14:16 +01:00
compose _actions . start ( "stream" , { topic : 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
2019-10-25 09:45:13 +02:00
window . compose = exports ;