]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Conversation.php
Return dynamically generated URLs for thumbnails for all locally stored entries
[quix0rs-gnu-social.git] / classes / Conversation.php
index c8d922a2f0f50d6ef504b73a4b8da45cecdd9210..3bad400174f116d01e345d718f6ffba231c4ddd5 100644 (file)
@@ -35,7 +35,7 @@ class Conversation extends Managed_DataObject
 {
     public $__table = 'conversation';        // table name
     public $id;                              // int(4)  primary_key not_null
-    public $uri;                             // varchar(255)  unique_key
+    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
 
@@ -43,8 +43,8 @@ class Conversation extends Managed_DataObject
     {
         return array(
             'fields' => array(
-                'id' => array('type' => 'int', 'not null' => true, 'description' => 'should be set from root notice id (since 2014-03-01 commit)'),
-                'uri' => array('type' => 'varchar', 'length' => 255, 'description' => 'URI of the conversation'),
+                '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'),
             ),
@@ -63,24 +63,17 @@ class Conversation extends Managed_DataObject
      *
      * @return Conversation the new conversation DO
      */
-    static function create(Notice $notice)
+    static function create($uri=null, $created=null)
     {
-        if (empty($notice->id)) {
-            throw new ServerException(_('Tried to create conversation for not yet inserted notice'));
-        }
+        // Be aware that the Notice does not have an id yet since it's not inserted!
         $conv = new Conversation();
-        $conv->created = common_sql_now();
-        $conv->id = $notice->id;
-        $conv->uri = sprintf('%s%s=%d:%s=%s',
+        $conv->created = $created ?: common_sql_now();
+        $conv->uri = $uri ?: sprintf('%s%s=%s:%s=%s',
                              TagURI::mint(),
-                             'noticeId', $notice->id,
-                             'objectType', 'thread');
-        $result = $conv->insert();
-
-        if ($result === false) {
-            common_log_db_error($conv, 'INSERT', __FILE__);
-            throw new ServerException(_('Failed to create conversation for notice'));
-        }
+                             'objectType', 'thread',
+                             'nonce', common_random_hexstr(8));
+        // This insert throws exceptions on failure
+        $conv->insert();
 
         return $conv;
     }
@@ -107,8 +100,8 @@ class Conversation extends Managed_DataObject
 
     static public function getUrlFromNotice(Notice $notice, $anchor=true)
     {
-        $conv = self::getKV('id', $notice->conversation);
-        return $conv->getUrl($anchor ? $notice->id : null);
+        $conv = Conversation::getByID($notice->conversation);
+        return $conv->getUrl($anchor ? $notice->getID() : null);
     }
 
     public function getUri()
@@ -119,18 +112,25 @@ class Conversation extends Managed_DataObject
     public function getUrl($noticeId=null)
     {
         // FIXME: the URL router should take notice-id as an argument...
-        return common_local_url('conversation', array('id' => $this->id)) .
+        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($offset=0, $limit=500, Profile $scoped=null)
+    public function getNotices(Profile $scoped=null, $offset=0, $limit=500)
     {
-        if ($scoped === null) {
-            $scoped = Profile::current();
-        }
-        $stream = new ConversationNoticeStream($this->id, $scoped);
+        $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;
+    }
 }