Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0942a43

Browse files
[Notifier] Fix thread key in GoogleChat bridge
Google Chat API has deprecated the use of `threadKey` query parameterin favor of `thread.threadKey` in request's body.
1 parente0ad00c commit0942a43

File tree

8 files changed

+94
-15
lines changed

8 files changed

+94
-15
lines changed

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

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

4+
7.2
5+
---
6+
7+
* Fix thread management with updated Google Chat API
8+
* Keep backward compatibility with`thread_key` in DSN but`threadKey` should be set through`GoogleChatOptions` instead
9+
* Deprecate`GoogleChatOptions::setThreadKey()` in favor of`threadKey()`
10+
411
7.0
512
---
613

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
*/
2121
finalclass GoogleChatOptionsimplements MessageOptionsInterface
2222
{
23-
private ?string$threadKey =null;
2423
privatearray$options = [];
2524

2625
publicfunction__construct(array$options = [])
@@ -82,18 +81,41 @@ public function text(string $text): static
8281
}
8382

8483
/**
84+
* @param string $threadKey Opaque thread identifier string that can be specified to group messages into a single thread.
85+
* If this is the first message with a given thread identifier, a new thread is created.
86+
* Subsequent messages with the same thread identifier will be posted into the same thread.
87+
* {@see https://developers.google.com/workspace/chat/api/reference/rest/v1/spaces.messages#thread}
88+
*
8589
* @return $this
8690
*/
87-
publicfunctionsetThreadKey(?string$threadKey):static
91+
publicfunctionthreadKey(string$threadKey):static
8892
{
89-
$this->threadKey =$threadKey;
93+
$this->options['thread']['threadKey'] =$threadKey;
9094

9195
return$this;
9296
}
9397

98+
/**
99+
* @return $this
100+
*
101+
* @deprecated since Symfony 7.2, to be removed in 8.0
102+
*/
103+
publicfunctionsetThreadKey(?string$threadKey):static
104+
{
105+
trigger_deprecation('symfony/notifier','7.2','The method "%s()" is deprecated and will be removed in 8.0. Use GoogleChatOptions::threadKey() instead.',__METHOD__);
106+
107+
if (null ===$threadKey) {
108+
unset($this->options['thread']);
109+
110+
return$this;
111+
}
112+
113+
return$this->threadKey($threadKey);
114+
}
115+
94116
publicfunctiongetThreadKey(): ?string
95117
{
96-
return$this->threadKey;
118+
return$this->options['thread']['threadKey'] ??null;
97119
}
98120

99121
publicfunctiongetRecipientId(): ?string

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,17 @@ public function __construct(
4848
?EventDispatcherInterface$dispatcher =null,
4949
) {
5050
parent::__construct($client,$dispatcher);
51+
52+
if (null !==$threadKey) {
53+
trigger_deprecation('symfony/notifier','7.2','The "$threadKey" argument of "%s()" is deprecated and will be removed in 8.0. Use GoogleChatOptions::threadKey() instead.',__METHOD__);
54+
}
5155
}
5256

5357
publicfunction__toString():string
5458
{
55-
returnsprintf('googlechat://%s/%s%s',
59+
returnsprintf('googlechat://%s/%s',
5660
$this->getEndpoint(),
57-
$this->space,
58-
$this->threadKey ?'?thread_key='.urlencode($this->threadKey) :''
61+
$this->space
5962
);
6063
}
6164

@@ -85,14 +88,18 @@ protected function doSend(MessageInterface $message): SentMessage
8588
}
8689
}
8790

88-
$threadKey =$options->getThreadKey() ?:$this->threadKey;
91+
if (null !==$this->threadKey &&null ===$options->getThreadKey()) {
92+
$options->threadKey($this->threadKey);
93+
}
94+
95+
$threadKey =$options->getThreadKey();
8996

