Skip to content

Commit

Permalink
Merge branch 'main' into hs/timestamp-api
Browse files Browse the repository at this point in the history
  • Loading branch information
Half-Shot authored Nov 22, 2023
2 parents 06e66df + ea8c166 commit 557b39c
Show file tree
Hide file tree
Showing 31 changed files with 1,130 additions and 738 deletions.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* turt2live
* @vector-im/bridges
4 changes: 2 additions & 2 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
node-version-file: .node-version
- run: yarn install
- run: yarn docs
- name: Build and deploy docs
uses: JamesIves/github-pages-deploy-action@v4
with:
branch: gh-pages
folder: .jsdoc/matrix-bot-sdk/develop
folder: .jsdoc/@vector-im/matrix-bot-sdk/develop
2 changes: 1 addition & 1 deletion .github/workflows/static_analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18' # Target desired node version
node-version-file: .node-version
- run: yarn install
- run: yarn lint

Expand Down
1 change: 1 addition & 0 deletions .node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
18
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
MIT License

Copyright (c) 2022 - 2023 New Vector Ltd
Copyright (c) 2018 - 2023 Travis Ralston

Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# matrix-bot-sdk

[![npm version](https://badge.fury.io/js/matrix-bot-sdk.svg)](https://www.npmjs.com/package/matrix-bot-sdk)
[![npm version](https://badge.fury.io/js/@vector-im%2Fmatrix-bot-sdk.svg)](https://www.npmjs.com/package/@vector-im/matrix-bot-sdk)

TypeScript/JavaScript SDK for Matrix bots. For help and support, visit [#matrix-bot-sdk:t2bot.io](https://matrix.to/#/#matrix-bot-sdk:t2bot.io)

Expand Down
6 changes: 3 additions & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![npm version](https://badge.fury.io/js/matrix-bot-sdk.svg)](https://www.npmjs.com/package/matrix-bot-sdk)
[![npm version](https://badge.fury.io/js/@vector-im%2Fmatrix-bot-sdk.svg)](https://www.npmjs.com/package/@vector-im/matrix-bot-sdk)

TypeScript/JavaScript SDK for Matrix bots. For help and support, visit [#matrix-bot-sdk:t2bot.io](https://matrix.to/#/#matrix-bot-sdk:t2bot.io)

Expand All @@ -11,7 +11,7 @@ TypeScript/JavaScript SDK for Matrix bots. For help and support, visit [#matrix-

## Installing

This package can be found on [npm](https://www.npmjs.com/package/matrix-bot-sdk):
This package can be found on [npm](https://www.npmjs.com/package/@vector-im/matrix-bot-sdk):
```
npm install matrix-bot-sdk
npm install @vector-im/matrix-bot-sdk
```
2 changes: 1 addition & 1 deletion examples/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const dmTarget = creds?.['dmTarget'] ?? "@admin:localhost";
const homeserverUrl = creds?.['homeserverUrl'] ?? "http://localhost:8008";
const accessToken = creds?.['accessToken'] ?? 'YOUR_TOKEN';
const storage = new SimpleFsStorageProvider("./examples/storage/bot.json");
const crypto = new RustSdkCryptoStorageProvider("./examples/storage/bot_sled", StoreType.Sled);
const crypto = new RustSdkCryptoStorageProvider("./examples/storage/bot_sqlite", StoreType.Sqlite);

const client = new MatrixClient(homeserverUrl, accessToken, storage, crypto);
AutojoinRoomsMixin.setupOnClient(client);
Expand Down
2 changes: 1 addition & 1 deletion examples/encryption_appservice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ try {
const dmTarget = creds?.['dmTarget'] ?? "@admin:localhost";
const homeserverUrl = creds?.['homeserverUrl'] ?? "http://localhost:8008";
const storage = new SimpleFsStorageProvider("./examples/storage/encryption_appservice.json");
const crypto = new RustSdkAppserviceCryptoStorageProvider("./examples/storage/encryption_appservice_sled", StoreType.Sled);
const crypto = new RustSdkAppserviceCryptoStorageProvider("./examples/storage/encryption_appservice_sqlite", StoreType.Sqlite);
const worksImage = fs.readFileSync("./examples/static/it-works.png");

const registration: IAppserviceRegistration = {
Expand Down
2 changes: 1 addition & 1 deletion examples/encryption_bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const dmTarget = creds?.['dmTarget'] ?? "@admin:localhost";
const homeserverUrl = creds?.['homeserverUrl'] ?? "http://localhost:8008";
const accessToken = creds?.['accessToken'] ?? 'YOUR_TOKEN';
const storage = new SimpleFsStorageProvider("./examples/storage/encryption_bot.json");
const crypto = new RustSdkCryptoStorageProvider("./examples/storage/encryption_bot_sled", StoreType.Sled);
const crypto = new RustSdkCryptoStorageProvider("./examples/storage/encryption_bot_sqlite", StoreType.Sqlite);
const worksImage = fs.readFileSync("./examples/static/it-works.png");

const client = new MatrixClient(homeserverUrl, accessToken, storage, crypto);
Expand Down
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
{
"name": "matrix-bot-sdk",
"name": "@vector-im/matrix-bot-sdk",
"version": "develop",
"description": "TypeScript/JavaScript SDK for Matrix bots and appservices",
"repository": {
"type": "git",
"url": "git+https://github.com/turt2live/matrix-bot-sdk.git"
"url": "git+https://github.com/vector-im/matrix-bot-sdk.git"
},
"author": "turt2live",
"author": "Element",
"license": "MIT",
"bugs": {
"url": "https://github.com/turt2live/matrix-bot-sdk/issues"
"url": "https://github.com/vector-im/matrix-bot-sdk/issues"
},
"homepage": "https://github.com/vector-im/matrix-bot-sdk#readme",
"publishConfig": {
"access": "public"
},
"homepage": "https://github.com/turt2live/matrix-bot-sdk#readme",
"scripts": {
"prepublishOnly": "yarn build",
"docs": "jsdoc -c jsdoc.json -P package.json -u docs/tutorials",
Expand Down Expand Up @@ -52,7 +55,7 @@
"tsconfig.json"
],
"dependencies": {
"@matrix-org/matrix-sdk-crypto-nodejs": "0.1.0-beta.6",
"@matrix-org/matrix-sdk-crypto-nodejs": "0.1.0-beta.11",
"@types/express": "^4.17.20",
"another-json": "^0.2.0",
"async-lock": "^1.4.0",
Expand Down
33 changes: 33 additions & 0 deletions scripts/fetch-remotes
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash
set -e

for REMOTE in $(git remote); do
URL=$(git remote get-url $REMOTE)
if [[ $URL =~ "turt2live" ]]; then
UPSTREAM_REPO=$REMOTE
elif [[ $URL =~ "vector-im" ]]; then
FORK_REPO=$REMOTE
fi
done

function echoAndDo {
echo "$*"
$*
}

if [[ -z $UPSTREAM_REPO ]]; then
echo -n 'Adding remote for upstream repo: '
UPSTREAM_REPO=turt2live
echoAndDo git remote add $UPSTREAM_REPO git@github.com:turt2live/matrix-bot-sdk.git
fi

if [[ -z $FORK_REPO ]]; then
echo -n 'Adding remote for fork repo: '
FORK_REPO=vector-im
echoAndDo git remote add $FORK_REPO git@github.com:vector-im/matrix-bot-sdk.git
fi

for REPO in $UPSTREAM_REPO $FORK_REPO; do
git fetch $REPO >/dev/null
git remote set-head $REPO -a >/dev/null
done
14 changes: 14 additions & 0 deletions scripts/prepare-patch-branch
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash
set -e

if [[ $# -lt 1 ]]; then
echo 'Please provide a title for your patch branch' >&2
exit 1
fi
PATCH_TITLE=$1

. $(dirname $0)/fetch-remotes

git checkout -b $PATCH_TITLE $(git merge-base $UPSTREAM_REPO $FORK_REPO)

echo "Branch '$PATCH_TITLE' is now ready. Push changes to this branch when preparing a PR, and aim to merge it to both upstream and the fork."
50 changes: 50 additions & 0 deletions scripts/tag-release
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash
set -e

if [[ -n $(git status --porcelain) ]]; then
echo 'Working dir is dirty, aborting' >&2
exit 1
fi

. $(dirname $0)/fetch-remotes

git checkout element-release
git reset --hard $FORK_REPO

PREID=element

# The latest upstream release tag reachable from the current commit
PREV_UPST_TAG=$(git log --decorate=short --decorate-refs=refs/tags/ --simplify-by-decoration --oneline | awk '/ \(tag: / && !/beta|element/ {sub(/)$/, "", $3); print $3; exit}')

# The commit hash of the retrieved tag (not of the tag itself)
PREV_UPST_TAG_HASH=$(git rev-parse ${PREV_UPST_TAG}~0)

# The immediate child commit of the release commit,
# to consider the 'Revert version back to "develop"' commits
PREV_UPST_NXT_HASH=$(git rev-list ${PREV_UPST_TAG}..${UPSTREAM_REPO} | tail -n 1)

# Check if the current branch is a direct merge of the previous upstream release
for MERGE_PARENT in $(git show -s | awk '/^Merge: / {print $2; print $3; exit}'); do
if [[ $PREV_UPST_TAG_HASH =~ ^$MERGE_PARENT || $PREV_UPST_NXT_HASH =~ ^$MERGE_PARENT ]]; then
RELEASE_MERGE=1
break
fi
done

if [[ $RELEASE_MERGE -eq 1 ]]; then
THIS_TAG="${PREV_UPST_TAG}-${PREID}"
THIS_VER=${THIS_TAG#v}
else
THIS_VER=$(npx semver --preid ${PREID} -i prerelease ${PREV_UPST_TAG#v})
while [[ -n $(git tag -l "v${THIS_VER}") ]]; do
THIS_VER=$(npx semver --preid ${PREID} -i prerelease $THIS_VER)
done
THIS_TAG="v${THIS_VER}"
fi

sed -i 's/\("version": "\).*\("\)/\1'$THIS_VER'\2/' package.json
git add package.json
git commit -m $THIS_TAG
git tag -sm $THIS_TAG{,}

echo "Tag '$THIS_TAG' is now ready and may be pushed"
81 changes: 81 additions & 0 deletions src/MatrixClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import { DMs } from "./DMs";
import { ServerVersions } from "./models/ServerVersions";
import { RoomCreateOptions } from "./models/CreateRoom";
import { PresenceState } from './models/events/PresenceEvent';
import { IKeyBackupInfo, IKeyBackupInfoRetrieved, IKeyBackupInfoUnsigned, IKeyBackupInfoUpdate, IKeyBackupVersion, KeyBackupVersion } from "./models/KeyBackup";
import { MatrixError } from "./models/MatrixError";

const SYNC_BACKOFF_MIN_MS = 5000;
const SYNC_BACKOFF_MAX_MS = 15000;
Expand Down Expand Up @@ -1980,6 +1982,85 @@ export class MatrixClient extends EventEmitter {
});
}

/**
* Get information about the latest room key backup version.
* @returns {Promise<IKeyBackupInfoRetrieved|null>} Resolves to the retrieved key backup info,
* or null if there is no existing backup.
*/
public async getKeyBackupVersion(): Promise<IKeyBackupInfoRetrieved|null> {
try {
return await this.doRequest("GET", "/_matrix/client/v3/room_keys/version");
} catch (e) {
if (e instanceof MatrixError && e.errcode === "M_NOT_FOUND") {
return null;
} else {
throw e;
}
}
}

/**
* Create a new room key backup.
* @param {IKeyBackupInfoUnsigned} info The properties of the key backup to create,
* with its auth_data left unsigned.
* @returns {Promise<IKeyBackupVersion>} Resolves to the version id of the new backup.
*/
@requiresCrypto()
public async signAndCreateKeyBackupVersion(info: IKeyBackupInfoUnsigned): Promise<IKeyBackupVersion> {
const data: IKeyBackupInfo = {
...info,
auth_data: {
...info.auth_data,
signatures: await this.crypto.sign(info),
},
};
return this.doRequest("POST", "/_matrix/client/v3/room_keys/version", null, data);
}

/**
* Update an existing room key backup.
* @param {KeyBackupVersion} version The key backup version to update.
* @param {IKeyBackupInfoUpdate} info The properties of the key backup to be applied.
* @returns {Promise<void>} Resolves when complete.
*/
@requiresCrypto()
public updateKeyBackupVersion(version: KeyBackupVersion, info: IKeyBackupInfoUpdate): Promise<void> {
const data = {
...info,
signatures: this.crypto.sign(info),
};
return this.doRequest("PUT", `/_matrix/client/v3/room_keys/version/${version}`, null, data);
}

/**
* Enable backing up of room keys.
* @param {IKeyBackupInfoRetrieved} info The configuration for key backup behaviour,
* as returned by {@link getKeyBackupVersion}.
* @returns {Promise<void>} Resolves when complete.
*/
@requiresCrypto()
public enableKeyBackup(info: IKeyBackupInfoRetrieved): Promise<void> {
return this.crypto.enableKeyBackup(info);
}

/**
* Disable backing up of room keys.
*/
public disableKeyBackup(): Promise<void> {
return this.crypto?.disableKeyBackup() ?? Promise.resolve();
}

/**
* Exports a set of keys for a given session.
* @param roomId The room ID for the session.
* @param sessionId The session ID.
* @returns An array of session keys.
*/
@requiresCrypto()
public exportRoomKeysForSession(roomId: string, sessionId: string) {
return this.crypto.exportRoomKeysForSession(roomId, sessionId);
}

/**
* Get relations for a given event.
* @param {string} roomId The room ID to for the given event.
Expand Down
Loading

0 comments on commit 557b39c

Please sign in to comment.