]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Conversation.php
getConversationUrl introduced for linking to conversations
[quix0rs-gnu-social.git] / classes / Conversation.php
index e029c20ba0d095145cff147b35aaec6f49d5dd5b..033b99384b0470f10c7e46d72bd37331eab79b5a 100755 (executable)
  * @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 $__table = 'conversation';        // table name
     public $id;                              // int(4)  primary_key not_null
-    public $uri;                             // varchar(225)  unique_key
+    public $uri;                             // varchar(255)  unique_key
     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); }
-
-    /* the code above is auto generated do not remove the tag below */
-    ###END_AUTOCODE
+    public static function schemaDef()
+    {
+        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'),
+                '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'),
+            ),
+        );
+    }
 
     /**
      * Factory method for creating a new conversation
      *
      * @return Conversation the new conversation DO
      */
-    static function create()
+    static function create(Notice $notice)
     {
+        if (empty($notice->id)) {
+            throw new ServerException(_('Tried to create conversation for not yet inserted notice'));
+        }
         $conv = new Conversation();
         $conv->created = common_sql_now();
-        $id = $conv->insert();
+        $conv->id = $notice->id;
+        $conv->uri = common_local_url('conversation', array('id' => $notice->id), null, null, false);
+        $result = $conv->insert();
 
-        if (empty($id)) {
+        if ($result === false) {
             common_log_db_error($conv, 'INSERT', __FILE__);
-            return null;
-        }
-
-        $orig = clone($conv);
-        $orig->uri = common_local_url('conversation', array('id' => $id),
-                                      null, null, false);
-        $result = $orig->update($conv);
-
-        if (empty($result)) {
-            common_log_db_error($conv, 'UPDATE', __FILE__);
-            return null;
+            throw new ServerException(_('Failed to create conversation for notice'));
         }
 
         return $conv;
@@ -93,4 +97,22 @@ class Conversation extends Memcached_DataObject
 
         return $cnt;
     }
+
+    static public function getUrlFromNotice(Notice $notice, $anchor=true)
+    {
+        $conv = self::getKV('id', $notice->conversation);
+        return $conv->getUrl($anchor ? $notice->id : 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->id)) .
+                ($noticeId===null ? '' : "#notice-{$noticeId}");
+    }
 }