-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.rb
88 lines (74 loc) · 2.37 KB
/
plugin.rb
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
83
84
85
86
87
88
# SPDX-FileCopyrightText: 2023 CERN
# SPDX-License-Identifier: MIT
# frozen_string_literal: true
# name: msgraph-polling
# about: A plugin to enable polling mails using Microsoft Graph API
# version: 1.0
# authors: CERN
# url: https://github.com/cern/msgraph-poll-discourse-plugin
require_relative "lib/msgraph-poller/api.rb"
require "oauth2"
enabled_site_setting :msgraph_polling_enabled
after_initialize do
class ::MsGraphEmailPoller < Email::Poller
def initialize
auth_url =
"#{SiteSetting.msgraph_polling_login_endpoint}/#{SiteSetting.msgraph_polling_tenant_id}/oauth2/v2.0/token"
oauth_provider =
OAuth2::Client.new(
SiteSetting.msgraph_polling_client_id,
nil,
token_url: auth_url
)
@token =
OAuth2::AccessToken.new(
oauth_provider,
nil,
refresh_token: SiteSetting.msgraph_polling_oauth2_refresh_token
)
begin
@token = @token.refresh! if self.enabled?
SiteSetting.msgraph_polling_oauth2_refresh_token = @token.refresh_token
rescue StandardError => e
Rails.logger.error(
"Error while initializing MSGraph plugin: #{e}"
)
end
end
def enabled?
SiteSetting.msgraph_polling_enabled?
end
def refresh_token_if_needed
@token = @token.refresh! if @token.expired?
SiteSetting.msgraph_polling_oauth2_refresh_token = @token.refresh_token
end
def poll_mailbox(process_cb)
begin
self.refresh_token_if_needed()
msgraph_api =
MsGraphAPI.new(
SiteSetting.msgraph_polling_graph_endpoint,
SiteSetting.msgraph_polling_mailbox,
@token.token
)
# To avoid managing paging we get all the emails until there are none remaining
while (messages = msgraph_api.get_messages_id).length > 0
messages.each do |message|
mime = msgraph_api.get_message_mime(message)
process_cb.call(mime)
msgraph_api.delete_message(message)
end
end
rescue StandardError => e
Rails.logger.error(
"Error while polling emails with MsGraph plugin: #{e}"
)
end
end
end
register_email_poller(::MsGraphEmailPoller.new)
end
load File.expand_path(
"../lib/validators/msgraph_poll_settings_validator.rb",
__FILE__
)