Showing reviews on package's details page from Ubuntu

This commit is contained in:
Christoph Haas 2016-08-16 12:50:03 +02:00
parent fc95fc9d9a
commit 339bb0427f
10 changed files with 89 additions and 1 deletions

View file

@ -19,3 +19,21 @@
//= require_tree .
$(function(){ $(document).foundation(); });
// Load reviews on package's details page
load_reviews = function() {
// Display message while loading reviews
// $('#reviews').html("<i>Loading reviews...</i>");
// Hide the DIV so that in can be faded in later
$('#reviews').hide();
// The URL is specified in the #reviews's DIV data attribute
var url = $('#reviews').attr('data-package-reviews-url');
$('#reviews').load(url, null,
// Fade in the reviews as soon as the DIV is loaded and filled
function(){
$(this).fadeIn('slow')
}
);
};

View file

@ -191,6 +191,12 @@ class PackagesController < ApplicationController
redirect_to :back
end
# Show an HTML partial with reviews of this package from the Ubuntu API
def reviews
@reviews = Package.find_by_name!(params[:name]).ubuntu_reviews
render '_reviews', layout: false
end
private
# Send a dummy thumbnail reading "No screenshot available. Sorry."

View file

@ -16,4 +16,11 @@ module PackagesHelper
end
end
# Show filled/empty star icons from FontAwesome
# depending on the rating (1-5)
def star_rating(rating)
filled_stars = fa_icon('star') * rating
empty_stars = fa_icon('star-o') * (5-rating)
(filled_stars+empty_stars).html_safe
end
end

View file

@ -1,3 +1,6 @@
require 'open-uri' # allows to load URLs using open()
require 'json'
class Package < ActiveRecord::Base
# PostgreSQL-based full-text search:
# https://github.com/Casecommons/pg_search
@ -57,6 +60,20 @@ class Package < ActiveRecord::Base
self.screenshots.to_a.sort { |x,y| version_compare(x.version,y.version) }
end
# Get reviews of this package from the Ubuntu API
def ubuntu_reviews
# Use the URL defined in the configuration to get a JSON string
json = open(Rails.configuration.ubuntu_reviews_api_url % self.name).read
# Turn JSON into a Ruby data structure
json = JSON.parse(json)
# Only show english reviews
# TODO: Support further languages
json = json.select {|x| x['language']=='en'}
# Sort by 'usefulness_total' (how many people found this review useful)
json = json.sort { |x,y| y['usefulness_total'].to_i <=> x['usefulness_total'].to_i}
return json
end
# Return the newest screenshot that is not newer than the given version.
# This algorithm collects all image
# versions of a package and determines the (second) newest version.

View file

@ -0,0 +1,22 @@
- if @reviews.count > 0
.subtitle Reviews
- @reviews.each do |review|
p
div
b = review['summary']
' …
= review['review_text']
.text-right
em
= review['reviewer_displayname']
'
= time_ago_in_words(DateTime.parse(review['date_created']))
' ago
= star_rating(review['rating'])
/ - else
/ p
/ em No reviews available.

View file

@ -43,7 +43,6 @@
onclick="return confirm('Really delete your screenshot again?');"
] Delete your screenshot
// or is the user not related to the screenshot and the screenshot is public?
- elsif screenshot.approved
= render(partial: 'report_dropdown', locals: {screenshot: screenshot})
@ -55,7 +54,18 @@
// TODO: Enable comments in a later version
// = partial '/package/comments'
// Load reviews from the Ubuntu API through our own (cached) URL.
// Use asynchronous load to keep loading times low if the review
// was not yet cached.
/ = render(partial: 'reviews', locals: {pkg: @package})
#reviews data-package-reviews-url=package_reviews_path(@package.name)
.small-5.medium-5.columns
= render(partial: 'details_rightbox', locals: {pkg: @package})
= render(partial: 'details_uploadmodal')
javascript:
$( function() {
load_reviews()
})

View file

@ -46,6 +46,10 @@ Debshots::Application.configure do
}
]
# Example file for development to avoid queries to online service
config.ubuntu_reviews_api_url = 'test/files/ubuntu-review-api/cream'
# config.ubuntu_reviews_api_url = 'https://reviews.ubuntu.com/reviews/api/1.0/reviews/filter/en/any/any/any/%s'
config.images_path = Rails.root.join('public', 'screenshots')
config.image_sizes = {

View file

@ -97,6 +97,9 @@ Debshots::Application.configure do
}
]
# URL to fetch reviews from the Ubuntu JSON API
config.ubuntu_reviews_api_url = 'https://reviews.ubuntu.com/reviews/api/1.0/reviews/filter/en/any/any/any/%s'
config.images_path = Rails.root.join('public', 'screenshots')
config.image_sizes = {

View file

@ -16,6 +16,7 @@ Debshots::Application.routes.draw do
get 'packages/list' => 'packages#list', as: :packages_list
get 'moderate' => 'moderate#index'
get 'package/:name' => 'packages#details', as: :package, name: /[^\/]+/
get 'package_reviews/:name' => 'packages#reviews', as: :package_reviews, name: /[^\/]+/
get 'upload', to: redirect('/packages') # legacy upload form
post 'uploadfile' => 'packages#legacy_uploadfile'
get 'upload/:name' => 'packages#upload', as: :upload_package_by_name