Пример #1
0
 /**
  * Unsubscribe from one or more streams, to stop receiving notifications.
  * Pooststs "Streams/unsubscribe" message to the streams.
  * Also posts "Streams/unsubscribed" messages to user's "Streams/participating" stream.
  * Does not change the actual subscription, but only the participant row.
  * (When subscribing again, the existing subscription will be used.)
  * @method unsubscribe
  * @static
  * @param {string} $asUserId The id of the user that is joining. Pass null here to use the logged-in user's id.
  * @param {string} $publisherId The id of the user publishing all the streams
  * @param {array} $streams An array of Streams_Stream objects or stream names
  * @param {array} [$options=array()]
  * @param {boolean} [$options.leave] set to true to also leave the streams
  * @param {boolean} [$options.skipAccess] if true, skip access check for whether user can join and subscribe
  * @return {array} Returns an array of Streams_Participant rows, if any were in the database.
  */
 static function unsubscribe($asUserId, $publisherId, $streams, $options = array())
 {
     $streams2 = self::_getStreams($asUserId, $publisherId, $streams);
     $streamNames = array();
     foreach ($streams2 as $s) {
         $streamNames[] = $s->name;
     }
     if (empty($options['skipAccess'])) {
         self::_accessExceptions($streams2, $streamNames, 'join');
     }
     $skipAccess = Q::ifset($options, 'skipAccess', false);
     if (empty($options['leave'])) {
         $criteria = array('publisherId' => $publisherId, 'streamName' => $streamNames, 'userId' => $asUserId);
         Streams_Participant::update()->set(array('subscribed' => 'no'))->where($criteria)->execute();
         $participants = Streams_Participant::select('*')->where($criteria)->fetchDbRows();
     } else {
         $participants = Streams::leave($asUserId, $publisherId, $streams2, compact('skipAccess'));
     }
     $messages = array();
     $pMessages = array();
     foreach ($streamNames as $sn) {
         $stream = $streams2[$sn];
         if ($participant = Q::ifset($participants, $sn, null)) {
             if ($participant instanceof Streams_Participant) {
                 $participant = $participant->toArray();
             }
         }
         // Send a message to Node
         Q_Utils::sendToNode(array("Q/method" => "Streams/Stream/unsubscribe", "participant" => Q::json_encode($participant), "stream" => Q::json_encode($stream->toArray())));
         // Stream messages to post
         $messages[$publisherId][$sn] = array('type' => 'Streams/unsubscribe');
         $pMessages[] = array('type' => 'Streams/unsubscribed', 'instructions' => array('publisherId' => $publisherId, 'streamName' => $sn));
     }
     Streams_Message::postMessages($asUserId, $messages, true);
     Streams_Message::postMessages($asUserId, array($asUserId => array('Streams/participating' => $pMessages)), true);
     return $participants;
 }