Skip to content

Commit

Permalink
Merge pull request #3 from 0x5c3p73r/feat/subscribe_control
Browse files Browse the repository at this point in the history
feat: add subscribe enable/disable control
  • Loading branch information
0x5c3p73r committed May 14, 2024
2 parents f4b5bb6 + 631bbd0 commit 86ff060
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 10 deletions.
30 changes: 27 additions & 3 deletions app/controllers/subscribes_controller.rb
@@ -1,6 +1,6 @@
class SubscribesController < ApplicationController
before_action :set_coach, only: %i[ edit update destroy ]
before_action :set_subscribe, only: %i[ show edit update destroy ]
before_action :set_coach, only: %i[ edit update destroy enable disable ]
before_action :set_subscribe, only: %i[ show edit update destroy enable disable ]
before_action :set_ticket, only: %i[ edit update destroy ]

# GET /coach/:coach_id/subscribes/:id/edit
Expand Down Expand Up @@ -31,6 +31,30 @@ def destroy
end
end

def enable
respond_to do |format|
if @subscribe.update(disabled: false)
format.html { redirect_to edit_coach_url(@coach, locale: I18n.locale), notice: "Subscribe was successfully updated." }
format.json { render json: @subscribe, status: :created }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @subscribe.errors, status: :unprocessable_entity }
end
end
end

def disable
respond_to do |format|
if @subscribe.update(disabled: true)
format.html { redirect_to edit_coach_url(@coach, locale: I18n.locale), notice: "Subscribe was successfully updated." }
format.json { render json: @subscribe, status: :created }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @subscribe.errors, status: :unprocessable_entity }
end
end
end

private

def set_coach
Expand All @@ -39,7 +63,7 @@ def set_coach

# Use callbacks to share common setup or constraints between actions.
def set_subscribe
@subscribe = Subscribe.find(params[:id])
@subscribe = @coach.subscribes.find(params[:id])
end

# Only allow a list of trusted parameters through.
Expand Down
6 changes: 5 additions & 1 deletion app/models/coach.rb
Expand Up @@ -107,6 +107,10 @@ def encrypt_matches?(private_key)
_encrypt(private_key) == encrypt_value
end

def subscribe_url?
!subscribes.where(disabled: false).count.zero?
end

def subconverter_url(overwrite_params = {})
new_target = overwrite_params.delete(:target) || target

Expand Down Expand Up @@ -147,7 +151,7 @@ def target_params(new_target)
end

def subscribes_urls
subscribes.select(:link).map(&:link).join("|")
subscribes.enabled.select(:link).map(&:link).join("|")
end

def generate_name
Expand Down
10 changes: 6 additions & 4 deletions app/models/subscribe.rb
Expand Up @@ -7,6 +7,8 @@ class Subscribe < ApplicationRecord
has_many :coach_subscribes
has_many :coaches, through: :coach_subscribes

scope :enabled, -> { where(disabled: false) }

validates :link, presence: true, url: { schemes: ['http', 'https', 'ss', 'ssr', 'vmess', 'trojan'] }

before_create :generate_name
Expand All @@ -18,10 +20,10 @@ def bandwidth_check
head_response = head(link)
if head_response.success? && (userinfo = head_response.headers['subscription-userinfo'])
upload, download, total, expired_time = userinfo.split('; ').map { |s| s.split('=')[-1].strip }
puts upload
puts download
puts total
puts expired_time
# puts upload
# puts download
# puts total
# puts expired_time

update(
upload_used: upload.to_i,
Expand Down
10 changes: 10 additions & 0 deletions app/views/coaches/_coach.html.erb
Expand Up @@ -11,9 +11,13 @@
<dl>
<div class="border-t border-gray-200 px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6 dark:border-gray-600">
<dt class="text-sm font-medium text-gray-900 dark:text-gray-400"><%= t('helpers.label.coach.permalink') %></dt>
<% if coach.subscribe_url? %>
<dd class="mt-1 text-sm sm:col-span-2 sm:mt-0 text-blue-600 hover:text-blue-500 dark:text-blue-400">
<%= link_to short_url(coach.id), short_url(coach.id), target: "_blank" %>
</dd>
<% else %>
<dd class="mt-1 text-sm sm:col-span-2 sm:mt-0 dark:text-gray-400"><%= short_url(coach.id) %></dd>
<% end %>
</div>

<div class="border-t border-gray-200 px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6 dark:border-gray-600">
Expand All @@ -39,6 +43,12 @@
<!--
<span class="ml-2 w-0 flex-1 truncate text-gray-400"><%= time_ago_in_words(subscribe.created_at) %> ago</span>
-->

</div>
<div class="ml-4 flex-shrink-0">
<span class="ml-2 w-0 flex-1 truncate font-medium text-gray-600 dark:text-gray-400">
<%= subscribe.disabled ? "Disabled" : "Enabled" %>
</span>
</div>
</li>
<% end %>
Expand Down
8 changes: 8 additions & 0 deletions app/views/coaches/_edit_form.html.erb
Expand Up @@ -36,6 +36,14 @@
<div class="ml-4 flex-shrink-0">
<%= link_to t("coaches.edit.edit"), edit_coach_subscribe_path(coach, subscribe, locale: I18n.locale), class: "font-medium text-blue-600 hover:text-blue-500 dark:text-blue-400" %>
</div>

<div class="ml-4 flex-shrink-0">
<% if subscribe.disabled %>
<%= link_to t("coaches.edit.enable"), enable_coach_subscribe_path(coach, subscribe, locale: I18n.locale), class: "font-medium text-blue-600 hover:text-blue-500 dark:text-blue-400" %>
<% else %>
<%= link_to t("coaches.edit.disable"), disable_coach_subscribe_path(coach, subscribe, locale: I18n.locale), class: "font-medium text-blue-600 hover:text-blue-500 dark:text-blue-400" %>
<% end %>
</div>
</li>
<% end %>
</ul>
Expand Down
7 changes: 6 additions & 1 deletion config/routes.rb
Expand Up @@ -14,7 +14,12 @@
get :verify_ticket
end

resources :subscribes, only: %i[ edit update destroy ]
resources :subscribes, only: %i[ edit update destroy ] do
member do
get :disable
get :enable
end
end
resources :configs, only: %i[ edit update destroy ]
resources :backends, only: %i[ edit update destroy ]
end
Expand Down
3 changes: 2 additions & 1 deletion db/schema.rb

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

0 comments on commit 86ff060

Please sign in to comment.