From 918677764d1ac62429d086f8ef836ee2a522c319 Mon Sep 17 00:00:00 2001 From: Ry Racherbaumer Date: Mon, 21 Oct 2024 15:37:39 -0500 Subject: [PATCH] Add content-type-group-updated --- .../content-type-group-updated/LICENSE | 21 +++++ .../content-type-group-updated/README.md | 39 ++++++++ .../content-type-group-updated/package.json | 89 +++++++++++++++++++ .../rollup.config.js | 58 ++++++++++++ .../src/GroupUpdated.test.ts | 22 +++++ .../src/GroupUpdated.ts | 43 +++++++++ .../content-type-group-updated/src/index.ts | 1 + .../content-type-group-updated/tsconfig.json | 4 + yarn.lock | 24 ++++- 9 files changed, 300 insertions(+), 1 deletion(-) create mode 100644 content-types/content-type-group-updated/LICENSE create mode 100644 content-types/content-type-group-updated/README.md create mode 100644 content-types/content-type-group-updated/package.json create mode 100644 content-types/content-type-group-updated/rollup.config.js create mode 100644 content-types/content-type-group-updated/src/GroupUpdated.test.ts create mode 100644 content-types/content-type-group-updated/src/GroupUpdated.ts create mode 100644 content-types/content-type-group-updated/src/index.ts create mode 100644 content-types/content-type-group-updated/tsconfig.json diff --git a/content-types/content-type-group-updated/LICENSE b/content-types/content-type-group-updated/LICENSE new file mode 100644 index 000000000..ae6695abd --- /dev/null +++ b/content-types/content-type-group-updated/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 XMTP (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. diff --git a/content-types/content-type-group-updated/README.md b/content-types/content-type-group-updated/README.md new file mode 100644 index 000000000..a0633d5ff --- /dev/null +++ b/content-types/content-type-group-updated/README.md @@ -0,0 +1,39 @@ +# Group updated content type + +This package provides an XMTP content type to support group updated messages. + +## Install the package + +```bash +# npm +npm i @xmtp/content-type-group-updated + +# yarn +yarn add @xmtp/content-type-group-updated + +# pnpm +pnpm i @xmtp/content-type-group-updated +``` + +## Notes + +This content type is included by default in official XMTP SDKs. + +## Developing + +Run `yarn dev` to build the content type and watch for changes, which will trigger a rebuild. + +## Testing + +Before running unit tests, start the required Docker container at the root of this repository. For more info, see [Running tests](../../README.md#running-tests). + +## Useful commands + +- `yarn build`: Builds the content type +- `yarn clean`: Removes `node_modules`, `dist`, and `.turbo` folders +- `yarn dev`: Builds the content type and watches for changes, which will trigger a rebuild +- `yarn lint`: Runs ESLint +- `yarn test:setup`: Starts a necessary docker container for testing +- `yarn test:teardown`: Stops docker container for testing +- `yarn test`: Runs all unit tests +- `yarn typecheck`: Runs `tsc` diff --git a/content-types/content-type-group-updated/package.json b/content-types/content-type-group-updated/package.json new file mode 100644 index 000000000..1ae275f47 --- /dev/null +++ b/content-types/content-type-group-updated/package.json @@ -0,0 +1,89 @@ +{ + "name": "@xmtp/content-type-group-updated", + "version": "1.0.0", + "description": "An XMTP content type to support group updated messages", + "keywords": [ + "xmtp", + "messaging", + "web3", + "js", + "ts", + "javascript", + "typescript", + "content-types" + ], + "homepage": "https://github.com/xmtp/xmtp-js", + "bugs": { + "url": "https://github.com/xmtp/xmtp-js/issues" + }, + "repository": { + "type": "git", + "url": "git+ssh://git@github.com/xmtp/xmtp-js.git", + "directory": "content-types/content-type-group-updated" + }, + "license": "MIT", + "author": "XMTP Labs ", + "sideEffects": false, + "type": "module", + "exports": { + ".": { + "types": "./dist/index.d.ts", + "browser": "./dist/browser/index.js", + "import": "./dist/index.js", + "require": "./dist/index.cjs" + } + }, + "main": "dist/index.cjs", + "module": "dist/index.js", + "browser": "dist/browser/index.js", + "types": "dist/index.d.ts", + "files": [ + "dist" + ], + "scripts": { + "build": "yarn clean:dist && yarn rollup -c", + "clean": "yarn clean:dist && rimraf .turbo node_modules", + "clean:dist": "rimraf dist", + "dev": "yarn clean:dist && yarn rollup -c --watch", + "test": "yarn test:node && yarn test:jsdom", + "test:jsdom": "NODE_TLS_REJECT_UNAUTHORIZED=0 vitest run --environment happy-dom", + "test:node": "NODE_TLS_REJECT_UNAUTHORIZED=0 vitest run --environment node", + "typecheck": "tsc --noEmit" + }, + "browserslist": { + "production": [ + ">0.2%", + "not dead", + "not op_mini all" + ], + "development": [ + "last 3 chrome versions", + "last 3 firefox versions", + "last 3 safari versions" + ] + }, + "dependencies": { + "@xmtp/content-type-primitives": "^1.0.1", + "@xmtp/proto": "^3.71.0" + }, + "devDependencies": { + "@rollup/plugin-terser": "^0.4.4", + "@rollup/plugin-typescript": "^12.1.1", + "@types/node": "^20.16.12", + "buffer": "^6.0.3", + "ethers": "^6.11.1", + "happy-dom": "^15.7.4", + "rimraf": "^6.0.1", + "rollup": "^4.24.0", + "rollup-plugin-dts": "^6.1.1", + "rollup-plugin-filesize": "^10.0.0", + "typescript": "^5.6.3", + "vite": "^5.4.9", + "vitest": "^2.1.3" + }, + "publishConfig": { + "access": "public", + "provenance": true, + "registry": "https://registry.npmjs.org/" + } +} diff --git a/content-types/content-type-group-updated/rollup.config.js b/content-types/content-type-group-updated/rollup.config.js new file mode 100644 index 000000000..5d4d78f91 --- /dev/null +++ b/content-types/content-type-group-updated/rollup.config.js @@ -0,0 +1,58 @@ +import terser from "@rollup/plugin-terser"; +import typescript from "@rollup/plugin-typescript"; +import { defineConfig } from "rollup"; +import { dts } from "rollup-plugin-dts"; +import filesize from "rollup-plugin-filesize"; + +const plugins = [ + typescript({ + declaration: false, + declarationMap: false, + }), + filesize({ + showMinifiedSize: false, + }), +]; + +const external = ["@xmtp/content-type-primitives", "@xmtp/proto"]; + +export default defineConfig([ + { + input: "src/index.ts", + output: { + file: "dist/index.js", + format: "es", + sourcemap: true, + }, + plugins, + external, + }, + { + input: "src/index.ts", + output: { + file: "dist/browser/index.js", + format: "es", + sourcemap: true, + }, + plugins: [...plugins, terser()], + external, + }, + { + input: "src/index.ts", + output: { + file: "dist/index.cjs", + format: "cjs", + sourcemap: true, + }, + plugins, + external, + }, + { + input: "src/index.ts", + output: { + file: "dist/index.d.ts", + format: "es", + }, + plugins: [dts()], + }, +]); diff --git a/content-types/content-type-group-updated/src/GroupUpdated.test.ts b/content-types/content-type-group-updated/src/GroupUpdated.test.ts new file mode 100644 index 000000000..fcb0df21d --- /dev/null +++ b/content-types/content-type-group-updated/src/GroupUpdated.test.ts @@ -0,0 +1,22 @@ +import { describe, expect, it } from "vitest"; +import { + ContentTypeGroupUpdated, + GroupUpdatedCodec, + type GroupUpdated, +} from "./GroupUpdated"; + +describe("ContentTypeGroupUpdated", () => { + it("can encode/decode group updated data", () => { + const groupUpdated: GroupUpdated = { + initiatedByInboxId: "inbox-id", + addedInboxes: [], + removedInboxes: [], + metadataFieldChanges: [], + }; + const codec = new GroupUpdatedCodec(); + const ec = codec.encode(groupUpdated); + expect(ec.type.sameAs(ContentTypeGroupUpdated)).toBe(true); + const groupUpdated2 = codec.decode(ec); + expect(groupUpdated2).toEqual(groupUpdated); + }); +}); diff --git a/content-types/content-type-group-updated/src/GroupUpdated.ts b/content-types/content-type-group-updated/src/GroupUpdated.ts new file mode 100644 index 000000000..fdf75bea0 --- /dev/null +++ b/content-types/content-type-group-updated/src/GroupUpdated.ts @@ -0,0 +1,43 @@ +import { + ContentTypeId, + type ContentCodec, + type EncodedContent, +} from "@xmtp/content-type-primitives"; +import { mlsTranscriptMessages } from "@xmtp/proto"; + +export const ContentTypeGroupUpdated = new ContentTypeId({ + authorityId: "xmtp.org", + typeId: "group_updated", + versionMajor: 1, + versionMinor: 0, +}); + +export type GroupUpdated = mlsTranscriptMessages.GroupUpdated; + +export class GroupUpdatedCodec + implements ContentCodec> +{ + get contentType(): ContentTypeId { + return ContentTypeGroupUpdated; + } + + encode(content: GroupUpdated) { + return { + type: this.contentType, + parameters: {}, + content: mlsTranscriptMessages.GroupUpdated.encode(content).finish(), + }; + } + + decode(content: EncodedContent) { + return mlsTranscriptMessages.GroupUpdated.decode(content.content); + } + + fallback(): undefined { + return undefined; + } + + shouldPush() { + return false; + } +} diff --git a/content-types/content-type-group-updated/src/index.ts b/content-types/content-type-group-updated/src/index.ts new file mode 100644 index 000000000..35c0221e8 --- /dev/null +++ b/content-types/content-type-group-updated/src/index.ts @@ -0,0 +1 @@ +export * from "./GroupUpdated"; diff --git a/content-types/content-type-group-updated/tsconfig.json b/content-types/content-type-group-updated/tsconfig.json new file mode 100644 index 000000000..f7dba01ea --- /dev/null +++ b/content-types/content-type-group-updated/tsconfig.json @@ -0,0 +1,4 @@ +{ + "extends": "tsconfig/build.json", + "include": ["src", "rollup.config.js"] +} diff --git a/yarn.lock b/yarn.lock index 26f3a0909..a508b6704 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2563,6 +2563,28 @@ __metadata: languageName: unknown linkType: soft +"@xmtp/content-type-group-updated@workspace:content-types/content-type-group-updated": + version: 0.0.0-use.local + resolution: "@xmtp/content-type-group-updated@workspace:content-types/content-type-group-updated" + dependencies: + "@rollup/plugin-terser": "npm:^0.4.4" + "@rollup/plugin-typescript": "npm:^12.1.1" + "@types/node": "npm:^20.16.12" + "@xmtp/content-type-primitives": "npm:^1.0.1" + "@xmtp/proto": "npm:^3.71.0" + buffer: "npm:^6.0.3" + ethers: "npm:^6.11.1" + happy-dom: "npm:^15.7.4" + rimraf: "npm:^6.0.1" + rollup: "npm:^4.24.0" + rollup-plugin-dts: "npm:^6.1.1" + rollup-plugin-filesize: "npm:^10.0.0" + typescript: "npm:^5.6.3" + vite: "npm:^5.4.9" + vitest: "npm:^2.1.3" + languageName: unknown + linkType: soft + "@xmtp/content-type-primitives@npm:^1.0.1, @xmtp/content-type-primitives@npm:^1.0.2, @xmtp/content-type-primitives@workspace:content-types/content-type-primitives": version: 0.0.0-use.local resolution: "@xmtp/content-type-primitives@workspace:content-types/content-type-primitives" @@ -2837,7 +2859,7 @@ __metadata: languageName: node linkType: hard -"@xmtp/proto@npm:^3.61.1, @xmtp/proto@npm:^3.62.1, @xmtp/proto@npm:^3.68.0": +"@xmtp/proto@npm:^3.61.1, @xmtp/proto@npm:^3.62.1, @xmtp/proto@npm:^3.68.0, @xmtp/proto@npm:^3.71.0": version: 3.71.0 resolution: "@xmtp/proto@npm:3.71.0" dependencies: