Skip to content

Commit

Permalink
#109 move
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Apr 21, 2020
1 parent 1d935bd commit cb9cdc6
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 55 deletions.
42 changes: 1 addition & 41 deletions codexia.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,47 +115,6 @@
haml :welcome, layout: nil, locals: merged
end

get '/recent' do
badges = (params[:badges] || '').strip.split(',')
page = (params[:page] || '0').strip.to_i
haml :recent, layout: :layout, locals: merged(
title: '/recent',
page: page,
list: the_author.projects.recent(
limit: 25,
offset: page * 25,
badges: badges,
show_deleted: the_author.karma.points > 100
)
)
end

get '/recent.json' do
content_type 'application/json'
JSON.pretty_generate(
the_author.projects.recent(
limit: 25,
offset: (params[:page] || '0').strip.to_i * 25,
show_deleted: true
)
)
end

get '/submit' do
haml :submit, layout: :layout, locals: merged(
title: '/submit'
)
end

post '/submit' do
platform = params[:platform]
raise Xia::Urror, '"platform" is a mandatory parameter' if platform.nil?
coordinates = params[:coordinates]
raise Xia::Urror, '"coordinates" is a mandatory parameter' if coordinates.nil?
project = the_author.projects.submit(platform, coordinates)
flash(iri.cut('/p').append(project.id), "A new project #{project.id} has been submitted!")
end

get '/terms' do
haml :terms, layout: :layout, locals: merged(
title: '/terms',
Expand Down Expand Up @@ -189,6 +148,7 @@ def iri
require_relative 'front/front_login.rb'
require_relative 'front/front_helpers.rb'
require_relative 'front/front_project.rb'
require_relative 'front/front_projects.rb'
require_relative 'front/front_karma.rb'
require_relative 'front/front_sql.rb'
require_relative 'front/front_bots.rb'
62 changes: 62 additions & 0 deletions front/front_projects.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# frozen_string_literal: true

# Copyright (c) 2020 Yegor Bugayenko
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the 'Software'), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

get '/recent' do
badges = (params[:badges] || '').strip.split(',')
page = (params[:page] || '0').strip.to_i
haml :recent, layout: :layout, locals: merged(
title: '/recent',
page: page,
list: the_author.projects.recent(
limit: 25,
offset: page * 25,
badges: badges,
show_deleted: the_author.karma.points > 100
)
)
end

get '/recent.json' do
content_type 'application/json'
JSON.pretty_generate(
the_author.projects.recent(
limit: 25,
offset: (params[:page] || '0').strip.to_i * 25,
show_deleted: true
)
)
end

get '/submit' do
haml :submit, layout: :layout, locals: merged(
title: '/submit'
)
end

post '/submit' do
platform = params[:platform]
raise Xia::Urror, '"platform" is a mandatory parameter' if platform.nil?
coordinates = params[:coordinates]
raise Xia::Urror, '"coordinates" is a mandatory parameter' if coordinates.nil?
project = the_author.projects.submit(platform, coordinates)
flash(iri.cut('/p').append(project.id), "A new project #{project.id} has been submitted!")
end
4 changes: 2 additions & 2 deletions objects/badges.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ def attach(text, force: false)
reviews = Xia::Reviews.new(@pgsql, @project, log: @log)
if after > before
Xia::Rank.new(@project.author).enter("badges.promote-to-#{text}")
reviews.post("The project has been promoted from L#{before} to L#{after}")
reviews.post("The project has been promoted from L#{before} to L#{after}", force: true)
end
if after < before
Xia::Rank.new(@project.author).enter("badges.degrade-from-L#{before}")
reviews.post("The project has been degraded from L#{before} to L#{after}")
reviews.post("The project has been degraded from L#{before} to L#{after}", force: true)
end
delete = true
elsif all.length >= 5
Expand Down
10 changes: 6 additions & 4 deletions objects/reviews.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ def exists?(hash)
)[0]['count'].to_i.zero?
end

