diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2019-09-14 00:06:25 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2019-09-14 00:06:25 +0000 |
commit | a93dfc1b7e55b118b1cf4a67afeb46556292914c (patch) | |
tree | 65b874b7940d0d05c4ebedaef43b8a1009362651 /app/services | |
parent | 188a57f93bba5953800de490fcc6246966a073fd (diff) | |
download | gitlab-ce-a93dfc1b7e55b118b1cf4a67afeb46556292914c.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/services')
-rw-r--r-- | app/services/issues/zoom_link_service.rb | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/app/services/issues/zoom_link_service.rb b/app/services/issues/zoom_link_service.rb new file mode 100644 index 00000000000..a061ab22875 --- /dev/null +++ b/app/services/issues/zoom_link_service.rb @@ -0,0 +1,90 @@ +# frozen_string_literal: true + +module Issues + class ZoomLinkService < Issues::BaseService + def initialize(issue, user) + super(issue.project, user) + + @issue = issue + end + + def add_link(link) + if can_add_link? && (link = parse_link(link)) + success(_('Zoom meeting added'), append_to_description(link)) + else + error(_('Failed to add a Zoom meeting')) + end + end + + def can_add_link? + available? && !link_in_issue_description? + end + + def remove_link + if can_remove_link? + success(_('Zoom meeting removed'), remove_from_description) + else + error(_('Failed to remove a Zoom meeting')) + end + end + + def can_remove_link? + available? && link_in_issue_description? + end + + def parse_link(link) + Gitlab::ZoomLinkExtractor.new(link).links.last + end + + private + + attr_reader :issue + + def issue_description + issue.description || '' + end + + def success(message, description) + ServiceResponse + .success(message: message, payload: { description: description }) + end + + def error(message) + ServiceResponse.error(message: message) + end + + def append_to_description(link) + "#{issue_description}\n\n#{link}" + end + + def remove_from_description + link = parse_link(issue_description) + return issue_description unless link + + issue_description.delete_suffix(link).rstrip + end + + def link_in_issue_description? + link = extract_link_from_issue_description + return unless link + + Gitlab::ZoomLinkExtractor.new(link).match? + end + + def extract_link_from_issue_description + issue_description[/(\S+)\z/, 1] + end + + def available? + feature_enabled? && can? + end + + def feature_enabled? + Feature.enabled?(:issue_zoom_integration, project) + end + + def can? + current_user.can?(:update_issue, project) + end + end +end |