hensei-api/app/controllers/api/v1/users_controller.rb
Justin Edmund 6cf11e6517
Jedmund/fix image embeds 4 (#177)
* Update gitignore

There is a mystery postgres folder and we are going to ignore it

* Add migrations

* Update preview state default to pending
* Adds indexes
* Adds PgHero and PgStatements

* Update .gitignore

* Update Gemfile

Production:
- `newrelic_rpm`

Development:
- `pg_query`
- `prosopite`

* Configure Sidekiq

Create job for cleaning up party previews

* Configure Prosopite and remove CacheFreeLogger

* Enable query logging

* Update api_controller.rb

Add N+1 detectioin via Prosopite in development/test environments

* Refactor canonical object blueprints

* Refactor grid object blueprints

* Remove N+1 from grid object models

Reimplementing `character` `summon` and `weapon` was making N+1s which made queries really slow

* Add counter caches to party

* Add preview generation helpers

The Party model can respond to queries about its preview state with the following models:
- `schedule_preview_generation`
- `preview_content_changed?`
- `preview_expired?`
- `should_generate_preview?`
- `ready_for_preview?`
- `needs_preview_generation?`
- `preview_relevant_attributes`

Removes the following methods:
- `schedule_preview_regeneration`
- `preview_relevant_changes?`

* Add cache to is_favorited(user)

* Refactored PartyBlueprint to minimize N+1s

* Remove preview expiry constants

These are defined in the Coordinator instead

* Add method comments

* Create parties_controller.rbs

* Update logic and logs

* Updates excluded methods and calculate_count

* Use `includes` instead of `joins`
* Use a less-insane way of counting

* Adds a helper method for party privacy

* Update filter condition helpers

Just minor refactoring

* Fix old view name in PartyBlueprint

* Refactor parties#create

* Remove redundant return

* Update parties_controller.rbs

* Update parties#index

* Update parties_controller.rb

Updates apply_includes and apply_excludes, along with modifying id_to_table and build_query

* Update parties_controller.rb

Adds the rest of the changes, too tired to write them all out. Some preview generation, some filtering

* Refactor parties#index and parties#favorites

These are mostly the same methods, so we remove common code into build_parties_query and render_paginated_parties

* Alias table name to object to maintain API consistency

* Maintain API consistency with raid blueprint

* Optimize party loading by adding eager loading to `set_from_slug`

- Refactored `set_from_slug` to use `includes` for eager loading associated models:
  - `user`, `job`, `raid` (with `group`)
  - `characters` (with `character` and `awakening`)
  - `weapons` (with `weapon`, `awakenings`, `weapon_key1`, `weapon_key2`, `weapon_key3`)
  - `summons` (with `summon`)
  - `guidebooks` (`guidebook1`, `guidebook2`, `guidebook3`)
  - `source_party`, `remixes`, `skills`, and `accessory`
- This change improves query efficiency by reducing N+1 queries and ensures all relevant associations are preloaded.
- Removed redundant favorite check as it was not necessary in this context.

* Refactor grid blueprints

- **GridCharacterBlueprint:**
  - Removed `:minimal` view restriction on `party` association.
  - Improved nil checks for `ring1`, `ring2`, and `earring` to prevent errors.
  - Converted string values in `awakening_level`, `over_mastery`, and `aetherial_mastery` fields to integers for consistency.
  - Ensured `over_mastery` and `aetherial_mastery` only include valid entries, filtering out blank or zero-modifier values.

- **GridWeaponBlueprint:**
  - Removed `:minimal` view restriction on `party` association.
  - Ensured `weapon` association exists before accessing `ax`, `series`, or `awakening`.
  - Improved conditional checks for `weapon_keys` to prevent errors when `weapon` or `series` is nil.
  - Converted `awakening_level` field to integer for consistency.

- **GridCharacterBlueprint:**
  - Removed `:minimal` view restriction on `party` association.

* Update raid blueprints

- Show flat representation of raid group in RaidBlueprint's nested view
- Show nested representation of raid in RaidGroupBlueprint's full view

* Move n+1 detection to around_action hook

* Improve handling mastery bonuses

- Improved handling of nested attributes:
  - Replaced old mastery structure with new `rings` and `awakening` assignments.
  - Added `new_rings` and `new_awakening` virtual attributes for easier updates.
  - Updated `assign_attributes` to exclude `rings` and `awakening` to prevent conflicts.

- Enhanced parameter transformation:
  - Introduced `transform_character_params` to process `rings`, `awakening`, and `earring` more reliably.
  - Ensured proper type conversion (`to_i`) for numeric values in `uncap_level`, `transcendence_step`, and `awakening_level`.
  - Improved error handling for missing values by setting defaults where needed.

- Optimized database queries:
  - Added `.includes(:awakening)` to `set` to prevent N+1 query issues.

- Updated strong parameters:
  - Changed `rings` from individual keys (`ring1`, `ring2`, etc.) to a structured array format.
  - Refactored permitted attributes to align with the new nested structure.

* Eager-load jobs when querying job skills

* Eager load raids/groups when querying

* Update users_controller.rb

More efficient way of denoting favorited parties.

* Update awakening.rb

- Removes explicitly defined associations and adds ActiveRecord associations instead

* Update party.rb

- Removes favorited accessor
- Renames derivative_parties to remixes and adds in-built sort

* Update weapon_awakening.rb

- Removes redefined explicit associations

* Update grid_character.rb

- Adds code transforming incoming ring and awakening values into something the db understands

* Update character.rb

Add explicit Awakenings enum

* Update coordinator.rb

Adds 'queued' as a state for generation
2025-02-09 22:50:18 -08:00

205 lines
7.4 KiB
Ruby

# frozen_string_literal: true
module Api
module V1
class UsersController < Api::V1::ApiController
class ForbiddenError < StandardError; end
before_action :set, except: %w[create check_email check_username]
before_action :set_by_id, only: %w[info update]
MAX_CHARACTERS = 5
MAX_SUMMONS = 8
MAX_WEAPONS = 13
DEFAULT_MIN_CHARACTERS = 0
DEFAULT_MIN_SUMMONS = 0
DEFAULT_MIN_WEAPONS = 0
DEFAULT_MAX_CLEAR_TIME = 5400
def create
user = User.new(user_params)
if user.save!
token = Doorkeeper::AccessToken.create!(
application_id: nil,
resource_owner_id: user.id,
expires_in: 30.days,
scopes: 'public'
).token
return render json: UserBlueprint.render({
id: user.id,
username: user.username,
token: token
},
view: :token),
status: :created
end
render_validation_error_response(@user)
end
# TODO: Allow admins to update other users
def update
render json: UserBlueprint.render(@user, view: :minimal) if @user.update(user_params)
end
def info
render json: UserBlueprint.render(@user, view: :minimal)
end
def show
if @user.nil?
render_not_found_response('user')
else
conditions = build_conditions
conditions[:user_id] = @user.id
favorites_query = "EXISTS (SELECT 1 FROM favorites WHERE favorites.party_id = parties.id AND favorites.user_id = #{current_user&.id || 'NULL'}) AS is_favorited"
parties = Party.where(conditions)
.where(name_quality)
.where(user_quality)
.where(original)
.where(privacy)
.includes(:favorites)
.select(Party.arel_table[Arel.star])
.select(
Arel.sql(favorites_query)
)
.order(created_at: :desc)
.paginate(page: request.params[:page], per_page: COLLECTION_PER_PAGE)
count = Party.where(conditions).count
render json: UserBlueprint.render(@user,
view: :profile,
root: 'profile',
parties: parties,
meta: {
count: count,
total_pages: count.to_f / COLLECTION_PER_PAGE > 1 ? (count.to_f / COLLECTION_PER_PAGE).ceil : 1,
per_page: COLLECTION_PER_PAGE
})
end
end
def check_email
render json: EmptyBlueprint.render_as_json(nil, email: params[:email], availability: true)
end
def check_username
render json: EmptyBlueprint.render_as_json(nil, username: params[:username], availability: true)
end
def destroy; end
private
def build_conditions
params = request.params
unless params['recency'].blank?
start_time = (DateTime.current - params['recency'].to_i.seconds)
.to_datetime.beginning_of_day
end
min_characters_count = params['characters_count'].blank? ? DEFAULT_MIN_CHARACTERS : params['characters_count'].to_i
min_summons_count = params['summons_count'].blank? ? DEFAULT_MIN_SUMMONS : params['summons_count'].to_i
min_weapons_count = params['weapons_count'].blank? ? DEFAULT_MIN_WEAPONS : params['weapons_count'].to_i
max_clear_time = params['max_clear_time'].blank? ? DEFAULT_MAX_CLEAR_TIME : params['max_clear_time'].to_i
{}.tap do |hash|
# Basic filters
hash[:element] = params['element'].to_i unless params['element'].blank?
hash[:raid] = params['raid'] unless params['raid'].blank?
hash[:created_at] = start_time..DateTime.current unless params['recency'].blank?
# Advanced filters: Team parameters
unless params['full_auto'].blank? || params['full_auto'].to_i == -1
hash[:full_auto] =
params['full_auto'].to_i
end
unless params['auto_guard'].blank? || params['auto_guard'].to_i == -1
hash[:auto_guard] =
params['auto_guard'].to_i
end
unless params['charge_attack'].blank? || params['charge_attack'].to_i == -1
hash[:charge_attack] =
params['charge_attack'].to_i
end
# Turn count of 0 will not be displayed, so disallow on the frontend or set default to 1
# How do we do the same for button count since that can reasonably be 1?
# hash[:turn_count] = params['turn_count'].to_i unless params['turn_count'].blank? || params['turn_count'].to_i <= 0
# hash[:button_count] = params['button_count'].to_i unless params['button_count'].blank?
# hash[:clear_time] = 0..max_clear_time
# Advanced filters: Object counts
hash[:characters_count] = min_characters_count..MAX_CHARACTERS
hash[:summons_count] = min_summons_count..MAX_SUMMONS
hash[:weapons_count] = min_weapons_count..MAX_WEAPONS
end
end
def original
return if params.key?('original') || params['original'].blank? || params['original'] == '0'
'source_party_id IS NULL'
end
def user_quality
return if params.key?('user_quality') || params[:user_quality].nil? || params[:user_quality] == '0'
'user_id IS NOT NULL'
end
def name_quality
low_quality = [
'Untitled',
'Remix of Untitled',
'Remix of Remix of Untitled',
'Remix of Remix of Remix of Untitled',
'Remix of Remix of Remix of Remix of Untitled',
'Remix of Remix of Remix of Remix of Remix of Untitled',
'無題',
'無題のリミックス',
'無題のリミックスのリミックス',
'無題のリミックスのリミックスのリミックス',
'無題のリミックスのリミックスのリミックスのリミックス',
'無題のリミックスのリミックスのリミックスのリミックスのリミックス'
]
joined_names = low_quality.map { |name| "'#{name}'" }.join(',')
return if params.key?('name_quality') || params[:name_quality].nil? || params[:name_quality] == '0'
"name NOT IN (#{joined_names})"
end
def privacy
return if admin_mode
'visibility = 1' if current_user != @user
end
# Specify whitelisted properties that can be modified.
def set
@user = User.find_by('lower(username) = ?', params[:id].downcase)
end
def set_by_id
@user = User.find_by('id = ?', params[:id])
end
def user_params
params.require(:user).permit(
:username, :email, :password, :password_confirmation,
:granblue_id, :picture, :element, :language, :gender, :private, :theme
)
end
end
end
end