function Streams_after_Streams_Stream_save_Streams_greeting($params) { $s = $params['stream']; $parts = explode('/', $s->name, 3); if (count($parts) < 3) { throw new Q_Exception_WrongValue(array('field' => 'stream name', 'range' => 'Streams/greeting/$communityId')); } $communityId = $parts[2]; $p = new Streams_Participant(); $p->publisherId = $communityId; $p->streamName = "Streams/community/main"; $p->userId = $s->publisherId; if ($p->retrieve()) { $p->setExtra('Streams/greeting', $s->content); $p->save(); } }
/** * 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 * @method leave * @param $options=array() {array} * An associative array of options. The keys can be:<br/> * "userId": The user who is leaving the stream. Defaults to the logged-in user. * "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 {boolean} */ function leave($options = array(), &$participant = null) { $stream = $this->fetchAsUser($options, $userId); if (empty($options['skipAccess']) and !$stream->testWriteLevel('join')) { if (!$stream->testReadLevel('see')) { throw new Streams_Exception_NoSuchStream(); } throw new Users_Exception_NotAuthorized(); } $participant = new Streams_Participant(); $participant->publisherId = $stream->publisherId; $participant->streamName = $stream->name; $participant->userId = $userId; if (!$participant->retrieve()) { throw new Q_Exception_MissingRow(array('table' => 'participant', 'criteria' => "userId = {$userId}, publisherId = {$stream->publisherId}, name = {$stream->name}")); } // Remove from participant list if ($participant->state === 'left') { return false; } $participant->state = 'left'; if (!$participant->save()) { return false; } Q_Utils::sendToNode(array("Q/method" => "Streams/Stream/leave", "participant" => Q::json_encode($participant->toArray()), "stream" => Q::json_encode($stream->toArray()))); // Post Streams/leave message to the stream $stream->post($userId, array('type' => 'Streams/leave'), true); // Now post Streams/left message to Streams/participating Streams_Message::post($userId, $userId, 'Streams/participating', array('type' => 'Streams/left', 'content' => '', 'instructions' => Q::json_encode(array('publisherId' => $stream->publisherId, 'streamName' => $stream->name))), true); return true; }
/** * Create or update subscription */ function Streams_subscription_put($params) { $items = array(); $subscribed = 'no'; $updateTemplate = true; $streamName = Streams::requestedName(); $publisherId = Streams::requestedPublisherId(true); $user = Users::loggedInUser(true); extract($_REQUEST); $items = json_decode($items, true); $stream = Streams::fetchOne($user->id, $publisherId, $streamName); if (!$stream) { throw new Q_Exception_MissingRow(array('table' => 'stream', 'criteria' => compact('publisherId', 'streamName'))); } $rules = Streams_Rule::select('*')->where(array('ofUserId' => $user->id, 'publisherId' => $publisherId, 'streamName' => $streamName))->fetchDbRows(null, '', 'ordinal'); $types = Q_Config::get('Streams', 'types', $stream->type, 'messages', array()); if ($subscribed !== 'no') { // update rules while ($item = array_pop($items)) { // join "grouped" message types to $items foreach ($types as $type => $msg) { if ($msg['title'] == $item['filter']->labels and $type != $item['filter']->types) { $items[] = (object) array('deliver' => $item->deliver, 'filter' => array('types' => $type, 'labels' => $msg['title'], 'notifications' => $item['filter']->notifications)); } } if (!($rule = array_pop($rules))) { $rule = new Streams_Rule(); $rule->ofUserId = $user->id; $rule->publisherId = $publisherId; $rule->streamName = $streamName; $rule->relevance = 1; } $rule->filter = Q::json_encode($item['filter']); $rule->deliver = Q::json_encode($item['deliver']); $rule->save(); } } foreach ($rules as $rule) { $rule->remove(); } $streams_subscription = new Streams_Subscription(); $streams_subscription->streamName = $streamName; $streams_subscription->publisherId = $publisherId; $streams_subscription->ofUserId = $user->id; $streams_subscription->filter = Q::json_encode(array()); $streams_subscription->retrieve(); $streams_participant = new Streams_Participant(); $streams_participant->publisherId = $publisherId; $streams_participant->streamName = $streamName; $streams_participant->userId = $user->id; $streams_participant->state = 'participating'; $streams_participant->reason = ''; $streams_participant->retrieve(); $streams_participant->subscribed = $subscribed; $streams_participant->save(); if ($subscribed === 'yes') { $stream->subscribe(array('skipRules' => true)); } else { $stream->unsubscribe(); } }
/** * Assigns unique id to 'token' field if not set * Saves corresponding row in Streams_Invited table * Inserting a new invite affects corresponding row in Streams_Participant table * @method beforeSave * @param {array} $modifiedFields * The fields that have been modified * @return {array} */ function beforeSave($modifiedFields) { if (!$this->retrieved) { if (!isset($modifiedFields['token'])) { $this->token = $modifiedFields['token'] = self::db()->uniqueId(self::table(), 'token', array('length' => Q_Config::get('Streams', 'invites', 'tokens', 'length', 16), 'characters' => Q_Config::get('Streams', 'invites', 'tokens', 'characters', 'abcdefghijklmnopqrstuvwxyz'))); } $p = new Streams_Participant(); $p->publisherId = $modifiedFields['publisherId']; $p->streamName = $modifiedFields['streamName']; $p->userId = $modifiedFields['userId']; if (!$p->retrieve()) { $p->state = 'invited'; $p->reason = ''; $p->save(); } } if (array_key_exists('state', $modifiedFields) or array_key_exists('expireTime', $modifiedFields)) { $invited = new Streams_Invited(); $invited->userId = $this->userId; // shouldn't change $invited->token = $this->token; // shouldn't change if (array_key_exists('state', $modifiedFields)) { $invited->state = $modifiedFields['state']; } if (array_key_exists('expireTime', $modifiedFields)) { $invited->expireTime = $modifiedFields['expireTime']; } $invited->save(true); } return parent::beforeSave($modifiedFields); }