Skip to content

Commit

Permalink
hooking up courier
Browse files Browse the repository at this point in the history
  • Loading branch information
dholms committed Jan 30, 2024
1 parent efb613a commit d79baff
Show file tree
Hide file tree
Showing 8 changed files with 700 additions and 3 deletions.
12 changes: 12 additions & 0 deletions packages/pds/buf.gen.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: v1
plugins:
- plugin: es
opt:
- target=ts
- import_extension=.ts
out: src/proto
- plugin: connect-es
opt:
- target=ts
- import_extension=.ts
out: src/proto
9 changes: 8 additions & 1 deletion packages/pds/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"test:sqlite": "jest --testPathIgnorePatterns /tests/proxied/*",
"test:log": "tail -50 test.log | pino-pretty",
"test:updateSnapshot": "jest --updateSnapshot",
"migration:create": "ts-node ./bin/migration-create.ts"
"migration:create": "ts-node ./bin/migration-create.ts",
"buf:gen": "buf generate ./proto"
},
"dependencies": {
"@atproto/api": "workspace:^",
Expand All @@ -42,6 +43,9 @@
"@atproto/syntax": "workspace:^",
"@atproto/xrpc": "workspace:^",
"@atproto/xrpc-server": "workspace:^",
"@bufbuild/protobuf": "^1.5.0",
"@connectrpc/connect": "^1.1.4",
"@connectrpc/connect-node": "^1.1.4",
"@did-plc/lib": "^0.0.1",
"better-sqlite3": "^7.6.2",
"bytes": "^3.1.2",
Expand Down Expand Up @@ -77,6 +81,9 @@
"@atproto/bsky": "workspace:^",
"@atproto/dev-env": "workspace:^",
"@atproto/lex-cli": "workspace:^",
"@bufbuild/buf": "^1.28.1",
"@bufbuild/protoc-gen-es": "^1.5.0",
"@connectrpc/protoc-gen-connect-es": "^1.1.4",
"@did-plc/server": "^0.0.1",
"@types/cors": "^2.8.12",
"@types/disposable-email": "^0.2.0",
Expand Down
56 changes: 56 additions & 0 deletions packages/pds/proto/courier.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
syntax = "proto3";

package courier;
option go_package = "./;courier";

import "google/protobuf/struct.proto";
import "google/protobuf/timestamp.proto";

//
// Messages
//

// Ping
message PingRequest {}
message PingResponse {}

// Notifications

enum AppPlatform {
APP_PLATFORM_UNSPECIFIED = 0;
APP_PLATFORM_IOS = 1;
APP_PLATFORM_ANDROID = 2;
APP_PLATFORM_WEB = 3;
}

message Notification {
string id = 1;
string recipient_did = 2;
string title = 3;
string message = 4;
string collapse_key = 5;
bool always_deliver = 6;
google.protobuf.Timestamp timestamp = 7;
google.protobuf.Struct additional = 8;
}

message PushNotificationsRequest {
repeated Notification notifications = 1;
}

message PushNotificationsResponse {}

message RegisterDeviceTokenRequest {
string did = 1;
string token = 2;
string app_id = 3;
AppPlatform platform = 4;
}

message RegisterDeviceTokenResponse {}

service Service {
rpc Ping(PingRequest) returns (PingResponse);
rpc PushNotifications(PushNotificationsRequest) returns (PushNotificationsResponse);
rpc RegisterDeviceToken(RegisterDeviceTokenRequest) returns (RegisterDeviceTokenResponse);
}
41 changes: 41 additions & 0 deletions packages/pds/src/courier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Service } from './proto/courier_connect'
import {
Code,
ConnectError,
PromiseClient,
createPromiseClient,
Interceptor,
} from '@connectrpc/connect'
import {
createConnectTransport,
ConnectTransportOptions,
} from '@connectrpc/connect-node'

export type CourierClient = PromiseClient<typeof Service>

export const createCourierClient = (
opts: ConnectTransportOptions,
): CourierClient => {
const transport = createConnectTransport(opts)
return createPromiseClient(Service, transport)
}

export { Code }

export const isCourierError = (
err: unknown,
code?: Code,
): err is ConnectError => {
if (err instanceof ConnectError) {
return !code || err.code === code
}
return false
}

export const authWithApiKey =
(apiKey: string): Interceptor =>
(next) =>
(req) => {
req.header.set('authorization', `Bearer ${apiKey}`)
return next(req)
}
44 changes: 44 additions & 0 deletions packages/pds/src/proto/courier_connect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// @generated by protoc-gen-connect-es v1.3.0 with parameter "target=ts,import_extension=.ts"
// @generated from file courier.proto (package courier, syntax proto3)
/* eslint-disable */
// @ts-nocheck

import { PingRequest, PingResponse, PushNotificationsRequest, PushNotificationsResponse, RegisterDeviceTokenRequest, RegisterDeviceTokenResponse } from "./courier_pb.ts";
import { MethodKind } from "@bufbuild/protobuf";

/**
* @generated from service courier.Service
*/
export const Service = {
typeName: "courier.Service",
methods: {
/**
* @generated from rpc courier.Service.Ping
*/
ping: {
name: "Ping",
I: PingRequest,
O: PingResponse,
kind: MethodKind.Unary,
},
/**
* @generated from rpc courier.Service.PushNotifications
*/
pushNotifications: {
name: "PushNotifications",
I: PushNotificationsRequest,
O: PushNotificationsResponse,
kind: MethodKind.Unary,
},
/**
* @generated from rpc courier.Service.RegisterDeviceToken
*/
registerDeviceToken: {
name: "RegisterDeviceToken",
I: RegisterDeviceTokenRequest,
O: RegisterDeviceTokenResponse,
kind: MethodKind.Unary,
},
}
} as const;

Loading

0 comments on commit d79baff

Please sign in to comment.