/** * 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; }
/** * If the user is participating in the stream, sets state of participant row * as "left" and posts a "Streams/leave" type message to the stream. * Also posts "Streams/left" message on user's "Streams/participating" stream * @method leave * @param $options=array() {array} An associative array of options. * @param {string} [$options.userId] The user who is leaving the stream. Defaults to the logged-in user. * @param {string} [$options.skipAccess] If true, skip access check for whether user can join * @param $participant=null {reference} * Optional reference to a participant object that will be filled * to point to the participant object, if any. * @return {Streams_Participant|null} */ function leave($options = array(), &$participant = null) { $userId = $this->_verifyUser($options); $participants = Streams::leave($userId, $this->publisherId, array($this->name), $options); return $participants ? reset($participants) : null; }