Skip to content

Commit

Permalink
Unapproved users (publiclab#7203)
Browse files Browse the repository at this point in the history
* Added the test

* Added 759-819 in if statement

* Fixed indentation

* Updated line 596

* Modified the statements

* Deleted modified the statements commit

* Restored the node.rb to commit before "Modified the statements"

* Refactored the line 810 - 812

* Changed '==' to '=' in line 811
  • Loading branch information
sssash18 authored and SidharthBansal committed Jan 12, 2020
1 parent 8d30dfb commit a76b1b3
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 47 deletions.
96 changes: 50 additions & 46 deletions app/models/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -756,66 +756,70 @@ def add_barnstar(tagname, giver)
end

def add_tag(tagname, user)
tagname = tagname.downcase
unless has_tag_without_aliasing(tagname)
saved = false
table_updated = false
tag = Tag.find_by(name: tagname) || Tag.new(vid: 3, # vocabulary id; 1
if user.status == 1
tagname = tagname.downcase
unless has_tag_without_aliasing(tagname)
saved = false
table_updated = false
tag = Tag.find_by(name: tagname) || Tag.new(vid: 3, # vocabulary id; 1
name: tagname,
description: '',
weight: 0)

ActiveRecord::Base.transaction do
if tag.valid?
key = tag.name.split(':')[0]
value = tag.name.split(':')[1]
# add base tags:
if ['question', 'upgrade', 'activity'].include?(key)
add_tag(value, user)
end
# add sub-tags:
subtags = {}
subtags['pm'] = 'particulate-matter'
if subtags.include?(key)
add_tag(subtags[key], user)
end
# parse date tags:
if key == 'date'
begin
DateTime.strptime(value, '%m-%d-%Y').to_date.to_s(:long)
rescue StandardError
return [false, tag.destroy]
ActiveRecord::Base.transaction do
if tag.valid?
key = tag.name.split(':')[0]
value = tag.name.split(':')[1]
# add base tags:
if ['question', 'upgrade', 'activity'].include?(key)
add_tag(value, user)
end
end
tag.save!
node_tag = NodeTag.new(tid: tag.id,
# add sub-tags:
subtags = {}
subtags['pm'] = 'particulate-matter'
if subtags.include?(key)
add_tag(subtags[key], user)
end
# parse date tags:
if key == 'date'
begin
DateTime.strptime(value, '%m-%d-%Y').to_date.to_s(:long)
rescue StandardError
return [false, tag.destroy]
end
end
tag.save!
node_tag = NodeTag.new(tid: tag.id,
uid: user.uid,
date: DateTime.now.to_i,
nid: id)

# Adding lat/lon values into node table
if key == 'lat'
tagvalue = value
table_updated = update_attributes(latitude: tagvalue, precision: decimals(tagvalue).to_s)
elsif key == 'lon'
tagvalue = value
table_updated = update_attributes(longitude: tagvalue)
end
# Adding lat/lon values into node table
if key == 'lat'
tagvalue = value
table_updated = update_attributes(latitude: tagvalue, precision: decimals(tagvalue).to_s)
elsif key == 'lon'
tagvalue = value
table_updated = update_attributes(longitude: tagvalue)
end

if node_tag.save
saved = true
tag.run_count # update count of tag usage
# send email notification if there are subscribers, status is OK, and less than 1 month old
unless tag.subscriptions.empty? || status == 3 || status == 4 || created < (DateTime.now - 1.month).to_i
SubscriptionMailer.notify_tag_added(self, tag, user).deliver_now
if node_tag.save
saved = true
tag.run_count # update count of tag usage
# send email notification if there are subscribers, status is OK, and less than 1 month old
isStatusValid = status == 3 || status == 4
isMonthOld = created < (DateTime.now - 1.month).to_i
unless tag.subscriptions.empty? || isStatusValid || !isMonthOld
SubscriptionMailer.notify_tag_added(self, tag, user).deliver_now
end
else
saved = false
tag.destroy
end
else
saved = false
tag.destroy
end
end
return [saved, tag, table_updated]
end
return [saved, tag, table_updated]
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/functional/wiki_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ def teardown
@user = UserSession.create(users(:jeff))
@node = nodes(:wiki_page)
slug = @node.path.gsub('/wiki/', '')
@node.add_tag('date:bad', @user)
@node.add_tag('date:bad', users(:jeff))

assert_equal false, @node.has_power_tag('date')
# assert_equal "anything goes", DateTime.strptime(@node.power_tag('date'),'%m- %d-%Y').to_date.to_s(:long)
Expand Down
7 changes: 7 additions & 0 deletions test/unit/node_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -529,4 +529,11 @@ def setup
assert_equal 'Canon A1200 IR conversion at PLOTS Barnraising at LUMCON', Node.find_by_tag_and_author('awesome', 2, 'notes').first.title
assert_equal 'Question by a moderated user', Node.find_by_tag_and_author('question:spectrometer', 9, 'questions').first.title
end
test 'non-approved users should not be able to add tags' do
node = nodes(:one)
assert_difference 'node.tags.count', 0 do
node.add_tag('myspamtag', users(:spammer))
end
assert_not node.has_tag('myspamtag')
end
end

0 comments on commit a76b1b3

Please sign in to comment.