Welcome to mirror list, hosted at ThFree Co, Russian Federation.

zoom_link_service.rb « issues « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6cc1cdeeff9f6dde0f196866f3cfc18ae7f98e1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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?
      can? && !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?
      can? && 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 can?
      current_user.can?(:update_issue, project)
    end
  end
end