Skip to content

Commit

Permalink
Merge branch 'main' into style/identidade-visual
Browse files Browse the repository at this point in the history
  • Loading branch information
GyodaiDDA authored Feb 16, 2024
2 parents 89869dd + 0f589df commit 98b6351
Show file tree
Hide file tree
Showing 77 changed files with 641 additions and 268 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ gem 'devise'

gem 'faker'
gem 'faraday'
gem 'friendly_id', '~> 5.5.0'
gem 'image_processing', '>= 1.2'
gem 'jbuilder'
gem 'jsbundling-rails'
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ GEM
webrick (~> 1.7)
websocket-driver (>= 0.6, < 0.8)
ffi (1.16.3)
friendly_id (5.5.1)
activerecord (>= 4.0.0)
globalid (1.2.1)
activesupport (>= 6.1)
i18n (1.14.1)
Expand Down Expand Up @@ -339,6 +341,7 @@ DEPENDENCIES
factory_bot_rails
faker
faraday
friendly_id (~> 5.5.0)
image_processing (>= 1.2)
jbuilder
jsbundling-rails
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ O Portfoliorrr é uma rede social com funcionalidades de portfólio para pessoas
- Rode o comando `bin/setup` e aguarde sua conclusão;
- Rode o comando `yarn install` (necessário ter `node` instalado em sua máquina);

## Populando o banco de dados

- O seed comum, para desenvolvimento, pode ser feito normalmente com `rails db:seed`
- Para apresentações e testes de front-end, é possível fazer um superseed, utilizando `rails db:seed:superseed`
- O superseed utiliza as gems Faker e FactoryBot, que são instaladas durante a configuração (ver item anterior)

## Como visualizar a aplicação no navegador

- Siga as instruções de configuração da aplicação
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/api/v1/profiles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ module V1
class ProfilesController < ApiController
def index
if params[:search].blank?
profiles = Profile.active.open_to_work
profiles = Profile.active.open_to_work.order_by_premium
profiles = profiles.map { |profile| format_profile(profile) }
else
profiles = Profile.active.open_to_work.get_profile_job_categories_json(params[:search])
profiles = Profile.active.open_to_work.order_by_premium.get_profile_job_categories_json(params[:search])
end
render status: :ok, json: { data: profiles }
end
Expand Down
11 changes: 5 additions & 6 deletions app/controllers/api/v1/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Api
module V1
class ProjectsController < ApiController
def index
response = Faraday.get('http://localhost:3000/api/v1/projects')
response = ProjectsService::ColaBoraApiGetProjects.send
if response.status == 200
projects = JSON.parse(response.body)
render status: :ok, json: projects.as_json
Expand All @@ -13,9 +13,9 @@ def index
end

def request_invitation
data = proposal_params.as_json
connection = Faraday.new(url: 'http://localhost:3000', params: data)
response = connection.post('api/v1/proposals')
invitation_request_id = proposal_params.fetch('invitation_request_id').to_i
invitation_request = InvitationRequest.find(invitation_request_id)
response = InvitationRequestService::ColaBoraInvitationRequestPost.send(invitation_request)

if response.status == 201
proposal = JSON.parse(response.body)
Expand All @@ -29,8 +29,7 @@ def request_invitation
private

def proposal_params
proposal_attributes = %i[invitation_request_id email message profile_id project_id]
params.require(:data).permit(proposal: proposal_attributes)
params.permit(:invitation_request_id)
end
end
end
Expand Down
14 changes: 10 additions & 4 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
class CommentsController < ApplicationController
before_action :authenticate_user!
before_action :set_post

def create
post = Post.find(params[:post_id])
comments_params = params.require(:comment).permit(:message)
comments_params[:user_id] = current_user.id
comment = post.comments.build(comments_params)
comment = @post.comments.build(comments_params)
if comment.save
redirect_to post, notice: t('.success')
redirect_to @post, notice: t('.success')
else
redirect_to post, alert: t('.error')
redirect_to @post, alert: t('.error')
end
end
end

private

def set_post
@post = Post.friendly.find(params[:post_id])
end
2 changes: 1 addition & 1 deletion app/controllers/connections_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ def set_connection_and_profile
end

def set_profile
@profile = Profile.find params[:profile_id]
@profile = Profile.friendly.find params[:profile_id]
end
end
2 changes: 1 addition & 1 deletion app/controllers/invitations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def show; end
private

def set_invitation
@invitation = Invitation.find(params[:id])
@invitation = Invitation.friendly.find(params[:id])
end

def authorize!
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/posts/likes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class LikesController < LikesController
private

def set_likeable
@likeable = Post.find(params[:post_id])
@likeable = Post.friendly.find(params[:post_id])
@post = @likeable
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def post_params
end

def set_post
@post = Post.find(params[:id])
@post = Post.friendly.find(params[:id])
end

def authorize!
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/profiles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def redirect_unauthorized_access
end

