-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathGoogleGmailConnector.php
More file actions
226 lines (188 loc) · 7.32 KB
/
GoogleGmailConnector.php
File metadata and controls
226 lines (188 loc) · 7.32 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
namespace App\Services\Support\Gmail;
use Google\Client as GoogleClient;
use Google\Service\Gmail as GmailService;
use Google\Service\Gmail\Label as GoogleLabel;
use Google\Service\Gmail\Message as GoogleMessage;
use Illuminate\Support\Str;
class GoogleGmailConnector implements GmailConnector
{
private GmailService $gmail;
private GoogleClient $client;
public function __construct()
{
$client = new GoogleClient();
$client->setApplicationName('Codeweek Internal Support Copilot');
$client->setScopes([GmailService::GMAIL_READONLY]);
$client->setAccessType('offline');
$credentials = config('support_gmail.credentials_json');
if (!$credentials) {
throw new \RuntimeException('SUPPORT_GMAIL_CREDENTIALS_JSON not set');
}
$client->setAuthConfig($credentials);
// Optional OAuth token json (installed-app flows).
$tokenJson = config('support_gmail.token_json');
if ($tokenJson && is_file($tokenJson)) {
$client->setAccessToken(json_decode((string) file_get_contents($tokenJson), true));
}
$this->client = $client;
$this->gmail = new GmailService($client);
}
public function fetchNewMessages(
string $mailbox,
?string $label,
string $query,
?string $sinceHistoryId,
int $max = 25,
): array {
$this->ensureValidToken();
$q = trim($query);
$labelId = null;
$warnings = [];
if ($label) {
// Label filtering is optional. If the label doesn't exist, we ingest without label scoping
// rather than failing the whole poll.
$labelId = $this->resolveLabelIdOrNull($mailbox, $label);
if ($labelId === null && trim((string) $label) !== '') {
$warnings[] = sprintf(
'Configured Gmail label "%s" was not found; polling without label filter.',
trim((string) $label),
);
}
}
$params = [
'q' => $q,
'maxResults' => $max,
];
if ($labelId) {
$params['labelIds'] = [$labelId];
}
// V1: we use search-based ingestion; historyId is only used as a stored cursor.
// If you want strict incremental ingest, we can switch to users.history.list.
$list = $this->gmail->users_messages->listUsersMessages($mailbox, $params);
$messages = [];
foreach (($list->getMessages() ?? []) as $msgRef) {
$full = $this->gmail->users_messages->get($mailbox, $msgRef->getId(), ['format' => 'full']);
$messages[] = $this->toMessage($full);
}
// Best-effort: use the highest historyId seen in payloads, if present.
$nextHistoryId = null;
foreach ($messages as $m) {
// Google "full" message does not always include historyId; ignore if missing.
// We'll set cursor to "now" by leaving null (caller can store polled_at).
$nextHistoryId = $nextHistoryId ?? $sinceHistoryId;
}
return [
'messages' => $messages,
'next_history_id' => $nextHistoryId,
'warnings' => $warnings,
];
}
private function resolveLabelIdOrNull(string $mailbox, string $label): ?string
{
$label = trim($label);
if ($label === '') {
return null;
}
// If user already provided a label ID (usually "Label_..."), use it directly.
if (Str::startsWith($label, 'Label_')) {
return $label;
}
$labels = $this->gmail->users_labels->listUsersLabels($mailbox)->getLabels() ?? [];
/** @var GoogleLabel $l */
foreach ($labels as $l) {
if ($l->getId() === $label || $l->getName() === $label) {
return (string) $l->getId();
}
}
return null;
}
private function ensureValidToken(): void
{
$token = $this->client->getAccessToken();
if (empty($token)) {
throw new \RuntimeException('Gmail token missing. Run support:gmail:authorize and set SUPPORT_GMAIL_TOKEN_JSON.');
}
if (!$this->client->isAccessTokenExpired()) {
return;
}
$refreshToken = $this->client->getRefreshToken() ?: ($token['refresh_token'] ?? null);
if (!$refreshToken) {
throw new \RuntimeException('Gmail token expired and no refresh_token available. Re-run support:gmail:authorize.');
}
$newToken = $this->client->fetchAccessTokenWithRefreshToken($refreshToken);
if (isset($newToken['error'])) {
throw new \RuntimeException('Failed to refresh Gmail token: '.($newToken['error_description'] ?? $newToken['error']));
}
$tokenJson = config('support_gmail.token_json');
if ($tokenJson) {
// Persist the refreshed token best-effort; file permissions handled by authorize command.
@file_put_contents($tokenJson, json_encode($this->client->getAccessToken(), JSON_PRETTY_PRINT));
}
}
private function toMessage(GoogleMessage $msg): GmailMessage
{
$headers = [];
foreach (($msg->getPayload()?->getHeaders() ?? []) as $h) {
$headers[strtolower($h->getName())] = $h->getValue();
}
$subject = $headers['subject'] ?? null;
$from = $headers['from'] ?? null;
$rawBody = $this->extractBestBody($msg);
return new GmailMessage(
id: (string) $msg->getId(),
threadId: $msg->getThreadId(),
subject: $subject,
from: $from,
rawBody: $rawBody,
internalDateMs: $msg->getInternalDate(),
);
}
private function extractBestBody(GoogleMessage $msg): string
{
$payload = $msg->getPayload();
if (!$payload) {
return '';
}
$body = $payload->getBody()?->getData();
if ($body) {
return $this->decodeBody($body);
}
$parts = $payload->getParts() ?? [];
$candidates = $this->flattenParts($parts);
// Prefer text/plain; fallback to text/html stripped.
foreach (['text/plain', 'text/html'] as $mime) {
foreach ($candidates as $part) {
if (($part['mimeType'] ?? null) === $mime && !empty($part['body'])) {
$decoded = $this->decodeBody($part['body']);
return $mime === 'text/html' ? strip_tags($decoded) : $decoded;
}
}
}
return '';
}
/**
* @param array<int, \Google\Service\Gmail\MessagePart> $parts
* @return array<int, array{mimeType: ?string, body: ?string}>
*/
private function flattenParts(array $parts): array
{
$out = [];
foreach ($parts as $p) {
$out[] = [
'mimeType' => $p->getMimeType(),
'body' => $p->getBody()?->getData(),
];
if ($p->getParts()) {
$out = array_merge($out, $this->flattenParts($p->getParts()));
}
}
return $out;
}
private function decodeBody(string $data): string
{
$data = str_replace(['-', '_'], ['+', '/'], $data);
$decoded = base64_decode($data, true);
return $decoded === false ? '' : $decoded;
}
}