How to get a previous version/revision of a post through the API


#1

Looking at the API, I don’t find any way to get a previous version/revision of a post. Am I missing something?

Also, I would like to get the diff between 2 versions. Is this computed client-side only or store in the DB?


(Vinoth Kannan) #2

Call GET method to the URL /posts/POSTID/revisions/VERSION.json


#3

Excellent, thanks @vinothkannans.

It works great for VERSION >=2, but it doesn’t for VERSION === 1.

Discourse::InvalidParameters in PostsController#revisions

revision

Extracted source (around line #481):
    post_id = params[:id] || params[:post_id]
    revision = params[:revision].to_i
    raise Discourse::InvalidParameters.new(:revision) if revision < 2

    post_revision = PostRevision.find_by(post_id: post_id, number: revision)
    raise Discourse::NotFound unless post_revision

Rails.root: /home/sylvain/discourse

Application Trace | Framework Trace | Full Trace
app/controllers/posts_controller.rb:481:in `find_post_revision_from_params'
app/controllers/posts_controller.rb:293:in `revisions'
config/initializers/100-quiet_logger.rb:17:in `call_with_quiet_assets'
config/initializers/100-silence_logger.rb:29:in `call'
lib/middleware/missing_avatars.rb:21:in `call'
lib/middleware/turbo_dev.rb:33:in `call'

Request

Parameters:

{"post_id"=>"16",
 "revision"=>"1",
 "format"=>"json"}

Any idea why? Any solution to get the first revision of a post?


(Vinoth Kannan) #4

PostRevision will not have exact post of the specific version. It will have the post with modifications like below

<del>Hi, </del>This is example post <ins>revision</ins>.
Hi, This is example post revision.

And it will start from number 2 only. Because revision 2 is the modifications of the post version 1 from version 2.

If you need the exact post instead then you may have to find an another way. Explore the code :thumbsup:.


#5

Of course @vinothkannans, thanks again, I should have had a closer look.

For those landing here, notice that Discourse doesn’t really get you revision number n. Instead, it returns html formatted blocks, ready to be displayed in the various tabs of the Discourse “history” window. This is how it looks:

{  
   "created_at":"2017-03-01T17:41:38.441Z",
   "post_id":15,
   "first_revision":2,
   "previous_revision":2,
   "current_revision":3,
   "next_revision":null,
   "last_revision":3,
   "current_version":3,
   "version_count":3,
   [...]
   "body_changes":{  
      "inline":"<div class=\"inline-diff\"><p><ul>\n<li><p>Why should people use this category? What is it for?</p></li>\n<li><p>How exactly is this different than the other categories we already have?</p></li>\n<li><p>What should topics in this category generally contain?</p></li>\n<li><p>Do we need this category? Can we merge with another category, or subcategory?</p></li>\n</ul><p>aaaaaaa<br class=\"diff-ins\"><ins>bbbbbbb</ins></p></div>",
      "side_by_side":"<div class=\"span8\"><p><ul>\n<li><p>Why should people use this category? What is it for?</p></li>\n<li><p>How exactly is this different than the other categories we already have?</p></li>\n<li><p>What should topics in this category generally contain?</p></li>\n<li><p>Do we need this category? Can we merge with another category, or subcategory?</p></li>\n</ul><p>aaaaaaa</p></div><div class=\"span8 offset1\"><p><ul>\n<li><p>Why should people use this category? What is it for?</p></li>\n<li><p>How exactly is this different than the other categories we already have?</p></li>\n<li><p>What should topics in this category generally contain?</p></li>\n<li><p>Do we need this category? Can we merge with another category, or subcategory?</p></li>\n</ul><p>aaaaaaa<br class=\"diff-ins\"><ins>bbbbbbb</ins></p></div>",
      "side_by_side_markdown":"<table class=\"markdown\"><tr><td>- Why should people use this category? What is it for?\n\n- How exactly is this different than the other categories we already have?\n\n- What should topics in this category generally contain?\n\n- Do we need this category? Can we merge with another category, or subcategory?\n\n</td><td>- Why should people use this category? What is it for?\n\n- How exactly is this different than the other categories we already have?\n\n- What should topics in this category generally contain?\n\n- Do we need this category? Can we merge with another category, or subcategory?\n\n</td></tr><tr><td class=\"diff-del\">aaaaaaa</td><td class=\"diff-ins\">aaaaaaa<ins>\nbbbbbbb</ins></td></tr></table>"
   },
   "title_changes":{  
      "inline":"<div class=\"inline-diff\"><div>About the subcategory category</div></div>",
      "side_by_side":"<div class=\"span8\"><div>About the subcategory category</div></div><div class=\"span8 offset1\"><div>About the subcategory category</div></div>"
   },
   "user_changes":null,
   "wiki":false,
   "can_edit":true
}