Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Read receipt boilerplate #17

Merged
merged 6 commits into from
Jul 19, 2023
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
5 changes: 5 additions & 0 deletions .changeset/readReceiptChangeset.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@xmtp/content-type-read-receipt": major
---

added read receipt codec
10 changes: 10 additions & 0 deletions packages/content-type-read-receipt/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
root: true,
extends: ["custom"],
parserOptions: {
project: "./tsconfig.eslint.json",
},
rules: {
"class-methods-use-this": "off",
},
};
21 changes: 21 additions & 0 deletions packages/content-type-read-receipt/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 XMTP developers (xmtp.org)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions packages/content-type-read-receipt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## Coming soon
95 changes: 95 additions & 0 deletions packages/content-type-read-receipt/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
{
"name": "@xmtp/content-type-read-receipt",
"version": "1.0.0",
"description": "An XMTP content type to support read receipts",
"author": "XMTP Labs <[email protected]>",
"license": "MIT",
"type": "module",
"main": "dist/index.cjs",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"browser": "dist/web/index.js",
"exports": {
".": {
"types": "./dist/index.d.ts",
"browser": "./dist/web/index.js",
"import": "./dist/index.js",
"require": "./dist/index.cjs"
}
},
"sideEffects": false,
"repository": {
"type": "git",
"url": "[email protected]:xmtp/xmtp-js-content-types.git",
"directory": "packages/content-type-read-receipt"
},
"homepage": "https://github.com/xmtp/xmtp-js-content-types",
"bugs": {
"url": "https://github.com/xmtp/xmtp-js-content-types/issues"
},
"keywords": [
"xmtp",
"messaging",
"web3",
"js",
"ts",
"javascript",
"typescript",
"content-types"
],
"publishConfig": {
"access": "public"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 3 chrome versions",
"last 3 firefox versions",
"last 3 safari versions"
]
},
"scripts": {
"build:node": "tsup",
"build:web": "tsup --platform browser --target esnext",
"build": "yarn clean:build && yarn build:node && yarn build:web",
"clean:build": "rimraf dist",
"clean": "rimraf .turbo node_modules && yarn clean:build",
"dev": "tsup --watch",
"format:base": "prettier --ignore-path ../../.gitignore",
"format:check": "yarn format:base -c .",
"format": "yarn format:base -w .",
"generate:types": "tsup --dts-only",
"lint": "eslint . --ignore-path ../../.gitignore",
"test:jsdom": "NODE_TLS_REJECT_UNAUTHORIZED=0 vitest run --environment jsdom",
"test:node": "NODE_TLS_REJECT_UNAUTHORIZED=0 vitest run --environment node",
"test": "yarn test:node && yarn test:jsdom",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@xmtp/proto": "^3.26.0",
"@xmtp/xmtp-js": "^9.2.0"
},
"devDependencies": {
"@types/node": "^18.14.2",
"buffer": "^6.0.3",
"esbuild": "^0.18.2",
"esbuild-plugin-external-global": "^1.0.1",
"eslint": "^8.43.0",
"eslint-config-custom": "workspace:*",
"ethers": "^6.0.8",
"jsdom": "^22.1.0",
"prettier": "^2.8.8",
"rimraf": "^5.0.1",
"tsup": "^7.0.0",
"typescript": "^5.1.3",
"vite": "^4.3.9",
"vitest": "^0.32.2"
},
"peerDependencies": {
"@xmtp/xmtp-js": "^9.2.0"
}
}
50 changes: 50 additions & 0 deletions packages/content-type-read-receipt/src/ReadReceipt.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Wallet } from "ethers";
import { Client } from "@xmtp/xmtp-js";
import { ContentTypeReadReceipt, ReadReceiptCodec } from "./ReadReceipt";
import type { ReadReceipt } from "./ReadReceipt";

describe("ReadReceiptContentType", () => {
it("has the right content type", () => {
expect(ContentTypeReadReceipt.authorityId).toBe("xmtp.org");
expect(ContentTypeReadReceipt.typeId).toBe("readReceipt");
expect(ContentTypeReadReceipt.versionMajor).toBe(1);
expect(ContentTypeReadReceipt.versionMinor).toBe(0);
});

it("can send a read receipt", async () => {
const aliceWallet = Wallet.createRandom();
const aliceClient = await Client.create(aliceWallet, { env: "local" });
aliceClient.registerCodec(new ReadReceiptCodec());
await aliceClient.publishUserContact();

const bobWallet = Wallet.createRandom();
const bobClient = await Client.create(bobWallet, { env: "local" });
bobClient.registerCodec(new ReadReceiptCodec());
await bobClient.publishUserContact();

const conversation = await aliceClient.conversations.newConversation(
bobWallet.address,
);

const dateString = new Date().toISOString();

const readReceipt: ReadReceipt = {
timestamp: dateString,
};

await conversation.send(readReceipt, {
contentType: ContentTypeReadReceipt,
});

const bobConversation = await bobClient.conversations.newConversation(
aliceWallet.address,
);
const messages = await bobConversation.messages();

expect(messages.length).toBe(1);

const readReceiptMessage = messages[0];
const messageContent = readReceiptMessage.content as ReadReceipt;
expect(messageContent.timestamp).toBe(dateString);
});
});
42 changes: 42 additions & 0 deletions packages/content-type-read-receipt/src/ReadReceipt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { ContentTypeId } from "@xmtp/xmtp-js";
import type { ContentCodec, EncodedContent } from "@xmtp/xmtp-js";