def set_profile_and_posts
@profile = Profile.find(params[:id])
@profile = Profile.friendly.find(params[:id])
@posts = current_user == @profile.user ? @profile.posts : @profile.posts.published
end

Expand Down
1 change: 1 addition & 0 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def index
@invitation_request = current_user.invitation_requests.build
@invitation_requests = current_user.invitation_requests.pluck(:project_id).to_json
@invitation_requests_projects_ids = current_user.invitation_requests.pluck(:project_id)
@projects_url = Rails.configuration.portfoliorrr_api_v1.projects_url
@free_user = current_user.subscription.inactive?
end

Expand Down
9 changes: 5 additions & 4 deletions app/controllers/reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ def remove_profile

def set_reportable_for_new
reportable_id = params[:reportable]
@reportable = Post.find(reportable_id) if params[:reportable_type] == 'Post'
@reportable = Profile.find(reportable_id) if params[:reportable_type] == 'Profile'
@reportable = Comment.find(reportable_id) if params[:reportable_type] == 'Comment'
@reportable = Reply.find(reportable_id) if params[:reportable_type] == 'Reply'
reportable_type = params[:reportable_type]
@reportable = Post.friendly.find(reportable_id) if reportable_type == 'Post'
@reportable = Profile.friendly.find(reportable_id) if reportable_type == 'Profile'
@reportable = Comment.find(reportable_id) if reportable_type == 'Comment'
@reportable = Reply.find(reportable_id) if reportable_type == 'Reply'
end

def set_reportable_for_create
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/settings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def open_to_work
private

def redirect_unauthorized_access
@profile = Profile.find(params[:profile_id])
@profile = Profile.friendly.find(params[:profile_id])
return if current_user == @profile.user

