function Streams_participant_response_participant() { if (isset(Streams::$cache['participant'])) { return Streams::$cache['participant']; } $publisherId = Streams::requestedPublisherId(true); $streamName = Streams::requestedName(true); if (empty($_REQUEST['userId'])) { throw new Q_Exception_RequiredField(array('field' => 'userId')); } $user = Users::loggedInUser(); $userId = $user ? $user->id : ""; $stream = Streams::fetch($userId, $publisherId, $streamName); if (empty($stream)) { throw new Q_Exception_MissingRow(array('table' => 'Stream', 'criteria' => "{publisherId: '{$publisherId}', name: '{$streamName}'}")); } $stream = reset($stream); if (!$stream->testReadLevel('participants')) { throw new Users_Exception_NotAuthorized(); } $p = new Streams_Participant(); $p->publisherId = $publisherId; $p->streamName = $streamName; $p->userId = $_REQUEST['userId']; if ($p->retrieve()) { return $p->exportArray(); } return null; }
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(); } }
/** * Subscription tool * @param array $options * "publisherId" => the id of the user who is publishing the stream * "streamName" => the name of the stream for which to edit access levels */ function Streams_subscription_tool($options) { $subscribed = 'no'; extract($options); $user = Users::loggedInUser(true); if (!isset($publisherId)) { $publisherId = Streams::requestedPublisherId(true); } if (!isset($streamName)) { $streamName = Streams::requestedName(); } $stream = Streams::fetchOne($user->id, $publisherId, $streamName); if (!$stream) { throw new Q_Exception_MissingRow(array('table' => 'stream', 'criteria' => compact('publisherId', 'streamName'))); } $streams_participant = new Streams_Participant(); $streams_participant->publisherId = $publisherId; $streams_participant->streamName = $streamName; $streams_participant->userId = $user->id; if ($streams_participant->retrieve()) { $subscribed = $streams_participant->subscribed; } $types = Q_Config::get('Streams', 'types', $stream->type, 'messages', array()); $messageTypes = array(); foreach ($types as $type => $msg) { $name = Q::ifset($msg, 'title', $type); /* * group by name */ foreach ($messageTypes as $msgType) { if ($msgType['name'] == $name) { continue 2; } } $messageTypes[] = array('value' => $type, 'name' => $name); } $usersFetch = array('userId' => $user->id, 'state' => 'active'); $devices = array(); $emails = Users_Email::select('address')->where($usersFetch)->fetchAll(PDO::FETCH_COLUMN); $mobiles = Users_Mobile::select('number')->where($usersFetch)->fetchAll(PDO::FETCH_COLUMN); foreach ($emails as $email) { $devices[] = array('value' => Q::json_encode(array('email' => $email)), 'name' => 'my email'); } foreach ($mobiles as $mobile) { $devices[] = array('value' => Q::json_encode(array('mobile' => $mobile)), 'name' => 'my mobile'); } $items = array(); $rules = Streams_Rule::select('deliver, filter')->where(array('ofUserId' => $user->id, 'publisherId' => $publisherId, 'streamName' => $streamName))->fetchAll(PDO::FETCH_ASSOC); while ($rule = array_pop($rules)) { $filter = json_decode($rule['filter']); /* * group by name */ foreach ($rules as $val) { if (json_decode($val['filter'])->labels == $filter->labels) { continue 2; } } $items[] = array('deliver' => json_decode($rule['deliver']), 'filter' => $filter); } Q_Response::addScript("plugins/Streams/js/Streams.js"); Q_Response::addScript("plugins/Streams/js/tools/subscription.js"); Q_Response::setToolOptions(compact('items', 'subscribed', 'messageTypes', 'devices', 'publisherId', 'streamName')); }
/** * If the user is participating, get the Streams_Participant object. * Otherwise, returns false, or null if the user isn't logged in. * @param {string} $userId Defaults to logged-in user's id, if any. * @return {Streams_Subscription|false|null} */ function participant($userId = null) { if (!isset($userId)) { $user = Users::loggedInUser(); if (!$user) { return null; } $userId = $user->id; } $p = new Streams_Participant(); $p->publisherId = $this->publisherId; $p->streamName = $this->name; $p->userId = $userId; return $p->retrieve(); }
/** * 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); }