Example #1
0
function Streams_participating_response()
{
    if (!Q_Request::isAjax()) {
        return;
    }
    $max_limit = Q_Config::expect('Streams', 'db', 'limits', 'participating');
    $user = Users::loggedInUser(true);
    $type = Streams::requestedType();
    $limit = Streams::requestedField('limit', false, $max_limit);
    if ($limit > $max_limit) {
        throw new Q_Exception("limit is too large, must be <= {$max_limit}");
    }
    $offset = Streams::requestedField('offset', false, 0);
    $order = Streams::requestedField('order', false, true);
    $participating = array();
    $q = Streams_Participating::select('*')->where(array('userId' => $user->id));
    if ($type) {
        $q = $q->where(array('streamName' => new Db_Range($type . '/', true, false, true)));
    }
    if ($limit) {
        $q = $q->limit($limit, $offset);
    }
    if ($order) {
        $q = $q->orderBy('updatedTime', false);
    }
    $res_participating = $q->fetchDbRows();
    foreach ($res_participating as $part) {
        $part_safe = $part->exportArray();
        if (isset($part_safe)) {
            $participating[] = $part_safe;
        }
    }
    Q_Response::setSlot('participating', $participating);
    if (!Q_Request::slotName('streams')) {
        return;
    }
    $res_streams = array();
    $streamNames = array();
    foreach ($res_participating as $p) {
        $streamNames[$p->publisherId][] = $p->streamName;
    }
    foreach ($streamNames as $p_id => $names) {
        $res_streams[$p_id] = Streams::fetch($user->id, $p_id, $names);
    }
    $streams = array();
    $o = array('asUserId' => $user->id);
    foreach ($res_streams as $publisherId => $streams_array) {
        if (!empty($streams_array)) {
            $streams[$publisherId] = array();
            foreach ($streams_array as $streamName => $stream) {
                $streams[$publisherId][$streamName] = $stream->exportArray($o);
            }
        }
    }
    Q_Response::setSlot('streams', $streams);
}
Example #2
0
/**
 * Adds contacts to the system. Fills the "contacts" slot.
 * @param {array} $_REQUEST
 * @param {string} $_REQUEST.label The label of the contact
 * @param {string} $_REQUEST.contactUserId The contactUserId of the contact
 * @param {string} [$_REQUEST.nickname] The nickname of the contact
 * @param {string} [$_REQUEST.userId=Users::loggedInUser(true)->id] You can override the user id, if another plugin adds a hook that allows you to do this
 */
function Users_contact_post($params = array())
{
    if (Q_Request::slotName('batch') or Q_Request::slotName('contacts')) {
        return;
    }
    $req = array_merge($_REQUEST, $params);
    Q_Valid::requireFields(array('label', 'contactUserId'), $req, true);
    $loggedInUserId = Users::loggedInUser(true)->id;
    $userId = Q::ifset($req, 'userId', $loggedInUserId);
    $contactUserId = $req['contactUserId'];
    $nickname = Q::ifset($req, 'nickname', null);
    $contacts = Users_Contact::addContact($userId, $req['label'], $contactUserId, $nickname);
    Q_Response::setSlot('contacts', Db::exportArray($contacts));
}
Example #3
0
function Streams_related_response()
{
    if (!Q_Request::slotName('relations') and !Q_Request::slotName('streams')) {
        return;
    }
    $user = Users::loggedInUser();
    $asUserId = $user ? $user->id : '';
    $publisherId = Streams::requestedPublisherId(true);
    $streamName = Streams::requestedName(true, 'original');
    $isCategory = !empty($_REQUEST['isCategory']);
    $slotNames = Q_Request::slotNames();
    $streams_requested = in_array('relatedStreams', $slotNames);
    $options = array('relationsOnly' => !$streams_requested, 'orderBy' => !empty($_REQUEST['ascending']));
    if (isset($_REQUEST['limit'])) {
        $options['limit'] = $_REQUEST['limit'];
    }
    if (isset($_REQUEST['offset'])) {
        $options['offset'] = $_REQUEST['offset'];
    }
    if (isset($_REQUEST['min'])) {
        $options['min'] = $_REQUEST['min'];
    }
    if (isset($_REQUEST['max'])) {
        $options['max'] = $_REQUEST['max'];
    }
    if (isset($_REQUEST['type'])) {
        $options['type'] = $_REQUEST['type'];
    }
    if (isset($_REQUEST['prefix'])) {
        $options['prefix'] = $_REQUEST['prefix'];
    }
    $result = Streams::related($asUserId, $publisherId, $streamName, $isCategory, $options);
    if ($streams_requested) {
        $rel = Db::exportArray($result[0], array('numeric' => true));
    } else {
        $rel = Db::exportArray($result, array('numeric' => true));
    }
    if (!empty($_REQUEST['omitRedundantInfo'])) {
        if ($isCategory) {
            foreach ($rel as &$r) {
                unset($r['toPublisherId']);
                unset($r['toStreamName']);
            }
        } else {
            foreach ($rel as &$r) {
                unset($r['fromPublisherId']);
                unset($r['fromStreamName']);
            }
        }
    }
    Q_Response::setSlot('relations', $rel);
    if (!$streams_requested) {
        return;
    }
    $streams = $result[1];
    $arr = Db::exportArray($streams, array('numeric' => true));
    foreach ($arr as $k => $stream) {
        if (!$stream) {
            continue;
        }
        $s = $streams[$stream['name']];
        $arr[$k]['access'] = array('readLevel' => $s->get('readLevel', $s->readLevel), 'writeLevel' => $s->get('writeLevel', $s->writeLevel), 'adminLevel' => $s->get('adminLevel', $s->adminLevel));
    }
    Q_Response::setSlot('relatedStreams', $arr);
    $stream = $result[2];
    if (is_array($stream)) {
        Q_Response::setSlot('streams', Db::exportArray($stream));
        return;
    } else {
        if (is_object($stream)) {
            Q_Response::setSlot('stream', $stream->exportArray());
        } else {
            Q_Response::setSlot('stream', false);
        }
    }
    if (!empty($_REQUEST['messages'])) {
        $max = -1;
        $limit = $_REQUEST['messages'];
        $messages = false;
        $type = isset($_REQUEST['messageType']) ? $_REQUEST['messageType'] : null;
        if ($stream->testReadLevel('messages')) {
            $messages = Db::exportArray($stream->getMessages(compact('type', 'max', 'limit')));
        }
        Q_Response::setSlot('messages', $messages);
    }
    if (!empty($_REQUEST['participants'])) {
        $limit = $_REQUEST['participants'];
        $offset = -1;
        $participants = false;
        if ($stream->testReadLevel('participants')) {
            $participants = Db::exportArray($stream->getParticipants(compact('limit', 'offset')));
        }
        Q_Response::setSlot('participants', $participants);
    }
}
Example #4
0
/**
 * Used to get a stream
 *
 * @param {array} $_REQUEST 
 * @param {string} $_REQUEST.publisherId Required
 * @param {string} $_REQUEST.streamName Required streamName or name
 * @param {integer} [$_REQUEST.messages] optionally pass a number here to fetch latest messages
 * @param {integer} [$_REQUEST.participants] optionally pass a number here to fetch participants
 * @return {void}
 */