def post(text, hash = SecureRandom.hex)
def post(text, hash = SecureRandom.hex, force: false)
Xia::Rank.new(@project.author).enter('reviews.post')
raise Xia::Urror, 'The project is dead, can\'t review' unless @project.deleted.nil?
raise Xia::Urror, 'The review is too short' if text.length < 60 && @project.author.login != '-test-'
raise Xia::Urror, 'You are reviewing too fast' if quota.negative?
raise Xia::Urror, 'The project is dead, can\'t review' unless force || @project.deleted.nil?
if text.length < 60 && !force
raise Xia::Urror, "The review is too short for us, just #{text.length} symbols (must be longer)"
end
raise Xia::Urror, 'You are reviewing too fast' if quota.negative? && !force
raise Xia::Urror, 'Hash can\'t be empty' if hash.empty?
raise DuplicateError, 'A review with this hash already exists' if exists?(hash)
id = @pgsql.exec(
Expand Down
7 changes: 5 additions & 2 deletions test/test_codexia.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

require 'minitest/autorun'
require 'rack/test'
require 'iri'
require_relative 'test__helper'
require_relative '../codexia'
require_relative '../objects/xia'
Expand Down Expand Up @@ -101,9 +102,11 @@ def test_api_post_review
name = 'abc/ddd'
id = post_project(name)
assert_equal(post_project(name), id)
post("/p/#{id}/post?text=hello&hash=123", nil, 'HTTP_X_CODEXIA_TOKEN' => '-test-')
text = 'This is a long enough review to pass all possible checks inside'
uri = Iri.new("/p/#{id}/post").add(text: text, hash: 123).to_s
post(uri, nil, 'HTTP_X_CODEXIA_TOKEN' => '-test-')
assert_equal(302, last_response.status, "#{p} fails: #{last_response.body}")
post("/p/#{id}/post?text=hello&hash=123", nil, 'HTTP_X_CODEXIA_TOKEN' => '-test-')
post(uri, nil, 'HTTP_X_CODEXIA_TOKEN' => '-test-')
assert_equal(302, last_response.status, "#{p} fails: #{last_response.body}")
end

Expand Down
2 changes: 1 addition & 1 deletion test/test_karma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_karma
author = authors.named(login)
project = author.projects.submit('github', "yegor256/takes#{rand(999)}")
reviews = project.reviews
reviews.post('How are you?', 'hey')
reviews.post('How are you?', 'hey', force: true)
karma = authors.named(login).karma.points
assert(karma.positive?, "The karma is #{karma}")
assert(!authors.named(login).karma.points(safe: true).nil?)
Expand Down
2 changes: 1 addition & 1 deletion test/test_review.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_votes_review
projects = author.projects
project = projects.submit('github', "yegor256/takes#{rand(999)}")
reviews = project.reviews
review = reviews.post('How are you?', 'ab')
review = reviews.post('How are you?', 'ab', force: true)
id = review.vote(true)
assert(!id.nil?)
assert_equal(id, review.vote(false))
Expand Down
8 changes: 4 additions & 4 deletions test/test_reviews.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_submits_review
projects = author.projects
project = projects.submit('github', "yegor256/takes#{rand(999)}")
reviews = project.reviews
review = reviews.post('How are you?', '--')
review = reviews.post('How are you?', '--', force: true)
assert(!review.id.nil?)
assert(!reviews.recent.empty?)
end
Expand All @@ -51,17 +51,17 @@ def test_fetches_only_right_reviews
author = Xia::Authors.new(t_pgsql).named('-test-')
projects = author.projects
project = projects.submit('github', "yegor256/foo#{rand(999)}")
project.reviews.post('How are you?', 'abc')
project.reviews.post('How are you?', 'abc', force: true)
assert_equal(1, project.reviews.recent.size)
end

def test_rejects_duplicate_markers
author = Xia::Authors.new(t_pgsql).named('-test-')
projects = author.projects
project = projects.submit('github', "yegor256/foo#{rand(999)}")
project.reviews.post('The test', 'hash')
project.reviews.post('The test', 'hash', force: true)
assert_raises(Xia::Reviews::DuplicateError) do
project.reviews.post('Another test', 'hash')
project.reviews.post('Another test', 'hash', force: true)
end
end
end

0 comments on commit cb9cdc6

Please sign in to comment.