Skip to content

Commit a40bbd6

Browse files
[Notifier] Fix thread key in GoogleChat bridge
Google Chat API has deprecated the use of `threadKey` query parameter in favor of `thread.threadKey` in request's body.
1 parent e6a26db commit a40bbd6

File tree

7 files changed

+74
-13
lines changed

7 files changed

+74
-13
lines changed

src/Symfony/Component/Notifier/Bridge/GoogleChat/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.4
5+
---
6+
* Fix thread management with updated Google Chat API
7+
* Keep backward compatibility with `thread_key` in DSN but `threadKey` should be set through `GoogleChatOptions` instead
8+
49
5.3
510
---
611

src/Symfony/Component/Notifier/Bridge/GoogleChat/GoogleChatOptions.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
*/
2121
final class GoogleChatOptions implements MessageOptionsInterface
2222
{
23-
private $threadKey;
2423
private $options = [];
2524

2625
public function __construct(array $options = [])
@@ -81,19 +80,34 @@ public function text(string $text): self
8180
return $this;
8281
}
8382

83+
public function threadKey(string $threadKey): self
84+
{
85+
$this->options['thread']['threadKey'] = $threadKey;
86+
87+
return $this;
88+
}
89+
8490
/**
8591
* @return $this
8692
*/
8793
public function setThreadKey(?string $threadKey): self
8894
{
89-
$this->threadKey = $threadKey;
95+
if (null === $threadKey) {
96+
unset($this->options['thread']);
9097

91-
return $this;
98+
return $this;
99+
}
100+
101+
return $this->threadKey($threadKey);
92102
}
93103

94104
public function getThreadKey(): ?string
95105
{
96-
return $this->threadKey;
106+
if (!isset($this->options['thread']['threadKey'])) {
107+
return null;
108+
}
109+
110+
return $this->options['thread']['threadKey'];
97111
}
98112

99113
public function getRecipientId(): ?string

src/Symfony/Component/Notifier/Bridge/GoogleChat/GoogleChatTransport.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,9 @@ public function __construct(string $space, string $accessKey, string $accessToke
5656

5757
public function __toString(): string
5858
{
59-
return sprintf('googlechat://%s/%s%s',
59+
return sprintf('googlechat://%s/%s',
6060
$this->getEndpoint(),
61-
$this->space,
62-
$this->threadKey ? '?thread_key='.urlencode($this->threadKey) : ''
61+
$this->space
6362
);
6463
}
6564

@@ -102,7 +101,7 @@ protected function doSend(MessageInterface $message): SentMessage
102101
$this->space,
103102
urlencode($this->accessKey),
104103
urlencode($this->accessToken),
105-
$threadKey ? '&threadKey='.urlencode($threadKey) : ''
104+
$threadKey ? '&messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD' : ''
106105
);
107106
$response = $this->client->request('POST', $url, [
108107
'json' => array_filter($options),

src/Symfony/Component/Notifier/Bridge/GoogleChat/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ where:
1414
- `ACCESS_KEY` is your Google Chat access key
1515
- `ACCESS_TOKEN` is your Google Chat access token
1616
- `SPACE` is the Google Chat space
17-
- `THREAD_KEY` is the Google Chat message thread to group messages into a single thread (optional)
17+
- ~~`THREAD_KEY` is the Google Chat message thread to group messages into a single thread (optional)~~ (deprecated)
1818

1919
Resources
2020
---------

src/Symfony/Component/Notifier/Bridge/GoogleChat/Tests/GoogleChatOptionsTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,16 @@ public function testOptionsWithThread()
4040
$options = new GoogleChatOptions();
4141
$options->setThreadKey($thread);
4242
$this->assertSame($thread, $options->getThreadKey());
43+
44+
$expected = [
45+
'thread' => [
46+
'threadKey' => $thread,
47+
],
48+
];
49+
$this->assertEquals($expected, $options->toArray());
50+
4351
$options->setThreadKey(null);
4452
$this->assertNull($options->getThreadKey());
53+
$this->assertEquals([], $options->toArray());
4554
}
4655
}

src/Symfony/Component/Notifier/Bridge/GoogleChat/Tests/GoogleChatTransportFactoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static function createProvider(): iterable
3333
];
3434

