1
1
import { Api } from "coder/site/src/api/api"
2
+ import { Workspace , GetInboxNotificationResponse } from "coder/site/src/api/typesGenerated"
2
3
import { ProxyAgent } from "proxy-agent"
3
4
import * as vscode from "vscode"
4
5
import { WebSocket } from "ws"
5
6
import { errToStr } from "./api-helper"
6
7
import { type Storage } from "./storage"
7
8
8
- type InboxMessage = {
9
- unread_count : number
10
- notification : {
11
- id : string
12
- user_id : string
13
- template_id : string
14
- targets : string [ ]
15
- title : string
16
- content : string
17
- actions : Record < string , string >
18
- read_at : string
19
- created_at : string
20
- }
21
- }
22
-
23
9
// These are the template IDs of our notifications.
24
10
// Maybe in the future we should avoid hardcoding
25
11
// these in both coderd and here.
@@ -31,17 +17,23 @@ export class Inbox implements vscode.Disposable {
31
17
private disposed = false
32
18
private socket : WebSocket
33
19
34
- constructor ( httpAgent : ProxyAgent , restClient : Api , storage : Storage ) {
20
+ constructor ( workspace : Workspace , httpAgent : ProxyAgent , restClient : Api , storage : Storage ) {
35
21
this . storage = storage
36
22
37
23
const baseUrlRaw = restClient . getAxiosInstance ( ) . defaults . baseURL
38
24
if ( ! baseUrlRaw ) {
39
25
throw new Error ( "No base URL set on REST client" )
40
26
}
41
27
28
+ const watchTemplates = [ TEMPLATE_WORKSPACE_OUT_OF_DISK , TEMPLATE_WORKSPACE_OUT_OF_MEMORY ]
29
+ const watchTemplatesParam = encodeURIComponent ( watchTemplates . join ( "," ) )
30
+
31
+ const watchTargets = [ workspace . id ]
32
+ const watchTargetsParam = encodeURIComponent ( watchTargets . join ( "," ) )
33
+
42
34
const baseUrl = new URL ( baseUrlRaw )
43
35
const socketProto = baseUrl . protocol === "https:" ? "wss:" : "ws:"
44
- const socketUrlRaw = `${ socketProto } //${ baseUrl . host } /api/v2/notifications/inbox/watch`
36
+ const socketUrlRaw = `${ socketProto } //${ baseUrl . host } /api/v2/notifications/inbox/watch?templates= ${ watchTemplatesParam } &targets= ${ watchTargetsParam } `
45
37
46
38
const coderSessionTokenHeader = "Coder-Session-Token"
47
39
this . socket = new WebSocket ( new URL ( socketUrlRaw ) , {
@@ -64,14 +56,9 @@ export class Inbox implements vscode.Disposable {
64
56
65
57
this . socket . on ( "message" , ( data ) => {
66
58
try {
67
- const inboxMessage = JSON . parse ( data . toString ( ) ) as InboxMessage
59
+ const inboxMessage = JSON . parse ( data . toString ( ) ) as GetInboxNotificationResponse
68
60
69
- if (
70
- inboxMessage . notification . template_id === TEMPLATE_WORKSPACE_OUT_OF_DISK ||
71
- inboxMessage . notification . template_id === TEMPLATE_WORKSPACE_OUT_OF_MEMORY
72
- ) {
73
- vscode . window . showInformationMessage ( inboxMessage . notification . title )
74
- }
61
+ vscode . window . showInformationMessage ( inboxMessage . notification . title )
75
62
} catch ( error ) {
76
63
this . notifyError ( error )
77
64
}
0 commit comments