Example #1
0
/**
 * Used to create a new stream
 *
 * @param {array} $_REQUEST 
 * @param {String} [$_REQUEST.title] Required. The title of the interest.
 * @param {String} [$_REQUEST.publisherId] Optional. Defaults to the app name.
 * @return {void}
 */
function Streams_interest_delete()
{
    $user = Users::loggedInUser(true);
    $title = Q::ifset($_REQUEST, 'title', null);
    if (!isset($title)) {
        throw new Q_Exception_RequiredField(array('field' => 'title'));
    }
    $app = Q_Config::expect('Q', 'app');
    $publisherId = Q::ifset($_REQUEST, 'publisherId', $app);
    $name = 'Streams/interest/' . Q_Utils::normalize($title);
    $stream = Streams::fetchOne(null, $publisherId, $name);
    if (!$stream) {
        throw new Q_Exception_MissingRow(array('table' => 'stream', 'criteria' => Q::json_encode(compact('publisherId', 'name'))));
    }
    $miPublisherId = $user->id;
    $miName = 'Streams/user/interests';
    $myInterests = Streams::fetchOne($user->id, $miPublisherId, $miName);
    if (!$myInterests) {
        throw new Q_Exception_MissingRow(array('table' => 'stream', 'criteria' => Q::json_encode(array('publisherId' => $miPublisherId, 'name' => $miName))));
    }
    $stream->leave();
    Streams::unrelate($user->id, $user->id, 'Streams/user/interests', 'Streams/interest', $publisherId, $name, array('adjustWeights' => true));
    Q_Response::setSlot('publisherId', $publisherId);
    Q_Response::setSlot('streamName', $name);
    /**
     * Occurs when the logged-in user has successfully removed an interest via HTTP
     * @event Streams/interest/delete {after}
     * @param {string} publisherId The publisher of the interest stream
     * @param {string} title The title of the interest
     * @param {Users_User} user The logged-in user
     * @param {Streams_Stream} stream The interest stream
     * @param {Streams_Stream} myInterests The user's "Streams/user/interests" stream
     */
    Q::event("Streams/interest/remove", compact('publisherId', 'title', 'subscribe', 'user', 'stream', 'myInterests'), 'after');
}
Example #2
0
function Streams_related_delete($params)
{
    $user = Users::loggedInUser(true);
    $asUserId = $user->id;
    $toPublisherId = $_REQUEST['toPublisherId'];
    $toStreamName = $_REQUEST['toStreamName'];
    $type = $_REQUEST['type'];
    $fromPublisherId = $_REQUEST['fromPublisherId'];
    $fromStreamName = $_REQUEST['fromStreamName'];
    // TODO: When we start supporting multiple hosts, this will have to be rewritten
    // to make servers communicate with one another when establishing relations between streams
    if (!($stream = Streams::fetch($asUserId, $toPublisherId, $toStreamName))) {
        Q_Response::setSlot('result', false);
    }
    if (!($stream = Streams::fetch($asUserId, $fromPublisherId, $fromStreamName))) {
        Q_Response::setSlot('result', false);
    }
    Streams::unrelate($asUserId, $toPublisherId, $toStreamName, $type, $fromPublisherId, $fromStreamName);
    Q_Response::setSlot('result', true);
}
Example #3
0
 function unrelateFrom($fromStream, $type, $asUserId = null, $options = array())
 {
     return Streams::unrelate($asUserId, $this->publisherId, $this->name, $type, $fromStream->publisherId, $fromStream->name, $options);
 }
Example #4
0
 /**
  * Closes a stream, which prevents anyone from posting messages to it
  * unless they have WRITE_LEVEL >= "close", as well as attempting to remove
  * all relations to other streams. A "cron job" can later go and delete
  * closed streams. The reason you should avoid deleting streams right away
  * is that other subscribers may still want to receive the last messages
  * posted to the stream.
  * @method close
  * @param {string} $asUserId The id of the user who would be closing the stream
  * @param {string} $publisherId The id of the user publishing the stream
  * @param {string} $streamName The name of the stream
  * @param {array} [$options=array()] Can include "skipAccess"
  * @static
  */
 static function close($asUserId, $publisherId, $streamName, $options = array())
 {
     $stream = new Streams_Stream();
     $stream->publisherId = $publisherId;
     $stream->name = $streamName;
     if (!$stream->retrieve()) {
         throw new Q_Exception_MissingRow(array('table' => 'stream', 'criteria' => "{publisherId: '{$publisherId}', name: '{$streamName}'}"));
     }
     // Authorization check
     if (empty($options['skipAccess'])) {
         if ($asUserId !== $publisherId) {
             $stream->calculateAccess($asUserId);
             if (!$stream->testWriteLevel('close')) {
                 throw new Users_Exception_NotAuthorized();
             }
         }
     }
     // Clean up relations from other streams to this category
     list($relations, $related) = Streams::related($asUserId, $stream->publisherId, $stream->name, true);
     foreach ($relations as $r) {
         try {
             Streams::unrelate($asUserId, $r->fromPublisherId, $r->fromStreamName, $r->type, $stream->publisherId, $stream->name);
         } catch (Exception $e) {
         }
     }
     // Clean up relations from this stream to categories
     list($relations, $related) = Streams::related($asUserId, $stream->publisherId, $stream->name, false);
     foreach ($relations as $r) {
         try {
             Streams::unrelate($asUserId, $r->toPublisherId, $r->toStreamName, $r->type, $stream->publisherId, $stream->name);
         } catch (Exception $e) {
         }
     }
     $result = false;
     try {
         $db = $stream->db();
         $stream->closedTime = $closedTime = $db->toDateTime($db->getCurrentTimestamp());
         if ($stream->save()) {
             $stream->post($asUserId, array('type' => 'Streams/closed', 'content' => '', 'instructions' => compact('closedTime')), true);
             $result = true;
         }
     } catch (Exception $e) {
         throw $e;
     }
     return $result;
 }