-
Notifications
You must be signed in to change notification settings - Fork 23
feat: add support for coder inbox #444
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f73eaee
feat: begin impl of coder inbox
DanielleMaywood 2de7f12
chore: take inspiration from existing websocket
DanielleMaywood 88b31a2
chore: apply feedback
DanielleMaywood 1809fb8
chore: update websocket url
DanielleMaywood eae596e
chore: replace showWarningMessage with showInformationMessage
DanielleMaywood 5f4d8e8
Merge branch 'main' into dm-coder-inbox-oom-ood
DanielleMaywood be3f0a4
chore: upgrade dependencies
DanielleMaywood 455ba73
Merge branch 'main' into dm-coder-inbox-oom-ood
DanielleMaywood f4f99b2
chore: only upgrade coder dependency
DanielleMaywood 57acbff
chore: socketUrlRaw -> socketUrl
DanielleMaywood 48d1c6b
chore: private -> #
DanielleMaywood 7f7d9c0
chore: cleanup on websocket error
DanielleMaywood cf5ebea
chore: add comment
DanielleMaywood 474b906
chore: appease the linter
DanielleMaywood d695588
chore: request plaintext format from api
DanielleMaywood 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 hidden or 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 hidden or 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,93 @@ | ||
import { Api } from "coder/site/src/api/api" | ||
import { ProxyAgent } from "proxy-agent" | ||
import * as vscode from "vscode" | ||
import { WebSocket } from "ws" | ||
import { errToStr } from "./api-helper" | ||
import { type Storage } from "./storage" | ||
|
||
type InboxMessage = { | ||
unread_count: number | ||
notification: { | ||
id: string | ||
user_id: string | ||
template_id: string | ||
targets: string[] | ||
title: string | ||
content: string | ||
actions: Record<string, string> | ||
read_at: string | ||
created_at: string | ||
} | ||
} | ||
|
||
// These are the template IDs of our notifications. | ||
// Maybe in the future we should avoid hardcoding | ||
// these in both coderd and here. | ||
const TEMPLATE_WORKSPACE_OUT_OF_MEMORY = "a9d027b4-ac49-4fb1-9f6d-45af15f64e7a" | ||
const TEMPLATE_WORKSPACE_OUT_OF_DISK = "f047f6a3-5713-40f7-85aa-0394cce9fa3a" | ||
|
||
export class Inbox implements vscode.Disposable { | ||
private readonly storage: Storage | ||
private disposed = false | ||
private socket: WebSocket | ||
DanielleMaywood marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
constructor(httpAgent: ProxyAgent, restClient: Api, storage: Storage) { | ||
this.storage = storage | ||
|
||
const baseUrlRaw = restClient.getAxiosInstance().defaults.baseURL | ||
if (!baseUrlRaw) { | ||
throw new Error("No base URL set on REST client") | ||
} | ||
|
||
const baseUrl = new URL(baseUrlRaw) | ||
DanielleMaywood marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const socketProto = baseUrl.protocol === "https:" ? "wss:" : "ws:" | ||
const socketUrlRaw = `${socketProto}//${baseUrl.host}/api/v2/notifications/watch` | ||
|
||
const coderSessionTokenHeader = "Coder-Session-Token" | ||
this.socket = new WebSocket(new URL(socketUrlRaw), { | ||
followRedirects: true, | ||
agent: httpAgent, | ||
headers: { | ||
[coderSessionTokenHeader]: restClient.getAxiosInstance().defaults.headers.common[coderSessionTokenHeader] as | ||
| string | ||
| undefined, | ||
}, | ||
}) | ||
|
||
this.socket.on("open", () => { | ||
this.storage.writeToCoderOutputChannel("Listening to Coder Inbox") | ||
}) | ||
|
||
this.socket.on("error", (error) => { | ||
this.notifyError(error) | ||
}) | ||
DanielleMaywood marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
this.socket.on("message", (data) => { | ||
Parkreiner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
try { | ||
const inboxMessage = JSON.parse(data.toString()) as InboxMessage | ||
Parkreiner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if ( | ||
inboxMessage.notification.template_id === TEMPLATE_WORKSPACE_OUT_OF_DISK || | ||
inboxMessage.notification.template_id === TEMPLATE_WORKSPACE_OUT_OF_MEMORY | ||
) { | ||
vscode.window.showWarningMessage(inboxMessage.notification.title) | ||
Parkreiner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} catch (error) { | ||
this.notifyError(error) | ||
} | ||
Parkreiner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
} | ||
|
||
dispose() { | ||
if (!this.disposed) { | ||
this.storage.writeToCoderOutputChannel("No longer listening to Coder Inbox") | ||
this.socket.close() | ||
this.disposed = true | ||
} | ||
} | ||
|
||
private notifyError(error: unknown) { | ||
const message = errToStr(error, "Got empty error while monitoring Coder Inbox") | ||
this.storage.writeToCoderOutputChannel(message) | ||
} | ||
} |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.