/** * Used to post messages to EXISTING stream * $_REQUEST shall contain the content of the message. Also may include 'streamNames' * field which is an array of additional names of the streams to post message to. * * @param string $params * publisher id and stream name of existing stream shall be supplied * @return {void} */ function Streams_message_post() { $user = Users::loggedInUser(true); $publisherId = Streams::requestedPublisherId(true); $streamName = Streams::requestedName(true); // check if type is allowed $streams = Streams::fetch($user->id, $publisherId, $streamName); if (empty($streams)) { throw new Streams_Exception_NoSuchStream(); } $stream = reset($streams); if (empty($_REQUEST['type'])) { throw new Q_Exception_RequiredField(array('field' => 'type'), 'type'); } $type = $_REQUEST['type']; if (!Q_Config::get("Streams", "types", $stream->type, "messages", $type, 'post', false)) { throw new Q_Exception("This app doesn't support directly posting messages of type '{$type}' for streams of type '{$stream->type}'"); } $result = Streams_Message::post($user->id, $publisherId, $streamName, $_REQUEST); if (is_array($result)) { Streams::$cache['messages'] = $result; } else { Streams::$cache['message'] = $result; } }
function Streams_after_Users_Contact_removeExecute($params) { // Update avatar as viewed by everyone who was in that contacts list $contacts = Streams::$cache['contacts_removed']; foreach ($contacts as $contact) { Streams::updateAvatar($contact->contactUserId, $contact->userId); } Streams_Message::post(null, $contact->userId, 'Streams/contacts', array('type' => 'Streams/contacts/removed', 'instructions' => array('contacts' => Db::exportArray($contacts))), true); }
function Streams_after_Users_Label_saveExecute($params) { // The icon or title might have been modified $modifiedFields = $params['modifiedFields']; $label = $params['row']; $updates = Q::take($modifiedFields, array('icon', 'title')); $updates['userId'] = $label->userId; $updates['label'] = $label->label; return Streams_Message::post(null, $label->userId, "Streams/labels", array('type' => 'Streams/labels/updated', 'instructions' => compact('updates')), true); }
function Streams_after_Users_Contact_saveExecute($params) { $inserted = $params['inserted']; $modifiedFields = $params['modifiedFields']; $contact = $params['row']; if ($inserted) { Streams_Message::post(null, $contact->userId, 'Streams/contacts', array('type' => 'Streams/contacts/inserted', 'instructions' => array('contact' => $contact->exportArray())), true); } else { $updates = Q::take($modifiedFields, array('nickname')); $updates = array_merge($contact->toArray(), $updates); Streams_Message::post(null, $contact->userId, 'Streams/contacts', array('type' => 'Streams/contacts/updated', 'instructions' => compact('updates')), true); } }
function Streams_after_Users_Label_saveExecute($params) { // The icon or title might have been modified $inserted = $params['inserted']; $modifiedFields = $params['modifiedFields']; $label = $params['row']; if ($inserted) { Streams_Message::post(null, $label->userId, 'Streams/labels', array('type' => 'Streams/labels/inserted', 'instructions' => array('label' => $label->exportArray())), true); } else { $updates = Q::take($modifiedFields, array('icon', 'title')); $updates = array_merge($label->toArray(), $updates); Streams_Message::post(null, $label->userId, "Streams/labels", array('type' => 'Streams/labels/updated', 'instructions' => compact('updates')), true); } }
function Streams_after_Users_Label_removeExecute($params) { $label = $params['row']; Streams_Message::post(null, $label->userId, 'Streams/labels', array('type' => 'Streams/labels/removed', 'instructions' => array('label' => $label->toArray())), true); }
/** * Post a message to stream * @method post * @param {string} $asUserId * The user to post as * @param {array} $message * The fields of the message. Also may include 'streamNames' field which is an array of additional * names of the streams to post message to. * @param {booleam} $skipAccess=false * If true, skips the access checks and just posts the message. * @param $options=array() {array} * @return {array} * The array of results - successfully posted messages or false if post failed */ function post($asUserId, $message, $skipAccess = false) { return Streams_Message::post($asUserId, $this->publisherId, $this->name, $message, $skipAccess, array($this)); }
/** * Updates the weight on a relation * @param {string} $asUserId * The id of the user on whose behalf the app will be updating the relation * @param {string} $toPublisherId * The publisher of the stream on the 'to' end of the reltion * @param {string} $toStreamName * The name of the stream on the 'to' end of the relation * @param {string} $type * The type of the relation * @param {string} $fromPublisherId * The publisher of the stream on the 'from' end of the reltion * @param {string} $fromStreamName * The name of the stream on the 'from' end of the reltion * @param {double} $weight * The new weight * @param {double} $adjustWeights=null * The amount to move the other weights by, to make room for this one * @param {array} $options=array() * An array of options that can include: * "skipAccess" => Defaults to false. If true, skips the access checks and just updates the weight on the relation * @return {array|boolean} * Returns false if the operation was canceled by a hook * Otherwise returns array with key "to" and value of type Streams_Message */ static function updateRelation($asUserId, $toPublisherId, $toStreamName, $type, $fromPublisherId, $fromStreamName, $weight, $adjustWeights = null, $options = array()) { self::getRelation($asUserId, $toPublisherId, $toStreamName, $type, $fromPublisherId, $fromStreamName, $relatedTo, $relatedFrom, $category, $stream, $options); if (!$relatedTo->retrieve()) { throw new Q_Exception_MissingRow(array('table' => 'relatedTo', 'criteria' => 'with those fields'), array('publisherId', 'name', 'type', 'toPublisherId', 'to_name')); } // if (!$relatedFrom->retrieve()) { // throw new Q_Exception_MissingRow( // array('table' => 'relatedFrom', 'criteria' => 'those fields'), // array('publisherId', 'name', 'type', 'fromPublisherId', 'from_name') // ); // } if (empty($options['skipAccess'])) { if (!$category->testWriteLevel('relations')) { throw new Users_Exception_NotAuthorized(); } } /** * @event Streams/updateRelation/$streamType {before} * @param {string} relatedTo * @param {string} relatedFrom * @param {string} asUserId * @param {double} weight * @param {double} previousWeight */ $previousWeight = $relatedTo->weight; $adjustWeightsBy = $weight < $previousWeight ? $adjustWeights : -$adjustWeights; if (Q::event("Streams/updateRelation/{$stream->type}", compact('relatedTo', 'relatedFrom', 'type', 'weight', 'previousWeight', 'adjustWeightsBy', 'asUserId'), 'before') === false) { return false; } if (!empty($adjustWeights) and is_numeric($adjustWeights) and $weight !== $previousWeight) { $criteria = array('toPublisherId' => $toPublisherId, 'toStreamName' => $toStreamName, 'type' => $type, 'weight' => $weight < $previousWeight ? new Db_Range($weight, true, false, $previousWeight) : new Db_Range($previousWeight, false, true, $weight)); Streams_RelatedTo::update()->set(array('weight' => new Db_Expression("weight + " . $adjustWeightsBy)))->where($criteria)->execute(); } $relatedTo->weight = $weight; $relatedTo->save(); // Send Streams/updatedRelateTo message to the category stream // node server will be notified by Streams_Message::post $message = Streams_Message::post($asUserId, $toPublisherId, $toStreamName, array('type' => 'Streams/updatedRelateTo', 'instructions' => Q::json_encode(compact('fromPublisherId', 'fromStreamName', 'type', 'weight', 'previousWeight', 'adjustWeightsBy', 'asUserId'))), true); // TODO: We are not yet sending Streams/updatedRelateFrom message to the other stream // because we might be changing a lot of weights, and we'd have to message a lot of streams. // This is better done in the background using Node.js after selecting using $criteria // When we implement this, we can introduce weight again in the relatedFrom table. /** * @event Streams/updateRelation/$streamType {after} * @param {string} relatedTo * @param {string} relatedFrom * @param {string} asUserId * @param {double} weight * @param {double} previousWeight */ Q::event("Streams/updateRelation/{$stream->type}", compact('relatedTo', 'relatedFrom', 'type', 'weight', 'previousWeight', 'adjustWeightsBy', 'asUserId'), 'after'); return $message; }
/** * Send credits, as the logged-in user, to another user * @method send * @static * @param {integer} $amount The amount of credits to send. * @param {string} $toUserId The id of the user to whom you will send the credits * @param {array} $more An array supplying more info, including * "reason" => Identifies the reason for sending, if any */ static function send($amount, $toUserId, $more = array()) { if (!is_int($amount) or $amount <= 0) { throw new Q_Exception_WrongType(array('field' => 'amount', 'type' => 'integer')); } $user = Users::loggedInUser(true); $from_stream = new Streams_Stream(); $from_stream->publisherId = $user->id; $from_stream->name = 'Awards/credits'; if (!$from_stream->retrieve()) { $from_stream = self::createStream($user); } $existing_amount = $from_stream->getAttribute('amount'); if ($existing_amount < $amount) { throw new Awards_Exception_NotEnoughCredits(array('missing' => $amount - $existing_amount)); } $to_user = Users_User::fetch($toUserId, true); $to_stream = new Streams_Stream(); $to_stream->publisherId = $toUserId; $to_stream->name = 'Awards/credits'; if (!$to_stream->retrieve()) { $to_stream = self::createStream($to_user); } $to_stream->setAttribute('amount', $to_stream->getAttribute('amount') - $amount); $to_stream->save(); // NOTE: we are not doing transactions here mainly because of sharding. // If if we reached this point without exceptions, that means everything worked. // But if the following statement fails, then someone will get free credits. $from_stream->setAttribute('amount', $from_stream->getAttribute('amount') - $amount); $from_stream->save(); $instructions_json = Q::json_encode(array_merge(array('app' => Q_Config::expect('Q', 'app')), $more)); Streams_Message::post($user->id, $userId, array('type' => 'Awards/credits/sent', 'content' => $amount, 'instructions' => $instructions_json)); Streams_Message::post($user->id, $toUserId, array('type' => 'Awards/credits/received', 'content' => $amount, 'instructions' => $instructions_json)); }
/** * Updates the weight on a relation * @param {string} $asUserId * The id of the user on whose behalf the app will be updating the relation * @param {string} $toPublisherId * The publisher of the stream on the 'to' end of the reltion * @param {string} $toStreamName * The name of the stream on the 'to' end of the relation * @param {string} $type * The type of the relation * @param {string} $fromPublisherId * The publisher of the stream on the 'from' end of the reltion * @param {string} $fromStreamName * The name of the stream on the 'from' end of the reltion * @param {double} $weight * The new weight * @param {double} [$adjustWeights=1] * The amount to move the other weights by, to make room for this one. * This should be a positive number - the direction of the shift is determined automatically. * Or, set to 0 to prevent moving the other weights. * @param {array} $options=array() * @param {boolean} [$options.skipAccess=false] If true, skips the access checks and just unrelates the stream from the category * @return {array|boolean} * Returns false if the operation was canceled by a hook * Otherwise returns array with key "to" and value of type Streams_Message */ static function updateRelation($asUserId, $toPublisherId, $toStreamName, $type, $fromPublisherId, $fromStreamName, $weight, $adjustWeights = 1, $options = array()) { self::getRelations($asUserId, $toPublisherId, $toStreamName, $type, $fromPublisherId, $fromStreamName, $relatedTo, $relatedFrom, $categories, $streams, $arrayField, $options); $relatedTo = reset($relatedTo); $relatedFrom = reset($relatedFrom); $category = reset($categories); $stream = reset($streams); if (!$relatedTo) { throw new Q_Exception_MissingRow(array('table' => 'relatedTo', 'criteria' => 'with those fields'), array('publisherId', 'name', 'type', 'toPublisherId', 'to_name')); } // if (!$relatedFrom->retrieve()) { // throw new Q_Exception_MissingRow( // array('table' => 'relatedFrom', 'criteria' => 'those fields'), // array('publisherId', 'name', 'type', 'fromPublisherId', 'from_name') // ); // } if (empty($options['skipAccess'])) { if (!$category->testWriteLevel('relations')) { throw new Users_Exception_NotAuthorized(); } } /** * @event Streams/updateRelation/$streamType {before} * @param {string} relatedTo * @param {string} relatedFrom * @param {string} asUserId * @param {double} weight * @param {double} previousWeight */ $previousWeight = $relatedTo->weight; if (empty($adjustWeights)) { $adjustWeights = 0; } if (!is_numeric($adjustWeights) or $adjustWeights < 0) { throw new Q_Exception_WrongType(array('fields' => 'adjustWeights', 'type' => 'a non-negative number')); } $adjustWeightsBy = $weight < $previousWeight ? $adjustWeights : -$adjustWeights; if (Q::event("Streams/updateRelation/{$stream->type}", compact('relatedTo', 'relatedFrom', 'type', 'weight', 'previousWeight', 'adjustWeightsBy', 'asUserId'), 'before') === false) { return false; } if ($adjustWeights and $weight !== $previousWeight) { $criteria = array('toPublisherId' => $toPublisherId, 'toStreamName' => $toStreamName, 'type' => $type, 'weight' => $weight < $previousWeight ? new Db_Range($weight, true, false, $previousWeight) : new Db_Range($previousWeight, false, true, $weight)); Streams_RelatedTo::update()->set(array('weight' => new Db_Expression("weight + " . $adjustWeightsBy)))->where($criteria)->execute(); } $relatedTo->weight = $weight; $relatedTo->save(); // Send Streams/updatedRelateTo message to the category stream // node server will be notified by Streams_Message::post $message = Streams_Message::post($asUserId, $toPublisherId, $toStreamName, array('type' => 'Streams/updatedRelateTo', 'instructions' => compact('fromPublisherId', 'fromStreamName', 'type', 'weight', 'previousWeight', 'adjustWeightsBy', 'asUserId')), true); /** * @event Streams/updateRelation/$categoryType {after} * @param {string} relatedTo * @param {string} relatedFrom * @param {string} asUserId * @param {double} weight * @param {double} previousWeight */ Q::event("Streams/updateRelation/{$category->type}", compact('relatedTo', 'relatedFrom', 'type', 'weight', 'previousWeight', 'adjustWeightsBy', 'asUserId'), 'after'); return $message; }