X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=classes%2FNotice.php;h=2d880f8cb39ad0324f3461e9106c84ea5d329985;hb=0ee3d6ccb088b396926a372854e9949785a52c13;hp=eb08dde5226518b9d11b2e7ed1183c92dc49620f;hpb=e1f04061b9ea1261ed8805ee52cd8764fe4d9494;p=quix0rs-gnu-social.git diff --git a/classes/Notice.php b/classes/Notice.php index eb08dde522..2d880f8cb3 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -24,7 +24,7 @@ if (!defined('LACONICA')) { exit(1); } */ require_once 'DB/DataObject.php'; -class Notice extends DB_DataObject +class Notice extends DB_DataObject { ###START_AUTOCODE /* the code below is auto generated do not remove the above tag */ @@ -32,10 +32,15 @@ class Notice extends DB_DataObject public $__table = 'notice'; // table name public $id; // int(4) primary_key not_null public $profile_id; // int(4) not_null - public $content; // varchar(140) - public $url; // varchar(255) + public $uri; // varchar(255) unique_key + public $content; // varchar(140) + public $rendered; // text() + public $url; // varchar(255) public $created; // datetime() not_null public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP + public $reply_to; // int(4) + public $is_local; // tinyint(1) + public $source; // varchar(32) /* Static get */ function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Notice',$k,$v); } @@ -47,7 +52,91 @@ class Notice extends DB_DataObject return Profile::staticGet($this->profile_id); } - function validateContent() { - return Validate::string($this->content, array('min_length' => 1, 'max_length' => 140)); + function saveTags() { + /* extract all #hastags */ + $count = preg_match_all('/(?:^|\s)#([A-Za-z0-9_\-\.]{1,64})/', strtolower($this->content), $match); + if (!$count) { + return true; + } + + /* elide characters we don't want in the tag */ + $match[1] = str_replace(array('-', '_', '.'), '', $match[1]); + + /* Add them to the database */ + foreach(array_unique($match[1]) as $hashtag) { + $tag = DB_DataObject::factory('Notice_tag'); + $tag->notice_id = $this->id; + $tag->tag = $hashtag; + $tag->created = $this->created; + $id = $tag->insert(); + if (!$id) { + $last_error = PEAR::getStaticProperty('DB_DataObject','lastError'); + common_log(LOG_ERR, 'DB error inserting hashtag: ' . $last_error->message); + common_server_error(sprintf(_('DB error inserting hashtag: %s'), $last_error->message)); + return; + } + } + return true; + } + + static function saveNew($profile_id, $content, $source=NULL, $is_local=1, $reply_to=NULL, $uri=NULL) { + + $notice = new Notice(); + $notice->profile_id = $profile_id; + $notice->is_local = $is_local; + $notice->reply_to = $reply_to; + $notice->created = common_sql_now(); + $notice->content = $content; + $notice->rendered = common_render_content($notice->content, $notice); + $notice->source = $source; + $notice->uri = $uri; + + $id = $notice->insert(); + + if (!$id) { + return _('Problem saving notice.'); + } + + # Update the URI after the notice is in the database + if (!$uri) { + $orig = clone($notice); + $notice->uri = common_notice_uri($notice); + + if (!$notice->update($orig)) { + return _('Problem saving notice.'); + } + } + + # XXX: do we need to change this for remote users? + + common_save_replies($notice); + $notice->saveTags(); + + # Clear the cache for subscribed users, so they'll update at next request + # XXX: someone clever could prepend instead of clearing the cache + + if (common_config('memcached', 'enabled')) { + $notice->blowSubsCache(); + } + + return $notice; + } + + function blowSubsCache() { + $cache = new Memcache(); + if ($cache->connect(common_config('memcached', 'server'), common_config('memcached', 'port'))) { + $user = new User(); + + $user->query('SELECT id ' . + 'FROM user JOIN subscription ON user.id = subscription.subscriber ' . + 'WHERE subscription.subscribed = ' . $this->profile_id); + + while ($user->fetch()) { + $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id)); + } + + $user->free(); + unset($user); + } } }