debian-mirror-gitlab/app/services/wiki_pages/base_service.rb

61 lines
1.7 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2016-06-02 11:05:42 +05:30
module WikiPages
2020-04-22 19:07:51 +05:30
# There are 3 notions of 'action' that inheriting classes must implement:
#
# - external_action: the action we report to external clients with webhooks
# - usage_counter_action: the action that we count in out internal counters
# - event_action: what we record as the value of `Event#action`
2020-05-24 23:13:21 +05:30
class BaseService < ::BaseContainerService
2016-06-02 11:05:42 +05:30
private
2020-04-22 19:07:51 +05:30
def execute_hooks(page)
page_data = payload(page)
2020-05-24 23:13:21 +05:30
container.execute_hooks(page_data, :wiki_page_hooks)
container.execute_services(page_data, :wiki_page_hooks)
2020-04-22 19:07:51 +05:30
increment_usage
create_wiki_event(page)
end
# Passed to web-hooks, and send to external consumers.
def external_action
raise NotImplementedError
end
# Passed to the WikiPageCounter to count events.
# Must be one of WikiPageCounter::KNOWN_EVENTS
def usage_counter_action
raise NotImplementedError
end
# Used to create `Event` records.
# Must be a valid value for `Event#action`
def event_action
raise NotImplementedError
end
def payload(page)
Gitlab::DataBuilder::WikiPage.build(page, current_user, external_action)
2019-10-12 21:52:04 +05:30
end
# This method throws an error if the action is an unanticipated value.
2020-04-22 19:07:51 +05:30
def increment_usage
Gitlab::UsageDataCounters::WikiPageCounter.count(usage_counter_action)
end
def create_wiki_event(page)
return unless ::Feature.enabled?(:wiki_events)
2020-05-24 23:13:21 +05:30
response = WikiPages::EventCreateService.new(current_user).execute(slug_for_page(page), page, event_action)
2020-04-22 19:07:51 +05:30
2020-05-24 23:13:21 +05:30
log_error(response.message) if response.error?
2020-04-22 19:07:51 +05:30
end
def slug_for_page(page)
page.slug
2016-06-02 11:05:42 +05:30
end
end
end
2019-12-04 20:38:33 +05:30
WikiPages::BaseService.prepend_if_ee('EE::WikiPages::BaseService')