function qa_db_event_create_not_entity($userid, $questionid, $lastpostid, $updatetype, $lastuserid, $timestamp = null) { require_once QA_INCLUDE_DIR . 'qa-app-updates.php'; $updatedsql = isset($timestamp) ? 'FROM_UNIXTIME(' . qa_db_argument_to_mysql($timestamp, false) . ')' : 'NOW()'; qa_db_query_sub("INSERT INTO ^userevents (userid, entitytype, entityid, questionid, lastpostid, updatetype, lastuserid, updated) " . "VALUES (\$, \$, 0, #, #, \$, \$, " . $updatedsql . ")", $userid, QA_ENTITY_NONE, $questionid, $lastpostid, $updatetype, $lastuserid); qa_db_user_events_truncate($userid, $questionid); }
function qa_db_favorite_create($userid, $entitytype, $entityid) { $threshold = qa_opt('max_copy_user_updates'); // if this many users subscribe to it, create a shared stream // Add in the favorite for this user, unshared events at first (will be switched later if appropriate) qa_db_query_sub('INSERT IGNORE INTO ^userfavorites (userid, entitytype, entityid, nouserevents) VALUES ($, $, #, 0)', $userid, $entitytype, $entityid); // See whether this entity already has another favoriter who uses its shared event stream $useshared = qa_db_read_one_value(qa_db_query_sub('SELECT COUNT(*) FROM ^userfavorites WHERE entitytype=$ AND entityid=# AND nouserevents>0 LIMIT 1', $entitytype, $entityid)); // If not, check whether it's time to switch it over to a shared stream if (!$useshared) { $favoriters = qa_db_read_one_value(qa_db_query_sub('SELECT COUNT(*) FROM ^userfavorites WHERE entitytype=$ AND entityid=# LIMIT #', $entitytype, $entityid, $threshold)); $useshared = $favoriters >= $threshold; } // If we're going to use the shared stream... if ($useshared) { // ... for all the people for whom we're switching this to a shared stream, find the highest number of other shared streams they have $maxshared = qa_db_read_one_value(qa_db_query_sub('SELECT MAX(c) FROM (SELECT COUNT(*) AS c FROM ^userfavorites AS shared JOIN ^userfavorites AS unshared ' . 'WHERE shared.userid=unshared.userid AND shared.nouserevents>0 AND unshared.entitytype=$ AND unshared.entityid=# AND unshared.nouserevents=0 GROUP BY shared.userid) y', $entitytype, $entityid)); // ... if this number is greater than our current 'max_copy_user_updates' threshold, increase that threshold (see long comment above) if ($maxshared + 1 > $threshold) { qa_opt('max_copy_user_updates', $maxshared + 1); } // ... now switch all unshared favoriters (including this new one) over to be shared qa_db_query_sub('UPDATE ^userfavorites SET nouserevents=1 WHERE entitytype=$ AND entityid=# AND nouserevents=0', $entitytype, $entityid); // Otherwise if we're going to record this in user-specific streams ... } else { require_once QA_INCLUDE_DIR . 'qa-db-events.php'; // ... copy across recent events from the shared stream qa_db_query_sub('INSERT INTO ^userevents (userid, entitytype, entityid, questionid, lastpostid, updatetype, lastuserid, updated) ' . 'SELECT #, entitytype, entityid, questionid, lastpostid, updatetype, lastuserid, updated FROM ' . '^sharedevents WHERE entitytype=$ AND entityid=#', $userid, $entitytype, $entityid); // ... and truncate the user's stream as appropriate qa_db_user_events_truncate($userid); } }