This repository has been archived by the owner on Oct 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from xmtp/dj/read-receipts
Read receipt boilerplate
- Loading branch information
Showing
14 changed files
with
309 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
--- | ||
"@xmtp/content-type-read-receipt": major | ||
--- | ||
|
||
added read receipt codec |
This file contains 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,10 @@ | ||
module.exports = { | ||
root: true, | ||
extends: ["custom"], | ||
parserOptions: { | ||
project: "./tsconfig.eslint.json", | ||
}, | ||
rules: { | ||
"class-methods-use-this": "off", | ||
}, | ||
}; |
This file contains 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,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. |
This file contains 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 @@ | ||
## Coming soon |
This file contains 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,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
50
packages/content-type-read-receipt/src/ReadReceipt.test.ts
This file contains 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,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); | ||
}); | ||
}); |
This file contains 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,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, | ||
}; | ||
} | ||
} |
This file contains 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,2 @@ | ||
export { ReadReceiptCodec, ContentTypeReadReceipt } from "./ReadReceipt"; | ||
export type { ReadReceipt, ReadReceiptParameters } from "./ReadReceipt"; |
This file contains 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,5 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"include": [".", ".eslintrc.cjs"], | ||
"exclude": ["dist", "node_modules"] | ||
} |
This file contains 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,5 @@ | ||
{ | ||
"extends": "tsconfig/build.json", | ||
"include": ["."], | ||
"exclude": ["dist", "node_modules"] | ||
} |
This file contains 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,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, | ||
}; | ||
}); |
This file contains 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,8 @@ | ||
import { defineConfig } from "vitest/config"; | ||
|
||
export default defineConfig({ | ||
test: { | ||
globals: true, | ||
setupFiles: "./vitest.setup.ts", | ||
}, | ||
}); |
This file contains 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,5 @@ | ||
import { Buffer } from "buffer"; | ||
import { webcrypto } from "crypto"; | ||
|
||
globalThis.Buffer = Buffer; | ||
globalThis.crypto = webcrypto as unknown as Crypto; |
This file contains 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