1
1
import { client_db } from "@cocalc/util/schema" ;
2
- import { alert_message } from "../.. /alerts" ;
3
- import { webapp_client } from "../.. /webapp-client" ;
2
+ import { alert_message } from "@cocalc/frontend /alerts" ;
3
+ import { webapp_client } from "@cocalc/frontend /webapp-client" ;
4
4
import { len , trunc_middle } from "@cocalc/util/misc" ;
5
+ import { redux } from "@cocalc/frontend/app-framework" ;
5
6
import { open_new_tab } from "../../misc/open-browser-tab" ;
6
- import { redux } from "../../app-framework" ;
7
7
8
8
const VIDEO_CHAT_SERVER = "https://meet.jit.si" ;
9
9
const VIDEO_UPDATE_INTERVAL_MS = 30 * 1000 ;
10
10
11
11
// Create pop-up window for video chat
12
- function video_window ( url : string ) {
13
- return open_new_tab ( url , true ) ;
12
+ function videoWindow ( url : string ) {
13
+ return open_new_tab ( url , true , { noopener : false } ) ;
14
14
}
15
15
16
- const video_windows = { } ;
16
+ const videoWindows = { } ;
17
17
18
18
export class VideoChat {
19
- private video_interval_id : any ;
19
+ private intervalId ? : any ;
20
20
private project_id : string ;
21
21
private path : string ;
22
- private account_id : string ;
23
22
24
- constructor ( project_id : string , path : string , account_id : string ) {
23
+ constructor ( { project_id , path } : { project_id : string ; path : string } ) {
25
24
this . project_id = project_id ;
26
25
this . path = path ;
27
- this . account_id = account_id ;
28
26
}
29
27
30
- public we_are_chatting ( ) : boolean {
31
- const timestamp : Date | undefined = this . get_users ( ) ?. [ this . account_id ] ;
28
+ close = ( ) => {
29
+ console . log ( "close video chat!" ) ;
30
+ this . closeVideoChatWindow ( ) ;
31
+ delete this . intervalId ;
32
+ } ;
33
+
34
+ weAreChatting = ( ) : boolean => {
35
+ const { account_id } = webapp_client ;
36
+ if ( account_id == null ) {
37
+ return false ;
38
+ }
39
+ const timestamp : Date | undefined = this . getUsers ( ) ?. [ account_id ] ;
32
40
return (
33
41
timestamp != null &&
34
42
webapp_client . server_time ( ) . valueOf ( ) - timestamp . valueOf ( ) <=
35
43
VIDEO_UPDATE_INTERVAL_MS
36
44
) ;
37
- }
45
+ } ;
38
46
39
- public num_users_chatting ( ) : number {
40
- return len ( this . get_users ( ) ) ;
41
- }
47
+ numUsersChatting = ( ) : number => {
48
+ return len ( this . getUsers ( ) ) ;
49
+ } ;
42
50
43
- public get_user_name ( ) : string | undefined {
51
+ getUserName = ( ) : string | undefined => {
44
52
const users = redux . getStore ( "users" ) ;
45
- return users . get_name ( this . account_id ) ;
46
- }
53
+ const { account_id } = webapp_client ;
54
+ if ( account_id == null ) {
55
+ return ;
56
+ }
57
+ return users ?. get_name ( account_id ) ;
58
+ } ;
47
59
48
- public get_user_names ( ) : string [ ] {
60
+ getUserNames = ( ) : string [ ] => {
49
61
const users = redux . getStore ( "users" ) ;
50
62
const v : string [ ] = [ ] ;
51
- for ( const account_id in this . get_users ( ) ) {
63
+ for ( const account_id in this . getUsers ( ) ) {
52
64
const name = users . get_name ( account_id ) ?. trim ( ) ;
53
65
if ( name ) {
54
66
v . push ( trunc_middle ( name , 25 ) ) ;
55
67
}
56
68
}
57
69
return v ;
58
- }
70
+ } ;
59
71
60
- private get_users ( ) : { [ account_id : string ] : Date } {
72
+ private getUsers = ( ) : { [ account_id : string ] : Date } => {
61
73
// Users is a map {account_id:timestamp of last chat file marking}
62
74
return (
63
75
redux . getStore ( "file_use" ) ?. get_video_chat_users ( {
@@ -66,19 +78,19 @@ export class VideoChat {
66
78
ttl : 1.3 * VIDEO_UPDATE_INTERVAL_MS ,
67
79
} ) ?? { }
68
80
) ;
69
- }
81
+ } ;
70
82
71
- public stop_chatting ( ) {
72
- this . close_video_chat_window ( ) ;
73
- }
83
+ stopChatting = ( ) => {
84
+ this . closeVideoChatWindow ( ) ;
85
+ } ;
74
86
75
- public start_chatting ( ) {
87
+ startChatting = ( ) => {
76
88
redux . getActions ( "file_use" ) ?. mark_file ( this . project_id , this . path , "chat" ) ;
77
- this . open_video_chat_window ( ) ;
78
- }
89
+ this . openVideoChatWindow ( ) ;
90
+ } ;
79
91
80
92
// The canonical secret chatroom id.
81
- private chatroom_id ( ) : string {
93
+ private chatroomId = ( ) : string => {
82
94
const secret_token = redux
83
95
. getStore ( "projects" )
84
96
. getIn ( [ "project_map" , this . project_id , "status" , "secret_token" ] ) ;
@@ -89,63 +101,68 @@ export class VideoChat {
89
101
} ) ;
90
102
}
91
103
return client_db . sha1 ( secret_token , this . path ) ;
92
- }
104
+ } ;
93
105
94
- public url ( ) : string {
95
- const room_id = this . chatroom_id ( ) ;
106
+ url = ( ) : string => {
107
+ const room_id = this . chatroomId ( ) ;
96
108
return `${ VIDEO_CHAT_SERVER } /${ room_id } ` ;
97
- }
109
+ } ;
98
110
99
111
// Open the video chat window, if it isn't already opened
100
- public open_video_chat_window ( ) : void {
101
- const room_id = this . chatroom_id ( ) ;
102
- if ( video_windows [ room_id ] ) {
112
+ private openVideoChatWindow = ( ) : void => {
113
+ const room_id = this . chatroomId ( ) ;
114
+ if ( videoWindows [ room_id ] ) {
103
115
return ;
104
116
}
105
117
106
- const chat_window_is_open = ( ) => {
118
+ const chatWindowIsOpen = ( ) => {
107
119
return redux
108
120
. getActions ( "file_use" )
109
121
?. mark_file ( this . project_id , this . path , "video" , 0 ) ;
110
122
} ;
111
123
112
- chat_window_is_open ( ) ;
113
- this . video_interval_id = setInterval (
114
- chat_window_is_open ,
115
- VIDEO_UPDATE_INTERVAL_MS * 0.8
124
+ chatWindowIsOpen ( ) ;
125
+ this . intervalId = setInterval (
126
+ chatWindowIsOpen ,
127
+ VIDEO_UPDATE_INTERVAL_MS * 0.8 ,
116
128
) ;
117
129
118
130
//const title = `CoCalc Video Chat: ${trunc_middle(this.path, 30)}`;
119
- const w = video_window ( this . url ( ) ) ;
131
+ const w = videoWindow ( this . url ( ) ) ;
120
132
// https://github.com/sagemathinc/cocalc/issues/3648
121
133
if ( w == null ) {
122
134
return ;
123
135
}
124
- video_windows [ room_id ] = w ;
136
+ videoWindows [ room_id ] = w ;
125
137
// disabled -- see https://github.com/sagemathinc/cocalc/issues/1899
126
138
//w.addEventListener "unload", =>
127
139
// @close_video_chat_window ()
128
140
// workaround for https://github.com/sagemathinc/cocalc/issues/1899
129
- const poll_window = setInterval ( ( ) => {
141
+ const pollWindow = setInterval ( ( ) => {
130
142
if ( w . closed !== false ) {
131
143
// != is required for compatibility with Opera
132
- clearInterval ( poll_window ) ;
133
- this . close_video_chat_window ( ) ;
144
+ clearInterval ( pollWindow ) ;
145
+ this . closeVideoChatWindow ( ) ;
134
146
}
135
147
} , 1000 ) ;
136
- }
148
+ } ;
137
149
138
150
// User wants to close the video chat window, but not via just clicking the
139
151
// close button on the popup window
140
- public close_video_chat_window ( ) : void {
141
- const room_id = this . chatroom_id ( ) ;
142
- const w = video_windows [ room_id ] ;
143
- if ( ! w ) return ;
152
+ private closeVideoChatWindow = ( ) : void => {
153
+ const room_id = this . chatroomId ( ) ;
154
+ const w = videoWindows [ room_id ] ;
155
+ if ( ! w ) {
156
+ return ;
157
+ }
144
158
redux
145
159
. getActions ( "file_use" )
146
160
?. mark_file ( this . project_id , this . path , "video" , 0 , true , new Date ( 0 ) ) ;
147
- clearInterval ( this . video_interval_id ) ;
148
- delete video_windows [ room_id ] ;
161
+ if ( this . intervalId ) {
162
+ clearInterval ( this . intervalId ) ;
163
+ delete this . intervalId ;
164
+ }
165
+ delete videoWindows [ room_id ] ;
149
166
w . close ( ) ;
150
- }
167
+ } ;
151
168
}
0 commit comments