1
1
/*
2
2
Copyright 2016 OpenMarket Ltd
3
+ Copyright 2017 New Vector Ltd
3
4
4
5
Licensed under the Apache License, Version 2.0 (the "License");
5
6
you may not use this file except in compliance with the License.
@@ -16,24 +17,11 @@ limitations under the License.
16
17
17
18
import MatrixClientPeg from './MatrixClientPeg' ;
18
19
import MultiInviter from './utils/MultiInviter' ;
19
-
20
- const emailRegex = / ^ \S + @ \S + \. \S + $ / ;
21
-
22
- const mxidRegex = / ^ @ \S + : \S + $ /
23
-
24
- export function getAddressType ( inputText ) {
25
- const isEmailAddress = emailRegex . test ( inputText ) ;
26
- const isMatrixId = mxidRegex . test ( inputText ) ;
27
-
28
- // sanity check the input for user IDs
29
- if ( isEmailAddress ) {
30
- return 'email' ;
31
- } else if ( isMatrixId ) {
32
- return 'mx' ;
33
- } else {
34
- return null ;
35
- }
36
- }
20
+ import Modal from './Modal' ;
21
+ import { getAddressType } from './UserAddress' ;
22
+ import createRoom from './createRoom' ;
23
+ import sdk from './' ;
24
+ import { _t } from './languageHandler' ;
37
25
38
26
export function inviteToRoom ( roomId , addr ) {
39
27
const addrType = getAddressType ( addr ) ;
@@ -52,12 +40,116 @@ export function inviteToRoom(roomId, addr) {
52
40
* Simpler interface to utils/MultiInviter but with
53
41
* no option to cancel.
54
42
*
55
- * @param {roomId } The ID of the room to invite to
56
- * @param {array } Array of strings of addresses to invite. May be matrix IDs or 3pids.
57
- * @returns Promise
43
+ * @param {string } roomId The ID of the room to invite to
44
+ * @param {string[] } addrs Array of strings of addresses to invite. May be matrix IDs or 3pids.
45
+ * @returns { Promise } Promise
58
46
*/
59
47
export function inviteMultipleToRoom ( roomId , addrs ) {
60
48
const inviter = new MultiInviter ( roomId ) ;
61
49
return inviter . invite ( addrs ) ;
62
50
}
63
51
52
+ export function showStartChatInviteDialog ( ) {
53
+ const UserPickerDialog = sdk . getComponent ( "dialogs.UserPickerDialog" ) ;
54
+ Modal . createTrackedDialog ( 'Start a chat' , '' , UserPickerDialog , {
55
+ title : _t ( 'Start a chat' ) ,
56
+ description : _t ( "Who would you like to communicate with?" ) ,
57
+ placeholder : _t ( "Email, name or matrix ID" ) ,
58
+ button : _t ( "Start Chat" ) ,
59
+ onFinished : _onStartChatFinished ,
60
+ } ) ;
61
+ }
62
+
63
+ export function showRoomInviteDialog ( roomId ) {
64
+ const UserPickerDialog = sdk . getComponent ( "dialogs.UserPickerDialog" ) ;
65
+ Modal . createTrackedDialog ( 'Chat Invite' , '' , UserPickerDialog , {
66
+ title : _t ( 'Invite new room members' ) ,
67
+ description : _t ( 'Who would you like to add to this room?' ) ,
68
+ button : _t ( 'Send Invites' ) ,
69
+ placeholder : _t ( "Email, name or matrix ID" ) ,
70
+ onFinished : ( shouldInvite , addrs ) => {
71
+ _onRoomInviteFinished ( roomId , shouldInvite , addrs ) ;
72
+ } ,
73
+ } ) ;
74
+ }
75
+
76
+ function _onStartChatFinished ( shouldInvite , addrs ) {
77
+ if ( ! shouldInvite ) return ;
78
+
79
+ const addrTexts = addrs . map ( ( addr ) => addr . address ) ;
80
+
81
+ if ( _isDmChat ( addrTexts ) ) {
82
+ // Start a new DM chat
83
+ createRoom ( { dmUserId : addrTexts [ 0 ] } ) . catch ( ( err ) => {
84
+ console . error ( err . stack ) ;
85
+ const ErrorDialog = sdk . getComponent ( "dialogs.ErrorDialog" ) ;
86
+ Modal . createTrackedDialog ( 'Failed to invite user' , '' , ErrorDialog , {
87
+ title : _t ( "Failed to invite user" ) ,
88
+ description : ( ( err && err . message ) ? err . message : _t ( "Operation failed" ) ) ,
89
+ } ) ;
90
+ } ) ;
91
+ } else {
92
+ // Start multi user chat
93
+ let room ;
94
+ createRoom ( ) . then ( ( roomId ) => {
95
+ room = MatrixClientPeg . get ( ) . getRoom ( roomId ) ;
96
+ return inviteMultipleToRoom ( roomId , addrTexts ) ;
97
+ } ) . then ( ( addrs ) => {
98
+ return _showAnyInviteErrors ( addrs , room ) ;
99
+ } ) . catch ( ( err ) => {
100
+ console . error ( err . stack ) ;
101
+ const ErrorDialog = sdk . getComponent ( "dialogs.ErrorDialog" ) ;
102
+ Modal . createTrackedDialog ( 'Failed to invite' , '' , ErrorDialog , {
103
+ title : _t ( "Failed to invite" ) ,
104
+ description : ( ( err && err . message ) ? err . message : _t ( "Operation failed" ) ) ,
105
+ } ) ;
106
+ } ) ;
107
+ }
108
+ }
109
+
110
+ function _onRoomInviteFinished ( roomId , shouldInvite , addrs ) {
111
+ if ( ! shouldInvite ) return ;
112
+
113
+ const addrTexts = addrs . map ( ( addr ) => addr . address ) ;
114
+
115
+ // Invite new users to a room
116
+ inviteMultipleToRoom ( roomId , addrTexts ) . then ( ( addrs ) => {
117
+ const room = MatrixClientPeg . get ( ) . getRoom ( roomId ) ;
118
+ return _showAnyInviteErrors ( addrs , room ) ;
119
+ } ) . catch ( ( err ) => {
120
+ console . error ( err . stack ) ;
121
+ const ErrorDialog = sdk . getComponent ( "dialogs.ErrorDialog" ) ;
122
+ Modal . createTrackedDialog ( 'Failed to invite' , '' , ErrorDialog , {
123
+ title : _t ( "Failed to invite" ) ,
124
+ description : ( ( err && err . message ) ? err . message : _t ( "Operation failed" ) ) ,
125
+ } ) ;
126
+ } ) ;
127
+ }
128
+
129
+ function _isDmChat ( addrTexts ) {
130
+ if ( addrTexts . length === 1 && getAddressType ( addrTexts [ 0 ] ) ) {
131
+ return true ;
132
+ } else {
133
+ return false ;
134
+ }
135
+ }
136
+
137
+ function _showAnyInviteErrors ( addrs , room ) {
138
+ // Show user any errors
139
+ const errorList = [ ] ;
140
+ for ( const addr of Object . keys ( addrs ) ) {
141
+ if ( addrs [ addr ] === "error" ) {
142
+ errorList . push ( addr ) ;
143
+ }
144
+ }
145
+
146
+ if ( errorList . length > 0 ) {
147
+ const ErrorDialog = sdk . getComponent ( "dialogs.ErrorDialog" ) ;
148
+ Modal . createTrackedDialog ( 'Failed to invite the following users to the room' , '' , ErrorDialog , {
149
+ title : _t ( "Failed to invite the following users to the %(roomName)s room:" , { roomName : room . name } ) ,
150
+ description : errorList . join ( ", " ) ,
151
+ } ) ;
152
+ }
153
+ return addrs ;
154
+ }
155
+
0 commit comments