function Streams_stream_response()
{
    // this handler is only for GET requests
    if (Q_Request::method() !== 'GET') {
        return null;
    }
    $publisherId = Streams::requestedPublisherId(true);
    $name = Streams::requestedName(true);
    $fields = Streams::requestedFields();
    $user = Users::loggedInUser();
    $userId = $user ? $user->id : "";
    if (isset(Streams::$cache['stream'])) {
        $stream = Streams::$cache['stream'];
    } else {
        $streams = Streams::fetch($userId, $publisherId, $name, $fields ? $fields : '*', array('withParticipant' => true));
        if (Q_Request::slotName('streams')) {
            Q_Response::setSlot('streams', Db::exportArray($streams));
        }
        if (empty($streams)) {
            if (Q_Request::slotName('stream')) {
                Q_Response::setSlot('stream', null);
                Q_Response::setSlot('messages', null);
                Q_Response::setSlot('participants', null);
                Q_Response::setSlot('related', null);
                Q_Response::setSlot('relatedTo', null);
            } else {
                if (!Q_Request::slotName('streams')) {
                    $app = Q_Config::expect('Q', 'app');
                    Q_Dispatcher::forward("{$app}/notFound");
                }
            }
            return null;
        }
        // The rest of the data is joined only on the first stream
        Streams::$cache['stream'] = $stream = reset($streams);
    }
    if (empty($stream)) {
        if (Q_Request::slotName('stream')) {
            Q_Response::setSlot('stream', null);
        }
        return null;
    }
    if ($userId && !empty($_REQUEST['join'])) {
        $stream->join();
        // NOTE: one of the rare times we may change state in a response handler
    }
    if (Q_Request::slotName('stream')) {
        Q_Response::setSlot('stream', $stream->exportArray());
    }
    if (!empty($_REQUEST['messages'])) {
        $max = -1;
        $limit = $_REQUEST['messages'];
        $messages = false;
        $type = isset($_REQUEST['messageType']) ? $_REQUEST['messageType'] : null;
        if ($stream->testReadLevel('messages')) {
            $messages = Db::exportArray($stream->getMessages(compact('type', 'max', 'limit')));
        }
        Q_Response::setSlot('messages', $messages);
    }
    if (!empty($_REQUEST['participants'])) {
        $limit = $_REQUEST['participants'];
        $participants = false;
        if ($stream->testReadLevel('participants')) {
            $participants = Db::exportArray($stream->getParticipants(compact('limit', 'offset')));
        }
        Q_Response::setSlot('participants', $participants);
    }
}