9097
$url =sprintf('https://%s/v1/spaces/%s/messages?key=%s&token=%s%s',
9198
$this->getEndpoint(),
9299
$this->space,
93100
urlencode($this->accessKey),
94101
urlencode($this->accessToken),
95-
$threadKey ?'&threadKey='.urlencode($threadKey) :''
102+
$threadKey ?'&messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD' :''
96103
);
97104
$response =$this->client->request('POST',$url, [
98105
'json' =>array_filter($options->toArray()),

‎src/Symfony/Component/Notifier/Bridge/GoogleChat/GoogleChatTransportFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
finalclass GoogleChatTransportFactoryextends AbstractTransportFactory
2222
{
2323
/**
24-
* @param Dsn $dsn Format: googlechat://<key>:<token>@default/<space>?thread_key=<thread>
24+
* @param Dsn $dsn Format: googlechat://<key>:<token>@default/<space>
2525
*/
2626
publicfunctioncreate(Dsn$dsn):GoogleChatTransport
2727
{

‎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
@@ -63,7 +63,16 @@ public function testOptionsWithThread()
6363
$options =newGoogleChatOptions();
6464
$options->setThreadKey($thread);
6565
$this->assertSame($thread,$options->getThreadKey());
66+
67+
$expected = [
68+
'thread' => [
69+
'threadKey' =>$thread,
70+
],
71+
];
72+
$this->assertEquals($expected,$options->toArray());
73+
6674
$options->setThreadKey(null);
6775
$this->assertNull($options->getThreadKey());
76+
$this->assertEquals([],$options->toArray());
6877
}
6978
}

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

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

3131
yield [
32-
'googlechat://chat.googleapis.com/AAAAA_YYYYY?thread_key=abcdefg',
32+
'googlechat://chat.googleapis.com/AAAAA_YYYYY',
3333
'googlechat://abcde-fghij:kl_mnopqrstwxyz%3D@chat.googleapis.com/AAAAA_YYYYY?thread_key=abcdefg',
3434
];
3535
}

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

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

4141
publicstaticfunctionsupportedMessagesProvider():iterable
@@ -108,11 +108,45 @@ public function testSendWithOptions()
108108
->method('getContent')
109109
->willReturn('{"name":"spaces/My-Space/messages/abcdefg.hijklmno"}');
110110

111-
$expectedBody =json_encode(['text' =>$message]);
111+
$expectedBody =json_encode(['text' =>$message,'thread' => ['threadKey' =>'My-Thread']]);
112+
113+
$client =newMockHttpClient(function (string$method,string$url,array$options = [])use ($response,$expectedBody):ResponseInterface {
114+
$this->assertSame('POST',$method);
115+
$this->assertSame('https://chat.googleapis.com/v1/spaces/My-Space/messages?key=theAccessKey&token=theAccessToken%3D&messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD',$url);
116+
$this->assertSame($expectedBody,$options['body']);
117+
118+
return$response;
119+
});
120+
121+
$transport =self::createTransport($client);
122+
123+
$options =newGoogleChatOptions(['text' =>$message]);
124+
$options->threadKey('My-Thread');
125+
126+
$sentMessage =$transport->send(newChatMessage($message,$options));
127+
128+
$this->assertSame('spaces/My-Space/messages/abcdefg.hijklmno',$sentMessage->getMessageId());
129+
}
130+
131+
publicfunctiontestSendWithOptionsAndThreadFallback()
132+
{
133+
$message ='testMessage';
134+
135+
$response =$this->createMock(ResponseInterface::class);
136+
137+
$response->expects($this->exactly(2))
138+
->method('getStatusCode')
139+
->willReturn(200);
140+
141+
$response->expects($this->once())
142+
->method('getContent')
143+
->willReturn('{"name":"spaces/My-Space/messages/abcdefg.hijklmno"}');
144+
145+
$expectedBody =json_encode(['text' =>$message,'thread' => ['threadKey' =>'My-Thread']]);
112146

113147
$client =newMockHttpClient(function (string$method,string$url,array$options = [])use ($response,$expectedBody):ResponseInterface {
114148
$this->assertSame('POST',$method);
115-
$this->assertSame('https://chat.googleapis.com/v1/spaces/My-Space/messages?key=theAccessKey&token=theAccessToken%3D&threadKey=My-Thread',$url);
149+
$this->assertSame('https://chat.googleapis.com/v1/spaces/My-Space/messages?key=theAccessKey&token=theAccessToken%3D&messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD',$url);
116150
$this->assertSame($expectedBody,$options['body']);
117151

118152
return$response;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp