예제 #1
0
function Websites_seo_post()
{
    if (empty($_REQUEST['streamName'])) {
        throw new Q_Exception_RequiredField(array('field' => 'streamName'));
    }
    $prefix = "Websites/seo/";
    if (substr($_REQUEST['streamName'], 0, strlen($prefix)) !== $prefix) {
        throw new Q_Exception_WrongValue(array('field' => 'streamName', 'range' => "string beginning with {$prefix}"));
    }
    $user = Users::loggedInUser(true);
    $publisherId = Users::communityId();
    $type = "Websites/seo";
    if (!Streams::isAuthorizedToCreate($user->id, $publisherId, $type)) {
        throw new Users_Exception_NotAuthorized();
    }
    $stream = new Streams_Stream($publisherId);
    $stream->publisherId = $publisherId;
    $stream->name = $_REQUEST['streamName'];
    $stream->type = $type;
    if (isset($_REQUEST['uri'])) {
        $stream->setAttribute('uri', $_REQUEST['uri']);
    }
    $stream->save();
    $stream->post($user->id, array('type' => 'Streams/created', 'content' => '', 'instructions' => Q::json_encode($stream->toArray())), true);
    $stream->subscribe();
    // autosubscribe to streams you yourself create, using templates
    Q_Response::setSlot('stream', $stream->exportArray());
}
예제 #2
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;
 }