Commit 4c575b72 authored by Marin Jankovski's avatar Marin Jankovski

Move checking of recepients to a service.

parent 8f0051e2
...@@ -124,18 +124,12 @@ class ProjectsController < ApplicationController ...@@ -124,18 +124,12 @@ class ProjectsController < ApplicationController
def autocomplete_sources def autocomplete_sources
note_type = params['type'] note_type = params['type']
note_id = params['type_id'] note_id = params['type_id']
participating = if note_type && note_id participants = ::Projects::ParticipantsService.new(@project).execute(note_type, note_id)
participants_in(note_type, note_id)
else
[]
end
team_members = sorted(@project.team.members)
participants = [{ username: "all", name: "Project and Group Members" }] + team_members + participating
@suggestions = { @suggestions = {
emojis: Emoji.names.map { |e| { name: e, path: view_context.image_url("emoji/#{e}.png") } }, emojis: Emoji.names.map { |e| { name: e, path: view_context.image_url("emoji/#{e}.png") } },
issues: @project.issues.select([:iid, :title, :description]), issues: @project.issues.select([:iid, :title, :description]),
mergerequests: @project.merge_requests.select([:iid, :title, :description]), mergerequests: @project.merge_requests.select([:iid, :title, :description]),
members: participants.uniq members: participants
} }
respond_to do |format| respond_to do |format|
...@@ -191,25 +185,4 @@ class ProjectsController < ApplicationController ...@@ -191,25 +185,4 @@ class ProjectsController < ApplicationController
def user_layout def user_layout
current_user ? "projects" : "public_projects" current_user ? "projects" : "public_projects"
end end
def participants_in(type, id)
users = case type
when "Issue"
issue = @project.issues.find_by_iid(id)
issue ? issue.participants : []
when "MergeRequest"
merge_request = @project.merge_requests.find_by_iid(id)
merge_request ? merge_request.participants : []
when "Commit"
author_ids = Note.for_commit_id(id).pluck(:author_id).uniq
User.where(id: author_ids)
else
[]
end
sorted(users)
end
def sorted(users)
users.uniq.to_a.compact.sort_by(&:username).map { |user| { username: user.username, name: user.name } }
end
end end
...@@ -55,7 +55,7 @@ module Mentionable ...@@ -55,7 +55,7 @@ module Mentionable
if has_project if has_project
id = project.team.members.find_by(username: identifier).try(:id) id = project.team.members.find_by(username: identifier).try(:id)
else else
id = User.where(username: identifier).pluck(:id).first id = User.find_by(username: identifier).try(:id)
end end
users << User.find(id) unless id.blank? users << User.find(id) unless id.blank?
end end
......
module Projects
class ParticipantsService < BaseService
def initialize(project)
@project = project
end
def execute(note_type, note_id)
participating = if note_type && note_id
participants_in(note_type, note_id)
else
[]
end
team_members = sorted(@project.team.members)
participants = all_members + team_members + participating
participants.uniq
end
def participants_in(type, id)
users = case type
when "Issue"
issue = @project.issues.find_by_iid(id)
issue ? issue.participants : []
when "MergeRequest"
merge_request = @project.merge_requests.find_by_iid(id)
merge_request ? merge_request.participants : []
when "Commit"
author_ids = Note.for_commit_id(id).pluck(:author_id).uniq
User.where(id: author_ids)
else
[]
end
sorted(users)
end
def sorted(users)
users.uniq.to_a.compact.sort_by(&:username).map { |user| { username: user.username, name: user.name } }
end
def all_members
[{ username: "all", name: "Project and Group Members" }]
end
end
end
...@@ -169,15 +169,13 @@ module Gitlab ...@@ -169,15 +169,13 @@ module Gitlab
end end
def reference_user(identifier, project = @project) def reference_user(identifier, project = @project)
if identifier == "all"
options = html_options.merge( options = html_options.merge(
class: "gfm gfm-team_member #{html_options[:class]}" class: "gfm gfm-team_member #{html_options[:class]}"
) )
if identifier == "all"
link_to("@all", project_url(project), options) link_to("@all", project_url(project), options)
elsif user = User.find_by(username: identifier) elsif user = User.find_by(username: identifier)
options = html_options.merge(
class: "gfm gfm-team_member #{html_options[:class]}"
)
link_to("@#{identifier}", user_url(identifier), options) link_to("@#{identifier}", user_url(identifier), options)
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment