-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathslack.ts
More file actions
167 lines (145 loc) · 4.83 KB
/
Copy pathslack.ts
File metadata and controls
167 lines (145 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import {
ROOT_LOGGER,
type RootLogger,
type ScopedLogger,
} from "@posthog/di/logger";
import {
DEEP_LINK_SERVICE,
type IDeepLinkRegistry,
} from "@posthog/platform/deep-link";
import {
type IMainWindow,
MAIN_WINDOW_SERVICE,
} from "@posthog/platform/main-window";
import {
type IUrlLauncher,
URL_LAUNCHER_SERVICE,
} from "@posthog/platform/url-launcher";
import {
type CloudRegion,
getCloudUrlFromRegion,
TypedEventEmitter,
} from "@posthog/shared";
import { inject, injectable } from "inversify";
import type { StartIntegrationFlowOutput } from "./schemas";
const FLOW_TIMEOUT_MS = 5 * 60 * 1000;
export const SlackIntegrationEvent = {
Callback: "callback",
FlowTimedOut: "flowTimedOut",
} as const;
export interface SlackIntegrationCallback {
projectId: number | null;
integrationId: number | null;
status: "success" | "error";
errorCode: string | null;
errorMessage: string | null;
}
export interface SlackFlowTimedOut {
projectId: number;
}
export interface SlackIntegrationEvents {
[SlackIntegrationEvent.Callback]: SlackIntegrationCallback;
[SlackIntegrationEvent.FlowTimedOut]: SlackFlowTimedOut;
}
@injectable()
export class SlackIntegrationService extends TypedEventEmitter<SlackIntegrationEvents> {
private pendingCallback: SlackIntegrationCallback | null = null;
private flowTimeout: ReturnType<typeof setTimeout> | null = null;
private readonly log: ScopedLogger;
constructor(
@inject(DEEP_LINK_SERVICE)
private readonly deepLinkService: IDeepLinkRegistry,
@inject(URL_LAUNCHER_SERVICE)
private readonly urlLauncher: IUrlLauncher,
@inject(MAIN_WINDOW_SERVICE)
private readonly mainWindow: IMainWindow,
@inject(ROOT_LOGGER)
rootLogger: RootLogger,
) {
super();
this.log = rootLogger.scope("slack-integration-service");
this.deepLinkService.registerHandler("slack-integration", (_path, params) =>
this.handleCallback(params),
);
}
public async startFlow(
region: CloudRegion,
projectId: number,
): Promise<StartIntegrationFlowOutput> {
try {
const cloudUrl = getCloudUrlFromRegion(region);
const nextPath = `/account-connected/slack-integration?provider=slack&project_id=${projectId}&connect_from=posthog_code`;
const authorizeUrl = `${cloudUrl}/api/environments/${projectId}/integrations/authorize/?kind=slack&next=${encodeURIComponent(nextPath)}`;
this.clearFlowTimeout();
this.flowTimeout = setTimeout(() => {
this.log.warn("Slack integration flow timed out", { projectId });
this.flowTimeout = null;
this.emit(SlackIntegrationEvent.FlowTimedOut, { projectId });
}, FLOW_TIMEOUT_MS);
await this.urlLauncher.launch(authorizeUrl);
return { success: true };
} catch (error) {
this.clearFlowTimeout();
this.log.error("Failed to start Slack integration flow", {
projectId,
error: error instanceof Error ? error.message : String(error),
});
return {
success: false,
error: error instanceof Error ? error.message : "Unknown error",
};
}
}
public consumePendingCallback(): SlackIntegrationCallback | null {
const pending = this.pendingCallback;
this.pendingCallback = null;
return pending;
}
private handleCallback(params: URLSearchParams): boolean {
const projectIdRaw = params.get("project_id");
const parsedProjectId = projectIdRaw ? Number(projectIdRaw) : null;
const integrationIdRaw = params.get("integration_id");
const parsedIntegrationId = integrationIdRaw
? Number(integrationIdRaw)
: null;
const status = params.get("status") === "error" ? "error" : "success";
const callback: SlackIntegrationCallback = {
projectId:
parsedProjectId !== null && Number.isFinite(parsedProjectId)
? parsedProjectId
: null,
integrationId:
parsedIntegrationId !== null && Number.isFinite(parsedIntegrationId)
? parsedIntegrationId
: null,
status,
errorCode: params.get("error_code") || null,
errorMessage: params.get("error_message") || null,
};
this.clearFlowTimeout();
if (status === "error") {
this.log.error("Received Slack integration callback with error", {
projectId: callback.projectId,
errorCode: callback.errorCode,
errorMessage: callback.errorMessage,
});
}
const hasListeners = this.listenerCount(SlackIntegrationEvent.Callback) > 0;
if (hasListeners) {
this.emit(SlackIntegrationEvent.Callback, callback);
} else {
this.pendingCallback = callback;
}
if (this.mainWindow.isMinimized()) {
this.mainWindow.restore();
}
this.mainWindow.focus();
return true;
}
private clearFlowTimeout(): void {
if (this.flowTimeout) {
clearTimeout(this.flowTimeout);
this.flowTimeout = null;
}
}
}