Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ declare class Sockudo {
triggerBatch(events: Array<Sockudo.BatchEvent>): Promise<Response>;

get(opts: Sockudo.GetOptions): Promise<Response>;
delete(opts: Sockudo.GetOptions): Promise<Response>;
channelHistory(
channel: string,
params?: Sockudo.HistoryParams,
Expand Down Expand Up @@ -50,6 +51,22 @@ declare class Sockudo {
messageSerial: string,
body: { data: string; [key: string]: unknown },
): Promise<Sockudo.MutationResponse>;
publishAnnotation(
channel: string,
messageSerial: string,
body: Sockudo.PublishAnnotationBody,
): Promise<Sockudo.PublishAnnotationResponse>;
deleteAnnotation(
channel: string,
messageSerial: string,
annotationSerial: string,
params?: { socket_id?: string },
): Promise<Sockudo.DeleteAnnotationResponse>;
listAnnotations(
channel: string,
messageSerial: string,
params?: Sockudo.AnnotationEventsParams,
): Promise<Sockudo.AnnotationEventsResponse>;
channelPresenceHistory(
channel: string,
params?: Sockudo.PresenceHistoryParams,
Expand Down Expand Up @@ -201,6 +218,55 @@ declare namespace Sockudo {
cursor?: string;
}

export interface PublishAnnotationBody {
type: string;
name?: string;
clientId?: string;
socketId?: string;
count?: number;
data?: any;
encoding?: string;
}

export interface PublishAnnotationResponse {
annotationSerial: string;
}

export interface DeleteAnnotationResponse {
annotationSerial: string;
deletedAnnotationSerial: string;
}

export interface AnnotationEventsParams extends Params {
type?: string;
from_serial?: string;
limit?: number;
socket_id?: string;
}

export interface AnnotationEvent {
action: "annotation.create" | "annotation.delete";
id?: string | null;
serial: string;
messageSerial: string;
type: string;
name?: string | null;
clientId?: string | null;
count?: number | null;
data?: any;
encoding?: string | null;
timestamp?: number | null;
}

export interface AnnotationEventsResponse {
channel: string;
messageSerial: string;
limit: number;
hasMore: boolean;
nextCursor?: string | null;
items: Array<AnnotationEvent>;
}

export type HistoryDirection = "newest_first" | "oldest_first";

export interface PresenceHistoryParams extends HistoryParams {
Expand Down
55 changes: 55 additions & 0 deletions src/sockudo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import Token = require("./token");
import WebHook = require("./webhook");
import type {
BatchEvent,
AnnotationEventsParams,
AnnotationEventsResponse,
ChannelAuthResponse,
DeleteAnnotationResponse,
GetMessageResponse,
GetOptions,
HistoryPage,
Expand All @@ -23,6 +26,8 @@ import type {
Options,
PostOptions,
PresenceChannelData,
PublishAnnotationBody,
PublishAnnotationResponse,
ResponseWithIdempotency,
SignedQueryStringOptions,
TriggerParams,
Expand Down Expand Up @@ -325,6 +330,13 @@ class Sockudo {
}) as Promise<ResponseWithIdempotency>;
}

delete(options: GetOptions): Promise<ResponseWithIdempotency> {
return requests.send(this.config, {
...options,
method: "DELETE",
}) as Promise<ResponseWithIdempotency>;
}

channelHistory(
channel: string,
params: GetOptions["params"] = {},
Expand Down Expand Up @@ -396,6 +408,49 @@ class Sockudo {
}).then((response) => response.json() as Promise<MutationResponse>);
}

publishAnnotation(
channel: string,
messageSerial: string,
body: PublishAnnotationBody,
): Promise<PublishAnnotationResponse> {
validateChannel(channel);
return this.post({
path: `/channels/${channel}/messages/${messageSerial}/annotations`,
body,
}).then(
(response) => response.json() as Promise<PublishAnnotationResponse>,
);
}

deleteAnnotation(
channel: string,
messageSerial: string,
annotationSerial: string,
params: { socket_id?: string } = {},
): Promise<DeleteAnnotationResponse> {
validateChannel(channel);
return this.delete({
path: `/channels/${channel}/messages/${messageSerial}/annotations/${annotationSerial}`,
params,
}).then(
(response) => response.json() as Promise<DeleteAnnotationResponse>,
);
}

listAnnotations(
channel: string,
messageSerial: string,
params: AnnotationEventsParams = {},
): Promise<AnnotationEventsResponse> {
validateChannel(channel);
return this.get({
path: `/channels/${channel}/messages/${messageSerial}/annotations`,
params,
}).then(
(response) => response.json() as Promise<AnnotationEventsResponse>,
);
}

channelPresenceHistory(
channel: string,
params: PresenceHistoryParams = {},
Expand Down
49 changes: 49 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,55 @@ export interface MessageVersionsParams extends RequestParams {
cursor?: string;
}

export interface PublishAnnotationBody {
type: string;
name?: string;
clientId?: string;
socketId?: string;
count?: number;
data?: unknown;
encoding?: string;
}

export interface PublishAnnotationResponse {
annotationSerial: string;
}

export interface DeleteAnnotationResponse {
annotationSerial: string;
deletedAnnotationSerial: string;
}

export interface AnnotationEventsParams extends RequestParams {
type?: string;
from_serial?: string;
limit?: number;
socket_id?: string;
}

export interface AnnotationEvent {
action: "annotation.create" | "annotation.delete";
id?: string | null;
serial: string;
messageSerial: string;
type: string;
name?: string | null;
clientId?: string | null;
count?: number | null;
data?: unknown;
encoding?: string | null;
timestamp?: number | null;
}

export interface AnnotationEventsResponse {
channel: string;
messageSerial: string;
limit: number;
hasMore: boolean;
nextCursor?: string | null;
items: AnnotationEvent[];
}

export interface PostOptions extends RequestOptions {
body: unknown;
}
Expand Down
42 changes: 42 additions & 0 deletions tests/integration/sockudo/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,48 @@ describe("Sockudo", function () {
});
});

describe("#listAnnotations", function () {
it("should call the annotation events endpoint with expected query params", function (done) {
nock("http://localhost")
.filteringPath(function (path) {
return path
.replace(/auth_timestamp=[0-9]+/, "auth_timestamp=X")
.replace(/auth_signature=[0-9a-f]{64}/, "auth_signature=Y");
})
.get(
"/apps/999/channels/chat:room-1/messages/msg:1/annotations?auth_key=111111&auth_timestamp=X&auth_version=1.0&cursor=abc&direction=oldest_first&limit=10&type=reaction%3Adistinct.v1&auth_signature=Y",
)
.reply(200, {
channel: "chat:room-1",
message_serial: "msg:1",
direction: "oldest_first",
limit: 10,
has_more: false,
items: [
{
annotation_serial: "ann:1",
type: "reaction:distinct.v1",
name: "like",
client_id: "alice",
},
],
});

sockudo
.listAnnotations("chat:room-1", "msg:1", {
limit: 10,
direction: "oldest_first",
cursor: "abc",
type: "reaction:distinct.v1",
})
.then((payload) => {
expect(payload.items[0].annotation_serial).to.equal("ann:1");
done();
})
.catch(done);
});
});

describe("#channelPresenceHistory", function () {
it("should call the presence history endpoint with expected query params", function (done) {
nock("http://localhost")
Expand Down
63 changes: 63 additions & 0 deletions tests/integration/sockudo/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,67 @@ describe("Sockudo", function () {
.catch(done);
});
});

describe("#publishAnnotation", function () {
it("should call the annotation publish endpoint", function (done) {
nock("http://localhost")
.filteringPath(function (path) {
return path
.replace(/auth_timestamp=[0-9]+/, "auth_timestamp=X")
.replace(/auth_signature=[0-9a-f]{64}/, "auth_signature=Y");
})
.post(
"/apps/10000/channels/chat:room-1/messages/msg:1/annotations?auth_key=aaaa&auth_timestamp=X&auth_version=1.0&body_md5=6212b152d0b9bfe76ea0cd5e73367410&auth_signature=Y",
'{"type":"reaction:distinct.v1","name":"like","client_id":"alice"}',
)
.reply(200, {
channel: "chat:room-1",
message_serial: "msg:1",
annotation_serial: "ann:1",
accepted: true,
});

sockudo
.publishAnnotation("chat:room-1", "msg:1", {
type: "reaction:distinct.v1",
name: "like",
client_id: "alice",
})
.then((payload) => {
expect(payload.annotation_serial).to.equal("ann:1");
done();
})
.catch(done);
});
});

describe("#deleteAnnotation", function () {
it("should call the annotation delete endpoint", function (done) {
nock("http://localhost")
.filteringPath(function (path) {
return path
.replace(/auth_timestamp=[0-9]+/, "auth_timestamp=X")
.replace(/auth_signature=[0-9a-f]{64}/, "auth_signature=Y");
})
.delete(
"/apps/10000/channels/chat:room-1/messages/msg:1/annotations/ann:1?auth_key=aaaa&auth_timestamp=X&auth_version=1.0&socket_id=123.456&auth_signature=Y",
)
.reply(200, {
channel: "chat:room-1",
message_serial: "msg:1",
annotation_serial: "ann:1",
deleted: true,
});

sockudo
.deleteAnnotation("chat:room-1", "msg:1", "ann:1", {
socket_id: "123.456",
})
.then((payload) => {
expect(payload.deleted).to.equal(true);
done();
})
.catch(done);
});
});
});
Loading