mirror of
https://github.com/discourse/discourse.git
synced 2025-05-28 13:51:18 +08:00
DEV: Apply syntax_tree formatting to lib/*
This commit is contained in:
@ -8,61 +8,58 @@ module Imap
|
||||
# all UIDs in a thread must have the \\Inbox label removed.
|
||||
#
|
||||
class Gmail < Generic
|
||||
X_GM_LABELS = 'X-GM-LABELS'
|
||||
X_GM_THRID = 'X-GM-THRID'
|
||||
X_GM_LABELS = "X-GM-LABELS"
|
||||
X_GM_THRID = "X-GM-THRID"
|
||||
|
||||
def imap
|
||||
@imap ||= super.tap { |imap| apply_gmail_patch(imap) }
|
||||
end
|
||||
|
||||
def emails(uids, fields, opts = {})
|
||||
|
||||
# gmail has a special header for labels
|
||||
if fields.include?('LABELS')
|
||||
fields[fields.index('LABELS')] = X_GM_LABELS
|
||||
end
|
||||
fields[fields.index("LABELS")] = X_GM_LABELS if fields.include?("LABELS")
|
||||
|
||||
emails = super(uids, fields, opts)
|
||||
|
||||
emails.each do |email|
|
||||
email['LABELS'] = Array(email['LABELS'])
|
||||
email["LABELS"] = Array(email["LABELS"])
|
||||
|
||||
if email[X_GM_LABELS]
|
||||
email['LABELS'] << Array(email.delete(X_GM_LABELS))
|
||||
email['LABELS'].flatten!
|
||||
email["LABELS"] << Array(email.delete(X_GM_LABELS))
|
||||
email["LABELS"].flatten!
|
||||
end
|
||||
|
||||
email['LABELS'] << '\\Inbox' if @open_mailbox_name == 'INBOX'
|
||||
email["LABELS"] << '\\Inbox' if @open_mailbox_name == "INBOX"
|
||||
|
||||
email['LABELS'].uniq!
|
||||
email["LABELS"].uniq!
|
||||
end
|
||||
|
||||
emails
|
||||
end
|
||||
|
||||
def store(uid, attribute, old_set, new_set)
|
||||
attribute = X_GM_LABELS if attribute == 'LABELS'
|
||||
attribute = X_GM_LABELS if attribute == "LABELS"
|
||||
super(uid, attribute, old_set, new_set)
|
||||
end
|
||||
|
||||
def to_tag(label)
|
||||
# Label `\\Starred` is Gmail equivalent of :Flagged (both present)
|
||||
return 'starred' if label == :Flagged
|
||||
return if label == '[Gmail]/All Mail'
|
||||
return "starred" if label == :Flagged
|
||||
return if label == "[Gmail]/All Mail"
|
||||
|
||||
label = label.to_s.gsub('[Gmail]/', '')
|
||||
label = label.to_s.gsub("[Gmail]/", "")
|
||||
super(label)
|
||||
end
|
||||
|
||||
def tag_to_flag(tag)
|
||||
return :Flagged if tag == 'starred'
|
||||
return :Flagged if tag == "starred"
|
||||
|
||||
super(tag)
|
||||
end
|
||||
|
||||
def tag_to_label(tag)
|
||||
return '\\Important' if tag == 'important'
|
||||
return '\\Starred' if tag == 'starred'
|
||||
return '\\Important' if tag == "important"
|
||||
return '\\Starred' if tag == "starred"
|
||||
|
||||
super(tag)
|
||||
end
|
||||
@ -73,11 +70,14 @@ module Imap
|
||||
thread_id = thread_id_from_uid(uid)
|
||||
emails_to_archive = emails_in_thread(thread_id)
|
||||
emails_to_archive.each do |email|
|
||||
labels = email['LABELS']
|
||||
labels = email["LABELS"]
|
||||
new_labels = labels.reject { |l| l == "\\Inbox" }
|
||||
store(email["UID"], "LABELS", labels, new_labels)
|
||||
end
|
||||
ImapSyncLog.log("Thread ID #{thread_id} (UID #{uid}) archived in Gmail mailbox for #{@username}", :debug)
|
||||
ImapSyncLog.log(
|
||||
"Thread ID #{thread_id} (UID #{uid}) archived in Gmail mailbox for #{@username}",
|
||||
:debug,
|
||||
)
|
||||
end
|
||||
|
||||
# Though Gmail considers the email thread unarchived if the first email
|
||||
@ -87,36 +87,38 @@ module Imap
|
||||
thread_id = thread_id_from_uid(uid)
|
||||
emails_to_unarchive = emails_in_thread(thread_id)
|
||||
emails_to_unarchive.each do |email|
|
||||
labels = email['LABELS']
|
||||
labels = email["LABELS"]
|
||||
new_labels = labels.dup
|
||||
if !new_labels.include?("\\Inbox")
|
||||
new_labels << "\\Inbox"
|
||||
end
|
||||
new_labels << "\\Inbox" if !new_labels.include?("\\Inbox")
|
||||
store(email["UID"], "LABELS", labels, new_labels)
|
||||
end
|
||||
ImapSyncLog.log("Thread ID #{thread_id} (UID #{uid}) unarchived in Gmail mailbox for #{@username}", :debug)
|
||||
ImapSyncLog.log(
|
||||
"Thread ID #{thread_id} (UID #{uid}) unarchived in Gmail mailbox for #{@username}",
|
||||
:debug,
|
||||
)
|
||||
end
|
||||
|
||||
def thread_id_from_uid(uid)
|
||||
fetched = imap.uid_fetch(uid, [X_GM_THRID])
|
||||
if !fetched
|
||||
raise "Thread not found for UID #{uid}!"
|
||||
end
|
||||
raise "Thread not found for UID #{uid}!" if !fetched
|
||||
|
||||
fetched.last.attr[X_GM_THRID]
|
||||
end
|
||||
|
||||
def emails_in_thread(thread_id)
|
||||
uids_to_fetch = imap.uid_search("#{X_GM_THRID} #{thread_id}")
|
||||
emails(uids_to_fetch, ["UID", "LABELS"])
|
||||
emails(uids_to_fetch, %w[UID LABELS])
|
||||
end
|
||||
|
||||
def trash_move(uid)
|
||||
thread_id = thread_id_from_uid(uid)
|
||||
email_uids_to_trash = emails_in_thread(thread_id).map { |e| e['UID'] }
|
||||
email_uids_to_trash = emails_in_thread(thread_id).map { |e| e["UID"] }
|
||||
|
||||
imap.uid_move(email_uids_to_trash, trash_mailbox)
|
||||
ImapSyncLog.log("Thread ID #{thread_id} (UID #{uid}) trashed in Gmail mailbox for #{@username}", :debug)
|
||||
ImapSyncLog.log(
|
||||
"Thread ID #{thread_id} (UID #{uid}) trashed in Gmail mailbox for #{@username}",
|
||||
:debug,
|
||||
)
|
||||
{ trash_uid_validity: open_trash_mailbox, email_uids_to_trash: email_uids_to_trash }
|
||||
end
|
||||
|
||||
@ -124,16 +126,15 @@ module Imap
|
||||
# used for the dropdown in the UI where we allow the user to select the
|
||||
# IMAP mailbox to sync with.
|
||||
def filter_mailboxes(mailboxes_with_attributes)
|
||||
mailboxes_with_attributes.reject do |mb|
|
||||
(mb.attr & [:Drafts, :Sent, :Junk, :Flagged, :Trash]).any?
|
||||
end.map(&:name)
|
||||
mailboxes_with_attributes
|
||||
.reject { |mb| (mb.attr & %i[Drafts Sent Junk Flagged Trash]).any? }
|
||||
.map(&:name)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def apply_gmail_patch(imap)
|
||||
class << imap.instance_variable_get('@parser')
|
||||
|
||||
class << imap.instance_variable_get("@parser")
|
||||
# Modified version of the original `msg_att` from here:
|
||||
# https://github.com/ruby/ruby/blob/1cc8ff001da217d0e98d13fe61fbc9f5547ef722/lib/net/imap.rb#L2346
|
||||
#
|
||||
@ -172,15 +173,14 @@ module Imap
|
||||
when /\A(?:MODSEQ)\z/ni
|
||||
name, val = modseq_data
|
||||
|
||||
# Adding support for GMail extended attributes.
|
||||
# Adding support for GMail extended attributes.
|
||||
when /\A(?:X-GM-LABELS)\z/ni
|
||||
name, val = label_data
|
||||
when /\A(?:X-GM-MSGID)\z/ni
|
||||
name, val = uid_data
|
||||
when /\A(?:X-GM-THRID)\z/ni
|
||||
name, val = uid_data
|
||||
# End custom support for Gmail.
|
||||
|
||||
# End custom support for Gmail.
|
||||
else
|
||||
parse_error("unknown attribute `%s' for {%d}", token.value, n)
|
||||
end
|
||||
|
Reference in New Issue
Block a user