Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate: Migrate handle #1

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ Chart Cyanvas is a sekai custom charts platform.
> **Warning**
> This project is still in development!

## Running

1. Install [Docker](https://www.docker.com/)
2. Copy `.env.prod.example` to `.env.prod` and fill the variables
3. Run `docker compose -fdocker-compose.prod.yml --profile prod --env-file .env.prod up -d`

## Architecture

![Architecture](./architecture.svg)
Expand Down
13 changes: 2 additions & 11 deletions backend/app/controllers/api/admin_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,7 @@ def reconvert_sus

def show_user
params.require(:handle)
@user =
if params[:handle].start_with?("x")
User
.where(handle: params[:handle].delete_prefix("x"))
.where.not(owner_id: nil)
.first
.owner
else
User.find_by(handle: params[:handle])
end
@user = User.find_by(handle: params[:handle])
if @user
user_data = @user.to_frontend
user_data[:altUsers] = @user.alt_users.map(&:to_frontend)
Expand All @@ -50,7 +41,7 @@ def show_user
end

around_action do |controller, action|
if !ENV["ADMIN_HANDLE"] || current_user&.handle != ENV["ADMIN_HANDLE"]
unless current_user&.admin?
logger.warn "Unauthorized admin access attempt by #{current_user&.handle} (Admin handle: #{ENV["ADMIN_HANDLE"]})"
render json: { code: "forbidden" }, status: :forbidden
next
Expand Down
12 changes: 4 additions & 8 deletions backend/app/controllers/api/charts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def all
end

if params[:author]
user = User.find_by(handle: params[:author].delete_prefix("x"))
user = User.find_by(handle: params[:author])
unless user
render json: {
code: "not_found",
Expand Down Expand Up @@ -202,8 +202,7 @@ def process_chart_request
return
end

author =
User.find_by(handle: data_parsed[:authorHandle].delete_prefix("x"))
author = User.find_by(handle: data_parsed[:authorHandle])
unless author
render json: {
code: "not_found",
Expand All @@ -214,9 +213,7 @@ def process_chart_request
end

session_user = User.find_by(id: session[:user_id])
unless (
ENV["ADMIN_HANDLE"] && session_user.handle == ENV["ADMIN_HANDLE"]
) || author.id == session[:user_id] ||
unless (session_user.admin?) || author.id == session[:user_id] ||
author.owner_id == session[:user_id]
render json: {
code: "forbidden",
Expand Down Expand Up @@ -410,8 +407,7 @@ def delete
end

user = User.find_by(id: session[:user_id])
unless (ENV["ADMIN_HANDLE"] && user&.handle == ENV["ADMIN_HANDLE"]) ||
chart.author_id == session[:user_id] ||
unless user&.admin? || chart.author_id == session[:user_id] ||
chart.author.owner_id == session[:user_id]
render json: {
code: "forbidden",
Expand Down
11 changes: 2 additions & 9 deletions backend/app/controllers/api/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,8 @@ module Api
class UsersController < FrontendController
def show
params.require(:handle)
@user =
if params[:handle].start_with?("x")
User
.where(handle: params[:handle].delete_prefix("x"))
.where.not(owner_id: nil)
.first
else
User.find_by(handle: params[:handle])
end
@user = User.find_by(sonolus_handle: params[:handle])
@user = User.find_by(handle: params[:handle]) unless @user
if @user
render json: { code: "ok", user: @user.to_frontend }
else
Expand Down
13 changes: 6 additions & 7 deletions backend/app/controllers/sonolus/levels_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,8 @@ def list
if params[:q_author].present?
authors =
params[:q_author].split.map do |author|
user =
if author.start_with?("x")
User.find_by(handle: author[1..])
else
User.find_by(handle: author)
end
user = User.find_by(sonolus_handle: author)
user = User.find_by(handle: author) if user.nil?
user&.id
end
if authors.any?(nil)
Expand Down Expand Up @@ -182,7 +178,10 @@ def list

def test_list
require_login!
params.permit(:page, *(self.class.test_search_options.map { |o| o[:query] }))
params.permit(
:page,
*(self.class.test_search_options.map { |o| o[:query] })
)

charts = Chart.where(author_id: current_user.id)

Expand Down
4 changes: 2 additions & 2 deletions backend/app/controllers/sonolus_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ class SonolusController < ApplicationController
self.session_data = { user: user_data[:userProfile] }
user_profile = user_data[:userProfile]
table_contents = {
handle: user_profile[:handle],
sonolus_handle: user_profile[:handle],
name: user_profile[:name],
about_me: user_profile[:aboutMe],
fg_color: user_profile[:avatarForegroundColor],
bg_color: user_profile[:avatarBackgroundColor]
}

user =
if (u = User.find_by(handle: user_profile[:handle]))
if (u = User.find_by(sonolus_handle: user_profile[:handle]))
if table_contents.each_pair.any? { |k, v| u[k] != v }
logger.info "User #{u.handle} updated, updating table"
u.update!(table_contents)
Expand Down
10 changes: 5 additions & 5 deletions backend/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ class User < ApplicationRecord
class_name: "User",
inverse_of: :user

def display_handle
owner_id ? "x#{handle}" : handle
end

def to_frontend()
def to_frontend
{
handle: owner_id ? "x#{handle}" : handle,
name:,
Expand All @@ -28,4 +24,8 @@ def to_frontend()
chartCount: charts_count
}
end

def admin?
ENV["ADMIN_HANDLE"] == sonolus_handle
end
end
4 changes: 4 additions & 0 deletions backend/config/environments/production.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# frozen_string_literal: true
require "active_support/core_ext/integer/time"

unless ENV["ADMIN_HANDLE"]
raise "ADMIN_HANDLE is not set! Please set it in .env.prod"
end

Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.

Expand Down
17 changes: 17 additions & 0 deletions backend/db/migrate/20230524140147_add_sonolus_handle_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class AddSonolusHandleToUsers < ActiveRecord::Migration[7.0]
def up
add_column :users, :sonolus_handle, :string, unique: true, index: true
User.find_each do |user|
if user.owner_id.present?
user.update(handle: "x" + user.handle, sonolus_handle: "x" + user.handle)
else
user.update(sonolus_handle: user.handle)
end
end
change_column :users, :sonolus_handle, :string, null: false
end

def down
remove_column :users, :sonolus_handle
end
end
25 changes: 12 additions & 13 deletions backend/db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions backend/db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
# Character.create(name: "Luke", movie: movies.first)
user =
User.create!(
handle: 1073,
handle: "1073",
sonolus_handle: "1073",
name: "Nanashi.",
about_me: "I'm a admin.",
fg_color: "#fff",
bg_color: "#48b0d5"
)
user.create_user!(
handle: 740,
handle: "x740",
sonolus_handle: "x740",
name: "Nanatsuki Kuten",
about_me: "Alt account of Nanashi.",
fg_color: "#48b0d5",
Expand Down