Skip to content

Commit

Permalink
Replace old code by new_watchlist code in API
Browse files Browse the repository at this point in the history
  • Loading branch information
saraycp committed Nov 29, 2022
1 parent b07306d commit 8015845
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 51 deletions.
57 changes: 18 additions & 39 deletions src/api/app/controllers/person_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'xmlhash'

class PersonController < ApplicationController
# TODO: action userinfo no longer exists, remove these validations
validate_action userinfo: { method: :get, response: :user }
validate_action userinfo: { method: :put, request: :user, response: :status }
validate_action grouplist: { method: :get, response: :directory }
Expand All @@ -11,7 +12,6 @@ class PersonController < ApplicationController
skip_before_action :require_login, only: [:command, :register]

before_action :set_user, only: [:post_userinfo, :change_my_password, :get_watchlist, :put_watchlist]
before_action :check_user_belongs_feature_flag, only: [:get_watchlist, :put_watchlist]

def show
@list = if params[:prefix]
Expand Down Expand Up @@ -160,7 +160,7 @@ def put_watchlist

xml = Xmlhash.parse(request.raw_post)
ActiveRecord::Base.transaction do
update_new_watchlist(@user, xml)
update_watchlist(@user, xml)
end
render_ok
end
Expand Down Expand Up @@ -224,22 +224,26 @@ def internal_register
end

def update_watchlist(user, xml)
# Keeping the backwards compatibility
if Flipper.enabled?(:new_watchlist, User.session)
update_new_watchlist(user, xml)
if xml.get('watchlist').empty?
projects = [xml.get('project')].flatten
packages = [xml.get('package')].flatten
requests = [xml.get('request')].flatten
else
new_watchlist = []
xml.get('watchlist').elements('project') do |e|
new_watchlist << e['name']
end
projects = xml.get('watchlist').elements('project')
packages = xml.get('watchlist').elements('package')
requests = xml.get('watchlist').elements('request')
end

new_watchlist.map! do |name|
WatchedProject.find_or_create_by(project: Project.find_by_name!(name), user: user)
end
user.watched_projects.replace(new_watchlist)
Rails.cache.delete(['watched_project_names', user])
watchables = []
watchables << projects.map { |proj| Project.find_by(name: proj['name']) }
watchables << packages.map { |pkg| Package.find_by_project_and_name(pkg['project'], pkg['name']) }
watchables << requests.map { |req| BsRequest.find_by(number: req['number']) }
user.watched_items.clear
watchables.flatten.compact.each do |item|
user.watched_items.create!(watchable: item)
end
end

private :update_watchlist

def update_globalroles(user, xml)
Expand Down Expand Up @@ -298,32 +302,7 @@ def change_password(login, password)

private

def update_new_watchlist(user, xml)
if xml.get('watchlist').empty?
projects = [xml.get('project')].flatten
packages = [xml.get('package')].flatten
requests = [xml.get('request')].flatten
else
projects = xml.get('watchlist').elements('project')
packages = xml.get('watchlist').elements('package')
requests = xml.get('watchlist').elements('request')
end

watchables = []
watchables << projects.map { |proj| Project.find_by(name: proj['name']) }
watchables << packages.map { |pkg| Package.find_by_project_and_name(pkg['project'], pkg['name']) }
watchables << requests.map { |req| BsRequest.find_by(number: req['number']) }
user.watched_items.clear
watchables.flatten.compact.each do |item|
user.watched_items.create!(watchable: item)
end
end

def set_user
@user = User.find_by(login: params[:login])
end

def check_user_belongs_feature_flag
raise NotFoundError unless Flipper.enabled?(:new_watchlist, User.session)
end
end
10 changes: 1 addition & 9 deletions src/api/app/views/models/_user.xml.builder
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@ else
xml.ignore_auth_services(my_model.ignore_auth_services) if my_model.is_admin?

# Show the watchlist only to the user for privacy reasons
if Flipper.enabled?(:new_watchlist, User.session) && watchlist
render partial: 'person/watchlist', locals: { builder: xml, my_model: my_model }
elsif watchlist
xml.watchlist do
my_model.watched_projects.each do |wp|
xml.project(name: wp.project.name)
end
end
end
render partial: 'person/watchlist', locals: { builder: xml, my_model: my_model } if watchlist
end
end
1 change: 1 addition & 0 deletions src/api/app/views/person/userinfo.xml.builder
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# TODO: remove this view, the corresponding action no longer exists
xml.person do
xml.login @render_user.login
xml.email @render_user.email
Expand Down
3 changes: 0 additions & 3 deletions src/api/spec/controllers/person_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@

before do
create(:package, project: project, name: 'test-pkg')
Flipper.enable_actor(:new_watchlist, admin_user)
login admin_user
put :put_userinfo, params: { login: test_user.login, format: :xml }, body: xml
end
Expand Down Expand Up @@ -198,7 +197,6 @@
before do
project = create(:project, name: 'test-proj')
package = create(:package, project: project, name: 'test-pkg')
Flipper.enable_actor(:new_watchlist, user)
user.watched_items.create(watchable: project)
user.watched_items.create(watchable: package)
login user
Expand Down Expand Up @@ -227,7 +225,6 @@
let!(:package) { create(:package, project: project, name: 'test-pkg') }

before do
Flipper.enable_actor(:new_watchlist, user)
login user
put :put_watchlist, params: { login: user.login, format: :xml }, body: xml
end
Expand Down

0 comments on commit 8015845

Please sign in to comment.