3535
yield [
36-
'googlechat://chat.googleapis.com/AAAAA_YYYYY?thread_key=abcdefg',
36+
'googlechat://chat.googleapis.com/AAAAA_YYYYY',
3737
'googlechat://abcde-fghij:kl_mnopqrstwxyz%3D@chat.googleapis.com/AAAAA_YYYYY?thread_key=abcdefg',
3838
];
3939
}

src/Symfony/Component/Notifier/Bridge/GoogleChat/Tests/GoogleChatTransportTest.php

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static function createTransport(?HttpClientInterface $client = null, ?str
3939
public static function toStringProvider(): iterable
4040
{
4141
yield ['googlechat://chat.googleapis.com/My-Space', self::createTransport()];
42-
yield ['googlechat://chat.googleapis.com/My-Space?thread_key=abcdefg', self::createTransport(null, 'abcdefg')];
42+
yield ['googlechat://chat.googleapis.com/My-Space', self::createTransport(null, 'abcdefg')];
4343
}
4444

4545
public static function supportedMessagesProvider(): iterable
@@ -116,11 +116,45 @@ public function testSendWithOptions()
116116
->method('getContent')
117117
->willReturn('{"name":"spaces/My-Space/messages/abcdefg.hijklmno"}');
118118

119-
$expectedBody = json_encode(['text' => $message]);
119+
$expectedBody = json_encode(['text' => $message, 'thread' => ['threadKey' => 'My-Thread']]);
120+
121+
$client = new MockHttpClient(function (string $method, string $url, array $options = []) use ($response, $expectedBody): ResponseInterface {
122+
$this->assertSame('POST', $method);
123+
$this->assertSame('https://chat.googleapis.com/v1/spaces/My-Space/messages?key=theAccessKey&token=theAccessToken%3D&messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD', $url);
124+
$this->assertSame($expectedBody, $options['body']);
125+
126+
return $response;
127+
});
128+
129+
$transport = self::createTransport($client);
130+
131+
$options = new GoogleChatOptions(['text' => $message]);
132+
$options->threadKey('My-Thread');
133+
134+
$sentMessage = $transport->send(new ChatMessage($message, $options));
135+
136+
$this->assertSame('spaces/My-Space/messages/abcdefg.hijklmno', $sentMessage->getMessageId());
137+
}
138+
139+
public function testSendWithOptionsAndThreadFallback()
140+
{
141+
$message = 'testMessage';
142+
143+
$response = $this->createMock(ResponseInterface::class);
144+
145+
$response->expects($this->exactly(2))
146+
->method('getStatusCode')
147+
->willReturn(200);
148+
149+
$response->expects($this->once())
150+
->method('getContent')
151+
->willReturn('{"name":"spaces/My-Space/messages/abcdefg.hijklmno"}');
152+
153+
$expectedBody = json_encode(['text' => $message, 'thread' => ['threadKey' => 'My-Thread']]);
120154

121155
$client = new MockHttpClient(function (string $method, string $url, array $options = []) use ($response, $expectedBody): ResponseInterface {
122156
$this->assertSame('POST', $method);
123-
$this->assertSame('https://chat.googleapis.com/v1/spaces/My-Space/messages?key=theAccessKey&token=theAccessToken%3D&threadKey=My-Thread', $url);
157+
$this->assertSame('https://chat.googleapis.com/v1/spaces/My-Space/messages?key=theAccessKey&token=theAccessToken%3D&messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD', $url);
124158
$this->assertSame($expectedBody, $options['body']);
125159

126160
return $response;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy