+
+ /**
+ * Magic function called at serialize() time.
+ *
+ * We use this to drop a couple process-specific references
+ * from DB_DataObject which can cause trouble in future
+ * processes.
+ *
+ * @return array of variable names to include in serialization.
+ */
+
+ function __sleep()
+ {
+ $vars = parent::__sleep();
+ $skip = array('_original', '_profile', '_groups', '_attachments', '_faves', '_replies', '_repeats');
+ return array_diff($vars, $skip);
+ }
+
+ static function defaultScope()
+ {
+ $scope = common_config('notice', 'defaultscope');
+ if (is_null($scope)) {
+ if (common_config('site', 'private')) {
+ $scope = 1;
+ } else {
+ $scope = 0;
+ }
+ }
+ return $scope;
+ }
+
+ static function fillProfiles($notices)
+ {
+ $map = self::getProfiles($notices);
+
+ foreach ($notices as $notice) {
+ if (array_key_exists($notice->profile_id, $map)) {
+ $notice->_setProfile($map[$notice->profile_id]);
+ }
+ }
+
+ return array_values($map);
+ }
+
+ static function getProfiles(&$notices)
+ {
+ $ids = array();
+ foreach ($notices as $notice) {
+ $ids[] = $notice->profile_id;
+ }
+
+ $ids = array_unique($ids);
+
+ return Memcached_DataObject::pivotGet('Profile', 'id', $ids);
+ }
+
+ static function fillGroups(&$notices)
+ {
+ $ids = self::_idsOf($notices);
+
+ $gis = Memcached_DataObject::listGet('Group_inbox', 'notice_id', $ids);
+
+ $gids = array();
+
+ foreach ($gis as $id => $gi)
+ {
+ foreach ($gi as $g)
+ {
+ $gids[] = $g->group_id;
+ }
+ }
+
+ $gids = array_unique($gids);
+
+ $group = Memcached_DataObject::pivotGet('User_group', 'id', $gids);
+
+ foreach ($notices as $notice)
+ {
+ $grps = array();
+ $gi = $gis[$notice->id];
+ foreach ($gi as $g) {
+ $grps[] = $group[$g->group_id];
+ }
+ $notice->_setGroups($grps);
+ }
+ }
+
+ static function _idsOf(&$notices)
+ {
+ $ids = array();
+ foreach ($notices as $notice) {
+ $ids[] = $notice->id;
+ }
+ $ids = array_unique($ids);
+ return $ids;
+ }
+
+ static function fillAttachments(&$notices)
+ {
+ $ids = self::_idsOf($notices);
+
+ $f2pMap = Memcached_DataObject::listGet('File_to_post', 'post_id', $ids);
+
+ $fileIds = array();
+
+ foreach ($f2pMap as $noticeId => $f2ps) {
+ foreach ($f2ps as $f2p) {
+ $fileIds[] = $f2p->file_id;
+ }
+ }
+
+ $fileIds = array_unique($fileIds);
+
+ $fileMap = Memcached_DataObject::pivotGet('File', 'id', $fileIds);
+
+ foreach ($notices as $notice)
+ {
+ $files = array();
+ $f2ps = $f2pMap[$notice->id];
+ foreach ($f2ps as $f2p) {
+ $files[] = $fileMap[$f2p->file_id];
+ }
+ $notice->_setAttachments($files);
+ }
+ }
+
+ protected $_faves;
+
+ /**
+ * All faves of this notice
+ *
+ * @return array Array of Fave objects
+ */
+
+ function getFaves()
+ {
+ if (isset($this->_faves) && is_array($this->_faves)) {
+ return $this->_faves;
+ }
+ $faveMap = Memcached_DataObject::listGet('Fave', 'notice_id', array($this->id));
+ $this->_faves = $faveMap[$this->id];
+ return $this->_faves;
+ }
+
+ function _setFaves(&$faves)
+ {
+ $this->_faves = $faves;
+ }
+
+ static function fillFaves(&$notices)
+ {
+ $ids = self::_idsOf($notices);
+ $faveMap = Memcached_DataObject::listGet('Fave', 'notice_id', $ids);
+ $cnt = 0;
+ $faved = array();
+ foreach ($faveMap as $id => $faves) {
+ $cnt += count($faves);
+ if (count($faves) > 0) {
+ $faved[] = $id;
+ }
+ }
+ foreach ($notices as $notice) {
+ $notice->_setFaves($faveMap[$notice->id]);
+ }
+ }
+
+ static function fillReplies(&$notices)
+ {
+ $ids = self::_idsOf($notices);
+ $replyMap = Memcached_DataObject::listGet('Reply', 'notice_id', $ids);
+ foreach ($notices as $notice) {
+ $replies = $replyMap[$notice->id];
+ $ids = array();
+ foreach ($replies as $reply) {
+ $ids[] = $reply->profile_id;
+ }
+ $notice->_setReplies($ids);
+ }
+ }
+
+ protected $_repeats;
+
+ function getRepeats()
+ {
+ if (isset($this->_repeats) && is_array($this->_repeats)) {
+ return $this->_repeats;
+ }
+ $repeatMap = Memcached_DataObject::listGet('Notice', 'repeat_of', array($this->id));
+ $this->_repeats = $repeatMap[$this->id];
+ return $this->_repeats;
+ }
+
+ function _setRepeats(&$repeats)
+ {
+ $this->_repeats = $repeats;
+ }
+
+ static function fillRepeats(&$notices)
+ {
+ $ids = self::_idsOf($notices);
+ $repeatMap = Memcached_DataObject::listGet('Notice', 'repeat_of', $ids);
+ foreach ($notices as $notice) {
+ $notice->_setRepeats($repeatMap[$notice->id]);
+ }
+ }