-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(helpscout): delete old conversations
- Loading branch information
Showing
4 changed files
with
378 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
app/tasks/maintenance/helpscout_delete_old_conversations_task.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# frozen_string_literal: true | ||
|
||
module Maintenance | ||
class HelpscoutDeleteOldConversationsTask < MaintenanceTasks::Task | ||
# Delete Helpscout conversations not modified in the last 2 years, given a status. | ||
# In order to delete all conversations, this task must be invoked 4 times | ||
# for the 4 status: active, closed, spam, pending. | ||
# Respects the Helpscount API rate limit (200 calls per minute). | ||
|
||
attribute :status, :string # active, closed, spam, or pending | ||
validates :status, presence: true | ||
|
||
MODIFIED_BEFORE = 2.years.freeze | ||
|
||
throttle_on do | ||
limit = Rails.cache.read(Helpscout::API::RATELIMIT_KEY) | ||
limit.present? && limit == 0 | ||
end | ||
|
||
def count | ||
_conversations, pagination = api.list_old_conversations(status, modified_before) | ||
|
||
pagination[:totalElements] | ||
end | ||
|
||
# Because conversations are deleted progressively, | ||
# ignore cursor and always pick the first page | ||
def enumerator_builder(cursor:) | ||
Enumerator.new do |yielder| | ||
loop do | ||
conversations, pagination = api.list_old_conversations(status, modified_before) | ||
conversations.each do |conversation| | ||
yielder.yield(conversation[:id], nil) # don't care about cursor parameter | ||
end | ||
|
||
# "number" is the current page (always 1 in our case) | ||
# iterate until there are no remaining pages | ||
break if pagination[:totalPages] == pagination[:number] | ||
end | ||
end | ||
end | ||
|
||
def process(conversation_id) | ||
@api.delete_conversation(conversation_id) | ||
end | ||
|
||
private | ||
|
||
def api | ||
@api ||= Helpscout::API.new | ||
end | ||
|
||
def modified_before | ||
MODIFIED_BEFORE.ago.utc.beginning_of_day | ||
end | ||
end | ||
end |
Oops, something went wrong.