Skip to content

Commit cd9a0a9

Browse files
authored
Merge pull request #104 from ousid/emails/messages-delta
Feat: Add Delta Messages Feature
2 parents f412178 + e1699a0 commit cd9a0a9

File tree

8 files changed

+66
-22
lines changed

8 files changed

+66
-22
lines changed

src/AdminResources/Emails.php

+18-1
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77

88
class Emails extends MsGraphAdmin
99
{
10+
private ?bool $delta = null;
11+
1012
private string $userId = '';
1113

1214
private string $top = '100';
1315

1416
private string $skip = '0';
1517

18+
private string $search = '';
19+
1620
private string $subject = '';
1721

1822
private string $body = '';
@@ -106,6 +110,13 @@ public function attachments(array $attachments): static
106110
return $this;
107111
}
108112

113+
public function delta(?bool $delta = true): static
114+
{
115+
$this->delta = $delta;
116+
117+
return $this;
118+
}
119+
109120
/**
110121
* @throws Exception
111122
*/
@@ -117,6 +128,11 @@ public function get(array $params = []): array
117128

118129
$top = request('top', $this->top);
119130
$skip = request('skip', $this->skip);
131+
$search = request('search', $this->search);
132+
133+
if (filled($search) && $this->delta) {
134+
throw new Exception('Search is not supported in delta queries.');
135+
}
120136

121137
if ($params == []) {
122138
$params = http_build_query([
@@ -130,7 +146,8 @@ public function get(array $params = []): array
130146
}
131147

132148
// get messages from folderId
133-
$emails = MsGraphAdmin::get('users/'.$this->userId.'/messages?'.$params);
149+
$messages = $this->delta ? 'messages/delta' : 'messages';
150+
$emails = MsGraphAdmin::get('users/'.$this->userId.'/'.$messages.'?'.$params);
134151

135152
if (isset($emails->error)) {
136153
throw new Exception("Graph API Error, code: {$emails->error->code}, Message: {$emails->error->message}");

src/MsGraph.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ protected function guzzle(string $type, string $request, array $data = [], array
341341

342342
} catch (ClientException $e) {
343343
throw new Exception($e->getMessage());
344-
//return json_decode(($e->getResponse()->getBody()->getContents()));
344+
// return json_decode(($e->getResponse()->getBody()->getContents()));
345345
} catch (Exception $e) {
346346
throw new Exception($e->getMessage());
347347
}

src/Resources/Emails/Emails.php

+41-13
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@
88

99
class Emails extends MsGraph
1010
{
11-
public function folders(): Folders
12-
{
13-
return new Folders;
14-
}
11+
private ?bool $delta = null;
1512

1613
private string $top = '';
1714

1815
private string $skip = '';
1916

17+
private string $search = '';
18+
2019
private string $subject = '';
2120

2221
private string $body = '';
@@ -91,6 +90,11 @@ public function attachments(array $attachments): static
9190
return $this;
9291
}
9392

93+
public function folders(): Folders
94+
{
95+
return new Folders;
96+
}
97+
9498
public function singleValueExtendedProperties(array $singleValueExtendedProperties): static
9599
{
96100
$this->singleValueExtendedProperties = $singleValueExtendedProperties;
@@ -112,6 +116,13 @@ public function skip(string $skip): static
112116
return $this;
113117
}
114118

119+
public function delta(?bool $delta = true): static
120+
{
121+
$this->delta = $delta;
122+
123+
return $this;
124+
}
125+
115126
/**
116127
* @throws Exception
117128
*/
@@ -121,6 +132,11 @@ public function get(string $folderIdOrName = 'Inbox', array $params = []): array
121132

122133
$top = request('top', $this->top);
123134
$skip = request('skip', $this->skip);
135+
$search = request('search', $this->search);
136+
137+
if (filled($search) && $this->delta) {
138+
throw new Exception('Search is not supported in delta queries.');
139+
}
124140

125141
if ($top === '') {
126142
$top = 25;
@@ -138,16 +154,28 @@ public function get(string $folderIdOrName = 'Inbox', array $params = []): array
138154
];
139155
}
140156

141-
if ($this->isId($folderIdOrName)) {
142-
$folder = MsGraph::emails()->folders()->find($folderIdOrName);
143-
} else {
144-
$folder = MsGraph::emails()->folders()->findByName($folderIdOrName);
145-
}
157+
$folder = $folderId == '' ? 'Inbox' : $folderId;
158+
159+
// get inbox from folders list
160+
$folder = MsGraph::get("me/mailFolders?\$filter=startswith(displayName,'$folder')");
146161

147-
if ($folder !== []) {
148-
return MsGraph::get("me/mailFolders/".$folder['id']."/messages?".http_build_query($params));
149-
} else {
150-
throw new Exception('Email folder not found');
162+
if (isset($folder['value'][0])) {
163+
// folder id
164+
$folderId = $folder['value'][0]['id'];
165+
$messages = $this->delta ? 'messages/delta' : 'messages';
166+
167+
// get messages from folderId
168+
if ($this->isId($folderIdOrName)) {
169+
$folder = MsGraph::emails()->folders()->find($folderIdOrName);
170+
} else {
171+
$folder = MsGraph::emails()->folders()->findByName($folderIdOrName);
172+
}
173+
174+
if ($folder !== []) {
175+
return MsGraph::get('me/mailFolders/'.$folder['id']."/{$messages}?".http_build_query($params));
176+
} else {
177+
throw new Exception('Email folder not found');
178+
}
151179
}
152180
}
153181

src/Resources/Emails/Folders.php

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function find(string $id): array
3939
public function findByName(string $name): array
4040
{
4141
$response = MsGraph::get("me/mailFolders?\$filter=startswith(displayName,'$name')");
42+
4243
return $response['value'][0] ?? [];
4344
}
4445

src/Validators/GraphQueryValidator.php

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
namespace Dcblogdev\MsGraph\Validators;
66

7-
use InvalidArgumentException;
8-
97
class GraphQueryValidator extends Validator
108
{
119
protected static array $allowedParams = [

tests/Validators/EmailFolderUpdateValidatorTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
test('valid parameters are accepted', function () {
66
$response = EmailFolderUpdateValidator::validate([
7-
'displayName' => 'demo'
7+
'displayName' => 'demo',
88
]);
99

1010
expect($response)->toEqual([
11-
'displayName' => 'demo'
11+
'displayName' => 'demo',
1212
]);
1313
});
1414

tests/Validators/GraphQueryValidatorTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@
4040
'$search' => 'test',
4141
'$format' => 'pdf',
4242
]);
43-
})->throws(InvalidArgumentException::class, 'Invalid parameters: $tops. Allowed parameters: $top, $skip, $filter, $orderby, $select, $expand, $count, $search, $format.');
43+
})->throws(InvalidArgumentException::class, 'Invalid parameters: $tops. Allowed parameters: $top, $skip, $filter, $orderby, $select, $expand, $count, $search, $format.');

tests/Validators/ValidatorTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010

1111
test('cannot validate none existing params', function () {
1212
Validator::validate([
13-
'$top' => 10
13+
'$top' => 10,
1414
]);
15-
})->throws(InvalidArgumentException::class, 'Invalid parameters: $top. Allowed parameters: .');
15+
})->throws(InvalidArgumentException::class, 'Invalid parameters: $top. Allowed parameters: .');

0 commit comments

Comments
 (0)