11/* @flow strict-local */
22import * as typing_status from '@zulip/shared/js/typing_status' ;
33
4- import type { Dispatch , GetState , Narrow } from '../types' ;
4+ import type { Auth , Account , Dispatch , GetState , GlobalState , Narrow } from '../types' ;
55import * as api from '../api' ;
66import { PRESENCE_RESPONSE } from '../actionConstants' ;
7- import { getAuth , tryGetAuth } from '../selectors' ;
7+ import { getAuth , tryGetAuth , tryGetActiveAccount } from '../selectors' ;
88import { isPrivateOrGroupNarrow , caseNarrowPartial } from '../utils/narrow' ;
9- import { getAllUsersByEmail } from './userSelectors' ;
9+ import { getAllUsersByEmail , getUserForId } from './userSelectors' ;
10+ import { ZulipVersion } from '../utils/zulipVersion' ;
1011
1112export const reportPresence = ( isActive : boolean = true , newUserInput : boolean = false ) => async (
1213 dispatch : Dispatch ,
@@ -25,17 +26,42 @@ export const reportPresence = (isActive: boolean = true, newUserInput: boolean =
2526 } ) ;
2627} ;
2728
28- const typingWorker = auth => ( {
29- get_current_time : ( ) => new Date ( ) . getTime ( ) ,
29+ const typingWorker = ( state : GlobalState ) => {
30+ const auth : Auth = getAuth ( state ) ;
3031
31- notify_server_start : ( user_ids_array : number [ ] ) => {
32- api . typing ( auth , JSON . stringify ( user_ids_array ) , 'start' ) ;
33- } ,
32+ // User ID arrays are only supported in server versions >= 2.0.0-rc1, for
33+ // versions before this, email arrays are used. If current server version is
34+ // undetermined, user ID arrays are optimistically used.
35+ let useEmailArrays : boolean ;
36+ const activeAccount : Account | void = tryGetActiveAccount ( state ) ;
3437
35- notify_server_stop : ( user_ids_array : number [ ] ) => {
36- api . typing ( auth , JSON . stringify ( user_ids_array ) , 'stop' ) ;
37- } ,
38- } ) ;
38+ if ( ! activeAccount || activeAccount . zulipVersion === undefined ) {
39+ useEmailArrays = false ;
40+ } else {
41+ useEmailArrays = ! new ZulipVersion ( activeAccount . zulipVersion ) . isAtLeast (
42+ new ZulipVersion ( '2.0.0-rc1' ) ,
43+ ) ;
44+ }
45+
46+ const getRecipients = user_ids_array => {
47+ if ( useEmailArrays ) {
48+ return JSON . stringify ( user_ids_array . map ( userId => getUserForId ( state , userId ) . email ) ) ;
49+ }
50+ return JSON . stringify ( user_ids_array ) ;
51+ } ;
52+
53+ return {
54+ get_current_time : ( ) => new Date ( ) . getTime ( ) ,
55+
56+ notify_server_start : ( user_ids_array : number [ ] ) => {
57+ api . typing ( auth , getRecipients ( user_ids_array ) , 'start' ) ;
58+ } ,
59+
60+ notify_server_stop : ( user_ids_array : number [ ] ) => {
61+ api . typing ( auth , getRecipients ( user_ids_array ) , 'stop' ) ;
62+ } ,
63+ } ;
64+ } ;
3965
4066export const sendTypingStart = ( narrow : Narrow ) => async (
4167 dispatch : Dispatch ,
@@ -56,9 +82,7 @@ export const sendTypingStart = (narrow: Narrow) => async (
5682 }
5783 return user . user_id ;
5884 } ) ;
59-
60- const auth = getAuth ( getState ( ) ) ;
61- typing_status . update ( typingWorker ( auth ) , recipientIds ) ;
85+ typing_status . update ( typingWorker ( getState ( ) ) , recipientIds ) ;
6286} ;
6387
6488// TODO call this on more than send: blur, navigate away,
@@ -71,6 +95,5 @@ export const sendTypingStop = (narrow: Narrow) => async (
7195 return ;
7296 }
7397
74- const auth = getAuth ( getState ( ) ) ;
75- typing_status . update ( typingWorker ( auth ) , null ) ;
98+ typing_status . update ( typingWorker ( getState ( ) ) , null ) ;
7699} ;
0 commit comments