Repository navigation
feat: add separate web and desktop expiry notifications #301
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 all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import { getAdminSessionErrorResponse } from '@/lib/server/admin/session'; | ||
| import { | ||
| type CredentialExpiryNotificationChannel, | ||
| subscribeCredentialExpiryNotifications, | ||
| type CredentialExpiryNotificationEvent, | ||
| } from '@/lib/server/domain/credential-expiry-notifications'; | ||
| import { createSseHeaders } from '@/lib/server/shared/sse'; | ||
|
|
||
| export const runtime = 'nodejs'; | ||
| export const dynamic = 'force-dynamic'; | ||
|
|
||
| const encoder = new TextEncoder(); | ||
|
|
||
| const encodeEvent = (event: CredentialExpiryNotificationEvent): Uint8Array => | ||
| encoder.encode( | ||
| `event: credential-expiry\ndata: ${JSON.stringify(event)}\n\n`, | ||
| ); | ||
|
|
||
| export const GET = async (request: Request): Promise<Response> => { | ||
| const authError = await getAdminSessionErrorResponse(request); | ||
|
|
||
| if (authError) return authError; | ||
|
|
||
| const channel = new URL(request.url).searchParams.get('channel'); | ||
| if (channel !== 'web' && channel !== 'desktop') { | ||
| return Response.json( | ||
| { error: 'Notification channel is required' }, | ||
| { status: 400 }, | ||
| ); | ||
| } | ||
|
|
||
| let close = (): void => undefined; | ||
| const stream = new ReadableStream<Uint8Array>({ | ||
| start(controller) { | ||
| let closed = false; | ||
| const finish = (): void => { | ||
| if (closed) return; | ||
| closed = true; | ||
| clearInterval(heartbeat); | ||
| unsubscribe(); | ||
| request.signal.removeEventListener('abort', finish); | ||
| try { | ||
| controller.close(); | ||
| } catch { | ||
| return; | ||
| } | ||
| }; | ||
| const unsubscribe = subscribeCredentialExpiryNotifications( | ||
| channel as CredentialExpiryNotificationChannel, | ||
| (event) => { | ||
| if (closed) return; | ||
| try { | ||
| controller.enqueue(encodeEvent(event)); | ||
| } catch { | ||
| finish(); | ||
| } | ||
| }, | ||
| ); | ||
| const heartbeat = setInterval(() => { | ||
| if (closed) return; | ||
| try { | ||
| controller.enqueue(encoder.encode(': keep-alive\n\n')); | ||
| } catch { | ||
| finish(); | ||
| } | ||
| }, 25_000); | ||
| close = finish; | ||
| request.signal.addEventListener('abort', finish); | ||
| controller.enqueue(encoder.encode(': connected\n\n')); | ||
| }, | ||
| cancel() { | ||
| close(); | ||
| }, | ||
| }); | ||
|
|
||
| return new Response(stream, { headers: createSseHeaders() }); | ||
| }; |
34 changes: 34 additions & 0 deletions
34
app/admin-api/notifications/credential-expiry/test/route.ts
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,34 @@ | ||
| import { getAdminSessionErrorResponse } from '@/lib/server/admin/session'; | ||
| import { publishCredentialExpiryNotification } from '@/lib/server/domain/credential-expiry-notifications'; | ||
| import { getJsonBody } from '@/lib/server/shared/http'; | ||
|
|
||
| export const runtime = 'nodejs'; | ||
| export const dynamic = 'force-dynamic'; | ||
|
|
||
| export const POST = async (request: Request): Promise<Response> => { | ||
| const authError = await getAdminSessionErrorResponse(request); | ||
|
|
||
| if (authError) return authError; | ||
|
|
||
| const body = await getJsonBody<{ channel?: unknown }>(request); | ||
| const channel = body.channel; | ||
| if (channel !== 'web' && channel !== 'desktop') { | ||
| return Response.json( | ||
| { error: 'Notification channel is required' }, | ||
| { status: 400 }, | ||
| ); | ||
| } | ||
|
|
||
| const delivered = publishCredentialExpiryNotification(channel, { | ||
| body: | ||
| channel === 'web' | ||
| ? '这是一条 CodeBuddy2API Web 端测试通知。' | ||
| : '这是一条 CodeBuddy2API 桌面端测试通知。', | ||
| credential: 'test', | ||
| expiresAt: Date.now() / 1000 + 3 * 24 * 60 * 60, | ||
| key: 'three_days', | ||
| title: 'CodeBuddy2API 凭据提醒测试', | ||
| }); | ||
|
|
||
| return Response.json({ delivered, message: 'Test notification sent.' }); | ||
| }; | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the page's EventSource has not connected yet, has been closed, or the browser does not support EventSource,
publishCredentialExpiryNotificationreturns0, but this route still responds with HTTP 200 and the settings UI treats any 2xx response as success. In those cases the user is told that the test notification was sent even though no notification was delivered; return a failure status (or have the client inspectdelivered) when the count is zero.Useful? React with 👍 / 👎.