Parcourir la source

changing paste read output for API refactoring

El RIDO il y a 10 ans
Parent
commit
f21567133c
7 fichiers modifiés avec 74 ajouts et 36 suppressions
  1. 21 21
      js/zerobin.js
  2. 4 0
      lib/model/paste.php
  3. 2 4
      lib/zerobin.php
  4. 1 1
      lib/zerobin/db.php
  5. 17 0
      tst/bootstrap.php
  6. 12 2
      tst/jsonApi.php
  7. 17 8
      tst/zerobin.php

+ 21 - 21
js/zerobin.js

@@ -592,28 +592,28 @@ $(function() {
          * Show decrypted text in the display area, including discussion (if open)
          * Show decrypted text in the display area, including discussion (if open)
          *
          *
          * @param string key : decryption key
          * @param string key : decryption key
-         * @param array comments : Array of messages to display (items = array with keys ('data','meta')
+         * @param object paste : paste object including comments to display (items = array with keys ('data','meta')
          */
          */
-        displayMessages: function(key, comments)
+        displayMessages: function(key, paste)
         {
         {
             // Try to decrypt the paste.
             // Try to decrypt the paste.
             var password = this.passwordInput.val();
             var password = this.passwordInput.val();
             if (!this.prettyPrint.hasClass('prettyprinted')) {
             if (!this.prettyPrint.hasClass('prettyprinted')) {
                 try
                 try
                 {
                 {
-                    if (comments[0].attachment)
+                    if (paste.attachment)
                     {
                     {
-                        var attachment = filter.decipher(key, password, comments[0].attachment);
+                        var attachment = filter.decipher(key, password, paste.attachment);
                         if (attachment.length == 0)
                         if (attachment.length == 0)
                         {
                         {
                             if (password.length == 0) password = this.requestPassword();
                             if (password.length == 0) password = this.requestPassword();
-                            attachment = filter.decipher(key, password, comments[0].attachment);
+                            attachment = filter.decipher(key, password, paste.attachment);
                         }
                         }
                         if (attachment.length == 0) throw 'failed to decipher attachment';
                         if (attachment.length == 0) throw 'failed to decipher attachment';
 
 
-                        if (comments[0].attachmentname)
+                        if (paste.attachmentname)
                         {
                         {
-                            var attachmentname = filter.decipher(key, password, comments[0].attachmentname);
+                            var attachmentname = filter.decipher(key, password, paste.attachmentname);
                             if (attachmentname.length > 0) this.attachmentLink.attr('download', attachmentname);
                             if (attachmentname.length > 0) this.attachmentLink.attr('download', attachmentname);
                         }
                         }
                         this.attachmentLink.attr('href', attachment);
                         this.attachmentLink.attr('href', attachment);
@@ -631,20 +631,20 @@ $(function() {
                             this.image.removeClass('hidden');
                             this.image.removeClass('hidden');
                         }
                         }
                     }
                     }
-                    var cleartext = filter.decipher(key, password, comments[0].data);
-                    if (cleartext.length == 0 && password.length == 0 && !comments[0].attachment)
+                    var cleartext = filter.decipher(key, password, paste.data);
+                    if (cleartext.length == 0 && password.length == 0 && !paste.attachment)
                     {
                     {
                         password = this.requestPassword();
                         password = this.requestPassword();
-                        cleartext = filter.decipher(key, password, comments[0].data);
+                        cleartext = filter.decipher(key, password, paste.data);
                     }
                     }
-                    if (cleartext.length == 0 && !comments[0].attachment) throw 'failed to decipher message';
+                    if (cleartext.length == 0 && !paste.attachment) throw 'failed to decipher message';
 
 
                     this.passwordInput.val(password);
                     this.passwordInput.val(password);
                     if (cleartext.length > 0)
                     if (cleartext.length > 0)
                     {
                     {
                         helper.setElementText(this.clearText, cleartext);
                         helper.setElementText(this.clearText, cleartext);
                         helper.setElementText(this.prettyPrint, cleartext);
                         helper.setElementText(this.prettyPrint, cleartext);
-                        this.formatPaste(comments[0].meta.formatter);
+                        this.formatPaste(paste.meta.formatter);
                     }
                     }
                 }
                 }
                 catch(err)
                 catch(err)
@@ -658,9 +658,9 @@ $(function() {
             }
             }
 
 
             // Display paste expiration / for your eyes only.
             // Display paste expiration / for your eyes only.
-            if (comments[0].meta.expire_date)
+            if (paste.meta.expire_date)
             {
             {
-                var expiration = helper.secondsToHuman(comments[0].meta.remaining_time),
+                var expiration = helper.secondsToHuman(paste.meta.remaining_time),
                     expirationLabel = [
                     expirationLabel = [
                         'This document will expire in %d ' + expiration[1] + '.',
                         'This document will expire in %d ' + expiration[1] + '.',
                         'This document will expire in %d ' + expiration[1] + 's.'
                         'This document will expire in %d ' + expiration[1] + 's.'
@@ -669,7 +669,7 @@ $(function() {
                 this.remainingTime.removeClass('foryoureyesonly')
                 this.remainingTime.removeClass('foryoureyesonly')
                                   .removeClass('hidden');
                                   .removeClass('hidden');
             }
             }
-            if (comments[0].meta.burnafterreading)
+            if (paste.meta.burnafterreading)
             {
             {
                 // unfortunately many web servers don't support DELETE (and PUT) out of the box
                 // unfortunately many web servers don't support DELETE (and PUT) out of the box
                 $.ajax({
                 $.ajax({
@@ -692,15 +692,15 @@ $(function() {
             }
             }
 
 
             // If the discussion is opened on this paste, display it.
             // If the discussion is opened on this paste, display it.
-            if (comments[0].meta.opendiscussion)
+            if (paste.meta.opendiscussion)
             {
             {
                 this.comments.html('');
                 this.comments.html('');
 
 
                 // iterate over comments
                 // iterate over comments
-                for (var i = 1; i < comments.length; i++)
+                for (var i = 0; i < paste.comments.length; ++i)
                 {
                 {
                     var place = this.comments;
                     var place = this.comments;
-                    var comment=comments[i];
+                    var comment = paste.comments[i];
                     var cleartext = '[' + i18n._('Could not decrypt comment; Wrong key?') + ']';
                     var cleartext = '[' + i18n._('Could not decrypt comment; Wrong key?') + ']';
                     try
                     try
                     {
                     {
@@ -837,7 +837,7 @@ $(function() {
                             {
                             {
                                 if (data.status == 0)
                                 if (data.status == 0)
                                 {
                                 {
-                                    zerobin.displayMessages(zerobin.pageKey(), data.messages);
+                                    zerobin.displayMessages(zerobin.pageKey(), data);
                                 }
                                 }
                                 else if (data.status == 1)
                                 else if (data.status == 1)
                                 {
                                 {
@@ -1293,12 +1293,12 @@ $(function() {
                 }
                 }
 
 
                 // List of messages to display.
                 // List of messages to display.
-                var messages = $.parseJSON(this.cipherData.text());
+                var data = $.parseJSON(this.cipherData.text());
 
 
                 // Show proper elements on screen.
                 // Show proper elements on screen.
                 this.stateExistingPaste();
                 this.stateExistingPaste();
 
 
-                this.displayMessages(this.pageKey(), messages);
+                this.displayMessages(this.pageKey(), data);
             }
             }
             // Display error message from php code.
             // Display error message from php code.
             else if (this.errorMessage.text().length > 1)
             else if (this.errorMessage.text().length > 1)

+ 4 - 0
lib/model/paste.php

@@ -52,6 +52,10 @@ class model_paste extends model_abstract
                 $this->_data->meta->formatter = $this->_conf->getKey('defaultformatter');
                 $this->_data->meta->formatter = $this->_conf->getKey('defaultformatter');
             }
             }
         }
         }
+        $this->_data->comments = array_values($this->getComments());
+        $this->_data->comment_count = count($this->_data->comments);
+        $this->_data->comment_offset = 0;
+        $this->_data->{'@context'} = 'js/paste.jsonld';
         return $this->_data;
         return $this->_data;
     }
     }
 
 

+ 2 - 4
lib/zerobin.php

@@ -339,9 +339,7 @@ class zerobin
             $paste = $this->_model->getPaste($dataid);
             $paste = $this->_model->getPaste($dataid);
             if ($paste->exists())
             if ($paste->exists())
             {
             {
-                // The paste itself is the first in the list of encrypted messages.
-                $messages = array_merge(array($paste->get()), $paste->getComments());
-                $this->_data = json_encode($messages);
+                $this->_data = json_encode($paste->get());
             }
             }
             else
             else
             {
             {
@@ -359,7 +357,7 @@ class zerobin
             }
             }
             else
             else
             {
             {
-                $this->_return_message(0, $dataid, array('messages' => $messages));
+                $this->_return_message(0, $dataid, json_decode($this->_data, true));
             }
             }
         }
         }
     }
     }

+ 1 - 1
lib/zerobin/db.php

@@ -384,7 +384,7 @@ class zerobin_db extends zerobin_abstract
                     $comments[$i]->meta->nickname = $row['nickname'];
                     $comments[$i]->meta->nickname = $row['nickname'];
                 if (array_key_exists('vizhash', $row))
                 if (array_key_exists('vizhash', $row))
                     $comments[$i]->meta->vizhash = $row['vizhash'];
                     $comments[$i]->meta->vizhash = $row['vizhash'];
-                $comments[$i]->meta->postdate = $i;
+                $comments[$i]->meta->postdate = (int) $row['postdate'];
                 $comments[$i]->meta->commentid = $row['dataid'];
                 $comments[$i]->meta->commentid = $row['dataid'];
                 $comments[$i]->meta->parentid = $row['parentid'];
                 $comments[$i]->meta->parentid = $row['parentid'];
             }
             }

+ 17 - 0
tst/bootstrap.php

@@ -88,6 +88,23 @@ class helper
         return $example;
         return $example;
     }
     }
 
 
+    /**
+     * get example paste
+     *
+     * @return array
+     */
+    public static function getPasteAsJson($meta = array())
+    {
+        $example = self::getPaste();
+        if (count($meta))
+            $example['meta'] = $meta;
+        $example['comments'] = array();
+        $example['comment_count'] = 0;
+        $example['comment_offset'] = 0;
+        $example['@context'] = 'js/paste.jsonld';
+        return json_encode($example);
+    }
+
     /**
     /**
      * get example paste ID
      * get example paste ID
      *
      *

+ 12 - 2
tst/jsonApi.php

@@ -142,7 +142,8 @@ class jsonApiTest extends PHPUnit_Framework_TestCase
     public function testRead()
     public function testRead()
     {
     {
         $this->reset();
         $this->reset();
-        $this->_model->create(helper::getPasteId(), helper::getPaste());
+        $paste = helper::getPasteWithAttachment();
+        $this->_model->create(helper::getPasteId(), $paste);
         $_SERVER['QUERY_STRING'] = helper::getPasteId();
         $_SERVER['QUERY_STRING'] = helper::getPasteId();
         $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
         $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
         ob_start();
         ob_start();
@@ -150,7 +151,16 @@ class jsonApiTest extends PHPUnit_Framework_TestCase
         $content = ob_get_contents();
         $content = ob_get_contents();
         $response = json_decode($content, true);
         $response = json_decode($content, true);
         $this->assertEquals(0, $response['status'], 'outputs success status');
         $this->assertEquals(0, $response['status'], 'outputs success status');
-        $this->assertEquals(array(helper::getPaste()), $response['messages'], 'outputs data correctly');
+        $this->assertEquals(helper::getPasteId(), $response['id'], 'outputs data correctly');
+        $this->assertStringEndsWith('?' . $response['id'], $response['url'], 'returned URL points to new paste');
+        $this->assertEquals($paste['data'], $response['data'], 'outputs data correctly');
+        $this->assertEquals($paste['attachment'], $response['attachment'], 'outputs attachment correctly');
+        $this->assertEquals($paste['attachmentname'], $response['attachmentname'], 'outputs attachmentname correctly');
+        $this->assertEquals($paste['meta']['formatter'], $response['meta']['formatter'], 'outputs format correctly');
+        $this->assertEquals($paste['meta']['postdate'], $response['meta']['postdate'], 'outputs postdate correctly');
+        $this->assertEquals($paste['meta']['opendiscussion'], $response['meta']['opendiscussion'], 'outputs opendiscussion correctly');
+        $this->assertEquals(0, $response['comment_count'], 'outputs comment_count correctly');
+        $this->assertEquals(0, $response['comment_offset'], 'outputs comment_offset correctly');
     }
     }
 
 
 }
 }

+ 17 - 8
tst/zerobin.php

@@ -587,7 +587,7 @@ class zerobinTest extends PHPUnit_Framework_TestCase
         $this->assertTag(
         $this->assertTag(
             array(
             array(
                 'id' => 'cipherdata',
                 'id' => 'cipherdata',
-                'content' => htmlspecialchars(json_encode(helper::getPaste()), ENT_NOQUOTES)
+                'content' => htmlspecialchars(helper::getPasteAsJson(), ENT_NOQUOTES)
             ),
             ),
             $content,
             $content,
             'outputs data correctly'
             'outputs data correctly'
@@ -671,7 +671,7 @@ class zerobinTest extends PHPUnit_Framework_TestCase
         $this->assertTag(
         $this->assertTag(
             array(
             array(
                 'id' => 'cipherdata',
                 'id' => 'cipherdata',
-                'content' => htmlspecialchars(json_encode($burnPaste), ENT_NOQUOTES)
+                'content' => htmlspecialchars(helper::getPasteAsJson($burnPaste['meta']), ENT_NOQUOTES)
             ),
             ),
             $content,
             $content,
             'outputs data correctly'
             'outputs data correctly'
@@ -684,7 +684,8 @@ class zerobinTest extends PHPUnit_Framework_TestCase
     public function testReadJson()
     public function testReadJson()
     {
     {
         $this->reset();
         $this->reset();
-        $this->_model->create(helper::getPasteId(), helper::getPaste());
+        $paste = helper::getPaste();
+        $this->_model->create(helper::getPasteId(), $paste);
         $_SERVER['QUERY_STRING'] = helper::getPasteId();
         $_SERVER['QUERY_STRING'] = helper::getPasteId();
         $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
         $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
         ob_start();
         ob_start();
@@ -692,7 +693,14 @@ class zerobinTest extends PHPUnit_Framework_TestCase
         $content = ob_get_contents();
         $content = ob_get_contents();
         $response = json_decode($content, true);
         $response = json_decode($content, true);
         $this->assertEquals(0, $response['status'], 'outputs success status');
         $this->assertEquals(0, $response['status'], 'outputs success status');
-        $this->assertEquals(array(helper::getPaste()), $response['messages'], 'outputs data correctly');
+        $this->assertEquals(helper::getPasteId(), $response['id'], 'outputs data correctly');
+        $this->assertStringEndsWith('?' . $response['id'], $response['url'], 'returned URL points to new paste');
+        $this->assertEquals($paste['data'], $response['data'], 'outputs data correctly');
+        $this->assertEquals($paste['meta']['formatter'], $response['meta']['formatter'], 'outputs format correctly');
+        $this->assertEquals($paste['meta']['postdate'], $response['meta']['postdate'], 'outputs postdate correctly');
+        $this->assertEquals($paste['meta']['opendiscussion'], $response['meta']['opendiscussion'], 'outputs opendiscussion correctly');
+        $this->assertEquals(0, $response['comment_count'], 'outputs comment_count correctly');
+        $this->assertEquals(0, $response['comment_offset'], 'outputs comment_offset correctly');
     }
     }
 
 
     /**
     /**
@@ -717,21 +725,22 @@ class zerobinTest extends PHPUnit_Framework_TestCase
     {
     {
         $this->reset();
         $this->reset();
         $oldPaste = helper::getPaste();
         $oldPaste = helper::getPaste();
-        $oldPaste['meta'] = array(
+        $meta = array(
             'syntaxcoloring' => true,
             'syntaxcoloring' => true,
             'postdate' => $oldPaste['meta']['postdate'],
             'postdate' => $oldPaste['meta']['postdate'],
             'opendiscussion' => $oldPaste['meta']['opendiscussion'],
             'opendiscussion' => $oldPaste['meta']['opendiscussion'],
         );
         );
+        $oldPaste['meta'] = $meta;
         $this->_model->create(helper::getPasteId(), $oldPaste);
         $this->_model->create(helper::getPasteId(), $oldPaste);
         $_SERVER['QUERY_STRING'] = helper::getPasteId();
         $_SERVER['QUERY_STRING'] = helper::getPasteId();
         ob_start();
         ob_start();
         new zerobin;
         new zerobin;
         $content = ob_get_contents();
         $content = ob_get_contents();
-        $oldPaste['meta']['formatter'] = 'syntaxhighlighting';
+        $meta['formatter'] = 'syntaxhighlighting';
         $this->assertTag(
         $this->assertTag(
             array(
             array(
                 'id' => 'cipherdata',
                 'id' => 'cipherdata',
-                'content' => htmlspecialchars(json_encode($oldPaste), ENT_NOQUOTES)
+                'content' => htmlspecialchars(helper::getPasteAsJson($meta), ENT_NOQUOTES)
             ),
             ),
             $content,
             $content,
             'outputs data correctly'
             'outputs data correctly'
@@ -755,7 +764,7 @@ class zerobinTest extends PHPUnit_Framework_TestCase
         $this->assertTag(
         $this->assertTag(
             array(
             array(
                 'id' => 'cipherdata',
                 'id' => 'cipherdata',
-                'content' => htmlspecialchars(json_encode($oldPaste), ENT_NOQUOTES)
+                'content' => htmlspecialchars(helper::getPasteAsJson($oldPaste['meta']), ENT_NOQUOTES)
             ),
             ),
             $content,
             $content,
             'outputs data correctly'
             'outputs data correctly'