Bladeren bron

Auto-create video chat room - refs #7558

Angel Fernando Quiroz Campos 9 jaren geleden
bovenliggende
commit
d28facfce8

+ 11 - 50
main/inc/ajax/chat.ajax.php

@@ -69,62 +69,21 @@ switch ($action) {
         $status = isset($_REQUEST['status']) ? intval($_REQUEST['status']) : 0;
         $chat->setUserStatus($status);
         break;
-    case 'start_video':
-        $room = VideoChat::getChatRoomByUsers(api_get_user_id(), $to_user_id);
-
-        if ($room !== false) {
-            $videoChatLink = Display::url(
-                Display::returnFontAswesomeIcon('video-camera') . get_lang('StartVideoChat'),
-                api_get_path(WEB_LIBRARY_JS_PATH) . "chat/video.php?room={$room['room_name']}"
-            );
-
-            $chat->send(
-                api_get_user_id(),
-                $to_user_id,
-                $videoChatLink,
-                false,
-                false
-            );
-
-            echo Display::tag('p', $videoChatLink, ['class' => 'lead']);
-            break;
-        }
-
-        $form = new FormValidator('start_video_chat');
-        $form->addText('chat_room_name', get_lang('ChatRoomName'), false);
-        $form->addHidden('to', $to_user_id);
-        $form->addButtonSend(get_lang('Create'));
-
-        $template = new Template();
-        $template->assign('form', $form->returnForm());
-
-        echo $template->fetch('default/javascript/chat/start_video.tpl');
-        break;
     case 'create_room':
         $room = VideoChat::getChatRoomByUsers(api_get_user_id(), $to_user_id);
-        $createdRoom = false;
 
         if ($room === false) {
-            $roomName = isset($_REQUEST['room_name']) ? Security::remove_XSS($_REQUEST['room_name']) : null;
-
-            if (VideoChat::nameExists($roomName)) {
-                echo Display::return_message(get_lang('TheVideoChatRoomXNameAlreadyExists'), 'error');
+            $createdRoom = VideoChat::createRoom(api_get_user_id(), $to_user_id);
 
+            if ($createdRoom === false) {
+                echo Display::return_message(get_lang('ChatRoomNotCreated'), 'error');
                 break;
             }
 
-            $createdRoom = VideoChat::createRoom($roomName, api_get_user_id(), $to_user_id);
-        } else {
-            $roomName = $room['room_name'];
-            $createdRoom = true;
-        }
-
-        if ($createdRoom === false) {
-            echo Display::return_message(get_lang('ChatRoomNotCreated'), 'error');
-            break;
+            $room = VideoChat::getChatRoomByUsers(api_get_user_id(), $to_user_id);
         }
 
-        $videoChatUrl = api_get_path(WEB_LIBRARY_JS_PATH) . "chat/video.php?room=$roomName";
+        $videoChatUrl = api_get_path(WEB_LIBRARY_JS_PATH) . "chat/video.php?room={$room['room_name']}";
         $videoChatLink = Display::url(
             Display::returnFontAswesomeIcon('video-camera') . get_lang('StartVideoChat'),
             $videoChatUrl
@@ -138,10 +97,12 @@ switch ($action) {
             false
         );
 
-        echo json_encode([
-            'name' => $roomName,
-            'url' =>  $videoChatUrl
-        ]);
+        $videoChatLink = Display::url(
+            Display::returnFontAswesomeIcon('video-camera') . get_lang('StartVideoChat'),
+            api_get_path(WEB_LIBRARY_JS_PATH) . "chat/video.php?room={$room['room_name']}"
+        );
+
+        echo Display::tag('p', $videoChatLink, ['class' => 'lead']);
         break;
     case 'notify_not_support':
         $chat->send(api_get_user_id(), $to_user_id, get_lang('TheXUserBrowserDoesNotSupportWebRTC'));

+ 10 - 4
main/inc/lib/VideoChat.php

@@ -41,20 +41,26 @@ class VideoChat
 
     /**
      * Create a video chat
-     * @param string $name The video chat name
      * @param int $fromUser The sender user
      * @param int $toUser The receiver user
-     *
      * @return int The created video chat id. Otherwise return false
      */
-    public static function createRoom($name, $fromUser, $toUser)
+    public static function createRoom($fromUser, $toUser)
     {
+        $fromUserInfo = api_get_user_info($fromUser);
+        $toUserInfo = api_get_user_info($toUser);
+
+        $chatName = vsprintf(
+            get_lang('VideoChatBetweenUserXAndUserY'),
+            [$fromUserInfo['firstname'], $toUserInfo['firstname']]
+        );
+
         return Database::insert(
             Database::get_main_table(TABLE_MAIN_CHAT_VIDEO),
             [
                 'from_user' => intval($fromUser),
                 'to_user' => intval($toUser),
-                'room_name' => $name,
+                'room_name' => $chatName,
                 'datetime' => api_get_utc_datetime()
             ]
         );

+ 9 - 25
main/inc/lib/javascript/chat/js/chat.js

@@ -384,34 +384,18 @@ function createChatBox(user_id, chatboxtitle, minimizeChatBox, online, userImage
             (online === '1' || online === 1)
         ) {
 		$('<a>')
-			.addClass('btn btn-xs')
+			.addClass('btn btn-xs ajax')
 			.attr({
-				href: '#'
+				href: ajax_url + '?action=create_room&to=' + user_id
 			})
+                        .data({
+                            title: '<i class="fa fa-video-camera"></i>',
+                            size: 'sm'
+                        })
+                        .on('click', function () {
+                            $(this).data('title', $('.chatboxtitle').text());
+                        })
 			.html('<i class="fa fa-video-camera"></i>')
-			.on('click', function(e) {
-				e.preventDefault();
-
-				var createForm = $.get(
-					ajax_url,
-					{
-						action: 'start_video',
-						to: user_id
-					}
-				);
-
-				$.when(createForm).done(function(response) {
-					$('#global-modal')
-						.find('.modal-dialog')
-						.removeClass('modal-lg');
-
-					$('#global-modal')
-						.find('.modal-body')
-						.html(response);
-
-					$('#global-modal').modal('show');
-				});
-			})
 			.appendTo(chatboxoptions);
 	}
 

+ 0 - 26
main/template/default/javascript/chat/start_video.tpl

@@ -1,26 +0,0 @@
-{{ form }}
-
-<script>
-    $('form[name="start_video_chat"]').on('submit', function(e) {
-        e.preventDefault();
-
-        var createChatRoom = $.post(
-            '{{ _p.web_ajax }}chat.ajax.php',
-            {
-                room_name: $(this).find('input[name="chat_room_name"]').val(),
-                to: $(this).find('input[name="to"]').val(),
-                action: 'create_room'
-            },
-            null,
-            'json'
-        );
-
-        $.when(createChatRoom).done(function(response) {
-            if (!response.url) {
-                return;
-            }
-
-            window.location.href = response.url;
-        });
-    });
-</script>