- Notifications
You must be signed in to change notification settings - Fork35
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
f73eaee2de7f1288b31a21809fb8eae596e5f4d8e8be3f0a4455ba73f4f99b257acbff48d1c6b7f7d9c0cf5ebea474b906d695588File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
Connect to Coder Inbox on /api/v2/notifications/watch to listen fornotifications. Currently limit this to only show notifications forOOM/OOD notifications.
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import { Api } from "coder/site/src/api/api" | ||
| import * as vscode from "vscode" | ||
| import { WebSocket } from "ws" | ||
| import { errToStr } from "./api-helper" | ||
| import { Storage } from "./storage" | ||
DanielleMaywood marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| type InboxMessage = { | ||
| unread_count: number | ||
| notification: { | ||
| id: string | ||
| user_id: string | ||
| template_id: string | ||
| targets: string[] | ||
| title: string | ||
| content: string | ||
| actions: { | ||
| [key: string]: string | ||
| } | ||
DanielleMaywood marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| read_at: string | ||
| created_at: string | ||
| } | ||
| } | ||
| const TemplateWorkspaceOutOfMemory = "a9d027b4-ac49-4fb1-9f6d-45af15f64e7a" | ||
| const TemplateWorkspaceOutOfDisk = "f047f6a3-5713-40f7-85aa-0394cce9fa3a" | ||
DanielleMaywood marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| export class Inbox implements vscode.Disposable { | ||
| private disposed = false | ||
| private socket: WebSocket | ||
| constructor( | ||
| private readonly restClient: Api, | ||
| private readonly storage: Storage, | ||
DanielleMaywood marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| ) { | ||
| // const url = this.restClient.getAxiosInstance().defaults.baseURL | ||
| const token = this.restClient.getAxiosInstance().defaults.headers.common["Coder-Session-Token"] as | ||
| | string | ||
| | undefined | ||
| // const inboxUrl = new URL(`${url}/api/v2/notifications/watch`); | ||
| const inboxUrl = new URL(`ws://localhost:8080`) | ||
| this.storage.writeToCoderOutputChannel("Listening to Coder Inbox") | ||
| // We're gonna connect over WebSocket so replace the scheme. | ||
| if (inboxUrl.protocol === "https") { | ||
| inboxUrl.protocol = "wss" | ||
| } else if (inboxUrl.protocol === "http") { | ||
| inboxUrl.protocol = "ws" | ||
| } | ||
| this.socket = new WebSocket(inboxUrl, { | ||
| headers: { | ||
| "Coder-Session-Token": token, | ||
| }, | ||
| }) | ||
| this.socket.on("error", (error) => { | ||
| this.notifyError(error) | ||
| }) | ||
DanielleMaywood marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| this.socket.on("message", (data) => { | ||
Parkreiner marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| try { | ||
| const inboxMessage = JSON.parse(data.toString()) as InboxMessage | ||
Parkreiner marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| if ( | ||
| inboxMessage.notification.template_id === TemplateWorkspaceOutOfDisk || | ||
| inboxMessage.notification.template_id === TemplateWorkspaceOutOfMemory | ||
| ) { | ||
| vscode.window.showWarningMessage(inboxMessage.notification.title) | ||
Parkreiner marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| } | ||
| } catch (error) { | ||
| this.notifyError(error) | ||
| } | ||
Parkreiner marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| }) | ||
| } | ||
| 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) | ||
| } | ||
| } | ||