<commit_before><?php
require STREAMS_PLUGIN_DIR.DS.'vendor'.DS.'autoload.php';

use Twilio\Jwt\AccessToken;
use Twilio\Jwt\Grants\VideoGrant;
use Twilio\Rest\Client;
/**
 * @module Streams
 */
/**
 * Class contains common methods for Webrtc tool
 *
 * @class Streams_WebRTC
 */
class Streams_Webrtc
{
    static function createRoom($publisherId, $streamName) {
        $loggedUserId = Users::loggedInUser(true)->id;

        if(isset($publisherId) && !empty($publisherId) && isset($streamName) && !empty($streamName)) {
            $stream = Streams::create($publisherId, $publisherId, 'Streams/webrtc', [
                'name' => 'Streams/webrtc/' . $streamName,
            ]);

        } else {
            $stream = Streams::create($loggedUserId, $loggedUserId, 'Streams/webrtc');
        }
        //print_r($stream);die;

        $roomName = str_replace("Streams/webrtc","", $stream->name);
        $twilioRoom = static::createTwilioRoom();
        $stream->setAttribute('twilioRoomSid', $twilioRoom->sid);
        $stream->setAttribute('twilioRoomName', $twilioRoom->uniqueName);
        $stream->save();

        return $stream;
    }
    static function joinRoom($loggedUserId, $publisherId, $streamName) {
        $stream = Streams::fetchOne($loggedUserId, $publisherId, $streamName);
        $participants = $stream->getParticipants(array(
            "state" => "participating"
        ));
        if (!isset($participants[$loggedUserId])) {
            $stream->join();
        } else {

            Streams_Message::post(null, $publisherId, $streamName, array(
                'type' => 'Streams/connected'
            ), true);
        }

        $twilioRoomSid = $stream->getAttribute('twilioRoomSid');
        $twilioRoomName = $stream->getAttribute('twilioRoomName');
        if(!empty($twilioRoomSid)){
            try {
                $twilioRoom = static::getTwilioRoom($twilioRoomName);
            } catch (Exception $e) {
                //die('catch');
                $twilioRoom = static::createTwilioRoom();
                $stream->setAttribute('twilioRoomSid', $twilioRoom->sid);
                $stream->setAttribute('twilioRoomName', $twilioRoom->uniqueName);
                $stream->save();
            }

        }

        //print_r($twilioRoom);die('1213');
        return $stream;
    }

    static function createTwilioRoom() {
        //die($roomName);
        $twilioAccountSid = Q_Config::expect('Streams', 'twilio', 'accountSid');
        $twilioApiKey = Q_Config::expect('Streams', 'twilio', 'apiKey');
        $twilioApiSecret = Q_Config::expect('Streams', 'twilio', 'apiSecret');
        $authToken = Q_Config::expect('Streams', 'twilio', 'authToken');


        $twilio = new Client($twilioApiKey, $twilioApiSecret, $twilioAccountSid);
        $roomUniqueName = static::makeRandomName();
        $room = $twilio->video->v1->rooms->create(array("uniqueName" => $roomUniqueName));<commit_msg>Fixed Webrtc.php<commit_after><?php
require STREAMS_PLUGIN_DIR.DS.'vendor'.DS.'autoload.php';

use Twilio\Jwt\AccessToken;
use Twilio\Jwt\Grants\VideoGrant;
use Twilio\Rest\Client;
/**
 * @module Streams
 */
/**
 * Class contains common methods for Webrtc tool
 *
 * @class Streams_WebRTC
 */
class Streams_Webrtc
{
    static function createRoom($publisherId, $streamName) {
        $loggedUserId = Users::loggedInUser(true)->id;

        if(isset($publisherId) && !empty($publisherId) && isset($streamName) && !empty($streamName)) {
            $stream = Streams::create($publisherId, $publisherId, 'Streams/webrtc', [
                'name' => 'Streams/webrtc/' . $streamName,
            ]);

        } else {
            $stream = Streams::create($loggedUserId, $loggedUserId, 'Streams/webrtc');
        }
        //print_r($stream);die;

        $roomName = str_replace("Streams/webrtc","", $stream->name);
        $twilioRoom = static::createTwilioRoom();
        $stream->setAttribute('twilioRoomSid', $twilioRoom->sid);
        $stream->setAttribute('twilioRoomName', $twilioRoom->uniqueName);
        $stream->changed();

        return $stream;
    }
    static function joinRoom($loggedUserId, $publisherId, $streamName) {
        $stream = Streams::fetchOne($loggedUserId, $publisherId, $streamName);
        $participants = $stream->getParticipants(array(
            "state" => "participating"
        ));
        if (!isset($participants[$loggedUserId])) {
            $stream->join();
        } else {

            Streams_Message::post(null, $publisherId, $streamName, array(
                'type' => 'Streams/connected'
            ), true);
        }

        $twilioRoomSid = $stream->getAttribute('twilioRoomSid');
        $twilioRoomName = $stream->getAttribute('twilioRoomName');
		if (!$twilioRoomSid) {
            $twilioRoom = static::createTwilioRoom();
            $stream->setAttribute('twilioRoomSid', $twilioRoom->sid);
            $stream->setAttribute('twilioRoomName', $twilioRoom->uniqueName);
            $stream->changed();
		} else {
            try {
                $twilioRoom = static::getTwilioRoom($twilioRoomName);
            } catch (Exception $e) {
                //die('catch');
                $twilioRoom = static::createTwilioRoom();
                $stream->setAttribute('twilioRoomSid', $twilioRoom->sid);
                $stream->setAttribute('twilioRoomName', $twilioRoom->uniqueName);
                $stream->changed();
            }

        }

        //print_r($twilioRoom);die('1213');
        return $stream;
    }

    static function createTwilioRoom() {
        //die($roomName);
        $twilioAccountSid = Q_Config::expect('Streams', 'twilio', 'accountSid');
        $twilioApiKey = Q_Config::expect('Streams', 'twilio', 'apiKey');
        $twilioApiSecret = Q_Config::expect('Streams', 'twilio', 'apiSecret');
        $authToken = Q_Config::expect('Streams', 'twilio', 'authToken');


        $twilio = new Client($twilioApiKey, $twilioApiSecret, $twilioAccountSid);
        $roomUniqueName = static::makeRandomName();
        $room = $twilio->video->v1->rooms->create(array("uniqueName" => $roomUniqueName));
{'fraction_non_alphanumeric': 0.15977781408266623, 'fraction_numerical': 0.0016337199803953602, 'mean_word_length': 2.7954122752634842, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2762301', 'n_tokens_mistral': 1883, 'n_tokens_neox': 1857, 'n_words': 275}