2019-09-13 20:06:25 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Issues
|
|
|
|
class ZoomLinkService < Issues::BaseService
|
|
|
|
def initialize(issue, user)
|
|
|
|
super(issue.project, user)
|
|
|
|
|
|
|
|
@issue = issue
|
2019-10-28 14:06:15 -04:00
|
|
|
@added_meeting = ZoomMeeting.canonical_meeting(@issue)
|
2019-09-13 20:06:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_link(link)
|
|
|
|
if can_add_link? && (link = parse_link(link))
|
2019-10-28 14:06:15 -04:00
|
|
|
begin
|
|
|
|
add_zoom_meeting(link)
|
|
|
|
rescue ActiveRecord::RecordNotUnique
|
2019-12-12 13:07:34 -05:00
|
|
|
error(message: _('Failed to add a Zoom meeting'))
|
2019-10-28 14:06:15 -04:00
|
|
|
end
|
2019-09-13 20:06:25 -04:00
|
|
|
else
|
2019-12-12 13:07:34 -05:00
|
|
|
error(message: _('Failed to add a Zoom meeting'))
|
2019-09-13 20:06:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_link
|
|
|
|
if can_remove_link?
|
2019-10-28 14:06:15 -04:00
|
|
|
remove_zoom_meeting
|
2019-12-12 13:07:34 -05:00
|
|
|
success(message: _('Zoom meeting removed'))
|
2019-09-13 20:06:25 -04:00
|
|
|
else
|
2019-12-12 13:07:34 -05:00
|
|
|
error(message: _('Failed to remove a Zoom meeting'))
|
2019-09-13 20:06:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-28 14:06:15 -04:00
|
|
|
def can_add_link?
|
2019-12-12 13:07:34 -05:00
|
|
|
can_change_link? && !@added_meeting
|
2019-10-28 14:06:15 -04:00
|
|
|
end
|
|
|
|
|
2019-09-13 20:06:25 -04:00
|
|
|
def can_remove_link?
|
2019-12-12 13:07:34 -05:00
|
|
|
can_change_link? && @issue.persisted? && !!@added_meeting
|
2019-09-13 20:06:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def parse_link(link)
|
|
|
|
Gitlab::ZoomLinkExtractor.new(link).links.last
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :issue
|
|
|
|
|
2019-10-17 20:07:45 -04:00
|
|
|
def track_meeting_added_event
|
|
|
|
::Gitlab::Tracking.event('IncidentManagement::ZoomIntegration', 'add_zoom_meeting', label: 'Issue ID', value: issue.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def track_meeting_removed_event
|
|
|
|
::Gitlab::Tracking.event('IncidentManagement::ZoomIntegration', 'remove_zoom_meeting', label: 'Issue ID', value: issue.id)
|
|
|
|
end
|
|
|
|
|
2019-10-28 14:06:15 -04:00
|
|
|
def add_zoom_meeting(link)
|
2019-12-12 13:07:34 -05:00
|
|
|
zoom_meeting = new_zoom_meeting(link)
|
|
|
|
response =
|
|
|
|
if @issue.persisted?
|
|
|
|
# Save the meeting directly since we only want to update one meeting, not all
|
|
|
|
zoom_meeting.save
|
2020-09-10 17:08:28 -04:00
|
|
|
track_incident_action(current_user, issue, :incident_zoom_meeting)
|
2019-12-12 13:07:34 -05:00
|
|
|
success(message: _('Zoom meeting added'))
|
|
|
|
else
|
|
|
|
success(message: _('Zoom meeting added'), payload: { zoom_meetings: [zoom_meeting] })
|
|
|
|
end
|
|
|
|
|
|
|
|
track_meeting_added_event
|
|
|
|
SystemNoteService.zoom_link_added(@issue, @project, current_user)
|
|
|
|
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
def new_zoom_meeting(link)
|
|
|
|
ZoomMeeting.new(
|
2019-10-28 14:06:15 -04:00
|
|
|
issue: @issue,
|
2019-12-12 13:07:34 -05:00
|
|
|
project: @project,
|
2019-10-28 14:06:15 -04:00
|
|
|
issue_status: :added,
|
|
|
|
url: link
|
|
|
|
)
|
2019-09-13 20:06:25 -04:00
|
|
|
end
|
|
|
|
|
2019-10-28 14:06:15 -04:00
|
|
|
def remove_zoom_meeting
|
|
|
|
@added_meeting.update(issue_status: :removed)
|
|
|
|
track_meeting_removed_event
|
|
|
|
SystemNoteService.zoom_link_removed(@issue, @project, current_user)
|
2019-09-13 20:06:25 -04:00
|
|
|
end
|
|
|
|
|
2019-12-12 13:07:34 -05:00
|
|
|
def success(message:, payload: nil)
|
|
|
|
ServiceResponse.success(message: message, payload: payload)
|
2019-09-13 20:06:25 -04:00
|
|
|
end
|
|
|
|
|
2019-12-12 13:07:34 -05:00
|
|
|
def error(message:)
|
2019-10-28 14:06:15 -04:00
|
|
|
ServiceResponse.error(message: message)
|
2019-09-13 20:06:25 -04:00
|
|
|
end
|
|
|
|
|
2019-12-12 13:07:34 -05:00
|
|
|
def can_change_link?
|
|
|
|
if @issue.persisted?
|
|
|
|
can?(current_user, :update_issue, @project)
|
|
|
|
else
|
|
|
|
can?(current_user, :create_issue, @project)
|
|
|
|
end
|
2019-09-13 20:06:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|