export const ContentTypeReadReceipt = new ContentTypeId({
authorityId: "xmtp.org",
typeId: "readReceipt",
versionMajor: 1,
versionMinor: 0,
});

export type ReadReceipt = {
/**
* The timestamp the read receipt was sent, in ISO 8601 format
*/
timestamp: string;
};

export type ReadReceiptParameters = Pick<ReadReceipt, "timestamp">;

export class ReadReceiptCodec implements ContentCodec<ReadReceipt> {
get contentType(): ContentTypeId {
return ContentTypeReadReceipt;
}

encode(content: ReadReceipt): EncodedContent<ReadReceiptParameters> {
return {
type: ContentTypeReadReceipt,
parameters: {
timestamp: content.timestamp,
},
content: new Uint8Array(),
};
}

decode(content: EncodedContent<ReadReceiptParameters>): ReadReceipt {
const { timestamp } = content.parameters;

return {
timestamp,
};
}
}
2 changes: 2 additions & 0 deletions packages/content-type-read-receipt/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { ReadReceiptCodec, ContentTypeReadReceipt } from "./ReadReceipt";
export type { ReadReceipt, ReadReceiptParameters } from "./ReadReceipt";
5 changes: 5 additions & 0 deletions packages/content-type-read-receipt/tsconfig.eslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "./tsconfig.json",
"include": [".", ".eslintrc.cjs"],
"exclude": ["dist", "node_modules"]
}
5 changes: 5 additions & 0 deletions packages/content-type-read-receipt/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "tsconfig/build.json",
"include": ["."],
"exclude": ["dist", "node_modules"]
}
35 changes: 35 additions & 0 deletions packages/content-type-read-receipt/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { defineConfig } from "tsup";
import externalGlobal from "esbuild-plugin-external-global";
import type { Plugin } from "esbuild";

export default defineConfig((options) => {
const esbuildPlugins: Plugin[] = [];

// for the browser bundle, replace `crypto` import with an object that
// returns the browser's built-in crypto library
if (options.platform === "browser") {
esbuildPlugins.push(
externalGlobal.externalGlobalPlugin({
crypto: "{ webcrypto: window.crypto }",
}),
);
}

return {
entry: options.entry ?? ["src/index.ts"],
outDir:
options.outDir ?? (options.platform === "browser" ? "dist/web" : "dist"),
splitting: false,
sourcemap: true,
treeshake: true,
clean: true,
bundle: true,
platform: options.platform ?? "node",
minify: options.platform === "browser",
dts: options.platform !== "browser",
format:
options.format ??
(options.platform === "browser" ? ["esm"] : ["esm", "cjs"]),
esbuildPlugins,
};
});
8 changes: 8 additions & 0 deletions packages/content-type-read-receipt/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
globals: true,
setupFiles: "./vitest.setup.ts",
},
});
5 changes: 5 additions & 0 deletions packages/content-type-read-receipt/vitest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Buffer } from "buffer";
import { webcrypto } from "crypto";

globalThis.Buffer = Buffer;
globalThis.crypto = webcrypto as unknown as Crypto;
25 changes: 25 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1722,6 +1722,31 @@ __metadata:
languageName: unknown
linkType: soft

"@xmtp/content-type-read-receipt@workspace:packages/content-type-read-receipt":
version: 0.0.0-use.local
resolution: "@xmtp/content-type-read-receipt@workspace:packages/content-type-read-receipt"
dependencies:
"@types/node": ^18.14.2
"@xmtp/proto": ^3.26.0
"@xmtp/xmtp-js": ^9.2.0
buffer: ^6.0.3
esbuild: ^0.18.2
esbuild-plugin-external-global: ^1.0.1
eslint: ^8.43.0
eslint-config-custom: "workspace:*"
ethers: ^6.0.8
jsdom: ^22.1.0
prettier: ^2.8.8
rimraf: ^5.0.1
tsup: ^7.0.0
typescript: ^5.1.3
vite: ^4.3.9
vitest: ^0.32.2
peerDependencies:
"@xmtp/xmtp-js": ^9.2.0
languageName: unknown
linkType: soft

"@xmtp/content-type-remote-attachment@workspace:*, @xmtp/content-type-remote-attachment@workspace:packages/content-type-remote-attachment":
version: 0.0.0-use.local
resolution: "@xmtp/content-type-remote-attachment@workspace:packages/content-type-remote-attachment"
Expand Down