|
2 | 2 |
|
3 | 3 | import logging |
4 | 4 | from collections import defaultdict |
5 | | -from collections.abc import Mapping, MutableMapping |
| 5 | +from collections.abc import Mapping |
6 | 6 | from typing import Any |
7 | 7 |
|
8 | 8 | import orjson |
|
31 | 31 | from sentry.integrations.slack.unfurl.types import LinkType, UnfurlableUrl |
32 | 32 | from sentry.integrations.slack.views.link_identity import build_linking_url |
33 | 33 | from sentry.organizations.services.organization import organization_service |
| 34 | +from sentry.organizations.services.organization.model import RpcOrganization |
34 | 35 |
|
35 | 36 | from .base import SlackDMEndpoint |
36 | 37 | from .command import LINK_FROM_CHANNEL_MESSAGE |
@@ -159,108 +160,135 @@ def on_message(self, request: Request, slack_request: SlackDMRequest) -> Respons |
159 | 160 |
|
160 | 161 | return self.respond() |
161 | 162 |
|
162 | | - def on_link_shared(self, request: Request, slack_request: SlackDMRequest) -> bool: |
163 | | - """Returns true on success""" |
164 | | - matches: MutableMapping[LinkType, list[UnfurlableUrl]] = defaultdict(list) |
| 163 | + def _get_unfurlable_links( |
| 164 | + self, |
| 165 | + request: Request, |
| 166 | + slack_request: SlackDMRequest, |
| 167 | + data: dict[str, Any], |
| 168 | + organization: RpcOrganization | None, |
| 169 | + logger_params: dict[str, Any], |
| 170 | + ) -> dict[LinkType, list[UnfurlableUrl]]: |
| 171 | + matches: dict[LinkType, list[UnfurlableUrl]] = defaultdict(list) |
165 | 172 | links_seen = set() |
166 | 173 |
|
167 | | - data = slack_request.data.get("event", {}) |
168 | | - |
169 | | - logger_params = { |
170 | | - "integration_id": slack_request.integration.id, |
171 | | - "team_id": slack_request.team_id, |
172 | | - "channel_id": slack_request.channel_id, |
173 | | - "user_id": slack_request.user_id, |
174 | | - "channel": slack_request.channel_id, |
175 | | - **data, |
176 | | - } |
177 | | - |
178 | | - # An unfurl may have multiple links to unfurl |
179 | 174 | for item in data.get("links", []): |
180 | | - try: |
181 | | - url = item["url"] |
182 | | - except Exception: |
183 | | - _logger.exception("parse-link-error", extra={**logger_params, "url": url}) |
184 | | - continue |
185 | | - |
186 | | - link_type, args = match_link(url) |
187 | | - |
188 | | - # Link can't be unfurled |
189 | | - if link_type is None or args is None: |
190 | | - continue |
191 | | - |
192 | | - ois = integration_service.get_organization_integrations( |
193 | | - integration_id=slack_request.integration.id, limit=1 |
194 | | - ) |
195 | | - organization_id = ois[0].organization_id if len(ois) > 0 else None |
196 | | - organization_context = ( |
197 | | - organization_service.get_organization_by_id( |
198 | | - id=organization_id, user_id=None, include_projects=False, include_teams=False |
199 | | - ) |
200 | | - if organization_id |
201 | | - else None |
202 | | - ) |
203 | | - organization = organization_context.organization if organization_context else None |
204 | | - logger_params["organization_id"] = organization_id |
205 | | - |
206 | | - if ( |
207 | | - organization |
208 | | - and link_type == LinkType.DISCOVER |
209 | | - and not slack_request.has_identity |
210 | | - and features.has("organizations:discover-basic", organization, actor=request.user) |
211 | | - ): |
| 175 | + with MessagingInteractionEvent( |
| 176 | + interaction_type=MessagingInteractionType.PROCESS_SHARED_LINK, |
| 177 | + spec=SlackMessagingSpec(), |
| 178 | + ).capture() as lifecycle: |
212 | 179 | try: |
213 | | - analytics.record( |
214 | | - SlackIntegrationChartUnfurl( |
215 | | - organization_id=organization.id, |
216 | | - unfurls_count=0, |
217 | | - ) |
| 180 | + url = item["url"] |
| 181 | + except Exception: |
| 182 | + lifecycle.record_failure("Failed to parse link", extra={**logger_params}) |
| 183 | + continue |
| 184 | + |
| 185 | + link_type, args = match_link(url) |
| 186 | + |
| 187 | + # Link can't be unfurled |
| 188 | + if link_type is None or args is None: |
| 189 | + lifecycle.record_halt("Unfurlable link", extra={"url": url}) |
| 190 | + continue |
| 191 | + |
| 192 | + if ( |
| 193 | + organization |
| 194 | + and link_type == LinkType.DISCOVER |
| 195 | + and not slack_request.has_identity |
| 196 | + and features.has( |
| 197 | + "organizations:discover-basic", organization, actor=request.user |
218 | 198 | ) |
219 | | - except Exception as e: |
220 | | - sentry_sdk.capture_exception(e) |
| 199 | + ): |
| 200 | + try: |
| 201 | + analytics.record( |
| 202 | + SlackIntegrationChartUnfurl( |
| 203 | + organization_id=organization.id, |
| 204 | + unfurls_count=0, |
| 205 | + ) |
| 206 | + ) |
| 207 | + except Exception as e: |
| 208 | + sentry_sdk.capture_exception(e) |
221 | 209 |
|
222 | | - self.prompt_link(slack_request) |
223 | | - return True |
| 210 | + self.prompt_link(slack_request) |
| 211 | + lifecycle.record_halt("Discover link requires identity", extra={"url": url}) |
| 212 | + return {} |
224 | 213 |
|
225 | | - # Don't unfurl the same thing multiple times |
226 | | - seen_marker = hash(orjson.dumps((link_type, list(args))).decode()) |
227 | | - if seen_marker in links_seen: |
228 | | - continue |
| 214 | + # Don't unfurl the same thing multiple times |
| 215 | + seen_marker = hash(orjson.dumps((link_type, list(args))).decode()) |
| 216 | + if seen_marker in links_seen: |
| 217 | + continue |
229 | 218 |
|
230 | | - links_seen.add(seen_marker) |
231 | | - matches[link_type].append(UnfurlableUrl(url=url, args=args)) |
| 219 | + links_seen.add(seen_marker) |
| 220 | + matches[link_type].append(UnfurlableUrl(url=url, args=args)) |
232 | 221 |
|
233 | | - if not matches: |
234 | | - return False |
| 222 | + return matches |
235 | 223 |
|
236 | | - # Unfurl each link type |
237 | | - results: MutableMapping[str, Any] = {} |
| 224 | + def _unfurl_links( |
| 225 | + self, slack_request: SlackDMRequest, matches: dict[LinkType, list[UnfurlableUrl]] |
| 226 | + ) -> dict[str, Any]: |
| 227 | + results: dict[str, Any] = {} |
238 | 228 | for link_type, unfurl_data in matches.items(): |
239 | 229 | results.update( |
240 | 230 | link_handlers[link_type].fn( |
241 | | - request, |
242 | | - slack_request.integration, |
243 | | - unfurl_data, |
244 | | - slack_request.user, |
| 231 | + slack_request.integration, unfurl_data, slack_request.user |
245 | 232 | ) |
246 | 233 | ) |
247 | 234 |
|
248 | | - if not results: |
249 | | - return False |
250 | | - |
251 | 235 | # XXX(isabella): we use our message builders to create the blocks for each link to be |
252 | 236 | # unfurled, so the original result will include the fallback text string, however, the |
253 | 237 | # unfurl endpoint does not accept fallback text. |
254 | 238 | for link_info in results.values(): |
255 | 239 | if "text" in link_info: |
256 | 240 | del link_info["text"] |
257 | 241 |
|
258 | | - payload = {"channel": data["channel"], "ts": data["message_ts"], "unfurls": results} |
| 242 | + return results |
| 243 | + |
| 244 | + def on_link_shared(self, request: Request, slack_request: SlackDMRequest) -> bool: |
| 245 | + """Returns true on success""" |
| 246 | + |
| 247 | + data = slack_request.data.get("event", {}) |
| 248 | + |
| 249 | + ois = integration_service.get_organization_integrations( |
| 250 | + integration_id=slack_request.integration.id, limit=1 |
| 251 | + ) |
| 252 | + organization_id = ois[0].organization_id if len(ois) > 0 else None |
| 253 | + organization_context = ( |
| 254 | + organization_service.get_organization_by_id( |
| 255 | + id=organization_id, |
| 256 | + user_id=None, |
| 257 | + include_projects=False, |
| 258 | + include_teams=False, |
| 259 | + ) |
| 260 | + if organization_id |
| 261 | + else None |
| 262 | + ) |
| 263 | + organization = organization_context.organization if organization_context else None |
| 264 | + |
| 265 | + logger_params = { |
| 266 | + "integration_id": slack_request.integration.id, |
| 267 | + "team_id": slack_request.team_id, |
| 268 | + "channel_id": slack_request.channel_id, |
| 269 | + "user_id": slack_request.user_id, |
| 270 | + "channel": slack_request.channel_id, |
| 271 | + "organization_id": organization_id, |
| 272 | + **data, |
| 273 | + } |
| 274 | + |
| 275 | + # An unfurl may have multiple links to unfurl |
| 276 | + matches = self._get_unfurlable_links( |
| 277 | + request, slack_request, data, organization, logger_params |
| 278 | + ) |
| 279 | + if not matches: |
| 280 | + return False |
| 281 | + |
| 282 | + # Unfurl each link type |
| 283 | + results = self._unfurl_links(slack_request, matches) |
| 284 | + if not results: |
| 285 | + return False |
259 | 286 |
|
260 | 287 | with MessagingInteractionEvent( |
261 | 288 | interaction_type=MessagingInteractionType.UNFURL_LINK, |
262 | 289 | spec=SlackMessagingSpec(), |
263 | 290 | ).capture() as lifecycle: |
| 291 | + payload = {"channel": data["channel"], "ts": data["message_ts"], "unfurls": results} |
264 | 292 | client = SlackSdkClient(integration_id=slack_request.integration.id) |
265 | 293 | try: |
266 | 294 | client.chat_unfurl( |
|
0 commit comments