]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Conversation.php
Conversation entries where id==0 would screw up the "re-auto-increment" sequencing
[quix0rs-gnu-social.git] / classes / Conversation.php
old mode 100755 (executable)
new mode 100644 (file)
index f540004..1dba2c1
  * @category  Data
  * @package   StatusNet
  * @author    Zach Copley <zach@status.net>
+ * @author    Mikael Nordfeldth <mmn@hethane.se>
  * @copyright 2010 StatusNet Inc.
+ * @copyright 2009-2014 Free Software Foundation, Inc http://www.fsf.org
  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  * @link      http://status.net/
  */
 
-require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
+if (!defined('GNUSOCIAL')) { exit(1); }
 
-class Conversation extends Memcached_DataObject
+class Conversation extends Managed_DataObject
 {
-    ###START_AUTOCODE
-    /* the code below is auto generated do not remove the above tag */
-
-    public $__table = 'conversation';                    // table name
-    public $id;                              // int(4)  primary_key not_null
-    public $uri;                             // varchar(225)  unique_key
+    public $__table = 'conversation';        // table name
+    public $id;                              // int(4)  primary_key not_null auto_increment
+    public $uri;                             // varchar(191)  unique_key   not 255 because utf8mb4 takes more space
     public $created;                         // datetime   not_null
     public $modified;                        // timestamp   not_null default_CURRENT_TIMESTAMP
 
-    /* Static get */
-    function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('conversation',$k,$v); }
+    public static function schemaDef()
+    {
+        return array(
+            'fields' => array(
+                'id' => array('type' => 'serial', 'not null' => true, 'description' => 'Unique identifier, (again) unrelated to notice id since 2016-01-06'),
+                'uri' => array('type' => 'varchar', 'not null'=>true, 'length' => 191, 'description' => 'URI of the conversation'),
+                'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
+                'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
+            ),
+            'primary key' => array('id'),
+            'unique keys' => array(
+                'conversation_uri_key' => array('uri'),
+            ),
+        );
+    }
+
+    static public function beforeSchemaUpdate()
+    {
+        $table = strtolower(get_called_class());
+        $schema = Schema::get();
+        $schemadef = $schema->getTableDef($table);
 
-    /* the code above is auto generated do not remove the tag below */
-    ###END_AUTOCODE
+        // 2016-01-06 We have to make sure there is no conversation with id==0 since it will screw up auto increment resequencing
+        if ($schemadef['fields']['id']['auto_increment']) {
+            // since we already have auto incrementing ('serial') we can continue
+            return;
+        }
+
+        // The conversation will be recreated in upgrade.php, which will
+        // generate a new URI, but that's collateral damage for you.
+        $conv = new Conversation();
+        $conv->id = 0;
+        if ($conv->find()) {
+            while ($conv->fetch()) {
+                // Since we have filtered on 0 this only deletes such entries
+                // which I have been afraid wouldn't work, but apparently does!
+                // (I thought it would act as null or something and find _all_ conversation entries)
+                $conv->delete();
+            }
+        }
+    }
 
     /**
-     * Factory method for creating a new conversation
+     * Factory method for creating a new conversation.
+     *
+     * Use this for locally initiated conversations. Remote notices should
+     * preferrably supply their own conversation URIs in the OStatus feed.
      *
      * @return Conversation the new conversation DO
      */
-    static function create()
+    static function create($uri=null, $created=null)
     {
+        // Be aware that the Notice does not have an id yet since it's not inserted!
         $conv = new Conversation();
-        $conv->created = common_sql_now();
-        $id = $conv->insert();
+        $conv->created = $created ?: common_sql_now();
+        $conv->uri = $uri ?: sprintf('%s%s=%s:%s=%s',
+                             TagURI::mint(),
+                             'objectType', 'thread',
+                             'nonce', common_random_hexstr(8));
+        // This insert throws exceptions on failure
+        $conv->insert();
 
-        if (empty($id)) {
-            common_log_db_error($conv, 'INSERT', __FILE__);
-            return null;
-        }
+        return $conv;
+    }
 
-        $orig = clone($conv);
-        $orig->uri = common_local_url('conversation', array('id' => $id),
-                                      null, null, false);
-        $result = $orig->update($conv);
+    static function noticeCount($id)
+    {
+        $keypart = sprintf('conversation:notice_count:%d', $id);
+
+        $cnt = self::cacheGet($keypart);
 
-        if (empty($result)) {
-            common_log_db_error($conv, 'UPDATE', __FILE__);
-            return null;
+        if ($cnt !== false) {
+            return $cnt;
         }
 
-        return $conv;
+        $notice               = new Notice();
+        $notice->conversation = $id;
+        $notice->whereAddIn('verb', array(ActivityVerb::POST, ActivityUtils::resolveUri(ActivityVerb::POST, true)), $notice->columnType('verb'));
+        $cnt                  = $notice->count();
+
+        self::cacheSet($keypart, $cnt);
+
+        return $cnt;
     }
 
-}
+    static public function getUrlFromNotice(Notice $notice, $anchor=true)
+    {
+        $conv = Conversation::getByID($notice->conversation);
+        return $conv->getUrl($anchor ? $notice->getID() : null);
+    }
 
+    public function getUri()
+    {
+        return $this->uri;
+    }
+
+    public function getUrl($noticeId=null)
+    {
+        // FIXME: the URL router should take notice-id as an argument...
+        return common_local_url('conversation', array('id' => $this->getID())) .
+                ($noticeId===null ? '' : "#notice-{$noticeId}");
+    }
+
+    // FIXME: ...will 500 ever be too low? Taken from ConversationAction::MAX_NOTICES
+    public function getNotices(Profile $scoped=null, $offset=0, $limit=500)
+    {
+        $stream = new ConversationNoticeStream($this->getID(), $scoped);
+        $notices = $stream->getNotices($offset, $limit);
+        return $notices;
+    }
+
+    public function insert()
+    {
+        $result = parent::insert();
+        if ($result === false) {
+            common_log_db_error($this, 'INSERT', __FILE__);
+            throw new ServerException(_('Failed to insert Conversation into database'));
+        }
+        return $result;
+    }
+}