2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
class SnippetsController < Snippets::ApplicationController
|
2017-08-17 22:00:37 +05:30
|
|
|
include SnippetsActions
|
2018-03-17 18:26:18 +05:30
|
|
|
include PreviewMarkdown
|
2020-06-23 00:09:42 +05:30
|
|
|
include ToggleAwardEmoji
|
|
|
|
include SpammableActions
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
before_action :snippet, only: [:show, :edit, :destroy, :update, :raw, :toggle_award_emoji, :mark_as_spam]
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
before_action :authorize_create_snippet!, only: [:new, :create]
|
2017-08-17 22:00:37 +05:30
|
|
|
before_action :authorize_read_snippet!, only: [:show, :raw]
|
2015-09-11 14:41:01 +05:30
|
|
|
before_action :authorize_update_snippet!, only: [:edit, :update]
|
|
|
|
before_action :authorize_admin_snippet!, only: [:destroy]
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
skip_before_action :authenticate_user!, only: [:index, :show, :raw]
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
layout 'snippets'
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
before_action do
|
|
|
|
push_frontend_feature_flag(:snippet_multiple_files, current_user)
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def index
|
2015-09-11 14:41:01 +05:30
|
|
|
if params[:username].present?
|
2018-12-13 13:39:08 +05:30
|
|
|
@user = UserFinder.new(params[:username]).find_by_username!
|
2015-09-11 14:41:01 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
@snippets = SnippetsFinder.new(current_user, author: @user, scope: params[:scope], sort: sort_param)
|
2019-12-04 20:38:33 +05:30
|
|
|
.execute
|
|
|
|
.page(params[:page])
|
|
|
|
.inc_author
|
|
|
|
|
|
|
|
return if redirect_out_of_range(@snippets)
|
|
|
|
|
|
|
|
@noteable_meta_data = noteable_meta_data(@snippets, 'Snippet')
|
2015-09-11 14:41:01 +05:30
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
render 'index'
|
2014-09-02 18:07:02 +05:30
|
|
|
else
|
2015-09-25 12:07:36 +05:30
|
|
|
redirect_to(current_user ? dashboard_snippets_path : explore_snippets_path)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
|
|
|
@snippet = PersonalSnippet.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2020-05-24 23:13:21 +05:30
|
|
|
create_params = snippet_params.merge(files: params.delete(:files))
|
2020-03-13 15:44:24 +05:30
|
|
|
service_response = Snippets::CreateService.new(nil, current_user, create_params).execute
|
|
|
|
@snippet = service_response.payload[:snippet]
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
if service_response.error? && @snippet.errors[:repository].present?
|
|
|
|
handle_repository_error(:new)
|
2020-04-08 14:13:33 +05:30
|
|
|
else
|
|
|
|
recaptcha_check_with_fallback { render :new }
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
alias_method :awardable, :snippet
|
2017-08-17 22:00:37 +05:30
|
|
|
alias_method :spammable, :snippet
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def spammable_path
|
|
|
|
snippet_path(@snippet)
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def snippet_params
|
2020-05-24 23:13:21 +05:30
|
|
|
params.require(:personal_snippet).permit(:title, :content, :file_name, :private, :visibility_level, :description).merge(spammable_params)
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|