redirect_to root_path, alert: t('alerts.unauthorized')
Expand Down
3 changes: 2 additions & 1 deletion app/javascript/components/projects_vue.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default {
invitationRequestsProjectsIds: window.invitationRequestsProjectsIds,
freeUser: window.freeUser,
errorMsg: false,
portfoliorrrProjectsApiUrl: window.portfoliorrrProjectsApiUrl,
}
},
computed:{
Expand Down Expand Up @@ -47,7 +48,7 @@ export default {
async created() {
if (!freeUser) {
try {
let response = await fetch('/api/v1/projects', { signal });
let response = await fetch(this.portfoliorrrProjectsApiUrl, { signal });
if (response.ok) {
let data = await response.json();
if (!data.message) {
Expand Down
4 changes: 3 additions & 1 deletion app/jobs/decline_invitation_job.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
class DeclineInvitationJob < ApplicationJob
PORTFOLIORRR_BASE_URL = Rails.configuration.portfoliorrr_api_v1.base_url
PORTFOLIORRR_INVITATION_URL = Rails.configuration.portfoliorrr_api_v1.invitations_url
retry_on Faraday::ConnectionFailed, Faraday::ServerError, wait: :polynomially_longer, attempts: 5 do |job|
job.arguments.first.pending!
end

def perform(invitation)
url = "http://localhost:4000/api/v1/invitations/#{invitation.colabora_invitation_id}"
url = "#{PORTFOLIORRR_BASE_URL}#{PORTFOLIORRR_INVITATION_URL}#{invitation.colabora_invitation_id}"
Faraday.new { |faraday| faraday.response :raise_error }.patch(url)
invitation.declined!
rescue Faraday::ResourceNotFound, Faraday::ConflictError
Expand Down
6 changes: 4 additions & 2 deletions app/jobs/request_invitation_job.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
class RequestInvitationJob < ApplicationJob
PORTFOLIORRR_BASE_URL = Rails.configuration.portfoliorrr_api_v1.base_url
PORTFOLIORRRR_REQUEST_INVITATION_URL = Rails.configuration.portfoliorrr_api_v1.request_invitation_url
queue_as :default
retry_on Exceptions::PortfoliorrrAPIOffline, wait: 1.hour, attempts: :unlimited
retry_on Exceptions::ColaBoraAPIOffline, wait: 1.hour, attempts: 5 do |job, _error|
job.arguments.first[:invitation_request].aborted!
end

def perform(invitation_request:)
data = invitation_request.create_json_for_proposal_request
response = Faraday.new(url: 'http://localhost:4000', params: data).get('/api/v1/projects/request_invitation')
data = { invitation_request_id: invitation_request.id }.as_json
response = Faraday.new(url: PORTFOLIORRR_BASE_URL, params: data).get(PORTFOLIORRRR_REQUEST_INVITATION_URL)
return raise Exceptions::PortfoliorrrAPIOffline if response.status == :internal_server_error

invitation_request.process_colabora_api_response(response)
Expand Down
7 changes: 3 additions & 4 deletions app/mailers/invitations_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
class InvitationsMailer < ApplicationMailer
def received_invitation
profile = Profile.find(params[:profile_id])
project_title = params[:project_title]
mail(subject: t('.subject'), to: profile.user.email,
body: t('.body', title: project_title))
@profile = Profile.friendly.find(params[:profile_id])
@project_title = params[:project_title]
mail(subject: t('.subject'), to: @profile.user.email)
end
end
3 changes: 3 additions & 0 deletions app/models/invitation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class Invitation < ApplicationRecord
after_create :create_notification
after_create :validate_and_approve_pending_request

extend FriendlyId
friendly_id :project_title, use: :slugged

def set_status
self.status = 'pending'
end
Expand Down
8 changes: 0 additions & 8 deletions app/models/invitation_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ def process_colabora_api_response(response)
end
end

def create_json_for_proposal_request
{ data: { proposal: { invitation_request_id: id,
project_id:,
profile_id: profile.id,
email: profile.email,
message: } } }.as_json
end

private

def json_treated_response(response)
Expand Down
3 changes: 3 additions & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class Post < ApplicationRecord

has_rich_text :content

extend FriendlyId
friendly_id :title, use: :slugged

def self.get_sample(amount)
published.sample amount
end
Expand Down
56 changes: 26 additions & 30 deletions app/models/profile.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
class Profile < ApplicationRecord
belongs_to :user
has_one_attached :photo
has_one :personal_info, dependent: :destroy
has_many :professional_infos, dependent: :destroy
has_many :education_infos, dependent: :destroy
has_many :profile_job_categories, dependent: :destroy

has_many :followers, class_name: 'Connection', foreign_key: :followed_profile_id, dependent: :destroy,
inverse_of: :follower

has_many :followed_profiles, class_name: 'Connection', foreign_key: :follower_id,
dependent: :destroy, inverse_of: :followed_profile

has_many :connections, foreign_key: :followed_profile_id, dependent: :destroy, inverse_of: :followed_profile

has_many :job_categories, through: :profile_job_categories
has_many :invitation_requests, dependent: :destroy

has_one_attached :photo
has_many :invitations, dependent: :destroy
has_many :posts, through: :user
has_many :notifications, dependent: :destroy

has_many :invitation_requests, dependent: :destroy
has_many :posts, through: :user
has_many :job_categories, through: :profile_job_categories
has_many :reports_submitted, class_name: 'Report', dependent: :destroy

has_many :reports_received, class_name: 'Report', as: :reportable, dependent: :destroy

has_many :connections, foreign_key: :followed_profile_id, dependent: :destroy, inverse_of: :followed_profile
has_many :followers, class_name: 'Connection', foreign_key: :followed_profile_id, dependent: :destroy,
inverse_of: :follower
has_many :followed_profiles, class_name: 'Connection', foreign_key: :follower_id, dependent: :destroy,
inverse_of: :followed_profile
accepts_nested_attributes_for :personal_info
accepts_nested_attributes_for :professional_infos
accepts_nested_attributes_for :education_infos
Expand All @@ -38,24 +30,33 @@ class Profile < ApplicationRecord
enum privacy: { private_profile: 0, public_profile: 10 }
enum status: { inactive: 0, active: 5 }

extend FriendlyId
friendly_id :profile_permalink, use: :slugged

delegate :full_name, :email, to: :user

def profile_permalink
user.full_name.to_s
end

def self.order_by_premium
joins(user: :subscription).order('subscriptions.status DESC, users.full_name ASC')
end

def self.advanced_search(search_query)
left_outer_joins(:job_categories, :personal_info, :user).where(
'job_categories.name LIKE :term OR
personal_infos.city LIKE :term OR
users.full_name LIKE :term OR users.search_name LIKE :term',
'job_categories.name LIKE :term OR personal_infos.city LIKE :term OR users.full_name LIKE :term
OR users.search_name LIKE :term',
{ term: "%#{sanitize_sql_like(search_query)}%" }
).public_profile.active.uniq
end

def self.get_profile_job_categories_json(query)
profiles = search_by_job_categories(query)
profiles_json = profiles.map do |profile|
profiles.map do |profile|
{ user_id: profile.user_id, full_name: profile.full_name,
job_categories: ProfileJobCategory.generate_profile_job_categories_json(profile.id) }
end
profiles_json.as_json
end.as_json
end

def self.search_by_job_categories(query)
Expand Down Expand Up @@ -92,20 +93,15 @@ def self.most_followed(limit)
def inactive!
super
user.update(old_name: user.full_name, full_name: 'Perfil Desativado')
user.posts.each do |post|
post.update(old_status: post.status)
end

user.posts.each { |post| post.update(old_status: post.status) }
user.posts.each(&:archived!)
Connection.where(follower: self).or(Connection.where(followed_profile: self)).find_each(&:inactive!)
end

def active!
super
user.update(full_name: user.old_name)
user.posts.each do |post|
post.update(status: post.old_status)
end
user.posts.each { |post| post.update(status: post.old_status) }
Connection.where(follower: self).or(Connection.where(followed_profile: self)).find_each(&:active!)
end

Expand Down
Loading

0 comments on commit 98b6351

Please sign in to comment.