-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unfurling Discord Message Links #221
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
765a24f
Basic support for Unfurling
HazAT c2424e8
Add Discord Client
HazAT ceaa6df
Lint
HazAT 13d5aad
Add docs and safeguard
HazAT 5e3a1ff
-.-
HazAT 3c6ded9
Formatting
cleptric b7f6a6a
Cleanup
cleptric 153de2e
Docker CS
cleptric File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package event | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/getsentry/sentry-go" | ||
"github.com/slack-go/slack/slackevents" | ||
) | ||
|
||
type LinkSharedEvent struct { | ||
Type PotalEventType `json:"type"` | ||
User string `json:"user"` | ||
TimeStamp string `json:"ts"` | ||
Channel string `json:"channel"` | ||
MessageTimeStamp string `json:"message_ts"` | ||
ThreadTimeStamp string `json:"thread_ts"` | ||
Links []Link `json:"links"` | ||
EventTimestamp string `json:"event_ts"` | ||
} | ||
|
||
type Link struct { | ||
Domain string `json:"domain"` | ||
URL string `json:"url"` | ||
} | ||
|
||
func (e LinkSharedEvent) isValid() bool { | ||
return len(e.Links) > 0 | ||
} | ||
|
||
func ProcessLinkSharedEvent(ctx context.Context, e *slackevents.LinkSharedEvent) *LinkSharedEvent { | ||
hub := sentry.GetHubFromContext(ctx) | ||
txn := sentry.TransactionFromContext(ctx) | ||
|
||
span := txn.StartChild("event.process", sentry.WithDescription("Process LinkShared Event")) | ||
defer span.Finish() | ||
|
||
links := make([]Link, len(e.Links)) | ||
for i, link := range e.Links { | ||
links[i] = Link{ | ||
Domain: link.Domain, | ||
URL: link.URL, | ||
} | ||
} | ||
|
||
linkSharedEvent := LinkSharedEvent{ | ||
Type: linkShared, | ||
User: e.User, | ||
TimeStamp: e.TimeStamp, | ||
Channel: e.Channel, | ||
MessageTimeStamp: e.MessageTimeStamp, | ||
ThreadTimeStamp: e.ThreadTimeStamp, | ||
Links: links, | ||
EventTimestamp: e.EventTimestamp, | ||
} | ||
|
||
if !linkSharedEvent.isValid() { | ||
span.Status = sentry.SpanStatusInvalidArgument | ||
return nil | ||
} | ||
span.Status = sentry.SpanStatusOK | ||
|
||
hub.Scope().SetExtra("event", linkSharedEvent) | ||
hub.Scope().SetTag("event_type", linkShared.String()) | ||
|
||
return &linkSharedEvent | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace App\Event; | ||
|
||
class LinkSharedEvent extends AbstractEvent | ||
{ | ||
public string $user; | ||
public string $timestamp; | ||
public string $channel; | ||
public string $messageTimeStamp; | ||
public string $threadTimeStamp; | ||
public array $links; | ||
public string $eventTimestamp; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param array $event Event data. | ||
*/ | ||
public function __construct(array $event) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->type = self::TYPE_LINK_SHARED; | ||
$this->user = $event['user']; | ||
$this->timestamp = $event['ts']; | ||
$this->channel = $event['channel']; | ||
$this->messageTimeStamp = $event['message_ts']; | ||
$this->threadTimeStamp = $event['thread_ts']; | ||
$this->eventTimestamp = $event['event_ts']; | ||
$this->links = $event['links']; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function process(): void | ||
{ | ||
foreach ($this->links as $link) { | ||
// if Discord link, unfurl and fetch the message and return it | ||
if (str_starts_with($link['url'], 'https://discord.com/')) { | ||
$message = $this->fetchDiscordMessage($link['url']); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Message might be empty |
||
$this->slackClient->unfurl( | ||
channel: $this->channel, | ||
timestamp: $this->messageTimeStamp, | ||
unfurls: [ | ||
$link['url'] => [ | ||
'blocks' => [ | ||
[ | ||
'type' => 'section', | ||
'text' => [ | ||
'type' => 'mrkdwn', | ||
'text' => $message, | ||
], | ||
], | ||
], | ||
], | ||
], | ||
); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Fetch a Discord message | ||
* | ||
* @param string $url The Discord message URL | ||
* @return string The message content | ||
*/ | ||
private function fetchDiscordMessage(string $url): string | ||
{ | ||
if (!preg_match('#^https://discord\.com/channels/\d+/\d+/\d+$#', $url)) { | ||
return $url; | ||
} | ||
|
||
$parts = explode('/', $url); | ||
$channelId = $parts[5]; | ||
$messageId = $parts[6]; | ||
|
||
$message = $this->discordClient->getMessage($channelId, $messageId); | ||
|
||
return $message; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace App\Http; | ||
|
||
use function Cake\Core\env; | ||
use function Sentry\captureMessage; | ||
use function Sentry\withScope; | ||
|
||
class DiscordClient | ||
{ | ||
protected const DISCORD_API_URL = 'discord.com/api/v10'; | ||
|
||
protected Client $client; | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->client = new Client([ | ||
'host' => self::DISCORD_API_URL, | ||
'scheme' => 'https', | ||
'headers' => [ | ||
'Authorization' => 'Bot ' . env('DISCORD_BOT_TOKEN'), | ||
'Content-Type' => 'application/json', | ||
], | ||
]); | ||
} | ||
|
||
/** | ||
* Get a message from a channel | ||
* | ||
* @param string $channelId The channel ID | ||
* @param string $messageId The message ID | ||
* @return string The message content | ||
*/ | ||
public function getMessage(string $channelId, string $messageId): string | ||
{ | ||
$response = $this->client->get("channels/{$channelId}/messages/{$messageId}"); | ||
|
||
if ($response->isSuccess()) { | ||
$json = $response->getJson(); | ||
|
||
return $json['content'] ?? ''; | ||
} | ||
|
||
withScope(function ($scope) use ($response, $channelId, $messageId): void { | ||
$scope->setContext('Discord API', [ | ||
'channel_id' => $channelId, | ||
'message_id' => $messageId, | ||
'discord_response' => $response->getJson(), | ||
]); | ||
captureMessage('Discord API error: Failed to fetch message'); | ||
}); | ||
|
||
return ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@HazAT As Slack only emits events for configured domains, do we need this check?