commit 9526dfa4f2c6853872133d90866b0bbaf1d8eb16 Author: PCoder Date: Mon Apr 18 14:27:08 2022 +0530 Initial commit version 0.8.13 diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..87ffb35 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,345 @@ +## [0.8.13] - 02nd Mar 2022 +- fix: send oldusername in displayname changed event +- fix: Dont encrypt reactions +- refactor: Make MatrixFile final and move all image calculation into isolate +- fix: own profile containing mxid +- chore: Update fluffybox + +## [0.8.12] - 02nd Mar 2022 +- fix: Rooms sort order after login +- fix: Change password using email authentication + +## [0.8.11] - 19nd Feb 2022 +- fix: Change password using email authentication + +## [0.8.10] - 19nd Feb 2022 +- chore: Increase default thumbnail size to 800 +- fix: sortRooms should be triggered right before onSync is called +- fix: UIA request stucks forever on unexpected matrixExceptions + +## [0.8.9] - 16nd Feb 2022 +- feat: Return homeserver summary on checkHomeserver +- fix: hasNewMessage true when last event is sent +- fix: Correctly end the call. + +## [0.8.8] - 15nd Feb 2022 +- fix: Has new messages compares ts +- fix: handle dynamic content for pinned events + +## [0.8.7] - 14nd Feb 2022 +- fix: Show reactions as last events and refactor hasNewMessage + +## [0.8.6] - 14nd Feb 2022 +- feat: Add hasNewMessages flag to room +- fix: Sort rooms after updating the UI on web + +## [0.8.5] - 14nd Feb 2022 +- fix: exception on removed widgets +- fix: Fix black screen when end screensharing with system buttons. + +## [0.8.4] - 08nd Feb 2022 +- fix: Call onChange correctly on redacted aggregation events + +## [0.8.3] - 07nd Feb 2022 +- fix: Remove onHistoryReceived which was broken anyway +- fix: Remove aggregation event doesnt trigger onChange + +## [0.8.2] - 04nd Feb 2022 +- fix: Add redaction events to timeline +- fix: Resize image with compute by using const class arguments + +## [0.8.1] - 03nd Feb 2022 +- refactor: Implement on history received in timeline +- fix: null-safety issues with widgets +- fix: Trigger onChange for index on aggregation event update +- feat: implement to get a room's widgets + +## [0.8.0] - 25nd Jan 2022 +- BREAKING CHANGE: high-level hadling of image sizes +- feat: expose Timeline.onChange to Room.getTimeline +- fix: Use where and map instead of a loop and a removeWhere +- fix: Remove sorts that shouldnt be required. + +## [0.7.3] - 14nd Jan 2022 +- fix: Fix turn credentials format issue for safari. +- fix: update fluffybox version to correctly clear rooms after logout. +- fix: Allow unpadded base64 decoding + +## [0.7.2] - 08nd Jan 2022 +- feat: Let sendDefaultMessage return false on encryption failure (Krille Fear) +- fix: Room Member updates should always be cached (Krille Fear) +- fix: Requested users are not stored (Christian Pauly) +- fix: Localize reactions (Krille Fear) +- refactor: Remove unnecessary type checks and imports (Krille Fear) + +## [0.7.1] - 08nd Dec 2021 +- fix: fallback in body for replies to replies (Nicolas Werner) +- fix: ignore 4xx errors when re-sending the to_device queue The to_device queue was introduced to ensure integrity if e.g. the server temporarily failed when attempting to send a to_device message. If, for whatever reason, the server responds with a 4xx error, though, then we want to ignore that to_device message from the queue and move on, as that means that something different was fundamentally wrong. This helps to fix the to_device queue clogging up, making clients incapable of sending to_device events anymore, should such clogging happen. (Sorunome) +- fix: Database corruptions by updating FluffyBox (Krille Fear) +- fix: Store the call state, fix the invite cannot be sent. (cloudwebrtc) +- fix: Allow consecutive edits for state events in-memory The lastEvent was incorrect when trying to process an edit of an edit. This fixes that by allowing consecutive edits for the last event. (Sorunome) +- fix: Only save state events from sync processing in-memory if needed If we dump all state events from sync into memory then we needlessly clog up our memory, potentially running out of ram. This is useless as when opening the timeline we post-load the unimportant state events anyways. So, this PR makes sure that only the state events of post-loaded rooms and important state events land in-memory when processing a sync request. (Sorunome) +- fix(ssss): Strip all whitespace characters from recovery keys upon decode Previously we stripped all spaces off of the recovery when decoding it, so that we could format the recovery key nicely. It turns out, however, that some element flavours also format with linebreaks, leading to the user having to manually remove them. We fix this by just stripping *all* whitespace off of the recovery key. (Sorunome) + +## [0.7.0] - 03nd Dec 2021 +- feat: Support for webRTC +- fix: Add missing calcDisplayname global rules to client constructor + +## [0.7.0-nullsafety.10] - 26nd Nov 2021 +- feat: Migrate olm sessions on database migration +- chore: Enable E2EE recovery by default + +## [0.7.0-nullsafety.9] - 25nd Nov 2021 +- fix: Limited timeline clean up on web +- fix: Remove account avatar + +## [0.7.0-nullsafety.8] - 24nd Nov 2021 +- chore: Update FluffyBox + +## [0.7.0-nullsafety.7] - 23nd Nov 2021 +- feat: Add commands to create chats +- feat: Add clear cache command +- feat: Implement new FluffyBox database API implementation +- fix: Workaround for a null exception for a non nullable boolean while user device key updating +- fix: Limited timeline clears too many events +- fix: Ability to remove avatar from room and account +- fix: Request history in archived rooms +- fix: Decrypt last event of a room +- refactor: Remove Sembast database implementation + +## [0.7.0-nullsafety.6] - 16nd Nov 2021 +- feat: Implement sembast store +- fix: HtmlToText crashes with an empty code block +- fix: use originServerTs to check if state event is old +- fix: Dont enable e2ee in new direct chats without encryption support +- fix: Change eventstatus of edits in prevEvent +- chore: Trim formatted username fallback + +## [0.7.0-nullsafety.5] - 10nd Nov 2021 +- fix: Edits as lastEvent do not update +- fix: JSON parsing in decryptRoomEvent method +- fix: Wrong null check in hive database +- fix: crash on invalid displaynames +- chore: Update matrix_api_lite + +## [0.7.0-nullsafety.4] - 09nd Nov 2021 +- feat: More advanced create chat methods (encryption is now enabled by default) +- feat: Make waiting on init db optional +- feat: Add more benchmarks for sync, timeline, init +- feat: Add onInsert, onRemove and onUpdate cb to timeline +- refactor: Move setreadmarker functionality to timeline + +## [0.7.0-nullsafety.3] - 05nd Nov 2021 +- fix: Null error in get own profile + +## [0.7.0-nullsafety.2] - 04nd Nov 2021 +- refactor: Make room in Event class not nullable +- refactor: download method should not return null + +## [0.7.0-nullsafety.1] - 04nd Nov 2021 +Prerelease of the null safety version of the SDK. +- feat: choose memberships returned by requestParticipants() +- refactor: Make SDK null safe +- fix: add room invite update to roomStateBox, so invites don't show empty room when app is restarted +- fix: Do not upload keys after logout +- fix: obay explicitly set ports in mxc URLs + +## [0.6.2] - 25nd Oct 2021 +- fix: Unnecessary null check +- fix: Auto update room states + +## [0.6.1] - 18nd Oct 2021 +- fix: Missing null check in a nested json map + +## [0.6.0] - 15nd Oct 2021 +- feat: Calc benchmarks for hive operations on init +- refactor: Change event status to enum +- refactor: Migrate more files to null safety +- fix: Type error when using CryptoKey in dart web +- fix: events with unknown users having invalid mxids This caused issues down the line where the sender id was assumed to be a valid matrix identifier +- fix: Sent events are sorted in SENDING timeline +- fix: use explicit type in fold instead of cast +- fix: apply review feedback +- fix: missing range check When requesting history the `start` parameter could become larger than the loaded events from the database were, resulting in an error when attempting to request history. +- fix: New verification requests on requesting history +- refactor: remove unused clientId +- fix: Add type checkings for User.displayName + +## [0.5.5] - 20nd Sep 2021 + fix: Autodetect mime type on file upload + +## [0.5.4] - 20nd Sep 2021 +- feat: Add waitForFirstSync parameter to init method + +## [0.5.3] - 19nd Sep 2021 +- feat: Add /discardsession command +- fix: Auto-reply key requests +- fix: Room previews not showing replies +- fix: missing content-type when changing avatar +- fix: only/number emotes in a reply + +## [0.5.2] - 14nd Sep 2021 +- fix: Delete box if it can not be cleared when calling database.clear() -> This should fix some box corruption problems +- fix: Do not set old events as state events -> This should fix the room list sort ordering bug + +## [0.5.1] - 13nd Sep 2021 +- fix: Room.notificationCount set to null sometimes + +## [0.5.0] - 13nd Sep 2021 +- hotfix: Key sharing security vulnerability! -> Please upgrade as soon as possible to this version +- feat: MSC2746: Improved Signalling for 1:1 VoIP +- fix: Get direct chat from user ID method crashes on more than one DM rooms with one account +- fix: compilation against newer matrix_api_lite +- refactor: Remove onRoomUpdate stream + +## [0.4.3] - 8nd Sep 2021 +- fix: Do not handle sending event updates which are already synced + +## [0.4.2] - 6nd Sep 2021 +- revert: Make bytes in EncryptedFile nullable + +## [0.4.1] - 6nd Sep 2021 +- fix: Make bytes in EncryptedFile nullable + +## [0.4.0] - 3nd Sep 2021 +- fix: Check if database got disposed in keyManager +- fix: Implement dummy transactions for hive +- fix: room account data key/type returned encoded +- fix: Missing null check +- fix: uiaRequests send broken auth object at first try +- fix: Requesting history being funky +- fix: Don't lag when sending messages in big rooms +- feat: Do not load all timeline events from store at once +- feat: Pin invited rooms +- refactor: Replace all logic regarding sortOrder +- refactor: Workarounds for missing mHeroes in rooms + +## [0.3.6] - 30nd Aug 2021 +- hotfix: uiaRequests send broken auth object at first try + +## [0.3.5] - 28nd Aug 2021 +- hotfix: Send unencrypted thumbnails + +## [0.3.4] - 28nd Aug 2021 +- fix: String.parseIdentifierIntoParts not working with unicode matrix.to links + Some clients do not uri-encode the identifier for matrix.to links, so we must + handle if we can't uri-decode them +- fix: missing null check in hideEdit condition +- fix: missing null check + It seems `device_keys` in the reply of `/keys/query` is not required. While synapse always + sent it, conduit did not, which resulted in an error. + +## [0.3.3] - 20nd Aug 2021 +- fix: room.lastEvent order now respects edits +- feat: use m.new_content in lastEvent (so no more * fallback) + +## [0.3.2] - 20nd Aug 2021 +- feat: cache archived rooms to access them with `getRoomById` +- fix: requestHistory() for archived rooms +- refactor: Change name of archive getter to function + +## [0.3.1] - 20nd Aug 2021 +- hotfix: Opt-out null safety for crypto files because of an error in web + +## [0.3.0] - 20nd Aug 2021 +- remove: deprecated moor database (breaking change) +- feat(events): add plain-text body representation from HTML +- feat: get new_content in getLocalizedBody +- feat: Add a way to get a verification request by its transaction id + A client might find the need to get the verification request object by + its transaction id, to be able to e.g. display for in-room verification + an "accept verification request" button easily. +- fix: Correctly parse the reason of a spoiler + Previously only the first child node of a spoiler was considered to + determine if there should be a spoiler reason. This was, unfortunately, + incorrect, as soon as e.g. the reason had more than one space. This is + fixed by properly iterating all child nodes to search for the reason. +- fix: Add space states to important events + We need the space state events in the important events to be able to + differentiate rooms and spaces in the room list. +- feat: Allow specifying extraContent for Room.sendFileEvent, in case clients want to specify some custom stuff +- fix: toDouble was called on null when you had a pinned room +- fix: Typo in key backup requests + This may lead to messages not decrypting after interactive verification, + which would make the user manually press the request keys button. +- refactor: rename LoginState.logged to loggedIn + +## [0.2.1] - 2nd Aug 2021 + +- fix: accidental OTK uploads on fakeSync calls + +## [0.2.0] - 27th Jul 2021 + +- Breaking API changes duo to use of code generated matrix API +- fix: Missing null checks in syncUpdate handling + +## [0.1.11] - 26th Jul 2021 + +- fix: Upload OTKs if the otk_count field is missing + +## [0.1.10] - 21th Jul 2021 + +Please note: This removes the isolate code from the SDK to make it compatible with dart web. If +you still want the SDK to execute code in the background to not block the UI on key generation +for example, pass the `compute` method from Flutter to your client: + +```dart +// ... +final client = Client('name...', + // ... + compute: compute, +); +``` + +## [0.1.9] - 20th Jul 2021 + +- fix: Add missing null check which made bootstrap fail for newest Synapse release + +## [0.1.8] - 18th Jul 2021 + +- fix: Provide a reasonable well-known fallback +- fix: Add locking to sending encrypted to_device messages to prevent potential race conditions +- fix: preserve homeserver port when creating thumbnail URIs +- feat: Add support for nicer mentions +- feat: Add general image pack handling as per MSC2545 + +## [0.1.7] - 10 Jul 2021 + +- change: Hive database schema (will trigger a database migration) +- fix: Dont migrate database from version null +- fix: Adjust emoji ranges to have less false positives +- fix: Sending of the to_device key + +## [0.1.6] - 06 Jul 2021 + +- feat: Make it possible to get the current loginState +- fix: Broken nested accountData content maps +- fix: Mark unsent events as failed +- fix: Pin moor to 4.3.2 to fix the CI errors + +## [0.1.5] - 26 Jun 2021 + +- fix: Don't run syncs while the client is being initialized + +## [0.1.4] - 19 Jun 2021 + +- change: Replace onSyncError Stream with onSyncStatus + +## [0.1.3] - 19 Jun 2021 + +- feat: Implement migration for hive schema versions + +## [0.1.2] - 19 Jun 2021 + +- fix: Hive breaks if room IDs contain emojis (yes there are users with hacked synapses out there who needs this) +- feat: Also migrate inbound group sessions + +## [0.1.1] - 18 Jun 2021 + +- refactor: Move pedantic to dev_dependencies +- chore: Update readme +- fix: Migrate missing device keys + +## [0.1.0] - 17 Jun 2021 + +First stable version diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..cf862c0 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,104 @@ +# Contributing code to Famedly + +*See also: Code of Conduct* + +We look forward to you joining our team. Everyone is welcome to contribute code via pull requests or to file issues on Gitlab or help other peoples. We communicate primarily over Gitlab and on chat channels. You should be willing to license your contributions under the same license as the project itself. + +# How to contribute +The only way to contribute changes to our project is to create a new branch or to fork it on Gitlab. Then create a merge request to ask us to merge your changes into the main branch of our repository. (https://docs.gitlab.com/ee/gitlab-basics/add-merge-request.html) + +**The main branch is our development branch where all the work happens.** + +## Merge request workflow in detail +- Create a new branch or fork the main branch of the project (Please follow the guidlines below of naming branch and commits) +- Make a merge request to merge your changes into the main branch +- We use the Gitlab merge request workflow to review your contribution + - Only merge requests with a working CI can be merged + - Only merge requests with at least one code reviewer of the core team can be merged + - Only merge requests which are signed-off can be merged +- Merge Requests may be refused if they don't follow the rules below. + +**Never ever just push something directly to the main branch!** + +## Naming guidelines & code style + +### Create a branch + +- Branches should get named by this pattern: `username/name-your-changes`. + +*That means for example: "alice/fix-this-bug".* + +- Use [Conventional Commits](https://www.conventionalcommits.org/) + +### File structure: +- File names must be `snake_case`. +- Directories need to be lowercase. + +### Code style: +- We recommend using Android Studio or VS Code for coding +- Follow the common Dart style in: https://dart.dev/guides/language/effective-dart/style +- Format the code with `flutter format lib` - otherwise the CI will fail + +### Code quality +- Don't repeat yourself! Use local variables, functions, classes. +- Don't mix UI and business logic in the same environment. +- Write tests for new classes, functions and widgets. +- Keep it simple stupid: https://en.wikipedia.org/wiki/KISS_principle +- Describe all of your classes, methods and attributes using **dartdoc** comments. Read this for more information: https://dart.dev/guides/language/effective-dart/documentation +- Don't write functions to create new widgets. Write classes! +- Don't insert unlocalized strings! +- Use Dart extensions to extend class functionality instead of writing wrapper classes! +- Don't mix functions which changes the state of something (functions with a return type of `void` or `Future`) and functional programming which doesn't. + +## Sign off +In order to have a concrete record that your contribution is intentional and you agree to license it under the same terms as the project's license, we've adopted the same lightweight approach that [the Linux Kernel](https://www.kernel.org/doc/Documentation/SubmittingPatches), [Docker](https://github.com/docker/docker/blob/master/CONTRIBUTING.md), and many other projects use: the [**DCO - Developer Certificate of Origin**](http://developercertificate.org/). This is a simple declaration that you wrote the contribution or otherwise have the right to contribute it to Matrix: + +``` +Developer Certificate of Origin +Version 1.1 + +Copyright (C) 2004, 2006 The Linux Foundation and its contributors. +660 York Street, Suite 102, +San Francisco, CA 94110 USA + +Everyone is permitted to copy and distribute verbatim copies of this +license document, but changing it is not allowed. + +Developer's Certificate of Origin 1.1 + +By making a contribution to this project, I certify that: + +(a) The contribution was created in whole or in part by me and I + have the right to submit it under the open source license + indicated in the file; or + +(b) The contribution is based upon previous work that, to the best + of my knowledge, is covered under an appropriate open source + license and I have the right under that license to submit that + work with modifications, whether created in whole or in part + by me, under the same open source license (unless I am + permitted to submit under a different license), as indicated + in the file; or + +(c) The contribution was provided directly to me by some other + person who certified (a), (b) or (c) and I have not modified + it. + +(d) I understand and agree that this project and the contribution + are public and that a record of the contribution (including all + personal information I submit with it, including my sign-off) is + maintained indefinitely and may be redistributed consistent with + this project or the open source license(s) involved. +``` + +If you agree to this for your contribution, then all that's needed is to include the line in your commit or merge request comment: + +`Signed-off-by: Your Name ` + +We accept contributions under a legally identifiable name, such as your name on government documentation or common-law names (names claimed by legitimate usage or repute). Unfortunately, we cannot accept anonymous contributions at this time. + +Git allows you to add this signoff automatically when using the `-s` flag to `git commit`, which uses the name and email set in your `user.name` and `user.email` git configs. + +If you forgot to sign off your commits before making your pull request and are on Git 2.17+ you can mass signoff using rebase: + +`git rebase --signoff origin/main` diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..096ad99 --- /dev/null +++ b/LICENSE @@ -0,0 +1,661 @@ + GNU AFFERO GENERAL PUBLIC LICENSE + Version 3, 19 November 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU Affero General Public License is a free, copyleft license for +software and other kinds of works, specifically designed to ensure +cooperation with the community in the case of network server software. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +our General Public Licenses are intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + Developers that use our General Public Licenses protect your rights +with two steps: (1) assert copyright on the software, and (2) offer +you this License which gives you legal permission to copy, distribute +and/or modify the software. + + A secondary benefit of defending all users' freedom is that +improvements made in alternate versions of the program, if they +receive widespread use, become available for other developers to +incorporate. Many developers of free software are heartened and +encouraged by the resulting cooperation. However, in the case of +software used on network servers, this result may fail to come about. +The GNU General Public License permits making a modified version and +letting the public access it on a server without ever releasing its +source code to the public. + + The GNU Affero General Public License is designed specifically to +ensure that, in such cases, the modified source code becomes available +to the community. It requires the operator of a network server to +provide the source code of the modified version running there to the +users of that server. Therefore, public use of a modified version, on +a publicly accessible server, gives the public access to the source +code of the modified version. + + An older license, called the Affero General Public License and +published by Affero, was designed to accomplish similar goals. This is +a different license, not a version of the Affero GPL, but Affero has +released a new version of the Affero GPL which permits relicensing under +this license. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU Affero General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Remote Network Interaction; Use with the GNU General Public License. + + Notwithstanding any other provision of this License, if you modify the +Program, your modified version must prominently offer all users +interacting with it remotely through a computer network (if your version +supports such interaction) an opportunity to receive the Corresponding +Source of your version by providing access to the Corresponding Source +from a network server at no charge, through some standard or customary +means of facilitating copying of software. This Corresponding Source +shall include the Corresponding Source for any work covered by version 3 +of the GNU General Public License that is incorporated pursuant to the +following paragraph. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the work with which it is combined will remain governed by version +3 of the GNU General Public License. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU Affero General Public License from time to time. Such new versions +will be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU Affero General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU Affero General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU Affero General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + famedlySDK + Copyright (C) 2019 famedly + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If your software can interact with users remotely through a computer +network, you should also make sure that it provides a way for users to +get its source. For example, if your program is a web application, its +interface could display a "Source" link that leads users to an archive +of the code. There are many ways you could offer source, and different +solutions will be better for different programs; see section 13 for the +specific requirements. + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU AGPL, see +. diff --git a/README.md b/README.md new file mode 100644 index 0000000..a6c0971 --- /dev/null +++ b/README.md @@ -0,0 +1,74 @@ +# Matrix SDK + +Matrix (matrix.org) SDK written in dart. + +## Native libraries + +For E2EE, libolm must be provided. + +Additionally, OpenSSL (libcrypto) must be provided on native platforms for E2EE. + +For flutter apps you can easily import it with the [flutter_olm](https://pub.dev/packages/flutter_olm) and the [flutter_openssl_crypto](https://pub.dev/packages/flutter_openssl_crypto) packages. + +## How to use this + +1. Import the sdk + +```yaml + matrix: + # Optional: + flutter_olm: + flutter_openssl_crypto: +``` + +```dart +import 'package:matrix/matrix.dart'; +``` + +2. Create a new client: + +```dart +final client = Client("HappyChat"); +``` + +The SDK works better with a database. Otherwise it has no persistence. For this you need to provide a databaseBuilder like this: + +```dart +final client = Client( + "HappyChat", + databaseBuilder: (Client client) async { + await Hive.init('/path/to/your/storage'); + final db = FamedlySdkHiveDatabase(client.clientName); + await db.open(); + return db; + }, +); +``` + +3. Connect to a Matrix Homeserver and listen to the streams: + +```dart +client.onLoginStateChanged.stream.listen((bool loginState){ + print("LoginState: ${loginState.toString()}"); +}); + +client.onEvent.stream.listen((EventUpdate eventUpdate){ + print("New event update!"); +}); + +client.onRoomUpdate.stream.listen((RoomUpdate eventUpdate){ + print("New room update!"); +}); + +await client.checkHomeserver("https://yourhomeserver.abc"); +await client.login( + identifier: AuthenticationUserIdentifier(user: 'alice'), + password: '123456', +); +``` + +4. Send a message to a Room: + +```dart +await client.getRoomById('your_room_id').sendTextEvent('Hello world'); +``` diff --git a/analysis_options.yaml b/analysis_options.yaml new file mode 100644 index 0000000..c96f3b0 --- /dev/null +++ b/analysis_options.yaml @@ -0,0 +1,50 @@ +include: package:pedantic/analysis_options.yaml + +linter: + rules: + - camel_case_types + - avoid_print + - constant_identifier_names + - prefer_final_locals + - prefer_final_in_for_each + +analyzer: + errors: + todo: ignore + exclude: + - example/main.dart + # needed until crypto packages upgrade + - lib/src/database/database.g.dart + plugins: + # Show code metrics in IDE + # - dart_code_metrics + +dart_code_metrics: + metrics: + cyclomatic-complexity: 20 + number-of-arguments: 4 + maximum-nesting-level: 5 + source-lines-of-code: 50 + # This is still unstable, so handle with care + maintainability-index: 40 + metrics-exclude: + - test/** + rules: + #- newline-before-return + - no-boolean-literal-compare + - no-empty-block + #- prefer-trailing-comma + - prefer-conditional-expressions + - no-equal-arguments + - no-equal-then-else + - no-magic-number: + severity: style # too noisy otherwise atm + - avoid-late-keyword: + severity: style # too noisy otherwise atm + - avoid-non-null-assertion: + severity: style # too noisy otherwise atm + - avoid-unused-parameters + - binary-expression-operand-order + anti-patterns: + - long-method + - long-parameter-list diff --git a/build.yaml b/build.yaml new file mode 100644 index 0000000..b07425b --- /dev/null +++ b/build.yaml @@ -0,0 +1,6 @@ +targets: + $default: + builders: + moor_generator: + options: + generate_connect_constructor: true diff --git a/example/main.dart b/example/main.dart new file mode 100644 index 0000000..19b09e8 --- /dev/null +++ b/example/main.dart @@ -0,0 +1,377 @@ +import 'package:flutter/material.dart'; +import 'package:matrix/matrix.dart'; +import 'package:path_provider/path_provider.dart'; +import 'package:provider/provider.dart'; + +void main() async { + WidgetsFlutterBinding.ensureInitialized(); + + final client = Client( + 'Matrix Example Chat', + databaseBuilder: (_) async { + final dir = await getApplicationSupportDirectory(); + final db = FluffyBoxDatabase('matrix_example_chat', dir.path); + await db.open(); + return db; + }, + ); + await client.init(); + runApp(MatrixExampleChat(client: client)); +} + +class MatrixExampleChat extends StatelessWidget { + final Client client; + const MatrixExampleChat({required this.client, Key? key}) : super(key: key); + + @override + Widget build(BuildContext context) { + return MaterialApp( + title: 'Matrix Example Chat', + builder: (context, child) => Provider( + create: (context) => client, + child: child, + ), + home: client.isLogged() ? const RoomListPage() : const LoginPage(), + ); + } +} + +class LoginPage extends StatefulWidget { + const LoginPage({Key? key}) : super(key: key); + + @override + _LoginPageState createState() => _LoginPageState(); +} + +class _LoginPageState extends State { + final TextEditingController _homeserverTextField = TextEditingController( + text: 'matrix.org', + ); + final TextEditingController _usernameTextField = TextEditingController(); + final TextEditingController _passwordTextField = TextEditingController(); + + bool _loading = false; + + void _login() async { + setState(() { + _loading = true; + }); + + try { + final client = Provider.of(context, listen: false); + await client + .checkHomeserver(Uri.https(_homeserverTextField.text.trim(), '')); + await client.login( + LoginType.mLoginPassword, + password: _passwordTextField.text, + identifier: AuthenticationUserIdentifier(user: _usernameTextField.text), + ); + Navigator.of(context).pushAndRemoveUntil( + MaterialPageRoute(builder: (_) => const RoomListPage()), + (route) => false, + ); + } catch (e) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text(e.toString()), + ), + ); + setState(() { + _loading = false; + }); + } + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar(title: const Text('Login')), + body: Padding( + padding: const EdgeInsets.all(16.0), + child: Column( + children: [ + TextField( + controller: _homeserverTextField, + readOnly: _loading, + autocorrect: false, + decoration: const InputDecoration( + prefixText: 'https://', + border: OutlineInputBorder(), + labelText: 'Homeserver', + ), + ), + const SizedBox(height: 16), + TextField( + controller: _usernameTextField, + readOnly: _loading, + autocorrect: false, + decoration: const InputDecoration( + border: OutlineInputBorder(), + labelText: 'Username', + ), + ), + const SizedBox(height: 16), + TextField( + controller: _passwordTextField, + readOnly: _loading, + autocorrect: false, + obscureText: true, + decoration: const InputDecoration( + border: OutlineInputBorder(), + labelText: 'Password', + ), + ), + const SizedBox(height: 16), + SizedBox( + width: double.infinity, + child: ElevatedButton( + onPressed: _loading ? null : _login, + child: _loading + ? const LinearProgressIndicator() + : const Text('Login'), + ), + ), + ], + ), + ), + ); + } +} + +class RoomListPage extends StatefulWidget { + const RoomListPage({Key? key}) : super(key: key); + + @override + _RoomListPageState createState() => _RoomListPageState(); +} + +class _RoomListPageState extends State { + void _logout() async { + final client = Provider.of(context, listen: false); + await client.logout(); + Navigator.of(context).pushAndRemoveUntil( + MaterialPageRoute(builder: (_) => const LoginPage()), + (route) => false, + ); + } + + void _join(Room room) async { + if (room.membership != Membership.join) { + await room.join(); + } + Navigator.of(context).push( + MaterialPageRoute( + builder: (_) => RoomPage(room: room), + ), + ); + } + + @override + Widget build(BuildContext context) { + final client = Provider.of(context, listen: false); + return Scaffold( + appBar: AppBar( + title: const Text('Chats'), + actions: [ + IconButton( + icon: const Icon(Icons.logout), + onPressed: _logout, + ), + ], + ), + body: StreamBuilder( + stream: client.onSync.stream, + builder: (context, _) => ListView.builder( + itemCount: client.rooms.length, + itemBuilder: (context, i) => ListTile( + leading: CircleAvatar( + foregroundImage: client.rooms[i].avatar == null + ? null + : NetworkImage(client.rooms[i].avatar! + .getThumbnail( + client, + width: 56, + height: 56, + ) + .toString()), + ), + title: Row( + children: [ + Expanded(child: Text(client.rooms[i].displayname)), + if (client.rooms[i].notificationCount > 0) + Material( + borderRadius: BorderRadius.circular(99), + color: Colors.red, + child: Padding( + padding: const EdgeInsets.all(2.0), + child: + Text(client.rooms[i].notificationCount.toString()), + )) + ], + ), + subtitle: Text( + client.rooms[i].lastEvent?.body ?? 'No messages', + maxLines: 1, + ), + onTap: () => _join(client.rooms[i]), + ), + ), + ), + ); + } +} + +class RoomPage extends StatefulWidget { + final Room room; + const RoomPage({required this.room, Key? key}) : super(key: key); + + @override + _RoomPageState createState() => _RoomPageState(); +} + +class _RoomPageState extends State { + late final Future _timelineFuture; + final GlobalKey _listKey = GlobalKey(); + int _count = 0; + + @override + void initState() { + _timelineFuture = widget.room.getTimeline(onChange: (i) { + print('on change! $i'); + _listKey.currentState?.setState(() {}); + }, onInsert: (i) { + print('on insert! $i'); + _listKey.currentState?.insertItem(i); + _count++; + }, onRemove: (i) { + print('On remove $i'); + _count--; + _listKey.currentState?.removeItem(i, (_, __) => const ListTile()); + }, onHistoryReceived: (count) { + print('On History Received $count'); + for (var i = 0; i < count; i++) { + _listKey.currentState?.insertItem(_count + i); + } + _count += count; + }); + super.initState(); + } + + final TextEditingController _sendController = TextEditingController(); + + void _send() { + widget.room.sendTextEvent(_sendController.text.trim()); + _sendController.clear(); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: Text(widget.room.displayname), + ), + body: SafeArea( + child: Column( + children: [ + Expanded( + child: FutureBuilder( + future: _timelineFuture, + builder: (context, snapshot) { + final timeline = snapshot.data; + if (timeline == null) { + return const Center( + child: CircularProgressIndicator.adaptive(), + ); + } + _count = timeline.events.length; + return Column( + children: [ + Center( + child: TextButton( + onPressed: timeline.requestHistory, + child: const Text('Load more...')), + ), + const Divider(height: 1), + Expanded( + child: AnimatedList( + key: _listKey, + reverse: true, + initialItemCount: timeline.events.length, + itemBuilder: (context, i, animation) => timeline + .events[i].relationshipEventId != + null + ? Container() + : ScaleTransition( + scale: animation, + child: Opacity( + opacity: timeline.events[i].status.isSent + ? 1 + : 0.5, + child: ListTile( + leading: CircleAvatar( + foregroundImage: timeline.events[i] + .sender.avatarUrl == + null + ? null + : NetworkImage(timeline + .events[i].sender.avatarUrl! + .getThumbnail( + widget.room.client, + width: 56, + height: 56, + ) + .toString()), + ), + title: Row( + children: [ + Expanded( + child: Text(timeline + .events[i].sender + .calcDisplayname()), + ), + Text( + timeline.events[i].originServerTs + .toIso8601String(), + style: + const TextStyle(fontSize: 10), + ), + ], + ), + subtitle: Text(timeline.events[i] + .getDisplayEvent(timeline) + .body), + ), + ), + ), + ), + ), + ], + ); + }, + ), + ), + const Divider(height: 1), + Padding( + padding: const EdgeInsets.symmetric(horizontal: 16.0), + child: Row( + children: [ + Expanded( + child: TextField( + controller: _sendController, + decoration: const InputDecoration( + hintText: 'Send message', + ), + )), + IconButton( + icon: const Icon(Icons.send_outlined), + onPressed: _send, + ), + ], + ), + ), + ], + ), + ), + ); + } +} diff --git a/home/index.html b/home/index.html new file mode 100644 index 0000000..68c9a5a --- /dev/null +++ b/home/index.html @@ -0,0 +1,12 @@ + + + + + + Famedly Matrix SDK + + +API +Documentation + + diff --git a/lib/encryption.dart b/lib/encryption.dart new file mode 100644 index 0000000..f534f83 --- /dev/null +++ b/lib/encryption.dart @@ -0,0 +1,26 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +/// Matrix SDK encryption specific extension +library encryption; + +export 'encryption/encryption.dart'; +export 'encryption/key_manager.dart'; +export 'encryption/ssss.dart'; +export 'encryption/utils/key_verification.dart'; +export 'encryption/utils/bootstrap.dart'; diff --git a/lib/encryption/cross_signing.dart b/lib/encryption/cross_signing.dart new file mode 100644 index 0000000..60d9a64 --- /dev/null +++ b/lib/encryption/cross_signing.dart @@ -0,0 +1,210 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:typed_data'; + +import 'package:matrix/encryption/utils/base64_unpadded.dart'; +import 'package:olm/olm.dart' as olm; + +import '../matrix.dart'; +import 'encryption.dart'; +import 'ssss.dart'; + +class CrossSigning { + final Encryption encryption; + Client get client => encryption.client; + CrossSigning(this.encryption) { + encryption.ssss.setValidator(EventTypes.CrossSigningSelfSigning, + (String secret) async { + final keyObj = olm.PkSigning(); + try { + return keyObj.init_with_seed(base64decodeUnpadded(secret)) == + client.userDeviceKeys[client.userID]!.selfSigningKey!.ed25519Key; + } catch (_) { + return false; + } finally { + keyObj.free(); + } + }); + encryption.ssss.setValidator(EventTypes.CrossSigningUserSigning, + (String secret) async { + final keyObj = olm.PkSigning(); + try { + return keyObj.init_with_seed(base64decodeUnpadded(secret)) == + client.userDeviceKeys[client.userID]!.userSigningKey!.ed25519Key; + } catch (_) { + return false; + } finally { + keyObj.free(); + } + }); + } + + bool get enabled => + encryption.ssss.isSecret(EventTypes.CrossSigningSelfSigning) && + encryption.ssss.isSecret(EventTypes.CrossSigningUserSigning) && + encryption.ssss.isSecret(EventTypes.CrossSigningMasterKey); + + Future isCached() async { + if (!enabled) { + return false; + } + return (await encryption.ssss + .getCached(EventTypes.CrossSigningSelfSigning)) != + null && + (await encryption.ssss.getCached(EventTypes.CrossSigningUserSigning)) != + null; + } + + Future selfSign( + {String? passphrase, + String? recoveryKey, + String? keyOrPassphrase, + OpenSSSS? openSsss}) async { + var handle = openSsss; + if (handle == null) { + handle = encryption.ssss.open(EventTypes.CrossSigningMasterKey); + await handle.unlock( + passphrase: passphrase, + recoveryKey: recoveryKey, + keyOrPassphrase: keyOrPassphrase, + postUnlock: false, + ); + await handle.maybeCacheAll(); + } + final masterPrivateKey = base64decodeUnpadded( + await handle.getStored(EventTypes.CrossSigningMasterKey)); + final keyObj = olm.PkSigning(); + String? masterPubkey; + try { + masterPubkey = keyObj.init_with_seed(masterPrivateKey); + } catch (e) { + masterPubkey = null; + } finally { + keyObj.free(); + } + final userDeviceKeys = + client.userDeviceKeys[client.userID]?.deviceKeys[client.deviceID]; + if (masterPubkey == null || userDeviceKeys == null) { + throw Exception('Master or user keys not found'); + } + final masterKey = client.userDeviceKeys[client.userID]?.masterKey; + if (masterKey == null || masterKey.ed25519Key != masterPubkey) { + throw Exception('Master pubkey key doesn\'t match'); + } + // master key is valid, set it to verified + await masterKey.setVerified(true, false); + // and now sign both our own key and our master key + await sign([ + masterKey, + userDeviceKeys, + ]); + } + + bool signable(List keys) => keys.any((key) => + key is CrossSigningKey && key.usage.contains('master') || + key is DeviceKeys && + key.userId == client.userID && + key.identifier != client.deviceID); + + Future sign(List keys) async { + final signedKeys = []; + Uint8List? selfSigningKey; + Uint8List? userSigningKey; + final userKeys = client.userDeviceKeys[client.userID]; + if (userKeys == null) { + throw Exception('[sign] keys are not in cache but sign was called'); + } + + final addSignature = + (SignableKey key, SignableKey signedWith, String signature) { + final signedKey = key.cloneForSigning(); + ((signedKey.signatures ??= + >{})[signedWith.userId] ??= + {})['ed25519:${signedWith.identifier}'] = signature; + signedKeys.add(signedKey); + }; + + for (final key in keys) { + if (key.userId == client.userID) { + // we are singing a key of ourself + if (key is CrossSigningKey) { + if (key.usage.contains('master')) { + // okay, we'll sign our own master key + final signature = + encryption.olmManager.signString(key.signingContent); + addSignature(key, userKeys.deviceKeys[client.deviceID]!, signature); + } + // we don't care about signing other cross-signing keys + } else { + // okay, we'll sign a device key with our self signing key + selfSigningKey ??= base64decodeUnpadded(await encryption.ssss + .getCached(EventTypes.CrossSigningSelfSigning) ?? + ''); + if (selfSigningKey.isNotEmpty) { + final signature = _sign(key.signingContent, selfSigningKey); + addSignature(key, userKeys.selfSigningKey!, signature); + } + } + } else if (key is CrossSigningKey && key.usage.contains('master')) { + // we are signing someone elses master key + userSigningKey ??= base64decodeUnpadded(await encryption.ssss + .getCached(EventTypes.CrossSigningUserSigning) ?? + ''); + if (userSigningKey.isNotEmpty) { + final signature = _sign(key.signingContent, userSigningKey); + addSignature(key, userKeys.userSigningKey!, signature); + } + } + } + if (signedKeys.isNotEmpty) { + // post our new keys! + final payload = >>{}; + for (final key in signedKeys) { + if (key.identifier == null || + key.signatures == null || + key.signatures?.isEmpty != false) { + continue; + } + if (!payload.containsKey(key.userId)) { + payload[key.userId] = >{}; + } + if (payload[key.userId]?[key.identifier]?['signatures'] != null) { + // we need to merge signature objects + payload[key.userId]![key.identifier]!['signatures'] + .addAll(key.signatures); + } else { + // we can just add signatures + payload[key.userId]![key.identifier!] = key.toJson(); + } + } + + await client.uploadCrossSigningSignatures(payload); + } + } + + String _sign(String canonicalJson, Uint8List key) { + final keyObj = olm.PkSigning(); + try { + keyObj.init_with_seed(key); + return keyObj.sign(canonicalJson); + } finally { + keyObj.free(); + } + } +} diff --git a/lib/encryption/encryption.dart b/lib/encryption/encryption.dart new file mode 100644 index 0000000..d04a507 --- /dev/null +++ b/lib/encryption/encryption.dart @@ -0,0 +1,455 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:convert'; +import 'dart:async'; + +import 'package:olm/olm.dart' as olm; + +import '../matrix.dart'; +import '../src/utils/run_in_root.dart'; +import 'cross_signing.dart'; +import 'key_manager.dart'; +import 'key_verification_manager.dart'; +import 'olm_manager.dart'; +import 'ssss.dart'; +import 'utils/bootstrap.dart'; + +class Encryption { + final Client client; + final bool debug; + + bool get enabled => olmManager.enabled; + + /// Returns the base64 encoded keys to store them in a store. + /// This String should **never** leave the device! + String? get pickledOlmAccount => olmManager.pickledOlmAccount; + + String? get fingerprintKey => olmManager.fingerprintKey; + String? get identityKey => olmManager.identityKey; + + late KeyManager keyManager; + late OlmManager olmManager; + late KeyVerificationManager keyVerificationManager; + late CrossSigning crossSigning; + late SSSS ssss; + + Encryption({ + required this.client, + this.debug = false, + }) { + ssss = SSSS(this); + keyManager = KeyManager(this); + olmManager = OlmManager(this); + keyVerificationManager = KeyVerificationManager(this); + crossSigning = CrossSigning(this); + } + + // initial login passes null to init a new olm account + Future init(String? olmAccount) async { + await olmManager.init(olmAccount); + _backgroundTasksRunning = true; + _backgroundTasks(); // start the background tasks + } + + bool isMinOlmVersion(int major, int minor, int patch) { + try { + final version = olm.get_library_version(); + return version[0] > major || + (version[0] == major && + (version[1] > minor || + (version[1] == minor && version[2] >= patch))); + } catch (_) { + return false; + } + } + + Bootstrap bootstrap({void Function()? onUpdate}) => Bootstrap( + encryption: this, + onUpdate: onUpdate, + ); + + void handleDeviceOneTimeKeysCount( + Map? countJson, List? unusedFallbackKeyTypes) { + runInRoot(() => olmManager.handleDeviceOneTimeKeysCount( + countJson, unusedFallbackKeyTypes)); + } + + void onSync() { + keyVerificationManager.cleanup(); + } + + Future handleToDeviceEvent(ToDeviceEvent event) async { + if (event.type == EventTypes.RoomKey) { + // a new room key. We need to handle this asap, before other + // events in /sync are handled + await keyManager.handleToDeviceEvent(event); + } + if ([EventTypes.RoomKeyRequest, EventTypes.ForwardedRoomKey] + .contains(event.type)) { + // "just" room key request things. We don't need these asap, so we handle + // them in the background + // ignore: unawaited_futures + runInRoot(() => keyManager.handleToDeviceEvent(event)); + } + if (event.type == EventTypes.Dummy) { + // the previous device just had to create a new olm session, due to olm session + // corruption. We want to try to send it the last message we just sent it, if possible + // ignore: unawaited_futures + runInRoot(() => olmManager.handleToDeviceEvent(event)); + } + if (event.type.startsWith('m.key.verification.')) { + // some key verification event. No need to handle it now, we can easily + // do this in the background + + // ignore: unawaited_futures + runInRoot(() => keyVerificationManager.handleToDeviceEvent(event)); + } + if (event.type.startsWith('m.secret.')) { + // some ssss thing. We can do this in the background + // ignore: unawaited_futures + runInRoot(() => ssss.handleToDeviceEvent(event)); + } + if (event.sender == client.userID) { + // maybe we need to re-try SSSS secrets + // ignore: unawaited_futures + runInRoot(() => ssss.periodicallyRequestMissingCache()); + } + } + + Future handleEventUpdate(EventUpdate update) async { + if (update.type == EventUpdateType.ephemeral || + update.type == EventUpdateType.history) { + return; + } + if (update.content['type'].startsWith('m.key.verification.') || + (update.content['type'] == EventTypes.Message && + (update.content['content']['msgtype'] is String) && + update.content['content']['msgtype'] + .startsWith('m.key.verification.'))) { + // "just" key verification, no need to do this in sync + + // ignore: unawaited_futures + runInRoot(() => keyVerificationManager.handleEventUpdate(update)); + } + if (update.content['sender'] == client.userID && + update.content['unsigned']?['transaction_id'] == null) { + // maybe we need to re-try SSSS secrets + // ignore: unawaited_futures + runInRoot(() => ssss.periodicallyRequestMissingCache()); + } + } + + Future decryptToDeviceEvent(ToDeviceEvent event) async { + try { + return await olmManager.decryptToDeviceEvent(event); + } catch (e, s) { + Logs().w( + '[LibOlm] Could not decrypt to device event from ${event.sender} with content: ${event.content}', + e, + s); + client.onEncryptionError.add( + SdkError( + exception: e is Exception ? e : Exception(e), + stackTrace: s, + ), + ); + return event; + } + } + + Event decryptRoomEventSync(String roomId, Event event) { + final content = event.parsedRoomEncryptedContent; + if (event.type != EventTypes.Encrypted || + content.ciphertextMegolm == null) { + return event; + } + Map decryptedPayload; + var canRequestSession = false; + try { + if (content.algorithm != AlgorithmTypes.megolmV1AesSha2) { + throw DecryptException(DecryptException.unknownAlgorithm); + } + final sessionId = content.sessionId; + final senderKey = content.senderKey; + if (sessionId == null) { + throw DecryptException(DecryptException.unknownSession); + } + + final inboundGroupSession = + keyManager.getInboundGroupSession(roomId, sessionId, senderKey); + if (!(inboundGroupSession?.isValid ?? false)) { + canRequestSession = true; + throw DecryptException(DecryptException.unknownSession); + } + + // decrypt errors here may mean we have a bad session key - others might have a better one + canRequestSession = true; + + final decryptResult = inboundGroupSession!.inboundGroupSession! + .decrypt(content.ciphertextMegolm!); + canRequestSession = false; + + // we can't have the key be an int, else json-serializing will fail, thus we need it to be a string + final messageIndexKey = 'key-' + decryptResult.message_index.toString(); + final messageIndexValue = event.eventId + + '|' + + event.originServerTs.millisecondsSinceEpoch.toString(); + final haveIndex = + inboundGroupSession.indexes.containsKey(messageIndexKey); + if (haveIndex && + inboundGroupSession.indexes[messageIndexKey] != messageIndexValue) { + Logs().e('[Decrypt] Could not decrypt due to a corrupted session.'); + throw DecryptException(DecryptException.channelCorrupted); + } + + inboundGroupSession.indexes[messageIndexKey] = messageIndexValue; + if (!haveIndex) { + // now we persist the udpated indexes into the database. + // the entry should always exist. In the case it doesn't, the following + // line *could* throw an error. As that is a future, though, and we call + // it un-awaited here, nothing happens, which is exactly the result we want + client.database?.updateInboundGroupSessionIndexes( + json.encode(inboundGroupSession.indexes), roomId, sessionId); + } + decryptedPayload = json.decode(decryptResult.plaintext); + } catch (exception) { + // alright, if this was actually by our own outbound group session, we might as well clear it + if (exception.toString() != DecryptException.unknownSession && + (keyManager + .getOutboundGroupSession(roomId) + ?.outboundGroupSession + ?.session_id() ?? + '') == + content.sessionId) { + runInRoot(() => + keyManager.clearOrUseOutboundGroupSession(roomId, wipe: true)); + } + if (canRequestSession) { + decryptedPayload = { + 'content': event.content, + 'type': EventTypes.Encrypted, + }; + decryptedPayload['content']['body'] = exception.toString(); + decryptedPayload['content']['msgtype'] = MessageTypes.BadEncrypted; + decryptedPayload['content']['can_request_session'] = true; + } else { + decryptedPayload = { + 'content': { + 'msgtype': MessageTypes.BadEncrypted, + 'body': exception.toString(), + }, + 'type': EventTypes.Encrypted, + }; + } + } + if (event.content['m.relates_to'] != null) { + decryptedPayload['content']['m.relates_to'] = + event.content['m.relates_to']; + } + return Event( + content: decryptedPayload['content'], + type: decryptedPayload['type'], + senderId: event.senderId, + eventId: event.eventId, + room: event.room, + originServerTs: event.originServerTs, + unsigned: event.unsigned, + stateKey: event.stateKey, + prevContent: event.prevContent, + status: event.status, + ); + } + + Future decryptRoomEvent(String roomId, Event event, + {bool store = false, + EventUpdateType updateType = EventUpdateType.timeline}) async { + if (event.type != EventTypes.Encrypted) { + return event; + } + final content = event.parsedRoomEncryptedContent; + final sessionId = content.sessionId; + try { + if (client.database != null && + sessionId != null && + !(keyManager + .getInboundGroupSession( + roomId, + sessionId, + content.senderKey, + ) + ?.isValid ?? + false)) { + await keyManager.loadInboundGroupSession( + roomId, + sessionId, + content.senderKey, + ); + } + event = decryptRoomEventSync(roomId, event); + if (event.type == EventTypes.Encrypted && + event.content['can_request_session'] == true && + sessionId != null) { + keyManager.maybeAutoRequest( + roomId, + sessionId, + content.senderKey, + ); + } + if (event.type != EventTypes.Encrypted && store) { + if (updateType != EventUpdateType.history) { + event.room.setState(event); + } + await client.database?.storeEventUpdate( + EventUpdate( + content: event.toJson(), + roomID: roomId, + type: updateType, + ), + client, + ); + } + return event; + } catch (e, s) { + Logs().e('[Decrypt] Could not decrpyt event', e, s); + return event; + } + } + + /// Encrypts the given json payload and creates a send-ready m.room.encrypted + /// payload. This will create a new outgoingGroupSession if necessary. + Future> encryptGroupMessagePayload( + String roomId, Map payload, + {String type = EventTypes.Message}) async { + final Map? mRelatesTo = payload.remove('m.relates_to'); + // Events which only contain a m.relates_to like reactions don't need to + // be encrypted. + if (payload.isEmpty && mRelatesTo != null) { + return {'m.relates_to': mRelatesTo}; + } + final room = client.getRoomById(roomId); + if (room == null || !room.encrypted || !enabled) { + return payload; + } + if (room.encryptionAlgorithm != AlgorithmTypes.megolmV1AesSha2) { + throw ('Unknown encryption algorithm'); + } + if (keyManager.getOutboundGroupSession(roomId)?.isValid != true) { + await keyManager.loadOutboundGroupSession(roomId); + } + await keyManager.clearOrUseOutboundGroupSession(roomId); + if (keyManager.getOutboundGroupSession(roomId)?.isValid != true) { + await keyManager.createOutboundGroupSession(roomId); + } + final sess = keyManager.getOutboundGroupSession(roomId); + if (sess?.isValid != true) { + throw ('Unable to create new outbound group session'); + } + // we clone the payload as we do not want to remove 'm.relates_to' from the + // original payload passed into this function + payload = payload.copy(); + final payloadContent = { + 'content': payload, + 'type': type, + 'room_id': roomId, + }; + final encryptedPayload = { + 'algorithm': AlgorithmTypes.megolmV1AesSha2, + 'ciphertext': + sess!.outboundGroupSession!.encrypt(json.encode(payloadContent)), + 'device_id': client.deviceID, + 'sender_key': identityKey, + 'session_id': sess.outboundGroupSession!.session_id(), + if (mRelatesTo != null) 'm.relates_to': mRelatesTo, + }; + await keyManager.storeOutboundGroupSession(roomId, sess); + return encryptedPayload; + } + + Future> encryptToDeviceMessage( + List deviceKeys, + String type, + Map payload) async { + return await olmManager.encryptToDeviceMessage(deviceKeys, type, payload); + } + + Future autovalidateMasterOwnKey() async { + // check if we can set our own master key as verified, if it isn't yet + final userId = client.userID; + final masterKey = client.userDeviceKeys[userId]?.masterKey; + if (client.database != null && + masterKey != null && + userId != null && + !masterKey.directVerified && + masterKey.hasValidSignatureChain(onlyValidateUserIds: {userId})) { + await masterKey.setVerified(true); + } + } + + // this method is responsible for all background tasks, such as uploading online key backups + bool _backgroundTasksRunning = true; + void _backgroundTasks() { + if (!_backgroundTasksRunning || !client.isLogged()) { + return; + } + + keyManager.backgroundTasks(); + +// autovalidateMasterOwnKey(); + + if (_backgroundTasksRunning) { + Timer(Duration(seconds: 10), _backgroundTasks); + } + } + + void dispose() { + _backgroundTasksRunning = false; + keyManager.dispose(); + olmManager.dispose(); + keyVerificationManager.dispose(); + } +} + +class DecryptException implements Exception { + String cause; + String? libolmMessage; + DecryptException(this.cause, [this.libolmMessage]); + + @override + String toString() => + cause + (libolmMessage != null ? ': $libolmMessage' : ''); + + static const String notEnabled = 'Encryption is not enabled in your client.'; + static const String unknownAlgorithm = 'Unknown encryption algorithm.'; + static const String unknownSession = + 'The sender has not sent us the session key.'; + static const String channelCorrupted = + 'The secure channel with the sender was corrupted.'; + static const String unableToDecryptWithAnyOlmSession = + 'Unable to decrypt with any existing OLM session'; + static const String senderDoesntMatch = + "Message was decrypted but sender doesn't match"; + static const String recipientDoesntMatch = + "Message was decrypted but recipient doesn't match"; + static const String ownFingerprintDoesntMatch = + "Message was decrypted but own fingerprint Key doesn't match"; + static const String isntSentForThisDevice = + "The message isn't sent for this device"; + static const String unknownMessageType = 'Unknown message type'; + static const String decryptionFailed = 'Decryption failed'; +} diff --git a/lib/encryption/key_manager.dart b/lib/encryption/key_manager.dart new file mode 100644 index 0000000..5f9f654 --- /dev/null +++ b/lib/encryption/key_manager.dart @@ -0,0 +1,1093 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:convert'; + +import 'package:matrix/encryption/utils/base64_unpadded.dart'; +import 'package:matrix/encryption/utils/stored_inbound_group_session.dart'; +import 'package:olm/olm.dart' as olm; +import 'package:collection/collection.dart'; + +import './encryption.dart'; +import './utils/outbound_group_session.dart'; +import './utils/session_key.dart'; +import '../matrix.dart'; +import '../src/utils/run_in_root.dart'; + +const megolmKey = EventTypes.MegolmBackup; + +class KeyManager { + final Encryption encryption; + Client get client => encryption.client; + final outgoingShareRequests = {}; + final incomingShareRequests = {}; + final _inboundGroupSessions = >{}; + final _outboundGroupSessions = {}; + final Set _loadedOutboundGroupSessions = {}; + final Set _requestedSessionIds = {}; + + KeyManager(this.encryption) { + encryption.ssss.setValidator(megolmKey, (String secret) async { + final keyObj = olm.PkDecryption(); + try { + final info = await getRoomKeysBackupInfo(false); + if (info.algorithm != + BackupAlgorithm.mMegolmBackupV1Curve25519AesSha2) { + return false; + } + return keyObj.init_with_private_key(base64decodeUnpadded(secret)) == + info.authData['public_key']; + } catch (_) { + return false; + } finally { + keyObj.free(); + } + }); + encryption.ssss.setCacheCallback(megolmKey, (String secret) { + // we got a megolm key cached, clear our requested keys and try to re-decrypt + // last events + _requestedSessionIds.clear(); + for (final room in client.rooms) { + final lastEvent = room.lastEvent; + if (lastEvent != null && + lastEvent.type == EventTypes.Encrypted && + lastEvent.content['can_request_session'] == true) { + try { + maybeAutoRequest(room.id, lastEvent.content['session_id'], + lastEvent.content['sender_key']); + } catch (_) { + // dispose + } + } + } + }); + } + + bool get enabled => encryption.ssss.isSecret(megolmKey); + + /// clear all cached inbound group sessions. useful for testing + void clearInboundGroupSessions() { + _inboundGroupSessions.clear(); + } + + void setInboundGroupSession( + String roomId, + String sessionId, + String senderKey, + Map content, { + bool forwarded = false, + Map? senderClaimedKeys, + bool uploaded = false, + Map>? allowedAtIndex, + }) { + final senderClaimedKeys_ = senderClaimedKeys ?? {}; + final allowedAtIndex_ = allowedAtIndex ?? >{}; + final userId = client.userID; + if (userId == null) return; + + if (!senderClaimedKeys_.containsKey('ed25519')) { + final device = client.getUserDeviceKeysByCurve25519Key(senderKey); + if (device != null && device.ed25519Key != null) { + senderClaimedKeys_['ed25519'] = device.ed25519Key!; + } + } + final oldSession = + getInboundGroupSession(roomId, sessionId, senderKey, otherRooms: false); + if (content['algorithm'] != AlgorithmTypes.megolmV1AesSha2) { + return; + } + late olm.InboundGroupSession inboundGroupSession; + try { + inboundGroupSession = olm.InboundGroupSession(); + if (forwarded) { + inboundGroupSession.import_session(content['session_key']); + } else { + inboundGroupSession.create(content['session_key']); + } + } catch (e, s) { + inboundGroupSession.free(); + Logs().e('[LibOlm] Could not create new InboundGroupSession', e, s); + return; + } + final newSession = SessionKey( + content: content, + inboundGroupSession: inboundGroupSession, + indexes: {}, + roomId: roomId, + sessionId: sessionId, + key: userId, + senderKey: senderKey, + senderClaimedKeys: senderClaimedKeys_, + allowedAtIndex: allowedAtIndex_, + ); + final oldFirstIndex = + oldSession?.inboundGroupSession?.first_known_index() ?? 0; + final newFirstIndex = newSession.inboundGroupSession!.first_known_index(); + if (oldSession == null || + newFirstIndex < oldFirstIndex || + (oldFirstIndex == newFirstIndex && + newSession.forwardingCurve25519KeyChain.length < + oldSession.forwardingCurve25519KeyChain.length)) { + // use new session + oldSession?.dispose(); + } else { + // we are gonna keep our old session + newSession.dispose(); + return; + } + + final roomInboundGroupSessions = + _inboundGroupSessions[roomId] ??= {}; + roomInboundGroupSessions[sessionId] = newSession; + if (!client.isLogged() || client.encryption == null) { + return; + } + client.database + ?.storeInboundGroupSession( + roomId, + sessionId, + inboundGroupSession.pickle(userId), + json.encode(content), + json.encode({}), + json.encode(allowedAtIndex_), + senderKey, + json.encode(senderClaimedKeys_), + ) + .then((_) { + if (!client.isLogged() || client.encryption == null) { + return; + } + if (uploaded) { + client.database?.markInboundGroupSessionAsUploaded(roomId, sessionId); + } else { + _haveKeysToUpload = true; + } + }); + final room = client.getRoomById(roomId); + if (room != null) { + // attempt to decrypt the last event + final event = room.getState(EventTypes.Encrypted); + if (event != null && event.content['session_id'] == sessionId) { + room.setState(encryption.decryptRoomEventSync(roomId, event)); + } + // and finally broadcast the new session + room.onSessionKeyReceived.add(sessionId); + } + } + + SessionKey? getInboundGroupSession( + String roomId, String sessionId, String senderKey, + {bool otherRooms = true}) { + final sess = _inboundGroupSessions[roomId]?[sessionId]; + if (sess != null) { + if (sess.senderKey != senderKey && sess.senderKey.isNotEmpty) { + return null; + } + return sess; + } + if (!otherRooms) { + return null; + } + // search if this session id is *somehow* found in another room + for (final val in _inboundGroupSessions.values) { + final sess = val[sessionId]; + if (sess != null) { + if (sess.senderKey != senderKey && sess.senderKey.isNotEmpty) { + return null; + } + return sess; + } + } + return null; + } + + /// Attempt auto-request for a key + void maybeAutoRequest(String roomId, String sessionId, String senderKey) { + final room = client.getRoomById(roomId); + final requestIdent = '$roomId|$sessionId|$senderKey'; + if (room != null && + !_requestedSessionIds.contains(requestIdent) && + !client.isUnknownSession) { + // do e2ee recovery + _requestedSessionIds.add(requestIdent); + runInRoot( + () => request(room, sessionId, senderKey, onlineKeyBackupOnly: true)); + } + } + + /// Loads an inbound group session + Future loadInboundGroupSession( + String roomId, String sessionId, String senderKey) async { + final sess = _inboundGroupSessions[roomId]?[sessionId]; + if (sess != null) { + if (sess.senderKey != senderKey && sess.senderKey.isNotEmpty) { + return null; // sender keys do not match....better not do anything + } + return sess; // nothing to do + } + final session = + await client.database?.getInboundGroupSession(roomId, sessionId); + if (session == null) return null; + final userID = client.userID; + if (userID == null) return null; + final dbSess = SessionKey.fromDb(session, userID); + final roomInboundGroupSessions = + _inboundGroupSessions[roomId] ??= {}; + if (!dbSess.isValid || + dbSess.senderKey.isEmpty || + dbSess.senderKey != senderKey) { + return null; + } + roomInboundGroupSessions[sessionId] = dbSess; + return sess; + } + + Map> _getDeviceKeyIdMap( + List deviceKeys) { + final deviceKeyIds = >{}; + for (final device in deviceKeys) { + final deviceId = device.deviceId; + if (deviceId == null) { + Logs().w('[KeyManager] ignoring device without deviceid'); + continue; + } + final userDeviceKeyIds = deviceKeyIds[device.userId] ??= {}; + userDeviceKeyIds[deviceId] = !device.encryptToDevice; + } + return deviceKeyIds; + } + + /// clear all cached inbound group sessions. useful for testing + void clearOutboundGroupSessions() { + _outboundGroupSessions.clear(); + } + + /// Clears the existing outboundGroupSession but first checks if the participating + /// devices have been changed. Returns false if the session has not been cleared because + /// it wasn't necessary. Otherwise returns true. + Future clearOrUseOutboundGroupSession(String roomId, + {bool wipe = false, bool use = true}) async { + final room = client.getRoomById(roomId); + final sess = getOutboundGroupSession(roomId); + if (room == null || sess == null || sess.outboundGroupSession == null) { + return true; + } + + if (!wipe) { + // first check if it needs to be rotated + final encryptionContent = + room.getState(EventTypes.Encryption)?.parsedRoomEncryptionContent; + final maxMessages = encryptionContent?.rotationPeriodMsgs ?? 100; + final maxAge = encryptionContent?.rotationPeriodMs ?? + 604800000; // default of one week + if ((sess.sentMessages ?? maxMessages) >= maxMessages || + sess.creationTime + .add(Duration(milliseconds: maxAge)) + .isBefore(DateTime.now())) { + wipe = true; + } + } + + final inboundSess = await loadInboundGroupSession(room.id, + sess.outboundGroupSession!.session_id(), encryption.identityKey!); + if (inboundSess == null) { + wipe = true; + } + + if (!wipe) { + // next check if the devices in the room changed + final devicesToReceive = []; + final newDeviceKeys = await room.getUserDeviceKeys(); + final newDeviceKeyIds = _getDeviceKeyIdMap(newDeviceKeys); + // first check for user differences + final oldUserIds = Set.from(sess.devices.keys); + final newUserIds = Set.from(newDeviceKeyIds.keys); + if (oldUserIds.difference(newUserIds).isNotEmpty) { + // a user left the room, we must wipe the session + wipe = true; + } else { + final newUsers = newUserIds.difference(oldUserIds); + if (newUsers.isNotEmpty) { + // new user! Gotta send the megolm session to them + devicesToReceive + .addAll(newDeviceKeys.where((d) => newUsers.contains(d.userId))); + } + // okay, now we must test all the individual user devices, if anything new got blocked + // or if we need to send to any new devices. + // for this it is enough if we iterate over the old user Ids, as the new ones already have the needed keys in the list. + // we also know that all the old user IDs appear in the old one, else we have already wiped the session + for (final userId in oldUserIds) { + final oldBlockedDevices = sess.devices.containsKey(userId) + ? Set.from(sess.devices[userId]!.entries + .where((e) => e.value) + .map((e) => e.key)) + : {}; + final newBlockedDevices = newDeviceKeyIds.containsKey(userId) + ? Set.from(newDeviceKeyIds[userId]! + .entries + .where((e) => e.value) + .map((e) => e.key)) + : {}; + // we don't really care about old devices that got dropped (deleted), we only care if new ones got added and if new ones got blocked + // check if new devices got blocked + if (newBlockedDevices.difference(oldBlockedDevices).isNotEmpty) { + wipe = true; + break; + } + // and now add all the new devices! + final oldDeviceIds = sess.devices.containsKey(userId) + ? Set.from(sess.devices[userId]!.entries + .where((e) => !e.value) + .map((e) => e.key)) + : {}; + final newDeviceIds = newDeviceKeyIds.containsKey(userId) + ? Set.from(newDeviceKeyIds[userId]! + .entries + .where((e) => !e.value) + .map((e) => e.key)) + : {}; + final newDevices = newDeviceIds.difference(oldDeviceIds); + if (newDeviceIds.isNotEmpty) { + devicesToReceive.addAll(newDeviceKeys.where( + (d) => d.userId == userId && newDevices.contains(d.deviceId))); + } + } + } + + if (!wipe) { + if (!use) { + return false; + } + // okay, we use the outbound group session! + sess.devices = newDeviceKeyIds; + final rawSession = { + 'algorithm': AlgorithmTypes.megolmV1AesSha2, + 'room_id': room.id, + 'session_id': sess.outboundGroupSession!.session_id(), + 'session_key': sess.outboundGroupSession!.session_key(), + }; + try { + devicesToReceive.removeWhere((k) => !k.encryptToDevice); + if (devicesToReceive.isNotEmpty) { + // update allowedAtIndex + for (final device in devicesToReceive) { + inboundSess!.allowedAtIndex[device.userId] ??= {}; + if (!inboundSess.allowedAtIndex[device.userId]! + .containsKey(device.curve25519Key) || + inboundSess.allowedAtIndex[device.userId]![ + device.curve25519Key]! > + sess.outboundGroupSession!.message_index()) { + inboundSess + .allowedAtIndex[device.userId]![device.curve25519Key!] = + sess.outboundGroupSession!.message_index(); + } + } + await client.database?.updateInboundGroupSessionAllowedAtIndex( + json.encode(inboundSess!.allowedAtIndex), + room.id, + sess.outboundGroupSession!.session_id()); + // send out the key + await client.sendToDeviceEncryptedChunked( + devicesToReceive, EventTypes.RoomKey, rawSession); + } + } catch (e, s) { + Logs().e( + '[LibOlm] Unable to re-send the session key at later index to new devices', + e, + s); + } + return false; + } + } + sess.dispose(); + _outboundGroupSessions.remove(roomId); + await client.database?.removeOutboundGroupSession(roomId); + return true; + } + + /// Store an outbound group session in the database + Future storeOutboundGroupSession( + String roomId, OutboundGroupSession sess) async { + final userID = client.userID; + if (userID == null) return; + await client.database?.storeOutboundGroupSession( + roomId, + sess.outboundGroupSession!.pickle(userID), + json.encode(sess.devices), + sess.creationTime.millisecondsSinceEpoch); + } + + final Map> + _pendingNewOutboundGroupSessions = {}; + + /// Creates an outbound group session for a given room id + Future createOutboundGroupSession(String roomId) async { + final sess = _pendingNewOutboundGroupSessions[roomId]; + if (sess != null) { + return sess; + } + _pendingNewOutboundGroupSessions[roomId] = + _createOutboundGroupSession(roomId); + await _pendingNewOutboundGroupSessions[roomId]; + return _pendingNewOutboundGroupSessions.remove(roomId)!; + } + + /// Prepares an outbound group session for a given room ID. That is, load it from + /// the database, cycle it if needed and create it if absent. + Future prepareOutboundGroupSession(String roomId) async { + if (getOutboundGroupSession(roomId) == null) { + await loadOutboundGroupSession(roomId); + } + await clearOrUseOutboundGroupSession(roomId, use: false); + if (getOutboundGroupSession(roomId) == null) { + await createOutboundGroupSession(roomId); + } + } + + Future _createOutboundGroupSession( + String roomId) async { + await clearOrUseOutboundGroupSession(roomId, wipe: true); + final room = client.getRoomById(roomId); + if (room == null) { + throw Exception( + 'Tried to create a megolm session in a non-existing room ($roomId)!'); + } + final userID = client.userID; + if (userID == null) { + throw Exception( + 'Tried to create a megolm session without being logged in!'); + } + + final deviceKeys = await room.getUserDeviceKeys(); + final deviceKeyIds = _getDeviceKeyIdMap(deviceKeys); + deviceKeys.removeWhere((k) => !k.encryptToDevice); + final outboundGroupSession = olm.OutboundGroupSession(); + try { + outboundGroupSession.create(); + } catch (e, s) { + outboundGroupSession.free(); + Logs().e('[LibOlm] Unable to create new outboundGroupSession', e, s); + rethrow; + } + final rawSession = { + 'algorithm': AlgorithmTypes.megolmV1AesSha2, + 'room_id': room.id, + 'session_id': outboundGroupSession.session_id(), + 'session_key': outboundGroupSession.session_key(), + }; + final allowedAtIndex = >{}; + for (final device in deviceKeys) { + if (!device.isValid) { + Logs().e('Skipping invalid device'); + continue; + } + allowedAtIndex[device.userId] ??= {}; + allowedAtIndex[device.userId]![device.curve25519Key!] = + outboundGroupSession.message_index(); + } + setInboundGroupSession( + roomId, rawSession['session_id'], encryption.identityKey!, rawSession, + allowedAtIndex: allowedAtIndex); + final sess = OutboundGroupSession( + devices: deviceKeyIds, + creationTime: DateTime.now(), + outboundGroupSession: outboundGroupSession, + key: userID, + ); + try { + await client.sendToDeviceEncryptedChunked( + deviceKeys, EventTypes.RoomKey, rawSession); + await storeOutboundGroupSession(roomId, sess); + _outboundGroupSessions[roomId] = sess; + } catch (e, s) { + Logs().e( + '[LibOlm] Unable to send the session key to the participating devices', + e, + s); + sess.dispose(); + rethrow; + } + return sess; + } + + /// Get an outbound group session for a room id + OutboundGroupSession? getOutboundGroupSession(String roomId) { + return _outboundGroupSessions[roomId]; + } + + /// Load an outbound group session from database + Future loadOutboundGroupSession(String roomId) async { + final database = client.database; + final userID = client.userID; + if (_loadedOutboundGroupSessions.contains(roomId) || + _outboundGroupSessions.containsKey(roomId) || + database == null || + userID == null) { + return; // nothing to do + } + _loadedOutboundGroupSessions.add(roomId); + final sess = await database.getOutboundGroupSession( + roomId, + userID, + ); + if (sess == null || !sess.isValid) { + return; + } + _outboundGroupSessions[roomId] = sess; + } + + Future isCached() async { + if (!enabled) { + return false; + } + return (await encryption.ssss.getCached(megolmKey)) != null; + } + + GetRoomKeysVersionCurrentResponse? _roomKeysVersionCache; + DateTime? _roomKeysVersionCacheDate; + Future getRoomKeysBackupInfo( + [bool useCache = true]) async { + if (_roomKeysVersionCache != null && + _roomKeysVersionCacheDate != null && + useCache && + DateTime.now() + .subtract(Duration(minutes: 5)) + .isBefore(_roomKeysVersionCacheDate!)) { + return _roomKeysVersionCache!; + } + _roomKeysVersionCache = await client.getRoomKeysVersionCurrent(); + _roomKeysVersionCacheDate = DateTime.now(); + return _roomKeysVersionCache!; + } + + Future loadFromResponse(RoomKeys keys) async { + if (!(await isCached())) { + return; + } + final privateKey = + base64decodeUnpadded((await encryption.ssss.getCached(megolmKey))!); + final decryption = olm.PkDecryption(); + final info = await getRoomKeysBackupInfo(); + String backupPubKey; + try { + backupPubKey = decryption.init_with_private_key(privateKey); + + if (info.algorithm != BackupAlgorithm.mMegolmBackupV1Curve25519AesSha2 || + info.authData['public_key'] != backupPubKey) { + return; + } + for (final roomEntry in keys.rooms.entries) { + final roomId = roomEntry.key; + for (final sessionEntry in roomEntry.value.sessions.entries) { + final sessionId = sessionEntry.key; + final session = sessionEntry.value; + final sessionData = session.sessionData; + Map? decrypted; + try { + decrypted = json.decode(decryption.decrypt(sessionData['ephemeral'], + sessionData['mac'], sessionData['ciphertext'])); + } catch (e, s) { + Logs().e('[LibOlm] Error decrypting room key', e, s); + } + if (decrypted != null) { + decrypted['session_id'] = sessionId; + decrypted['room_id'] = roomId; + setInboundGroupSession( + roomId, sessionId, decrypted['sender_key'], decrypted, + forwarded: true, + senderClaimedKeys: decrypted['sender_claimed_keys'] != null + ? Map.from( + decrypted['sender_claimed_keys']!) + : {}, + uploaded: true); + } + } + } + } finally { + decryption.free(); + } + } + + Future loadSingleKey(String roomId, String sessionId) async { + final info = await getRoomKeysBackupInfo(); + final ret = + await client.getRoomKeysBySessionId(roomId, sessionId, info.version); + final keys = RoomKeys.fromJson({ + 'rooms': { + roomId: { + 'sessions': { + sessionId: ret.toJson(), + }, + }, + }, + }); + await loadFromResponse(keys); + } + + /// Request a certain key from another device + Future request( + Room room, + String sessionId, + String senderKey, { + bool tryOnlineBackup = true, + bool onlineKeyBackupOnly = false, + }) async { + if (tryOnlineBackup && await isCached()) { + // let's first check our online key backup store thingy... + final hadPreviously = + getInboundGroupSession(room.id, sessionId, senderKey) != null; + try { + await loadSingleKey(room.id, sessionId); + } catch (err, stacktrace) { + if (err is MatrixException && err.errcode == 'M_NOT_FOUND') { + Logs().i( + '[KeyManager] Key not in online key backup, requesting it from other devices...'); + } else { + Logs().e('[KeyManager] Failed to access online key backup', err, + stacktrace); + } + } + // TODO: also don't request from others if we have an index of 0 now + if (!hadPreviously && + getInboundGroupSession(room.id, sessionId, senderKey) != null) { + return; // we managed to load the session from online backup, no need to care about it now + } + } + if (onlineKeyBackupOnly) { + return; // we only want to do the online key backup + } + try { + // while we just send the to-device event to '*', we still need to save the + // devices themself to know where to send the cancel to after receiving a reply + final devices = await room.getUserDeviceKeys(); + final requestId = client.generateUniqueTransactionId(); + final request = KeyManagerKeyShareRequest( + requestId: requestId, + devices: devices, + room: room, + sessionId: sessionId, + senderKey: senderKey, + ); + final userList = await room.requestParticipants(); + await client.sendToDevicesOfUserIds( + userList.map((u) => u.id).toSet(), + EventTypes.RoomKeyRequest, + { + 'action': 'request', + 'body': { + 'algorithm': AlgorithmTypes.megolmV1AesSha2, + 'room_id': room.id, + 'sender_key': senderKey, + 'session_id': sessionId, + }, + 'request_id': requestId, + 'requesting_device_id': client.deviceID, + }, + ); + outgoingShareRequests[request.requestId] = request; + } catch (e, s) { + Logs().e('[Key Manager] Sending key verification request failed', e, s); + } + } + + bool _isUploadingKeys = false; + bool _haveKeysToUpload = true; + Future backgroundTasks() async { + final database = client.database; + final userID = client.userID; + if (_isUploadingKeys || database == null || userID == null) { + return; + } + _isUploadingKeys = true; + try { + if (!_haveKeysToUpload || !(await isCached())) { + return; // we can't backup anyways + } + final dbSessions = await database.getInboundGroupSessionsToUpload(); + if (dbSessions.isEmpty) { + _haveKeysToUpload = false; + return; // nothing to do + } + final privateKey = + base64decodeUnpadded((await encryption.ssss.getCached(megolmKey))!); + // decryption is needed to calculate the public key and thus see if the claimed information is in fact valid + final decryption = olm.PkDecryption(); + final info = await getRoomKeysBackupInfo(false); + String backupPubKey; + try { + backupPubKey = decryption.init_with_private_key(privateKey); + + if (info.algorithm != + BackupAlgorithm.mMegolmBackupV1Curve25519AesSha2 || + info.authData['public_key'] != backupPubKey) { + return; + } + final args = _GenerateUploadKeysArgs( + pubkey: backupPubKey, + dbSessions: <_DbInboundGroupSessionBundle>[], + userId: userID, + ); + // we need to calculate verified beforehand, as else we pass a closure to an isolate + // with 500 keys they do, however, noticably block the UI, which is why we give brief async suspentions in here + // so that the event loop can progress + var i = 0; + for (final dbSession in dbSessions) { + final device = + client.getUserDeviceKeysByCurve25519Key(dbSession.senderKey); + args.dbSessions.add(_DbInboundGroupSessionBundle( + dbSession: dbSession, + verified: device?.verified ?? false, + )); + i++; + if (i > 10) { + await Future.delayed(Duration(milliseconds: 1)); + i = 0; + } + } + final roomKeys = + await client.runInBackground( + _generateUploadKeys, args); + Logs().i('[Key Manager] Uploading ${dbSessions.length} room keys...'); + // upload the payload... + await client.putRoomKeys(info.version, roomKeys); + // and now finally mark all the keys as uploaded + // no need to optimze this, as we only run it so seldomly and almost never with many keys at once + for (final dbSession in dbSessions) { + await database.markInboundGroupSessionAsUploaded( + dbSession.roomId, dbSession.sessionId); + } + } finally { + decryption.free(); + } + } catch (e, s) { + Logs().e('[Key Manager] Error uploading room keys', e, s); + } finally { + _isUploadingKeys = false; + } + } + + /// Handle an incoming to_device event that is related to key sharing + Future handleToDeviceEvent(ToDeviceEvent event) async { + if (event.type == EventTypes.RoomKeyRequest) { + if (!(event.content['request_id'] is String)) { + return; // invalid event + } + if (event.content['action'] == 'request') { + // we are *receiving* a request + Logs().i( + '[KeyManager] Received key sharing request from ${event.sender}:${event.content['requesting_device_id']}...'); + if (!event.content.containsKey('body')) { + Logs().i('[KeyManager] No body, doing nothing'); + return; // no body + } + final device = client.userDeviceKeys[event.sender] + ?.deviceKeys[event.content['requesting_device_id']]; + if (device == null) { + Logs().i('[KeyManager] Device not found, doing nothing'); + return; // device not found + } + if (device.userId == client.userID && + device.deviceId == client.deviceID) { + Logs().i('[KeyManager] Request is by ourself, ignoring'); + return; // ignore requests by ourself + } + final room = client.getRoomById(event.content['body']['room_id']); + if (room == null) { + Logs().i('[KeyManager] Unknown room, ignoring'); + return; // unknown room + } + final sessionId = event.content['body']['session_id']; + final senderKey = event.content['body']['sender_key']; + // okay, let's see if we have this session at all + final session = + await loadInboundGroupSession(room.id, sessionId, senderKey); + if (session == null) { + Logs().i('[KeyManager] Unknown session, ignoring'); + return; // we don't have this session anyways + } + final request = KeyManagerKeyShareRequest( + requestId: event.content['request_id'], + devices: [device], + room: room, + sessionId: sessionId, + senderKey: senderKey, + ); + if (incomingShareRequests.containsKey(request.requestId)) { + Logs().i('[KeyManager] Already processed this request, ignoring'); + return; // we don't want to process one and the same request multiple times + } + incomingShareRequests[request.requestId] = request; + final roomKeyRequest = + RoomKeyRequest.fromToDeviceEvent(event, this, request); + if (device.userId == client.userID && + device.verified && + !device.blocked) { + Logs().i('[KeyManager] All checks out, forwarding key...'); + // alright, we can forward the key + await roomKeyRequest.forwardKey(); + } else if (device.encryptToDevice && + session.allowedAtIndex + .tryGet>(device.userId) + ?.tryGet(device.curve25519Key!) != + null) { + // if we know the user may see the message, then we can just forward the key. + // we do not need to check if the device is verified, just if it is not blocked, + // as that is the logic we already initially try to send out the room keys. + final index = + session.allowedAtIndex[device.userId]![device.curve25519Key]!; + Logs().i( + '[KeyManager] Valid foreign request, forwarding key at index $index...'); + await roomKeyRequest.forwardKey(index); + } else { + Logs() + .i('[KeyManager] Asking client, if the key should be forwarded'); + client.onRoomKeyRequest + .add(roomKeyRequest); // let the client handle this + } + } else if (event.content['action'] == 'request_cancellation') { + // we got told to cancel an incoming request + if (!incomingShareRequests.containsKey(event.content['request_id'])) { + return; // we don't know this request anyways + } + // alright, let's just cancel this request + final request = incomingShareRequests[event.content['request_id']]!; + request.canceled = true; + incomingShareRequests.remove(request.requestId); + } + } else if (event.type == EventTypes.ForwardedRoomKey) { + // we *received* an incoming key request + final encryptedContent = event.encryptedContent; + if (encryptedContent == null) { + return; // event wasn't encrypted, this is a security risk + } + final request = outgoingShareRequests.values.firstWhereOrNull((r) => + r.room.id == event.content['room_id'] && + r.sessionId == event.content['session_id'] && + r.senderKey == event.content['sender_key']); + if (request == null || request.canceled) { + return; // no associated request found or it got canceled + } + final device = request.devices.firstWhereOrNull((d) => + d.userId == event.sender && + d.curve25519Key == encryptedContent['sender_key']); + if (device == null) { + return; // someone we didn't send our request to replied....better ignore this + } + // we add the sender key to the forwarded key chain + if (!(event.content['forwarding_curve25519_key_chain'] is List)) { + event.content['forwarding_curve25519_key_chain'] = []; + } + event.content['forwarding_curve25519_key_chain'] + .add(encryptedContent['sender_key']); + // TODO: verify that the keys work to decrypt a message + // alright, all checks out, let's go ahead and store this session + setInboundGroupSession( + request.room.id, request.sessionId, request.senderKey, event.content, + forwarded: true, + senderClaimedKeys: { + 'ed25519': event.content['sender_claimed_ed25519_key'], + }); + request.devices.removeWhere( + (k) => k.userId == device.userId && k.deviceId == device.deviceId); + outgoingShareRequests.remove(request.requestId); + // send cancel to all other devices + if (request.devices.isEmpty) { + return; // no need to send any cancellation + } + // Send with send-to-device messaging + final sendToDeviceMessage = { + 'action': 'request_cancellation', + 'request_id': request.requestId, + 'requesting_device_id': client.deviceID, + }; + final data = >>{}; + for (final device in request.devices) { + final userData = data[device.userId] ??= {}; + userData[device.deviceId!] = sendToDeviceMessage; + } + await client.sendToDevice( + EventTypes.RoomKeyRequest, + client.generateUniqueTransactionId(), + data, + ); + } else if (event.type == EventTypes.RoomKey) { + Logs().v( + '[KeyManager] Received room key with session ${event.content['session_id']}'); + final encryptedContent = event.encryptedContent; + if (encryptedContent == null) { + Logs().v('[KeyManager] not encrypted, ignoring...'); + return; // the event wasn't encrypted, this is a security risk; + } + final String roomId = event.content['room_id']; + final String sessionId = event.content['session_id']; + final sender_ed25519 = client.userDeviceKeys[event.sender] + ?.deviceKeys[event.content['requesting_device_id']]?.ed25519Key; + if (sender_ed25519 != null) { + event.content['sender_claimed_ed25519_key'] = sender_ed25519; + } + Logs().v('[KeyManager] Keeping room key'); + setInboundGroupSession( + roomId, sessionId, encryptedContent['sender_key'], event.content, + forwarded: false); + } + } + + void dispose() { + for (final sess in _outboundGroupSessions.values) { + sess.dispose(); + } + for (final entries in _inboundGroupSessions.values) { + for (final sess in entries.values) { + sess.dispose(); + } + } + } +} + +class KeyManagerKeyShareRequest { + final String requestId; + final List devices; + final Room room; + final String sessionId; + final String senderKey; + bool canceled; + + KeyManagerKeyShareRequest( + {required this.requestId, + List? devices, + required this.room, + required this.sessionId, + required this.senderKey, + this.canceled = false}) + : devices = devices ?? []; +} + +class RoomKeyRequest extends ToDeviceEvent { + KeyManager keyManager; + KeyManagerKeyShareRequest request; + RoomKeyRequest.fromToDeviceEvent( + ToDeviceEvent toDeviceEvent, this.keyManager, this.request) + : super( + sender: toDeviceEvent.sender, + content: toDeviceEvent.content, + type: toDeviceEvent.type); + + Room get room => request.room; + + DeviceKeys get requestingDevice => request.devices.first; + + Future forwardKey([int? index]) async { + if (request.canceled) { + keyManager.incomingShareRequests.remove(request.requestId); + return; // request is canceled, don't send anything + } + final room = this.room; + final session = await keyManager.loadInboundGroupSession( + room.id, request.sessionId, request.senderKey); + if (session?.inboundGroupSession == null) { + Logs().v("[KeyManager] Not forwarding key we don't have"); + return; + } + + final message = session!.content.copy(); + message['forwarding_curve25519_key_chain'] = + List.from(session.forwardingCurve25519KeyChain); + + message['sender_key'] = + (session.senderKey.isNotEmpty) ? session.senderKey : request.senderKey; + message['sender_claimed_ed25519_key'] = + session.senderClaimedKeys['ed25519'] ?? + (session.forwardingCurve25519KeyChain.isEmpty + ? keyManager.encryption.fingerprintKey + : null); + message['session_key'] = session.inboundGroupSession!.export_session( + index ?? session.inboundGroupSession!.first_known_index()); + // send the actual reply of the key back to the requester + await keyManager.client.sendToDeviceEncrypted( + [requestingDevice], + EventTypes.ForwardedRoomKey, + message, + ); + keyManager.incomingShareRequests.remove(request.requestId); + } +} + +RoomKeys _generateUploadKeys(_GenerateUploadKeysArgs args) { + final enc = olm.PkEncryption(); + try { + enc.set_recipient_key(args.pubkey); + // first we generate the payload to upload all the session keys in this chunk + final roomKeys = RoomKeys(rooms: {}); + for (final dbSession in args.dbSessions) { + final sess = SessionKey.fromDb(dbSession.dbSession, args.userId); + if (!sess.isValid) { + continue; + } + // create the room if it doesn't exist + final roomKeyBackup = + roomKeys.rooms[sess.roomId] ??= RoomKeyBackup(sessions: {}); + // generate the encrypted content + final payload = { + 'algorithm': AlgorithmTypes.megolmV1AesSha2, + 'forwarding_curve25519_key_chain': sess.forwardingCurve25519KeyChain, + 'sender_key': sess.senderKey, + 'sender_clencaimed_keys': sess.senderClaimedKeys, + 'session_key': sess.inboundGroupSession! + .export_session(sess.inboundGroupSession!.first_known_index()), + }; + // encrypt the content + final encrypted = enc.encrypt(json.encode(payload)); + // fetch the device, if available... + //final device = args.client.getUserDeviceKeysByCurve25519Key(sess.senderKey); + // aaaand finally add the session key to our payload + roomKeyBackup.sessions[sess.sessionId] = KeyBackupData( + firstMessageIndex: sess.inboundGroupSession!.first_known_index(), + forwardedCount: sess.forwardingCurve25519KeyChain.length, + isVerified: dbSession.verified, //device?.verified ?? false, + sessionData: { + 'ephemeral': encrypted.ephemeral, + 'ciphertext': encrypted.ciphertext, + 'mac': encrypted.mac, + }, + ); + } + return roomKeys; + } catch (e, s) { + Logs().e('[Key Manager] Error generating payload', e, s); + rethrow; + } finally { + enc.free(); + } +} + +class _DbInboundGroupSessionBundle { + _DbInboundGroupSessionBundle( + {required this.dbSession, required this.verified}); + + StoredInboundGroupSession dbSession; + bool verified; +} + +class _GenerateUploadKeysArgs { + _GenerateUploadKeysArgs( + {required this.pubkey, required this.dbSessions, required this.userId}); + + String pubkey; + List<_DbInboundGroupSessionBundle> dbSessions; + String userId; +} diff --git a/lib/encryption/key_verification_manager.dart b/lib/encryption/key_verification_manager.dart new file mode 100644 index 0000000..2b1dbfb --- /dev/null +++ b/lib/encryption/key_verification_manager.dart @@ -0,0 +1,148 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import '../matrix.dart'; +import 'encryption.dart'; +import 'utils/key_verification.dart'; + +class KeyVerificationManager { + final Encryption encryption; + Client get client => encryption.client; + + KeyVerificationManager(this.encryption); + + final Map _requests = {}; + + Future cleanup() async { + final Set entriesToDispose = {}; + for (final entry in _requests.entries) { + var dispose = entry.value.canceled || + entry.value.state == KeyVerificationState.done || + entry.value.state == KeyVerificationState.error; + if (!dispose) { + dispose = !(await entry.value.verifyActivity()); + } + if (dispose) { + entry.value.dispose(); + entriesToDispose.add(entry.key); + } + } + entriesToDispose.forEach(_requests.remove); + } + + void addRequest(KeyVerification request) { + if (request.transactionId == null) { + return; + } + _requests[request.transactionId!] = request; + } + + KeyVerification? getRequest(String requestId) => _requests[requestId]; + + Future handleToDeviceEvent(ToDeviceEvent event) async { + if (!event.type.startsWith('m.key.verification.') || + client.verificationMethods.isEmpty) { + return; + } + // we have key verification going on! + final transactionId = KeyVerification.getTransactionId(event.content); + if (transactionId == null) { + return; // TODO: send cancel with unknown transaction id + } + final request = _requests[transactionId]; + if (request != null) { + // make sure that new requests can't come from ourself + if (!{EventTypes.KeyVerificationRequest}.contains(event.type)) { + await request.handlePayload(event.type, event.content); + } + } else { + if (!{EventTypes.KeyVerificationRequest, EventTypes.KeyVerificationStart} + .contains(event.type)) { + return; // we can only start on these + } + final newKeyRequest = + KeyVerification(encryption: encryption, userId: event.sender); + await newKeyRequest.handlePayload(event.type, event.content); + if (newKeyRequest.state != KeyVerificationState.askAccept) { + // okay, something went wrong (unknown transaction id?), just dispose it + newKeyRequest.dispose(); + } else { + _requests[transactionId] = newKeyRequest; + client.onKeyVerificationRequest.add(newKeyRequest); + } + } + } + + Future handleEventUpdate(EventUpdate update) async { + final event = update.content; + final type = event['type'].startsWith('m.key.verification.') + ? event['type'] + : event['content']['msgtype']; + if (type == null || + !type.startsWith('m.key.verification.') || + client.verificationMethods.isEmpty) { + return; + } + if (type == EventTypes.KeyVerificationRequest) { + event['content']['timestamp'] = event['origin_server_ts']; + } + + final transactionId = + KeyVerification.getTransactionId(event['content']) ?? event['event_id']; + + final req = _requests[transactionId]; + if (req != null) { + final otherDeviceId = event['content']['from_device']; + if (event['sender'] != client.userID) { + await req.handlePayload(type, event['content'], event['event_id']); + } else if (event['sender'] == client.userID && + otherDeviceId != null && + otherDeviceId != client.deviceID) { + // okay, another of our devices answered + req.otherDeviceAccepted(); + req.dispose(); + _requests.remove(transactionId); + } + } else if (event['sender'] != client.userID) { + if (!{EventTypes.KeyVerificationRequest, EventTypes.KeyVerificationStart} + .contains(type)) { + return; // we can only start on these + } + final room = client.getRoomById(update.roomID) ?? + Room(id: update.roomID, client: client); + final newKeyRequest = KeyVerification( + encryption: encryption, userId: event['sender'], room: room); + await newKeyRequest.handlePayload( + type, event['content'], event['event_id']); + if (newKeyRequest.state != KeyVerificationState.askAccept) { + // something went wrong, let's just dispose the request + newKeyRequest.dispose(); + } else { + // new request! Let's notify it and stuff + _requests[transactionId] = newKeyRequest; + client.onKeyVerificationRequest.add(newKeyRequest); + } + } + } + + void dispose() { + for (final req in _requests.values) { + req.dispose(); + } + } +} diff --git a/lib/encryption/olm_manager.dart b/lib/encryption/olm_manager.dart new file mode 100644 index 0000000..05478d1 --- /dev/null +++ b/lib/encryption/olm_manager.dart @@ -0,0 +1,690 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:convert'; + +import 'package:canonical_json/canonical_json.dart'; +import 'package:collection/collection.dart'; +import 'package:matrix/matrix.dart'; +import 'package:olm/olm.dart' as olm; + +import '../encryption/utils/json_signature_check_extension.dart'; +import '../src/utils/run_in_root.dart'; +import 'encryption.dart'; +import 'utils/olm_session.dart'; + +class OlmManager { + final Encryption encryption; + Client get client => encryption.client; + olm.Account? _olmAccount; + + /// Returns the base64 encoded keys to store them in a store. + /// This String should **never** leave the device! + String? get pickledOlmAccount => + enabled ? _olmAccount!.pickle(client.userID!) : null; + String? get fingerprintKey => + enabled ? json.decode(_olmAccount!.identity_keys())['ed25519'] : null; + String? get identityKey => + enabled ? json.decode(_olmAccount!.identity_keys())['curve25519'] : null; + + bool get enabled => _olmAccount != null; + + OlmManager(this.encryption); + + /// A map from Curve25519 identity keys to existing olm sessions. + Map> get olmSessions => _olmSessions; + final Map> _olmSessions = {}; + + // NOTE(Nico): On initial login we pass null to create a new account + Future init(String? olmAccount) async { + if (olmAccount == null) { + try { + await olm.init(); + _olmAccount = olm.Account(); + _olmAccount!.create(); + if (!await uploadKeys(uploadDeviceKeys: true, updateDatabase: false)) { + throw ('Upload key failed'); + } + } catch (_) { + _olmAccount?.free(); + _olmAccount = null; + rethrow; + } + } else { + try { + await olm.init(); + _olmAccount = olm.Account(); + _olmAccount!.unpickle(client.userID!, olmAccount); + } catch (_) { + _olmAccount?.free(); + _olmAccount = null; + rethrow; + } + } + } + + /// Adds a signature to this json from this olm account and returns the signed + /// json. + Map signJson(Map payload) { + if (!enabled) throw ('Encryption is disabled'); + final Map? unsigned = payload['unsigned']; + final Map? signatures = payload['signatures']; + payload.remove('unsigned'); + payload.remove('signatures'); + final canonical = canonicalJson.encode(payload); + final signature = _olmAccount!.sign(String.fromCharCodes(canonical)); + if (signatures != null) { + payload['signatures'] = signatures; + } else { + payload['signatures'] = {}; + } + if (!payload['signatures'].containsKey(client.userID)) { + payload['signatures'][client.userID] = {}; + } + payload['signatures'][client.userID]['ed25519:${client.deviceID}'] = + signature; + if (unsigned != null) { + payload['unsigned'] = unsigned; + } + return payload; + } + + String signString(String s) { + return _olmAccount!.sign(s); + } + + /// Checks the signature of a signed json object. + @deprecated + bool checkJsonSignature(String key, Map signedJson, + String userId, String deviceId) { + if (!enabled) throw ('Encryption is disabled'); + final Map? signatures = signedJson['signatures']; + if (signatures == null || !signatures.containsKey(userId)) return false; + signedJson.remove('unsigned'); + signedJson.remove('signatures'); + if (!signatures[userId].containsKey('ed25519:$deviceId')) return false; + final String signature = signatures[userId]['ed25519:$deviceId']; + final canonical = canonicalJson.encode(signedJson); + final message = String.fromCharCodes(canonical); + var isValid = false; + final olmutil = olm.Utility(); + try { + olmutil.ed25519_verify(key, message, signature); + isValid = true; + } catch (e, s) { + isValid = false; + Logs().w('[LibOlm] Signature check failed', e, s); + } finally { + olmutil.free(); + } + return isValid; + } + + bool _uploadKeysLock = false; + + /// Generates new one time keys, signs everything and upload it to the server. + Future uploadKeys({ + bool uploadDeviceKeys = false, + int? oldKeyCount = 0, + bool updateDatabase = true, + bool? unusedFallbackKey = false, + }) async { + final _olmAccount = this._olmAccount; + if (_olmAccount == null) { + return true; + } + + if (_uploadKeysLock) { + return false; + } + _uploadKeysLock = true; + + try { + final signedOneTimeKeys = {}; + int? uploadedOneTimeKeysCount; + if (oldKeyCount != null) { + // check if we have OTKs that still need uploading. If we do, we don't try to generate new ones, + // instead we try to upload the old ones first + final oldOTKsNeedingUpload = json + .decode(_olmAccount.one_time_keys())['curve25519'] + .entries + .length as int; + // generate one-time keys + // we generate 2/3rds of max, so that other keys people may still have can + // still be used + final oneTimeKeysCount = + (_olmAccount.max_number_of_one_time_keys() * 2 / 3).floor() - + oldKeyCount - + oldOTKsNeedingUpload; + if (oneTimeKeysCount > 0) { + _olmAccount.generate_one_time_keys(oneTimeKeysCount); + } + uploadedOneTimeKeysCount = oneTimeKeysCount + oldOTKsNeedingUpload; + final Map oneTimeKeys = + json.decode(_olmAccount.one_time_keys()); + + // now sign all the one-time keys + for (final entry in oneTimeKeys['curve25519'].entries) { + final key = entry.key; + final value = entry.value; + signedOneTimeKeys['signed_curve25519:$key'] = signJson({ + 'key': value, + }); + } + } + + final signedFallbackKeys = {}; + if (encryption.isMinOlmVersion(3, 2, 0) && unusedFallbackKey == false) { + // we don't have an unused fallback key uploaded....so let's change that! + _olmAccount.generate_fallback_key(); + final fallbackKey = json.decode(_olmAccount.fallback_key()); + // now sign all the fallback keys + for (final entry in fallbackKey['curve25519'].entries) { + final key = entry.key; + final value = entry.value; + signedFallbackKeys['signed_curve25519:$key'] = signJson({ + 'key': value, + 'fallback': true, + }); + } + } + + // and now generate the payload to upload + final keysContent = { + if (uploadDeviceKeys) + 'device_keys': { + 'user_id': client.userID, + 'device_id': client.deviceID, + 'algorithms': [ + AlgorithmTypes.olmV1Curve25519AesSha2, + AlgorithmTypes.megolmV1AesSha2 + ], + 'keys': {}, + }, + }; + if (uploadDeviceKeys) { + final Map keys = + json.decode(_olmAccount.identity_keys()); + for (final entry in keys.entries) { + final algorithm = entry.key; + final value = entry.value; + keysContent['device_keys']['keys']['$algorithm:${client.deviceID}'] = + value; + } + keysContent['device_keys'] = + signJson(keysContent['device_keys'] as Map); + } + + // we save the generated OTKs into the database. + // in case the app gets killed during upload or the upload fails due to bad network + // we can still re-try later + if (updateDatabase) { + await client.database?.updateClientKeys(pickledOlmAccount!); + } + // Workaround: Make sure we stop if we got logged out in the meantime. + if (!client.isLogged()) return true; + final response = await client.uploadKeys( + deviceKeys: uploadDeviceKeys + ? MatrixDeviceKeys.fromJson(keysContent['device_keys']) + : null, + oneTimeKeys: signedOneTimeKeys, + fallbackKeys: signedFallbackKeys, + ); + // mark the OTKs as published and save that to datbase + _olmAccount.mark_keys_as_published(); + if (updateDatabase) { + await client.database?.updateClientKeys(pickledOlmAccount!); + } + return (uploadedOneTimeKeysCount != null && + response['signed_curve25519'] == uploadedOneTimeKeysCount) || + uploadedOneTimeKeysCount == null; + } finally { + _uploadKeysLock = false; + } + } + + void handleDeviceOneTimeKeysCount( + Map? countJson, List? unusedFallbackKeyTypes) { + if (!enabled) { + return; + } + final haveFallbackKeys = encryption.isMinOlmVersion(3, 2, 0); + // Check if there are at least half of max_number_of_one_time_keys left on the server + // and generate and upload more if not. + + // If the server did not send us a count, assume it is 0 + final keyCount = countJson?.tryGet('signed_curve25519') ?? 0; + + // If the server does not support fallback keys, it will not tell us about them. + // If the server supports them but has no key, upload a new one. + var unusedFallbackKey = true; + if (unusedFallbackKeyTypes?.contains('signed_curve25519') == false) { + unusedFallbackKey = false; + } + + // fixup accidental too many uploads. We delete only one of them so that the server has time to update the counts and because we will get rate limited anyway. + if (keyCount > _olmAccount!.max_number_of_one_time_keys()) { + final requestingKeysFrom = { + client.userID!: {client.deviceID!: 'signed_curve25519'} + }; + client.claimKeys(requestingKeysFrom, timeout: 10000); + } + + // Only upload keys if they are less than half of the max or we have no unused fallback key + if (keyCount < (_olmAccount!.max_number_of_one_time_keys() / 2) || + !unusedFallbackKey) { + uploadKeys( + oldKeyCount: keyCount < (_olmAccount!.max_number_of_one_time_keys() / 2) + ? keyCount + : null, + unusedFallbackKey: haveFallbackKeys ? unusedFallbackKey : null, + ); + } + } + + Future storeOlmSession(OlmSession session) async { + if (session.sessionId == null || session.pickledSession == null) { + return; + } + + _olmSessions[session.identityKey] ??= []; + final ix = _olmSessions[session.identityKey]! + .indexWhere((s) => s.sessionId == session.sessionId); + if (ix == -1) { + // add a new session + _olmSessions[session.identityKey]!.add(session); + } else { + // update an existing session + _olmSessions[session.identityKey]![ix] = session; + } + await client.database?.storeOlmSession( + session.identityKey, + session.sessionId!, + session.pickledSession!, + session.lastReceived?.millisecondsSinceEpoch ?? + DateTime.now().millisecondsSinceEpoch); + } + + ToDeviceEvent _decryptToDeviceEvent(ToDeviceEvent event) { + if (event.type != EventTypes.Encrypted) { + return event; + } + final content = event.parsedRoomEncryptedContent; + if (content.algorithm != AlgorithmTypes.olmV1Curve25519AesSha2) { + throw DecryptException(DecryptException.unknownAlgorithm); + } + if (content.ciphertextOlm == null || + !content.ciphertextOlm!.containsKey(identityKey)) { + throw DecryptException(DecryptException.isntSentForThisDevice); + } + String? plaintext; + final senderKey = content.senderKey; + final body = content.ciphertextOlm![identityKey]!.body; + final type = content.ciphertextOlm![identityKey]!.type; + if (type != 0 && type != 1) { + throw DecryptException(DecryptException.unknownMessageType); + } + final device = client.userDeviceKeys[event.sender]?.deviceKeys.values + .firstWhereOrNull((d) => d.curve25519Key == senderKey); + final existingSessions = olmSessions[senderKey]; + final updateSessionUsage = ([OlmSession? session]) => runInRoot(() async { + if (session != null) { + session.lastReceived = DateTime.now(); + await storeOlmSession(session); + } + if (device != null) { + device.lastActive = DateTime.now(); + await client.database?.setLastActiveUserDeviceKey( + device.lastActive.millisecondsSinceEpoch, + device.userId, + device.deviceId!); + } + }); + if (existingSessions != null) { + for (final session in existingSessions) { + if (session.session == null) { + continue; + } + if (type == 0 && session.session!.matches_inbound(body)) { + try { + plaintext = session.session!.decrypt(type, body); + } catch (e) { + // The message was encrypted during this session, but is unable to decrypt + throw DecryptException( + DecryptException.decryptionFailed, e.toString()); + } + updateSessionUsage(session); + break; + } else if (type == 1) { + try { + plaintext = session.session!.decrypt(type, body); + updateSessionUsage(session); + break; + } catch (_) { + plaintext = null; + } + } + } + } + if (plaintext == null && type != 0) { + throw DecryptException(DecryptException.unableToDecryptWithAnyOlmSession); + } + + if (plaintext == null) { + final newSession = olm.Session(); + try { + newSession.create_inbound_from(_olmAccount!, senderKey, body); + _olmAccount!.remove_one_time_keys(newSession); + client.database?.updateClientKeys(pickledOlmAccount!); + plaintext = newSession.decrypt(type, body); + runInRoot(() => storeOlmSession(OlmSession( + key: client.userID!, + identityKey: senderKey, + sessionId: newSession.session_id(), + session: newSession, + lastReceived: DateTime.now(), + ))); + updateSessionUsage(); + } catch (e) { + newSession.free(); + throw DecryptException(DecryptException.decryptionFailed, e.toString()); + } + } + final Map plainContent = json.decode(plaintext); + if (plainContent['sender'] != event.sender) { + throw DecryptException(DecryptException.senderDoesntMatch); + } + if (plainContent['recipient'] != client.userID) { + throw DecryptException(DecryptException.recipientDoesntMatch); + } + if (plainContent['recipient_keys'] is Map && + plainContent['recipient_keys']['ed25519'] is String && + plainContent['recipient_keys']['ed25519'] != fingerprintKey) { + throw DecryptException(DecryptException.ownFingerprintDoesntMatch); + } + return ToDeviceEvent( + content: plainContent['content'], + encryptedContent: event.content, + type: plainContent['type'], + sender: event.sender, + ); + } + + Future> getOlmSessionsFromDatabase(String senderKey) async { + final olmSessions = + await client.database?.getOlmSessions(senderKey, client.userID!); + return olmSessions?.where((sess) => sess.isValid).toList() ?? []; + } + + Future getOlmSessionsForDevicesFromDatabase( + List senderKeys) async { + final rows = await client.database?.getOlmSessionsForDevices( + senderKeys, + client.userID!, + ); + final res = >{}; + for (final sess in rows ?? []) { + res[sess.identityKey] ??= []; + if (sess.isValid) { + res[sess.identityKey]!.add(sess); + } + } + for (final entry in res.entries) { + _olmSessions[entry.key] = entry.value; + } + } + + Future> getOlmSessions(String senderKey, + {bool getFromDb = true}) async { + var sess = olmSessions[senderKey]; + if ((getFromDb) && (sess == null || sess.isEmpty)) { + final sessions = await getOlmSessionsFromDatabase(senderKey); + if (sessions.isEmpty) { + return []; + } + sess = _olmSessions[senderKey] = sessions; + } + if (sess == null) { + return []; + } + sess.sort((a, b) => a.lastReceived == b.lastReceived + ? (a.sessionId ?? '').compareTo(b.sessionId ?? '') + : (b.lastReceived ?? DateTime(0)) + .compareTo(a.lastReceived ?? DateTime(0))); + return sess; + } + + final Map _restoredOlmSessionsTime = {}; + + Future restoreOlmSession(String userId, String senderKey) async { + if (!client.userDeviceKeys.containsKey(userId)) { + return; + } + final device = client.userDeviceKeys[userId]!.deviceKeys.values + .firstWhereOrNull((d) => d.curve25519Key == senderKey); + if (device == null) { + return; + } + // per device only one olm session per hour should be restored + final mapKey = '$userId;$senderKey'; + if (_restoredOlmSessionsTime.containsKey(mapKey) && + DateTime.now() + .subtract(Duration(hours: 1)) + .isBefore(_restoredOlmSessionsTime[mapKey]!)) { + return; + } + _restoredOlmSessionsTime[mapKey] = DateTime.now(); + await startOutgoingOlmSessions([device]); + await client.sendToDeviceEncrypted([device], EventTypes.Dummy, {}); + } + + Future decryptToDeviceEvent(ToDeviceEvent event) async { + if (event.type != EventTypes.Encrypted) { + return event; + } + final senderKey = event.parsedRoomEncryptedContent.senderKey; + final loadFromDb = () async { + final sessions = await getOlmSessions(senderKey); + return sessions.isNotEmpty; + }; + if (!_olmSessions.containsKey(senderKey)) { + await loadFromDb(); + } + try { + event = _decryptToDeviceEvent(event); + if (event.type != EventTypes.Encrypted || !(await loadFromDb())) { + return event; + } + // retry to decrypt! + return _decryptToDeviceEvent(event); + } catch (_) { + // okay, the thing errored while decrypting. It is safe to assume that the olm session is corrupt and we should generate a new one + + // ignore: unawaited_futures + runInRoot(() => restoreOlmSession(event.senderId, senderKey)); + + rethrow; + } + } + + Future startOutgoingOlmSessions(List deviceKeys) async { + Logs().v( + '[OlmManager] Starting session with ${deviceKeys.length} devices...'); + final requestingKeysFrom = >{}; + for (final device in deviceKeys) { + if (requestingKeysFrom[device.userId] == null) { + requestingKeysFrom[device.userId] = {}; + } + requestingKeysFrom[device.userId]![device.deviceId!] = + 'signed_curve25519'; + } + + final response = await client.claimKeys(requestingKeysFrom, timeout: 10000); + + for (final userKeysEntry in response.oneTimeKeys.entries) { + final userId = userKeysEntry.key; + for (final deviceKeysEntry in userKeysEntry.value.entries) { + final deviceId = deviceKeysEntry.key; + final fingerprintKey = + client.userDeviceKeys[userId]!.deviceKeys[deviceId]!.ed25519Key; + final identityKey = + client.userDeviceKeys[userId]!.deviceKeys[deviceId]!.curve25519Key; + for (final Map deviceKey + in deviceKeysEntry.value.values) { + if (fingerprintKey == null || + identityKey == null || + !deviceKey.checkJsonSignature(fingerprintKey, userId, deviceId)) { + continue; + } + Logs().v('[OlmManager] Starting session with $userId:$deviceId'); + final session = olm.Session(); + try { + session.create_outbound( + _olmAccount!, identityKey, deviceKey['key']); + await storeOlmSession(OlmSession( + key: client.userID!, + identityKey: identityKey, + sessionId: session.session_id(), + session: session, + lastReceived: + DateTime.now(), // we want to use a newly created session + )); + } catch (e, s) { + session.free(); + Logs() + .e('[LibOlm] Could not create new outbound olm session', e, s); + } + } + } + } + } + + Future> encryptToDeviceMessagePayload( + DeviceKeys device, String type, Map payload, + {bool getFromDb = true}) async { + final sess = + await getOlmSessions(device.curve25519Key!, getFromDb: getFromDb); + if (sess.isEmpty) { + throw ('No olm session found for ${device.userId}:${device.deviceId}'); + } + final fullPayload = { + 'type': type, + 'content': payload, + 'sender': client.userID, + 'keys': {'ed25519': fingerprintKey}, + 'recipient': device.userId, + 'recipient_keys': {'ed25519': device.ed25519Key}, + }; + final encryptResult = sess.first.session!.encrypt(json.encode(fullPayload)); + await storeOlmSession(sess.first); + if (client.database != null) { + // ignore: unawaited_futures + runInRoot(() => client.database?.setLastSentMessageUserDeviceKey( + json.encode({ + 'type': type, + 'content': payload, + }), + device.userId, + device.deviceId!)); + } + final encryptedBody = { + 'algorithm': AlgorithmTypes.olmV1Curve25519AesSha2, + 'sender_key': identityKey, + 'ciphertext': {}, + }; + encryptedBody['ciphertext'][device.curve25519Key] = { + 'type': encryptResult.type, + 'body': encryptResult.body, + }; + return encryptedBody; + } + + Future> encryptToDeviceMessage( + List deviceKeys, + String type, + Map payload) async { + final data = >>{}; + // first check if any of our sessions we want to encrypt for are in the database + if (client.database != null) { + await getOlmSessionsForDevicesFromDatabase( + deviceKeys.map((d) => d.curve25519Key!).toList()); + } + final deviceKeysWithoutSession = List.from(deviceKeys); + deviceKeysWithoutSession.removeWhere((DeviceKeys deviceKeys) => + olmSessions[deviceKeys.curve25519Key]?.isNotEmpty ?? false); + if (deviceKeysWithoutSession.isNotEmpty) { + await startOutgoingOlmSessions(deviceKeysWithoutSession); + } + for (final device in deviceKeys) { + final userData = data[device.userId] ??= {}; + try { + userData[device.deviceId!] = await encryptToDeviceMessagePayload( + device, type, payload, + getFromDb: false); + } catch (e, s) { + Logs().w('[LibOlm] Error encrypting to-device event', e, s); + continue; + } + } + return data; + } + + Future handleToDeviceEvent(ToDeviceEvent event) async { + if (event.type == EventTypes.Dummy) { + // We receive dan encrypted m.dummy. This means that the other end was not able to + // decrypt our last message. So, we re-send it. + final encryptedContent = event.encryptedContent; + if (encryptedContent == null || client.database == null) { + return; + } + final device = client.getUserDeviceKeysByCurve25519Key( + encryptedContent.tryGet('sender_key') ?? ''); + if (device == null) { + return; // device not found + } + Logs().v( + '[OlmManager] Device ${device.userId}:${device.deviceId} generated a new olm session, replaying last sent message...'); + final lastSentMessageRes = await client.database + ?.getLastSentMessageUserDeviceKey(device.userId, device.deviceId!); + if (lastSentMessageRes == null || + lastSentMessageRes.isEmpty || + lastSentMessageRes.first.isEmpty) { + return; + } + final lastSentMessage = json.decode(lastSentMessageRes.first); + // We do *not* want to re-play m.dummy events, as they hold no value except of saying + // what olm session is the most recent one. In fact, if we *do* replay them, then + // we can easily land in an infinite ping-pong trap! + if (lastSentMessage['type'] != EventTypes.Dummy) { + // okay, time to send the message! + await client.sendToDeviceEncrypted( + [device], lastSentMessage['type'], lastSentMessage['content']); + } + } + } + + void dispose() { + for (final sessions in olmSessions.values) { + for (final sess in sessions) { + sess.dispose(); + } + } + _olmAccount?.free(); + _olmAccount = null; + } +} diff --git a/lib/encryption/ssss.dart b/lib/encryption/ssss.dart new file mode 100644 index 0000000..60e4dab --- /dev/null +++ b/lib/encryption/ssss.dart @@ -0,0 +1,755 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:async'; +import 'dart:convert'; +import 'dart:core'; +import 'dart:typed_data'; + +import 'package:base58check/base58.dart'; +import 'package:crypto/crypto.dart'; +import 'package:collection/collection.dart'; +import 'package:matrix/encryption/utils/base64_unpadded.dart'; + +import '../matrix.dart'; +import '../src/utils/crypto/crypto.dart' as uc; +import '../src/utils/run_in_root.dart'; +import 'encryption.dart'; +import 'utils/ssss_cache.dart'; + +const cacheTypes = { + EventTypes.CrossSigningSelfSigning, + EventTypes.CrossSigningUserSigning, + EventTypes.MegolmBackup, +}; + +const zeroStr = + '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'; +const base58Alphabet = + '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; +const base58 = Base58Codec(base58Alphabet); +const olmRecoveryKeyPrefix = [0x8B, 0x01]; +const ssssKeyLength = 32; +const pbkdf2DefaultIterations = 500000; +const pbkdf2SaltLength = 64; + +/// SSSS: **S**ecure **S**ecret **S**torage and **S**haring +/// Read more about SSSS at: +/// https://matrix.org/docs/guides/implementing-more-advanced-e-2-ee-features-such-as-cross-signing#3-implementing-ssss +class SSSS { + final Encryption encryption; + + Client get client => encryption.client; + final pendingShareRequests = {}; + final _validators = Function(String)>{}; + final _cacheCallbacks = Function(String)>{}; + final Map _cache = {}; + + SSSS(this.encryption); + + // for testing + Future clearCache() async { + await client.database?.clearSSSSCache(); + _cache.clear(); + } + + static _DerivedKeys deriveKeys(Uint8List key, String name) { + final zerosalt = Uint8List(8); + final prk = Hmac(sha256, zerosalt).convert(key); + final b = Uint8List(1); + b[0] = 1; + final aesKey = Hmac(sha256, prk.bytes).convert(utf8.encode(name) + b); + b[0] = 2; + final hmacKey = + Hmac(sha256, prk.bytes).convert(aesKey.bytes + utf8.encode(name) + b); + return _DerivedKeys( + aesKey: Uint8List.fromList(aesKey.bytes), + hmacKey: Uint8List.fromList(hmacKey.bytes)); + } + + static Future<_Encrypted> encryptAes(String data, Uint8List key, String name, + [String? ivStr]) async { + Uint8List iv; + if (ivStr != null) { + iv = base64decodeUnpadded(ivStr); + } else { + iv = Uint8List.fromList(uc.secureRandomBytes(16)); + } + // we need to clear bit 63 of the IV + iv[8] &= 0x7f; + + final keys = deriveKeys(key, name); + + final plain = Uint8List.fromList(utf8.encode(data)); + final ciphertext = await uc.aesCtr.encrypt(plain, keys.aesKey, iv); + + final hmac = Hmac(sha256, keys.hmacKey).convert(ciphertext); + + return _Encrypted( + iv: base64.encode(iv), + ciphertext: base64.encode(ciphertext), + mac: base64.encode(hmac.bytes)); + } + + static Future decryptAes( + _Encrypted data, Uint8List key, String name) async { + final keys = deriveKeys(key, name); + final cipher = base64decodeUnpadded(data.ciphertext); + final hmac = base64 + .encode(Hmac(sha256, keys.hmacKey).convert(cipher).bytes) + .replaceAll(RegExp(r'=+$'), ''); + if (hmac != data.mac.replaceAll(RegExp(r'=+$'), '')) { + throw Exception('Bad MAC'); + } + final decipher = await uc.aesCtr + .encrypt(cipher, keys.aesKey, base64decodeUnpadded(data.iv)); + return String.fromCharCodes(decipher); + } + + static Uint8List decodeRecoveryKey(String recoveryKey) { + final result = base58.decode(recoveryKey.replaceAll(RegExp(r'\s'), '')); + + final parity = result.fold(0, (a, b) => a ^ b); + if (parity != 0) { + throw Exception('Incorrect parity'); + } + + for (var i = 0; i < olmRecoveryKeyPrefix.length; i++) { + if (result[i] != olmRecoveryKeyPrefix[i]) { + throw Exception('Incorrect prefix'); + } + } + + if (result.length != olmRecoveryKeyPrefix.length + ssssKeyLength + 1) { + throw Exception('Incorrect length'); + } + + return Uint8List.fromList(result.sublist(olmRecoveryKeyPrefix.length, + olmRecoveryKeyPrefix.length + ssssKeyLength)); + } + + static String encodeRecoveryKey(Uint8List recoveryKey) { + final keyToEncode = [...olmRecoveryKeyPrefix, ...recoveryKey]; + final parity = keyToEncode.fold(0, (a, b) => a ^ b); + keyToEncode.add(parity); + // base58-encode and add a space every four chars + return base58 + .encode(keyToEncode) + .replaceAllMapped(RegExp(r'.{4}'), (s) => '${s.group(0)} ') + .trim(); + } + + static Future keyFromPassphrase( + String passphrase, PassphraseInfo info) async { + if (info.algorithm != AlgorithmTypes.pbkdf2) { + throw Exception('Unknown algorithm'); + } + if (info.iterations == null) { + throw Exception('Passphrase info without iterations'); + } + if (info.salt == null) { + throw Exception('Passphrase info without salt'); + } + return await uc.pbkdf2( + Uint8List.fromList(utf8.encode(passphrase)), + Uint8List.fromList(utf8.encode(info.salt!)), + uc.sha512, + info.iterations!, + info.bits ?? 256); + } + + void setValidator(String type, FutureOr Function(String) validator) { + _validators[type] = validator; + } + + void setCacheCallback(String type, FutureOr Function(String) callback) { + _cacheCallbacks[type] = callback; + } + + String? get defaultKeyId => client + .accountData[EventTypes.SecretStorageDefaultKey] + ?.parsedSecretStorageDefaultKeyContent + .key; + + Future setDefaultKeyId(String keyId) async { + await client.setAccountData( + client.userID!, + EventTypes.SecretStorageDefaultKey, + SecretStorageDefaultKeyContent(key: keyId).toJson(), + ); + } + + SecretStorageKeyContent? getKey(String keyId) { + return client.accountData[EventTypes.secretStorageKey(keyId)] + ?.parsedSecretStorageKeyContent; + } + + bool isKeyValid(String keyId) => + getKey(keyId)?.algorithm == AlgorithmTypes.secretStorageV1AesHmcSha2; + + /// Creates a new secret storage key, optional encrypts it with [passphrase] + /// and stores it in the user's `accountData`. + Future createKey([String? passphrase]) async { + Uint8List privateKey; + final content = SecretStorageKeyContent(); + if (passphrase != null) { + // we need to derive the key off of the passphrase + content.passphrase = PassphraseInfo( + iterations: pbkdf2DefaultIterations, + salt: base64.encode(uc.secureRandomBytes(pbkdf2SaltLength)), + algorithm: AlgorithmTypes.pbkdf2, + bits: ssssKeyLength * 8, + ); + privateKey = await client + .runInBackground( + _keyFromPassphrase, + _KeyFromPassphraseArgs( + passphrase: passphrase, + info: content.passphrase!, + ), + ) + .timeout(Duration(seconds: 10)); + } else { + // we need to just generate a new key from scratch + privateKey = Uint8List.fromList(uc.secureRandomBytes(ssssKeyLength)); + } + // now that we have the private key, let's create the iv and mac + final encrypted = await encryptAes(zeroStr, privateKey, ''); + content.iv = encrypted.iv; + content.mac = encrypted.mac; + content.algorithm = AlgorithmTypes.secretStorageV1AesHmcSha2; + + const keyidByteLength = 24; + + // make sure we generate a unique key id + final keyId = () sync* { + for (;;) { + yield base64.encode(uc.secureRandomBytes(keyidByteLength)); + } + }() + .firstWhere((keyId) => getKey(keyId) == null); + + final accountDataType = EventTypes.secretStorageKey(keyId); + // noooow we set the account data + final waitForAccountData = client.onSync.stream.firstWhere((syncUpdate) => + syncUpdate.accountData != null && + syncUpdate.accountData! + .any((accountData) => accountData.type == accountDataType)); + await client.setAccountData( + client.userID!, accountDataType, content.toJson()); + await waitForAccountData; + + final key = open(keyId); + await key.setPrivateKey(privateKey); + return key; + } + + Future checkKey(Uint8List key, SecretStorageKeyContent info) async { + if (info.algorithm == AlgorithmTypes.secretStorageV1AesHmcSha2) { + if ((info.mac is String) && (info.iv is String)) { + final encrypted = await encryptAes(zeroStr, key, '', info.iv); + return info.mac!.replaceAll(RegExp(r'=+$'), '') == + encrypted.mac.replaceAll(RegExp(r'=+$'), ''); + } else { + // no real information about the key, assume it is valid + return true; + } + } else { + throw Exception('Unknown Algorithm'); + } + } + + bool isSecret(String type) => + client.accountData[type] != null && + client.accountData[type]!.content['encrypted'] is Map; + + Future getCached(String type) async { + if (client.database == null) { + return null; + } + // check if it is still valid + final keys = keyIdsFromType(type); + if (keys == null) { + return null; + } + final isValid = (dbEntry) => + keys.contains(dbEntry.keyId) && + dbEntry.ciphertext != null && + client.accountData[type]?.content['encrypted'][dbEntry.keyId] + ['ciphertext'] == + dbEntry.ciphertext; + if (_cache.containsKey(type) && isValid(_cache[type])) { + return _cache[type]?.content; + } + final ret = await client.database?.getSSSSCache(type); + if (ret == null) { + return null; + } + if (isValid(ret)) { + _cache[type] = ret; + return ret.content; + } + return null; + } + + Future getStored(String type, String keyId, Uint8List key) async { + final secretInfo = client.accountData[type]; + if (secretInfo == null) { + throw Exception('Not found'); + } + if (!(secretInfo.content['encrypted'] is Map)) { + throw Exception('Content is not encrypted'); + } + if (!(secretInfo.content['encrypted'][keyId] is Map)) { + throw Exception('Wrong / unknown key'); + } + final enc = secretInfo.content['encrypted'][keyId]; + final encryptInfo = _Encrypted( + iv: enc['iv'], ciphertext: enc['ciphertext'], mac: enc['mac']); + final decrypted = await decryptAes(encryptInfo, key, type); + final db = client.database; + if (cacheTypes.contains(type) && db != null) { + // cache the thing + await db.storeSSSSCache(type, keyId, enc['ciphertext'], decrypted); + if (_cacheCallbacks.containsKey(type) && await getCached(type) == null) { + _cacheCallbacks[type]!(decrypted); + } + } + return decrypted; + } + + Future store(String type, String secret, String keyId, Uint8List key, + {bool add = false}) async { + final encrypted = await encryptAes(secret, key, type); + Map? content; + if (add && client.accountData[type] != null) { + content = client.accountData[type]!.content.copy(); + if (!(content['encrypted'] is Map)) { + content['encrypted'] = {}; + } + } + content ??= { + 'encrypted': {}, + }; + content['encrypted'][keyId] = { + 'iv': encrypted.iv, + 'ciphertext': encrypted.ciphertext, + 'mac': encrypted.mac, + }; + // store the thing in your account data + await client.setAccountData(client.userID!, type, content); + final db = client.database; + if (cacheTypes.contains(type) && db != null) { + // cache the thing + await db.storeSSSSCache(type, keyId, encrypted.ciphertext, secret); + if (_cacheCallbacks.containsKey(type) && await getCached(type) == null) { + _cacheCallbacks[type]!(secret); + } + } + } + + Future validateAndStripOtherKeys( + String type, String secret, String keyId, Uint8List key) async { + if (await getStored(type, keyId, key) != secret) { + throw Exception('Secrets do not match up!'); + } + // now remove all other keys + final content = client.accountData[type]?.content.copy(); + if (content == null) { + throw Exception('Key has no content!'); + } + + final otherKeys = + Set.from(content['encrypted'].keys.where((k) => k != keyId)); + content['encrypted'].removeWhere((k, v) => otherKeys.contains(k)); + // yes, we are paranoid... + if (await getStored(type, keyId, key) != secret) { + throw Exception('Secrets do not match up!'); + } + // store the thing in your account data + await client.setAccountData(client.userID!, type, content); + if (cacheTypes.contains(type)) { + // cache the thing + await client.database?.storeSSSSCache( + type, keyId, content['encrypted'][keyId]['ciphertext'], secret); + } + } + + Future maybeCacheAll(String keyId, Uint8List key) async { + for (final type in cacheTypes) { + final secret = await getCached(type); + if (secret == null) { + try { + await getStored(type, keyId, key); + } catch (_) { + // the entry wasn't stored, just ignore it + } + } + } + } + + Future maybeRequestAll([List? devices]) async { + for (final type in cacheTypes) { + if (keyIdsFromType(type) != null) { + final secret = await getCached(type); + if (secret == null) { + await request(type, devices); + } + } + } + } + + Future request(String type, [List? devices]) async { + // only send to own, verified devices + Logs().i('[SSSS] Requesting type $type...'); + if (devices == null || devices.isEmpty) { + if (!client.userDeviceKeys.containsKey(client.userID)) { + Logs().w('[SSSS] User does not have any devices'); + return; + } + devices = + client.userDeviceKeys[client.userID]!.deviceKeys.values.toList(); + } + devices.removeWhere((DeviceKeys d) => + d.userId != client.userID || + !d.verified || + d.blocked || + d.deviceId == client.deviceID); + if (devices.isEmpty) { + Logs().w('[SSSS] No devices'); + return; + } + final requestId = client.generateUniqueTransactionId(); + final request = _ShareRequest( + requestId: requestId, + type: type, + devices: devices, + ); + pendingShareRequests[requestId] = request; + await client.sendToDeviceEncrypted(devices, EventTypes.SecretRequest, { + 'action': 'request', + 'requesting_device_id': client.deviceID, + 'request_id': requestId, + 'name': type, + }); + } + + DateTime? _lastCacheRequest; + bool _isPeriodicallyRequestingMissingCache = false; + + Future periodicallyRequestMissingCache() async { + if (_isPeriodicallyRequestingMissingCache || + (_lastCacheRequest != null && + DateTime.now() + .subtract(Duration(minutes: 15)) + .isBefore(_lastCacheRequest!)) || + client.isUnknownSession) { + // we are already requesting right now or we attempted to within the last 15 min + return; + } + _lastCacheRequest = DateTime.now(); + _isPeriodicallyRequestingMissingCache = true; + try { + await maybeRequestAll(); + } finally { + _isPeriodicallyRequestingMissingCache = false; + } + } + + Future handleToDeviceEvent(ToDeviceEvent event) async { + if (event.type == EventTypes.SecretRequest) { + // got a request to share a secret + Logs().i('[SSSS] Received sharing request...'); + if (event.sender != client.userID || + !client.userDeviceKeys.containsKey(client.userID)) { + Logs().i('[SSSS] Not sent by us'); + return; // we aren't asking for it ourselves, so ignore + } + if (event.content['action'] != 'request') { + Logs().i('[SSSS] it is actually a cancelation'); + return; // not actually requesting, so ignore + } + final device = client.userDeviceKeys[client.userID]! + .deviceKeys[event.content['requesting_device_id']]; + if (device == null || !device.verified || device.blocked) { + Logs().i('[SSSS] Unknown / unverified devices, ignoring'); + return; // nope....unknown or untrusted device + } + // alright, all seems fine...let's check if we actually have the secret they are asking for + final type = event.content['name']; + final secret = await getCached(type); + if (secret == null) { + Logs() + .i('[SSSS] We don\'t have the secret for $type ourself, ignoring'); + return; // seems like we don't have this, either + } + // okay, all checks out...time to share this secret! + Logs().i('[SSSS] Replying with secret for $type'); + await client.sendToDeviceEncrypted( + [device], + EventTypes.SecretSend, + { + 'request_id': event.content['request_id'], + 'secret': secret, + }); + } else if (event.type == EventTypes.SecretSend) { + // receiving a secret we asked for + Logs().i('[SSSS] Received shared secret...'); + final encryptedContent = event.encryptedContent; + if (event.sender != client.userID || + !pendingShareRequests.containsKey(event.content['request_id']) || + encryptedContent == null) { + Logs().i('[SSSS] Not by us or unknown request'); + return; // we have no idea what we just received + } + final request = pendingShareRequests[event.content['request_id']]!; + // alright, as we received a known request id, let's check if the sender is valid + final device = request.devices.firstWhereOrNull((d) => + d.userId == event.sender && + d.curve25519Key == encryptedContent['sender_key']); + if (device == null) { + Logs().i('[SSSS] Someone else replied?'); + return; // someone replied whom we didn't send the share request to + } + final secret = event.content['secret']; + if (!(event.content['secret'] is String)) { + Logs().i('[SSSS] Secret wasn\'t a string'); + return; // the secret wasn't a string....wut? + } + // let's validate if the secret is, well, valid + if (_validators.containsKey(request.type) && + !(await _validators[request.type]!(secret))) { + Logs().i('[SSSS] The received secret was invalid'); + return; // didn't pass the validator + } + pendingShareRequests.remove(request.requestId); + if (request.start.add(Duration(minutes: 15)).isBefore(DateTime.now())) { + Logs().i('[SSSS] Request is too far in the past'); + return; // our request is more than 15min in the past...better not trust it anymore + } + Logs().i('[SSSS] Secret for type ${request.type} is ok, storing it'); + final db = client.database; + if (db != null) { + final keyId = keyIdFromType(request.type); + if (keyId != null) { + final ciphertext = client.accountData[request.type]! + .content['encrypted'][keyId]['ciphertext']; + await db.storeSSSSCache(request.type, keyId, ciphertext, secret); + if (_cacheCallbacks.containsKey(request.type)) { + _cacheCallbacks[request.type]!(secret); + } + } + } + } + } + + Set? keyIdsFromType(String type) { + final data = client.accountData[type]; + if (data == null) { + return null; + } + if (data.content['encrypted'] is Map) { + return data.content['encrypted'].keys.toSet(); + } + return null; + } + + String? keyIdFromType(String type) { + final keys = keyIdsFromType(type); + if (keys == null || keys.isEmpty) { + return null; + } + if (keys.contains(defaultKeyId)) { + return defaultKeyId; + } + return keys.first; + } + + OpenSSSS open([String? identifier]) { + identifier ??= defaultKeyId; + if (identifier == null) { + throw Exception('Dont know what to open'); + } + final keyToOpen = keyIdFromType(identifier) ?? identifier; + final key = getKey(keyToOpen); + if (key == null) { + throw Exception('Unknown key to open'); + } + return OpenSSSS(ssss: this, keyId: keyToOpen, keyData: key); + } +} + +class _ShareRequest { + final String requestId; + final String type; + final List devices; + final DateTime start; + + _ShareRequest( + {required this.requestId, required this.type, required this.devices}) + : start = DateTime.now(); +} + +class _Encrypted { + final String iv; + final String ciphertext; + final String mac; + + _Encrypted({required this.iv, required this.ciphertext, required this.mac}); +} + +class _DerivedKeys { + final Uint8List aesKey; + final Uint8List hmacKey; + + _DerivedKeys({required this.aesKey, required this.hmacKey}); +} + +class OpenSSSS { + final SSSS ssss; + final String keyId; + final SecretStorageKeyContent keyData; + + OpenSSSS({required this.ssss, required this.keyId, required this.keyData}); + + Uint8List? privateKey; + + bool get isUnlocked => privateKey != null; + + bool get hasPassphrase => keyData.passphrase != null; + + String? get recoveryKey => + isUnlocked ? SSSS.encodeRecoveryKey(privateKey!) : null; + + Future unlock( + {String? passphrase, + String? recoveryKey, + String? keyOrPassphrase, + bool postUnlock = true}) async { + if (keyOrPassphrase != null) { + try { + await unlock(recoveryKey: keyOrPassphrase, postUnlock: postUnlock); + } catch (_) { + if (hasPassphrase) { + await unlock(passphrase: keyOrPassphrase, postUnlock: postUnlock); + } else { + rethrow; + } + } + return; + } else if (passphrase != null) { + if (!hasPassphrase) { + throw Exception( + 'Tried to unlock with passphrase while key does not have a passphrase'); + } + privateKey = await ssss.client + .runInBackground( + _keyFromPassphrase, + _KeyFromPassphraseArgs( + passphrase: passphrase, + info: keyData.passphrase!, + ), + ) + .timeout(Duration(seconds: 10)); + } else if (recoveryKey != null) { + privateKey = SSSS.decodeRecoveryKey(recoveryKey); + } else { + throw Exception('Nothing specified'); + } + // verify the validity of the key + if (!await ssss.checkKey(privateKey!, keyData)) { + privateKey = null; + throw Exception('Inalid key'); + } + if (postUnlock) { + await runInRoot(() => _postUnlock()); + } + } + + Future setPrivateKey(Uint8List key) async { + if (!await ssss.checkKey(key, keyData)) { + throw Exception('Invalid key'); + } + privateKey = key; + } + + Future getStored(String type) async { + final privateKey = this.privateKey; + if (privateKey == null) { + throw Exception('SSSS not unlocked'); + } + return await ssss.getStored(type, keyId, privateKey); + } + + Future store(String type, String secret, {bool add = false}) async { + final privateKey = this.privateKey; + if (privateKey == null) { + throw Exception('SSSS not unlocked'); + } + await ssss.store(type, secret, keyId, privateKey, add: add); + } + + Future validateAndStripOtherKeys(String type, String secret) async { + final privateKey = this.privateKey; + if (privateKey == null) { + throw Exception('SSSS not unlocked'); + } + await ssss.validateAndStripOtherKeys(type, secret, keyId, privateKey); + } + + Future maybeCacheAll() async { + final privateKey = this.privateKey; + if (privateKey == null) { + throw Exception('SSSS not unlocked'); + } + await ssss.maybeCacheAll(keyId, privateKey); + } + + Future _postUnlock() async { + // first try to cache all secrets that aren't cached yet + await maybeCacheAll(); + // now try to self-sign + if (ssss.encryption.crossSigning.enabled && + ssss.client.userDeviceKeys[ssss.client.userID]?.masterKey != null && + (ssss + .keyIdsFromType(EventTypes.CrossSigningMasterKey) + ?.contains(keyId) ?? + false) && + (ssss.client.isUnknownSession || + ssss.client.userDeviceKeys[ssss.client.userID]!.masterKey + ?.directVerified != + true)) { + try { + await ssss.encryption.crossSigning.selfSign(openSsss: this); + } catch (e, s) { + Logs().e('[SSSS] Failed to self-sign', e, s); + } + } + } +} + +class _KeyFromPassphraseArgs { + final String passphrase; + final PassphraseInfo info; + + _KeyFromPassphraseArgs({required this.passphrase, required this.info}); +} + +Future _keyFromPassphrase(_KeyFromPassphraseArgs args) async { + return await SSSS.keyFromPassphrase(args.passphrase, args.info); +} diff --git a/lib/encryption/utils/base64_unpadded.dart b/lib/encryption/utils/base64_unpadded.dart new file mode 100644 index 0000000..c66b64e --- /dev/null +++ b/lib/encryption/utils/base64_unpadded.dart @@ -0,0 +1,13 @@ +import 'dart:convert'; +import 'dart:typed_data'; + +/// decodes base64 +/// +/// Dart's native [base64.decode] requires a padded base64 input String. +/// This function allows unpadded base64 too. +/// +/// See: https://github.com/dart-lang/sdk/issues/39510 +Uint8List base64decodeUnpadded(String s) { + final needEquals = (4 - (s.length % 4)) % 4; + return base64.decode(s + ('=' * needEquals)); +} diff --git a/lib/encryption/utils/bootstrap.dart b/lib/encryption/utils/bootstrap.dart new file mode 100644 index 0000000..515d074 --- /dev/null +++ b/lib/encryption/utils/bootstrap.dart @@ -0,0 +1,606 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:convert'; +import 'dart:typed_data'; + +import 'package:canonical_json/canonical_json.dart'; +import 'package:olm/olm.dart' as olm; + +import '../encryption.dart'; +import '../ssss.dart'; +import '../key_manager.dart'; +import '../../matrix.dart'; +import 'base64_unpadded.dart'; + +enum BootstrapState { + /// Is loading. + loading, + + /// Existing SSSS found, should we wipe it? + askWipeSsss, + + /// Ask if an existing SSSS should be userDeviceKeys + askUseExistingSsss, + + /// Ask to unlock all the SSSS keys + askUnlockSsss, + + /// SSSS is in a bad state, continue with potential dataloss? + askBadSsss, + + /// Ask for new SSSS key / passphrase + askNewSsss, + + /// Open an existing SSSS key + openExistingSsss, + + /// Ask if cross signing should be wiped + askWipeCrossSigning, + + /// Ask if cross signing should be set up + askSetupCrossSigning, + + /// Ask if online key backup should be wiped + askWipeOnlineKeyBackup, + + /// Ask if the online key backup should be set up + askSetupOnlineKeyBackup, + + /// An error has been occured. + error, + + /// done + done, +} + +/// Bootstrapping SSSS and cross-signing +class Bootstrap { + final Encryption encryption; + Client get client => encryption.client; + void Function()? onUpdate; + BootstrapState get state => _state; + BootstrapState _state = BootstrapState.loading; + Map? oldSsssKeys; + OpenSSSS? newSsssKey; + Map? secretMap; + + Bootstrap({required this.encryption, this.onUpdate}) { + if (analyzeSecrets().isNotEmpty) { + state = BootstrapState.askWipeSsss; + } else { + state = BootstrapState.askNewSsss; + } + } + + // cache the secret analyzing so that we don't drop stuff a different client sets during bootstrapping + Map>? _secretsCache; + Map> analyzeSecrets() { + final secretsCache = _secretsCache; + if (secretsCache != null) { + // deep-copy so that we can do modifications + final newSecrets = >{}; + for (final s in secretsCache.entries) { + newSecrets[s.key] = Set.from(s.value); + } + return newSecrets; + } + final secrets = >{}; + for (final entry in client.accountData.entries) { + final type = entry.key; + final event = entry.value; + if (!(event.content['encrypted'] is Map)) { + continue; + } + final validKeys = {}; + final invalidKeys = {}; + for (final keyEntry in event.content['encrypted'].entries) { + final key = keyEntry.key; + final value = keyEntry.value; + if (!(value is Map)) { + // we don't add the key to invalidKeys as this was not a proper secret anyways! + continue; + } + if (!(value['iv'] is String) || + !(value['ciphertext'] is String) || + !(value['mac'] is String)) { + invalidKeys.add(key); + continue; + } + if (!encryption.ssss.isKeyValid(key)) { + invalidKeys.add(key); + continue; + } + validKeys.add(key); + } + if (validKeys.isEmpty && invalidKeys.isEmpty) { + continue; // this didn't contain any keys anyways! + } + // if there are no valid keys and only invalid keys then the validKeys set will be empty + // from that we know that there were errors with this secret and that we won't be able to migrate it + secrets[type] = validKeys; + } + _secretsCache = secrets; + return analyzeSecrets(); + } + + Set badSecrets() { + final secrets = analyzeSecrets(); + secrets.removeWhere((k, v) => v.isNotEmpty); + return Set.from(secrets.keys); + } + + String mostUsedKey(Map> secrets) { + final usage = {}; + for (final keys in secrets.values) { + for (final key in keys) { + usage.update(key, (i) => i + 1, ifAbsent: () => 1); + } + } + final entriesList = usage.entries.toList(); + entriesList.sort((a, b) => a.value.compareTo(b.value)); + return entriesList.first.key; + } + + Set allNeededKeys() { + final secrets = analyzeSecrets(); + secrets.removeWhere( + (k, v) => v.isEmpty); // we don't care about the failed secrets here + final keys = {}; + final defaultKeyId = encryption.ssss.defaultKeyId; + final removeKey = (String key) { + final sizeBefore = secrets.length; + secrets.removeWhere((k, v) => v.contains(key)); + return sizeBefore - secrets.length; + }; + // first we want to try the default key id + if (defaultKeyId != null) { + if (removeKey(defaultKeyId) > 0) { + keys.add(defaultKeyId); + } + } + // now we re-try as long as we have keys for all secrets + while (secrets.isNotEmpty) { + final key = mostUsedKey(secrets); + removeKey(key); + keys.add(key); + } + return keys; + } + + void wipeSsss(bool wipe) { + if (state != BootstrapState.askWipeSsss) { + throw BootstrapBadStateException('Wrong State'); + } + if (wipe) { + state = BootstrapState.askNewSsss; + } else if (encryption.ssss.defaultKeyId != null && + encryption.ssss.isKeyValid(encryption.ssss.defaultKeyId!)) { + state = BootstrapState.askUseExistingSsss; + } else if (badSecrets().isNotEmpty) { + state = BootstrapState.askBadSsss; + } else { + migrateOldSsss(); + } + } + + void useExistingSsss(bool use) { + if (state != BootstrapState.askUseExistingSsss) { + throw BootstrapBadStateException('Wrong State'); + } + if (use) { + try { + newSsssKey = encryption.ssss.open(encryption.ssss.defaultKeyId); + state = BootstrapState.openExistingSsss; + } catch (e, s) { + Logs().e('[Bootstrapping] Error open SSSS', e, s); + state = BootstrapState.error; + return; + } + } else if (badSecrets().isNotEmpty) { + state = BootstrapState.askBadSsss; + } else { + migrateOldSsss(); + } + } + + void ignoreBadSecrets(bool ignore) { + if (state != BootstrapState.askBadSsss) { + throw BootstrapBadStateException('Wrong State'); + } + if (ignore) { + migrateOldSsss(); + } else { + // that's it, folks. We can't do anything here + state = BootstrapState.error; + } + } + + void migrateOldSsss() { + final keys = allNeededKeys(); + final oldSsssKeys = this.oldSsssKeys = {}; + try { + for (final key in keys) { + oldSsssKeys[key] = encryption.ssss.open(key); + } + } catch (e, s) { + Logs().e('[Bootstrapping] Error construction ssss key', e, s); + state = BootstrapState.error; + return; + } + state = BootstrapState.askUnlockSsss; + } + + void unlockedSsss() { + if (state != BootstrapState.askUnlockSsss) { + throw BootstrapBadStateException('Wrong State'); + } + state = BootstrapState.askNewSsss; + } + + Future newSsss([String? passphrase]) async { + if (state != BootstrapState.askNewSsss) { + throw BootstrapBadStateException('Wrong State'); + } + state = BootstrapState.loading; + try { + Logs().v('Create key...'); + newSsssKey = await encryption.ssss.createKey(passphrase); + if (oldSsssKeys != null) { + // alright, we have to re-encrypt old secrets with the new key + final secrets = analyzeSecrets(); + final removeKey = (String key) { + final s = secrets.entries + .where((e) => e.value.contains(key)) + .map((e) => e.key) + .toSet(); + secrets.removeWhere((k, v) => v.contains(key)); + return s; + }; + secretMap = {}; + for (final entry in oldSsssKeys!.entries) { + final key = entry.value; + final keyId = entry.key; + if (!key.isUnlocked) { + continue; + } + for (final s in removeKey(keyId)) { + Logs().v('Get stored key of type $s...'); + secretMap![s] = await key.getStored(s); + Logs().v('Store new secret with this key...'); + await newSsssKey!.store(s, secretMap![s]!, add: true); + } + } + // alright, we re-encrypted all the secrets. We delete the dead weight only *after* we set our key to the default key + } + final updatedAccountData = client.onSync.stream.firstWhere((syncUpdate) => + syncUpdate.accountData != null && + syncUpdate.accountData!.any((accountData) => + accountData.type == EventTypes.SecretStorageDefaultKey)); + await encryption.ssss.setDefaultKeyId(newSsssKey!.keyId); + await updatedAccountData; + if (oldSsssKeys != null) { + for (final entry in secretMap!.entries) { + Logs().v('Validate and stripe other keys ${entry.key}...'); + await newSsssKey!.validateAndStripOtherKeys(entry.key, entry.value); + } + Logs().v('And make super sure we have everything cached...'); + await newSsssKey!.maybeCacheAll(); + } + } catch (e, s) { + Logs().e('[Bootstrapping] Error trying to migrate old secrets', e, s); + state = BootstrapState.error; + return; + } + // alright, we successfully migrated all secrets, if needed + + checkCrossSigning(); + } + + Future openExistingSsss() async { + final newSsssKey = this.newSsssKey; + if (state != BootstrapState.openExistingSsss || newSsssKey == null) { + throw BootstrapBadStateException(); + } + if (!newSsssKey.isUnlocked) { + throw BootstrapBadStateException('Key not unlocked'); + } + Logs().v('Maybe cache all...'); + await newSsssKey.maybeCacheAll(); + checkCrossSigning(); + } + + void checkCrossSigning() { + // so, let's see if we have cross signing set up + if (encryption.crossSigning.enabled) { + // cross signing present, ask for wipe + state = BootstrapState.askWipeCrossSigning; + return; + } + // no cross signing present + state = BootstrapState.askSetupCrossSigning; + } + + void wipeCrossSigning(bool wipe) { + if (state != BootstrapState.askWipeCrossSigning) { + throw BootstrapBadStateException(); + } + if (wipe) { + state = BootstrapState.askSetupCrossSigning; + } else { + checkOnlineKeyBackup(); + } + } + + Future askSetupCrossSigning( + {bool setupMasterKey = false, + bool setupSelfSigningKey = false, + bool setupUserSigningKey = false}) async { + if (state != BootstrapState.askSetupCrossSigning) { + throw BootstrapBadStateException(); + } + if (!setupMasterKey && !setupSelfSigningKey && !setupUserSigningKey) { + checkOnlineKeyBackup(); + return; + } + final userID = client.userID!; + try { + Uint8List masterSigningKey; + final secretsToStore = {}; + MatrixCrossSigningKey? masterKey; + MatrixCrossSigningKey? selfSigningKey; + MatrixCrossSigningKey? userSigningKey; + String? masterPub; + if (setupMasterKey) { + final master = olm.PkSigning(); + try { + masterSigningKey = master.generate_seed(); + masterPub = master.init_with_seed(masterSigningKey); + final json = { + 'user_id': userID, + 'usage': ['master'], + 'keys': { + 'ed25519:$masterPub': masterPub, + }, + }; + masterKey = MatrixCrossSigningKey.fromJson(json); + secretsToStore[EventTypes.CrossSigningMasterKey] = + base64.encode(masterSigningKey); + } finally { + master.free(); + } + } else { + Logs().v('Get stored key...'); + masterSigningKey = base64decodeUnpadded( + await newSsssKey?.getStored(EventTypes.CrossSigningMasterKey) ?? + ''); + if (masterSigningKey.isEmpty) { + // no master signing key :( + throw BootstrapBadStateException('No master key'); + } + final master = olm.PkSigning(); + try { + masterPub = master.init_with_seed(masterSigningKey); + } finally { + master.free(); + } + } + final _sign = (Map object) { + final keyObj = olm.PkSigning(); + try { + keyObj.init_with_seed(masterSigningKey); + return keyObj + .sign(String.fromCharCodes(canonicalJson.encode(object))); + } finally { + keyObj.free(); + } + }; + if (setupSelfSigningKey) { + final selfSigning = olm.PkSigning(); + try { + final selfSigningPriv = selfSigning.generate_seed(); + final selfSigningPub = selfSigning.init_with_seed(selfSigningPriv); + final json = { + 'user_id': userID, + 'usage': ['self_signing'], + 'keys': { + 'ed25519:$selfSigningPub': selfSigningPub, + }, + }; + final signature = _sign(json); + json['signatures'] = { + userID: { + 'ed25519:$masterPub': signature, + }, + }; + selfSigningKey = MatrixCrossSigningKey.fromJson(json); + secretsToStore[EventTypes.CrossSigningSelfSigning] = + base64.encode(selfSigningPriv); + } finally { + selfSigning.free(); + } + } + if (setupUserSigningKey) { + final userSigning = olm.PkSigning(); + try { + final userSigningPriv = userSigning.generate_seed(); + final userSigningPub = userSigning.init_with_seed(userSigningPriv); + final json = { + 'user_id': userID, + 'usage': ['user_signing'], + 'keys': { + 'ed25519:$userSigningPub': userSigningPub, + }, + }; + final signature = _sign(json); + json['signatures'] = { + userID: { + 'ed25519:$masterPub': signature, + }, + }; + userSigningKey = MatrixCrossSigningKey.fromJson(json); + secretsToStore[EventTypes.CrossSigningUserSigning] = + base64.encode(userSigningPriv); + } finally { + userSigning.free(); + } + } + // upload the keys! + state = BootstrapState.loading; + Logs().v('Upload device signing keys.'); + await client.uiaRequestBackground( + (AuthenticationData? auth) => client.uploadCrossSigningKeys( + masterKey: masterKey, + selfSigningKey: selfSigningKey, + userSigningKey: userSigningKey, + auth: auth, + )); + Logs().v('Device signing keys have been uploaded.'); + // aaaand set the SSSS secrets + final futures = >[]; + if (masterKey != null) { + futures.add( + client.onSync.stream + .firstWhere((syncUpdate) => + masterKey?.publicKey != null && + client.userDeviceKeys[client.userID]?.masterKey?.ed25519Key == + masterKey?.publicKey) + .then((_) => Logs().v('New Master Key was created')), + ); + } + for (final entry in secretsToStore.entries) { + futures.add( + client.onSync.stream + .firstWhere((syncUpdate) => + syncUpdate.accountData != null && + syncUpdate.accountData! + .any((accountData) => accountData.type == entry.key)) + .then((_) => + Logs().v('New Key with type ${entry.key} was created')), + ); + Logs().v('Store new SSSS key ${entry.key}...'); + await newSsssKey?.store(entry.key, entry.value); + } + Logs().v( + 'Wait for MasterKey and ${secretsToStore.entries.length} keys to be created'); + await Future.wait(futures); + final keysToSign = []; + if (masterKey != null) { + if (client.userDeviceKeys[client.userID]?.masterKey?.ed25519Key != + masterKey.publicKey) { + throw BootstrapBadStateException( + 'ERROR: New master key does not match up!'); + } + Logs().v('Set own master key to verified...'); + await client.userDeviceKeys[client.userID]!.masterKey! + .setVerified(true, false); + keysToSign.add(client.userDeviceKeys[client.userID]!.masterKey!); + } + if (selfSigningKey != null) { + keysToSign.add( + client.userDeviceKeys[client.userID]!.deviceKeys[client.deviceID]!); + } + Logs().v('Sign ourself...'); + await encryption.crossSigning.sign(keysToSign); + } catch (e, s) { + Logs().e('[Bootstrapping] Error setting up cross signing', e, s); + state = BootstrapState.error; + return; + } + + checkOnlineKeyBackup(); + } + + void checkOnlineKeyBackup() { + // check if we have online key backup set up + if (encryption.keyManager.enabled) { + state = BootstrapState.askWipeOnlineKeyBackup; + return; + } + state = BootstrapState.askSetupOnlineKeyBackup; + } + + void wipeOnlineKeyBackup(bool wipe) { + if (state != BootstrapState.askWipeOnlineKeyBackup) { + throw BootstrapBadStateException(); + } + if (wipe) { + state = BootstrapState.askSetupOnlineKeyBackup; + } else { + state = BootstrapState.done; + } + } + + Future askSetupOnlineKeyBackup(bool setup) async { + if (state != BootstrapState.askSetupOnlineKeyBackup) { + throw BootstrapBadStateException(); + } + if (!setup) { + state = BootstrapState.done; + return; + } + try { + final keyObj = olm.PkDecryption(); + String pubKey; + Uint8List privKey; + try { + pubKey = keyObj.generate_key(); + privKey = keyObj.get_private_key(); + } finally { + keyObj.free(); + } + Logs().v('Create the new backup version...'); + await client.postRoomKeysVersion( + BackupAlgorithm.mMegolmBackupV1Curve25519AesSha2, + { + 'public_key': pubKey, + }, + ); + Logs().v('Store the secret...'); + await newSsssKey?.store(megolmKey, base64.encode(privKey)); + Logs().v( + 'And finally set all megolm keys as needing to be uploaded again...'); + await client.database?.markInboundGroupSessionsAsNeedingUpload(); + } catch (e, s) { + Logs().e('[Bootstrapping] Error setting up online key backup', e, s); + state = BootstrapState.error; + encryption.client.onEncryptionError.add( + SdkError(exception: e, stackTrace: s), + ); + return; + } + state = BootstrapState.done; + } + + set state(BootstrapState newState) { + Logs().v('BootstrapState: $newState'); + if (state != BootstrapState.error) { + _state = newState; + } + + onUpdate?.call(); + } +} + +class BootstrapBadStateException implements Exception { + String cause; + BootstrapBadStateException([this.cause = 'Bad state']); + + @override + String toString() => 'BootstrapBadStateException: $cause'; +} diff --git a/lib/encryption/utils/json_signature_check_extension.dart b/lib/encryption/utils/json_signature_check_extension.dart new file mode 100644 index 0000000..63afe7b --- /dev/null +++ b/lib/encryption/utils/json_signature_check_extension.dart @@ -0,0 +1,50 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:canonical_json/canonical_json.dart'; +import 'package:olm/olm.dart' as olm; + +import '../../matrix.dart'; + +extension JsonSignatureCheckExtension on Map { + /// Checks the signature of a signed json object. + bool checkJsonSignature(String key, String userId, String deviceId) { + final signatures = this['signatures']; + if (signatures == null || + !(signatures is Map) || + !signatures.containsKey(userId)) return false; + remove('unsigned'); + remove('signatures'); + if (!signatures[userId].containsKey('ed25519:$deviceId')) return false; + final String signature = signatures[userId]['ed25519:$deviceId']; + final canonical = canonicalJson.encode(this); + final message = String.fromCharCodes(canonical); + var isValid = false; + final olmutil = olm.Utility(); + try { + olmutil.ed25519_verify(key, message, signature); + isValid = true; + } catch (e, s) { + isValid = false; + Logs().w('[LibOlm] Signature check failed', e, s); + } finally { + olmutil.free(); + } + return isValid; + } +} diff --git a/lib/encryption/utils/key_verification.dart b/lib/encryption/utils/key_verification.dart new file mode 100644 index 0000000..d21e973 --- /dev/null +++ b/lib/encryption/utils/key_verification.dart @@ -0,0 +1,1242 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:async'; +import 'dart:typed_data'; + +import 'package:canonical_json/canonical_json.dart'; +import 'package:olm/olm.dart' as olm; + +import '../../matrix.dart'; +import '../encryption.dart'; + +/* + +-------------+ +-----------+ + | AliceDevice | | BobDevice | + +-------------+ +-----------+ + | | + | (m.key.verification.request) | + |-------------------------------->| (ASK FOR VERIFICATION REQUEST) + | | + | (m.key.verification.ready) | + |<--------------------------------| + | | + | (m.key.verification.start) | we will probably not send this + |<--------------------------------| for simplicities sake + | | + | m.key.verification.start | + |-------------------------------->| (ASK FOR VERIFICATION REQUEST) + | | + | m.key.verification.accept | + |<--------------------------------| + | | + | m.key.verification.key | + |-------------------------------->| + | | + | m.key.verification.key | + |<--------------------------------| + | | + | COMPARE EMOJI / NUMBERS | + | | + | m.key.verification.mac | + |-------------------------------->| success + | | + | m.key.verification.mac | + success |<--------------------------------| + | | +*/ + +enum KeyVerificationState { + askAccept, + askSSSS, + waitingAccept, + askSas, + waitingSas, + done, + error +} + +enum KeyVerificationMethod { emoji, numbers } + +List _intersect(List? a, List? b) => + (b == null || a == null) ? [] : a.where(b.contains).toList(); + +List _bytesToInt(Uint8List bytes, int totalBits) { + final ret = []; + var current = 0; + var numBits = 0; + for (final byte in bytes) { + for (final bit in [7, 6, 5, 4, 3, 2, 1, 0]) { + numBits++; + current |= ((byte >> bit) & 1) << (totalBits - numBits); + if (numBits >= totalBits) { + ret.add(current); + current = 0; + numBits = 0; + } + } + } + return ret; +} + +_KeyVerificationMethod _makeVerificationMethod( + String type, KeyVerification request) { + if (type == 'm.sas.v1') { + return _KeyVerificationMethodSas(request: request); + } + throw Exception('Unkown method type'); +} + +class KeyVerification { + String? transactionId; + final Encryption encryption; + Client get client => encryption.client; + final Room? room; + final String userId; + void Function()? onUpdate; + String? get deviceId => _deviceId; + String? _deviceId; + bool startedVerification = false; + _KeyVerificationMethod? method; + List possibleMethods = []; + Map? startPayload; + String? _nextAction; + List _verifiedDevices = []; + + DateTime lastActivity; + String? lastStep; + + KeyVerificationState state = KeyVerificationState.waitingAccept; + bool canceled = false; + String? canceledCode; + String? canceledReason; + bool get isDone => + canceled || + {KeyVerificationState.error, KeyVerificationState.done}.contains(state); + + KeyVerification( + {required this.encryption, + this.room, + required this.userId, + String? deviceId, + this.onUpdate}) + : _deviceId = deviceId, + lastActivity = DateTime.now(); + + void dispose() { + Logs().i('[Key Verification] disposing object...'); + method?.dispose(); + } + + static String? getTransactionId(Map payload) { + return payload['transaction_id'] ?? + (payload['m.relates_to'] is Map + ? payload['m.relates_to']['event_id'] + : null); + } + + List get knownVerificationMethods { + final methods = []; + if (client.verificationMethods.contains(KeyVerificationMethod.numbers) || + client.verificationMethods.contains(KeyVerificationMethod.emoji)) { + methods.add('m.sas.v1'); + } + return methods; + } + + Future sendStart() async { + await send(EventTypes.KeyVerificationRequest, { + 'methods': knownVerificationMethods, + if (room == null) 'timestamp': DateTime.now().millisecondsSinceEpoch, + }); + startedVerification = true; + setState(KeyVerificationState.waitingAccept); + lastActivity = DateTime.now(); + } + + Future start() async { + if (room == null) { + transactionId = client.generateUniqueTransactionId(); + } + if (encryption.crossSigning.enabled && + !(await encryption.crossSigning.isCached()) && + !client.isUnknownSession) { + setState(KeyVerificationState.askSSSS); + _nextAction = 'request'; + } else { + await sendStart(); + } + } + + bool _handlePayloadLock = false; + + Future handlePayload(String type, Map payload, + [String? eventId]) async { + if (isDone) { + return; // no need to do anything with already canceled requests + } + while (_handlePayloadLock) { + await Future.delayed(Duration(milliseconds: 50)); + } + _handlePayloadLock = true; + Logs().i('[Key Verification] Received type $type: ' + payload.toString()); + try { + var thisLastStep = lastStep; + switch (type) { + case EventTypes.KeyVerificationRequest: + _deviceId ??= payload['from_device']; + transactionId ??= eventId ?? payload['transaction_id']; + // verify the timestamp + final now = DateTime.now(); + final verifyTime = + DateTime.fromMillisecondsSinceEpoch(payload['timestamp']); + if (now.subtract(Duration(minutes: 10)).isAfter(verifyTime) || + now.add(Duration(minutes: 5)).isBefore(verifyTime)) { + // if the request is more than 20min in the past we just silently fail it + // to not generate too many cancels + await cancel('m.timeout', + now.subtract(Duration(minutes: 20)).isAfter(verifyTime)); + return; + } + // verify it has a method we can use + possibleMethods = + _intersect(knownVerificationMethods, payload['methods']); + if (possibleMethods.isEmpty) { + // reject it outright + await cancel('m.unknown_method'); + return; + } + setState(KeyVerificationState.askAccept); + break; + case 'm.key.verification.ready': + if (deviceId == '*') { + _deviceId = payload['from_device']; // gotta set the real device id + // and broadcast the cancel to the other devices + final devices = List.from( + client.userDeviceKeys[userId]?.deviceKeys.values ?? + Iterable.empty()); + devices.removeWhere( + (d) => {deviceId, client.deviceID}.contains(d.deviceId)); + final cancelPayload = { + 'reason': 'Another device accepted the request', + 'code': 'm.accepted', + }; + makePayload(cancelPayload); + await client.sendToDeviceEncrypted( + devices, EventTypes.KeyVerificationCancel, cancelPayload); + } + _deviceId ??= payload['from_device']; + possibleMethods = + _intersect(knownVerificationMethods, payload['methods']); + if (possibleMethods.isEmpty) { + // reject it outright + await cancel('m.unknown_method'); + return; + } + // as both parties can send a start, the last step being "ready" is race-condition prone + // as such, we better set it *before* we send our start + lastStep = type; + // TODO: Pick method? + final method = this.method = + _makeVerificationMethod(possibleMethods.first, this); + await method.sendStart(); + setState(KeyVerificationState.waitingAccept); + break; + case EventTypes.KeyVerificationStart: + _deviceId ??= payload['from_device']; + transactionId ??= eventId ?? payload['transaction_id']; + if (method != null) { + // the other side sent us a start, even though we already sent one + if (payload['method'] == method!.type) { + // same method. Determine priority + final ourEntry = '${client.userID}|${client.deviceID}'; + final entries = [ourEntry, '$userId|$deviceId']; + entries.sort(); + if (entries.first == ourEntry) { + // our start won, nothing to do + return; + } else { + // the other start won, let's hand off + startedVerification = false; // it is now as if they started + thisLastStep = lastStep = + EventTypes.KeyVerificationRequest; // we fake the last step + method!.dispose(); // in case anything got created already + } + } else { + // methods don't match up, let's cancel this + await cancel('m.unexpected_message'); + return; + } + } + if (!(await verifyLastStep( + [EventTypes.KeyVerificationRequest, null]))) { + return; // abort + } + if (!knownVerificationMethods.contains(payload['method'])) { + await cancel('m.unknown_method'); + return; + } + method = _makeVerificationMethod(payload['method'], this); + if (lastStep == null) { + // validate the start time + if (room != null) { + // we just silently ignore in-room-verification starts + await cancel('m.unknown_method', true); + return; + } + // validate the specific payload + if (!method!.validateStart(payload)) { + await cancel('m.unknown_method'); + return; + } + startPayload = payload; + setState(KeyVerificationState.askAccept); + } else { + Logs().i('handling start in method.....'); + await method!.handlePayload(type, payload); + } + break; + case EventTypes.KeyVerificationDone: + // do nothing + break; + case EventTypes.KeyVerificationCancel: + canceled = true; + canceledCode = payload['code']; + canceledReason = payload['reason']; + setState(KeyVerificationState.error); + break; + default: + final method = this.method; + if (method != null) { + await method.handlePayload(type, payload); + } else { + await cancel('m.invalid_message'); + } + break; + } + if (lastStep == thisLastStep) { + lastStep = type; + } + } catch (err, stacktrace) { + Logs().e('[Key Verification] An error occured', err, stacktrace); + await cancel('m.invalid_message'); + } finally { + _handlePayloadLock = false; + } + } + + void otherDeviceAccepted() { + canceled = true; + canceledCode = 'm.accepted'; + canceledReason = 'm.accepted'; + setState(KeyVerificationState.error); + } + + Future openSSSS( + {String? passphrase, + String? recoveryKey, + String? keyOrPassphrase, + bool skip = false}) async { + final next = () { + if (_nextAction == 'request') { + sendStart(); + } else if (_nextAction == 'done') { + // and now let's sign them all in the background + encryption.crossSigning.sign(_verifiedDevices); + setState(KeyVerificationState.done); + } + }; + if (skip) { + next(); + return; + } + final handle = encryption.ssss.open(EventTypes.CrossSigningUserSigning); + await handle.unlock( + passphrase: passphrase, + recoveryKey: recoveryKey, + keyOrPassphrase: keyOrPassphrase); + await handle.maybeCacheAll(); + next(); + } + + /// called when the user accepts an incoming verification + Future acceptVerification() async { + if (!(await verifyLastStep([ + EventTypes.KeyVerificationRequest, + EventTypes.KeyVerificationStart + ]))) { + return; + } + setState(KeyVerificationState.waitingAccept); + if (lastStep == EventTypes.KeyVerificationRequest) { + // we need to send a ready event + await send('m.key.verification.ready', { + 'methods': possibleMethods, + }); + } else { + // we need to send an accept event + await method! + .handlePayload(EventTypes.KeyVerificationStart, startPayload!); + } + } + + /// called when the user rejects an incoming verification + Future rejectVerification() async { + if (isDone) { + return; + } + if (!(await verifyLastStep([ + EventTypes.KeyVerificationRequest, + EventTypes.KeyVerificationStart + ]))) { + return; + } + await cancel('m.user'); + } + + Future acceptSas() async { + if (method is _KeyVerificationMethodSas) { + await (method as _KeyVerificationMethodSas).acceptSas(); + } + } + + Future rejectSas() async { + if (method is _KeyVerificationMethodSas) { + await (method as _KeyVerificationMethodSas).rejectSas(); + } + } + + List get sasNumbers { + if (method is _KeyVerificationMethodSas) { + return _bytesToInt((method as _KeyVerificationMethodSas).makeSas(5), 13) + .map((n) => n + 1000) + .toList(); + } + return []; + } + + List get sasTypes { + if (method is _KeyVerificationMethodSas) { + return (method as _KeyVerificationMethodSas).authenticationTypes ?? []; + } + return []; + } + + List get sasEmojis { + if (method is _KeyVerificationMethodSas) { + final numbers = + _bytesToInt((method as _KeyVerificationMethodSas).makeSas(6), 6); + return numbers.map((n) => KeyVerificationEmoji(n)).toList().sublist(0, 7); + } + return []; + } + + Future maybeRequestSSSSSecrets([int i = 0]) async { + final requestInterval = [10, 60]; + if ((!encryption.crossSigning.enabled || + (encryption.crossSigning.enabled && + (await encryption.crossSigning.isCached()))) && + (!encryption.keyManager.enabled || + (encryption.keyManager.enabled && + (await encryption.keyManager.isCached())))) { + // no need to request cache, we already have it + return; + } + // ignore: unawaited_futures + encryption.ssss + .maybeRequestAll(_verifiedDevices.whereType().toList()); + if (requestInterval.length <= i) { + return; + } + Timer(Duration(seconds: requestInterval[i]), + () => maybeRequestSSSSSecrets(i + 1)); + } + + Future verifyKeys(Map keys, + Future Function(String, SignableKey) verifier) async { + _verifiedDevices = []; + + final userDeviceKey = client.userDeviceKeys[userId]; + if (userDeviceKey == null) { + await cancel('m.key_mismatch'); + return; + } + for (final entry in keys.entries) { + final keyId = entry.key; + final verifyDeviceId = keyId.substring('ed25519:'.length); + final keyInfo = entry.value; + final key = userDeviceKey.getKey(verifyDeviceId); + if (key != null) { + if (!(await verifier(keyInfo, key))) { + await cancel('m.key_mismatch'); + return; + } + _verifiedDevices.add(key); + } + } + // okay, we reached this far, so all the devices are verified! + var verifiedMasterKey = false; + final wasUnknownSession = client.isUnknownSession; + for (final key in _verifiedDevices) { + await key.setVerified( + true, false); // we don't want to sign the keys juuuust yet + if (key is CrossSigningKey && key.usage.contains('master')) { + verifiedMasterKey = true; + } + } + if (verifiedMasterKey && userId == client.userID) { + // it was our own master key, let's request the cross signing keys + // we do it in the background, thus no await needed here + // ignore: unawaited_futures + maybeRequestSSSSSecrets(); + } + await send(EventTypes.KeyVerificationDone, {}); + + var askingSSSS = false; + if (encryption.crossSigning.enabled && + encryption.crossSigning.signable(_verifiedDevices)) { + // these keys can be signed! Let's do so + if (await encryption.crossSigning.isCached()) { + // and now let's sign them all in the background + // ignore: unawaited_futures + encryption.crossSigning.sign(_verifiedDevices); + } else if (!wasUnknownSession) { + askingSSSS = true; + } + } + if (askingSSSS) { + setState(KeyVerificationState.askSSSS); + _nextAction = 'done'; + } else { + setState(KeyVerificationState.done); + } + } + + Future verifyActivity() async { + if (lastActivity.add(Duration(minutes: 10)).isAfter(DateTime.now())) { + lastActivity = DateTime.now(); + return true; + } + await cancel('m.timeout'); + return false; + } + + Future verifyLastStep(List checkLastStep) async { + if (!(await verifyActivity())) { + return false; + } + if (checkLastStep.contains(lastStep)) { + return true; + } + await cancel('m.unexpected_message'); + return false; + } + + Future cancel([String code = 'm.unknown', bool quiet = false]) async { + if (!quiet && (deviceId != null || room != null)) { + await send(EventTypes.KeyVerificationCancel, { + 'reason': code, + 'code': code, + }); + } + canceled = true; + canceledCode = code; + setState(KeyVerificationState.error); + } + + void makePayload(Map payload) { + payload['from_device'] = client.deviceID; + if (transactionId != null) { + if (room != null) { + payload['m.relates_to'] = { + 'rel_type': 'm.reference', + 'event_id': transactionId, + }; + } else { + payload['transaction_id'] = transactionId; + } + } + } + + Future send(String type, Map payload) async { + makePayload(payload); + Logs().i('[Key Verification] Sending type $type: ' + payload.toString()); + if (room != null) { + Logs().i('[Key Verification] Sending to $userId in room ${room!.id}...'); + if ({EventTypes.KeyVerificationRequest}.contains(type)) { + payload['msgtype'] = type; + payload['to'] = userId; + payload['body'] = + 'Attempting verification request. ($type) Apparently your client doesn\'t support this'; + type = EventTypes.Message; + } + final newTransactionId = await room!.sendEvent(payload, type: type); + if (transactionId == null) { + transactionId = newTransactionId; + encryption.keyVerificationManager.addRequest(this); + } + } else { + Logs().i('[Key Verification] Sending to $userId device $deviceId...'); + if (deviceId == '*') { + if ({ + EventTypes.KeyVerificationRequest, + EventTypes.KeyVerificationCancel, + }.contains(type)) { + await client.sendToDevicesOfUserIds({userId}, type, payload); + } else { + Logs().e( + '[Key Verification] Tried to broadcast and un-broadcastable type: $type'); + } + } else { + if (client.userDeviceKeys[userId]?.deviceKeys[deviceId] == null) { + Logs().e('[Key Verification] Unknown device'); + } + await client.sendToDeviceEncrypted( + [client.userDeviceKeys[userId]!.deviceKeys[deviceId]!], + type, + payload); + } + } + } + + void setState(KeyVerificationState newState) { + if (state != KeyVerificationState.error) { + state = newState; + } + + onUpdate?.call(); + } +} + +abstract class _KeyVerificationMethod { + KeyVerification request; + Encryption get encryption => request.encryption; + Client get client => request.client; + _KeyVerificationMethod({required this.request}); + + Future handlePayload(String type, Map payload); + bool validateStart(Map payload) { + return false; + } + + late String _type; + String get type => _type; + + Future sendStart(); + void dispose() {} +} + +const knownKeyAgreementProtocols = ['curve25519-hkdf-sha256', 'curve25519']; +const knownHashes = ['sha256']; +const knownHashesAuthentificationCodes = ['hkdf-hmac-sha256']; + +class _KeyVerificationMethodSas extends _KeyVerificationMethod { + _KeyVerificationMethodSas({required KeyVerification request}) + : super(request: request); + + @override + final _type = 'm.sas.v1'; + + String? keyAgreementProtocol; + String? hash; + String? messageAuthenticationCode; + List? authenticationTypes; + late String startCanonicalJson; + String? commitment; + late String theirPublicKey; + Map? macPayload; + olm.SAS? sas; + + @override + void dispose() { + sas?.free(); + } + + List get knownAuthentificationTypes { + final types = []; + if (request.client.verificationMethods + .contains(KeyVerificationMethod.emoji)) { + types.add('emoji'); + } + if (request.client.verificationMethods + .contains(KeyVerificationMethod.numbers)) { + types.add('decimal'); + } + return types; + } + + @override + Future handlePayload(String type, Map payload) async { + try { + switch (type) { + case EventTypes.KeyVerificationStart: + if (!(await request.verifyLastStep([ + EventTypes.KeyVerificationRequest, + EventTypes.KeyVerificationStart + ]))) { + return; // abort + } + if (!validateStart(payload)) { + await request.cancel('m.unknown_method'); + return; + } + await _sendAccept(); + break; + case EventTypes.KeyVerificationAccept: + if (!(await request.verifyLastStep(['m.key.verification.ready']))) { + return; + } + if (!_handleAccept(payload)) { + await request.cancel('m.unknown_method'); + return; + } + await _sendKey(); + break; + case 'm.key.verification.key': + if (!(await request.verifyLastStep([ + EventTypes.KeyVerificationAccept, + EventTypes.KeyVerificationStart + ]))) { + return; + } + _handleKey(payload); + if (request.lastStep == EventTypes.KeyVerificationStart) { + // we need to send our key + await _sendKey(); + } else { + // we already sent our key, time to verify the commitment being valid + if (!_validateCommitment()) { + await request.cancel('m.mismatched_commitment'); + return; + } + } + request.setState(KeyVerificationState.askSas); + break; + case 'm.key.verification.mac': + if (!(await request.verifyLastStep(['m.key.verification.key']))) { + return; + } + macPayload = payload; + if (request.state == KeyVerificationState.waitingSas) { + await _processMac(); + } + break; + } + } catch (err, stacktrace) { + Logs().e('[Key Verification SAS] An error occured', err, stacktrace); + if (request.deviceId != null) { + await request.cancel('m.invalid_message'); + } + } + } + + Future acceptSas() async { + await _sendMac(); + request.setState(KeyVerificationState.waitingSas); + if (macPayload != null) { + await _processMac(); + } + } + + Future rejectSas() async { + await request.cancel('m.mismatched_sas'); + } + + @override + Future sendStart() async { + final payload = { + 'method': type, + 'key_agreement_protocols': knownKeyAgreementProtocols, + 'hashes': knownHashes, + 'message_authentication_codes': knownHashesAuthentificationCodes, + 'short_authentication_string': knownAuthentificationTypes, + }; + request.makePayload(payload); + // We just store the canonical json in here for later verification + startCanonicalJson = String.fromCharCodes(canonicalJson.encode(payload)); + await request.send(EventTypes.KeyVerificationStart, payload); + } + + @override + bool validateStart(Map payload) { + if (payload['method'] != type) { + return false; + } + final possibleKeyAgreementProtocols = _intersect( + knownKeyAgreementProtocols, payload['key_agreement_protocols']); + if (possibleKeyAgreementProtocols.isEmpty) { + return false; + } + keyAgreementProtocol = possibleKeyAgreementProtocols.first; + final possibleHashes = _intersect(knownHashes, payload['hashes']); + if (possibleHashes.isEmpty) { + return false; + } + hash = possibleHashes.first; + final possibleMessageAuthenticationCodes = _intersect( + knownHashesAuthentificationCodes, + payload['message_authentication_codes']); + if (possibleMessageAuthenticationCodes.isEmpty) { + return false; + } + messageAuthenticationCode = possibleMessageAuthenticationCodes.first; + final possibleAuthenticationTypes = _intersect( + knownAuthentificationTypes, payload['short_authentication_string']); + if (possibleAuthenticationTypes.isEmpty) { + return false; + } + authenticationTypes = possibleAuthenticationTypes; + startCanonicalJson = String.fromCharCodes(canonicalJson.encode(payload)); + return true; + } + + Future _sendAccept() async { + final sas = this.sas = olm.SAS(); + commitment = _makeCommitment(sas.get_pubkey(), startCanonicalJson); + await request.send(EventTypes.KeyVerificationAccept, { + 'method': type, + 'key_agreement_protocol': keyAgreementProtocol, + 'hash': hash, + 'message_authentication_code': messageAuthenticationCode, + 'short_authentication_string': authenticationTypes, + 'commitment': commitment, + }); + } + + bool _handleAccept(Map payload) { + if (!knownKeyAgreementProtocols + .contains(payload['key_agreement_protocol'])) { + return false; + } + keyAgreementProtocol = payload['key_agreement_protocol']; + if (!knownHashes.contains(payload['hash'])) { + return false; + } + hash = payload['hash']; + if (!knownHashesAuthentificationCodes + .contains(payload['message_authentication_code'])) { + return false; + } + messageAuthenticationCode = payload['message_authentication_code']; + final possibleAuthenticationTypes = _intersect( + knownAuthentificationTypes, payload['short_authentication_string']); + if (possibleAuthenticationTypes.isEmpty) { + return false; + } + authenticationTypes = possibleAuthenticationTypes; + commitment = payload['commitment']; + sas = olm.SAS(); + return true; + } + + Future _sendKey() async { + await request.send('m.key.verification.key', { + 'key': sas!.get_pubkey(), + }); + } + + void _handleKey(Map payload) { + theirPublicKey = payload['key']; + sas!.set_their_key(payload['key']); + } + + bool _validateCommitment() { + final checkCommitment = _makeCommitment(theirPublicKey, startCanonicalJson); + return commitment == checkCommitment; + } + + Uint8List makeSas(int bytes) { + var sasInfo = ''; + if (keyAgreementProtocol == 'curve25519-hkdf-sha256') { + final ourInfo = + '${client.userID}|${client.deviceID}|${sas!.get_pubkey()}|'; + final theirInfo = + '${request.userId}|${request.deviceId}|$theirPublicKey|'; + sasInfo = 'MATRIX_KEY_VERIFICATION_SAS|' + + (request.startedVerification + ? ourInfo + theirInfo + : theirInfo + ourInfo) + + request.transactionId!; + } else if (keyAgreementProtocol == 'curve25519') { + final ourInfo = client.userID! + client.deviceID!; + final theirInfo = request.userId + request.deviceId!; + sasInfo = 'MATRIX_KEY_VERIFICATION_SAS' + + (request.startedVerification + ? ourInfo + theirInfo + : theirInfo + ourInfo) + + request.transactionId!; + } else { + throw Exception('Unknown key agreement protocol'); + } + return sas!.generate_bytes(sasInfo, bytes); + } + + Future _sendMac() async { + final baseInfo = 'MATRIX_KEY_VERIFICATION_MAC' + + client.userID! + + client.deviceID! + + request.userId + + request.deviceId! + + request.transactionId!; + final mac = {}; + final keyList = []; + + // now add all the keys we want the other to verify + // for now it is just our device key, once we have cross-signing + // we would also add the cross signing key here + final deviceKeyId = 'ed25519:${client.deviceID}'; + mac[deviceKeyId] = + _calculateMac(encryption.fingerprintKey!, baseInfo + deviceKeyId); + keyList.add(deviceKeyId); + + final masterKey = client.userDeviceKeys[client.userID]?.masterKey; + if (masterKey != null && masterKey.verified) { + // we have our own master key verified, let's send it! + final masterKeyId = 'ed25519:${masterKey.publicKey}'; + mac[masterKeyId] = + _calculateMac(masterKey.publicKey!, baseInfo + masterKeyId); + keyList.add(masterKeyId); + } + + keyList.sort(); + final keys = _calculateMac(keyList.join(','), baseInfo + 'KEY_IDS'); + await request.send('m.key.verification.mac', { + 'mac': mac, + 'keys': keys, + }); + } + + Future _processMac() async { + final payload = macPayload!; + final baseInfo = 'MATRIX_KEY_VERIFICATION_MAC' + + request.userId + + request.deviceId! + + client.userID! + + client.deviceID! + + request.transactionId!; + + final keyList = payload['mac'].keys.toList(); + keyList.sort(); + if (payload['keys'] != + _calculateMac(keyList.join(','), baseInfo + 'KEY_IDS')) { + await request.cancel('m.key_mismatch'); + return; + } + + if (!client.userDeviceKeys.containsKey(request.userId)) { + await request.cancel('m.key_mismatch'); + return; + } + final mac = {}; + for (final entry in payload['mac'].entries) { + if (entry.value is String) { + mac[entry.key] = entry.value; + } + } + await request.verifyKeys(mac, (String mac, SignableKey key) async { + return mac == + _calculateMac( + key.ed25519Key!, baseInfo + 'ed25519:' + key.identifier!); + }); + } + + String _makeCommitment(String pubKey, String canonicalJson) { + if (hash == 'sha256') { + final olmutil = olm.Utility(); + final ret = olmutil.sha256(pubKey + canonicalJson); + olmutil.free(); + return ret; + } + throw Exception('Unknown hash method'); + } + + String _calculateMac(String input, String info) { + if (messageAuthenticationCode == 'hkdf-hmac-sha256') { + return sas!.calculate_mac(input, info); + } else { + throw Exception('Unknown message authentification code'); + } + } +} + +const _emojiMap = [ + { + 'emoji': '\u{1F436}', + 'name': 'Dog', + }, + { + 'emoji': '\u{1F431}', + 'name': 'Cat', + }, + { + 'emoji': '\u{1F981}', + 'name': 'Lion', + }, + { + 'emoji': '\u{1F40E}', + 'name': 'Horse', + }, + { + 'emoji': '\u{1F984}', + 'name': 'Unicorn', + }, + { + 'emoji': '\u{1F437}', + 'name': 'Pig', + }, + { + 'emoji': '\u{1F418}', + 'name': 'Elephant', + }, + { + 'emoji': '\u{1F430}', + 'name': 'Rabbit', + }, + { + 'emoji': '\u{1F43C}', + 'name': 'Panda', + }, + { + 'emoji': '\u{1F413}', + 'name': 'Rooster', + }, + { + 'emoji': '\u{1F427}', + 'name': 'Penguin', + }, + { + 'emoji': '\u{1F422}', + 'name': 'Turtle', + }, + { + 'emoji': '\u{1F41F}', + 'name': 'Fish', + }, + { + 'emoji': '\u{1F419}', + 'name': 'Octopus', + }, + { + 'emoji': '\u{1F98B}', + 'name': 'Butterfly', + }, + { + 'emoji': '\u{1F337}', + 'name': 'Flower', + }, + { + 'emoji': '\u{1F333}', + 'name': 'Tree', + }, + { + 'emoji': '\u{1F335}', + 'name': 'Cactus', + }, + { + 'emoji': '\u{1F344}', + 'name': 'Mushroom', + }, + { + 'emoji': '\u{1F30F}', + 'name': 'Globe', + }, + { + 'emoji': '\u{1F319}', + 'name': 'Moon', + }, + { + 'emoji': '\u{2601}\u{FE0F}', + 'name': 'Cloud', + }, + { + 'emoji': '\u{1F525}', + 'name': 'Fire', + }, + { + 'emoji': '\u{1F34C}', + 'name': 'Banana', + }, + { + 'emoji': '\u{1F34E}', + 'name': 'Apple', + }, + { + 'emoji': '\u{1F353}', + 'name': 'Strawberry', + }, + { + 'emoji': '\u{1F33D}', + 'name': 'Corn', + }, + { + 'emoji': '\u{1F355}', + 'name': 'Pizza', + }, + { + 'emoji': '\u{1F382}', + 'name': 'Cake', + }, + { + 'emoji': '\u{2764}\u{FE0F}', + 'name': 'Heart', + }, + { + 'emoji': '\u{1F600}', + 'name': 'Smiley', + }, + { + 'emoji': '\u{1F916}', + 'name': 'Robot', + }, + { + 'emoji': '\u{1F3A9}', + 'name': 'Hat', + }, + { + 'emoji': '\u{1F453}', + 'name': 'Glasses', + }, + { + 'emoji': '\u{1F527}', + 'name': 'Spanner', + }, + { + 'emoji': '\u{1F385}', + 'name': 'Santa', + }, + { + 'emoji': '\u{1F44D}', + 'name': 'Thumbs Up', + }, + { + 'emoji': '\u{2602}\u{FE0F}', + 'name': 'Umbrella', + }, + { + 'emoji': '\u{231B}', + 'name': 'Hourglass', + }, + { + 'emoji': '\u{23F0}', + 'name': 'Clock', + }, + { + 'emoji': '\u{1F381}', + 'name': 'Gift', + }, + { + 'emoji': '\u{1F4A1}', + 'name': 'Light Bulb', + }, + { + 'emoji': '\u{1F4D5}', + 'name': 'Book', + }, + { + 'emoji': '\u{270F}\u{FE0F}', + 'name': 'Pencil', + }, + { + 'emoji': '\u{1F4CE}', + 'name': 'Paperclip', + }, + { + 'emoji': '\u{2702}\u{FE0F}', + 'name': 'Scissors', + }, + { + 'emoji': '\u{1F512}', + 'name': 'Lock', + }, + { + 'emoji': '\u{1F511}', + 'name': 'Key', + }, + { + 'emoji': '\u{1F528}', + 'name': 'Hammer', + }, + { + 'emoji': '\u{260E}\u{FE0F}', + 'name': 'Telephone', + }, + { + 'emoji': '\u{1F3C1}', + 'name': 'Flag', + }, + { + 'emoji': '\u{1F682}', + 'name': 'Train', + }, + { + 'emoji': '\u{1F6B2}', + 'name': 'Bicycle', + }, + { + 'emoji': '\u{2708}\u{FE0F}', + 'name': 'Aeroplane', + }, + { + 'emoji': '\u{1F680}', + 'name': 'Rocket', + }, + { + 'emoji': '\u{1F3C6}', + 'name': 'Trophy', + }, + { + 'emoji': '\u{26BD}', + 'name': 'Ball', + }, + { + 'emoji': '\u{1F3B8}', + 'name': 'Guitar', + }, + { + 'emoji': '\u{1F3BA}', + 'name': 'Trumpet', + }, + { + 'emoji': '\u{1F514}', + 'name': 'Bell', + }, + { + 'emoji': '\u{2693}', + 'name': 'Anchor', + }, + { + 'emoji': '\u{1F3A7}', + 'name': 'Headphones', + }, + { + 'emoji': '\u{1F4C1}', + 'name': 'Folder', + }, + { + 'emoji': '\u{1F4CC}', + 'name': 'Pin', + }, +]; + +class KeyVerificationEmoji { + final int number; + KeyVerificationEmoji(this.number); + + String get emoji => _emojiMap[number]['emoji'] ?? ''; + String get name => _emojiMap[number]['name'] ?? ''; +} diff --git a/lib/encryption/utils/olm_session.dart b/lib/encryption/utils/olm_session.dart new file mode 100644 index 0000000..2d66b3a --- /dev/null +++ b/lib/encryption/utils/olm_session.dart @@ -0,0 +1,61 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:olm/olm.dart' as olm; + +import '../../matrix.dart'; + +class OlmSession { + String identityKey; + String? sessionId; + olm.Session? session; + DateTime? lastReceived; + final String key; + String? get pickledSession => session?.pickle(key); + + bool get isValid => session != null; + + OlmSession({ + required this.key, + required this.identityKey, + required this.sessionId, + required this.session, + required this.lastReceived, + }); + + OlmSession.fromJson(Map dbEntry, String key) + : key = key, + identityKey = dbEntry['identity_key'] ?? '' { + session = olm.Session(); + try { + session!.unpickle(key, dbEntry['pickle']); + sessionId = dbEntry['session_id']; + lastReceived = + DateTime.fromMillisecondsSinceEpoch(dbEntry['last_received'] ?? 0); + assert(sessionId == session!.session_id()); + } catch (e, s) { + Logs().e('[LibOlm] Could not unpickle olm session', e, s); + dispose(); + } + } + + void dispose() { + session?.free(); + session = null; + } +} diff --git a/lib/encryption/utils/outbound_group_session.dart b/lib/encryption/utils/outbound_group_session.dart new file mode 100644 index 0000000..45d9a09 --- /dev/null +++ b/lib/encryption/utils/outbound_group_session.dart @@ -0,0 +1,72 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:convert'; + +import 'package:olm/olm.dart' as olm; + +import '../../matrix.dart'; + +class OutboundGroupSession { + /// The devices is a map from user id to device id to if the device is blocked. + /// This way we can easily know if a new user is added, leaves, a new devices is added, and, + /// very importantly, if we block a device. These are all important for determining if/when + /// an outbound session needs to be rotated. + Map> devices = {}; + // Default to a date, that would get this session rotated in any case to make handling easier + DateTime creationTime = DateTime.fromMillisecondsSinceEpoch(0); + olm.OutboundGroupSession? outboundGroupSession; + int? get sentMessages => outboundGroupSession?.message_index(); + bool get isValid => outboundGroupSession != null; + final String key; + + OutboundGroupSession( + {required this.devices, + required this.creationTime, + required this.outboundGroupSession, + required this.key}); + + OutboundGroupSession.fromJson(Map dbEntry, String key) + : key = key { + try { + for (final entry in json.decode(dbEntry['device_ids']).entries) { + devices[entry.key] = Map.from(entry.value); + } + } catch (e) { + // devices is bad (old data), so just not use this session + Logs().i( + '[OutboundGroupSession] Session in database is old, not using it. ' + + e.toString()); + return; + } + outboundGroupSession = olm.OutboundGroupSession(); + try { + outboundGroupSession!.unpickle(key, dbEntry['pickle']); + creationTime = + DateTime.fromMillisecondsSinceEpoch(dbEntry['creation_time']); + } catch (e, s) { + dispose(); + Logs().e('[LibOlm] Unable to unpickle outboundGroupSession', e, s); + } + } + + void dispose() { + outboundGroupSession?.free(); + outboundGroupSession = null; + } +} diff --git a/lib/encryption/utils/session_key.dart b/lib/encryption/utils/session_key.dart new file mode 100644 index 0000000..310b139 --- /dev/null +++ b/lib/encryption/utils/session_key.dart @@ -0,0 +1,115 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:matrix/encryption/utils/stored_inbound_group_session.dart'; +import 'package:matrix_api_lite/src/utils/filter_map_extension.dart'; +import 'package:olm/olm.dart' as olm; + +import '../../matrix.dart'; + +class SessionKey { + /// The raw json content of the key + Map content = {}; + + /// Map of stringified-index to event id, so that we can detect replay attacks + Map indexes; + + /// Map of userId to map of deviceId to index, that we know that device receivied, e.g. sending it ourself. + /// Used for automatically answering key requests + Map> allowedAtIndex; + + /// Underlying olm [InboundGroupSession] object + olm.InboundGroupSession? inboundGroupSession; + + /// Key for libolm pickle / unpickle + final String key; + + /// Forwarding keychain + List get forwardingCurve25519KeyChain => + (content['forwarding_curve25519_key_chain'] != null + ? List.from(content['forwarding_curve25519_key_chain']) + : null) ?? + []; + + /// Claimed keys of the original sender + late Map senderClaimedKeys; + + /// Sender curve25519 key + String senderKey; + + /// Is this session valid? + bool get isValid => inboundGroupSession != null; + + /// roomId for this session + String roomId; + + /// Id of this session + String sessionId; + + SessionKey( + {required this.content, + required this.inboundGroupSession, + required this.key, + Map? indexes, + Map>? allowedAtIndex, + required this.roomId, + required this.sessionId, + required this.senderKey, + required this.senderClaimedKeys}) + : indexes = indexes ?? {}, + allowedAtIndex = allowedAtIndex ?? >{}; + + SessionKey.fromDb(StoredInboundGroupSession dbEntry, String key) + : key = key, + content = Event.getMapFromPayload(dbEntry.content), + indexes = Event.getMapFromPayload(dbEntry.indexes) + .catchMap((k, v) => MapEntry(k, v)), + allowedAtIndex = Event.getMapFromPayload(dbEntry.allowedAtIndex) + .catchMap((k, v) => MapEntry(k, Map.from(v))), + roomId = dbEntry.roomId, + sessionId = dbEntry.sessionId, + senderKey = dbEntry.senderKey, + inboundGroupSession = olm.InboundGroupSession() { + final parsedSenderClaimedKeys = + Event.getMapFromPayload(dbEntry.senderClaimedKeys) + .catchMap((k, v) => MapEntry(k, v)); + // we need to try...catch as the map used to be and that will throw an error. + senderClaimedKeys = (parsedSenderClaimedKeys.isNotEmpty) + ? parsedSenderClaimedKeys + : (content['sender_claimed_keys'] is Map + ? content['sender_claimed_keys'] + .catchMap((k, v) => MapEntry(k, v)) + : (content['sender_claimed_ed25519_key'] is String + ? { + 'ed25519': content['sender_claimed_ed25519_key'] + } + : {})); + + try { + inboundGroupSession!.unpickle(key, dbEntry.pickle); + } catch (e, s) { + dispose(); + Logs().e('[LibOlm] Unable to unpickle inboundGroupSession', e, s); + } + } + + void dispose() { + inboundGroupSession?.free(); + inboundGroupSession = null; + } +} diff --git a/lib/encryption/utils/ssss_cache.dart b/lib/encryption/utils/ssss_cache.dart new file mode 100644 index 0000000..df22d87 --- /dev/null +++ b/lib/encryption/utils/ssss_cache.dart @@ -0,0 +1,40 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +class SSSSCache { + final String? type; + final String? keyId; + final String? ciphertext; + final String? content; + + const SSSSCache({this.type, this.keyId, this.ciphertext, this.content}); + + factory SSSSCache.fromJson(Map json) => SSSSCache( + type: json['type'], + keyId: json['key_id'], + ciphertext: json['ciphertext'], + content: json['content'], + ); + + Map toJson() => { + 'type': type, + 'key_id': keyId, + 'ciphertext': ciphertext, + 'content': content, + }; +} diff --git a/lib/encryption/utils/stored_inbound_group_session.dart b/lib/encryption/utils/stored_inbound_group_session.dart new file mode 100644 index 0000000..028ec40 --- /dev/null +++ b/lib/encryption/utils/stored_inbound_group_session.dart @@ -0,0 +1,66 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +class StoredInboundGroupSession { + final String roomId; + final String sessionId; + final String pickle; + final String content; + final String indexes; + final String allowedAtIndex; + final bool uploaded; + final String senderKey; + final String senderClaimedKeys; + + StoredInboundGroupSession({ + required this.roomId, + required this.sessionId, + required this.pickle, + required this.content, + required this.indexes, + required this.allowedAtIndex, + required this.uploaded, + required this.senderKey, + required this.senderClaimedKeys, + }); + + factory StoredInboundGroupSession.fromJson(Map json) => + StoredInboundGroupSession( + roomId: json['room_id'], + sessionId: json['session_id'], + pickle: json['pickle'], + content: json['content'], + indexes: json['indexes'], + allowedAtIndex: json['allowed_at_index'], + uploaded: json['uploaded'], + senderKey: json['sender_key'], + senderClaimedKeys: json['sender_claimed_keys'], + ); + + Map toJson() => { + 'room_id': roomId, + 'session_id': sessionId, + 'pickle': pickle, + 'content': content, + 'indexes': indexes, + 'allowed_at_index': allowedAtIndex, + 'uploaded': uploaded, + 'sender_key': senderKey, + 'sender_claimed_keys': senderClaimedKeys, + }; +} diff --git a/lib/matrix.dart b/lib/matrix.dart new file mode 100644 index 0000000..24a0c9e --- /dev/null +++ b/lib/matrix.dart @@ -0,0 +1,49 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +/// Matrix SDK written in pure Dart. +library matrix; + +export 'package:matrix_api_lite/matrix_api_lite.dart'; + +export 'src/client.dart'; +export 'src/database/database_api.dart'; +export 'src/database/hive_database.dart'; +export 'src/database/fluffybox_database.dart'; +export 'src/event.dart'; +export 'src/event_status.dart'; +export 'src/voip.dart'; +export 'src/voip_content.dart'; +export 'src/room.dart'; +export 'src/timeline.dart'; +export 'src/user.dart'; +export 'src/utils/commands_extension.dart'; +export 'src/utils/crypto/encrypted_file.dart'; +export 'src/utils/device_keys_list.dart'; +export 'src/utils/event_update.dart'; +export 'src/utils/http_timeout.dart'; +export 'src/utils/image_pack_extension.dart'; +export 'src/utils/matrix_file.dart'; +export 'src/utils/matrix_id_string_extension.dart'; +export 'src/utils/matrix_localizations.dart'; +export 'src/utils/receipt.dart'; +export 'src/utils/sync_update_extension.dart'; +export 'src/utils/to_device_event.dart'; +export 'src/utils/uia_request.dart'; +export 'src/utils/uri_extension.dart'; +export 'src/voip_content.dart'; diff --git a/lib/src/client.dart b/lib/src/client.dart new file mode 100644 index 0000000..ba1129b --- /dev/null +++ b/lib/src/client.dart @@ -0,0 +1,2542 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:async'; +import 'dart:convert'; +import 'dart:core'; +import 'dart:typed_data'; + +import 'package:http/http.dart' as http; +import 'package:matrix/src/utils/run_in_root.dart'; +import 'package:matrix/src/utils/sync_update_item_count.dart'; +import 'package:mime/mime.dart'; +import 'package:olm/olm.dart' as olm; +import 'package:collection/collection.dart' show IterableExtension; + +import '../encryption.dart'; +import '../matrix.dart'; +import 'utils/run_benchmarked.dart'; +import 'utils/multilock.dart'; + +typedef RoomSorter = int Function(Room a, Room b); + +enum LoginState { loggedIn, loggedOut } + +extension TrailingSlash on Uri { + Uri stripTrailingSlash() => path.endsWith('/') + ? replace(path: path.substring(0, path.length - 1)) + : this; +} + +/// Represents a Matrix client to communicate with a +/// [Matrix](https://matrix.org) homeserver and is the entry point for this +/// SDK. +class Client extends MatrixApi { + int? _id; + + // Keeps track of the currently ongoing syncRequest + // in case we want to cancel it. + int _currentSyncId = -1; + + int? get id => _id; + + final FutureOr Function(Client)? databaseBuilder; + final FutureOr Function(Client)? legacyDatabaseBuilder; + final FutureOr Function(Client)? databaseDestroyer; + final FutureOr Function(Client)? legacyDatabaseDestroyer; + DatabaseApi? _database; + + DatabaseApi? get database => _database; + + @deprecated + MatrixApi get api => this; + + Encryption? encryption; + + Set verificationMethods; + + Set importantStateEvents; + + Set roomPreviewLastEvents; + + Set supportedLoginTypes; + + int sendMessageTimeoutSeconds; + + bool requestHistoryOnLimitedTimeline; + + final bool formatLocalpart; + + final bool mxidLocalPartFallback; + + // For CommandsClientExtension + final Map Function(CommandArgs)> commands = {}; + final Filter syncFilter; + + String? syncFilterId; + + final Future Function(FutureOr Function(Q), Q, + {String debugLabel})? compute; + + Future runInBackground( + FutureOr Function(U arg) function, U arg) async { + final compute = this.compute; + if (compute != null) { + return await compute(function, arg); + } + return await function(arg); + } + + /// Create a client + /// [clientName] = unique identifier of this client + /// [databaseBuilder]: A function that creates the database instance, that will be used. + /// [legacyDatabaseBuilder]: Use this for your old database implementation to perform an automatic migration + /// [databaseDestroyer]: A function that can be used to destroy a database instance, for example by deleting files from disk. + /// [verificationMethods]: A set of all the verification methods this client can handle. Includes: + /// KeyVerificationMethod.numbers: Compare numbers. Most basic, should be supported + /// KeyVerificationMethod.emoji: Compare emojis + /// [importantStateEvents]: A set of all the important state events to load when the client connects. + /// To speed up performance only a set of state events is loaded on startup, those that are + /// needed to display a room list. All the remaining state events are automatically post-loaded + /// when opening the timeline of a room or manually by calling `room.postLoad()`. + /// This set will always include the following state events: + /// - m.room.name + /// - m.room.avatar + /// - m.room.message + /// - m.room.encrypted + /// - m.room.encryption + /// - m.room.canonical_alias + /// - m.room.tombstone + /// - *some* m.room.member events, where needed + /// [roomPreviewLastEvents]: The event types that should be used to calculate the last event + /// in a room for the room list. + /// Set [requestHistoryOnLimitedTimeline] to controll the automatic behaviour if the client + /// receives a limited timeline flag for a room. + /// If [mxidLocalPartFallback] is true, then the local part of the mxid will be shown + /// if there is no other displayname available. If not then this will return "Unknown user". + /// If [formatLocalpart] is true, then the localpart of an mxid will + /// be formatted in the way, that all "_" characters are becomming white spaces and + /// the first character of each word becomes uppercase. + /// If your client supports more login types like login with token or SSO, then add this to + /// [supportedLoginTypes]. Set a custom [syncFilter] if you like. By default the app + /// will use lazy_load_members. + /// Set [compute] to the Flutter compute method to enable the SDK to run some + /// code in background. + Client( + this.clientName, { + this.databaseBuilder, + this.databaseDestroyer, + this.legacyDatabaseBuilder, + this.legacyDatabaseDestroyer, + @Deprecated('This is now always enabled by default.') + bool? enableE2eeRecovery, + Set? verificationMethods, + http.Client? httpClient, + Set? importantStateEvents, + Set? roomPreviewLastEvents, + this.pinUnreadRooms = false, + this.pinInvitedRooms = true, + this.sendMessageTimeoutSeconds = 60, + this.requestHistoryOnLimitedTimeline = false, + Set? supportedLoginTypes, + this.mxidLocalPartFallback = true, + this.formatLocalpart = true, + this.compute, + Filter? syncFilter, + @deprecated bool? debug, + }) : syncFilter = syncFilter ?? + Filter( + room: RoomFilter( + state: StateFilter(lazyLoadMembers: true), + ), + ), + importantStateEvents = importantStateEvents ??= {}, + roomPreviewLastEvents = roomPreviewLastEvents ??= {}, + supportedLoginTypes = + supportedLoginTypes ?? {AuthenticationTypes.password}, + __loginState = LoginState.loggedOut, + verificationMethods = verificationMethods ?? {}, + super( + httpClient: + VariableTimeoutHttpClient(httpClient ?? http.Client())) { + importantStateEvents.addAll([ + EventTypes.RoomName, + EventTypes.RoomAvatar, + EventTypes.Message, + EventTypes.Encrypted, + EventTypes.Encryption, + EventTypes.RoomCanonicalAlias, + EventTypes.RoomTombstone, + EventTypes.spaceChild, + EventTypes.spaceParent, + EventTypes.RoomCreate, + ]); + roomPreviewLastEvents.addAll([ + EventTypes.Message, + EventTypes.Encrypted, + EventTypes.Sticker, + ]); + + // register all the default commands + registerDefaultCommands(); + } + + /// The required name for this client. + final String clientName; + + /// The Matrix ID of the current logged user. + String? get userID => _userID; + String? _userID; + + /// This points to the position in the synchronization history. + String? prevBatch; + + /// The device ID is an unique identifier for this device. + String? get deviceID => _deviceID; + String? _deviceID; + + /// The device name is a human readable identifier for this device. + String? get deviceName => _deviceName; + String? _deviceName; + + /// Returns the current login state. + LoginState get loginState => __loginState; + LoginState __loginState; + set _loginState(LoginState state) { + __loginState = state; + onLoginStateChanged.add(state); + } + + bool isLogged() => accessToken != null; + + /// A list of all rooms the user is participating or invited. + List get rooms => _rooms; + List _rooms = []; + + /// Whether this client supports end-to-end encryption using olm. + bool get encryptionEnabled => encryption?.enabled == true; + + /// Whether this client is able to encrypt and decrypt files. + bool get fileEncryptionEnabled => encryptionEnabled && true; + + String get identityKey => encryption?.identityKey ?? ''; + + String get fingerprintKey => encryption?.fingerprintKey ?? ''; + + /// Wheather this session is unknown to others + bool get isUnknownSession => + userDeviceKeys[userID]?.deviceKeys[deviceID]?.signed != true; + + /// Warning! This endpoint is for testing only! + set rooms(List newList) { + Logs().w('Warning! This endpoint is for testing only!'); + _rooms = newList; + } + + /// Key/Value store of account data. + Map accountData = {}; + + /// Presences of users by a given matrix ID + Map presences = {}; + + int _transactionCounter = 0; + + String generateUniqueTransactionId() { + _transactionCounter++; + return '$clientName-$_transactionCounter-${DateTime.now().millisecondsSinceEpoch}'; + } + + Room? getRoomByAlias(String alias) { + for (final room in rooms) { + if (room.canonicalAlias == alias) return room; + } + return null; + } + + /// Searches in the local cache for the given room and returns null if not + /// found. If you have loaded the [loadArchive()] before, it can also return + /// archived rooms. + Room? getRoomById(String id) { + for (final room in [...rooms, ..._archivedRooms]) { + if (room.id == id) return room; + } + + return null; + } + + Map get directChats => + accountData['m.direct']?.content ?? {}; + + /// Returns the (first) room ID from the store which is a private chat with the user [userId]. + /// Returns null if there is none. + String? getDirectChatFromUserId(String userId) { + final directChats = accountData['m.direct']?.content[userId]; + if (directChats is List && directChats.isNotEmpty) { + final potentialRooms = directChats + .cast() + .map(getRoomById) + .where((room) => room != null && room.membership == Membership.join); + if (potentialRooms.isNotEmpty) { + return potentialRooms.fold(potentialRooms.first!, + (Room prev, Room? r) { + if (r == null) { + return prev; + } + final prevLast = + prev.lastEvent?.originServerTs.millisecondsSinceEpoch ?? 0; + final rLast = r.lastEvent?.originServerTs.millisecondsSinceEpoch ?? 0; + + return rLast > prevLast ? r : prev; + }).id; + } + } + for (final room in rooms) { + if (room.membership == Membership.invite && + room.getState(EventTypes.RoomMember, userID!)?.senderId == userId && + room.getState(EventTypes.RoomMember, userID!)?.content['is_direct'] == + true) { + return room.id; + } + } + return null; + } + + /// Gets discovery information about the domain. The file may include additional keys. + Future getDiscoveryInformationsByUserId( + String MatrixIdOrDomain, + ) async { + try { + final response = await http.get(Uri.https( + MatrixIdOrDomain.domain ?? '', '/.well-known/matrix/client')); + var respBody = response.body; + try { + respBody = utf8.decode(response.bodyBytes); + } catch (_) { + // No-OP + } + final rawJson = json.decode(respBody); + return DiscoveryInformation.fromJson(rawJson); + } catch (_) { + // we got an error processing or fetching the well-known information, let's + // provide a reasonable fallback. + return DiscoveryInformation( + mHomeserver: HomeserverInformation( + baseUrl: Uri.https(MatrixIdOrDomain.domain ?? '', '')), + ); + } + } + + /// Checks the supported versions of the Matrix protocol and the supported + /// login types. Throws an exception if the server is not compatible with the + /// client and sets [homeserver] to [homeserverUrl] if it is. Supports the + /// types `Uri` and `String`. + Future checkHomeserver(dynamic homeserverUrl, + {bool checkWellKnown = true}) async { + try { + var homeserver = this.homeserver = + (homeserverUrl is Uri) ? homeserverUrl : Uri.parse(homeserverUrl); + homeserver = this.homeserver = homeserver.stripTrailingSlash(); + + // Look up well known + DiscoveryInformation? wellKnown; + if (checkWellKnown) { + try { + wellKnown = await getWellknown(); + homeserver = this.homeserver = + wellKnown.mHomeserver.baseUrl.stripTrailingSlash(); + } catch (e) { + Logs().v('Found no well known information', e); + } + } + + // Check if server supports at least one supported version + final versions = await getVersions(); + if (!versions.versions + .any((version) => supportedVersions.contains(version))) { + throw BadServerVersionsException( + versions.versions.toSet(), supportedVersions); + } + + final loginTypes = await getLoginFlows() ?? []; + if (!loginTypes.any((f) => supportedLoginTypes.contains(f.type))) { + throw BadServerLoginTypesException( + loginTypes.map((f) => f.type ?? '').toSet(), supportedLoginTypes); + } + + return HomeserverSummary( + discoveryInformation: wellKnown, + versions: versions, + loginFlows: loginTypes, + ); + } catch (_) { + homeserver = null; + rethrow; + } + } + + /// Checks to see if a username is available, and valid, for the server. + /// Returns the fully-qualified Matrix user ID (MXID) that has been registered. + /// You have to call [checkHomeserver] first to set a homeserver. + @override + Future register({ + String? username, + String? password, + String? deviceId, + String? initialDeviceDisplayName, + bool? inhibitLogin, + AuthenticationData? auth, + AccountKind? kind, + }) async { + final response = await super.register( + username: username, + password: password, + auth: auth, + deviceId: deviceId, + initialDeviceDisplayName: initialDeviceDisplayName, + inhibitLogin: inhibitLogin, + ); + + // Connect if there is an access token in the response. + final accessToken = response.accessToken; + final deviceId_ = response.deviceId; + final userId = response.userId; + final homeserver = this.homeserver; + if (accessToken == null || deviceId_ == null || homeserver == null) { + throw Exception( + 'Registered but token, device ID, user ID or homeserver is null.'); + } + await init( + newToken: accessToken, + newUserID: userId, + newHomeserver: homeserver, + newDeviceName: initialDeviceDisplayName ?? '', + newDeviceID: deviceId_); + return response; + } + + /// Handles the login and allows the client to call all APIs which require + /// authentication. Returns false if the login was not successful. Throws + /// MatrixException if login was not successful. + /// To just login with the username 'alice' you set [identifier] to: + /// `AuthenticationUserIdentifier(user: 'alice')` + /// Maybe you want to set [user] to the same String to stay compatible with + /// older server versions. + @override + Future login( + LoginType type, { + AuthenticationIdentifier? identifier, + String? password, + String? token, + String? deviceId, + String? initialDeviceDisplayName, + AuthenticationData? auth, + @Deprecated('Deprecated in favour of identifier.') String? user, + @Deprecated('Deprecated in favour of identifier.') String? medium, + @Deprecated('Deprecated in favour of identifier.') String? address, + }) async { + if (homeserver == null && user != null && user.isValidMatrixId) { + await checkHomeserver(user.domain); + } + final response = await super.login( + type, + identifier: identifier, + password: password, + token: token, + deviceId: deviceId, + initialDeviceDisplayName: initialDeviceDisplayName, + // ignore: deprecated_member_use + user: user, + // ignore: deprecated_member_use + medium: medium, + // ignore: deprecated_member_use + address: address, + ); + + // Connect if there is an access token in the response. + final accessToken = response.accessToken; + final deviceId_ = response.deviceId; + final userId = response.userId; + final homeserver_ = homeserver; + if (accessToken == null || + deviceId_ == null || + userId == null || + homeserver_ == null) { + throw Exception('Registered but token, device ID or user ID is null.'); + } + await init( + newToken: accessToken, + newUserID: userId, + newHomeserver: homeserver_, + newDeviceName: initialDeviceDisplayName ?? '', + newDeviceID: deviceId_, + ); + return response; + } + + /// Sends a logout command to the homeserver and clears all local data, + /// including all persistent data from the store. + @override + Future logout() async { + try { + await super.logout(); + } catch (e, s) { + Logs().e('Logout failed', e, s); + rethrow; + } finally { + await clear(); + } + } + + /// Sends a logout command to the homeserver and clears all local data, + /// including all persistent data from the store. + @override + Future logoutAll() async { + try { + await super.logoutAll(); + } catch (e, s) { + Logs().e('Logout all failed', e, s); + rethrow; + } finally { + await clear(); + } + } + + /// Run any request and react on user interactive authentication flows here. + Future uiaRequestBackground( + Future Function(AuthenticationData? auth) request) { + final completer = Completer(); + UiaRequest? uia; + uia = UiaRequest( + request: request, + onUpdate: (state) { + if (uia != null) { + if (state == UiaRequestState.done) { + completer.complete(uia.result); + } else if (state == UiaRequestState.fail) { + completer.completeError(uia.error!); + } else { + onUiaRequest.add(uia); + } + } + }, + ); + return completer.future; + } + + /// Returns an existing direct room ID with this user or creates a new one. + /// By default encryption will be enabled if the client supports encryption + /// and the other user has uploaded any encryption keys. + Future startDirectChat( + String mxid, { + bool? enableEncryption, + List? initialState, + bool waitForSync = true, + }) async { + // Try to find an existing direct chat + final directChatRoomId = getDirectChatFromUserId(mxid); + if (directChatRoomId != null) return directChatRoomId; + + enableEncryption ??= + encryptionEnabled && await userOwnsEncryptionKeys(mxid); + if (enableEncryption) { + initialState ??= []; + if (!initialState.any((s) => s.type == EventTypes.Encryption)) { + initialState.add(StateEvent( + content: { + 'algorithm': supportedGroupEncryptionAlgorithms.first, + }, + type: EventTypes.Encryption, + )); + } + } + + // Start a new direct chat + final roomId = await createRoom( + invite: [mxid], + isDirect: true, + preset: CreateRoomPreset.trustedPrivateChat, + initialState: initialState, + ); + + if (waitForSync && getRoomById(roomId) == null) { + // Wait for room actually appears in sync + await onSync.stream + .firstWhere((sync) => sync.rooms?.join?.containsKey(roomId) ?? false); + } + + await Room(id: roomId, client: this).addToDirectChat(mxid); + + return roomId; + } + + /// Simplified method to create a new group chat. By default it is a private + /// chat. The encryption is enabled if this client supports encryption and + /// the preset is not a public chat. + Future createGroupChat({ + String? groupName, + bool? enableEncryption, + List? invite, + CreateRoomPreset preset = CreateRoomPreset.privateChat, + List? initialState, + Visibility? visibility, + bool waitForSync = true, + }) async { + enableEncryption ??= + encryptionEnabled && preset != CreateRoomPreset.publicChat; + if (enableEncryption) { + initialState ??= []; + if (!initialState.any((s) => s.type == EventTypes.Encryption)) { + initialState.add(StateEvent( + content: { + 'algorithm': supportedGroupEncryptionAlgorithms.first, + }, + type: EventTypes.Encryption, + )); + } + } + final roomId = await createRoom( + invite: invite, + preset: preset, + name: groupName, + initialState: initialState, + visibility: visibility, + ); + + if (waitForSync) { + if (getRoomById(roomId) == null) { + // Wait for room actually appears in sync + await onSync.stream.firstWhere( + (sync) => sync.rooms?.join?.containsKey(roomId) ?? false); + } + } + return roomId; + } + + /// Checks if the given user has encryption keys. May query keys from the + /// server to answer this. + Future userOwnsEncryptionKeys(String userId) async { + if (userId == userID) return encryptionEnabled; + if (_userDeviceKeys.containsKey(userId)) { + return true; + } + final keys = await queryKeys({userId: []}); + return keys.deviceKeys?[userId]?.isNotEmpty ?? false; + } + + /// Creates a new space and returns the Room ID. The parameters are mostly + /// the same like in [createRoom()]. + /// Be aware that spaces appear in the [rooms] list. You should check if a + /// room is a space by using the `room.isSpace` getter and then just use the + /// room as a space with `room.toSpace()`. + /// + /// https://github.com/matrix-org/matrix-doc/blob/matthew/msc1772/proposals/1772-groups-as-rooms.md + Future createSpace({ + String? name, + String? topic, + Visibility visibility = Visibility.public, + String? spaceAliasName, + List? invite, + List? invite3pid, + String? roomVersion, + }) => + createRoom( + name: name, + topic: topic, + visibility: visibility, + roomAliasName: spaceAliasName, + creationContent: {'type': 'm.space'}, + powerLevelContentOverride: {'events_default': 100}, + invite: invite, + invite3pid: invite3pid, + roomVersion: roomVersion, + ); + + /// Returns the user's own displayname and avatar url. In Matrix it is possible that + /// one user can have different displaynames and avatar urls in different rooms. So + /// this endpoint first checks if the profile is the same in all rooms. If not, the + /// profile will be requested from the homserver. + Future get ownProfile async { + if (rooms.isNotEmpty) { + final profileSet = {}; + for (final room in rooms) { + final user = await room.requestUser(userID!); + if (user != null) { + profileSet.add(Profile( + avatarUrl: user.avatarUrl, + displayName: user.displayName, + userId: user.id, + )); + } + } + if (profileSet.length == 1) return profileSet.single; + } + return getProfileFromUserId(userID!); + } + + final Map _profileCache = {}; + + /// Get the combined profile information for this user. + /// If [getFromRooms] is true then the profile will first be searched from the + /// room memberships. This is unstable if the given user makes use of different displaynames + /// and avatars per room, which is common for some bots and bridges. + /// If [cache] is true then + /// the profile get cached for this session. Please note that then the profile may + /// become outdated if the user changes the displayname or avatar in this session. + Future getProfileFromUserId(String userId, + {bool cache = true, bool getFromRooms = true}) async { + if (getFromRooms) { + final room = rooms.firstWhereOrNull((Room room) => + room.getParticipants().indexWhere((User user) => user.id == userId) != + -1); + if (room != null) { + final user = + room.getParticipants().firstWhere((User user) => user.id == userId); + return Profile( + userId: userId, + displayName: user.displayName, + avatarUrl: user.avatarUrl); + } + } + + var profile = _profileCache[userId]; + if (cache && profile != null) { + return Profile( + userId: userId, + displayName: profile.displayname, + avatarUrl: profile.avatarUrl); + } + profile = await getUserProfile(userId); + _profileCache[userId] = profile; + return Profile( + userId: userId, + displayName: profile.displayname, + avatarUrl: profile.avatarUrl); + } + + final List _archivedRooms = []; + + @Deprecated('Use [loadArchive()] instead.') + Future> get archive => loadArchive(); + + Future> loadArchive() async { + _archivedRooms.clear(); + final syncResp = await sync( + filter: '{"room":{"include_leave":true,"timeline":{"limit":10}}}', + timeout: 0, + ); + + final leave = syncResp.rooms?.leave; + if (leave != null) { + for (final entry in leave.entries) { + final id = entry.key; + final room = entry.value; + final leftRoom = Room( + id: id, + membership: Membership.leave, + client: this, + roomAccountData: + room.accountData?.asMap().map((k, v) => MapEntry(v.type, v)) ?? + {}, + ); + + room.timeline?.events?.forEach((event) { + leftRoom.setState(Event.fromMatrixEvent( + event, + leftRoom, + )); + }); + leftRoom.prev_batch = room.timeline?.prevBatch; + room.state?.forEach((event) { + leftRoom.setState(Event.fromMatrixEvent( + event, + leftRoom, + )); + }); + _archivedRooms.add(leftRoom); + } + } + return _archivedRooms; + } + + /// Uploads a file and automatically caches it in the database, if it is small enough + /// and returns the mxc url. + @override + Future uploadContent(Uint8List file, + {String? filename, String? contentType}) async { + contentType ??= lookupMimeType(filename ?? '', headerBytes: file); + final mxc = await super + .uploadContent(file, filename: filename, contentType: contentType); + + final database = this.database; + if (database != null && file.length <= database.maxFileSize) { + await database.storeFile( + mxc, file, DateTime.now().millisecondsSinceEpoch); + } + return mxc; + } + + /// Sends a typing notification and initiates a megolm session, if needed + @override + Future setTyping( + String userId, + String roomId, + bool typing, { + int? timeout, + }) async { + await super.setTyping(userId, roomId, typing, timeout: timeout); + final room = getRoomById(roomId); + if (typing && room != null && encryptionEnabled && room.encrypted) { + // ignore: unawaited_futures + encryption?.keyManager.prepareOutboundGroupSession(roomId); + } + } + + /// Uploads a new user avatar for this user. Leave file null to remove the + /// current avatar. + Future setAvatar(MatrixFile? file) async { + if (file == null) { + // We send an empty String to remove the avatar. Sending Null **should** + // work but it doesn't with Synapse. See: + // https://gitlab.com/famedly/company/frontend/famedlysdk/-/issues/254 + return setAvatarUrl(userID!, Uri.parse('')); + } + final uploadResp = await uploadContent( + file.bytes, + filename: file.name, + contentType: file.mimeType, + ); + await setAvatarUrl(userID!, uploadResp); + return; + } + + /// Returns the global push rules for the logged in user. + PushRuleSet? get globalPushRules { + final pushrules = accountData['m.push_rules']?.content['global']; + return pushrules != null ? PushRuleSet.fromJson(pushrules) : null; + } + + /// Returns the device push rules for the logged in user. + PushRuleSet? get devicePushRules { + final pushrules = accountData['m.push_rules']?.content['device']; + return pushrules != null ? PushRuleSet.fromJson(pushrules) : null; + } + + static const Set supportedVersions = {'r0.5.0', 'r0.6.0'}; + static const List supportedDirectEncryptionAlgorithms = [ + AlgorithmTypes.olmV1Curve25519AesSha2 + ]; + static const List supportedGroupEncryptionAlgorithms = [ + AlgorithmTypes.megolmV1AesSha2 + ]; + static const int defaultThumbnailSize = 800; + + /// The newEvent signal is the most important signal in this concept. Every time + /// the app receives a new synchronization, this event is called for every signal + /// to update the GUI. For example, for a new message, it is called: + /// onRoomEvent( "m.room.message", "!chat_id:server.com", "timeline", {sender: "@bob:server.com", body: "Hello world"} ) + final StreamController onEvent = StreamController.broadcast(); + + /// The onToDeviceEvent is called when there comes a new to device event. It is + /// already decrypted if necessary. + final StreamController onToDeviceEvent = + StreamController.broadcast(); + + /// Called when the login state e.g. user gets logged out. + final StreamController onLoginStateChanged = + StreamController.broadcast(); + + /// Called when the local cache is reset + final StreamController onCacheCleared = StreamController.broadcast(); + + /// Encryption errors are coming here. + final StreamController onEncryptionError = + StreamController.broadcast(); + + /// This is called once, when the first sync has been processed. + final StreamController onFirstSync = StreamController.broadcast(); + + /// When a new sync response is coming in, this gives the complete payload. + final StreamController onSync = StreamController.broadcast(); + + /// This gives the current status of the synchronization + final StreamController onSyncStatus = + StreamController.broadcast(); + + /// Callback will be called on presences. + final StreamController onPresence = StreamController.broadcast(); + + /// Callback will be called on account data updates. + final StreamController onAccountData = + StreamController.broadcast(); + + /// Will be called on call invites. + final StreamController onCallInvite = StreamController.broadcast(); + + /// Will be called on call hangups. + final StreamController onCallHangup = StreamController.broadcast(); + + /// Will be called on call candidates. + final StreamController onCallCandidates = StreamController.broadcast(); + + /// Will be called on call answers. + final StreamController onCallAnswer = StreamController.broadcast(); + + /// Will be called on call replaces. + final StreamController onCallReplaces = StreamController.broadcast(); + + /// Will be called on select answers. + final StreamController onCallSelectAnswer = + StreamController.broadcast(); + + /// Will be called on rejects. + final StreamController onCallReject = StreamController.broadcast(); + + /// Will be called on negotiates. + final StreamController onCallNegotiate = StreamController.broadcast(); + + /// Will be called on Asserted Identity received. + final StreamController onAssertedIdentityReceived = + StreamController.broadcast(); + + /// Will be called on SDPStream Metadata changed. + final StreamController onSDPStreamMetadataChangedReceived = + StreamController.broadcast(); + + /// Will be called when another device is requesting session keys for a room. + final StreamController onRoomKeyRequest = + StreamController.broadcast(); + + /// Will be called when another device is requesting verification with this device. + final StreamController onKeyVerificationRequest = + StreamController.broadcast(); + + /// When the library calls an endpoint that needs UIA the `UiaRequest` is passed down this screen. + /// The client can open a UIA prompt based on this. + final StreamController onUiaRequest = + StreamController.broadcast(); + + /// How long should the app wait until it retrys the synchronisation after + /// an error? + int syncErrorTimeoutSec = 3; + + @Deprecated('Use init() instead') + void connect({ + String? newToken, + Uri? newHomeserver, + String? newUserID, + String? newDeviceName, + String? newDeviceID, + String? newOlmAccount, + }) => + init( + newToken: newToken, + newHomeserver: newHomeserver, + newUserID: newUserID, + newDeviceName: newDeviceName, + newDeviceID: newDeviceID, + newOlmAccount: newOlmAccount, + ); + + bool _initLock = false; + + /// Sets the user credentials and starts the synchronisation. + /// + /// Before you can connect you need at least an [accessToken], a [homeserver], + /// a [userID], a [deviceID], and a [deviceName]. + /// + /// Usually you don't need to call this method yourself because [login()], [register()] + /// and even the constructor calls it. + /// + /// Sends [LoginState.loggedIn] to [onLoginStateChanged]. + /// + /// If one of [newToken], [newUserID], [newDeviceID], [newDeviceName] is set then + /// all of them must be set! If you don't set them, this method will try to + /// get them from the database. + /// + /// Set [waitForFirstSync] and [waitUntilLoadCompletedLoaded] to false to speed this + /// up. You can then wait for `roomsLoading`, `accountDataLoading` and + /// `userDeviceKeysLoading` where it is necessary. + Future init({ + String? newToken, + Uri? newHomeserver, + String? newUserID, + String? newDeviceName, + String? newDeviceID, + String? newOlmAccount, + bool waitForFirstSync = true, + bool waitUntilLoadCompletedLoaded = true, + }) async { + if ((newToken != null || + newUserID != null || + newDeviceID != null || + newDeviceName != null) && + (newToken == null || + newUserID == null || + newDeviceID == null || + newDeviceName == null)) { + throw Exception( + 'If one of [newToken, newUserID, newDeviceID, newDeviceName] is set then all of them must be set!'); + } + + if (_initLock) throw Exception('[init()] has been called multiple times!'); + _initLock = true; + try { + Logs().i('Initialize client $clientName'); + if (isLogged()) { + throw Exception('User is already logged in! Call [logout()] first!'); + } + + final databaseBuilder = this.databaseBuilder; + if (databaseBuilder != null) { + _database ??= await runBenchmarked( + 'Build database', + () async => await databaseBuilder(this), + ); + } + + String? olmAccount; + String? accessToken; + String? _userID; + final account = await this.database?.getClient(clientName); + if (account != null) { + _id = account['client_id']; + homeserver = Uri.parse(account['homeserver_url']); + accessToken = this.accessToken = account['token']; + _userID = this._userID = account['user_id']; + _deviceID = account['device_id']; + _deviceName = account['device_name']; + syncFilterId = account['sync_filter_id']; + prevBatch = account['prev_batch']; + olmAccount = account['olm_account']; + } + if (newToken != null) { + accessToken = this.accessToken = newToken; + homeserver = newHomeserver; + _userID = this._userID = newUserID; + _deviceID = newDeviceID; + _deviceName = newDeviceName; + olmAccount = newOlmAccount; + } else { + accessToken = this.accessToken = newToken ?? accessToken; + homeserver = newHomeserver ?? homeserver; + _userID = this._userID = newUserID ?? _userID; + _deviceID = newDeviceID ?? _deviceID; + _deviceName = newDeviceName ?? _deviceName; + olmAccount = newOlmAccount ?? olmAccount; + } + + if (accessToken == null || homeserver == null || _userID == null) { + if (legacyDatabaseBuilder != null) { + await _migrateFromLegacyDatabase(); + if (isLogged()) return; + } + // we aren't logged in + encryption?.dispose(); + encryption = null; + _loginState = LoginState.loggedOut; + Logs().i('User is not logged in.'); + _initLock = false; + return; + } + + encryption?.dispose(); + try { + // make sure to throw an exception if libolm doesn't exist + await olm.init(); + olm.get_library_version(); + encryption = Encryption(client: this); + } catch (_) { + encryption?.dispose(); + encryption = null; + } + await encryption?.init(olmAccount); + + final database = this.database; + if (database != null) { + if (id != null) { + await database.updateClient( + homeserver.toString(), + accessToken, + _userID, + _deviceID, + _deviceName, + prevBatch, + encryption?.pickledOlmAccount, + ); + } else { + _id = await database.insertClient( + clientName, + homeserver.toString(), + accessToken, + _userID, + _deviceID, + _deviceName, + prevBatch, + encryption?.pickledOlmAccount, + ); + } + userDeviceKeysLoading = database + .getUserDeviceKeys(this) + .then((keys) => _userDeviceKeys = keys); + roomsLoading = database.getRoomList(this).then((rooms) { + _rooms = rooms; + _sortRooms(); + }); + accountDataLoading = + database.getAccountData().then((data) => accountData = data); + presences.clear(); + if (waitUntilLoadCompletedLoaded) { + await userDeviceKeysLoading; + await roomsLoading; + await accountDataLoading; + } + } + _initLock = false; + _loginState = LoginState.loggedIn; + Logs().i( + 'Successfully connected as ${userID?.localpart} with ${homeserver.toString()}', + ); + + final syncFuture = _sync(); + if (waitForFirstSync) { + await syncFuture; + } + return; + } catch (e, s) { + Logs().e('Initialization failed', e, s); + await logout().catchError((_) => null); + onLoginStateChanged.addError(e, s); + _initLock = false; + rethrow; + } + } + + /// Used for testing only + void setUserId(String s) { + _userID = s; + } + + /// Resets all settings and stops the synchronisation. + Future clear() async { + Logs().outputEvents.clear(); + try { + await abortSync(); + await database?.clear(); + _backgroundSync = true; + } catch (e, s) { + Logs().e('Unable to clear database', e, s); + } finally { + _database = null; + } + + _id = accessToken = syncFilterId = + homeserver = _userID = _deviceID = _deviceName = prevBatch = null; + _rooms = []; + encryption?.dispose(); + encryption = null; + final databaseDestroyer = this.databaseDestroyer; + if (databaseDestroyer != null) { + try { + await database?.close(); + } catch (e, s) { + Logs().e('Unable to close database', e, s); + } + await databaseDestroyer(this); + _database = null; + } + _loginState = LoginState.loggedOut; + } + + bool _backgroundSync = true; + Future? _currentSync; + Future _retryDelay = Future.value(); + + bool get syncPending => _currentSync != null; + + /// Controls the background sync (automatically looping forever if turned on). + set backgroundSync(bool enabled) { + _backgroundSync = enabled; + if (_backgroundSync) { + _sync(); + } + } + + /// Immediately start a sync and wait for completion. + /// If there is an active sync already, wait for the active sync instead. + Future oneShotSync() { + return _sync(); + } + + Future _sync() async { + if (_currentSync == null) { + final _currentSync = this._currentSync = _innerSync(); + // ignore: unawaited_futures + _currentSync.whenComplete(() { + this._currentSync = null; + if (_backgroundSync && isLogged() && !_disposed) { + _sync(); + } + }); + } + await _currentSync; + } + + /// Presence that is set on sync. + PresenceType? syncPresence; + + Future _checkSyncFilter() async { + final userID = this.userID; + if (syncFilterId == null && userID != null) { + final syncFilterId = + this.syncFilterId = await defineFilter(userID, syncFilter); + await database?.storeSyncFilterId(syncFilterId); + } + return; + } + + Future _innerSync() async { + await _retryDelay; + _retryDelay = Future.delayed(Duration(seconds: syncErrorTimeoutSec)); + if (!isLogged() || _disposed || _aborted) return null; + try { + if (_initLock) { + Logs().d('Running sync while init isn\'t done yet, dropping request'); + return; + } + var syncError; + await _checkSyncFilter(); + final syncRequest = sync( + filter: syncFilterId, + since: prevBatch, + timeout: prevBatch != null ? 30000 : null, + setPresence: syncPresence, + ).then((v) => Future.value(v)).catchError((e) { + syncError = e; + return null; + }); + _currentSyncId = syncRequest.hashCode; + onSyncStatus.add(SyncStatusUpdate(SyncStatus.waitingForResponse)); + final syncResp = await syncRequest; + onSyncStatus.add(SyncStatusUpdate(SyncStatus.processing)); + if (syncResp == null) throw syncError ?? 'Unknown sync error'; + if (_currentSyncId != syncRequest.hashCode) { + Logs() + .w('Current sync request ID has changed. Dropping this sync loop!'); + return; + } + + final database = this.database; + if (database != null) { + await userDeviceKeysLoading; + await roomsLoading; + await accountDataLoading; + _currentTransaction = database.transaction(() async { + await _handleSync(syncResp); + if (prevBatch != syncResp.nextBatch) { + await database.storePrevBatch(syncResp.nextBatch); + } + }); + await runBenchmarked( + 'Process sync', + () async => await _currentTransaction, + syncResp.itemCount, + ); + onSyncStatus.add(SyncStatusUpdate(SyncStatus.cleaningUp)); + } else { + await _handleSync(syncResp); + } + if (_disposed || _aborted) return; + if (prevBatch == null) { + onFirstSync.add(true); + prevBatch = syncResp.nextBatch; + } + prevBatch = syncResp.nextBatch; + // ignore: unawaited_futures + database?.deleteOldFiles( + DateTime.now().subtract(Duration(days: 30)).millisecondsSinceEpoch); + await updateUserDeviceKeys(); + if (encryptionEnabled) { + encryption?.onSync(); + } + + // try to process the to_device queue + try { + await processToDeviceQueue(); + } catch (_) {} // we want to dispose any errors this throws + + _retryDelay = Future.value(); + onSyncStatus.add(SyncStatusUpdate(SyncStatus.finished)); + } on MatrixException catch (e, s) { + onSyncStatus.add(SyncStatusUpdate(SyncStatus.error, + error: SdkError(exception: e, stackTrace: s))); + if (e.error == MatrixError.M_UNKNOWN_TOKEN) { + Logs().w('The user has been logged out!'); + await clear(); + } + } on MatrixConnectionException catch (e, s) { + Logs().w('Synchronization connection failed'); + onSyncStatus.add(SyncStatusUpdate(SyncStatus.error, + error: SdkError(exception: e, stackTrace: s))); + } catch (e, s) { + if (!isLogged() || _disposed || _aborted) return; + Logs().e('Error during processing events', e, s); + onSyncStatus.add(SyncStatusUpdate(SyncStatus.error, + error: SdkError( + exception: e is Exception ? e : Exception(e), stackTrace: s))); + } + } + + /// Use this method only for testing utilities! + Future handleSync(SyncUpdate sync, {bool sortAtTheEnd = false}) async { + // ensure we don't upload keys because someone forgot to set a key count + sync.deviceOneTimeKeysCount ??= {'signed_curve25519': 100}; + await _handleSync(sync, sortAtTheEnd: sortAtTheEnd); + } + + Future _handleSync(SyncUpdate sync, {bool sortAtTheEnd = false}) async { + final syncToDevice = sync.toDevice; + if (syncToDevice != null) { + await _handleToDeviceEvents(syncToDevice); + } + + if (sync.rooms != null) { + final join = sync.rooms?.join; + if (join != null) { + await _handleRooms(join, sortAtTheEnd: sortAtTheEnd); + } + final invite = sync.rooms?.invite; + if (invite != null) { + await _handleRooms(invite, sortAtTheEnd: sortAtTheEnd); + } + final leave = sync.rooms?.leave; + if (leave != null) { + await _handleRooms(leave, sortAtTheEnd: sortAtTheEnd); + } + } + for (final newPresence in sync.presence ?? []) { + presences[newPresence.senderId] = newPresence; + onPresence.add(newPresence); + } + for (final newAccountData in sync.accountData ?? []) { + await database?.storeAccountData( + newAccountData.type, + jsonEncode(newAccountData.content), + ); + accountData[newAccountData.type] = newAccountData; + onAccountData.add(newAccountData); + } + + final syncDeviceLists = sync.deviceLists; + if (syncDeviceLists != null) { + await _handleDeviceListsEvents(syncDeviceLists); + } + if (encryptionEnabled) { + encryption?.handleDeviceOneTimeKeysCount( + sync.deviceOneTimeKeysCount, sync.deviceUnusedFallbackKeyTypes); + } + _sortRooms(); + onSync.add(sync); + } + + Future _handleDeviceListsEvents(DeviceListsUpdate deviceLists) async { + if (deviceLists.changed is List) { + for (final userId in deviceLists.changed ?? []) { + final userKeys = _userDeviceKeys[userId]; + if (userKeys != null) { + userKeys.outdated = true; + await database?.storeUserDeviceKeysInfo(userId, true); + } + } + for (final userId in deviceLists.left ?? []) { + if (_userDeviceKeys.containsKey(userId)) { + _userDeviceKeys.remove(userId); + } + } + } + } + + Future _handleToDeviceEvents(List events) async { + for (final event in events) { + var toDeviceEvent = ToDeviceEvent.fromJson(event.toJson()); + Logs().v('Got to_device event of type ${toDeviceEvent.type}'); + if (toDeviceEvent.type == EventTypes.Encrypted && encryptionEnabled) { + toDeviceEvent = await encryption!.decryptToDeviceEvent(toDeviceEvent); + Logs().v('Decrypted type is: ${toDeviceEvent.type}'); + } + if (encryptionEnabled) { + await encryption?.handleToDeviceEvent(toDeviceEvent); + } + onToDeviceEvent.add(toDeviceEvent); + } + } + + Future _handleRooms(Map rooms, + {bool sortAtTheEnd = false}) async { + var handledRooms = 0; + for (final entry in rooms.entries) { + onSyncStatus.add(SyncStatusUpdate( + SyncStatus.processing, + progress: ++handledRooms / rooms.length, + )); + final id = entry.key; + final room = entry.value; + + await database?.storeRoomUpdate(id, room, this); + _updateRoomsByRoomUpdate(id, room); + + /// Handle now all room events and save them in the database + if (room is JoinedRoomUpdate) { + final state = room.state; + if (state != null && state.isNotEmpty) { + // TODO: This method seems to be comperatively slow for some updates + await _handleRoomEvents( + id, state.map((i) => i.toJson()).toList(), EventUpdateType.state, + sortAtTheEnd: sortAtTheEnd); + } + + final timelineEvents = room.timeline?.events; + if (timelineEvents != null && timelineEvents.isNotEmpty) { + await _handleRoomEvents( + id, + timelineEvents.map((i) => i.toJson()).toList(), + sortAtTheEnd ? EventUpdateType.history : EventUpdateType.timeline, + sortAtTheEnd: sortAtTheEnd); + } + + final ephemeral = room.ephemeral; + if (ephemeral != null && ephemeral.isNotEmpty) { + // TODO: This method seems to be comperatively slow for some updates + await _handleEphemerals( + id, ephemeral.map((i) => i.toJson()).toList()); + } + + final accountData = room.accountData; + if (accountData != null && accountData.isNotEmpty) { + await _handleRoomEvents( + id, + accountData.map((i) => i.toJson()).toList(), + EventUpdateType.accountData); + } + } + + if (room is LeftRoomUpdate) { + final timelineEvents = room.timeline?.events; + if (timelineEvents != null && timelineEvents.isNotEmpty) { + await _handleRoomEvents( + id, + timelineEvents.map((i) => i.toJson()).toList(), + EventUpdateType.timeline, + sortAtTheEnd: sortAtTheEnd); + } + final accountData = room.accountData; + if (accountData != null && accountData.isNotEmpty) { + await _handleRoomEvents( + id, + accountData.map((i) => i.toJson()).toList(), + EventUpdateType.accountData); + } + final state = room.state; + if (state != null && state.isNotEmpty) { + await _handleRoomEvents( + id, state.map((i) => i.toJson()).toList(), EventUpdateType.state); + } + } + + if (room is InvitedRoomUpdate) { + final state = room.inviteState; + if (state != null && state.isNotEmpty) { + await _handleRoomEvents(id, state.map((i) => i.toJson()).toList(), + EventUpdateType.inviteState); + } + } + } + } + + Future _handleEphemerals(String id, List events) async { + for (final event in events) { + await _handleEvent(event, id, EventUpdateType.ephemeral); + + // Receipt events are deltas between two states. We will create a + // fake room account data event for this and store the difference + // there. + if (event['type'] == 'm.receipt') { + var room = getRoomById(id); + room ??= Room(id: id, client: this); + + final receiptStateContent = + room.roomAccountData['m.receipt']?.content ?? {}; + for (final eventEntry in event['content'].entries) { + final String eventID = eventEntry.key; + if (event['content'][eventID]['m.read'] != null) { + final Map userTimestampMap = + event['content'][eventID]['m.read']; + for (final userTimestampMapEntry in userTimestampMap.entries) { + final mxid = userTimestampMapEntry.key; + + // Remove previous receipt event from this user + if (receiptStateContent[eventID] is Map && + receiptStateContent[eventID]['m.read'] + is Map && + receiptStateContent[eventID]['m.read'].containsKey(mxid)) { + receiptStateContent[eventID]['m.read'].remove(mxid); + } + if (userTimestampMap[mxid] is Map && + userTimestampMap[mxid].containsKey('ts')) { + receiptStateContent[mxid] = { + 'event_id': eventID, + 'ts': userTimestampMap[mxid]['ts'], + }; + } + } + } + } + event['content'] = receiptStateContent; + await _handleEvent(event, id, EventUpdateType.accountData); + } + } + } + + Future _handleRoomEvents( + String chat_id, List events, EventUpdateType type, + {bool sortAtTheEnd = false}) async { + for (final event in events) { + await _handleEvent(event, chat_id, type, sortAtTheEnd: sortAtTheEnd); + } + } + + Future _handleEvent( + Map event, String roomID, EventUpdateType type, + {bool sortAtTheEnd = false}) async { + if (event['type'] is String && event['content'] is Map) { + // The client must ignore any new m.room.encryption event to prevent + // man-in-the-middle attacks! + final room = getRoomById(roomID); + if (room == null || + (event['type'] == EventTypes.Encryption && + room.encrypted && + event['content']['algorithm'] != + room.getState(EventTypes.Encryption)?.content['algorithm'])) { + return; + } + + var update = EventUpdate( + roomID: roomID, + type: type, + content: event, + ); + if (event['type'] == EventTypes.Encrypted && encryptionEnabled) { + update = await update.decrypt(room); + } + if (event['type'] == EventTypes.Message && + !room.isDirectChat && + database != null && + room.getState(EventTypes.RoomMember, event['sender']) == null) { + // In order to correctly render room list previews we need to fetch the member from the database + final user = await database?.getUser(event['sender'], room); + if (user != null) { + room.setState(user); + } + } + _updateRoomsByEventUpdate(update); + if (type != EventUpdateType.ephemeral) { + await database?.storeEventUpdate(update, this); + } + if (encryptionEnabled) { + await encryption?.handleEventUpdate(update); + } + onEvent.add(update); + + final rawUnencryptedEvent = update.content; + + if (prevBatch != null && type == EventUpdateType.timeline) { + if (rawUnencryptedEvent['type'] == EventTypes.CallInvite) { + onCallInvite.add(Event.fromJson(rawUnencryptedEvent, room)); + } else if (rawUnencryptedEvent['type'] == EventTypes.CallHangup) { + onCallHangup.add(Event.fromJson(rawUnencryptedEvent, room)); + } else if (rawUnencryptedEvent['type'] == EventTypes.CallAnswer) { + onCallAnswer.add(Event.fromJson(rawUnencryptedEvent, room)); + } else if (rawUnencryptedEvent['type'] == EventTypes.CallCandidates) { + onCallCandidates.add(Event.fromJson(rawUnencryptedEvent, room)); + } else if (rawUnencryptedEvent['type'] == EventTypes.CallSelectAnswer) { + onCallSelectAnswer.add(Event.fromJson(rawUnencryptedEvent, room)); + } else if (rawUnencryptedEvent['type'] == EventTypes.CallReject) { + onCallReject.add(Event.fromJson(rawUnencryptedEvent, room)); + } else if (rawUnencryptedEvent['type'] == EventTypes.CallNegotiate) { + onCallNegotiate.add(Event.fromJson(rawUnencryptedEvent, room)); + } else if (rawUnencryptedEvent['type'] == EventTypes.CallReplaces) { + onCallReplaces.add(Event.fromJson(rawUnencryptedEvent, room)); + } else if (rawUnencryptedEvent['type'] == + EventTypes.CallAssertedIdentity || + rawUnencryptedEvent['type'] == + EventTypes.CallAssertedIdentityPrefix) { + onAssertedIdentityReceived + .add(Event.fromJson(rawUnencryptedEvent, room)); + } else if (rawUnencryptedEvent['type'] == + EventTypes.CallSDPStreamMetadataChanged || + rawUnencryptedEvent['type'] == + EventTypes.CallSDPStreamMetadataChangedPrefix) { + onSDPStreamMetadataChangedReceived + .add(Event.fromJson(rawUnencryptedEvent, room)); + } + } + } + } + + void _updateRoomsByRoomUpdate(String roomId, SyncRoomUpdate chatUpdate) { + // Update the chat list item. + // Search the room in the rooms + final roomIndex = rooms.indexWhere((r) => r.id == roomId); + final found = roomIndex != -1; + final membership = chatUpdate is LeftRoomUpdate + ? Membership.leave + : chatUpdate is InvitedRoomUpdate + ? Membership.invite + : Membership.join; + + // Does the chat already exist in the list rooms? + if (!found && membership != Membership.leave) { + final position = membership == Membership.invite ? 0 : rooms.length; + // Add the new chat to the list + final newRoom = chatUpdate is JoinedRoomUpdate + ? Room( + id: roomId, + membership: membership, + prev_batch: chatUpdate.timeline?.prevBatch, + highlightCount: + chatUpdate.unreadNotifications?.highlightCount ?? 0, + notificationCount: + chatUpdate.unreadNotifications?.notificationCount ?? 0, + summary: chatUpdate.summary, + client: this, + ) + : Room(id: roomId, membership: membership, client: this); + rooms.insert(position, newRoom); + } + // If the membership is "leave" then remove the item and stop here + else if (found && membership == Membership.leave) { + rooms.removeAt(roomIndex); + } + // Update notification, highlight count and/or additional informations + else if (found && + chatUpdate is JoinedRoomUpdate && + (rooms[roomIndex].membership != membership || + rooms[roomIndex].notificationCount != + (chatUpdate.unreadNotifications?.notificationCount ?? 0) || + rooms[roomIndex].highlightCount != + (chatUpdate.unreadNotifications?.highlightCount ?? 0) || + chatUpdate.summary != null || + chatUpdate.timeline?.prevBatch != null)) { + rooms[roomIndex].membership = membership; + rooms[roomIndex].notificationCount = + chatUpdate.unreadNotifications?.notificationCount ?? 0; + rooms[roomIndex].highlightCount = + chatUpdate.unreadNotifications?.highlightCount ?? 0; + if (chatUpdate.timeline?.prevBatch != null) { + rooms[roomIndex].prev_batch = chatUpdate.timeline?.prevBatch; + } + + final summary = chatUpdate.summary; + if (summary != null) { + final roomSummaryJson = rooms[roomIndex].summary.toJson() + ..addAll(summary.toJson()); + rooms[roomIndex].summary = RoomSummary.fromJson(roomSummaryJson); + } + rooms[roomIndex].onUpdate.add(rooms[roomIndex].id); + if ((chatUpdate.timeline?.limited ?? false) && + requestHistoryOnLimitedTimeline) { + Logs().v( + 'Limited timeline for ${rooms[roomIndex].id} request history now'); + runInRoot(rooms[roomIndex].requestHistory); + } + } + } + + void _updateRoomsByEventUpdate(EventUpdate eventUpdate) { + if (eventUpdate.type == EventUpdateType.history) return; + + final room = getRoomById(eventUpdate.roomID); + if (room == null) return; + + switch (eventUpdate.type) { + case EventUpdateType.timeline: + case EventUpdateType.state: + case EventUpdateType.inviteState: + final stateEvent = Event.fromJson(eventUpdate.content, room); + if (stateEvent.type == EventTypes.Redaction) { + final String redacts = eventUpdate.content['redacts']; + room.states.forEach( + (String key, Map states) => states.forEach( + (String key, Event state) { + if (state.eventId == redacts) { + state.setRedactionEvent(stateEvent); + } + }, + ), + ); + } else { + // We want to set state the in-memory cache for the room with the new event. + // To do this, we have to respect to not save edits, unless they edit the + // current last event. + // Additionally, we only store the event in-memory if the room has either been + // post-loaded or the event is animportant state event. + final noMessageOrNoEdit = stateEvent.type != EventTypes.Message || + stateEvent.relationshipType != RelationshipTypes.edit; + final editingLastEvent = + stateEvent.relationshipEventId == room.lastEvent?.eventId; + final consecutiveEdit = + room.lastEvent?.relationshipType == RelationshipTypes.edit && + stateEvent.relationshipEventId == + room.lastEvent?.relationshipEventId; + final importantOrRoomLoaded = + eventUpdate.type == EventUpdateType.inviteState || + !room.partial || + importantStateEvents.contains(stateEvent.type); + if ((noMessageOrNoEdit || editingLastEvent || consecutiveEdit) && + importantOrRoomLoaded) { + room.setState(stateEvent); + } + } + break; + case EventUpdateType.accountData: + room.roomAccountData[eventUpdate.content['type']] = + BasicRoomEvent.fromJson(eventUpdate.content); + break; + case EventUpdateType.ephemeral: + room.ephemerals[eventUpdate.content['type']] = + BasicRoomEvent.fromJson(eventUpdate.content); + break; + case EventUpdateType.history: + break; + } + room.onUpdate.add(room.id); + } + + bool _sortLock = false; + + /// If `true` then unread rooms are pinned at the top of the room list. + bool pinUnreadRooms; + + /// If `true` then unread rooms are pinned at the top of the room list. + bool pinInvitedRooms; + + /// The compare function how the rooms should be sorted internally. By default + /// rooms are sorted by timestamp of the last m.room.message event or the last + /// event if there is no known message. + RoomSorter get sortRoomsBy => (a, b) => (a.isFavourite != b.isFavourite) + ? (a.isFavourite ? -1 : 1) + : (pinUnreadRooms && a.notificationCount != b.notificationCount) + ? b.notificationCount.compareTo(a.notificationCount) + : b.timeCreated.millisecondsSinceEpoch + .compareTo(a.timeCreated.millisecondsSinceEpoch); + + void _sortRooms() { + if (_sortLock || rooms.length < 2) return; + _sortLock = true; + rooms.sort(sortRoomsBy); + _sortLock = false; + } + + Future? userDeviceKeysLoading; + Future? roomsLoading; + Future? accountDataLoading; + + /// A map of known device keys per user. + Map get userDeviceKeys => _userDeviceKeys; + Map _userDeviceKeys = {}; + + /// Gets user device keys by its curve25519 key. Returns null if it isn't found + DeviceKeys? getUserDeviceKeysByCurve25519Key(String senderKey) { + for (final user in userDeviceKeys.values) { + final device = user.deviceKeys.values + .firstWhereOrNull((e) => e.curve25519Key == senderKey); + if (device != null) { + return device; + } + } + return null; + } + + Future> _getUserIdsInEncryptedRooms() async { + final userIds = {}; + for (final room in rooms) { + if (room.encrypted) { + try { + final userList = await room.requestParticipants(); + for (final user in userList) { + if ([Membership.join, Membership.invite] + .contains(user.membership)) { + userIds.add(user.id); + } + } + } catch (e, s) { + Logs().e('[E2EE] Failed to fetch participants', e, s); + } + } + } + return userIds; + } + + final Map _keyQueryFailures = {}; + + Future updateUserDeviceKeys() async { + try { + final database = this.database; + if (!isLogged() || database == null) return; + final dbActions = Function()>[]; + final trackedUserIds = await _getUserIdsInEncryptedRooms(); + if (!isLogged()) return; + trackedUserIds.add(userID!); + + // Remove all userIds we no longer need to track the devices of. + _userDeviceKeys + .removeWhere((String userId, v) => !trackedUserIds.contains(userId)); + + // Check if there are outdated device key lists. Add it to the set. + final outdatedLists = >{}; + for (final userId in trackedUserIds) { + final deviceKeysList = + _userDeviceKeys[userId] ??= DeviceKeysList(userId, this); + final failure = _keyQueryFailures[userId.domain]; + + // deviceKeysList.outdated is not nullable but we have seen this error + // in production: `Failed assertion: boolean expression must not be null` + // So this could either be a null safety bug in Dart or a result of + // using unsound null safety. The extra equal check `!= false` should + // save us here. + if (deviceKeysList.outdated != false && + (failure == null || + DateTime.now() + .subtract(Duration(minutes: 5)) + .isAfter(failure))) { + outdatedLists[userId] = []; + } + } + + if (outdatedLists.isNotEmpty) { + // Request the missing device key lists from the server. + final response = await queryKeys(outdatedLists, timeout: 10000); + if (!isLogged()) return; + + final deviceKeys = response.deviceKeys; + if (deviceKeys != null) { + for (final rawDeviceKeyListEntry in deviceKeys.entries) { + final userId = rawDeviceKeyListEntry.key; + final userKeys = + _userDeviceKeys[userId] ??= DeviceKeysList(userId, this); + final oldKeys = Map.from(userKeys.deviceKeys); + userKeys.deviceKeys = {}; + for (final rawDeviceKeyEntry + in rawDeviceKeyListEntry.value.entries) { + final deviceId = rawDeviceKeyEntry.key; + + // Set the new device key for this device + final entry = DeviceKeys.fromMatrixDeviceKeys( + rawDeviceKeyEntry.value, this, oldKeys[deviceId]?.lastActive); + final ed25519Key = entry.ed25519Key; + final curve25519Key = entry.curve25519Key; + if (entry.isValid && + deviceId == entry.deviceId && + ed25519Key != null && + curve25519Key != null) { + // Check if deviceId or deviceKeys are known + if (!oldKeys.containsKey(deviceId)) { + final oldPublicKeys = + await database.deviceIdSeen(userId, deviceId); + if (oldPublicKeys != null && + oldPublicKeys != curve25519Key + ed25519Key) { + Logs().w( + 'Already seen Device ID has been added again. This might be an attack!'); + continue; + } + final oldDeviceId = await database.publicKeySeen(ed25519Key); + if (oldDeviceId != null && oldDeviceId != deviceId) { + Logs().w( + 'Already seen ED25519 has been added again. This might be an attack!'); + continue; + } + final oldDeviceId2 = + await database.publicKeySeen(curve25519Key); + if (oldDeviceId2 != null && oldDeviceId2 != deviceId) { + Logs().w( + 'Already seen Curve25519 has been added again. This might be an attack!'); + continue; + } + await database.addSeenDeviceId( + userId, deviceId, curve25519Key + ed25519Key); + await database.addSeenPublicKey(ed25519Key, deviceId); + await database.addSeenPublicKey(curve25519Key, deviceId); + } + + // is this a new key or the same one as an old one? + // better store an update - the signatures might have changed! + final oldKey = oldKeys[deviceId]; + if (oldKey == null || + (oldKey.ed25519Key == entry.ed25519Key && + oldKey.curve25519Key == entry.curve25519Key)) { + if (oldKey != null) { + // be sure to save the verified status + entry.setDirectVerified(oldKey.directVerified); + entry.blocked = oldKey.blocked; + entry.validSignatures = oldKey.validSignatures; + } + userKeys.deviceKeys[deviceId] = entry; + if (deviceId == deviceID && + entry.ed25519Key == fingerprintKey) { + // Always trust the own device + entry.setDirectVerified(true); + } + dbActions.add(() => database.storeUserDeviceKey( + userId, + deviceId, + json.encode(entry.toJson()), + entry.directVerified, + entry.blocked, + entry.lastActive.millisecondsSinceEpoch, + )); + } else if (oldKeys.containsKey(deviceId)) { + // This shouldn't ever happen. The same device ID has gotten + // a new public key. So we ignore the update. TODO: ask krille + // if we should instead use the new key with unknown verified / blocked status + userKeys.deviceKeys[deviceId] = oldKeys[deviceId]!; + } + } else { + Logs().w('Invalid device ${entry.userId}:${entry.deviceId}'); + } + } + // delete old/unused entries + for (final oldDeviceKeyEntry in oldKeys.entries) { + final deviceId = oldDeviceKeyEntry.key; + if (!userKeys.deviceKeys.containsKey(deviceId)) { + // we need to remove an old key + dbActions + .add(() => database.removeUserDeviceKey(userId, deviceId)); + } + } + userKeys.outdated = false; + dbActions + .add(() => database.storeUserDeviceKeysInfo(userId, false)); + } + } + // next we parse and persist the cross signing keys + final crossSigningTypes = { + 'master': response.masterKeys, + 'self_signing': response.selfSigningKeys, + 'user_signing': response.userSigningKeys, + }; + for (final crossSigningKeysEntry in crossSigningTypes.entries) { + final keyType = crossSigningKeysEntry.key; + final keys = crossSigningKeysEntry.value; + if (keys == null) { + continue; + } + for (final crossSigningKeyListEntry in keys.entries) { + final userId = crossSigningKeyListEntry.key; + final userKeys = + _userDeviceKeys[userId] ??= DeviceKeysList(userId, this); + final oldKeys = + Map.from(userKeys.crossSigningKeys); + userKeys.crossSigningKeys = {}; + // add the types we aren't handling atm back + for (final oldEntry in oldKeys.entries) { + if (!oldEntry.value.usage.contains(keyType)) { + userKeys.crossSigningKeys[oldEntry.key] = oldEntry.value; + } else { + // There is a previous cross-signing key with this usage, that we no + // longer need/use. Clear it from the database. + dbActions.add(() => + database.removeUserCrossSigningKey(userId, oldEntry.key)); + } + } + final entry = CrossSigningKey.fromMatrixCrossSigningKey( + crossSigningKeyListEntry.value, this); + final publicKey = entry.publicKey; + if (entry.isValid && publicKey != null) { + final oldKey = oldKeys[publicKey]; + if (oldKey == null || oldKey.ed25519Key == entry.ed25519Key) { + if (oldKey != null) { + // be sure to save the verification status + entry.setDirectVerified(oldKey.directVerified); + entry.blocked = oldKey.blocked; + entry.validSignatures = oldKey.validSignatures; + } + userKeys.crossSigningKeys[publicKey] = entry; + } else { + // This shouldn't ever happen. The same device ID has gotten + // a new public key. So we ignore the update. TODO: ask krille + // if we should instead use the new key with unknown verified / blocked status + userKeys.crossSigningKeys[publicKey] = oldKey; + } + dbActions.add(() => database.storeUserCrossSigningKey( + userId, + publicKey, + json.encode(entry.toJson()), + entry.directVerified, + entry.blocked, + )); + } + _userDeviceKeys[userId]?.outdated = false; + dbActions + .add(() => database.storeUserDeviceKeysInfo(userId, false)); + } + } + + // now process all the failures + if (response.failures != null) { + for (final failureDomain in response.failures?.keys ?? []) { + _keyQueryFailures[failureDomain] = DateTime.now(); + } + } + } + + if (dbActions.isNotEmpty) { + if (!isLogged()) return; + await database.transaction(() async { + for (final f in dbActions) { + await f(); + } + }); + } + } catch (e, s) { + Logs().e('[LibOlm] Unable to update user device keys', e, s); + } + } + + bool _toDeviceQueueNeedsProcessing = true; + + /// Processes the to_device queue and tries to send every entry. + /// This function MAY throw an error, which just means the to_device queue wasn't + /// proccessed all the way. + Future processToDeviceQueue() async { + final database = this.database; + if (database == null || !_toDeviceQueueNeedsProcessing) { + return; + } + final entries = await database.getToDeviceEventQueue(); + if (entries.isEmpty) { + _toDeviceQueueNeedsProcessing = false; + return; + } + for (final entry in entries) { + // Convert the Json Map to the correct format regarding + // https: //matrix.org/docs/spec/client_server/r0.6.1#put-matrix-client-r0-sendtodevice-eventtype-txnid + final data = entry.content.map((k, v) => + MapEntry>>( + k, + (v as Map).map((k, v) => MapEntry>( + k, Map.from(v))))); + + try { + await super.sendToDevice(entry.type, entry.txnId, data); + } on MatrixException catch (e) { + Logs().w( + '[To-Device] failed to to_device message from the queue to the server. Ignoring error: $e'); + Logs().w('Payload: $data'); + } + await database.deleteFromToDeviceQueue(entry.id); + } + } + + /// Sends a raw to_device event with a [eventType], a [txnId] and a content + /// [messages]. Before sending, it tries to re-send potentially queued + /// to_device events and adds the current one to the queue, should it fail. + @override + Future sendToDevice( + String eventType, + String txnId, + Map>> messages, + ) async { + try { + await processToDeviceQueue(); + await super.sendToDevice(eventType, txnId, messages); + } catch (e, s) { + Logs().w( + '[Client] Problem while sending to_device event, retrying later...', + e, + s); + final database = this.database; + if (database != null) { + _toDeviceQueueNeedsProcessing = true; + await database.insertIntoToDeviceQueue( + eventType, txnId, json.encode(messages)); + } + rethrow; + } + } + + /// Send an (unencrypted) to device [message] of a specific [eventType] to all + /// devices of a set of [users]. + Future sendToDevicesOfUserIds( + Set users, + String eventType, + Map message, { + String? messageId, + }) async { + // Send with send-to-device messaging + final data = >>{}; + for (final user in users) { + data[user] = {'*': message}; + } + await sendToDevice( + eventType, messageId ?? generateUniqueTransactionId(), data); + return; + } + + final MultiLock _sendToDeviceEncryptedLock = MultiLock(); + + /// Sends an encrypted [message] of this [eventType] to these [deviceKeys]. + Future sendToDeviceEncrypted( + List deviceKeys, + String eventType, + Map message, { + String? messageId, + bool onlyVerified = false, + }) async { + final encryption = this.encryption; + if (!encryptionEnabled || encryption == null) return; + // Don't send this message to blocked devices, and if specified onlyVerified + // then only send it to verified devices + if (deviceKeys.isNotEmpty) { + deviceKeys.removeWhere((DeviceKeys deviceKeys) => + deviceKeys.blocked || + (deviceKeys.userId == userID && deviceKeys.deviceId == deviceID) || + (onlyVerified && !deviceKeys.verified)); + if (deviceKeys.isEmpty) return; + } + + // So that we can guarantee order of encrypted to_device messages to be preserved we + // must ensure that we don't attempt to encrypt multiple concurrent to_device messages + // to the same device at the same time. + // A failure to do so can result in edge-cases where encryption and sending order of + // said to_device messages does not match up, resulting in an olm session corruption. + // As we send to multiple devices at the same time, we may only proceed here if the lock for + // *all* of them is freed and lock *all* of them while sending. + + try { + await _sendToDeviceEncryptedLock.lock(deviceKeys); + + // Send with send-to-device messaging + final data = await encryption.encryptToDeviceMessage( + deviceKeys, eventType, message) + as Map>>; + eventType = EventTypes.Encrypted; + await sendToDevice( + eventType, messageId ?? generateUniqueTransactionId(), data); + } finally { + _sendToDeviceEncryptedLock.unlock(deviceKeys); + } + } + + /// Sends an encrypted [message] of this [eventType] to these [deviceKeys]. + /// This request happens partly in the background and partly in the + /// foreground. It automatically chunks sending to device keys based on + /// activity. + Future sendToDeviceEncryptedChunked( + List deviceKeys, + String eventType, + Map message, + ) async { + if (!encryptionEnabled) return; + // be sure to copy our device keys list + deviceKeys = List.from(deviceKeys); + deviceKeys.removeWhere((DeviceKeys k) => + k.blocked || (k.userId == userID && k.deviceId == deviceID)); + if (deviceKeys.isEmpty) return; + message = message.copy(); // make sure we deep-copy the message + // make sure all the olm sessions are loaded from database + Logs().v('Sending to device chunked... (${deviceKeys.length} devices)'); + // sort so that devices we last received messages from get our message first + deviceKeys.sort((keyA, keyB) => keyB.lastActive.compareTo(keyA.lastActive)); + // and now send out in chunks of 20 + const chunkSize = 20; + + // first we send out all the chunks that we await + var i = 0; + // we leave this in a for-loop for now, so that we can easily adjust the break condition + // based on other things, if we want to hard-`await` more devices in the future + for (; i < deviceKeys.length && i <= 0; i += chunkSize) { + Logs().v('Sending chunk $i...'); + final chunk = deviceKeys.sublist( + i, + i + chunkSize > deviceKeys.length + ? deviceKeys.length + : i + chunkSize); + // and send + await sendToDeviceEncrypted(chunk, eventType, message); + } + // now send out the background chunks + if (i < deviceKeys.length) { + // ignore: unawaited_futures + () async { + for (; i < deviceKeys.length; i += chunkSize) { + // wait 50ms to not freeze the UI + await Future.delayed(Duration(milliseconds: 50)); + Logs().v('Sending chunk $i...'); + final chunk = deviceKeys.sublist( + i, + i + chunkSize > deviceKeys.length + ? deviceKeys.length + : i + chunkSize); + // and send + // ignore: unawaited_futures + sendToDeviceEncrypted(chunk, eventType, message); + } + }(); + } + } + + /// Whether all push notifications are muted using the [.m.rule.master] + /// rule of the push rules: https://matrix.org/docs/spec/client_server/r0.6.0#m-rule-master + bool get allPushNotificationsMuted { + final Map? globalPushRules = + accountData['m.push_rules']?.content['global']; + if (globalPushRules == null) return false; + + if (globalPushRules['override'] is List) { + for (final pushRule in globalPushRules['override']) { + if (pushRule['rule_id'] == '.m.rule.master') { + return pushRule['enabled']; + } + } + } + return false; + } + + Future setMuteAllPushNotifications(bool muted) async { + await setPushRuleEnabled( + 'global', + PushRuleKind.override, + '.m.rule.master', + muted, + ); + return; + } + + /// Changes the password. You should either set oldPasswort or another authentication flow. + @override + Future changePassword(String newPassword, + {String? oldPassword, + AuthenticationData? auth, + bool? logoutDevices}) async { + final userID = this.userID; + try { + if (oldPassword != null && userID != null) { + auth = AuthenticationPassword( + identifier: AuthenticationUserIdentifier(user: userID), + password: oldPassword, + ); + } + await super.changePassword(newPassword, + auth: auth, logoutDevices: logoutDevices); + } on MatrixException catch (matrixException) { + if (!matrixException.requireAdditionalAuthentication) { + rethrow; + } + if (matrixException.authenticationFlows?.length != 1 || + !(matrixException.authenticationFlows?.first.stages + .contains(AuthenticationTypes.password) ?? + false)) { + rethrow; + } + if (oldPassword == null || userID == null) { + rethrow; + } + return changePassword( + newPassword, + auth: AuthenticationPassword( + identifier: AuthenticationUserIdentifier(user: userID), + password: oldPassword, + session: matrixException.session, + ), + logoutDevices: logoutDevices, + ); + } catch (_) { + rethrow; + } + } + + @Deprecated('Use clearCache()') + Future clearLocalCachedMessages() async { + await clearCache(); + } + + /// Clear all local cached messages, room information and outbound group + /// sessions and perform a new clean sync. + Future clearCache() async { + await abortSync(); + prevBatch = null; + rooms.clear(); + await database?.clearCache(); + encryption?.keyManager.clearOutboundGroupSessions(); + onCacheCleared.add(true); + // Restart the syncloop + backgroundSync = true; + } + + /// A list of mxids of users who are ignored. + List get ignoredUsers => (accountData + .containsKey('m.ignored_user_list') && + accountData['m.ignored_user_list']?.content['ignored_users'] is Map) + ? List.from( + accountData['m.ignored_user_list']?.content['ignored_users'].keys) + : []; + + /// Ignore another user. This will clear the local cached messages to + /// hide all previous messages from this user. + Future ignoreUser(String userId) async { + if (!userId.isValidMatrixId) { + throw Exception('$userId is not a valid mxid!'); + } + await setAccountData(userID!, 'm.ignored_user_list', { + 'ignored_users': Map.fromEntries( + (ignoredUsers..add(userId)).map((key) => MapEntry(key, {}))), + }); + await clearCache(); + return; + } + + /// Unignore a user. This will clear the local cached messages and request + /// them again from the server to avoid gaps in the timeline. + Future unignoreUser(String userId) async { + if (!userId.isValidMatrixId) { + throw Exception('$userId is not a valid mxid!'); + } + if (!ignoredUsers.contains(userId)) { + throw Exception('$userId is not in the ignore list!'); + } + await setAccountData(userID!, 'm.ignored_user_list', { + 'ignored_users': Map.fromEntries( + (ignoredUsers..remove(userId)).map((key) => MapEntry(key, {}))), + }); + await clearCache(); + return; + } + + bool _disposed = false; + bool _aborted = false; + Future _currentTransaction = Future.sync(() => {}); + + /// Blackholes any ongoing sync call. Currently ongoing sync *processing* is + /// still going to be finished, new data is ignored. + Future abortSync() async { + _aborted = true; + backgroundSync = false; + _currentSyncId = -1; + try { + await _currentTransaction; + } catch (_) { + // No-OP + } + _currentSync = null; + // reset _aborted for being able to restart the sync. + _aborted = false; + } + + /// Stops the synchronization and closes the database. After this + /// you can safely make this Client instance null. + Future dispose({bool closeDatabase = true}) async { + _disposed = true; + await abortSync(); + encryption?.dispose(); + encryption = null; + try { + if (closeDatabase) { + await database + ?.close() + .catchError((e, s) => Logs().w('Failed to close database: ', e, s)); + _database = null; + } + } catch (error, stacktrace) { + Logs().w('Failed to close database: ', error, stacktrace); + } + return; + } + + Future _migrateFromLegacyDatabase() async { + Logs().i('Check legacy database for migration data...'); + final legacyDatabase = await legacyDatabaseBuilder?.call(this); + final migrateClient = await legacyDatabase?.getClient(clientName); + final database = this.database; + + if (migrateClient != null && legacyDatabase != null && database != null) { + Logs().i('Found data in the legacy database!'); + _id = migrateClient['client_id']; + await database.insertClient( + clientName, + migrateClient['homeserver_url'], + migrateClient['token'], + migrateClient['user_id'], + migrateClient['device_id'], + migrateClient['device_name'], + null, + migrateClient['olm_account'], + ); + Logs().d('Migrate SSSSCache...'); + for (final type in cacheTypes) { + final ssssCache = await legacyDatabase.getSSSSCache(type); + if (ssssCache != null) { + Logs().d('Migrate $type...'); + await database.storeSSSSCache( + type, + ssssCache.keyId ?? '', + ssssCache.ciphertext ?? '', + ssssCache.content ?? '', + ); + } + } + Logs().d('Migrate OLM sessions...'); + try { + final olmSessions = await legacyDatabase.getAllOlmSessions(); + for (final identityKey in olmSessions.keys) { + final sessions = olmSessions[identityKey]!; + for (final sessionId in sessions.keys) { + final session = sessions[sessionId]!; + await database.storeOlmSession( + identityKey, + session['session_id'] as String, + session['pickle'] as String, + session['last_received'] as int, + ); + } + } + } catch (e, s) { + Logs().e('Unable to migrate OLM sessions!', e, s); + } + Logs().d('Migrate Device Keys...'); + final userDeviceKeys = await legacyDatabase.getUserDeviceKeys(this); + for (final userId in userDeviceKeys.keys) { + Logs().d('Migrate Device Keys of user $userId...'); + final deviceKeysList = userDeviceKeys[userId]; + for (final crossSigningKey + in deviceKeysList?.crossSigningKeys.values ?? []) { + final pubKey = crossSigningKey.publicKey; + if (pubKey != null) { + Logs().d( + 'Migrate cross signing key with usage ${crossSigningKey.usage} and verified ${crossSigningKey.directVerified}...'); + await database.storeUserCrossSigningKey( + userId, + pubKey, + jsonEncode(crossSigningKey.toJson()), + crossSigningKey.directVerified, + crossSigningKey.blocked, + ); + } + } + + if (deviceKeysList != null) { + for (final deviceKeys in deviceKeysList.deviceKeys.values) { + final deviceId = deviceKeys.deviceId; + if (deviceId != null) { + Logs().d('Migrate device keys for ${deviceKeys.deviceId}...'); + await database.storeUserDeviceKey( + userId, + deviceId, + jsonEncode(deviceKeys.toJson()), + deviceKeys.directVerified, + deviceKeys.blocked, + deviceKeys.lastActive.millisecondsSinceEpoch, + ); + } + } + Logs().d('Migrate user device keys info...'); + await database.storeUserDeviceKeysInfo( + userId, deviceKeysList.outdated); + } + } + Logs().d('Migrate inbound group sessions...'); + try { + final sessions = await legacyDatabase.getAllInboundGroupSessions(); + for (var i = 0; i < sessions.length; i++) { + Logs().d('$i / ${sessions.length}'); + final session = sessions[i]; + await database.storeInboundGroupSession( + session.roomId, + session.sessionId, + session.pickle, + session.content, + session.indexes, + session.allowedAtIndex, + session.senderKey, + session.senderClaimedKeys, + ); + } + } catch (e, s) { + Logs().e('Unable to migrate inbound group sessions!', e, s); + } + + await legacyDatabase.clear(); + await legacyDatabaseDestroyer?.call(this); + } + await legacyDatabase?.close(); + _initLock = false; + if (migrateClient != null) { + return init(); + } + } +} + +class SdkError { + dynamic exception; + StackTrace? stackTrace; + + SdkError({this.exception, this.stackTrace}); +} + +class SyncStatusUpdate { + final SyncStatus status; + final SdkError? error; + final double? progress; + const SyncStatusUpdate(this.status, {this.error, this.progress}); +} + +enum SyncStatus { + waitingForResponse, + processing, + cleaningUp, + finished, + error, +} + +class BadServerVersionsException implements Exception { + final Set serverVersions, supportedVersions; + BadServerVersionsException(this.serverVersions, this.supportedVersions); + + @override + String toString() => + 'Server supports the versions: ${serverVersions.toString()} but this application is only compatible with ${supportedVersions.toString()}.'; +} + +class BadServerLoginTypesException implements Exception { + final Set serverLoginTypes, supportedLoginTypes; + BadServerLoginTypesException(this.serverLoginTypes, this.supportedLoginTypes); + + @override + String toString() => + 'Server supports the Login Types: ${serverLoginTypes.toString()} but this application is only compatible with ${supportedLoginTypes.toString()}.'; +} + +class HomeserverSummary { + final DiscoveryInformation? discoveryInformation; + final GetVersionsResponse versions; + final List loginFlows; + + HomeserverSummary({ + required this.discoveryInformation, + required this.versions, + required this.loginFlows, + }); +} diff --git a/lib/src/database/database_api.dart b/lib/src/database/database_api.dart new file mode 100644 index 0000000..2e3b754 --- /dev/null +++ b/lib/src/database/database_api.dart @@ -0,0 +1,309 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:typed_data'; + +import 'package:matrix/encryption/utils/olm_session.dart'; +import 'package:matrix/encryption/utils/outbound_group_session.dart'; +import 'package:matrix/encryption/utils/ssss_cache.dart'; +import 'package:matrix/encryption/utils/stored_inbound_group_session.dart'; +import 'package:matrix/src/utils/queued_to_device_event.dart'; + +import '../../matrix.dart'; + +abstract class DatabaseApi { + int get maxFileSize => 1 * 1024 * 1024; + bool get supportsFileStoring => false; + Future?> getClient(String name); + + Future updateClient( + String homeserverUrl, + String token, + String userId, + String? deviceId, + String? deviceName, + String? prevBatch, + String? olmAccount, + ); + + Future insertClient( + String name, + String homeserverUrl, + String token, + String userId, + String? deviceId, + String? deviceName, + String? prevBatch, + String? olmAccount, + ); + + Future> getRoomList(Client client); + + Future> getAccountData(); + + /// Stores a RoomUpdate object in the database. Must be called inside of + /// [transaction]. + Future storeRoomUpdate( + String roomId, SyncRoomUpdate roomUpdate, Client client); + + /// Stores an EventUpdate object in the database. Must be called inside of + /// [transaction]. + Future storeEventUpdate(EventUpdate eventUpdate, Client client); + + Future getEventById(String eventId, Room room); + + Future forgetRoom(String roomId); + + Future clearCache(); + + Future clear(); + + Future getUser(String userId, Room room); + + Future> getUsers(Room room); + + Future> getEventList( + Room room, { + int start = 0, + int limit, + }); + + Future getFile(Uri mxcUri); + + Future storeFile(Uri mxcUri, Uint8List bytes, int time); + + Future storeSyncFilterId( + String syncFilterId, + ); + + Future storeAccountData(String type, String content); + + Future> getUserDeviceKeys(Client client); + + Future getSSSSCache(String type); + + Future getOutboundGroupSession( + String roomId, + String userId, + ); + + Future> getAllInboundGroupSessions(); + + Future getInboundGroupSession( + String roomId, + String sessionId, + ); + + Future updateInboundGroupSessionIndexes( + String indexes, + String roomId, + String sessionId, + ); + + Future storeInboundGroupSession( + String roomId, + String sessionId, + String pickle, + String content, + String indexes, + String allowedAtIndex, + String senderKey, + String senderClaimedKey, + ); + + Future markInboundGroupSessionAsUploaded( + String roomId, + String sessionId, + ); + + Future updateInboundGroupSessionAllowedAtIndex( + String allowedAtIndex, + String roomId, + String sessionId, + ); + + Future removeOutboundGroupSession(String roomId); + + Future storeOutboundGroupSession( + String roomId, + String pickle, + String deviceIds, + int creationTime, + ); + + Future updateClientKeys( + String olmAccount, + ); + + Future storeOlmSession( + String identitiyKey, + String sessionId, + String pickle, + int lastReceived, + ); + + Future setLastActiveUserDeviceKey( + int lastActive, + String userId, + String deviceId, + ); + + Future setLastSentMessageUserDeviceKey( + String lastSentMessage, + String userId, + String deviceId, + ); + + Future clearSSSSCache(); + + Future storeSSSSCache( + String type, + String keyId, + String ciphertext, + String content, + ); + + Future markInboundGroupSessionsAsNeedingUpload(); + + Future storePrevBatch( + String prevBatch, + ); + + Future deleteOldFiles(int savedAt); + + Future storeUserDeviceKeysInfo( + String userId, + bool outdated, + ); + + Future storeUserDeviceKey( + String userId, + String deviceId, + String content, + bool verified, + bool blocked, + int lastActive, + ); + + Future removeUserDeviceKey( + String userId, + String deviceId, + ); + + Future removeUserCrossSigningKey( + String userId, + String publicKey, + ); + + Future storeUserCrossSigningKey( + String userId, + String publicKey, + String content, + bool verified, + bool blocked, + ); + + Future deleteFromToDeviceQueue(int id); + + Future removeEvent(String eventId, String roomId); + + Future updateRoomSortOrder( + double oldestSortOrder, + double newestSortOrder, + String roomId, + ); + + Future setRoomPrevBatch( + String prevBatch, + String roomId, + Client client, + ); + + Future resetNotificationCount(String roomId); + + Future setVerifiedUserCrossSigningKey( + bool verified, + String userId, + String publicKey, + ); + + Future setBlockedUserCrossSigningKey( + bool blocked, + String userId, + String publicKey, + ); + + Future setVerifiedUserDeviceKey( + bool verified, + String userId, + String deviceId, + ); + + Future setBlockedUserDeviceKey( + bool blocked, + String userId, + String deviceId, + ); + + Future> getUnimportantRoomEventStatesForRoom( + List events, + Room room, + ); + + Future> getOlmSessions( + String identityKey, + String userId, + ); + + Future> getAllOlmSessions(); + + Future> getOlmSessionsForDevices( + List identityKeys, + String userId, + ); + + Future> getToDeviceEventQueue(); + + /// Please do `jsonEncode(content)` in your code to stay compatible with + /// auto generated methods here. + Future insertIntoToDeviceQueue( + String type, + String txnId, + String content, + ); + + Future> getLastSentMessageUserDeviceKey( + String userId, + String deviceId, + ); + + Future> getInboundGroupSessionsToUpload(); + + Future addSeenDeviceId( + String userId, String deviceId, String publicKeys); + + Future addSeenPublicKey(String publicKey, String deviceId); + + Future deviceIdSeen(userId, deviceId); + + Future publicKeySeen(String publicKey); + + Future close(); + + Future transaction(Future Function() action); +} diff --git a/lib/src/database/fluffybox_database.dart b/lib/src/database/fluffybox_database.dart new file mode 100644 index 0000000..720cf1a --- /dev/null +++ b/lib/src/database/fluffybox_database.dart @@ -0,0 +1,1478 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:async'; +import 'dart:convert'; +import 'dart:math'; +import 'dart:typed_data'; + +import 'package:fluffybox/fluffybox.dart'; +import 'package:fluffybox/hive.dart' show HiveCipher; +import 'package:matrix/encryption/utils/olm_session.dart'; +import 'package:matrix/encryption/utils/outbound_group_session.dart'; +import 'package:matrix/encryption/utils/ssss_cache.dart'; +import 'package:matrix/encryption/utils/stored_inbound_group_session.dart'; +import 'package:matrix/matrix.dart'; +import 'package:matrix/src/utils/queued_to_device_event.dart'; +import 'package:matrix/src/utils/run_benchmarked.dart'; + +/// This database does not support file caching! +class FluffyBoxDatabase extends DatabaseApi { + static const int version = 6; + final String name; + final String path; + final HiveCipher? key; + late final BoxCollection _collection; + late Box _clientBox; + late Box _accountDataBox; + late Box _roomsBox; + late Box _toDeviceQueueBox; + + /// Key is a tuple as TupleKey(roomId, type) where stateKey can be + /// an empty string. + late Box _roomStateBox; + + /// Key is a tuple as TupleKey(roomId, userId) + late Box _roomMembersBox; + + /// Key is a tuple as TupleKey(roomId, type) + late Box _roomAccountDataBox; + late Box _inboundGroupSessionsBox; + late Box _outboundGroupSessionsBox; + late Box _olmSessionsBox; + + /// Key is a tuple as TupleKey(userId, deviceId) + late Box _userDeviceKeysBox; + + /// Key is the user ID as a String + late Box _userDeviceKeysOutdatedBox; + + /// Key is a tuple as TupleKey(userId, publicKey) + late Box _userCrossSigningKeysBox; + late Box _ssssCacheBox; + late Box _presencesBox; + + /// Key is a tuple as Multikey(roomId, fragmentId) while the default + /// fragmentId is an empty String + late Box _timelineFragmentsBox; + + /// Key is a tuple as TupleKey(roomId, eventId) + late Box _eventsBox; + + /// Key is a tuple as TupleKey(userId, deviceId) + late Box _seenDeviceIdsBox; + + late Box _seenDeviceKeysBox; + + String get _clientBoxName => 'box_client'; + + String get _accountDataBoxName => 'box_account_data'; + + String get _roomsBoxName => 'box_rooms'; + + String get _toDeviceQueueBoxName => 'box_to_device_queue'; + + String get _roomStateBoxName => 'box_room_states'; + + String get _roomMembersBoxName => 'box_room_members'; + + String get _roomAccountDataBoxName => 'box_room_account_data'; + + String get _inboundGroupSessionsBoxName => 'box_inbound_group_session'; + + String get _outboundGroupSessionsBoxName => 'box_outbound_group_session'; + + String get _olmSessionsBoxName => 'box_olm_session'; + + String get _userDeviceKeysBoxName => 'box_user_device_keys'; + + String get _userDeviceKeysOutdatedBoxName => 'box_user_device_keys_outdated'; + + String get _userCrossSigningKeysBoxName => 'box_cross_signing_keys'; + + String get _ssssCacheBoxName => 'box_ssss_cache'; + + String get _presencesBoxName => 'box_presences'; + + String get _timelineFragmentsBoxName => 'box_timeline_fragments'; + + String get _eventsBoxName => 'box_events'; + + String get _seenDeviceIdsBoxName => 'box_seen_device_ids'; + + String get _seenDeviceKeysBoxName => 'box_seen_device_keys'; + + FluffyBoxDatabase(this.name, this.path, {this.key}); + + @override + int get maxFileSize => 0; + + Future open() async { + _collection = await BoxCollection.open( + name, + { + _clientBoxName, + _accountDataBoxName, + _roomsBoxName, + _toDeviceQueueBoxName, + _roomStateBoxName, + _roomMembersBoxName, + _roomAccountDataBoxName, + _inboundGroupSessionsBoxName, + _outboundGroupSessionsBoxName, + _olmSessionsBoxName, + _userDeviceKeysBoxName, + _userDeviceKeysOutdatedBoxName, + _userCrossSigningKeysBoxName, + _ssssCacheBoxName, + _presencesBoxName, + _timelineFragmentsBoxName, + _eventsBoxName, + _seenDeviceIdsBoxName, + _seenDeviceKeysBoxName, + }, + key: key, + path: path, + ); + _clientBox = await _collection.openBox( + _clientBoxName, + preload: true, + ); + _accountDataBox = await _collection.openBox( + _accountDataBoxName, + preload: true, + ); + _roomsBox = await _collection.openBox( + _roomsBoxName, + preload: true, + ); + _roomStateBox = await _collection.openBox( + _roomStateBoxName, + ); + _roomMembersBox = await _collection.openBox( + _roomMembersBoxName, + ); + _toDeviceQueueBox = await _collection.openBox( + _toDeviceQueueBoxName, + preload: true, + ); + _roomAccountDataBox = await _collection.openBox( + _roomAccountDataBoxName, + preload: true, + ); + _inboundGroupSessionsBox = await _collection.openBox( + _inboundGroupSessionsBoxName, + ); + _outboundGroupSessionsBox = await _collection.openBox( + _outboundGroupSessionsBoxName, + ); + _olmSessionsBox = await _collection.openBox( + _olmSessionsBoxName, + ); + _userDeviceKeysBox = await _collection.openBox( + _userDeviceKeysBoxName, + ); + _userDeviceKeysOutdatedBox = await _collection.openBox( + _userDeviceKeysOutdatedBoxName, + ); + _userCrossSigningKeysBox = await _collection.openBox( + _userCrossSigningKeysBoxName, + ); + _ssssCacheBox = await _collection.openBox( + _ssssCacheBoxName, + ); + _presencesBox = await _collection.openBox( + _presencesBoxName, + ); + _timelineFragmentsBox = await _collection.openBox( + _timelineFragmentsBoxName, + ); + _eventsBox = await _collection.openBox( + _eventsBoxName, + ); + _seenDeviceIdsBox = await _collection.openBox( + _seenDeviceIdsBoxName, + ); + _seenDeviceKeysBox = await _collection.openBox( + _seenDeviceKeysBoxName, + ); + + // Check version and check if we need a migration + final currentVersion = int.tryParse(await _clientBox.get('version') ?? ''); + if (currentVersion == null) { + await _clientBox.put('version', version.toString()); + } else if (currentVersion != version) { + await _migrateFromVersion(currentVersion); + } + + return; + } + + Future _migrateFromVersion(int currentVersion) async { + Logs().i('Migrate store database from version $currentVersion to $version'); + await clearCache(); + await _clientBox.put('version', version.toString()); + } + + @override + Future clear() => _collection.deleteFromDisk(); + + @override + Future clearCache() => transaction(() async { + await _roomsBox.clear(); + await _accountDataBox.clear(); + await _roomStateBox.clear(); + await _roomMembersBox.clear(); + await _eventsBox.clear(); + await _timelineFragmentsBox.clear(); + await _outboundGroupSessionsBox.clear(); + await _presencesBox.clear(); + await _clientBox.delete('prev_batch'); + }); + + @override + Future clearSSSSCache() => _ssssCacheBox.clear(); + + @override + Future close() async => _collection.close(); + + @override + Future deleteFromToDeviceQueue(int id) async { + await _toDeviceQueueBox.delete(id.toString()); + return; + } + + @override + Future deleteOldFiles(int savedAt) async { + return; + } + + @override + Future forgetRoom(String roomId) => transaction(() async { + await _timelineFragmentsBox.delete(TupleKey(roomId, '').toString()); + final eventsBoxKeys = await _eventsBox.getAllKeys(); + for (final key in eventsBoxKeys) { + final multiKey = TupleKey.fromString(key); + if (multiKey.parts.first != roomId) continue; + await _eventsBox.delete(key); + } + final roomStateBoxKeys = await _roomStateBox.getAllKeys(); + for (final key in roomStateBoxKeys) { + final multiKey = TupleKey.fromString(key); + if (multiKey.parts.first != roomId) continue; + await _roomStateBox.delete(key); + } + final roomMembersBoxKeys = await _roomMembersBox.getAllKeys(); + for (final key in roomMembersBoxKeys) { + final multiKey = TupleKey.fromString(key); + if (multiKey.parts.first != roomId) continue; + await _roomMembersBox.delete(key); + } + final roomAccountDataBoxKeys = await _roomAccountDataBox.getAllKeys(); + for (final key in roomAccountDataBoxKeys) { + final multiKey = TupleKey.fromString(key); + if (multiKey.parts.first != roomId) continue; + await _roomAccountDataBox.delete(key); + } + await _roomsBox.delete(roomId); + }); + + @override + Future> getAccountData() => + runBenchmarked>('Get all account data from store', + () async { + final accountData = {}; + final raws = await _accountDataBox.getAllValues(); + for (final entry in raws.entries) { + accountData[entry.key] = BasicEvent( + type: entry.key, + content: copyMap(entry.value), + ); + } + return accountData; + }); + + @override + Future?> getClient(String name) => + runBenchmarked('Get Client from store', () async { + final map = {}; + final keys = await _clientBox.getAllKeys(); + for (final key in keys) { + if (key == 'version') continue; + final value = await _clientBox.get(key); + if (value != null) map[key] = value; + } + if (map.isEmpty) return null; + return map; + }); + + @override + Future getEventById(String eventId, Room room) async { + final raw = await _eventsBox.get(TupleKey(room.id, eventId).toString()); + if (raw == null) return null; + return Event.fromJson(copyMap(raw), room); + } + + /// Loads a whole list of events at once from the store for a specific room + Future> _getEventsByIds(List eventIds, Room room) async { + final keys = eventIds + .map( + (eventId) => TupleKey(room.id, eventId).toString(), + ) + .toList(); + final rawEvents = await _eventsBox.getAll(keys); + return rawEvents + .map((rawEvent) => Event.fromJson(copyMap(rawEvent!), room)) + .toList(); + } + + @override + Future> getEventList( + Room room, { + int start = 0, + int? limit, + }) => + runBenchmarked>('Get event list', () async { + // Get the synced event IDs from the store + final timelineKey = TupleKey(room.id, '').toString(); + final timelineEventIds = + (await _timelineFragmentsBox.get(timelineKey) ?? []); + + // Get the local stored SENDING events from the store + late final List sendingEventIds; + if (start != 0) { + sendingEventIds = []; + } else { + final sendingTimelineKey = TupleKey(room.id, 'SENDING').toString(); + sendingEventIds = + (await _timelineFragmentsBox.get(sendingTimelineKey) ?? []); + } + + // Combine those two lists while respecting the start and limit parameters. + final end = min(timelineEventIds.length, + start + (limit ?? timelineEventIds.length)); + final eventIds = sendingEventIds + + (start < timelineEventIds.length + ? timelineEventIds.getRange(start, end).toList() + : []); + + return await _getEventsByIds(eventIds.cast(), room); + }); + + @override + Future getFile(Uri mxcUri) async { + return null; + } + + @override + Future getInboundGroupSession( + String roomId, + String sessionId, + ) async { + final raw = await _inboundGroupSessionsBox.get(sessionId); + if (raw == null) return null; + return StoredInboundGroupSession.fromJson(copyMap(raw)); + } + + @override + Future> + getInboundGroupSessionsToUpload() async { + final sessions = (await _inboundGroupSessionsBox.getAllValues()) + .values + .where((rawSession) => rawSession['uploaded'] == false) + .take(50) + .map( + (json) => StoredInboundGroupSession.fromJson( + copyMap(json), + ), + ) + .toList(); + return sessions; + } + + @override + Future> getLastSentMessageUserDeviceKey( + String userId, String deviceId) async { + final raw = + await _userDeviceKeysBox.get(TupleKey(userId, deviceId).toString()); + if (raw == null) return []; + return [raw['last_sent_message']]; + } + + @override + Future storeOlmSession(String identityKey, String sessionId, + String pickle, int lastReceived) async { + final rawSessions = (await _olmSessionsBox.get(identityKey)) ?? {}; + rawSessions[sessionId] = { + 'identity_key': identityKey, + 'pickle': pickle, + 'session_id': sessionId, + 'last_received': lastReceived, + }; + await _olmSessionsBox.put(identityKey, rawSessions); + return; + } + + @override + Future> getOlmSessions( + String identityKey, String userId) async { + final rawSessions = await _olmSessionsBox.get(identityKey); + if (rawSessions == null || rawSessions.isEmpty) return []; + return rawSessions.values + .map((json) => OlmSession.fromJson(copyMap(json), userId)) + .toList(); + } + + @override + Future> getAllOlmSessions() => + _olmSessionsBox.getAllValues(); + + @override + Future> getOlmSessionsForDevices( + List identityKey, String userId) async { + final sessions = await Future.wait( + identityKey.map((identityKey) => getOlmSessions(identityKey, userId))); + return [for (final sublist in sessions) ...sublist]; + } + + @override + Future getOutboundGroupSession( + String roomId, String userId) async { + final raw = await _outboundGroupSessionsBox.get(roomId); + if (raw == null) return null; + return OutboundGroupSession.fromJson(copyMap(raw), userId); + } + + @override + Future> getRoomList(Client client) => + runBenchmarked>('Get room list from store', () async { + final rooms = {}; + final userID = client.userID; + + final rawRooms = await _roomsBox.getAllValues(); + + final getRoomStateRequests = >{}; + final getRoomMembersRequests = >{}; + for (final raw in rawRooms.values) { + // Get the room + final room = Room.fromJson(copyMap(raw), client); + + final membersToPostload = {if (userID != null) userID}; + + // If the room is a direct chat, those IDs should be there too + if (room.isDirectChat) { + membersToPostload + .add(TupleKey(room.id, room.directChatMatrixID!).toString()); + } + // the lastEvent message preview might have an author we need to fetch, if it is a group chat + final lastEvent = room.getState(EventTypes.Message); + if (lastEvent != null && !room.isDirectChat) { + membersToPostload + .add(TupleKey(room.id, lastEvent.senderId).toString()); + } + // if the room has no name and no canonical alias, its name is calculated + // based on the heroes of the room + if (room.getState(EventTypes.RoomName) == null && + room.getState(EventTypes.RoomCanonicalAlias) == null) { + // we don't have a name and no canonical alias, so we'll need to + // post-load the heroes + final heroes = room.summary.mHeroes; + if (heroes != null) { + heroes.forEach((hero) => membersToPostload.add(hero)); + } + } + + // Get the "important" room states. All other states will be loaded once + // `getUnimportantRoomStates()` is called. + final dbKeys = client.importantStateEvents + .map((state) => TupleKey(room.id, state).toString()) + .toList(); + getRoomStateRequests[room.id] = _roomStateBox.getAll( + dbKeys, + ); + + // Load members + final membersDbKeys = membersToPostload + .map((member) => TupleKey(room.id, member).toString()) + .toList(); + getRoomMembersRequests[room.id] = _roomMembersBox.getAll( + membersDbKeys, + ); + + // Add to the list and continue. + rooms[room.id] = room; + } + + for (final room in rooms.values) { + // Add states to the room + final statesList = await getRoomStateRequests[room.id]; + if (statesList != null) { + for (final states in statesList) { + if (states == null) continue; + final stateEvents = states.values + .map((raw) => Event.fromJson(copyMap(raw), room)) + .toList(); + for (final state in stateEvents) { + room.setState(state); + } + } + } + + // Add members to the room + final members = await getRoomMembersRequests[room.id]; + if (members != null) { + for (final member in members) { + if (member == null) continue; + room.setState(Event.fromJson(copyMap(member), room)); + } + } + } + + // Get the room account data + final roomAccountDataRaws = await _roomAccountDataBox.getAllValues(); + for (final entry in roomAccountDataRaws.entries) { + final keys = TupleKey.fromString(entry.key); + final basicRoomEvent = BasicRoomEvent.fromJson( + copyMap(entry.value), + ); + final roomId = keys.parts.first; + if (rooms.containsKey(roomId)) { + rooms[roomId]!.roomAccountData[basicRoomEvent.type] = + basicRoomEvent; + } else { + Logs().w( + 'Found account data for unknown room $roomId. Delete now...'); + await _roomAccountDataBox + .delete(TupleKey(roomId, basicRoomEvent.type).toString()); + } + } + + return rooms.values.toList(); + }); + + @override + Future getSSSSCache(String type) async { + final raw = await _ssssCacheBox.get(type); + if (raw == null) return null; + return SSSSCache.fromJson(copyMap(raw)); + } + + @override + Future> getToDeviceEventQueue() async { + final raws = await _toDeviceQueueBox.getAllValues(); + final copiedRaws = raws.entries.map((entry) { + final copiedRaw = copyMap(entry.value); + copiedRaw['id'] = int.parse(entry.key); + copiedRaw['content'] = jsonDecode(copiedRaw['content']); + return copiedRaw; + }).toList(); + return copiedRaws.map((raw) => QueuedToDeviceEvent.fromJson(raw)).toList(); + } + + @override + Future> getUnimportantRoomEventStatesForRoom( + List events, Room room) async { + final keys = (await _roomStateBox.getAllKeys()).where((key) { + final tuple = TupleKey.fromString(key); + return tuple.parts.first == room.id && !events.contains(tuple.parts[1]); + }); + + final unimportantEvents = []; + for (final key in keys) { + final states = await _roomStateBox.get(key); + if (states == null) continue; + unimportantEvents.addAll( + states.values.map((raw) => Event.fromJson(copyMap(raw), room))); + } + return unimportantEvents; + } + + @override + Future getUser(String userId, Room room) async { + final state = + await _roomMembersBox.get(TupleKey(room.id, userId).toString()); + if (state == null) return null; + return Event.fromJson(copyMap(state), room).asUser; + } + + @override + Future> getUserDeviceKeys(Client client) => + runBenchmarked>( + 'Get all user device keys from store', () async { + final deviceKeysOutdated = + await _userDeviceKeysOutdatedBox.getAllKeys(); + if (deviceKeysOutdated.isEmpty) { + return {}; + } + final res = {}; + final userDeviceKeysBoxKeys = await _userDeviceKeysBox.getAllKeys(); + final userCrossSigningKeysBoxKeys = + await _userCrossSigningKeysBox.getAllKeys(); + for (final userId in deviceKeysOutdated) { + final deviceKeysBoxKeys = userDeviceKeysBoxKeys.where((tuple) { + final tupleKey = TupleKey.fromString(tuple); + return tupleKey.parts.first == userId; + }); + final crossSigningKeysBoxKeys = + userCrossSigningKeysBoxKeys.where((tuple) { + final tupleKey = TupleKey.fromString(tuple); + return tupleKey.parts.first == userId; + }); + final childEntries = await Future.wait( + deviceKeysBoxKeys.map( + (key) async { + final userDeviceKey = await _userDeviceKeysBox.get(key); + if (userDeviceKey == null) return null; + return copyMap(userDeviceKey); + }, + ), + ); + final crossSigningEntries = await Future.wait( + crossSigningKeysBoxKeys.map( + (key) async { + final crossSigningKey = await _userCrossSigningKeysBox.get(key); + if (crossSigningKey == null) return null; + return copyMap(crossSigningKey); + }, + ), + ); + res[userId] = DeviceKeysList.fromDbJson( + { + 'client_id': client.id, + 'user_id': userId, + 'outdated': await _userDeviceKeysOutdatedBox.get(userId), + }, + childEntries + .where((c) => c != null) + .toList() + .cast>(), + crossSigningEntries + .where((c) => c != null) + .toList() + .cast>(), + client); + } + return res; + }); + + @override + Future> getUsers(Room room) async { + final users = []; + final keys = (await _roomMembersBox.getAllKeys()) + .where((key) => TupleKey.fromString(key).parts.first == room.id) + .toList(); + final states = await _roomMembersBox.getAll(keys); + states.removeWhere((state) => state == null); + states.forEach( + (state) => users.add(Event.fromJson(copyMap(state!), room).asUser), + ); + + return users; + } + + @override + Future insertClient( + String name, + String homeserverUrl, + String token, + String userId, + String? deviceId, + String? deviceName, + String? prevBatch, + String? olmAccount) async { + await transaction(() async { + await _clientBox.put('homeserver_url', homeserverUrl); + await _clientBox.put('token', token); + await _clientBox.put('user_id', userId); + if (deviceId == null) { + await _clientBox.delete('device_id'); + } else { + await _clientBox.put('device_id', deviceId); + } + if (deviceName == null) { + await _clientBox.delete('device_name'); + } else { + await _clientBox.put('device_name', deviceName); + } + if (prevBatch == null) { + await _clientBox.delete('prev_batch'); + } else { + await _clientBox.put('prev_batch', prevBatch); + } + if (olmAccount == null) { + await _clientBox.delete('olm_account'); + } else { + await _clientBox.put('olm_account', olmAccount); + } + await _clientBox.delete('sync_filter_id'); + }); + return 0; + } + + @override + Future insertIntoToDeviceQueue( + String type, String txnId, String content) async { + final id = DateTime.now().millisecondsSinceEpoch; + await _toDeviceQueueBox.put(id.toString(), { + 'type': type, + 'txn_id': txnId, + 'content': content, + }); + return id; + } + + @override + Future markInboundGroupSessionAsUploaded( + String roomId, String sessionId) async { + final raw = await _inboundGroupSessionsBox.get(sessionId); + if (raw == null) { + Logs().w( + 'Tried to mark inbound group session as uploaded which was not found in the database!'); + return; + } + raw['uploaded'] = true; + await _inboundGroupSessionsBox.put(sessionId, raw); + return; + } + + @override + Future markInboundGroupSessionsAsNeedingUpload() async { + final keys = await _inboundGroupSessionsBox.getAllKeys(); + for (final sessionId in keys) { + final raw = await _inboundGroupSessionsBox.get(sessionId); + if (raw == null) continue; + raw['uploaded'] = false; + await _inboundGroupSessionsBox.put(sessionId, raw); + } + return; + } + + @override + Future removeEvent(String eventId, String roomId) async { + await _eventsBox.delete(TupleKey(roomId, eventId).toString()); + final keys = await _timelineFragmentsBox.getAllKeys(); + for (final key in keys) { + final multiKey = TupleKey.fromString(key); + if (multiKey.parts.first != roomId) continue; + final eventIds = await _timelineFragmentsBox.get(key) ?? []; + final prevLength = eventIds.length; + eventIds.removeWhere((id) => id == eventId); + if (eventIds.length < prevLength) { + await _timelineFragmentsBox.put(key, eventIds); + } + } + return; + } + + @override + Future removeOutboundGroupSession(String roomId) async { + await _outboundGroupSessionsBox.delete(roomId); + return; + } + + @override + Future removeUserCrossSigningKey( + String userId, String publicKey) async { + await _userCrossSigningKeysBox + .delete(TupleKey(userId, publicKey).toString()); + return; + } + + @override + Future removeUserDeviceKey(String userId, String deviceId) async { + await _userDeviceKeysBox.delete(TupleKey(userId, deviceId).toString()); + return; + } + + @override + Future resetNotificationCount(String roomId) async { + final raw = await _roomsBox.get(roomId); + if (raw == null) return; + raw['notification_count'] = raw['highlight_count'] = 0; + await _roomsBox.put(roomId, raw); + return; + } + + @override + Future setBlockedUserCrossSigningKey( + bool blocked, String userId, String publicKey) async { + final raw = await _userCrossSigningKeysBox + .get(TupleKey(userId, publicKey).toString()); + raw!['blocked'] = blocked; + await _userCrossSigningKeysBox.put( + TupleKey(userId, publicKey).toString(), + raw, + ); + return; + } + + @override + Future setBlockedUserDeviceKey( + bool blocked, String userId, String deviceId) async { + final raw = + await _userDeviceKeysBox.get(TupleKey(userId, deviceId).toString()); + raw!['blocked'] = blocked; + await _userDeviceKeysBox.put( + TupleKey(userId, deviceId).toString(), + raw, + ); + return; + } + + @override + Future setLastActiveUserDeviceKey( + int lastActive, String userId, String deviceId) async { + final raw = + await _userDeviceKeysBox.get(TupleKey(userId, deviceId).toString()); + raw!['last_active'] = lastActive; + await _userDeviceKeysBox.put( + TupleKey(userId, deviceId).toString(), + raw, + ); + } + + @override + Future setLastSentMessageUserDeviceKey( + String lastSentMessage, String userId, String deviceId) async { + final raw = + await _userDeviceKeysBox.get(TupleKey(userId, deviceId).toString()); + raw!['last_sent_message'] = lastSentMessage; + await _userDeviceKeysBox.put( + TupleKey(userId, deviceId).toString(), + raw, + ); + } + + @override + Future setRoomPrevBatch( + String prevBatch, String roomId, Client client) async { + final raw = await _roomsBox.get(roomId); + if (raw == null) return; + final room = Room.fromJson(copyMap(raw), client); + room.prev_batch = prevBatch; + await _roomsBox.put(roomId, room.toJson()); + return; + } + + @override + Future setVerifiedUserCrossSigningKey( + bool verified, String userId, String publicKey) async { + final raw = (await _userCrossSigningKeysBox + .get(TupleKey(userId, publicKey).toString())) ?? + {}; + raw['verified'] = verified; + await _userCrossSigningKeysBox.put( + TupleKey(userId, publicKey).toString(), + raw, + ); + return; + } + + @override + Future setVerifiedUserDeviceKey( + bool verified, String userId, String deviceId) async { + final raw = + await _userDeviceKeysBox.get(TupleKey(userId, deviceId).toString()); + raw!['verified'] = verified; + await _userDeviceKeysBox.put( + TupleKey(userId, deviceId).toString(), + raw, + ); + return; + } + + @override + Future storeAccountData(String type, String content) async { + await _accountDataBox.put(type, copyMap(jsonDecode(content))); + return; + } + + @override + Future storeEventUpdate(EventUpdate eventUpdate, Client client) async { + // Ephemerals should not be stored + if (eventUpdate.type == EventUpdateType.ephemeral) return; + final tmpRoom = Room(id: eventUpdate.roomID, client: client); + + // In case of this is a redaction event + if (eventUpdate.content['type'] == EventTypes.Redaction) { + final event = await getEventById(eventUpdate.content['redacts'], tmpRoom); + if (event != null) { + event.setRedactionEvent(Event.fromJson(eventUpdate.content, tmpRoom)); + await _eventsBox.put( + TupleKey(eventUpdate.roomID, event.eventId).toString(), + event.toJson()); + } + } + + // Store a common message event + if ({EventUpdateType.timeline, EventUpdateType.history} + .contains(eventUpdate.type)) { + final eventId = eventUpdate.content['event_id']; + // Is this ID already in the store? + final prevEvent = await _eventsBox + .get(TupleKey(eventUpdate.roomID, eventId).toString()); + final prevStatus = prevEvent == null + ? null + : () { + final json = copyMap(prevEvent); + final statusInt = json.tryGet('status') ?? + json + .tryGetMap('unsigned') + ?.tryGet(messageSendingStatusKey); + return statusInt == null ? null : eventStatusFromInt(statusInt); + }(); + + // calculate the status + final newStatus = eventStatusFromInt( + eventUpdate.content.tryGet('status') ?? + eventUpdate.content + .tryGetMap('unsigned') + ?.tryGet(messageSendingStatusKey) ?? + EventStatus.synced.intValue, + ); + + // Is this the response to a sending event which is already synced? Then + // there is nothing to do here. + if (!newStatus.isSynced && prevStatus != null && prevStatus.isSynced) { + return; + } + + final status = newStatus.isError || prevStatus == null + ? newStatus + : latestEventStatus( + prevStatus, + newStatus, + ); + + // Add the status and the sort order to the content so it get stored + eventUpdate.content['unsigned'] ??= {}; + eventUpdate.content['unsigned'][messageSendingStatusKey] = + eventUpdate.content['status'] = status.intValue; + + // In case this event has sent from this account we have a transaction ID + final transactionId = eventUpdate.content + .tryGetMap('unsigned') + ?.tryGet('transaction_id'); + await _eventsBox.put(TupleKey(eventUpdate.roomID, eventId).toString(), + eventUpdate.content); + + // Update timeline fragments + final key = TupleKey(eventUpdate.roomID, status.isSent ? '' : 'SENDING') + .toString(); + + final eventIds = + List.from(await _timelineFragmentsBox.get(key) ?? []); + + if (!eventIds.contains(eventId)) { + if (eventUpdate.type == EventUpdateType.history) { + eventIds.add(eventId); + } else { + eventIds.insert(0, eventId); + } + await _timelineFragmentsBox.put(key, eventIds); + } else if (status.isSynced && + prevStatus != null && + prevStatus.isSent && + eventUpdate.type != EventUpdateType.history) { + // Status changes from 1 -> 2? Make sure event is correctly sorted. + eventIds.remove(eventId); + eventIds.insert(0, eventId); + } + + // If event comes from server timeline, remove sending events with this ID + if (status.isSent) { + final key = TupleKey(eventUpdate.roomID, 'SENDING').toString(); + final eventIds = + List.from(await _timelineFragmentsBox.get(key) ?? []); + final i = eventIds.indexWhere((id) => id == eventId); + if (i != -1) { + await _timelineFragmentsBox.put(key, eventIds..removeAt(i)); + } + } + + // Is there a transaction id? Then delete the event with this id. + if (!status.isError && !status.isSending && transactionId != null) { + await removeEvent(transactionId, eventUpdate.roomID); + } + } + + // Store a common state event + if ({ + EventUpdateType.timeline, + EventUpdateType.state, + EventUpdateType.inviteState + }.contains(eventUpdate.type)) { + if (eventUpdate.content['type'] == EventTypes.RoomMember) { + await _roomMembersBox.put( + TupleKey( + eventUpdate.roomID, + eventUpdate.content['state_key'], + ).toString(), + eventUpdate.content); + } else { + final key = TupleKey( + eventUpdate.roomID, + eventUpdate.content['type'], + ).toString(); + final stateMap = copyMap(await _roomStateBox.get(key) ?? {}); + // store state events and new messages, that either are not an edit or an edit of the lastest message + // An edit is an event, that has an edit relation to the latest event. In some cases for the second edit, we need to compare if both have an edit relation to the same event instead. + if (eventUpdate.content + .tryGetMap('content') + ?.tryGetMap('m.relates_to') == + null) { + stateMap[eventUpdate.content['state_key'] ?? ''] = + eventUpdate.content; + await _roomStateBox.put(key, stateMap); + } else { + final editedEventRelationshipEventId = eventUpdate.content + .tryGetMap('content') + ?.tryGetMap('m.relates_to') + ?.tryGet('event_id'); + final state = stateMap[''] == null + ? null + : Event.fromJson(stateMap[''] as Map, tmpRoom); + if (eventUpdate.content['type'] != EventTypes.Message || + eventUpdate.content + .tryGetMap('content') + ?.tryGetMap('m.relates_to') + ?.tryGet('rel_type') != + RelationshipTypes.edit || + editedEventRelationshipEventId == state?.eventId || + ((state?.relationshipType == RelationshipTypes.edit && + editedEventRelationshipEventId == + state?.relationshipEventId))) { + stateMap[eventUpdate.content['state_key'] ?? ''] = + eventUpdate.content; + await _roomStateBox.put(key, stateMap); + } + } + } + } + + // Store a room account data event + if (eventUpdate.type == EventUpdateType.accountData) { + await _roomAccountDataBox.put( + TupleKey( + eventUpdate.roomID, + eventUpdate.content['type'], + ).toString(), + eventUpdate.content, + ); + } + } + + @override + Future storeFile(Uri mxcUri, Uint8List bytes, int time) async { + return; + } + + @override + Future storeInboundGroupSession( + String roomId, + String sessionId, + String pickle, + String content, + String indexes, + String allowedAtIndex, + String senderKey, + String senderClaimedKey) async { + await _inboundGroupSessionsBox.put( + sessionId, + StoredInboundGroupSession( + roomId: roomId, + sessionId: sessionId, + pickle: pickle, + content: content, + indexes: indexes, + allowedAtIndex: allowedAtIndex, + senderKey: senderKey, + senderClaimedKeys: senderClaimedKey, + uploaded: false, + ).toJson()); + return; + } + + @override + Future storeOutboundGroupSession( + String roomId, String pickle, String deviceIds, int creationTime) async { + await _outboundGroupSessionsBox.put(roomId, { + 'room_id': roomId, + 'pickle': pickle, + 'device_ids': deviceIds, + 'creation_time': creationTime, + }); + return; + } + + @override + Future storePrevBatch( + String prevBatch, + ) async { + if ((await _clientBox.getAllKeys()).isEmpty) return; + await _clientBox.put('prev_batch', prevBatch); + return; + } + + @override + Future storeRoomUpdate( + String roomId, SyncRoomUpdate roomUpdate, Client client) async { + // Leave room if membership is leave + if (roomUpdate is LeftRoomUpdate) { + await forgetRoom(roomId); + return; + } + final membership = roomUpdate is LeftRoomUpdate + ? Membership.leave + : roomUpdate is InvitedRoomUpdate + ? Membership.invite + : Membership.join; + // Make sure room exists + final currentRawRoom = await _roomsBox.get(roomId); + if (currentRawRoom == null) { + await _roomsBox.put( + roomId, + roomUpdate is JoinedRoomUpdate + ? Room( + client: client, + id: roomId, + membership: membership, + highlightCount: + roomUpdate.unreadNotifications?.highlightCount?.toInt() ?? + 0, + notificationCount: roomUpdate + .unreadNotifications?.notificationCount + ?.toInt() ?? + 0, + prev_batch: roomUpdate.timeline?.prevBatch, + summary: roomUpdate.summary, + ).toJson() + : Room( + client: client, + id: roomId, + membership: membership, + ).toJson()); + } else if (roomUpdate is JoinedRoomUpdate) { + final currentRoom = Room.fromJson(copyMap(currentRawRoom), client); + await _roomsBox.put( + roomId, + Room( + client: client, + id: roomId, + membership: membership, + highlightCount: + roomUpdate.unreadNotifications?.highlightCount?.toInt() ?? + currentRoom.highlightCount, + notificationCount: + roomUpdate.unreadNotifications?.notificationCount?.toInt() ?? + currentRoom.notificationCount, + prev_batch: + roomUpdate.timeline?.prevBatch ?? currentRoom.prev_batch, + summary: RoomSummary.fromJson(currentRoom.summary.toJson() + ..addAll(roomUpdate.summary?.toJson() ?? {})), + ).toJson()); + } + + // Is the timeline limited? Then all previous messages should be + // removed from the database! + if (roomUpdate is JoinedRoomUpdate && + roomUpdate.timeline?.limited == true) { + await _timelineFragmentsBox.delete(TupleKey(roomId, '').toString()); + } + } + + @override + Future storeSSSSCache( + String type, String keyId, String ciphertext, String content) async { + await _ssssCacheBox.put( + type, + SSSSCache( + type: type, + keyId: keyId, + ciphertext: ciphertext, + content: content, + ).toJson()); + } + + @override + Future storeSyncFilterId( + String syncFilterId, + ) async { + await _clientBox.put('sync_filter_id', syncFilterId); + } + + @override + Future storeUserCrossSigningKey(String userId, String publicKey, + String content, bool verified, bool blocked) async { + await _userCrossSigningKeysBox.put( + TupleKey(userId, publicKey).toString(), + { + 'user_id': userId, + 'public_key': publicKey, + 'content': content, + 'verified': verified, + 'blocked': blocked, + }, + ); + } + + @override + Future storeUserDeviceKey(String userId, String deviceId, + String content, bool verified, bool blocked, int lastActive) async { + await _userDeviceKeysBox.put(TupleKey(userId, deviceId).toString(), { + 'user_id': userId, + 'device_id': deviceId, + 'content': content, + 'verified': verified, + 'blocked': blocked, + 'last_active': lastActive, + 'last_sent_message': '', + }); + return; + } + + @override + Future storeUserDeviceKeysInfo(String userId, bool outdated) async { + await _userDeviceKeysOutdatedBox.put(userId, outdated); + return; + } + + Completer? _transactionLock; + final _transactionZones = {}; + + @override + Future transaction(Future Function() action) async { + // we want transactions to lock, however NOT if transactoins are run inside of each other. + // to be able to do this, we use dart zones (https://dart.dev/articles/archive/zones). + // _transactionZones holds a set of all zones which are currently running a transaction. + // _transactionLock holds the lock. + + // first we try to determine if we are inside of a transaction currently + var isInTransaction = false; + Zone? zone = Zone.current; + // for that we keep on iterating to the parent zone until there is either no zone anymore + // or we have found a zone inside of _transactionZones. + while (zone != null) { + if (_transactionZones.contains(zone)) { + isInTransaction = true; + break; + } + zone = zone.parent; + } + // if we are inside a transaction....just run the action + if (isInTransaction) { + return await action(); + } + // if we are *not* in a transaction, time to wait for the lock! + while (_transactionLock != null) { + await _transactionLock!.future; + } + // claim the lock + final lock = Completer(); + _transactionLock = lock; + try { + // run the action inside of a new zone + return await runZoned(() async { + try { + // don't forget to add the new zone to _transactionZones! + _transactionZones.add(Zone.current); + late final T result; + await _collection.transaction(() async { + result = await action(); + }); + return result; + } finally { + // aaaand remove the zone from _transactionZones again + _transactionZones.remove(Zone.current); + } + }); + } finally { + // aaaand finally release the lock + _transactionLock = null; + lock.complete(); + } + } + + @override + Future updateClient( + String homeserverUrl, + String token, + String userId, + String? deviceId, + String? deviceName, + String? prevBatch, + String? olmAccount, + ) async { + await transaction(() async { + await _clientBox.put('homeserver_url', homeserverUrl); + await _clientBox.put('token', token); + await _clientBox.put('user_id', userId); + if (deviceId == null) { + await _clientBox.delete('device_id'); + } else { + await _clientBox.put('device_id', deviceId); + } + if (deviceName == null) { + await _clientBox.delete('device_name'); + } else { + await _clientBox.put('device_name', deviceName); + } + if (prevBatch == null) { + await _clientBox.delete('prev_batch'); + } else { + await _clientBox.put('prev_batch', prevBatch); + } + if (olmAccount == null) { + await _clientBox.delete('olm_account'); + } else { + await _clientBox.put('olm_account', olmAccount); + } + }); + return; + } + + @override + Future updateClientKeys( + String olmAccount, + ) async { + await _clientBox.put('olm_account', olmAccount); + return; + } + + @override + Future updateInboundGroupSessionAllowedAtIndex( + String allowedAtIndex, String roomId, String sessionId) async { + final raw = await _inboundGroupSessionsBox.get(sessionId); + if (raw == null) { + Logs().w( + 'Tried to update inbound group session as uploaded which wasnt found in the database!'); + return; + } + raw['allowed_at_index'] = allowedAtIndex; + await _inboundGroupSessionsBox.put(sessionId, raw); + return; + } + + @override + Future updateInboundGroupSessionIndexes( + String indexes, String roomId, String sessionId) async { + final raw = await _inboundGroupSessionsBox.get(sessionId); + if (raw == null) { + Logs().w( + 'Tried to update inbound group session indexes of a session which was not found in the database!'); + return; + } + final json = copyMap(raw); + json['indexes'] = indexes; + await _inboundGroupSessionsBox.put(sessionId, json); + return; + } + + @override + Future updateRoomSortOrder( + double oldestSortOrder, double newestSortOrder, String roomId) async { + final raw = await _roomsBox.get(roomId); + if (raw == null) throw ('Room not found'); + raw['oldest_sort_order'] = oldestSortOrder; + raw['newest_sort_order'] = newestSortOrder; + await _roomsBox.put(roomId, raw); + return; + } + + @override + Future> getAllInboundGroupSessions() async { + final rawSessions = await _inboundGroupSessionsBox.getAllValues(); + return rawSessions.values + .map((raw) => StoredInboundGroupSession.fromJson(copyMap(raw))) + .toList(); + } + + @override + Future addSeenDeviceId( + String userId, + String deviceId, + String publicKeysHash, + ) => + _seenDeviceIdsBox.put( + TupleKey(userId, deviceId).toString(), publicKeysHash); + + @override + Future addSeenPublicKey( + String publicKey, + String deviceId, + ) => + _seenDeviceKeysBox.put(publicKey, deviceId); + + @override + Future deviceIdSeen(userId, deviceId) async { + final raw = + await _seenDeviceIdsBox.get(TupleKey(userId, deviceId).toString()); + if (raw == null) return null; + return raw; + } + + @override + Future publicKeySeen(String publicKey) async { + final raw = await _seenDeviceKeysBox.get(publicKey); + if (raw == null) return null; + return raw; + } +} + +class TupleKey { + final List parts; + + TupleKey(String key1, [String? key2, String? key3]) + : parts = [ + key1, + if (key2 != null) key2, + if (key3 != null) key3, + ]; + + const TupleKey.byParts(this.parts); + + TupleKey.fromString(String multiKeyString) + : parts = multiKeyString.split('|').toList(); + + @override + String toString() => parts.join('|'); + + @override + bool operator ==(other) => parts.toString() == other.toString(); +} + +dynamic _castValue(dynamic value) { + if (value is Map) { + return copyMap(value); + } + if (value is List) { + return value.map(_castValue).toList(); + } + return value; +} + +/// The store always gives back an `_InternalLinkedHasMap`. This +/// creates a deep copy of the json and makes sure that the format is always +/// `Map`. +Map copyMap(Map map) { + final copy = Map.from(map); + for (final entry in copy.entries) { + copy[entry.key] = _castValue(entry.value); + } + return copy; +} diff --git a/lib/src/database/hive_database.dart b/lib/src/database/hive_database.dart new file mode 100644 index 0000000..d2656ae --- /dev/null +++ b/lib/src/database/hive_database.dart @@ -0,0 +1,1433 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:async'; +import 'dart:convert'; +import 'dart:math'; +import 'dart:typed_data'; + +import 'package:hive/hive.dart'; +import 'package:matrix/encryption/utils/olm_session.dart'; +import 'package:matrix/encryption/utils/outbound_group_session.dart'; +import 'package:matrix/encryption/utils/ssss_cache.dart'; +import 'package:matrix/encryption/utils/stored_inbound_group_session.dart'; +import 'package:matrix/matrix.dart'; +import 'package:matrix/src/utils/queued_to_device_event.dart'; +import 'package:matrix/src/utils/run_benchmarked.dart'; + +/// This is a basic database for the Matrix SDK using the hive store. You need +/// to make sure that you perform `Hive.init()` or `Hive.flutterInit()` before +/// you use this. +/// +/// This database does not support file caching! +class FamedlySdkHiveDatabase extends DatabaseApi { + static const int version = 5; + final String name; + late Box _clientBox; + late Box _accountDataBox; + late Box _roomsBox; + late Box _toDeviceQueueBox; + + /// Key is a tuple as MultiKey(roomId, type) where stateKey can be + /// an empty string. + late LazyBox _roomStateBox; + + /// Key is a tuple as MultiKey(roomId, userId) + late LazyBox _roomMembersBox; + + /// Key is a tuple as MultiKey(roomId, type) + late LazyBox _roomAccountDataBox; + late LazyBox _inboundGroupSessionsBox; + late LazyBox _outboundGroupSessionsBox; + late LazyBox _olmSessionsBox; + + /// Key is a tuple as MultiKey(userId, deviceId) + late LazyBox _userDeviceKeysBox; + + /// Key is the user ID as a String + late LazyBox _userDeviceKeysOutdatedBox; + + /// Key is a tuple as MultiKey(userId, publicKey) + late LazyBox _userCrossSigningKeysBox; + late LazyBox _ssssCacheBox; + late LazyBox _presencesBox; + + /// Key is a tuple as Multikey(roomId, fragmentId) while the default + /// fragmentId is an empty String + late LazyBox _timelineFragmentsBox; + + /// Key is a tuple as MultiKey(roomId, eventId) + late LazyBox _eventsBox; + + /// Key is a tuple as MultiKey(userId, deviceId) + late LazyBox _seenDeviceIdsBox; + + late LazyBox _seenDeviceKeysBox; + + String get _clientBoxName => '$name.box.client'; + + String get _accountDataBoxName => '$name.box.account_data'; + + String get _roomsBoxName => '$name.box.rooms'; + + String get _toDeviceQueueBoxName => '$name.box.to_device_queue'; + + String get _roomStateBoxName => '$name.box.room_states'; + + String get _roomMembersBoxName => '$name.box.room_members'; + + String get _roomAccountDataBoxName => '$name.box.room_account_data'; + + String get _inboundGroupSessionsBoxName => '$name.box.inbound_group_session'; + + String get _outboundGroupSessionsBoxName => + '$name.box.outbound_group_session'; + + String get _olmSessionsBoxName => '$name.box.olm_session'; + + String get _userDeviceKeysBoxName => '$name.box.user_device_keys'; + + String get _userDeviceKeysOutdatedBoxName => + '$name.box.user_device_keys_outdated'; + + String get _userCrossSigningKeysBoxName => '$name.box.cross_signing_keys'; + + String get _ssssCacheBoxName => '$name.box.ssss_cache'; + + String get _presencesBoxName => '$name.box.presences'; + + String get _timelineFragmentsBoxName => '$name.box.timeline_fragments'; + + String get _eventsBoxName => '$name.box.events'; + + String get _seenDeviceIdsBoxName => '$name.box.seen_device_ids'; + + String get _seenDeviceKeysBoxName => '$name.box.seen_device_keys'; + + final HiveCipher? encryptionCipher; + + FamedlySdkHiveDatabase(this.name, {this.encryptionCipher}); + + @override + int get maxFileSize => 0; + + Future _actionOnAllBoxes(Future Function(BoxBase box) action) => + Future.wait([ + action(_clientBox), + action(_accountDataBox), + action(_roomsBox), + action(_roomStateBox), + action(_roomMembersBox), + action(_toDeviceQueueBox), + action(_roomAccountDataBox), + action(_inboundGroupSessionsBox), + action(_outboundGroupSessionsBox), + action(_olmSessionsBox), + action(_userDeviceKeysBox), + action(_userDeviceKeysOutdatedBox), + action(_userCrossSigningKeysBox), + action(_ssssCacheBox), + action(_presencesBox), + action(_timelineFragmentsBox), + action(_eventsBox), + action(_seenDeviceIdsBox), + action(_seenDeviceKeysBox), + ]); + + Future open() async { + _clientBox = await Hive.openBox( + _clientBoxName, + encryptionCipher: encryptionCipher, + ); + _accountDataBox = await Hive.openBox( + _accountDataBoxName, + encryptionCipher: encryptionCipher, + ); + _roomsBox = await Hive.openBox( + _roomsBoxName, + encryptionCipher: encryptionCipher, + ); + _roomStateBox = await Hive.openLazyBox( + _roomStateBoxName, + encryptionCipher: encryptionCipher, + ); + _roomMembersBox = await Hive.openLazyBox( + _roomMembersBoxName, + encryptionCipher: encryptionCipher, + ); + _toDeviceQueueBox = await Hive.openBox( + _toDeviceQueueBoxName, + encryptionCipher: encryptionCipher, + ); + _roomAccountDataBox = await Hive.openLazyBox( + _roomAccountDataBoxName, + encryptionCipher: encryptionCipher, + ); + _inboundGroupSessionsBox = await Hive.openLazyBox( + _inboundGroupSessionsBoxName, + encryptionCipher: encryptionCipher, + ); + _outboundGroupSessionsBox = await Hive.openLazyBox( + _outboundGroupSessionsBoxName, + encryptionCipher: encryptionCipher, + ); + _olmSessionsBox = await Hive.openLazyBox( + _olmSessionsBoxName, + encryptionCipher: encryptionCipher, + ); + _userDeviceKeysBox = await Hive.openLazyBox( + _userDeviceKeysBoxName, + encryptionCipher: encryptionCipher, + ); + _userDeviceKeysOutdatedBox = await Hive.openLazyBox( + _userDeviceKeysOutdatedBoxName, + encryptionCipher: encryptionCipher, + ); + _userCrossSigningKeysBox = await Hive.openLazyBox( + _userCrossSigningKeysBoxName, + encryptionCipher: encryptionCipher, + ); + _ssssCacheBox = await Hive.openLazyBox( + _ssssCacheBoxName, + encryptionCipher: encryptionCipher, + ); + _presencesBox = await Hive.openLazyBox( + _presencesBoxName, + encryptionCipher: encryptionCipher, + ); + _timelineFragmentsBox = await Hive.openLazyBox( + _timelineFragmentsBoxName, + encryptionCipher: encryptionCipher, + ); + _eventsBox = await Hive.openLazyBox( + _eventsBoxName, + encryptionCipher: encryptionCipher, + ); + _seenDeviceIdsBox = await Hive.openLazyBox( + _seenDeviceIdsBoxName, + encryptionCipher: encryptionCipher, + ); + _seenDeviceKeysBox = await Hive.openLazyBox( + _seenDeviceKeysBoxName, + encryptionCipher: encryptionCipher, + ); + + // Check version and check if we need a migration + final currentVersion = (await _clientBox.get('version') as int?); + if (currentVersion == null) { + await _clientBox.put('version', version); + } else if (currentVersion != version) { + await _migrateFromVersion(currentVersion); + } + + return; + } + + Future _migrateFromVersion(int currentVersion) async { + Logs().i('Migrate Hive database from version $currentVersion to $version'); + if (version == 5) { + for (final key in _userDeviceKeysBox.keys) { + try { + final raw = await _userDeviceKeysBox.get(key) as Map; + if (!raw.containsKey('keys')) continue; + final deviceKeys = DeviceKeys.fromJson( + convertToJson(raw), + Client(''), + ); + await addSeenDeviceId(deviceKeys.userId, deviceKeys.deviceId!, + deviceKeys.curve25519Key! + deviceKeys.ed25519Key!); + await addSeenPublicKey(deviceKeys.ed25519Key!, deviceKeys.deviceId!); + await addSeenPublicKey( + deviceKeys.curve25519Key!, deviceKeys.deviceId!); + } catch (e) { + Logs().w('Can not migrate device $key', e); + } + } + } + await clearCache(); + await _clientBox.put('version', version); + } + + @override + Future clear() async { + Logs().i('Clear and close hive database...'); + await _actionOnAllBoxes((box) async { + try { + await box.deleteAll(box.keys); + await box.close(); + } catch (e) { + Logs().v('Unable to clear box ${box.name}', e); + await box.deleteFromDisk(); + } + }); + return; + } + + @override + Future clearCache() async { + await _roomsBox.deleteAll(_roomsBox.keys); + await _accountDataBox.deleteAll(_accountDataBox.keys); + await _roomStateBox.deleteAll(_roomStateBox.keys); + await _roomMembersBox.deleteAll(_roomMembersBox.keys); + await _eventsBox.deleteAll(_eventsBox.keys); + await _timelineFragmentsBox.deleteAll(_timelineFragmentsBox.keys); + await _outboundGroupSessionsBox.deleteAll(_outboundGroupSessionsBox.keys); + await _presencesBox.deleteAll(_presencesBox.keys); + await _clientBox.delete('prev_batch'); + } + + @override + Future clearSSSSCache() async { + await _ssssCacheBox.deleteAll(_ssssCacheBox.keys); + } + + @override + Future close() => _actionOnAllBoxes((box) => box.close()); + + @override + Future deleteFromToDeviceQueue(int id) async { + await _toDeviceQueueBox.delete(id); + return; + } + + @override + Future deleteOldFiles(int savedAt) async { + return; + } + + @override + Future forgetRoom(String roomId) async { + await _timelineFragmentsBox.delete(MultiKey(roomId, '').toString()); + for (final key in _eventsBox.keys) { + final multiKey = MultiKey.fromString(key); + if (multiKey.parts.first != roomId) continue; + await _eventsBox.delete(key); + } + for (final key in _roomStateBox.keys) { + final multiKey = MultiKey.fromString(key); + if (multiKey.parts.first != roomId) continue; + await _roomStateBox.delete(key); + } + for (final key in _roomMembersBox.keys) { + final multiKey = MultiKey.fromString(key); + if (multiKey.parts.first != roomId) continue; + await _roomMembersBox.delete(key); + } + for (final key in _roomAccountDataBox.keys) { + final multiKey = MultiKey.fromString(key); + if (multiKey.parts.first != roomId) continue; + await _roomAccountDataBox.delete(key); + } + await _roomsBox.delete(roomId.toHiveKey); + } + + @override + Future> getAccountData() => + runBenchmarked>('Get all account data from Hive', + () async { + final accountData = {}; + for (final key in _accountDataBox.keys) { + final raw = await _accountDataBox.get(key); + accountData[key.toString().fromHiveKey] = BasicEvent( + type: key.toString().fromHiveKey, + content: convertToJson(raw), + ); + } + return accountData; + }, _accountDataBox.keys.length); + + @override + Future?> getClient(String name) => + runBenchmarked('Get Client from Hive', () async { + final map = {}; + for (final key in _clientBox.keys) { + if (key == 'version') continue; + map[key] = await _clientBox.get(key); + } + if (map.isEmpty) return null; + return map; + }); + + @override + Future getEventById(String eventId, Room room) async { + final raw = await _eventsBox.get(MultiKey(room.id, eventId).toString()); + if (raw == null) return null; + return Event.fromJson(convertToJson(raw), room); + } + + /// Loads a whole list of events at once from the store for a specific room + Future> _getEventsByIds(List eventIds, Room room) => + Future.wait(eventIds + .map( + (eventId) async => Event.fromJson( + convertToJson( + await _eventsBox.get(MultiKey(room.id, eventId).toString()), + ), + room, + ), + ) + .toList()); + + @override + Future> getEventList( + Room room, { + int start = 0, + int? limit, + }) => + runBenchmarked>('Get event list', () async { + // Get the synced event IDs from the store + final timelineKey = MultiKey(room.id, '').toString(); + final timelineEventIds = + (await _timelineFragmentsBox.get(timelineKey) as List? ?? []); + + // Get the local stored SENDING events from the store + late final List sendingEventIds; + if (start != 0) { + sendingEventIds = []; + } else { + final sendingTimelineKey = MultiKey(room.id, 'SENDING').toString(); + sendingEventIds = + (await _timelineFragmentsBox.get(sendingTimelineKey) as List? ?? + []); + } + + // Combine those two lists while respecting the start and limit parameters. + final end = min(timelineEventIds.length, + start + (limit ?? timelineEventIds.length)); + final eventIds = sendingEventIds + + (start < timelineEventIds.length + ? timelineEventIds.getRange(start, end).toList() + : []); + + return await _getEventsByIds(eventIds.cast(), room); + }); + + @override + Future getFile(Uri mxcUri) async { + return null; + } + + @override + Future getInboundGroupSession( + String roomId, + String sessionId, + ) async { + final raw = await _inboundGroupSessionsBox.get(sessionId.toHiveKey); + if (raw == null) return null; + return StoredInboundGroupSession.fromJson(convertToJson(raw)); + } + + @override + Future> + getInboundGroupSessionsToUpload() async { + final sessions = (await Future.wait(_inboundGroupSessionsBox.keys.map( + (sessionId) async => + await _inboundGroupSessionsBox.get(sessionId)))) + .where((rawSession) => rawSession['uploaded'] == false) + .take(500) + .map( + (json) => StoredInboundGroupSession.fromJson( + convertToJson(json), + ), + ) + .toList(); + return sessions; + } + + @override + Future> getLastSentMessageUserDeviceKey( + String userId, String deviceId) async { + final raw = + await _userDeviceKeysBox.get(MultiKey(userId, deviceId).toString()); + if (raw == null) return []; + return [raw['last_sent_message']]; + } + + @override + Future storeOlmSession(String identityKey, String sessionId, + String pickle, int lastReceived) async { + final rawSessions = + (await _olmSessionsBox.get(identityKey.toHiveKey) as Map?) ?? {}; + rawSessions[sessionId] = { + 'identity_key': identityKey, + 'pickle': pickle, + 'session_id': sessionId, + 'last_received': lastReceived, + }; + await _olmSessionsBox.put(identityKey.toHiveKey, rawSessions); + return; + } + + @override + Future> getOlmSessions( + String identityKey, String userId) async { + final rawSessions = + await _olmSessionsBox.get(identityKey.toHiveKey) as Map?; + if (rawSessions == null || rawSessions.isEmpty) return []; + return rawSessions.values + .map((json) => OlmSession.fromJson(convertToJson(json), userId)) + .toList(); + } + + @override + Future> getAllOlmSessions() async { + final backup = Map.fromEntries( + await Future.wait( + _olmSessionsBox.keys.map( + (key) async => MapEntry( + key, + await _olmSessionsBox.get(key), + ), + ), + ), + ); + return backup.cast(); + } + + @override + Future> getOlmSessionsForDevices( + List identityKey, String userId) async { + final sessions = await Future.wait( + identityKey.map((identityKey) => getOlmSessions(identityKey, userId))); + return [for (final sublist in sessions) ...sublist]; + } + + @override + Future getOutboundGroupSession( + String roomId, String userId) async { + final raw = await _outboundGroupSessionsBox.get(roomId.toHiveKey); + if (raw == null) return null; + return OutboundGroupSession.fromJson(convertToJson(raw), userId); + } + + @override + Future> getRoomList(Client client) => + runBenchmarked>('Get room list from hive', () async { + final rooms = {}; + final userID = client.userID; + final importantRoomStates = client.importantStateEvents; + for (final key in _roomsBox.keys) { + // Get the room + final raw = await _roomsBox.get(key); + final room = Room.fromJson(convertToJson(raw), client); + + // let's see if we need any m.room.member events + // We always need the member event for ourself + final membersToPostload = {if (userID != null) userID}; + // If the room is a direct chat, those IDs should be there too + if (room.isDirectChat) { + membersToPostload.add(room.directChatMatrixID!); + } + // the lastEvent message preview might have an author we need to fetch, if it is a group chat + final lastEvent = room.getState(EventTypes.Message); + if (lastEvent != null && !room.isDirectChat) { + membersToPostload.add(lastEvent.senderId); + } + // if the room has no name and no canonical alias, its name is calculated + // based on the heroes of the room + if (room.getState(EventTypes.RoomName) == null && + room.getState(EventTypes.RoomCanonicalAlias) == null) { + // we don't have a name and no canonical alias, so we'll need to + // post-load the heroes + membersToPostload.addAll(room.summary.mHeroes ?? []); + } + // Load members + for (final userId in membersToPostload) { + final state = + await _roomMembersBox.get(MultiKey(room.id, userId).toString()); + if (state == null) { + Logs().w('Unable to post load member $userId'); + continue; + } + room.setState(Event.fromJson(convertToJson(state), room)); + } + + // Get the "important" room states. All other states will be loaded once + // `getUnimportantRoomStates()` is called. + for (final type in importantRoomStates) { + final states = await _roomStateBox + .get(MultiKey(room.id, type).toString()) as Map?; + if (states == null) continue; + final stateEvents = states.values + .map((raw) => Event.fromJson(convertToJson(raw), room)) + .toList(); + for (final state in stateEvents) { + room.setState(state); + } + } + + // Add to the list and continue. + rooms[room.id] = room; + } + + // Get the room account data + for (final key in _roomAccountDataBox.keys) { + final roomId = MultiKey.fromString(key).parts.first; + if (rooms.containsKey(roomId)) { + final raw = await _roomAccountDataBox.get(key); + final basicRoomEvent = BasicRoomEvent.fromJson( + convertToJson(raw), + ); + rooms[roomId]!.roomAccountData[basicRoomEvent.type] = + basicRoomEvent; + } else { + Logs().w( + 'Found account data for unknown room $roomId. Delete now...'); + await _roomAccountDataBox.delete(key); + } + } + + return rooms.values.toList(); + }, _roomsBox.keys.length); + + @override + Future getSSSSCache(String type) async { + final raw = await _ssssCacheBox.get(type); + if (raw == null) return null; + return SSSSCache.fromJson(convertToJson(raw)); + } + + @override + Future> getToDeviceEventQueue() async => + await Future.wait(_toDeviceQueueBox.keys.map((i) async { + final raw = await _toDeviceQueueBox.get(i); + raw['id'] = i; + return QueuedToDeviceEvent.fromJson(convertToJson(raw)); + }).toList()); + + @override + Future> getUnimportantRoomEventStatesForRoom( + List events, Room room) async { + final keys = _roomStateBox.keys.where((key) { + final tuple = MultiKey.fromString(key); + return tuple.parts.first == room.id && !events.contains(tuple.parts[1]); + }); + + final unimportantEvents = []; + for (final key in keys) { + final Map states = await _roomStateBox.get(key); + unimportantEvents.addAll( + states.values.map((raw) => Event.fromJson(convertToJson(raw), room))); + } + return unimportantEvents; + } + + @override + Future getUser(String userId, Room room) async { + final state = + await _roomMembersBox.get(MultiKey(room.id, userId).toString()); + if (state == null) return null; + return Event.fromJson(convertToJson(state), room).asUser; + } + + @override + Future> getUserDeviceKeys(Client client) => + runBenchmarked>( + 'Get all user device keys from Hive', () async { + final deviceKeysOutdated = _userDeviceKeysOutdatedBox.keys; + if (deviceKeysOutdated.isEmpty) { + return {}; + } + final res = {}; + for (final userId in deviceKeysOutdated) { + final deviceKeysBoxKeys = _userDeviceKeysBox.keys.where((tuple) { + final tupleKey = MultiKey.fromString(tuple); + return tupleKey.parts.first == userId; + }); + final crossSigningKeysBoxKeys = + _userCrossSigningKeysBox.keys.where((tuple) { + final tupleKey = MultiKey.fromString(tuple); + return tupleKey.parts.first == userId; + }); + res[userId] = DeviceKeysList.fromDbJson( + { + 'client_id': client.id, + 'user_id': userId, + 'outdated': await _userDeviceKeysOutdatedBox.get(userId), + }, + await Future.wait(deviceKeysBoxKeys.map((key) async => + convertToJson(await _userDeviceKeysBox.get(key)))), + await Future.wait(crossSigningKeysBoxKeys.map((key) async => + convertToJson(await _userCrossSigningKeysBox.get(key)))), + client); + } + return res; + }, _userDeviceKeysBox.keys.length); + + @override + Future> getUsers(Room room) async { + final users = []; + for (final key in _roomMembersBox.keys) { + final statesKey = MultiKey.fromString(key); + if (statesKey.parts[0] != room.id) continue; + final state = await _roomMembersBox.get(key); + users.add(Event.fromJson(convertToJson(state), room).asUser); + } + return users; + } + + @override + Future insertClient( + String name, + String homeserverUrl, + String token, + String userId, + String? deviceId, + String? deviceName, + String? prevBatch, + String? olmAccount) async { + await _clientBox.put('homeserver_url', homeserverUrl); + await _clientBox.put('token', token); + await _clientBox.put('user_id', userId); + await _clientBox.put('device_id', deviceId); + await _clientBox.put('device_name', deviceName); + await _clientBox.put('prev_batch', prevBatch); + await _clientBox.put('olm_account', olmAccount); + await _clientBox.put('sync_filter_id', null); + return; + } + + @override + Future insertIntoToDeviceQueue( + String type, String txnId, String content) async { + return await _toDeviceQueueBox.add({ + 'type': type, + 'txn_id': txnId, + 'content': content, + }); + } + + @override + Future markInboundGroupSessionAsUploaded( + String roomId, String sessionId) async { + final raw = await _inboundGroupSessionsBox.get(sessionId.toHiveKey); + if (raw == null) { + Logs().w( + 'Tried to mark inbound group session as uploaded which was not found in the database!'); + return; + } + raw['uploaded'] = true; + await _inboundGroupSessionsBox.put(sessionId.toHiveKey, raw); + return; + } + + @override + Future markInboundGroupSessionsAsNeedingUpload() async { + for (final sessionId in _inboundGroupSessionsBox.keys) { + final raw = await _inboundGroupSessionsBox.get(sessionId); + raw['uploaded'] = false; + await _inboundGroupSessionsBox.put(sessionId, raw); + } + return; + } + + @override + Future removeEvent(String eventId, String roomId) async { + await _eventsBox.delete(MultiKey(roomId, eventId).toString()); + for (final key in _timelineFragmentsBox.keys) { + final multiKey = MultiKey.fromString(key); + if (multiKey.parts.first != roomId) continue; + final List eventIds = await _timelineFragmentsBox.get(key) ?? []; + final prevLength = eventIds.length; + eventIds.removeWhere((id) => id == eventId); + if (eventIds.length < prevLength) { + await _timelineFragmentsBox.put(key, eventIds); + } + } + return; + } + + @override + Future removeOutboundGroupSession(String roomId) async { + await _outboundGroupSessionsBox.delete(roomId.toHiveKey); + return; + } + + @override + Future removeUserCrossSigningKey( + String userId, String publicKey) async { + await _userCrossSigningKeysBox + .delete(MultiKey(userId, publicKey).toString()); + return; + } + + @override + Future removeUserDeviceKey(String userId, String deviceId) async { + await _userDeviceKeysBox.delete(MultiKey(userId, deviceId).toString()); + return; + } + + @override + Future resetNotificationCount(String roomId) async { + final raw = await _roomsBox.get(roomId.toHiveKey); + if (raw == null) return; + raw['notification_count'] = raw['highlight_count'] = 0; + await _roomsBox.put(roomId.toHiveKey, raw); + return; + } + + @override + Future setBlockedUserCrossSigningKey( + bool blocked, String userId, String publicKey) async { + final raw = await _userCrossSigningKeysBox + .get(MultiKey(userId, publicKey).toString()); + raw['blocked'] = blocked; + await _userCrossSigningKeysBox.put( + MultiKey(userId, publicKey).toString(), + raw, + ); + return; + } + + @override + Future setBlockedUserDeviceKey( + bool blocked, String userId, String deviceId) async { + final raw = + await _userDeviceKeysBox.get(MultiKey(userId, deviceId).toString()); + raw['blocked'] = blocked; + await _userDeviceKeysBox.put( + MultiKey(userId, deviceId).toString(), + raw, + ); + return; + } + + @override + Future setLastActiveUserDeviceKey( + int lastActive, String userId, String deviceId) async { + final raw = + await _userDeviceKeysBox.get(MultiKey(userId, deviceId).toString()); + raw['last_active'] = lastActive; + await _userDeviceKeysBox.put( + MultiKey(userId, deviceId).toString(), + raw, + ); + } + + @override + Future setLastSentMessageUserDeviceKey( + String lastSentMessage, String userId, String deviceId) async { + final raw = + await _userDeviceKeysBox.get(MultiKey(userId, deviceId).toString()); + raw['last_sent_message'] = lastSentMessage; + await _userDeviceKeysBox.put( + MultiKey(userId, deviceId).toString(), + raw, + ); + } + + @override + Future setRoomPrevBatch( + String prevBatch, String roomId, Client client) async { + final raw = await _roomsBox.get(roomId.toHiveKey); + if (raw == null) return; + final room = Room.fromJson(convertToJson(raw), client); + room.prev_batch = prevBatch; + await _roomsBox.put(roomId.toHiveKey, room.toJson()); + return; + } + + @override + Future setVerifiedUserCrossSigningKey( + bool verified, String userId, String publicKey) async { + final raw = (await _userCrossSigningKeysBox + .get(MultiKey(userId, publicKey).toString()) as Map?) ?? + {}; + raw['verified'] = verified; + await _userCrossSigningKeysBox.put( + MultiKey(userId, publicKey).toString(), + raw, + ); + return; + } + + @override + Future setVerifiedUserDeviceKey( + bool verified, String userId, String deviceId) async { + final raw = + await _userDeviceKeysBox.get(MultiKey(userId, deviceId).toString()); + raw['verified'] = verified; + await _userDeviceKeysBox.put( + MultiKey(userId, deviceId).toString(), + raw, + ); + return; + } + + @override + Future storeAccountData(String type, String content) async { + await _accountDataBox.put( + type.toHiveKey, convertToJson(jsonDecode(content))); + return; + } + + @override + Future storeEventUpdate(EventUpdate eventUpdate, Client client) async { + // Ephemerals should not be stored + if (eventUpdate.type == EventUpdateType.ephemeral) return; + + // In case of this is a redaction event + if (eventUpdate.content['type'] == EventTypes.Redaction) { + final tmpRoom = Room(id: eventUpdate.roomID, client: client); + final event = await getEventById(eventUpdate.content['redacts'], tmpRoom); + if (event != null) { + event.setRedactionEvent(Event.fromJson(eventUpdate.content, tmpRoom)); + await _eventsBox.put( + MultiKey(eventUpdate.roomID, event.eventId).toString(), + event.toJson()); + } + } + + // Store a common message event + if ({EventUpdateType.timeline, EventUpdateType.history} + .contains(eventUpdate.type)) { + final eventId = eventUpdate.content['event_id']; + // Is this ID already in the store? + final Map? prevEvent = await _eventsBox + .get(MultiKey(eventUpdate.roomID, eventId).toString()); + final prevStatus = prevEvent == null + ? null + : () { + final json = convertToJson(prevEvent); + final statusInt = json.tryGet('status') ?? + json + .tryGetMap('unsigned') + ?.tryGet(messageSendingStatusKey); + return statusInt == null ? null : eventStatusFromInt(statusInt); + }(); + + // calculate the status + final newStatus = eventStatusFromInt( + eventUpdate.content.tryGet('status') ?? + eventUpdate.content + .tryGetMap('unsigned') + ?.tryGet(messageSendingStatusKey) ?? + EventStatus.synced.intValue, + ); + + // Is this the response to a sending event which is already synced? Then + // there is nothing to do here. + if (!newStatus.isSynced && prevStatus != null && prevStatus.isSynced) { + return; + } + + final status = newStatus.isError || prevStatus == null + ? newStatus + : latestEventStatus( + prevStatus, + newStatus, + ); + + // Add the status and the sort order to the content so it get stored + eventUpdate.content['unsigned'] ??= {}; + eventUpdate.content['unsigned'][messageSendingStatusKey] = + eventUpdate.content['status'] = status.intValue; + + // In case this event has sent from this account we have a transaction ID + final transactionId = eventUpdate.content + .tryGetMap('unsigned') + ?.tryGet('transaction_id'); + + await _eventsBox.put(MultiKey(eventUpdate.roomID, eventId).toString(), + eventUpdate.content); + + // Update timeline fragments + final key = MultiKey(eventUpdate.roomID, status.isSent ? '' : 'SENDING') + .toString(); + + final List eventIds = (await _timelineFragmentsBox.get(key) ?? []); + + if (!eventIds.contains(eventId)) { + if (eventUpdate.type == EventUpdateType.history) { + eventIds.add(eventId); + } else { + eventIds.insert(0, eventId); + } + await _timelineFragmentsBox.put(key, eventIds); + } else if (status.isSynced && + prevStatus != null && + prevStatus.isSent && + eventUpdate.type != EventUpdateType.history) { + // Status changes from 1 -> 2? Make sure event is correctly sorted. + eventIds.remove(eventId); + eventIds.insert(0, eventId); + } + + // If event comes from server timeline, remove sending events with this ID + if (status.isSent) { + final key = MultiKey(eventUpdate.roomID, 'SENDING').toString(); + final List eventIds = (await _timelineFragmentsBox.get(key) ?? []); + final i = eventIds.indexWhere((id) => id == eventId); + if (i != -1) { + await _timelineFragmentsBox.put(key, eventIds..removeAt(i)); + } + } + + // Is there a transaction id? Then delete the event with this id. + if (!status.isError && !status.isSending && transactionId != null) { + await removeEvent(transactionId, eventUpdate.roomID); + } + } + + // Store a common state event + if ({ + EventUpdateType.timeline, + EventUpdateType.state, + EventUpdateType.inviteState + }.contains(eventUpdate.type)) { + if (eventUpdate.content['type'] == EventTypes.RoomMember) { + await _roomMembersBox.put( + MultiKey( + eventUpdate.roomID, + eventUpdate.content['state_key'], + ).toString(), + eventUpdate.content); + } else { + final key = MultiKey( + eventUpdate.roomID, + eventUpdate.content['type'], + ).toString(); + final Map stateMap = await _roomStateBox.get(key) ?? {}; + // store state events and new messages, that either are not an edit or an edit of the lastest message + // An edit is an event, that has an edit relation to the latest event. In some cases for the second edit, we need to compare if both have an edit relation to the same event instead. + if (eventUpdate.content + .tryGetMap('content') + ?.tryGetMap('m.relates_to') == + null) { + stateMap[eventUpdate.content['state_key']] = eventUpdate.content; + await _roomStateBox.put(key, stateMap); + } else { + final editedEventRelationshipEventId = eventUpdate.content + .tryGetMap('content') + ?.tryGetMap('m.relates_to') + ?.tryGet('event_id'); + if (eventUpdate.content['type'] != EventTypes.Message || + eventUpdate.content + .tryGetMap('content') + ?.tryGetMap('m.relates_to') + ?.tryGet('rel_type') != + RelationshipTypes.edit || + editedEventRelationshipEventId == stateMap['']?.eventId || + ((stateMap['']?.relationshipType == RelationshipTypes.edit && + editedEventRelationshipEventId == + stateMap['']?.relationshipEventId))) { + stateMap[eventUpdate.content['state_key']] = eventUpdate.content; + await _roomStateBox.put(key, stateMap); + } + } + } + } + + // Store a room account data event + if (eventUpdate.type == EventUpdateType.accountData) { + await _roomAccountDataBox.put( + MultiKey( + eventUpdate.roomID, + eventUpdate.content['type'], + ).toString(), + eventUpdate.content, + ); + } + } + + @override + Future storeFile(Uri mxcUri, Uint8List bytes, int time) async { + return; + } + + @override + Future storeInboundGroupSession( + String roomId, + String sessionId, + String pickle, + String content, + String indexes, + String allowedAtIndex, + String senderKey, + String senderClaimedKey) async { + await _inboundGroupSessionsBox.put( + sessionId.toHiveKey, + StoredInboundGroupSession( + roomId: roomId, + sessionId: sessionId, + pickle: pickle, + content: content, + indexes: indexes, + allowedAtIndex: allowedAtIndex, + senderKey: senderKey, + senderClaimedKeys: senderClaimedKey, + uploaded: false, + ).toJson()); + return; + } + + @override + Future storeOutboundGroupSession( + String roomId, String pickle, String deviceIds, int creationTime) async { + await _outboundGroupSessionsBox.put(roomId.toHiveKey, { + 'room_id': roomId, + 'pickle': pickle, + 'device_ids': deviceIds, + 'creation_time': creationTime, + }); + return; + } + + @override + Future storePrevBatch( + String prevBatch, + ) async { + if (_clientBox.keys.isEmpty) return; + await _clientBox.put('prev_batch', prevBatch); + return; + } + + @override + Future storeRoomUpdate( + String roomId, SyncRoomUpdate roomUpdate, Client client) async { + // Leave room if membership is leave + if (roomUpdate is LeftRoomUpdate) { + await forgetRoom(roomId); + return; + } + final membership = roomUpdate is LeftRoomUpdate + ? Membership.leave + : roomUpdate is InvitedRoomUpdate + ? Membership.invite + : Membership.join; + // Make sure room exists + if (!_roomsBox.containsKey(roomId.toHiveKey)) { + await _roomsBox.put( + roomId.toHiveKey, + roomUpdate is JoinedRoomUpdate + ? Room( + client: client, + id: roomId, + membership: membership, + highlightCount: + roomUpdate.unreadNotifications?.highlightCount?.toInt() ?? + 0, + notificationCount: roomUpdate + .unreadNotifications?.notificationCount + ?.toInt() ?? + 0, + prev_batch: roomUpdate.timeline?.prevBatch, + summary: roomUpdate.summary, + ).toJson() + : Room( + client: client, + id: roomId, + membership: membership, + ).toJson()); + } else if (roomUpdate is JoinedRoomUpdate) { + final currentRawRoom = await _roomsBox.get(roomId.toHiveKey); + final currentRoom = Room.fromJson(convertToJson(currentRawRoom), client); + await _roomsBox.put( + roomId.toHiveKey, + Room( + client: client, + id: roomId, + membership: membership, + highlightCount: + roomUpdate.unreadNotifications?.highlightCount?.toInt() ?? + currentRoom.highlightCount, + notificationCount: + roomUpdate.unreadNotifications?.notificationCount?.toInt() ?? + currentRoom.notificationCount, + prev_batch: + roomUpdate.timeline?.prevBatch ?? currentRoom.prev_batch, + summary: RoomSummary.fromJson(currentRoom.summary.toJson() + ..addAll(roomUpdate.summary?.toJson() ?? {})), + ).toJson()); + } + + // Is the timeline limited? Then all previous messages should be + // removed from the database! + if (roomUpdate is JoinedRoomUpdate && + roomUpdate.timeline?.limited == true) { + await _timelineFragmentsBox.delete(MultiKey(roomId, '').toString()); + } + } + + @override + Future storeSSSSCache( + String type, String keyId, String ciphertext, String content) async { + await _ssssCacheBox.put( + type, + SSSSCache( + type: type, + keyId: keyId, + ciphertext: ciphertext, + content: content, + ).toJson()); + } + + @override + Future storeSyncFilterId( + String syncFilterId, + ) async { + await _clientBox.put('sync_filter_id', syncFilterId); + } + + @override + Future storeUserCrossSigningKey(String userId, String publicKey, + String content, bool verified, bool blocked) async { + await _userCrossSigningKeysBox.put( + MultiKey(userId, publicKey).toString(), + { + 'user_id': userId, + 'public_key': publicKey, + 'content': content, + 'verified': verified, + 'blocked': blocked, + }, + ); + } + + @override + Future storeUserDeviceKey(String userId, String deviceId, + String content, bool verified, bool blocked, int lastActive) async { + await _userDeviceKeysBox.put(MultiKey(userId, deviceId).toString(), { + 'user_id': userId, + 'device_id': deviceId, + 'content': content, + 'verified': verified, + 'blocked': blocked, + 'last_active': lastActive, + 'last_sent_message': '', + }); + return; + } + + @override + Future storeUserDeviceKeysInfo(String userId, bool outdated) async { + await _userDeviceKeysOutdatedBox.put(userId.toHiveKey, outdated); + return; + } + + Completer? _transactionLock; + final _transactionZones = {}; + + @override + Future transaction(Future Function() action) async { + // we want transactions to lock, however NOT if transactoins are run inside of each other. + // to be able to do this, we use dart zones (https://dart.dev/articles/archive/zones). + // _transactionZones holds a set of all zones which are currently running a transaction. + // _transactionLock holds the lock. + + // first we try to determine if we are inside of a transaction currently + var isInTransaction = false; + Zone? zone = Zone.current; + // for that we keep on iterating to the parent zone until there is either no zone anymore + // or we have found a zone inside of _transactionZones. + while (zone != null) { + if (_transactionZones.contains(zone)) { + isInTransaction = true; + break; + } + zone = zone.parent; + } + // if we are inside a transaction....just run the action + if (isInTransaction) { + return await action(); + } + // if we are *not* in a transaction, time to wait for the lock! + while (_transactionLock != null) { + await _transactionLock!.future; + } + // claim the lock + final lock = Completer(); + _transactionLock = lock; + try { + // run the action inside of a new zone + return await runZoned(() async { + try { + // don't forget to add the new zone to _transactionZones! + _transactionZones.add(Zone.current); + return await action(); + } finally { + // aaaand remove the zone from _transactionZones again + _transactionZones.remove(Zone.current); + } + }); + } finally { + // aaaand finally release the lock + _transactionLock = null; + lock.complete(); + } + } + + @override + Future updateClient( + String homeserverUrl, + String token, + String userId, + String? deviceId, + String? deviceName, + String? prevBatch, + String? olmAccount, + ) async { + await _clientBox.put('homeserver_url', homeserverUrl); + await _clientBox.put('token', token); + await _clientBox.put('user_id', userId); + await _clientBox.put('device_id', deviceId); + await _clientBox.put('device_name', deviceName); + await _clientBox.put('prev_batch', prevBatch); + await _clientBox.put('olm_account', olmAccount); + return; + } + + @override + Future updateClientKeys( + String olmAccount, + ) async { + await _clientBox.put('olm_account', olmAccount); + return; + } + + @override + Future updateInboundGroupSessionAllowedAtIndex( + String allowedAtIndex, String roomId, String sessionId) async { + final raw = await _inboundGroupSessionsBox.get(sessionId.toHiveKey); + if (raw == null) { + Logs().w( + 'Tried to update inbound group session as uploaded which wasnt found in the database!'); + return; + } + raw['allowed_at_index'] = allowedAtIndex; + await _inboundGroupSessionsBox.put(sessionId.toHiveKey, raw); + return; + } + + @override + Future updateInboundGroupSessionIndexes( + String indexes, String roomId, String sessionId) async { + final raw = await _inboundGroupSessionsBox.get(sessionId.toHiveKey); + if (raw == null) { + Logs().w( + 'Tried to update inbound group session indexes of a session which was not found in the database!'); + return; + } + raw['indexes'] = indexes; + await _inboundGroupSessionsBox.put(sessionId.toHiveKey, raw); + return; + } + + @override + Future updateRoomSortOrder( + double oldestSortOrder, double newestSortOrder, String roomId) async { + final raw = await _roomsBox.get(roomId.toHiveKey); + raw['oldest_sort_order'] = oldestSortOrder; + raw['newest_sort_order'] = newestSortOrder; + await _roomsBox.put(roomId.toHiveKey, raw); + return; + } + + @override + Future> getAllInboundGroupSessions() async { + final rawSessions = await Future.wait(_inboundGroupSessionsBox.keys + .map((key) => _inboundGroupSessionsBox.get(key))); + return rawSessions + .map((raw) => StoredInboundGroupSession.fromJson(convertToJson(raw))) + .toList(); + } + + @override + Future addSeenDeviceId( + String userId, + String deviceId, + String publicKeysHash, + ) => + _seenDeviceIdsBox.put( + MultiKey(userId, deviceId).toString(), publicKeysHash); + + @override + Future addSeenPublicKey( + String publicKey, + String deviceId, + ) => + _seenDeviceKeysBox.put(publicKey.toHiveKey, deviceId); + + @override + Future deviceIdSeen(userId, deviceId) async { + final raw = + await _seenDeviceIdsBox.get(MultiKey(userId, deviceId).toString()); + if (raw == null) return null; + return raw as String; + } + + @override + Future publicKeySeen(String publicKey) async { + final raw = await _seenDeviceKeysBox.get(publicKey.toHiveKey); + if (raw == null) return null; + return raw as String; + } +} + +dynamic _castValue(dynamic value) { + if (value is Map) { + return convertToJson(value); + } + if (value is List) { + return value.map(_castValue).toList(); + } + return value; +} + +/// Hive always gives back an `_InternalLinkedHasMap`. This +/// creates a deep copy of the json and makes sure that the format is always +/// `Map`. +Map convertToJson(Map map) { + final copy = Map.from(map); + for (final entry in copy.entries) { + copy[entry.key] = _castValue(entry.value); + } + return copy; +} + +class MultiKey { + final List parts; + + MultiKey(String key1, [String? key2, String? key3]) + : parts = [ + key1, + if (key2 != null) key2, + if (key3 != null) key3, + ]; + + const MultiKey.byParts(this.parts); + + MultiKey.fromString(String multiKeyString) + : parts = multiKeyString.split('|').map((s) => s.fromHiveKey).toList(); + + @override + String toString() => parts.map((s) => s.toHiveKey).join('|'); + + @override + bool operator ==(other) => parts.toString() == other.toString(); +} + +extension HiveKeyExtension on String { + String get toHiveKey => isValidMatrixId + ? '$sigil${Uri.encodeComponent(localpart!)}:${Uri.encodeComponent(domain!)}' + : Uri.encodeComponent(this); +} + +extension FromHiveKeyExtension on String { + String get fromHiveKey => Uri.decodeComponent(this); +} diff --git a/lib/src/event.dart b/lib/src/event.dart new file mode 100644 index 0000000..b8979b4 --- /dev/null +++ b/lib/src/event.dart @@ -0,0 +1,792 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:convert'; +import 'dart:typed_data'; + +import 'package:http/http.dart' as http; + +import '../matrix.dart'; +import 'utils/event_localizations.dart'; +import 'utils/html_to_text.dart'; + +abstract class RelationshipTypes { + static const String reply = 'm.in_reply_to'; + static const String edit = 'm.replace'; + static const String reaction = 'm.annotation'; +} + +/// All data exchanged over Matrix is expressed as an "event". Typically each client action (e.g. sending a message) correlates with exactly one event. +class Event extends MatrixEvent { + User get sender => room.getUserByMXIDSync(senderId); + + @Deprecated('Use [originServerTs] instead') + DateTime get time => originServerTs; + + @Deprecated('Use [type] instead') + String get typeKey => type; + + @Deprecated('Use [sender.calcDisplayname()] instead') + String? get senderName => sender.calcDisplayname(); + + /// The room this event belongs to. May be null. + final Room room; + + /// The status of this event. + EventStatus status; + + static const EventStatus defaultStatus = EventStatus.synced; + + /// Optional. The event that redacted this event, if any. Otherwise null. + Event? get redactedBecause { + final redacted_because = unsigned?['redacted_because']; + final room = this.room; + return (redacted_because is Map) + ? Event.fromJson(redacted_because, room) + : null; + } + + bool get redacted => redactedBecause != null; + + User? get stateKeyUser => room.getUserByMXIDSync(stateKey!); + + Event({ + this.status = defaultStatus, + required Map content, + required String type, + required String eventId, + required String senderId, + required DateTime originServerTs, + Map? unsigned, + Map? prevContent, + String? stateKey, + required this.room, + }) : super( + content: content, + type: type, + eventId: eventId, + senderId: senderId, + originServerTs: originServerTs, + roomId: room.id, + ) { + this.eventId = eventId; + this.unsigned = unsigned; + // synapse unfortunately isn't following the spec and tosses the prev_content + // into the unsigned block. + // Currently we are facing a very strange bug in web which is impossible to debug. + // It may be because of this line so we put this in try-catch until we can fix it. + try { + this.prevContent = (prevContent != null && prevContent.isNotEmpty) + ? prevContent + : (unsigned != null && + unsigned.containsKey('prev_content') && + unsigned['prev_content'] is Map) + ? unsigned['prev_content'] + : null; + } catch (_) { + // A strange bug in dart web makes this crash + } + this.stateKey = stateKey; + + // Mark event as failed to send if status is `sending` and event is older + // than the timeout. This should not happen with the deprecated Moor + // database! + if (status.isSending && room.client.database != null) { + // Age of this event in milliseconds + final age = DateTime.now().millisecondsSinceEpoch - + originServerTs.millisecondsSinceEpoch; + + final room = this.room; + if (age > room.client.sendMessageTimeoutSeconds * 1000) { + // Update this event in database and open timelines + final json = toJson(); + json['unsigned'] ??= {}; + json['unsigned'][messageSendingStatusKey] = EventStatus.error.intValue; + room.client.handleSync( + SyncUpdate( + nextBatch: '', + rooms: RoomsUpdate( + join: { + room.id: JoinedRoomUpdate( + timeline: TimelineUpdate( + events: [MatrixEvent.fromJson(json)], + ), + ) + }, + ), + ), + ); + } + } + } + + static Map getMapFromPayload(dynamic payload) { + if (payload is String) { + try { + return json.decode(payload); + } catch (e) { + return {}; + } + } + if (payload is Map) return payload; + return {}; + } + + factory Event.fromMatrixEvent( + MatrixEvent matrixEvent, + Room room, { + EventStatus status = defaultStatus, + }) => + Event( + status: status, + content: matrixEvent.content, + type: matrixEvent.type, + eventId: matrixEvent.eventId, + senderId: matrixEvent.senderId, + originServerTs: matrixEvent.originServerTs, + unsigned: matrixEvent.unsigned, + prevContent: matrixEvent.prevContent, + stateKey: matrixEvent.stateKey, + room: room, + ); + + /// Get a State event from a table row or from the event stream. + factory Event.fromJson( + Map jsonPayload, + Room room, + ) { + final content = Event.getMapFromPayload(jsonPayload['content']); + final unsigned = Event.getMapFromPayload(jsonPayload['unsigned']); + final prevContent = Event.getMapFromPayload(jsonPayload['prev_content']); + return Event( + status: eventStatusFromInt(jsonPayload['status'] ?? + unsigned[messageSendingStatusKey] ?? + defaultStatus.intValue), + stateKey: jsonPayload['state_key'], + prevContent: prevContent, + content: content, + type: jsonPayload['type'], + eventId: jsonPayload['event_id'] ?? '', + senderId: jsonPayload['sender'], + originServerTs: jsonPayload.containsKey('origin_server_ts') + ? DateTime.fromMillisecondsSinceEpoch(jsonPayload['origin_server_ts']) + : DateTime.now(), + unsigned: unsigned, + room: room, + ); + } + + @override + Map toJson() { + final data = {}; + if (stateKey != null) data['state_key'] = stateKey; + if (prevContent?.isNotEmpty == true) { + data['prev_content'] = prevContent; + } + data['content'] = content; + data['type'] = type; + data['event_id'] = eventId; + data['room_id'] = roomId; + data['sender'] = senderId; + data['origin_server_ts'] = originServerTs.millisecondsSinceEpoch; + if (unsigned?.isNotEmpty == true) { + data['unsigned'] = unsigned; + } + return data; + } + + User get asUser => User.fromState( + // state key should always be set for member events + stateKey: stateKey!, + prevContent: prevContent, + content: content, + typeKey: type, + eventId: eventId, + roomId: roomId, + senderId: senderId, + originServerTs: originServerTs, + unsigned: unsigned, + room: room); + + String get messageType => type == EventTypes.Sticker + ? MessageTypes.Sticker + : (content['msgtype'] is String ? content['msgtype'] : MessageTypes.Text); + + void setRedactionEvent(Event redactedBecause) { + unsigned = { + 'redacted_because': redactedBecause.toJson(), + }; + prevContent = null; + final contentKeyWhiteList = []; + switch (type) { + case EventTypes.RoomMember: + contentKeyWhiteList.add('membership'); + break; + case EventTypes.RoomCreate: + contentKeyWhiteList.add('creator'); + break; + case EventTypes.RoomJoinRules: + contentKeyWhiteList.add('join_rule'); + break; + case EventTypes.RoomPowerLevels: + contentKeyWhiteList.add('ban'); + contentKeyWhiteList.add('events'); + contentKeyWhiteList.add('events_default'); + contentKeyWhiteList.add('kick'); + contentKeyWhiteList.add('redact'); + contentKeyWhiteList.add('state_default'); + contentKeyWhiteList.add('users'); + contentKeyWhiteList.add('users_default'); + break; + case EventTypes.RoomAliases: + contentKeyWhiteList.add('aliases'); + break; + case EventTypes.HistoryVisibility: + contentKeyWhiteList.add('history_visibility'); + break; + default: + break; + } + content.removeWhere((k, v) => !contentKeyWhiteList.contains(k)); + } + + /// Returns the body of this event if it has a body. + String get text => content['body'] is String ? content['body'] : ''; + + /// Returns the formatted boy of this event if it has a formatted body. + String get formattedText => + content['formatted_body'] is String ? content['formatted_body'] : ''; + + /// Use this to get the body. + String get body { + if (redacted) return 'Redacted'; + if (text != '') return text; + if (formattedText != '') return formattedText; + return '$type'; + } + + /// Use this to get a plain-text representation of the event, stripping things + /// like spoilers and thelike. Useful for plain text notifications. + String get plaintextBody => content['format'] == 'org.matrix.custom.html' + ? HtmlToText.convert(formattedText) + : body; + + /// Returns a list of [Receipt] instances for this event. + List get receipts { + final room = this.room; + final receipt = room.roomAccountData['m.receipt']; + if (receipt == null) return []; + return receipt.content.entries + .where((entry) => entry.value['event_id'] == eventId) + .map((entry) => Receipt(room.getUserByMXIDSync(entry.key), + DateTime.fromMillisecondsSinceEpoch(entry.value['ts']))) + .toList(); + } + + /// Removes this event if the status is [sending], [error] or [removed]. + /// This event will just be removed from the database and the timelines. + /// Returns [false] if not removed. + Future remove() async { + final room = this.room; + + if (!status.isSent) { + await room.client.database?.removeEvent(eventId, room.id); + + room.client.onEvent.add(EventUpdate( + roomID: room.id, + type: EventUpdateType.timeline, + content: { + 'event_id': eventId, + 'status': EventStatus.removed.intValue, + 'content': {'body': 'Removed...'} + }, + )); + return true; + } + return false; + } + + /// Try to send this event again. Only works with events of status -1. + Future sendAgain({String? txid}) async { + if (!status.isError) return null; + // we do not remove the event here. It will automatically be updated + // in the `sendEvent` method to transition -1 -> 0 -> 1 -> 2 + final newEventId = await room.sendEvent( + content, + txid: txid ?? unsigned?['transaction_id'] ?? eventId, + ); + return newEventId; + } + + /// Whether the client is allowed to redact this event. + bool get canRedact => senderId == room.client.userID || room.canRedact; + + /// Redacts this event. Throws `ErrorResponse` on error. + Future redactEvent({String? reason, String? txid}) async => + await room.redactEvent(eventId, reason: reason, txid: txid); + + /// Searches for the reply event in the given timeline. + Future getReplyEvent(Timeline timeline) async { + if (relationshipType != RelationshipTypes.reply) return null; + final relationshipEventId = this.relationshipEventId; + return relationshipEventId == null + ? null + : await timeline.getEventById(relationshipEventId); + } + + /// If this event is encrypted and the decryption was not successful because + /// the session is unknown, this requests the session key from other devices + /// in the room. If the event is not encrypted or the decryption failed because + /// of a different error, this throws an exception. + Future requestKey() async { + if (type != EventTypes.Encrypted || + messageType != MessageTypes.BadEncrypted || + content['can_request_session'] != true) { + throw ('Session key not requestable'); + } + await room.requestSessionKey(content['session_id'], content['sender_key']); + return; + } + + /// Gets the info map of file events, or a blank map if none present + Map get infoMap => + content['info'] is Map ? content['info'] : {}; + + /// Gets the thumbnail info map of file events, or a blank map if nonepresent + Map get thumbnailInfoMap => infoMap['thumbnail_info'] is Map + ? infoMap['thumbnail_info'] + : {}; + + /// Returns if a file event has an attachment + bool get hasAttachment => content['url'] is String || content['file'] is Map; + + /// Returns if a file event has a thumbnail + bool get hasThumbnail => + infoMap['thumbnail_url'] is String || infoMap['thumbnail_file'] is Map; + + /// Returns if a file events attachment is encrypted + bool get isAttachmentEncrypted => content['file'] is Map; + + /// Returns if a file events thumbnail is encrypted + bool get isThumbnailEncrypted => infoMap['thumbnail_file'] is Map; + + /// Gets the mimetype of the attachment of a file event, or a blank string if not present + String get attachmentMimetype => infoMap['mimetype'] is String + ? infoMap['mimetype'].toLowerCase() + : (content['file'] is Map && content['file']['mimetype'] is String + ? content['file']['mimetype'] + : ''); + + /// Gets the mimetype of the thumbnail of a file event, or a blank string if not present + String get thumbnailMimetype => thumbnailInfoMap['mimetype'] is String + ? thumbnailInfoMap['mimetype'].toLowerCase() + : (infoMap['thumbnail_file'] is Map && + infoMap['thumbnail_file']['mimetype'] is String + ? infoMap['thumbnail_file']['mimetype'] + : ''); + + /// Gets the underlying mxc url of an attachment of a file event, or null if not present + Uri? get attachmentMxcUrl { + final url = isAttachmentEncrypted ? content['file']['url'] : content['url']; + return url is String ? Uri.tryParse(url) : null; + } + + /// Gets the underlying mxc url of a thumbnail of a file event, or null if not present + Uri? get thumbnailMxcUrl { + final url = isThumbnailEncrypted + ? infoMap['thumbnail_file']['url'] + : infoMap['thumbnail_url']; + return url is String ? Uri.tryParse(url) : null; + } + + /// Gets the mxc url of an attachment/thumbnail of a file event, taking sizes into account, or null if not present + Uri? attachmentOrThumbnailMxcUrl({bool getThumbnail = false}) { + if (getThumbnail && + infoMap['size'] is int && + thumbnailInfoMap['size'] is int && + infoMap['size'] <= thumbnailInfoMap['size']) { + getThumbnail = false; + } + if (getThumbnail && !hasThumbnail) { + getThumbnail = false; + } + return getThumbnail ? thumbnailMxcUrl : attachmentMxcUrl; + } + + // size determined from an approximate 800x800 jpeg thumbnail with method=scale + static const _minNoThumbSize = 80 * 1024; + + /// Gets the attachment https URL to display in the timeline, taking into account if the original image is tiny. + /// Returns null for encrypted rooms, if the image can't be fetched via http url or if the event does not contain an attachment. + /// Set [getThumbnail] to true to fetch the thumbnail, set [width], [height] and [method] + /// for the respective thumbnailing properties. + /// [minNoThumbSize] is the minimum size that an original image may be to not fetch its thumbnail, defaults to 80k + /// [useThumbnailMxcUrl] says weather to use the mxc url of the thumbnail, rather than the original attachment. + /// [animated] says weather the thumbnail is animated + Uri? getAttachmentUrl( + {bool getThumbnail = false, + bool useThumbnailMxcUrl = false, + double width = 800.0, + double height = 800.0, + ThumbnailMethod method = ThumbnailMethod.scale, + int minNoThumbSize = _minNoThumbSize, + bool animated = false}) { + if (![EventTypes.Message, EventTypes.Sticker].contains(type) || + !hasAttachment || + isAttachmentEncrypted) { + return null; // can't url-thumbnail in encrypted rooms + } + if (useThumbnailMxcUrl && !hasThumbnail) { + return null; // can't fetch from thumbnail + } + final thisInfoMap = useThumbnailMxcUrl ? thumbnailInfoMap : infoMap; + final thisMxcUrl = + useThumbnailMxcUrl ? infoMap['thumbnail_url'] : content['url']; + // if we have as method scale, we can return safely the original image, should it be small enough + if (getThumbnail && + method == ThumbnailMethod.scale && + thisInfoMap['size'] is int && + thisInfoMap['size'] < minNoThumbSize) { + getThumbnail = false; + } + // now generate the actual URLs + if (getThumbnail) { + return Uri.parse(thisMxcUrl).getThumbnail( + room.client, + width: width, + height: height, + method: method, + animated: animated, + ); + } else { + return Uri.parse(thisMxcUrl).getDownloadLink(room.client); + } + } + + /// Returns if an attachment is in the local store + Future isAttachmentInLocalStore({bool getThumbnail = false}) async { + if (![EventTypes.Message, EventTypes.Sticker].contains(type)) { + throw ("This event has the type '$type' and so it can't contain an attachment."); + } + final mxcUrl = attachmentOrThumbnailMxcUrl(getThumbnail: getThumbnail); + if (mxcUrl == null) { + throw "This event hasn't any attachment or thumbnail."; + } + getThumbnail = mxcUrl != attachmentMxcUrl; + // Is this file storeable? + final thisInfoMap = getThumbnail ? thumbnailInfoMap : infoMap; + final database = room.client.database; + if (database == null) { + return false; + } + + final storeable = thisInfoMap['size'] is int && + thisInfoMap['size'] <= database.maxFileSize; + + Uint8List? uint8list; + if (storeable) { + uint8list = await database.getFile(mxcUrl); + } + return uint8list != null; + } + + /// Downloads (and decrypts if necessary) the attachment of this + /// event and returns it as a [MatrixFile]. If this event doesn't + /// contain an attachment, this throws an error. Set [getThumbnail] to + /// true to download the thumbnail instead. + Future downloadAndDecryptAttachment( + {bool getThumbnail = false, + Future Function(Uri)? downloadCallback}) async { + if (![EventTypes.Message, EventTypes.Sticker].contains(type)) { + throw ("This event has the type '$type' and so it can't contain an attachment."); + } + final database = room.client.database; + final mxcUrl = attachmentOrThumbnailMxcUrl(getThumbnail: getThumbnail); + if (mxcUrl == null) { + throw "This event hasn't any attachment or thumbnail."; + } + getThumbnail = mxcUrl != attachmentMxcUrl; + final isEncrypted = + getThumbnail ? isThumbnailEncrypted : isAttachmentEncrypted; + if (isEncrypted && !room.client.encryptionEnabled) { + throw ('Encryption is not enabled in your Client.'); + } + + // Is this file storeable? + final thisInfoMap = getThumbnail ? thumbnailInfoMap : infoMap; + var storeable = database != null && + thisInfoMap['size'] is int && + thisInfoMap['size'] <= database.maxFileSize; + + Uint8List? uint8list; + if (storeable) { + uint8list = await room.client.database?.getFile(mxcUrl); + } + + // Download the file + if (uint8list == null) { + downloadCallback ??= (Uri url) async => (await http.get(url)).bodyBytes; + uint8list = await downloadCallback(mxcUrl.getDownloadLink(room.client)); + storeable = database != null && + storeable && + uint8list.lengthInBytes < database.maxFileSize; + if (storeable) { + await database.storeFile( + mxcUrl, uint8list, DateTime.now().millisecondsSinceEpoch); + } + } + + // Decrypt the file + if (isEncrypted) { + final fileMap = + getThumbnail ? infoMap['thumbnail_file'] : content['file']; + if (!fileMap['key']['key_ops'].contains('decrypt')) { + throw ("Missing 'decrypt' in 'key_ops'."); + } + final encryptedFile = EncryptedFile( + data: uint8list, + iv: fileMap['iv'], + k: fileMap['key']['k'], + sha256: fileMap['hashes']['sha256'], + ); + uint8list = await room.client.runInBackground( + decryptFile, encryptedFile); + if (uint8list == null) { + throw ('Unable to decrypt file'); + } + } + return MatrixFile(bytes: uint8list, name: body); + } + + /// Returns if this is a known event type. + bool get isEventTypeKnown => + EventLocalizations.localizationsMap.containsKey(type); + + /// Returns a localized String representation of this event. For a + /// room list you may find [withSenderNamePrefix] useful. Set [hideReply] to + /// crop all lines starting with '>'. With [plaintextBody] it'll use the + /// plaintextBody instead of the normal body. + String getLocalizedBody( + MatrixLocalizations i18n, { + bool withSenderNamePrefix = false, + bool hideReply = false, + bool hideEdit = false, + bool plaintextBody = false, + }) { + if (redacted) { + return i18n.removedBy(redactedBecause?.sender.calcDisplayname() ?? ''); + } + var body = plaintextBody ? this.plaintextBody : this.body; + + // we need to know if the message is an html message to be able to determine + // if we need to strip the reply fallback. + var htmlMessage = content['format'] != 'org.matrix.custom.html'; + // If we have an edit, we want to operate on the new content + if (hideEdit && + relationshipType == RelationshipTypes.edit && + content.tryGet>('m.new_content') != null) { + if (plaintextBody && + content['m.new_content']['format'] == 'org.matrix.custom.html') { + htmlMessage = true; + body = HtmlToText.convert( + (content['m.new_content'] as Map) + .tryGet('formatted_body') ?? + formattedText); + } else { + htmlMessage = false; + body = (content['m.new_content'] as Map) + .tryGet('body') ?? + body; + } + } + // Hide reply fallback + // Be sure that the plaintextBody already stripped teh reply fallback, + // if the message is formatted + if (hideReply && (!plaintextBody || htmlMessage)) { + body = body.replaceFirst( + RegExp(r'^>( \*)? <[^>]+>[^\n\r]+\r?\n(> [^\n]*\r?\n)*\r?\n'), ''); + } + final callback = EventLocalizations.localizationsMap[type]; + var localizedBody = i18n.unknownEvent(type); + if (callback != null) { + localizedBody = callback(this, i18n, body); + } + + // Add the sender name prefix + if (withSenderNamePrefix && + type == EventTypes.Message && + textOnlyMessageTypes.contains(messageType)) { + final senderNameOrYou = senderId == room.client.userID + ? i18n.you + : (sender.calcDisplayname()); + localizedBody = '$senderNameOrYou: $localizedBody'; + } + + return localizedBody; + } + + static const Set textOnlyMessageTypes = { + MessageTypes.Text, + MessageTypes.Notice, + MessageTypes.Emote, + MessageTypes.None, + }; + + /// returns if this event matches the passed event or transaction id + bool matchesEventOrTransactionId(String? search) { + if (search == null) { + return false; + } + if (eventId == search) { + return true; + } + return unsigned?['transaction_id'] == search; + } + + /// Get the relationship type of an event. `null` if there is none + String? get relationshipType { + if (content.tryGet>('m.relates_to') == null) { + return null; + } + if (content['m.relates_to'].containsKey('m.in_reply_to')) { + return RelationshipTypes.reply; + } + return content + .tryGet>('m.relates_to') + ?.tryGet('rel_type'); + } + + /// Get the event ID that this relationship will reference. `null` if there is none + String? get relationshipEventId { + if (!(content['m.relates_to'] is Map)) { + return null; + } + if (content['m.relates_to'].containsKey('event_id')) { + return content['m.relates_to']['event_id']; + } + if (content['m.relates_to']['m.in_reply_to'] is Map && + content['m.relates_to']['m.in_reply_to'].containsKey('event_id')) { + return content['m.relates_to']['m.in_reply_to']['event_id']; + } + return null; + } + + /// Get whether this event has aggregated events from a certain [type] + /// To be able to do that you need to pass a [timeline] + bool hasAggregatedEvents(Timeline timeline, String type) => + timeline.aggregatedEvents[eventId]?.containsKey(type) == true; + + /// Get all the aggregated event objects for a given [type]. To be able to do this + /// you have to pass a [timeline] + Set aggregatedEvents(Timeline timeline, String type) => + timeline.aggregatedEvents[eventId]?[type] ?? {}; + + /// Fetches the event to be rendered, taking into account all the edits and the like. + /// It needs a [timeline] for that. + Event getDisplayEvent(Timeline timeline) { + if (redacted) { + return this; + } + if (hasAggregatedEvents(timeline, RelationshipTypes.edit)) { + // alright, we have an edit + final allEditEvents = aggregatedEvents(timeline, RelationshipTypes.edit) + // we only allow edits made by the original author themself + .where((e) => e.senderId == senderId && e.type == EventTypes.Message) + .toList(); + // we need to check again if it isn't empty, as we potentially removed all + // aggregated edits + if (allEditEvents.isNotEmpty) { + allEditEvents.sort((a, b) => a.originServerTs.millisecondsSinceEpoch - + b.originServerTs.millisecondsSinceEpoch > + 0 + ? 1 + : -1); + final rawEvent = allEditEvents.last.toJson(); + // update the content of the new event to render + if (rawEvent['content']['m.new_content'] is Map) { + rawEvent['content'] = rawEvent['content']['m.new_content']; + } + return Event.fromJson(rawEvent, room); + } + } + return this; + } + + /// returns if a message is a rich message + bool get isRichMessage => + content['format'] == 'org.matrix.custom.html' && + content['formatted_body'] is String; + + // regexes to fetch the number of emotes, including emoji, and if the message consists of only those + // to match an emoji we can use the following regex: + // (?:\x{00a9}|\x{00ae}|[\x{2600}-\x{27bf}]|[\x{2b00}-\x{2bff}]|\x{d83c}[\x{d000}-\x{dfff}]|\x{d83d}[\x{d000}-\x{dfff}]|\x{d83e}[\x{d000}-\x{dfff}])[\x{fe00}-\x{fe0f}]? + // we need to replace \x{0000} with \u0000, the comment is left in the other format to be able to paste into regex101.com + // to see if there is a custom emote, we use the following regex: ]+data-mx-(?:emote|emoticon)(?==|>|\s)[^>]*> + // now we combind the two to have four regexes: + // 1. are there only emoji, or whitespace + // 2. are there only emoji, emotes, or whitespace + // 3. count number of emoji + // 4- count number of emoji or emotes + static final RegExp _onlyEmojiRegex = RegExp( + r'^((?:\u00a9|\u00ae|[\u2600-\u27bf]|[\u2b00-\u2bff]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])[\ufe00-\ufe0f]?|\s)*$', + caseSensitive: false, + multiLine: false); + static final RegExp _onlyEmojiEmoteRegex = RegExp( + r'^((?:\u00a9|\u00ae|[\u2600-\u27bf]|[\u2b00-\u2bff]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])[\ufe00-\ufe0f]?|]+data-mx-(?:emote|emoticon)(?==|>|\s)[^>]*>|\s)*$', + caseSensitive: false, + multiLine: false); + static final RegExp _countEmojiRegex = RegExp( + r'((?:\u00a9|\u00ae|[\u2600-\u27bf]|[\u2b00-\u2bff]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])[\ufe00-\ufe0f]?)', + caseSensitive: false, + multiLine: false); + static final RegExp _countEmojiEmoteRegex = RegExp( + r'((?:\u00a9|\u00ae|[\u2600-\u27bf]|[\u2b00-\u2bff]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])[\ufe00-\ufe0f]?|]+data-mx-(?:emote|emoticon)(?==|>|\s)[^>]*>)', + caseSensitive: false, + multiLine: false); + + /// Returns if a given event only has emotes, emojis or whitespace as content. + /// If the body contains a reply then it is stripped. + /// This is useful to determine if stand-alone emotes should be displayed bigger. + bool get onlyEmotes { + if (isRichMessage) { + final formattedTextStripped = formattedText.replaceAll( + RegExp('.*<\/mx-reply>', + caseSensitive: false, multiLine: false, dotAll: true), + ''); + return _onlyEmojiEmoteRegex.hasMatch(formattedTextStripped); + } else { + return _onlyEmojiRegex.hasMatch(plaintextBody); + } + } + + /// Gets the number of emotes in a given message. This is useful to determine + /// if the emotes should be displayed bigger. + /// If the body contains a reply then it is stripped. + /// WARNING: This does **not** test if there are only emotes. Use `event.onlyEmotes` for that! + int get numberEmotes { + if (isRichMessage) { + final formattedTextStripped = formattedText.replaceAll( + RegExp('.*<\/mx-reply>', + caseSensitive: false, multiLine: false, dotAll: true), + ''); + return _countEmojiEmoteRegex.allMatches(formattedTextStripped).length; + } else { + return _countEmojiRegex.allMatches(plaintextBody).length; + } + } +} diff --git a/lib/src/event_status.dart b/lib/src/event_status.dart new file mode 100644 index 0000000..9bec269 --- /dev/null +++ b/lib/src/event_status.dart @@ -0,0 +1,70 @@ +/// Defines event status: +/// - removed +/// - error: (http request failed) +/// - sending: (http request started) +/// - sent: (http request successful) +/// - synced: (event came from sync loop) +/// - roomState +enum EventStatus { + removed, + error, + sending, + sent, + synced, + roomState, +} + +/// Returns `EventStatusEnum` value from `intValue`. +/// +/// - -2 == removed; +/// - -1 == error; +/// - 0 == sending; +/// - 1 == sent; +/// - 2 == synced; +/// - 3 == roomState; +EventStatus eventStatusFromInt(int intValue) => + EventStatus.values[intValue + 2]; + +/// Takes two [EventStatus] values and returns the one with higher +/// (better in terms of message sending) status. +EventStatus latestEventStatus(EventStatus status1, EventStatus status2) => + status1.intValue > status2.intValue ? status1 : status2; + +extension EventStatusExtension on EventStatus { + /// Returns int value of the event status. + /// + /// - -2 == removed; + /// - -1 == error; + /// - 0 == sending; + /// - 1 == sent; + /// - 2 == synced; + /// - 3 == roomState; + int get intValue => (index - 2); + + /// Return `true` if the `EventStatus` equals `removed`. + bool get isRemoved => this == EventStatus.removed; + + /// Return `true` if the `EventStatus` equals `error`. + bool get isError => this == EventStatus.error; + + /// Return `true` if the `EventStatus` equals `sending`. + bool get isSending => this == EventStatus.sending; + + /// Return `true` if the `EventStatus` equals `roomState`. + bool get isRoomState => this == EventStatus.roomState; + + /// Returns `true` if the status is sent or later: + /// [EventStatus.sent], [EventStatus.synced] or [EventStatus.roomState]. + bool get isSent => [ + EventStatus.sent, + EventStatus.synced, + EventStatus.roomState + ].contains(this); + + /// Returns `true` if the status is `synced` or `roomState`: + /// [EventStatus.synced] or [EventStatus.roomState]. + bool get isSynced => [ + EventStatus.synced, + EventStatus.roomState, + ].contains(this); +} diff --git a/lib/src/room.dart b/lib/src/room.dart new file mode 100644 index 0000000..eea5385 --- /dev/null +++ b/lib/src/room.dart @@ -0,0 +1,2100 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:async'; +import 'dart:convert'; +import 'dart:typed_data'; + +import 'package:collection/collection.dart'; +import 'package:html_unescape/html_unescape.dart'; +import 'package:matrix/src/utils/crypto/crypto.dart'; +import 'package:matrix/src/utils/space_child.dart'; +import 'package:matrix/widget.dart'; + +import '../matrix.dart'; +import 'utils/markdown.dart'; +import 'utils/marked_unread.dart'; + +/// https://github.com/matrix-org/matrix-doc/pull/2746 +/// version 1 +const String voipProtoVersion = '1'; + +enum PushRuleState { notify, mentionsOnly, dontNotify } +enum JoinRules { public, knock, invite, private } +enum GuestAccess { canJoin, forbidden } +enum HistoryVisibility { invited, joined, shared, worldReadable } + +const Map _guestAccessMap = { + GuestAccess.canJoin: 'can_join', + GuestAccess.forbidden: 'forbidden', +}; + +const Map _historyVisibilityMap = { + HistoryVisibility.invited: 'invited', + HistoryVisibility.joined: 'joined', + HistoryVisibility.shared: 'shared', + HistoryVisibility.worldReadable: 'world_readable', +}; + +const String messageSendingStatusKey = + 'com.famedly.famedlysdk.message_sending_status'; + +const String sortOrderKey = 'com.famedly.famedlysdk.sort_order'; + +/// Represents a Matrix room. +class Room { + /// The full qualified Matrix ID for the room in the format '!localid:server.abc'. + final String id; + + /// Membership status of the user for this room. + Membership membership; + + /// The count of unread notifications. + int notificationCount; + + /// The count of highlighted notifications. + int highlightCount; + + /// A token that can be supplied to the from parameter of the rooms/{roomId}/messages endpoint. + String? prev_batch; + + RoomSummary summary; + + @deprecated + List? get mHeroes => summary.mHeroes; + + @deprecated + int? get mJoinedMemberCount => summary.mJoinedMemberCount; + + @deprecated + int? get mInvitedMemberCount => summary.mInvitedMemberCount; + + /// The room states are a key value store of the key (`type`,`state_key`) => State(event). + /// In a lot of cases the `state_key` might be an empty string. You **should** use the + /// methods `getState()` and `setState()` to interact with the room states. + Map> states = {}; + + /// Key-Value store for ephemerals. + Map ephemerals = {}; + + /// Key-Value store for private account data only visible for this user. + Map roomAccountData = {}; + + Map toJson() => { + 'id': id, + 'membership': membership.toString().split('.').last, + 'highlight_count': highlightCount, + 'notification_count': notificationCount, + 'prev_batch': prev_batch, + 'summary': summary.toJson(), + 'newest_sort_order': 0, + 'oldest_sort_order': 0, + }; + + factory Room.fromJson(Map json, Client client) => Room( + client: client, + id: json['id'], + membership: Membership.values.singleWhere( + (m) => m.toString() == 'Membership.${json['membership']}', + orElse: () => Membership.join, + ), + notificationCount: json['notification_count'], + highlightCount: json['highlight_count'], + prev_batch: json['prev_batch'], + summary: + RoomSummary.fromJson(Map.from(json['summary'])), + newestSortOrder: json['newest_sort_order'].toDouble(), + oldestSortOrder: json['oldest_sort_order'].toDouble(), + ); + + /// Flag if the room is partial, meaning not all state events have been loaded yet + bool partial = true; + + /// Post-loads the room. + /// This load all the missing state events for the room from the database + /// If the room has already been loaded, this does nothing. + Future postLoad() async { + if (!partial) { + return; + } + final allStates = await client.database + ?.getUnimportantRoomEventStatesForRoom( + client.importantStateEvents.toList(), this); + + if (allStates != null) { + for (final state in allStates) { + setState(state); + } + } + partial = false; + } + + /// Returns the [Event] for the given [typeKey] and optional [stateKey]. + /// If no [stateKey] is provided, it defaults to an empty string. + Event? getState(String typeKey, [String stateKey = '']) => + states[typeKey]?[stateKey]; + + /// Adds the [state] to this room and overwrites a state with the same + /// typeKey/stateKey key pair if there is one. + void setState(Event state) { + // Decrypt if necessary + if (state.type == EventTypes.Encrypted && client.encryptionEnabled) { + try { + state = client.encryption?.decryptRoomEventSync(id, state) ?? state; + } catch (e, s) { + Logs().e('[LibOlm] Could not decrypt room state', e, s); + } + } + + // We ignore room verification events for lastEvents + if (state.type == EventTypes.Message && + state.messageType.startsWith('m.room.verification.')) { + return; + } + + final isMessageEvent = client.roomPreviewLastEvents.contains(state.type); + + // We ignore events editing events older than the current-latest here so + // i.e. newly sent edits for older events don't show up in room preview + final lastEvent = this.lastEvent; + if (isMessageEvent && + state.relationshipEventId != null && + state.relationshipType == RelationshipTypes.edit && + lastEvent != null && + !state.matchesEventOrTransactionId(lastEvent.eventId) && + lastEvent.eventId != state.relationshipEventId && + !(lastEvent.relationshipType == RelationshipTypes.edit && + lastEvent.relationshipEventId == state.relationshipEventId)) { + return; + } + + // Ignore other non-state events + final stateKey = isMessageEvent ? '' : state.stateKey; + final roomId = state.roomId; + if (stateKey == null || roomId == null) { + return; + } + + // Do not set old events as state events + final prevEvent = getState(state.type, stateKey); + if (prevEvent != null && + prevEvent.eventId != state.eventId && + prevEvent.originServerTs.millisecondsSinceEpoch > + state.originServerTs.millisecondsSinceEpoch) { + return; + } + + (states[state.type] ??= {})[stateKey] = state; + } + + /// ID of the fully read marker event. + String get fullyRead => + roomAccountData['m.fully_read']?.content['event_id'] ?? ''; + + /// If something changes, this callback will be triggered. Will return the + /// room id. + final StreamController onUpdate = StreamController.broadcast(); + + /// If there is a new session key received, this will be triggered with + /// the session ID. + final StreamController onSessionKeyReceived = + StreamController.broadcast(); + + /// The name of the room if set by a participant. + String get name { + final n = getState(EventTypes.RoomName)?.content['name']; + return (n is String) ? n : ''; + } + + /// The pinned events for this room. If there are none this returns an empty + /// list. + List get pinnedEventIds { + final pinned = getState(EventTypes.RoomPinnedEvents)?.content['pinned']; + return pinned is Iterable ? pinned.map((e) => e.toString()).toList() : []; + } + + /// Returns a localized displayname for this server. If the room is a groupchat + /// without a name, then it will return the localized version of 'Group with Alice' instead + /// of just 'Alice' to make it different to a direct chat. + /// Empty chats will become the localized version of 'Empty Chat'. + /// This method requires a localization class which implements [MatrixLocalizations] + String getLocalizedDisplayname(MatrixLocalizations i18n) { + if (name.isEmpty && + canonicalAlias.isEmpty && + !isDirectChat && + (summary.mHeroes != null && summary.mHeroes?.isNotEmpty == true)) { + return i18n.groupWith(displayname); + } + if (displayname.isNotEmpty) { + return displayname; + } + return i18n.emptyChat; + } + + /// The topic of the room if set by a participant. + String get topic { + final t = getState(EventTypes.RoomTopic)?.content['topic']; + return t is String ? t : ''; + } + + /// The avatar of the room if set by a participant. + Uri? get avatar { + final avatarUrl = getState(EventTypes.RoomAvatar)?.content['url']; + if (avatarUrl is String) { + return Uri.tryParse(avatarUrl); + } + + final heroes = summary.mHeroes; + if (heroes != null && heroes.length == 1) { + final hero = getState(EventTypes.RoomMember, heroes.first); + if (hero != null) { + return hero.asUser.avatarUrl; + } + } + if (isDirectChat) { + final user = directChatMatrixID; + if (user != null) { + return getUserByMXIDSync(user).avatarUrl; + } + } + if (membership == Membership.invite) { + return getState(EventTypes.RoomMember, client.userID!)?.sender.avatarUrl; + } + return null; + } + + /// The address in the format: #roomname:homeserver.org. + String get canonicalAlias { + final alias = getState(EventTypes.RoomCanonicalAlias)?.content['alias']; + return (alias is String) ? alias : ''; + } + + /// Sets the canonical alias. If the [canonicalAlias] is not yet an alias of + /// this room, it will create one. + Future setCanonicalAlias(String canonicalAlias) async { + final aliases = await client.getLocalAliases(id); + if (!aliases.contains(canonicalAlias)) { + await client.setRoomAlias(canonicalAlias, id); + } + await client.setRoomStateWithKey(id, EventTypes.RoomCanonicalAlias, '', { + 'alias': canonicalAlias, + }); + } + + /// If this room is a direct chat, this is the matrix ID of the user. + /// Returns null otherwise. + String? get directChatMatrixID { + if (membership == Membership.invite) { + final invitation = getState(EventTypes.RoomMember, client.userID!); + if (invitation != null && invitation.content['is_direct'] == true) { + return invitation.senderId; + } + } + + return client.directChats.entries + .firstWhereOrNull((MapEntry e) { + final roomIds = e.value; + return roomIds is List && roomIds.contains(id); + })?.key; + } + + /// Wheither this is a direct chat or not + bool get isDirectChat => directChatMatrixID != null; + + /// Must be one of [all, mention] + String? notificationSettings; + + Event? get lastEvent { + // as lastEvent calculation is based on the state events we unfortunately cannot + // use sortOrder here: With many state events we just know which ones are the + // newest ones, without knowing in which order they actually happened. As such, + // using the origin_server_ts is the best guess for this algorithm. While not + // perfect, it is only used for the room preview in the room list and sorting + // said room list, so it should be good enough. + var lastTime = DateTime.fromMillisecondsSinceEpoch(0); + final lastEvents = + client.roomPreviewLastEvents.map(getState).whereType(); + + var lastEvent = lastEvents.isEmpty + ? null + : lastEvents.reduce((a, b) { + if (a.originServerTs == b.originServerTs) { + // if two events have the same sort order we want to give encrypted events a lower priority + // This is so that if the same event exists in the state both encrypted *and* unencrypted, + // the unencrypted one is picked + return a.type == EventTypes.Encrypted ? b : a; + } + return a.originServerTs.millisecondsSinceEpoch > + b.originServerTs.millisecondsSinceEpoch + ? a + : b; + }); + if (lastEvent == null) { + states.forEach((final String key, final entry) { + final state = entry['']; + if (state == null) return; + if (state.originServerTs.millisecondsSinceEpoch > + lastTime.millisecondsSinceEpoch) { + lastTime = state.originServerTs; + lastEvent = state; + } + }); + } + return lastEvent; + } + + /// Returns a list of all current typing users. + List get typingUsers { + final typingMxid = ephemerals['m.typing']?.content['user_ids']; + return (typingMxid is List) + ? typingMxid.cast().map(getUserByMXIDSync).toList() + : []; + } + + /// Returns all present Widgets in the room. + List get widgets => { + ...states['m.widget'] ?? {}, + ...states['im.vector.modular.widgets'] ?? {}, + }.values.expand((e) { + try { + return [MatrixWidget.fromJson(e.content, this)]; + } catch (_) { + return []; + } + }).toList(); + + /// Your current client instance. + final Client client; + + Room({ + required this.id, + this.membership = Membership.join, + this.notificationCount = 0, + this.highlightCount = 0, + this.prev_batch, + required this.client, + this.notificationSettings, + Map? roomAccountData, + double newestSortOrder = 0.0, + double oldestSortOrder = 0.0, + RoomSummary? summary, + }) : roomAccountData = roomAccountData ?? {}, + summary = summary ?? + RoomSummary.fromJson({ + 'm.joined_member_count': 0, + 'm.invited_member_count': 0, + 'm.heroes': [], + }); + + /// The default count of how much events should be requested when requesting the + /// history of this room. + static const int defaultHistoryCount = 30; + + /// Calculates the displayname. First checks if there is a name, then checks for a canonical alias and + /// then generates a name from the heroes. + String get displayname { + if (name.isNotEmpty) return name; + + final canonicalAlias = this.canonicalAlias.localpart; + if (canonicalAlias != null && canonicalAlias.isNotEmpty) { + return canonicalAlias; + } + + final heroes = summary.mHeroes; + if (heroes != null && heroes.isNotEmpty) { + return heroes + .where((hero) => hero.isNotEmpty) + .map((hero) => getUserByMXIDSync(hero).calcDisplayname()) + .join(', '); + } + if (isDirectChat) { + final user = directChatMatrixID; + if (user != null) { + return getUserByMXIDSync(user).calcDisplayname(); + } + } + if (membership == Membership.invite) { + final sender = getState(EventTypes.RoomMember, client.userID!) + ?.sender + .calcDisplayname(); + if (sender != null) return sender; + } + return 'Empty chat'; + } + + @Deprecated('Use [lastEvent.body] instead') + String get lastMessage => lastEvent?.body ?? ''; + + /// When the last message received. + DateTime get timeCreated => lastEvent?.originServerTs ?? DateTime.now(); + + /// Call the Matrix API to change the name of this room. Returns the event ID of the + /// new m.room.name event. + Future setName(String newName) => client.setRoomStateWithKey( + id, + EventTypes.RoomName, + '', + {'name': newName}, + ); + + /// Call the Matrix API to change the topic of this room. + Future setDescription(String newName) => client.setRoomStateWithKey( + id, + EventTypes.RoomTopic, + '', + {'topic': newName}, + ); + + /// Add a tag to the room. + Future addTag(String tag, {double? order}) => client.setRoomTag( + client.userID!, + id, + tag, + order: order, + ); + + /// Removes a tag from the room. + Future removeTag(String tag) => client.deleteRoomTag( + client.userID!, + id, + tag, + ); + + // Tag is part of client-to-server-API, so it uses strict parsing. + // For roomAccountData, permissive parsing is more suitable, + // so it is implemented here. + static Tag _tryTagFromJson(Object o) { + if (o is Map) { + return Tag( + order: o.tryGet('order', TryGet.silent)?.toDouble(), + additionalProperties: Map.from(o)..remove('order')); + } + return Tag(); + } + + /// Returns all tags for this room. + Map get tags { + final tags = roomAccountData['m.tag']?.content['tags']; + + if (tags is Map) { + final parsedTags = + tags.map((k, v) => MapEntry(k, _tryTagFromJson(v))); + parsedTags.removeWhere((k, v) => !TagType.isValid(k)); + return parsedTags; + } + + return {}; + } + + bool get markedUnread { + return MarkedUnread.fromJson( + roomAccountData[EventType.markedUnread]?.content ?? {}) + .unread; + } + + /// Checks if the last event has a read marker of the user. + /// Warning: This compares the origin server timestamp which might not map + /// to the real sort order of the timeline. + bool get hasNewMessages { + final lastEvent = this.lastEvent; + + // There is no known event or the last event is only a state fallback event, + // we assume there is no new messages. + if (lastEvent == null || + !client.roomPreviewLastEvents.contains(lastEvent.type)) return false; + + // Read marker is on the last event so no new messages. + if (lastEvent.receipts + .any((receipt) => receipt.user.senderId == client.userID!)) { + return false; + } + + // If the last event is sent, we mark the room as read. + if (lastEvent.senderId == client.userID) return false; + + // Get the timestamp of read marker and compare + final readAtMilliseconds = roomAccountData['m.receipt'] + ?.content + .tryGetMap(client.userID!) + ?.tryGet('ts') ?? + 0; + return readAtMilliseconds < lastEvent.originServerTs.millisecondsSinceEpoch; + } + + /// Returns true if this room is unread. To check if there are new messages + /// in muted rooms, use [hasNewMessages]. + bool get isUnread => notificationCount > 0 || markedUnread; + + @Deprecated('Use [markUnread] instead') + Future setUnread(bool unread) => markUnread(unread); + + /// Sets an unread flag manually for this room. This changes the local account + /// data model before syncing it to make sure + /// this works if there is no connection to the homeserver. This does **not** + /// set a read marker! + Future markUnread(bool unread) async { + final content = MarkedUnread(unread).toJson(); + await _handleFakeSync( + SyncUpdate( + nextBatch: '', + rooms: RoomsUpdate( + join: { + id: JoinedRoomUpdate( + accountData: [ + BasicRoomEvent( + content: content, + roomId: id, + type: EventType.markedUnread, + ), + ], + ) + }, + ), + ), + ); + await client.setAccountDataPerRoom( + client.userID!, + id, + EventType.markedUnread, + content, + ); + } + + /// Returns true if this room has a m.favourite tag. + bool get isFavourite => + tags[TagType.favourite] != null || + (client.pinInvitedRooms && membership == Membership.invite); + + /// Sets the m.favourite tag for this room. + Future setFavourite(bool favourite) => + favourite ? addTag(TagType.favourite) : removeTag(TagType.favourite); + + /// Call the Matrix API to change the pinned events of this room. + Future setPinnedEvents(List pinnedEventIds) => + client.setRoomStateWithKey( + id, + EventTypes.RoomPinnedEvents, + '', + {'pinned': pinnedEventIds}, + ); + + /// return all current emote packs for this room + @deprecated + Map> get emotePacks => + getImagePacksFlat(ImagePackUsage.emoticon); + + /// returns the resolved mxid for a mention string, or null if none found + String? getMention(String mention) => getParticipants() + .firstWhereOrNull((u) => u.mentionFragments.contains(mention)) + ?.id; + + /// Sends a normal text message to this room. Returns the event ID generated + /// by the server for this message. + Future sendTextEvent(String message, + {String? txid, + Event? inReplyTo, + String? editEventId, + bool parseMarkdown = true, + @deprecated Map>? emotePacks, + bool parseCommands = true, + String msgtype = MessageTypes.Text}) { + if (parseCommands) { + return client.parseAndRunCommand(this, message, + inReplyTo: inReplyTo, editEventId: editEventId, txid: txid); + } + final event = { + 'msgtype': msgtype, + 'body': message, + }; + if (parseMarkdown) { + final html = markdown(event['body'], + getEmotePacks: () => getImagePacksFlat(ImagePackUsage.emoticon), + getMention: getMention); + // if the decoded html is the same as the body, there is no need in sending a formatted message + if (HtmlUnescape().convert(html.replaceAll(RegExp(r'
\n?'), '\n')) != + event['body']) { + event['format'] = 'org.matrix.custom.html'; + event['formatted_body'] = html; + } + } + return sendEvent(event, + txid: txid, inReplyTo: inReplyTo, editEventId: editEventId); + } + + /// Sends a reaction to an event with an [eventId] and the content [key] into a room. + /// Returns the event ID generated by the server for this reaction. + Future sendReaction(String eventId, String key, {String? txid}) { + return sendEvent({ + 'm.relates_to': { + 'rel_type': RelationshipTypes.reaction, + 'event_id': eventId, + 'key': key, + }, + }, type: EventTypes.Reaction, txid: txid); + } + + /// Sends the location with description [body] and geo URI [geoUri] into a room. + /// Returns the event ID generated by the server for this message. + Future sendLocation(String body, String geoUri, {String? txid}) { + final event = { + 'msgtype': 'm.location', + 'body': body, + 'geo_uri': geoUri, + }; + return sendEvent(event, txid: txid); + } + + /// Sends a [file] to this room after uploading it. Returns the mxc uri of + /// the uploaded file. If [waitUntilSent] is true, the future will wait until + /// the message event has received the server. Otherwise the future will only + /// wait until the file has been uploaded. + /// Optionally specify [extraContent] to tack on to the event. + /// + /// In case [file] is a [MatrixImageFile], [thumbnail] is automatically + /// computed unless it is explicitly provided. + Future sendFileEvent( + MatrixFile file, { + String? txid, + Event? inReplyTo, + String? editEventId, + bool waitUntilSent = false, + MatrixImageFile? thumbnail, + Map? extraContent, + }) async { + MatrixFile uploadFile = file; // ignore: omit_local_variable_types + // computing the thumbnail in case we can + thumbnail ??= (file is MatrixImageFile && encrypted + ? await file.generateThumbnail(compute: client.runInBackground) + : null); + MatrixFile? uploadThumbnail = + thumbnail; // ignore: omit_local_variable_types + EncryptedFile? encryptedFile; + EncryptedFile? encryptedThumbnail; + if (encrypted && client.fileEncryptionEnabled) { + encryptedFile = await file.encrypt(); + uploadFile = encryptedFile.toMatrixFile(); + + if (thumbnail != null) { + encryptedThumbnail = await thumbnail.encrypt(); + uploadThumbnail = encryptedThumbnail.toMatrixFile(); + } + } + final uploadResp = await client.uploadContent( + uploadFile.bytes, + filename: uploadFile.name, + contentType: uploadFile.mimeType, + ); + final thumbnailUploadResp = uploadThumbnail != null + ? await client.uploadContent( + uploadThumbnail.bytes, + filename: uploadThumbnail.name, + contentType: uploadThumbnail.mimeType, + ) + : null; + + // Send event + final content = { + 'msgtype': file.msgType, + 'body': file.name, + 'filename': file.name, + if (encryptedFile == null) 'url': uploadResp.toString(), + if (encryptedFile != null) + 'file': { + 'url': uploadResp.toString(), + 'mimetype': file.mimeType, + 'v': 'v2', + 'key': { + 'alg': 'A256CTR', + 'ext': true, + 'k': encryptedFile.k, + 'key_ops': ['encrypt', 'decrypt'], + 'kty': 'oct' + }, + 'iv': encryptedFile.iv, + 'hashes': {'sha256': encryptedFile.sha256} + }, + 'info': { + ...file.info, + if (thumbnail != null && encryptedThumbnail == null) + 'thumbnail_url': thumbnailUploadResp.toString(), + if (thumbnail != null && encryptedThumbnail != null) + 'thumbnail_file': { + 'url': thumbnailUploadResp.toString(), + 'mimetype': thumbnail.mimeType, + 'v': 'v2', + 'key': { + 'alg': 'A256CTR', + 'ext': true, + 'k': encryptedThumbnail.k, + 'key_ops': ['encrypt', 'decrypt'], + 'kty': 'oct' + }, + 'iv': encryptedThumbnail.iv, + 'hashes': {'sha256': encryptedThumbnail.sha256} + }, + if (thumbnail != null) 'thumbnail_info': thumbnail.info, + }, + if (extraContent != null) ...extraContent, + }; + final sendResponse = sendEvent( + content, + txid: txid, + inReplyTo: inReplyTo, + editEventId: editEventId, + ); + if (waitUntilSent) { + await sendResponse; + } + return uploadResp; + } + + Future _sendContent( + String type, + Map content, { + String? txid, + }) async { + txid ??= client.generateUniqueTransactionId(); + + final mustEncrypt = encrypted && client.encryptionEnabled; + + final sendMessageContent = mustEncrypt + ? await client.encryption! + .encryptGroupMessagePayload(id, content, type: type) + : content; + return await client.sendMessage( + id, + sendMessageContent.containsKey('ciphertext') + ? EventTypes.Encrypted + : type, + txid, + sendMessageContent, + ); + } + + String _stripBodyFallback(String body) { + if (body.startsWith('> <@')) { + var temp = ''; + var inPrefix = true; + for (final l in body.split('\n')) { + if (inPrefix && (l.isEmpty || l.startsWith('> '))) { + continue; + } + + inPrefix = false; + temp += temp.isEmpty ? l : ('\n' + l); + } + + return temp; + } else { + return body; + } + } + + /// Sends an event to this room with this json as a content. Returns the + /// event ID generated from the server. + Future sendEvent( + Map content, { + String type = EventTypes.Message, + String? txid, + Event? inReplyTo, + String? editEventId, + }) async { + // Create new transaction id + String messageID; + if (txid == null) { + messageID = client.generateUniqueTransactionId(); + } else { + messageID = txid; + } + + if (inReplyTo != null) { + var replyText = + '<${inReplyTo.senderId}> ' + _stripBodyFallback(inReplyTo.body); + replyText = replyText.split('\n').map((line) => '> $line').join('\n'); + content['format'] = 'org.matrix.custom.html'; + // be sure that we strip any previous reply fallbacks + final replyHtml = (inReplyTo.formattedText.isNotEmpty + ? inReplyTo.formattedText + : htmlEscape.convert(inReplyTo.body).replaceAll('\n', '
')) + .replaceAll( + RegExp(r'.*<\/mx-reply>', + caseSensitive: false, multiLine: false, dotAll: true), + ''); + final repliedHtml = content.tryGet('formatted_body') ?? + htmlEscape + .convert(content.tryGet('body') ?? '') + .replaceAll('\n', '
'); + content['formatted_body'] = + '
In reply to ${inReplyTo.senderId}
$replyHtml
$repliedHtml'; + // We escape all @room-mentions here to prevent accidental room pings when an admin + // replies to a message containing that! + content['body'] = + '${replyText.replaceAll('@room', '@\u200broom')}\n\n${content.tryGet('body') ?? ''}'; + content['m.relates_to'] = { + 'm.in_reply_to': { + 'event_id': inReplyTo.eventId, + }, + }; + } + if (editEventId != null) { + final newContent = content.copy(); + content['m.new_content'] = newContent; + content['m.relates_to'] = { + 'event_id': editEventId, + 'rel_type': RelationshipTypes.edit, + }; + if (content['body'] is String) { + content['body'] = '* ' + content['body']; + } + if (content['formatted_body'] is String) { + content['formatted_body'] = '* ' + content['formatted_body']; + } + } + final sentDate = DateTime.now(); + final syncUpdate = SyncUpdate( + nextBatch: '', + rooms: RoomsUpdate( + join: { + id: JoinedRoomUpdate( + timeline: TimelineUpdate( + events: [ + MatrixEvent( + content: content, + type: type, + eventId: messageID, + senderId: client.userID!, + originServerTs: sentDate, + unsigned: { + messageSendingStatusKey: EventStatus.sending.intValue, + 'transaction_id': messageID, + }, + ), + ], + ), + ), + }, + ), + ); + await _handleFakeSync(syncUpdate); + + // Send the text and on success, store and display a *sent* event. + String? res; + while (res == null) { + try { + res = await _sendContent( + type, + content, + txid: messageID, + ); + } catch (e, s) { + if ((DateTime.now().millisecondsSinceEpoch - + sentDate.millisecondsSinceEpoch) < + (1000 * client.sendMessageTimeoutSeconds)) { + Logs().w('[Client] Problem while sending message because of "' + + e.toString() + + '". Try again in 1 seconds...'); + await Future.delayed(Duration(seconds: 1)); + } else { + Logs().w('[Client] Problem while sending message', e, s); + syncUpdate.rooms!.join!.values.first.timeline!.events!.first + .unsigned![messageSendingStatusKey] = EventStatus.error.intValue; + await _handleFakeSync(syncUpdate); + return null; + } + } + } + syncUpdate.rooms!.join!.values.first.timeline!.events!.first + .unsigned![messageSendingStatusKey] = EventStatus.sent.intValue; + syncUpdate.rooms!.join!.values.first.timeline!.events!.first.eventId = res; + await _handleFakeSync(syncUpdate); + + return res; + } + + /// Call the Matrix API to join this room if the user is not already a member. + /// If this room is intended to be a direct chat, the direct chat flag will + /// automatically be set. + Future join({bool leaveIfNotFound = true}) async { + try { + await client.joinRoomById(id); + final invitation = getState(EventTypes.RoomMember, client.userID!); + if (invitation != null && + invitation.content['is_direct'] is bool && + invitation.content['is_direct']) { + await addToDirectChat(invitation.sender.id); + } + } on MatrixException catch (exception) { + if (leaveIfNotFound && + [MatrixError.M_NOT_FOUND, MatrixError.M_UNKNOWN] + .contains(exception.error)) { + await leave(); + } + rethrow; + } + return; + } + + /// Call the Matrix API to leave this room. If this room is set as a direct + /// chat, this will be removed too. + Future leave() async { + if (directChatMatrixID != '') await removeFromDirectChat(); + try { + await client.leaveRoom(id); + } on MatrixException catch (exception) { + if ([MatrixError.M_NOT_FOUND, MatrixError.M_UNKNOWN] + .contains(exception.error)) { + await _handleFakeSync( + SyncUpdate( + nextBatch: '', + rooms: RoomsUpdate( + leave: { + id: LeftRoomUpdate(), + }, + ), + ), + ); + } + rethrow; + } + return; + } + + /// Call the Matrix API to forget this room if you already left it. + Future forget() async { + await client.database?.forgetRoom(id); + await client.forgetRoom(id); + return; + } + + /// Call the Matrix API to kick a user from this room. + Future kick(String userID) => client.kick(id, userID); + + /// Call the Matrix API to ban a user from this room. + Future ban(String userID) => client.ban(id, userID); + + /// Call the Matrix API to unban a banned user from this room. + Future unban(String userID) => client.unban(id, userID); + + /// Set the power level of the user with the [userID] to the value [power]. + /// Returns the event ID of the new state event. If there is no known + /// power level event, there might something broken and this returns null. + Future setPower(String userID, int power) async { + var powerMap = getState(EventTypes.RoomPowerLevels)?.content; + if (!(powerMap is Map)) { + powerMap = {}; + } + (powerMap['users'] ??= {})[userID] = power; + + return await client.setRoomStateWithKey( + id, + EventTypes.RoomPowerLevels, + '', + powerMap, + ); + } + + /// Call the Matrix API to invite a user to this room. + Future invite(String userID) => client.inviteUser(id, userID); + + /// Request more previous events from the server. [historyCount] defines how much events should + /// be received maximum. When the request is answered, [onHistoryReceived] will be triggered **before** + /// the historical events will be published in the onEvent stream. + /// Returns the actual count of received timeline events. + Future requestHistory( + {int historyCount = defaultHistoryCount, + void Function()? onHistoryReceived}) async { + final prev_batch = this.prev_batch; + if (prev_batch == null) { + throw 'Tried to request history without a prev_batch token'; + } + final resp = await client.getRoomEvents( + id, + prev_batch, + Direction.b, + limit: historyCount, + filter: jsonEncode(StateFilter(lazyLoadMembers: true).toJson()), + ); + + if (onHistoryReceived != null) onHistoryReceived(); + this.prev_batch = resp.end; + + final loadFn = () async { + if (!((resp.chunk?.isNotEmpty ?? false) && resp.end != null)) return; + + await client.handleSync( + SyncUpdate( + nextBatch: '', + rooms: RoomsUpdate( + join: membership == Membership.join + ? { + id: JoinedRoomUpdate( + state: resp.state, + timeline: TimelineUpdate( + limited: false, + events: resp.chunk, + prevBatch: resp.end, + ), + ) + } + : null, + leave: membership != Membership.join + ? { + id: LeftRoomUpdate( + state: resp.state, + timeline: TimelineUpdate( + limited: false, + events: resp.chunk, + prevBatch: resp.end, + ), + ), + } + : null), + ), + sortAtTheEnd: true); + }; + + if (client.database != null) { + await client.database?.transaction(() async { + await client.database?.setRoomPrevBatch(resp.end!, id, client); + await loadFn(); + }); + } else { + await loadFn(); + } + + return resp.chunk?.length ?? 0; + } + + /// Sets this room as a direct chat for this user if not already. + Future addToDirectChat(String userID) async { + final directChats = client.directChats; + if (directChats[userID] is List) { + if (!directChats[userID].contains(id)) { + directChats[userID].add(id); + } else { + return; + } // Is already in direct chats + } else { + directChats[userID] = [id]; + } + + await client.setAccountData( + client.userID!, + 'm.direct', + directChats, + ); + return; + } + + /// Removes this room from all direct chat tags. + Future removeFromDirectChat() async { + final directChats = client.directChats; + if (directChats[directChatMatrixID] is List && + directChats[directChatMatrixID].contains(id)) { + directChats[directChatMatrixID].remove(id); + } else { + return; + } // Nothing to do here + + await client.setAccountDataPerRoom( + client.userID!, + id, + 'm.direct', + directChats, + ); + return; + } + + /// Sets the position of the read marker for a given room, and optionally the + /// read receipt's location. + Future setReadMarker(String eventId, {String? mRead}) async { + if (mRead != null) { + notificationCount = 0; + await client.database?.resetNotificationCount(id); + } + await client.setReadMarker( + id, + eventId, + mRead: mRead, + ); + return; + } + + /// This API updates the marker for the given receipt type to the event ID + /// specified. + Future postReceipt(String eventId) async { + notificationCount = 0; + await client.database?.resetNotificationCount(id); + await client.postReceipt( + id, + ReceiptType.mRead, + eventId, + {}, + ); + return; + } + + /// Sends *m.fully_read* and *m.read* for the given event ID. + @Deprecated('Use sendReadMarker instead') + Future sendReadReceipt(String eventID) async { + notificationCount = 0; + await client.database?.resetNotificationCount(id); + await client.setReadMarker( + id, + eventID, + mRead: eventID, + ); + return; + } + + /// Creates a timeline from the store. Returns a [Timeline] object. If you + /// just want to update the whole timeline on every change, use the [onUpdate] + /// callback. For updating only the parts that have changed, use the + /// [onChange], [onRemove], [onInsert] and the [onHistoryReceived] callbacks. + Future getTimeline({ + void Function(int index)? onChange, + void Function(int index)? onRemove, + void Function(int insertID)? onInsert, + void Function()? onUpdate, + }) async { + await postLoad(); + var events; + events = await client.database?.getEventList( + this, + limit: defaultHistoryCount, + ) ?? + []; + + // Try again to decrypt encrypted events and update the database. + if (encrypted && client.database != null && client.encryptionEnabled) { + await client.database?.transaction(() async { + for (var i = 0; i < events.length; i++) { + if (events[i].type == EventTypes.Encrypted && + events[i].content['can_request_session'] == true) { + events[i] = await client.encryption + ?.decryptRoomEvent(id, events[i], store: true); + } + } + }); + } + + final timeline = Timeline( + room: this, + events: events, + onChange: onChange, + onRemove: onRemove, + onInsert: onInsert, + onUpdate: onUpdate, + ); + if (client.database == null) { + await requestHistory(historyCount: 10); + } + return timeline; + } + + /// Returns all participants for this room. With lazy loading this + /// list may not be complete. Use [requestParticipants] in this + /// case. + /// List `membershipFilter` defines with what membership do you want the + /// participants, default set to + /// [[Membership.join, Membership.invite, Membership.knock]] + List getParticipants( + [List membershipFilter = const [ + Membership.join, + Membership.invite, + Membership.knock, + ]]) { + final members = states[EventTypes.RoomMember]; + if (members != null) { + return members.entries + .where((entry) => entry.value.type == EventTypes.RoomMember) + .map((entry) => entry.value.asUser) + .where((user) => membershipFilter.contains(user.membership)) + .toList(); + } + return []; + } + + bool _requestedParticipants = false; + + /// Request the full list of participants from the server. The local list + /// from the store is not complete if the client uses lazy loading. + /// List `membershipFilter` defines with what membership do you want the + /// participants, default set to + /// [[Membership.join, Membership.invite, Membership.knock]] + Future> requestParticipants( + [List membershipFilter = const [ + Membership.join, + Membership.invite, + Membership.knock, + ]]) async { + if (!participantListComplete && partial) { + // we aren't fully loaded, maybe the users are in the database + final users = await client.database?.getUsers(this) ?? []; + for (final user in users) { + setState(user); + } + } + if (_requestedParticipants || participantListComplete) { + return getParticipants(); + } + final matrixEvents = await client.getMembersByRoom(id); + final users = matrixEvents + ?.map((e) => Event.fromMatrixEvent(e, this).asUser) + .toList() ?? + []; + for (final user in users) { + setState(user); // at *least* cache this in-memory + } + _requestedParticipants = true; + users.removeWhere((u) => !membershipFilter.contains(u.membership)); + return users; + } + + /// Checks if the local participant list of joined and invited users is complete. + bool get participantListComplete { + final knownParticipants = getParticipants(); + knownParticipants.removeWhere( + (u) => ![Membership.join, Membership.invite].contains(u.membership)); + return knownParticipants.length == + (summary.mJoinedMemberCount ?? 0) + (summary.mInvitedMemberCount ?? 0); + } + + /// Returns the [User] object for the given [mxID] or requests it from + /// the homeserver and waits for a response. + @Deprecated('Use [requestUser] instead') + Future getUserByMXID(String mxID) async => + getState(EventTypes.RoomMember, mxID)?.asUser ?? await requestUser(mxID); + + /// Returns the [User] object for the given [mxID] or requests it from + /// the homeserver and returns a default [User] object while waiting. + User getUserByMXIDSync(String mxID) { + final user = getState(EventTypes.RoomMember, mxID); + if (user != null) { + return user.asUser; + } else { + requestUser(mxID, ignoreErrors: true); + return User(mxID, room: this); + } + } + + final Set _requestingMatrixIds = {}; + + /// Requests a missing [User] for this room. Important for clients using + /// lazy loading. If the user can't be found this method tries to fetch + /// the displayname and avatar from the profile if [requestProfile] is true. + Future requestUser( + String mxID, { + bool ignoreErrors = false, + bool requestProfile = true, + }) async { + // Checks if the user is really missing + final stateUser = getState(EventTypes.RoomMember, mxID); + if (stateUser != null) { + return stateUser.asUser; + } + + // it may be in the database + final dbuser = await client.database?.getUser(mxID, this); + if (dbuser != null) { + setState(dbuser); + onUpdate.add(id); + return dbuser; + } + + if (!_requestingMatrixIds.add(mxID)) return null; + Map? resp; + try { + Logs().v( + 'Request missing user $mxID in room $displayname from the server...'); + resp = await client.getRoomStateWithKey( + id, + EventTypes.RoomMember, + mxID, + ); + } catch (e, s) { + if (!ignoreErrors) { + _requestingMatrixIds.remove(mxID); + rethrow; + } else { + Logs().w('Unable to request the user $mxID from the server', e, s); + } + } + if (resp == null && requestProfile) { + try { + final profile = await client.getUserProfile(mxID); + resp = { + 'displayname': profile.displayname, + 'avatar_url': profile.avatarUrl.toString(), + }; + } catch (e, s) { + _requestingMatrixIds.remove(mxID); + if (!ignoreErrors) { + rethrow; + } else { + Logs().w('Unable to request the profile $mxID from the server', e, s); + } + } + } + if (resp == null) { + return null; + } + final user = User(mxID, + displayName: resp['displayname'], + avatarUrl: resp['avatar_url'], + room: this); + setState(user); + await client.database?.transaction(() async { + final fakeEventId = String.fromCharCodes( + await sha256( + Uint8List.fromList( + (id + mxID + client.generateUniqueTransactionId()).codeUnits), + ), + ); + await client.database?.storeEventUpdate( + EventUpdate( + content: MatrixEvent( + type: EventTypes.RoomMember, + content: resp!, + stateKey: mxID, + originServerTs: DateTime.now(), + senderId: mxID, + eventId: fakeEventId, + ).toJson(), + roomID: id, + type: EventUpdateType.state, + ), + client, + ); + }); + onUpdate.add(id); + _requestingMatrixIds.remove(mxID); + return user; + } + + /// Searches for the event on the server. Returns null if not found. + Future getEventById(String eventID) async { + try { + final matrixEvent = await client.getOneRoomEvent(id, eventID); + final event = Event.fromMatrixEvent(matrixEvent, this); + if (event.type == EventTypes.Encrypted && client.encryptionEnabled) { + // attempt decryption + return await client.encryption + ?.decryptRoomEvent(id, event, store: false); + } + return event; + } on MatrixException catch (err) { + if (err.errcode == 'M_NOT_FOUND') { + return null; + } + rethrow; + } + } + + /// Returns the power level of the given user ID. + int getPowerLevelByUserId(String userId) { + var powerLevel = 0; + final powerLevelState = getState(EventTypes.RoomPowerLevels); + if (powerLevelState == null) return powerLevel; + if (powerLevelState.content['users_default'] is int) { + powerLevel = powerLevelState.content['users_default']; + } + if (powerLevelState.content + .tryGet>('users') + ?.tryGet(userId) != + null) { + powerLevel = powerLevelState.content['users'][userId]; + } + return powerLevel; + } + + /// Returns the user's own power level. + int get ownPowerLevel => getPowerLevelByUserId(client.userID!); + + /// Returns the power levels from all users for this room or null if not given. + Map? get powerLevels { + final powerLevelState = + getState(EventTypes.RoomPowerLevels)?.content['users']; + return (powerLevelState is Map) ? powerLevelState : null; + } + + /// Uploads a new user avatar for this room. Returns the event ID of the new + /// m.room.avatar event. Leave empty to remove the current avatar. + Future setAvatar(MatrixFile? file) async { + final uploadResp = file == null + ? null + : await client.uploadContent(file.bytes, filename: file.name); + return await client.setRoomStateWithKey( + id, + EventTypes.RoomAvatar, + '', + { + if (uploadResp != null) 'url': uploadResp.toString(), + }, + ); + } + + bool _hasPermissionFor(String action) { + final pl = getState(EventTypes.RoomPowerLevels)?.content[action]; + if (pl == null) { + return true; + } + return ownPowerLevel >= pl; + } + + /// The level required to ban a user. + bool get canBan => _hasPermissionFor('ban'); + + /// The default level required to send message events. Can be overridden by the events key. + bool get canSendDefaultMessages => + _hasPermissionFor('events_default') && + (!encrypted || client.encryptionEnabled); + + /// The level required to invite a user. + bool get canInvite => _hasPermissionFor('invite'); + + /// The level required to kick a user. + bool get canKick => _hasPermissionFor('kick'); + + /// The level required to redact an event. + bool get canRedact => _hasPermissionFor('redact'); + + /// The default level required to send state events. Can be overridden by the events key. + bool get canSendDefaultStates => _hasPermissionFor('state_default'); + + bool get canChangePowerLevel => canSendEvent(EventTypes.RoomPowerLevels); + + bool canSendEvent(String eventType) { + final pl = + getState(EventTypes.RoomPowerLevels)?.content['events']?[eventType]; + if (pl == null) { + return eventType == EventTypes.Message + ? canSendDefaultMessages + : canSendDefaultStates; + } + return ownPowerLevel >= pl; + } + + /// Returns the [PushRuleState] for this room, based on the m.push_rules stored in + /// the account_data. + PushRuleState get pushRuleState { + final globalPushRules = + client.accountData['m.push_rules']?.content['global']; + if (!(globalPushRules is Map)) { + return PushRuleState.notify; + } + + if (globalPushRules['override'] is List) { + for (final pushRule in globalPushRules['override']) { + if (pushRule['rule_id'] == id) { + if (pushRule['actions'].indexOf('dont_notify') != -1) { + return PushRuleState.dontNotify; + } + break; + } + } + } + + if (globalPushRules['room'] is List) { + for (final pushRule in globalPushRules['room']) { + if (pushRule['rule_id'] == id) { + if (pushRule['actions'].indexOf('dont_notify') != -1) { + return PushRuleState.mentionsOnly; + } + break; + } + } + } + + return PushRuleState.notify; + } + + /// Sends a request to the homeserver to set the [PushRuleState] for this room. + /// Returns ErrorResponse if something goes wrong. + Future setPushRuleState(PushRuleState newState) async { + if (newState == pushRuleState) return null; + dynamic resp; + switch (newState) { + // All push notifications should be sent to the user + case PushRuleState.notify: + if (pushRuleState == PushRuleState.dontNotify) { + await client.deletePushRule('global', PushRuleKind.override, id); + } else if (pushRuleState == PushRuleState.mentionsOnly) { + await client.deletePushRule('global', PushRuleKind.room, id); + } + break; + // Only when someone mentions the user, a push notification should be sent + case PushRuleState.mentionsOnly: + if (pushRuleState == PushRuleState.dontNotify) { + await client.deletePushRule('global', PushRuleKind.override, id); + await client.setPushRule( + 'global', + PushRuleKind.room, + id, + [PushRuleAction.dontNotify], + ); + } else if (pushRuleState == PushRuleState.notify) { + await client.setPushRule( + 'global', + PushRuleKind.room, + id, + [PushRuleAction.dontNotify], + ); + } + break; + // No push notification should be ever sent for this room. + case PushRuleState.dontNotify: + if (pushRuleState == PushRuleState.mentionsOnly) { + await client.deletePushRule('global', PushRuleKind.room, id); + } + await client.setPushRule( + 'global', + PushRuleKind.override, + id, + [PushRuleAction.dontNotify], + conditions: [ + PushCondition(kind: 'event_match', key: 'room_id', pattern: id) + ], + ); + } + return resp; + } + + /// Redacts this event. Throws `ErrorResponse` on error. + Future redactEvent(String eventId, + {String? reason, String? txid}) async { + // Create new transaction id + String messageID; + final now = DateTime.now().millisecondsSinceEpoch; + if (txid == null) { + messageID = 'msg$now'; + } else { + messageID = txid; + } + final data = {}; + if (reason != null) data['reason'] = reason; + return await client.redactEvent( + id, + eventId, + messageID, + reason: reason, + ); + } + + /// This tells the server that the user is typing for the next N milliseconds + /// where N is the value specified in the timeout key. Alternatively, if typing is false, + /// it tells the server that the user has stopped typing. + Future setTyping(bool isTyping, {int? timeout}) => + client.setTyping(client.userID!, id, isTyping, timeout: timeout); + + @Deprecated('Use sendTypingNotification instead') + Future sendTypingInfo(bool isTyping, {int? timeout}) => + setTyping(isTyping, timeout: timeout); + + /// This is sent by the caller when they wish to establish a call. + /// [callId] is a unique identifier for the call. + /// [version] is the version of the VoIP specification this message adheres to. This specification is version 1. + /// [lifetime] is the time in milliseconds that the invite is valid for. Once the invite age exceeds this value, + /// clients should discard it. They should also no longer show the call as awaiting an answer in the UI. + /// [type] The type of session description. Must be 'offer'. + /// [sdp] The SDP text of the session description. + /// [invitee] The user ID of the person who is being invited. Invites without an invitee field are defined to be + /// intended for any member of the room other than the sender of the event. + /// [party_id] The party ID for call, Can be set to client.deviceId. + Future inviteToCall( + String callId, int lifetime, String party_id, String? invitee, String sdp, + {String type = 'offer', + String version = voipProtoVersion, + String? txid, + CallCapabilities? capabilities, + SDPStreamMetadata? metadata}) async { + txid ??= 'txid${DateTime.now().millisecondsSinceEpoch}'; + + final content = { + 'call_id': callId, + 'party_id': party_id, + 'version': version, + 'lifetime': lifetime, + 'offer': {'sdp': sdp, 'type': type}, + if (invitee != null) 'invitee': invitee, + if (capabilities != null) 'capabilities': capabilities.toJson(), + if (metadata != null) sdpStreamMetadataKey: metadata.toJson(), + }; + return await _sendContent( + EventTypes.CallInvite, + content, + txid: txid, + ); + } + + /// The calling party sends the party_id of the first selected answer. + /// + /// Usually after receiving the first answer sdp in the client.onCallAnswer event, + /// save the `party_id`, and then send `CallSelectAnswer` to others peers that the call has been picked up. + /// + /// [callId] is a unique identifier for the call. + /// [version] is the version of the VoIP specification this message adheres to. This specification is version 1. + /// [party_id] The party ID for call, Can be set to client.deviceId. + /// [selected_party_id] The party ID for the selected answer. + Future selectCallAnswer( + String callId, int lifetime, String party_id, String selected_party_id, + {String version = voipProtoVersion, String? txid}) async { + txid ??= 'txid${DateTime.now().millisecondsSinceEpoch}'; + + final content = { + 'call_id': callId, + 'party_id': party_id, + 'version': version, + 'lifetime': lifetime, + 'selected_party_id': selected_party_id, + }; + + return await _sendContent( + EventTypes.CallSelectAnswer, + content, + txid: txid, + ); + } + + /// Reject a call + /// [callId] is a unique identifier for the call. + /// [version] is the version of the VoIP specification this message adheres to. This specification is version 1. + /// [party_id] The party ID for call, Can be set to client.deviceId. + Future sendCallReject(String callId, int lifetime, String party_id, + {String version = voipProtoVersion, String? txid}) async { + txid ??= 'txid${DateTime.now().millisecondsSinceEpoch}'; + + final content = { + 'call_id': callId, + 'party_id': party_id, + 'version': version, + 'lifetime': lifetime, + }; + + return await _sendContent( + EventTypes.CallReject, + content, + txid: txid, + ); + } + + /// When local audio/video tracks are added/deleted or hold/unhold, + /// need to createOffer and renegotiation. + /// [callId] is a unique identifier for the call. + /// [version] is the version of the VoIP specification this message adheres to. This specification is version 1. + /// [party_id] The party ID for call, Can be set to client.deviceId. + Future sendCallNegotiate( + String callId, int lifetime, String party_id, String sdp, + {String type = 'offer', + String version = voipProtoVersion, + String? txid, + CallCapabilities? capabilities, + SDPStreamMetadata? metadata}) async { + txid ??= 'txid${DateTime.now().millisecondsSinceEpoch}'; + final content = { + 'call_id': callId, + 'party_id': party_id, + 'version': version, + 'lifetime': lifetime, + 'description': {'sdp': sdp, 'type': type}, + if (capabilities != null) 'capabilities': capabilities.toJson(), + if (metadata != null) sdpStreamMetadataKey: metadata.toJson(), + }; + return await _sendContent( + EventTypes.CallNegotiate, + content, + txid: txid, + ); + } + + /// This is sent by callers after sending an invite and by the callee after answering. + /// Its purpose is to give the other party additional ICE candidates to try using to communicate. + /// + /// [callId] The ID of the call this event relates to. + /// + /// [version] The version of the VoIP specification this messages adheres to. This specification is version 1. + /// + /// [party_id] The party ID for call, Can be set to client.deviceId. + /// + /// [candidates] Array of objects describing the candidates. Example: + /// + /// ``` + /// [ + /// { + /// "candidate": "candidate:863018703 1 udp 2122260223 10.9.64.156 43670 typ host generation 0", + /// "sdpMLineIndex": 0, + /// "sdpMid": "audio" + /// } + /// ], + /// ``` + Future sendCallCandidates( + String callId, + String party_id, + List> candidates, { + String version = voipProtoVersion, + String? txid, + }) async { + txid ??= 'txid${DateTime.now().millisecondsSinceEpoch}'; + final content = { + 'call_id': callId, + 'party_id': party_id, + 'version': version, + 'candidates': candidates, + }; + return await _sendContent( + EventTypes.CallCandidates, + content, + txid: txid, + ); + } + + /// This event is sent by the callee when they wish to answer the call. + /// [callId] is a unique identifier for the call. + /// [version] is the version of the VoIP specification this message adheres to. This specification is version 1. + /// [type] The type of session description. Must be 'answer'. + /// [sdp] The SDP text of the session description. + /// [party_id] The party ID for call, Can be set to client.deviceId. + Future answerCall(String callId, String sdp, String party_id, + {String type = 'answer', + String version = voipProtoVersion, + String? txid, + CallCapabilities? capabilities, + SDPStreamMetadata? metadata}) async { + txid ??= 'txid${DateTime.now().millisecondsSinceEpoch}'; + final content = { + 'call_id': callId, + 'party_id': party_id, + 'version': version, + 'answer': {'sdp': sdp, 'type': type}, + if (capabilities != null) 'capabilities': capabilities.toJson(), + if (metadata != null) sdpStreamMetadataKey: metadata.toJson(), + }; + return await _sendContent( + EventTypes.CallAnswer, + content, + txid: txid, + ); + } + + /// This event is sent by the callee when they wish to answer the call. + /// [callId] The ID of the call this event relates to. + /// [version] is the version of the VoIP specification this message adheres to. This specification is version 1. + /// [party_id] The party ID for call, Can be set to client.deviceId. + Future hangupCall( + String callId, String party_id, String? hangupCause, + {String version = voipProtoVersion, String? txid}) async { + txid ??= 'txid${DateTime.now().millisecondsSinceEpoch}'; + + final content = { + 'call_id': callId, + 'party_id': party_id, + 'version': version, + if (hangupCause != null) 'reason': hangupCause, + }; + return await _sendContent( + EventTypes.CallHangup, + content, + txid: txid, + ); + } + + /// Send SdpStreamMetadata Changed event. + /// + /// This MSC also adds a new call event m.call.sdp_stream_metadata_changed, + /// which has the common VoIP fields as specified in + /// MSC2746 (version, call_id, party_id) and a sdp_stream_metadata object which + /// is the same thing as sdp_stream_metadata in m.call.negotiate, m.call.invite + /// and m.call.answer. The client sends this event the when sdp_stream_metadata + /// has changed but no negotiation is required + /// (e.g. the user mutes their camera/microphone). + /// + /// [callId] The ID of the call this event relates to. + /// [version] is the version of the VoIP specification this message adheres to. This specification is version 1. + /// [party_id] The party ID for call, Can be set to client.deviceId. + /// [metadata] The sdp_stream_metadata object. + Future sendSDPStreamMetadataChanged( + String callId, String party_id, SDPStreamMetadata metadata, + {String version = voipProtoVersion, String? txid}) async { + txid ??= 'txid${DateTime.now().millisecondsSinceEpoch}'; + final content = { + 'call_id': callId, + 'party_id': party_id, + 'version': version, + sdpStreamMetadataKey: metadata.toJson(), + }; + return await _sendContent( + EventTypes.CallSDPStreamMetadataChangedPrefix, + content, + txid: txid, + ); + } + + /// CallReplacesEvent for Transfered calls + /// + /// [callId] The ID of the call this event relates to. + /// [version] is the version of the VoIP specification this message adheres to. This specification is version 1. + /// [party_id] The party ID for call, Can be set to client.deviceId. + /// [callReplaces] transfer info + Future sendCallReplaces( + String callId, String party_id, CallReplaces callReplaces, + {String version = voipProtoVersion, String? txid}) async { + txid ??= 'txid${DateTime.now().millisecondsSinceEpoch}'; + final content = { + 'call_id': callId, + 'party_id': party_id, + 'version': version, + ...callReplaces.toJson(), + }; + return await _sendContent( + EventTypes.CallReplaces, + content, + txid: txid, + ); + } + + /// send AssertedIdentity event + /// + /// [callId] The ID of the call this event relates to. + /// [version] is the version of the VoIP specification this message adheres to. This specification is version 1. + /// [party_id] The party ID for call, Can be set to client.deviceId. + /// [assertedIdentity] the asserted identity + Future sendAssertedIdentity( + String callId, String party_id, AssertedIdentity assertedIdentity, + {String version = voipProtoVersion, String? txid}) async { + txid ??= 'txid${DateTime.now().millisecondsSinceEpoch}'; + final content = { + 'call_id': callId, + 'party_id': party_id, + 'version': version, + 'asserted_identity': assertedIdentity.toJson(), + }; + return await _sendContent( + EventTypes.CallAssertedIdentity, + content, + txid: txid, + ); + } + + /// A room may be public meaning anyone can join the room without any prior action. Alternatively, + /// it can be invite meaning that a user who wishes to join the room must first receive an invite + /// to the room from someone already inside of the room. Currently, knock and private are reserved + /// keywords which are not implemented. + JoinRules? get joinRules { + final joinRule = getState(EventTypes.RoomJoinRules)?.content['join_rule']; + return joinRule != null + ? JoinRules.values.firstWhereOrNull( + (r) => r.toString().replaceAll('JoinRules.', '') == joinRule) + : null; + } + + /// Changes the join rules. You should check first if the user is able to change it. + Future setJoinRules(JoinRules joinRules) async { + await client.setRoomStateWithKey( + id, + EventTypes.RoomJoinRules, + '', + { + 'join_rule': joinRules.toString().replaceAll('JoinRules.', ''), + }, + ); + return; + } + + /// Whether the user has the permission to change the join rules. + bool get canChangeJoinRules => canSendEvent(EventTypes.RoomJoinRules); + + /// This event controls whether guest users are allowed to join rooms. If this event + /// is absent, servers should act as if it is present and has the guest_access value "forbidden". + GuestAccess get guestAccess { + final ga = getState(EventTypes.GuestAccess)?.content['guest_access']; + return ga != null + ? (_guestAccessMap.map((k, v) => MapEntry(v, k))[ga] ?? + GuestAccess.forbidden) + : GuestAccess.forbidden; + } + + /// Changes the guest access. You should check first if the user is able to change it. + Future setGuestAccess(GuestAccess guestAccess) async { + await client.setRoomStateWithKey( + id, + EventTypes.GuestAccess, + '', + { + 'guest_access': _guestAccessMap[guestAccess], + }, + ); + return; + } + + /// Whether the user has the permission to change the guest access. + bool get canChangeGuestAccess => canSendEvent(EventTypes.GuestAccess); + + /// This event controls whether a user can see the events that happened in a room from before they joined. + HistoryVisibility? get historyVisibility { + final hv = + getState(EventTypes.HistoryVisibility)?.content['history_visibility']; + return hv != null + ? _historyVisibilityMap.map((k, v) => MapEntry(v, k))[hv] + : null; + } + + /// Changes the history visibility. You should check first if the user is able to change it. + Future setHistoryVisibility(HistoryVisibility historyVisibility) async { + await client.setRoomStateWithKey( + id, + EventTypes.HistoryVisibility, + '', + { + 'history_visibility': _historyVisibilityMap[historyVisibility], + }, + ); + return; + } + + /// Whether the user has the permission to change the history visibility. + bool get canChangeHistoryVisibility => + canSendEvent(EventTypes.HistoryVisibility); + + /// Returns the encryption algorithm. Currently only `m.megolm.v1.aes-sha2` is supported. + /// Returns null if there is no encryption algorithm. + String? get encryptionAlgorithm => + getState(EventTypes.Encryption)?.parsedRoomEncryptionContent.algorithm; + + /// Checks if this room is encrypted. + bool get encrypted => encryptionAlgorithm != null; + + Future enableEncryption({int algorithmIndex = 0}) async { + if (encrypted) throw ('Encryption is already enabled!'); + final algorithm = Client.supportedGroupEncryptionAlgorithms[algorithmIndex]; + await client.setRoomStateWithKey( + id, + EventTypes.Encryption, + '', + { + 'algorithm': algorithm, + }, + ); + return; + } + + /// Returns all known device keys for all participants in this room. + Future> getUserDeviceKeys() async { + await client.userDeviceKeysLoading; + final deviceKeys = []; + final users = await requestParticipants(); + for (final user in users) { + final userDeviceKeys = client.userDeviceKeys[user.id]?.deviceKeys.values; + if ([Membership.invite, Membership.join].contains(user.membership) && + userDeviceKeys != null) { + for (final deviceKeyEntry in userDeviceKeys) { + deviceKeys.add(deviceKeyEntry); + } + } + } + return deviceKeys; + } + + Future requestSessionKey(String sessionId, String senderKey) async { + if (!client.encryptionEnabled) { + return; + } + await client.encryption?.keyManager.request(this, sessionId, senderKey); + } + + Future _handleFakeSync(SyncUpdate syncUpdate, + {bool sortAtTheEnd = false}) async { + if (client.database != null) { + await client.database?.transaction(() async { + await client.handleSync(syncUpdate, sortAtTheEnd: sortAtTheEnd); + }); + } else { + await client.handleSync(syncUpdate, sortAtTheEnd: sortAtTheEnd); + } + } + + /// Whether this is an extinct room which has been archived in favor of a new + /// room which replaces this. Use `getLegacyRoomInformations()` to get more + /// informations about it if this is true. + bool get isExtinct => getState(EventTypes.RoomTombstone) != null; + + /// Returns informations about how this room is + TombstoneContent? get extinctInformations => + getState(EventTypes.RoomTombstone)?.parsedTombstoneContent; + + /// Checks if the `m.room.create` state has a `type` key with the value + /// `m.space`. + bool get isSpace => + getState(EventTypes.RoomCreate)?.content.tryGet('type') == + RoomCreationTypes.mSpace; // TODO: Magic string! + + /// The parents of this room. Currently this SDK doesn't yet set the canonical + /// flag and is not checking if this room is in fact a child of this space. + /// You should therefore not rely on this and always check the children of + /// the space. + List get spaceParents => + states[EventTypes.spaceParent] + ?.values + .map((state) => SpaceParent.fromState(state)) + .where((child) => child.via?.isNotEmpty ?? false) + .toList() ?? + []; + + /// List all children of this space. Children without a `via` domain will be + /// ignored. + /// Children are sorted by the `order` while those without this field will be + /// sorted at the end of the list. + List get spaceChildren => !isSpace + ? throw Exception('Room is not a space!') + : (states[EventTypes.spaceChild] + ?.values + .map((state) => SpaceChild.fromState(state)) + .where((child) => child.via?.isNotEmpty ?? false) + .toList() ?? + []) + ..sort((a, b) => a.order.isEmpty || b.order.isEmpty + ? b.order.compareTo(a.order) + : a.order.compareTo(b.order)); + + /// Adds or edits a child of this space. + Future setSpaceChild( + String roomId, { + List? via, + String? order, + bool? suggested, + }) async { + if (!isSpace) throw Exception('Room is not a space!'); + via ??= [client.userID!.domain!]; + await client.setRoomStateWithKey(id, EventTypes.spaceChild, roomId, { + 'via': via, + if (order != null) 'order': order, + if (suggested != null) 'suggested': suggested, + }); + await client.setRoomStateWithKey(roomId, EventTypes.spaceParent, id, { + 'via': via, + }); + return; + } + + /// Remove a child from this space by setting the `via` to an empty list. + Future removeSpaceChild(String roomId) => !isSpace + ? throw Exception('Room is not a space!') + : setSpaceChild(roomId, via: const []); + + @override + bool operator ==(dynamic other) => (other is Room && other.id == id); +} diff --git a/lib/src/timeline.dart b/lib/src/timeline.dart new file mode 100644 index 0000000..7324b10 --- /dev/null +++ b/lib/src/timeline.dart @@ -0,0 +1,370 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:async'; + +import 'package:collection/src/iterable_extensions.dart'; + +import '../matrix.dart'; + +/// Represents the timeline of a room. The callback [onUpdate] will be triggered +/// automatically. The initial +/// event list will be retreived when created by the `room.getTimeline()` method. +class Timeline { + final Room room; + final List events; + + /// Map of event ID to map of type to set of aggregated events + final Map>> aggregatedEvents = {}; + + final void Function()? onUpdate; + final void Function(int index)? onChange; + final void Function(int index)? onInsert; + final void Function(int index)? onRemove; + + StreamSubscription? sub; + StreamSubscription? roomSub; + StreamSubscription? sessionIdReceivedSub; + bool isRequestingHistory = false; + + final Map _eventCache = {}; + + /// Searches for the event in this timeline. If not + /// found, requests from the server. Requested events + /// are cached. + Future getEventById(String id) async { + for (final event in events) { + if (event.eventId == id) return event; + } + if (_eventCache.containsKey(id)) return _eventCache[id]; + final requestedEvent = await room.getEventById(id); + if (requestedEvent == null) return null; + _eventCache[id] = requestedEvent; + return _eventCache[id]; + } + + // When fetching history, we will collect them into the `_historyUpdates` set + // first, and then only process all events at once, once we have the full history. + // This ensures that the entire history fetching only triggers `onUpdate` only *once*, + // even if /sync's complete while history is being proccessed. + bool _collectHistoryUpdates = false; + + bool get canRequestHistory { + if (events.isEmpty) return true; + return events.last.type != EventTypes.RoomCreate; + } + + Future requestHistory( + {int historyCount = Room.defaultHistoryCount}) async { + if (isRequestingHistory) { + return; + } + isRequestingHistory = true; + onUpdate?.call(); + + try { + // Look up for events in hive first + final eventsFromStore = await room.client.database?.getEventList( + room, + start: events.length, + limit: Room.defaultHistoryCount, + ); + if (eventsFromStore != null && eventsFromStore.isNotEmpty) { + events.addAll(eventsFromStore); + final startIndex = events.length - eventsFromStore.length; + final endIndex = events.length; + for (var i = startIndex; i < endIndex; i++) { + onInsert?.call(i); + } + } else { + Logs().v('No more events found in the store. Request from server...'); + await room.requestHistory( + historyCount: historyCount, + onHistoryReceived: () { + _collectHistoryUpdates = true; + }, + ); + } + } finally { + _collectHistoryUpdates = false; + isRequestingHistory = false; + onUpdate?.call(); + } + } + + Timeline({ + required this.room, + List? events, + this.onUpdate, + this.onChange, + this.onInsert, + this.onRemove, + }) : events = events ?? [] { + sub = room.client.onEvent.stream.listen(_handleEventUpdate); + + // If the timeline is limited we want to clear our events cache + roomSub = room.client.onSync.stream + .where((sync) => sync.rooms?.join?[room.id]?.timeline?.limited == true) + .listen(_removeEventsNotInThisSync); + + sessionIdReceivedSub = + room.onSessionKeyReceived.stream.listen(_sessionKeyReceived); + + // we want to populate our aggregated events + for (final e in this.events) { + addAggregatedEvent(e); + } + } + + /// Removes all entries from [events] which are not in this SyncUpdate. + void _removeEventsNotInThisSync(SyncUpdate sync) { + final newSyncEvents = sync.rooms?.join?[room.id]?.timeline?.events ?? []; + final keepEventIds = newSyncEvents.map((e) => e.eventId); + events.removeWhere((e) => !keepEventIds.contains(e.eventId)); + } + + /// Don't forget to call this before you dismiss this object! + void cancelSubscriptions() { + sub?.cancel(); + roomSub?.cancel(); + sessionIdReceivedSub?.cancel(); + } + + void _sessionKeyReceived(String sessionId) async { + var decryptAtLeastOneEvent = false; + final decryptFn = () async { + final encryption = room.client.encryption; + if (!room.client.encryptionEnabled || encryption == null) { + return; + } + for (var i = 0; i < events.length; i++) { + if (events[i].type == EventTypes.Encrypted && + events[i].messageType == MessageTypes.BadEncrypted && + events[i].content['session_id'] == sessionId) { + events[i] = await encryption.decryptRoomEvent(room.id, events[i], + store: true); + onChange?.call(i); + if (events[i].type != EventTypes.Encrypted) { + decryptAtLeastOneEvent = true; + } + } + } + }; + if (room.client.database != null) { + await room.client.database?.transaction(decryptFn); + } else { + await decryptFn(); + } + if (decryptAtLeastOneEvent) onUpdate?.call(); + } + + /// Request the keys for undecryptable events of this timeline + void requestKeys() { + for (final event in events) { + if (event.type == EventTypes.Encrypted && + event.messageType == MessageTypes.BadEncrypted && + event.content['can_request_session'] == true) { + try { + room.client.encryption?.keyManager.maybeAutoRequest(room.id, + event.content['session_id'], event.content['sender_key']); + } catch (_) { + // dispose + } + } + } + } + + /// Set the read marker to the last synced event in this timeline. + Future setReadMarker([String? eventId]) async { + eventId ??= + events.firstWhereOrNull((event) => event.status.isSynced)?.eventId; + if (eventId == null) return; + return room.setReadMarker(eventId, mRead: eventId); + } + + int _findEvent({String? event_id, String? unsigned_txid}) { + // we want to find any existing event where either the passed event_id or the passed unsigned_txid + // matches either the event_id or transaction_id of the existing event. + // For that we create two sets, searchNeedle, what we search, and searchHaystack, where we check if there is a match. + // Now, after having these two sets, if the intersect between them is non-empty, we know that we have at least one match in one pair, + // thus meaning we found our element. + final searchNeedle = {}; + if (event_id != null) { + searchNeedle.add(event_id); + } + if (unsigned_txid != null) { + searchNeedle.add(unsigned_txid); + } + int i; + for (i = 0; i < events.length; i++) { + final searchHaystack = {events[i].eventId}; + + final txnid = events[i].unsigned?['transaction_id']; + if (txnid != null) { + searchHaystack.add(txnid); + } + if (searchNeedle.intersection(searchHaystack).isNotEmpty) { + break; + } + } + return i; + } + + void _removeEventFromSet(Set eventSet, Event event) { + eventSet.removeWhere((e) => + e.matchesEventOrTransactionId(event.eventId) || + (event.unsigned != null && + e.matchesEventOrTransactionId(event.unsigned?['transaction_id']))); + } + + void addAggregatedEvent(Event event) { + // we want to add an event to the aggregation tree + final relationshipType = event.relationshipType; + final relationshipEventId = event.relationshipEventId; + if (relationshipType == null || relationshipEventId == null) { + return; // nothing to do + } + final events = (aggregatedEvents[relationshipEventId] ??= + >{})[relationshipType] ??= {}; + // remove a potential old event + _removeEventFromSet(events, event); + // add the new one + events.add(event); + if (onChange != null) { + final index = _findEvent(event_id: relationshipEventId); + onChange?.call(index); + } + } + + void removeAggregatedEvent(Event event) { + aggregatedEvents.remove(event.eventId); + if (event.unsigned != null) { + aggregatedEvents.remove(event.unsigned?['transaction_id']); + } + for (final types in aggregatedEvents.values) { + for (final events in types.values) { + _removeEventFromSet(events, event); + } + } + } + + void _handleEventUpdate(EventUpdate eventUpdate, {bool update = true}) { + try { + if (eventUpdate.roomID != room.id) return; + + if (eventUpdate.type != EventUpdateType.timeline && + eventUpdate.type != EventUpdateType.history) { + return; + } + final status = eventStatusFromInt(eventUpdate.content['status'] ?? + (eventUpdate.content['unsigned'] is Map + ? eventUpdate.content['unsigned'][messageSendingStatusKey] + : null) ?? + EventStatus.synced.intValue); + + if (status.isRemoved) { + final i = _findEvent(event_id: eventUpdate.content['event_id']); + if (i < events.length) { + removeAggregatedEvent(events[i]); + events.removeAt(i); + onRemove?.call(i); + } + } else { + final i = _findEvent( + event_id: eventUpdate.content['event_id'], + unsigned_txid: eventUpdate.content['unsigned'] is Map + ? eventUpdate.content['unsigned']['transaction_id'] + : null); + + if (i < events.length) { + // if the old status is larger than the new one, we also want to preserve the old status + final oldStatus = events[i].status; + events[i] = Event.fromJson( + eventUpdate.content, + room, + ); + // do we preserve the status? we should allow 0 -> -1 updates and status increases + if ((latestEventStatus(status, oldStatus) == oldStatus) && + !(status.isError && oldStatus.isSending)) { + events[i].status = oldStatus; + } + addAggregatedEvent(events[i]); + onChange?.call(i); + } else { + final newEvent = Event.fromJson( + eventUpdate.content, + room, + ); + + if (eventUpdate.type == EventUpdateType.history && + events.indexWhere( + (e) => e.eventId == eventUpdate.content['event_id']) != + -1) return; + var index = events.length; + if (eventUpdate.type == EventUpdateType.history) { + events.add(newEvent); + } else { + index = events.firstIndexWhereNotError; + events.insert(index, newEvent); + } + onInsert?.call(index); + + addAggregatedEvent(newEvent); + } + } + + // Handle redaction events + if (eventUpdate.content['type'] == EventTypes.Redaction) { + final index = _findEvent(event_id: eventUpdate.content['redacts']); + if (index < events.length) { + removeAggregatedEvent(events[index]); + + // Is the redacted event a reaction? Then update the event this + // belongs to: + if (onChange != null) { + final relationshipEventId = events[index].relationshipEventId; + if (relationshipEventId != null) { + onChange?.call(_findEvent(event_id: relationshipEventId)); + } + } + + events[index].setRedactionEvent(Event.fromJson( + eventUpdate.content, + room, + )); + onChange?.call(index); + } + } + + if (update && !_collectHistoryUpdates) { + onUpdate?.call(); + } + } catch (e, s) { + Logs().w('Handle event update failed', e, s); + } + } +} + +extension on List { + int get firstIndexWhereNotError { + if (isEmpty) return 0; + final index = indexWhere((event) => !event.status.isError); + if (index == -1) return length; + return index; + } +} diff --git a/lib/src/user.dart b/lib/src/user.dart new file mode 100644 index 0000000..ef26688 --- /dev/null +++ b/lib/src/user.dart @@ -0,0 +1,242 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import '../matrix.dart'; + +/// Represents a Matrix User which may be a participant in a Matrix Room. +class User extends Event { + factory User( + String id, { + String? membership, + String? displayName, + String? avatarUrl, + required Room room, + }) { + return User.fromState( + stateKey: id, + content: { + if (membership != null) 'membership': membership, + if (displayName != null) 'displayname': displayName, + if (avatarUrl != null) 'avatar_url': avatarUrl, + }, + typeKey: EventTypes.RoomMember, + roomId: room.id, + room: room, + originServerTs: DateTime.now(), + ); + } + + User.fromState({ + dynamic prevContent, + required String stateKey, + dynamic content, + required String typeKey, + String eventId = 'fakevent', + String? roomId, + String senderId = 'fakesender', + required DateTime originServerTs, + dynamic unsigned, + required Room room, + }) : super( + stateKey: stateKey, + prevContent: prevContent, + content: content, + type: typeKey, + eventId: eventId, + senderId: senderId, + originServerTs: originServerTs, + unsigned: unsigned, + room: room, + ); + + /// The full qualified Matrix ID in the format @username:server.abc. + String get id => stateKey ?? '@unknown:unknown'; + + /// The displayname of the user if the user has set one. + String? get displayName => + content.tryGet('displayname') ?? + prevContent?.tryGet('displayname'); + + /// Returns the power level of this user. + int get powerLevel => room.getPowerLevelByUserId(id); + + /// The membership status of the user. One of: + /// join + /// invite + /// leave + /// ban + Membership get membership => Membership.values.firstWhere((e) { + if (content['membership'] != null) { + return e.toString() == 'Membership.' + content['membership']; + } + return false; + }, orElse: () => Membership.join); + + /// The avatar if the user has one. + Uri? get avatarUrl { + final prevContent = this.prevContent; + return content.containsKey('avatar_url') + ? (content['avatar_url'] is String + ? Uri.tryParse(content['avatar_url']) + : null) + : (prevContent != null && prevContent['avatar_url'] is String + ? Uri.tryParse(prevContent['avatar_url']) + : null); + } + + /// Returns the displayname or the local part of the Matrix ID if the user + /// has no displayname. If [formatLocalpart] is true, then the localpart will + /// be formatted in the way, that all "_" characters are becomming white spaces and + /// the first character of each word becomes uppercase. + /// If [mxidLocalPartFallback] is true, then the local part of the mxid will be shown + /// if there is no other displayname available. If not then this will return "Unknown user". + String calcDisplayname({ + bool? formatLocalpart, + bool? mxidLocalPartFallback, + }) { + formatLocalpart ??= room.client.formatLocalpart; + mxidLocalPartFallback ??= room.client.mxidLocalPartFallback; + final displayName = this.displayName; + if (displayName != null && displayName.isNotEmpty) { + return displayName; + } + final stateKey = this.stateKey; + if (stateKey != null && mxidLocalPartFallback) { + if (!formatLocalpart) { + return stateKey.localpart ?? ''; + } + final words = stateKey.localpart?.replaceAll('_', ' ').split(' ') ?? []; + for (var i = 0; i < words.length; i++) { + if (words[i].isNotEmpty) { + words[i] = words[i][0].toUpperCase() + words[i].substring(1); + } + } + return words.join(' ').trim(); + } + return 'Unknown user'; + } + + /// Call the Matrix API to kick this user from this room. + Future kick() async => await room.kick(id); + + /// Call the Matrix API to ban this user from this room. + Future ban() async => await room.ban(id); + + /// Call the Matrix API to unban this banned user from this room. + Future unban() async => await room.unban(id); + + /// Call the Matrix API to change the power level of this user. + Future setPower(int power) async => await room.setPower(id, power); + + /// Returns an existing direct chat ID with this user or creates a new one. + /// Returns null on error. + Future startDirectChat({ + bool? enableEncryption, + List? initialState, + bool waitForSync = true, + }) async => + room.client.startDirectChat( + id, + enableEncryption: enableEncryption, + initialState: initialState, + waitForSync: waitForSync, + ); + + /// The newest presence of this user if there is any and null if not. + Presence? get presence => room.client.presences[id]; + + /// Whether the client is able to ban/unban this user. + bool get canBan => room.canBan && powerLevel < room.ownPowerLevel; + + /// Whether the client is able to kick this user. + bool get canKick => + [Membership.join, Membership.invite].contains(membership) && + room.canKick && + powerLevel < room.ownPowerLevel; + + /// Whether the client is allowed to change the power level of this user. + /// Please be aware that you can only set the power level to at least your own! + bool get canChangePowerLevel => + room.canChangePowerLevel && powerLevel < room.ownPowerLevel; + + @override + bool operator ==(dynamic other) => (other is User && + other.id == id && + other.room == room && + other.membership == membership); + + /// Get the mention text to use in a plain text body to mention this specific user + /// in this specific room + String get mention { + // if the displayname has [ or ] or : we can't build our more fancy stuff, so fall back to the id + // [] is used for the delimitors + // If we allowed : we could get collissions with the mxid fallbacks + final displayName = this.displayName; + if (displayName == null || + displayName.isEmpty || + {'[', ']', ':'}.any(displayName.contains)) { + return id; + } + + final identifier = '@' + + // if we have non-word characters we need to surround with [] + (RegExp(r'^\w+$').hasMatch(displayName) + ? displayName + : '[$displayName]'); + + // get all the users with the same display name + final allUsersWithSameDisplayname = room.getParticipants(); + allUsersWithSameDisplayname.removeWhere((user) => + user.id == id || + (user.displayName?.isEmpty ?? true) || + user.displayName != displayName); + if (allUsersWithSameDisplayname.isEmpty) { + return identifier; + } + // ok, we have multiple users with the same display name....time to calculate a hash + final hashes = allUsersWithSameDisplayname.map((u) => _hash(u.id)); + final ourHash = _hash(id); + // hash collission...just return our own mxid again + if (hashes.contains(ourHash)) { + return id; + } + return '$identifier#$ourHash'; + } + + /// Get the mention fragments for this user. + Set get mentionFragments { + final displayName = this.displayName; + if (displayName == null || + displayName.isEmpty || + {'[', ']', ':'}.any(displayName.contains)) { + return {}; + } + final identifier = '@' + + // if we have non-word characters we need to surround with [] + (RegExp(r'^\w+$').hasMatch(displayName) + ? displayName + : '[$displayName]'); + + final hash = _hash(id); + return {identifier, '$identifier#$hash'}; + } +} + +const _maximumHashLength = 10000; +String _hash(String s) => + (s.codeUnits.fold(0, (a, b) => a + b) % _maximumHashLength).toString(); diff --git a/lib/src/utils/commands_extension.dart b/lib/src/utils/commands_extension.dart new file mode 100644 index 0000000..038ab4e --- /dev/null +++ b/lib/src/utils/commands_extension.dart @@ -0,0 +1,237 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:async'; + +import '../../matrix.dart'; + +extension CommandsClientExtension on Client { + /// Add a command to the command handler. `command` is its name, and `callback` is the + /// callback to invoke + void addCommand( + String command, FutureOr Function(CommandArgs) callback) { + commands[command.toLowerCase()] = callback; + } + + /// Parse and execute a string, `msg` is the input. Optionally `inReplyTo` is the event being + /// replied to and `editEventId` is the eventId of the event being replied to + Future parseAndRunCommand(Room room, String msg, + {Event? inReplyTo, String? editEventId, String? txid}) async { + final args = CommandArgs( + inReplyTo: inReplyTo, + editEventId: editEventId, + msg: '', + room: room, + txid: txid, + ); + if (!msg.startsWith('/')) { + final sendCommand = commands['send']; + if (sendCommand != null) { + args.msg = msg; + return await sendCommand(args); + } + return null; + } + // remove the / + msg = msg.substring(1); + var command = msg; + if (msg.contains(' ')) { + final idx = msg.indexOf(' '); + command = msg.substring(0, idx).toLowerCase(); + args.msg = msg.substring(idx + 1); + } else { + command = msg.toLowerCase(); + } + final commandOp = commands[command]; + if (commandOp != null) { + return await commandOp(args); + } + if (msg.startsWith('/') && commands.containsKey('send')) { + // re-set to include the "command" + final sendCommand = commands['send']; + if (sendCommand != null) { + args.msg = msg; + return await sendCommand(args); + } + } + return null; + } + + /// Unregister all commands + void unregisterAllCommands() { + commands.clear(); + } + + /// Register all default commands + void registerDefaultCommands() { + addCommand('send', (CommandArgs args) async { + return await args.room.sendTextEvent( + args.msg, + inReplyTo: args.inReplyTo, + editEventId: args.editEventId, + parseCommands: false, + txid: args.txid, + ); + }); + addCommand('me', (CommandArgs args) async { + return await args.room.sendTextEvent( + args.msg, + inReplyTo: args.inReplyTo, + editEventId: args.editEventId, + msgtype: MessageTypes.Emote, + parseCommands: false, + txid: args.txid, + ); + }); + addCommand('dm', (CommandArgs args) async { + final parts = args.msg.split(' '); + return await args.room.client.startDirectChat( + parts.first, + enableEncryption: !parts.any((part) => part == '--no-encryption'), + ); + }); + addCommand('create', (CommandArgs args) async { + final parts = args.msg.split(' '); + return await args.room.client.createGroupChat( + enableEncryption: !parts.any((part) => part == '--no-encryption'), + ); + }); + addCommand('plain', (CommandArgs args) async { + return await args.room.sendTextEvent( + args.msg, + inReplyTo: args.inReplyTo, + editEventId: args.editEventId, + parseMarkdown: false, + parseCommands: false, + txid: args.txid, + ); + }); + addCommand('html', (CommandArgs args) async { + final event = { + 'msgtype': 'm.text', + 'body': args.msg, + 'format': 'org.matrix.custom.html', + 'formatted_body': args.msg, + }; + return await args.room.sendEvent( + event, + inReplyTo: args.inReplyTo, + editEventId: args.editEventId, + txid: args.txid, + ); + }); + addCommand('react', (CommandArgs args) async { + final inReplyTo = args.inReplyTo; + if (inReplyTo == null) { + return null; + } + return await args.room.sendReaction(inReplyTo.eventId, args.msg); + }); + addCommand('join', (CommandArgs args) async { + await args.room.client.joinRoom(args.msg); + return null; + }); + addCommand('leave', (CommandArgs args) async { + await args.room.leave(); + return ''; + }); + addCommand('op', (CommandArgs args) async { + final parts = args.msg.split(' '); + if (parts.isEmpty) { + return null; + } + int? pl; + if (parts.length >= 2) { + pl = int.tryParse(parts[1]); + } + final mxid = parts.first; + return await args.room.setPower(mxid, pl ?? 50); + }); + addCommand('kick', (CommandArgs args) async { + final parts = args.msg.split(' '); + await args.room.kick(parts.first); + return ''; + }); + addCommand('ban', (CommandArgs args) async { + final parts = args.msg.split(' '); + await args.room.ban(parts.first); + return ''; + }); + addCommand('unban', (CommandArgs args) async { + final parts = args.msg.split(' '); + await args.room.unban(parts.first); + return ''; + }); + addCommand('invite', (CommandArgs args) async { + final parts = args.msg.split(' '); + await args.room.invite(parts.first); + return ''; + }); + addCommand('myroomnick', (CommandArgs args) async { + final currentEventJson = args.room + .getState(EventTypes.RoomMember, args.room.client.userID!) + ?.content + .copy() ?? + {}; + currentEventJson['displayname'] = args.msg; + return await args.room.client.setRoomStateWithKey( + args.room.id, + EventTypes.RoomMember, + args.room.client.userID!, + currentEventJson, + ); + }); + addCommand('myroomavatar', (CommandArgs args) async { + final currentEventJson = args.room + .getState(EventTypes.RoomMember, args.room.client.userID!) + ?.content + .copy() ?? + {}; + currentEventJson['avatar_url'] = args.msg; + return await args.room.client.setRoomStateWithKey( + args.room.id, + EventTypes.RoomMember, + args.room.client.userID!, + currentEventJson, + ); + }); + addCommand('discardsession', (CommandArgs args) async { + await encryption?.keyManager + .clearOrUseOutboundGroupSession(args.room.id, wipe: true); + return ''; + }); + addCommand('clearcache', (CommandArgs args) async { + await clearCache(); + return ''; + }); + } +} + +class CommandArgs { + String msg; + String? editEventId; + Event? inReplyTo; + Room room; + String? txid; + CommandArgs( + {required this.msg, + this.editEventId, + this.inReplyTo, + required this.room, + this.txid}); +} diff --git a/lib/src/utils/crypto/crypto.dart b/lib/src/utils/crypto/crypto.dart new file mode 100644 index 0000000..7bbddf8 --- /dev/null +++ b/lib/src/utils/crypto/crypto.dart @@ -0,0 +1,29 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +export 'native.dart' if (dart.library.js) 'js.dart'; + +import 'dart:typed_data'; +import 'dart:math'; + +Uint8List secureRandomBytes(int len) { + final rng = Random.secure(); + final list = Uint8List(len); + list.setAll(0, Iterable.generate(list.length, (i) => rng.nextInt(256))); + return list; +} diff --git a/lib/src/utils/crypto/encrypted_file.dart b/lib/src/utils/crypto/encrypted_file.dart new file mode 100644 index 0000000..d3dd874 --- /dev/null +++ b/lib/src/utils/crypto/encrypted_file.dart @@ -0,0 +1,60 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +import 'dart:typed_data'; +import 'dart:convert'; + +import 'package:matrix/encryption/utils/base64_unpadded.dart'; + +import 'crypto.dart'; + +class EncryptedFile { + EncryptedFile({ + required this.data, + required this.k, + required this.iv, + required this.sha256, + }); + Uint8List data; + String k; + String iv; + String sha256; +} + +Future encryptFile(Uint8List input) async { + final key = secureRandomBytes(32); + final iv = secureRandomBytes(16); + final data = await aesCtr.encrypt(input, key, iv); + final hash = await sha256(data); + return EncryptedFile( + data: data, + k: base64Url.encode(key).replaceAll('=', ''), + iv: base64.encode(iv).replaceAll('=', ''), + sha256: base64.encode(hash).replaceAll('=', ''), + ); +} + +Future decryptFile(EncryptedFile input) async { + if (base64.encode(await sha256(input.data)) != + base64.normalize(input.sha256)) { + return null; + } + + final key = base64decodeUnpadded(base64.normalize(input.k)); + final iv = base64decodeUnpadded(base64.normalize(input.iv)); + return await aesCtr.encrypt(input.data, key, iv); +} diff --git a/lib/src/utils/crypto/ffi.dart b/lib/src/utils/crypto/ffi.dart new file mode 100644 index 0000000..0cb7926 --- /dev/null +++ b/lib/src/utils/crypto/ffi.dart @@ -0,0 +1,123 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +import 'dart:ffi'; +import 'dart:io'; + +final libcrypto = Platform.isIOS + ? DynamicLibrary.process() + : DynamicLibrary.open(Platform.isAndroid + ? 'libcrypto.so' + : Platform.isWindows + ? 'libcrypto.dll' + : Platform.isMacOS + ? 'libcrypto.1.1.dylib' + : 'libcrypto.so.1.1'); + +final PKCS5_PBKDF2_HMAC = libcrypto.lookupFunction< + IntPtr Function( + Pointer pass, + IntPtr passlen, + Pointer salt, + IntPtr saltlen, + IntPtr iter, + Pointer digest, + IntPtr keylen, + Pointer out), + int Function( + Pointer pass, + int passlen, + Pointer salt, + int saltlen, + int iter, + Pointer digest, + int keylen, + Pointer out)>('PKCS5_PBKDF2_HMAC'); + +final EVP_sha1 = libcrypto.lookupFunction Function(), + Pointer Function()>('EVP_sha1'); + +final EVP_sha256 = libcrypto.lookupFunction Function(), + Pointer Function()>('EVP_sha256'); + +final EVP_sha512 = libcrypto.lookupFunction Function(), + Pointer Function()>('EVP_sha512'); + +final EVP_aes_128_ctr = libcrypto.lookupFunction Function(), + Pointer Function()>('EVP_aes_128_ctr'); + +final EVP_aes_256_ctr = libcrypto.lookupFunction Function(), + Pointer Function()>('EVP_aes_256_ctr'); + +final EVP_CIPHER_CTX_new = libcrypto.lookupFunction< + Pointer Function(), + Pointer Function()>('EVP_CIPHER_CTX_new'); + +final EVP_EncryptInit_ex = libcrypto.lookupFunction< + Pointer Function( + Pointer ctx, + Pointer alg, + Pointer some, + Pointer key, + Pointer iv), + Pointer Function( + Pointer ctx, + Pointer alg, + Pointer some, + Pointer key, + Pointer iv)>('EVP_EncryptInit_ex'); + +final EVP_EncryptUpdate = libcrypto.lookupFunction< + Pointer Function(Pointer ctx, Pointer output, + Pointer outputLen, Pointer input, IntPtr inputLen), + Pointer Function( + Pointer ctx, + Pointer output, + Pointer outputLen, + Pointer input, + int inputLen)>('EVP_EncryptUpdate'); + +final EVP_EncryptFinal_ex = libcrypto.lookupFunction< + Pointer Function( + Pointer ctx, Pointer data, Pointer len), + Pointer Function(Pointer ctx, Pointer data, + Pointer len)>('EVP_EncryptFinal_ex'); + +final EVP_CIPHER_CTX_free = libcrypto.lookupFunction< + Pointer Function(Pointer ctx), + Pointer Function( + Pointer ctx)>('EVP_CIPHER_CTX_free'); + +final EVP_Digest = libcrypto.lookupFunction< + IntPtr Function( + Pointer data, + IntPtr len, + Pointer hash, + Pointer hsize, + Pointer alg, + Pointer engine), + int Function( + Pointer data, + int len, + Pointer hash, + Pointer hsize, + Pointer alg, + Pointer engine)>('EVP_Digest'); + +final EVP_MD_size = libcrypto.lookupFunction< + IntPtr Function(Pointer ctx), + int Function(Pointer ctx)>('EVP_MD_size'); diff --git a/lib/src/utils/crypto/js.dart b/lib/src/utils/crypto/js.dart new file mode 100644 index 0000000..b889720 --- /dev/null +++ b/lib/src/utils/crypto/js.dart @@ -0,0 +1,64 @@ +// Copyright (c) 2020 Famedly GmbH +// SPDX-License-Identifier: AGPL-3.0-or-later + +import 'dart:typed_data'; + +import 'subtle.dart'; +import 'subtle.dart' as subtle; + +abstract class Hash { + Hash._(this.name); + String name; + + Future call(Uint8List input) async => + Uint8List.view(await digest(name, input)); +} + +final Hash sha1 = _Sha1(); +final Hash sha256 = _Sha256(); +final Hash sha512 = _Sha512(); + +class _Sha1 extends Hash { + _Sha1() : super._('SHA-1'); +} + +class _Sha256 extends Hash { + _Sha256() : super._('SHA-256'); +} + +class _Sha512 extends Hash { + _Sha512() : super._('SHA-512'); +} + +abstract class Cipher { + Cipher._(this.name); + String name; + Object params(Uint8List iv); + Future encrypt( + Uint8List input, Uint8List key, Uint8List iv) async { + final subtleKey = await importKey('raw', key, name, false, ['encrypt']); + return (await subtle.encrypt(params(iv), subtleKey, input)).asUint8List(); + } +} + +final Cipher aesCtr = _AesCtr(); + +class _AesCtr extends Cipher { + _AesCtr() : super._('AES-CTR'); + + @override + Object params(Uint8List iv) => + AesCtrParams(name: name, counter: iv, length: 64); +} + +Future pbkdf2(Uint8List passphrase, Uint8List salt, Hash hash, + int iterations, int bits) async { + final raw = + await importKey('raw', passphrase, 'PBKDF2', false, ['deriveBits']); + final res = await deriveBits( + Pbkdf2Params( + name: 'PBKDF2', hash: hash.name, salt: salt, iterations: iterations), + raw, + bits); + return Uint8List.view(res); +} diff --git a/lib/src/utils/crypto/native.dart b/lib/src/utils/crypto/native.dart new file mode 100644 index 0000000..ce31785 --- /dev/null +++ b/lib/src/utils/crypto/native.dart @@ -0,0 +1,102 @@ +import 'dart:async'; +import 'dart:typed_data'; +import 'dart:ffi'; +import 'package:ffi/ffi.dart'; + +import 'ffi.dart'; + +abstract class Hash { + Hash._(this.ptr); + Pointer ptr; + + FutureOr call(Uint8List data) { + final outSize = EVP_MD_size(ptr); + final mem = malloc.call(outSize + data.length); + final dataMem = mem.elementAt(outSize); + try { + dataMem.asTypedList(data.length).setAll(0, data); + EVP_Digest(dataMem, data.length, mem, nullptr, ptr, nullptr); + return Uint8List.fromList(mem.asTypedList(outSize)); + } finally { + malloc.free(mem); + } + } +} + +final Hash sha1 = _Sha1(); +final Hash sha256 = _Sha256(); +final Hash sha512 = _Sha512(); + +class _Sha1 extends Hash { + _Sha1() : super._(EVP_sha1()); +} + +class _Sha256 extends Hash { + _Sha256() : super._(EVP_sha256()); +} + +class _Sha512 extends Hash { + _Sha512() : super._(EVP_sha512()); +} + +abstract class Cipher { + Cipher._(); + Pointer getAlg(int keysize); + FutureOr encrypt(Uint8List input, Uint8List key, Uint8List iv) { + final alg = getAlg(key.length * 8); + final mem = malloc + .call(sizeOf() + key.length + iv.length + input.length); + final lenMem = mem.cast(); + final keyMem = mem.elementAt(sizeOf()); + final ivMem = keyMem.elementAt(key.length); + final dataMem = ivMem.elementAt(iv.length); + try { + keyMem.asTypedList(key.length).setAll(0, key); + ivMem.asTypedList(iv.length).setAll(0, iv); + dataMem.asTypedList(input.length).setAll(0, input); + final ctx = EVP_CIPHER_CTX_new(); + EVP_EncryptInit_ex(ctx, alg, nullptr, keyMem, ivMem); + EVP_EncryptUpdate(ctx, dataMem, lenMem, dataMem, input.length); + EVP_EncryptFinal_ex(ctx, dataMem.elementAt(lenMem.value), lenMem); + EVP_CIPHER_CTX_free(ctx); + return Uint8List.fromList(dataMem.asTypedList(input.length)); + } finally { + malloc.free(mem); + } + } +} + +final Cipher aesCtr = _AesCtr(); + +class _AesCtr extends Cipher { + _AesCtr() : super._(); + + @override + Pointer getAlg(int keysize) { + switch (keysize) { + case 128: + return EVP_aes_128_ctr(); + case 256: + return EVP_aes_256_ctr(); + default: + throw ArgumentError('invalid key size'); + } + } +} + +FutureOr pbkdf2( + Uint8List passphrase, Uint8List salt, Hash hash, int iterations, int bits) { + final outLen = bits ~/ 8; + final mem = malloc.call(passphrase.length + salt.length + outLen); + final saltMem = mem.elementAt(passphrase.length); + final outMem = saltMem.elementAt(salt.length); + try { + mem.asTypedList(passphrase.length).setAll(0, passphrase); + saltMem.asTypedList(salt.length).setAll(0, salt); + PKCS5_PBKDF2_HMAC(mem, passphrase.length, saltMem, salt.length, iterations, + hash.ptr, outLen, outMem); + return Uint8List.fromList(outMem.asTypedList(outLen)); + } finally { + malloc.free(mem); + } +} diff --git a/lib/src/utils/crypto/subtle.dart b/lib/src/utils/crypto/subtle.dart new file mode 100644 index 0000000..a96ea31 --- /dev/null +++ b/lib/src/utils/crypto/subtle.dart @@ -0,0 +1,93 @@ +// Copyright (c) 2020 Famedly GmbH +// SPDX-License-Identifier: AGPL-3.0-or-later + +@JS() +library subtle; + +import 'package:js/js.dart'; +import 'dart:async'; +import 'dart:js_util'; +import 'dart:typed_data'; + +@JS() +@anonymous +class Pbkdf2Params { + external factory Pbkdf2Params({ + String name, + String hash, + Uint8List salt, + int iterations, + }); + String? name; + String? hash; + Uint8List? salt; + int? iterations; +} + +@JS() +@anonymous +class AesCtrParams { + external factory AesCtrParams({ + String name, + Uint8List counter, + int length, + }); + String? name; + Uint8List? counter; + int? length; +} + +@JS('crypto.subtle.encrypt') +external dynamic _encrypt(dynamic algorithm, dynamic key, Uint8List data); + +Future encrypt(dynamic algorithm, dynamic key, Uint8List data) { + return promiseToFuture(_encrypt(algorithm, key, data)); +} + +@JS('crypto.subtle.decrypt') +external dynamic _decrypt(dynamic algorithm, dynamic key, Uint8List data); + +Future decrypt(dynamic algorithm, dynamic key, Uint8List data) { + return promiseToFuture(_decrypt(algorithm, key, data)); +} + +@JS('crypto.subtle.importKey') +external dynamic _importKey(String format, dynamic keyData, dynamic algorithm, + bool extractable, List keyUsages); + +Future importKey(String format, dynamic keyData, dynamic algorithm, + bool extractable, List keyUsages) { + return promiseToFuture( + _importKey(format, keyData, algorithm, extractable, keyUsages)); +} + +@JS('crypto.subtle.exportKey') +external dynamic _exportKey(String algorithm, dynamic key); + +Future exportKey(String algorithm, dynamic key) { + return promiseToFuture(_exportKey(algorithm, key)); +} + +@JS('crypto.subtle.deriveKey') +external dynamic _deriveKey(dynamic algorithm, dynamic baseKey, + dynamic derivedKeyAlgorithm, bool extractable, List keyUsages); + +Future deriveKey(dynamic algorithm, dynamic baseKey, + dynamic derivedKeyAlgorithm, bool extractable, List keyUsages) { + return promiseToFuture(_deriveKey( + algorithm, baseKey, derivedKeyAlgorithm, extractable, keyUsages)); +} + +@JS('crypto.subtle.deriveBits') +external dynamic _deriveBits(dynamic algorithm, dynamic baseKey, int length); + +Future deriveBits(dynamic algorithm, dynamic baseKey, int length) { + return promiseToFuture(_deriveBits(algorithm, baseKey, length)); +} + +@JS('crypto.subtle.digest') +external dynamic _digest(String algorithm, Uint8List data); + +Future digest(String algorithm, Uint8List data) { + return promiseToFuture(_digest(algorithm, data)); +} diff --git a/lib/src/utils/device_keys_list.dart b/lib/src/utils/device_keys_list.dart new file mode 100644 index 0000000..9661e2c --- /dev/null +++ b/lib/src/utils/device_keys_list.dart @@ -0,0 +1,515 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:convert'; + +import 'package:canonical_json/canonical_json.dart'; +import 'package:collection/collection.dart' show IterableExtension; +import 'package:matrix/matrix.dart'; +import 'package:olm/olm.dart' as olm; + +import '../../encryption.dart'; + +enum UserVerifiedStatus { verified, unknown, unknownDevice } + +class DeviceKeysList { + Client client; + String userId; + bool outdated = true; + Map deviceKeys = {}; + Map crossSigningKeys = {}; + + SignableKey? getKey(String id) => deviceKeys[id] ?? crossSigningKeys[id]; + + CrossSigningKey? getCrossSigningKey(String type) => + crossSigningKeys.values.firstWhereOrNull((k) => k.usage.contains(type)); + + CrossSigningKey? get masterKey => getCrossSigningKey('master'); + CrossSigningKey? get selfSigningKey => getCrossSigningKey('self_signing'); + CrossSigningKey? get userSigningKey => getCrossSigningKey('user_signing'); + + UserVerifiedStatus get verified { + if (masterKey == null) { + return UserVerifiedStatus.unknown; + } + if (masterKey!.verified) { + for (final key in deviceKeys.values) { + if (!key.verified) { + return UserVerifiedStatus.unknownDevice; + } + } + return UserVerifiedStatus.verified; + } else { + for (final key in deviceKeys.values) { + if (!key.verified) { + return UserVerifiedStatus.unknown; + } + } + return UserVerifiedStatus.verified; + } + } + + /// Starts a verification with this device. This might need to create a new + /// direct chat to send the verification request over this room. For this you + /// can set parameters here. + Future startVerification({ + bool? newDirectChatEnableEncryption, + List? newDirectChatInitialState, + }) async { + final encryption = client.encryption; + if (encryption == null) { + throw Exception('Encryption not enabled'); + } + if (userId != client.userID) { + // in-room verification with someone else + final roomId = await client.startDirectChat( + userId, + enableEncryption: newDirectChatEnableEncryption, + initialState: newDirectChatInitialState, + waitForSync: false, + ); + + final room = + client.getRoomById(roomId) ?? Room(id: roomId, client: client); + final request = + KeyVerification(encryption: encryption, room: room, userId: userId); + await request.start(); + // no need to add to the request client object. As we are doing a room + // verification request that'll happen automatically once we know the transaction id + return request; + } else { + // broadcast self-verification + final request = KeyVerification( + encryption: encryption, userId: userId, deviceId: '*'); + await request.start(); + encryption.keyVerificationManager.addRequest(request); + return request; + } + } + + DeviceKeysList.fromDbJson( + Map dbEntry, + List> childEntries, + List> crossSigningEntries, + Client cl) + : client = cl, + userId = dbEntry['user_id'] ?? '' { + outdated = dbEntry['outdated']; + deviceKeys = {}; + for (final childEntry in childEntries) { + final entry = DeviceKeys.fromDb(childEntry, client); + if (entry.isValid) { + deviceKeys[childEntry['device_id']] = entry; + } else { + outdated = true; + } + } + for (final crossSigningEntry in crossSigningEntries) { + final entry = CrossSigningKey.fromDbJson(crossSigningEntry, client); + if (entry.isValid) { + crossSigningKeys[crossSigningEntry['public_key']] = entry; + } else { + outdated = true; + } + } + } + + DeviceKeysList(this.userId, this.client); +} + +class SimpleSignableKey extends MatrixSignableKey { + @override + String? identifier; + + SimpleSignableKey.fromJson(Map json) : super.fromJson(json); +} + +abstract class SignableKey extends MatrixSignableKey { + Client client; + Map? validSignatures; + bool? _verified; + bool? _blocked; + + String? get ed25519Key => keys['ed25519:$identifier']; + bool get verified => + identifier != null && (directVerified || crossVerified) && !(blocked); + bool get blocked => _blocked ?? false; + set blocked(bool b) => _blocked = b; + + bool get encryptToDevice => + !(blocked) && + identifier != null && + ed25519Key != null && + (client.userDeviceKeys[userId]?.masterKey?.verified ?? false + ? verified + : true); + + void setDirectVerified(bool v) { + _verified = v; + } + + bool get directVerified => _verified ?? false; + bool get crossVerified => hasValidSignatureChain(); + bool get signed => hasValidSignatureChain(verifiedOnly: false); + + SignableKey.fromJson(Map json, Client cl) + : client = cl, + super.fromJson(json) { + _verified = false; + _blocked = false; + } + + SimpleSignableKey cloneForSigning() { + final newKey = SimpleSignableKey.fromJson(toJson().copy()); + newKey.identifier = identifier; + (newKey.signatures ??= {}).clear(); + return newKey; + } + + String get signingContent { + final data = super.toJson().copy(); + // some old data might have the custom verified and blocked keys + data.remove('verified'); + data.remove('blocked'); + // remove the keys not needed for signing + data.remove('unsigned'); + data.remove('signatures'); + return String.fromCharCodes(canonicalJson.encode(data)); + } + + bool _verifySignature(String pubKey, String signature, + {bool isSignatureWithoutLibolmValid = false}) { + olm.Utility olmutil; + try { + olmutil = olm.Utility(); + } catch (e) { + // if no libolm is present we land in this catch block, and return the default + // set if no libolm is there. Some signatures should be assumed-valid while others + // should be assumed-invalid + return isSignatureWithoutLibolmValid; + } + var valid = false; + try { + olmutil.ed25519_verify(pubKey, signingContent, signature); + valid = true; + } catch (_) { + // bad signature + valid = false; + } finally { + olmutil.free(); + } + return valid; + } + + bool hasValidSignatureChain( + {bool verifiedOnly = true, + Set? visited, + Set? onlyValidateUserIds}) { + if (!client.encryptionEnabled) { + return false; + } + + final visited_ = visited ?? {}; + final onlyValidateUserIds_ = onlyValidateUserIds ?? {}; + + final setKey = '$userId;$identifier'; + if (visited_.contains(setKey) || + (onlyValidateUserIds_.isNotEmpty && + !onlyValidateUserIds_.contains(userId))) { + return false; // prevent recursion & validate hasValidSignatureChain + } + visited_.add(setKey); + + if (signatures == null) return false; + + for (final signatureEntries in signatures!.entries) { + final otherUserId = signatureEntries.key; + if (!client.userDeviceKeys.containsKey(otherUserId)) { + continue; + } + // we don't allow transitive trust unless it is for ourself + if (otherUserId != userId && otherUserId != client.userID) { + continue; + } + for (final signatureEntry in signatureEntries.value.entries) { + final fullKeyId = signatureEntry.key; + final signature = signatureEntry.value; + final keyId = fullKeyId.substring('ed25519:'.length); + // we ignore self-signatures here + if (otherUserId == userId && keyId == identifier) { + continue; + } + + final key = client.userDeviceKeys[otherUserId]?.deviceKeys[keyId] ?? + client.userDeviceKeys[otherUserId]?.crossSigningKeys[keyId]; + if (key == null) { + continue; + } + + if (onlyValidateUserIds_.isNotEmpty && + !onlyValidateUserIds_.contains(key.userId)) { + // we don't want to verify keys from this user + continue; + } + + if (key.blocked) { + continue; // we can't be bothered about this keys signatures + } + var haveValidSignature = false; + var gotSignatureFromCache = false; + final fullKeyIdBool = validSignatures + ?.tryGetMap(otherUserId) + ?.tryGet(fullKeyId); + if (fullKeyIdBool == true) { + haveValidSignature = true; + gotSignatureFromCache = true; + } else if (fullKeyIdBool == false) { + haveValidSignature = false; + gotSignatureFromCache = true; + } + + if (!gotSignatureFromCache && key.ed25519Key != null) { + // validate the signature manually + haveValidSignature = _verifySignature(key.ed25519Key!, signature); + final validSignatures = this.validSignatures ??= {}; + if (!validSignatures.containsKey(otherUserId)) { + validSignatures[otherUserId] = {}; + } + validSignatures[otherUserId][fullKeyId] = haveValidSignature; + } + if (!haveValidSignature) { + // no valid signature, this key is useless + continue; + } + + if ((verifiedOnly && key.directVerified) || + (key is CrossSigningKey && + key.usage.contains('master') && + key.directVerified && + key.userId == client.userID)) { + return true; // we verified this key and it is valid...all checks out! + } + // or else we just recurse into that key and chack if it works out + final haveChain = key.hasValidSignatureChain( + verifiedOnly: verifiedOnly, + visited: visited_, + onlyValidateUserIds: onlyValidateUserIds); + if (haveChain) { + return true; + } + } + } + return false; + } + + Future setVerified(bool newVerified, [bool sign = true]) async { + _verified = newVerified; + final encryption = client.encryption; + if (newVerified && + sign && + encryption != null && + client.encryptionEnabled && + encryption.crossSigning.signable([this])) { + // sign the key! + // ignore: unawaited_futures + encryption.crossSigning.sign([this]); + } + } + + Future setBlocked(bool newBlocked); + + @override + Map toJson() { + final data = super.toJson().copy(); + // some old data may have the verified and blocked keys which are unneeded now + data.remove('verified'); + data.remove('blocked'); + return data; + } + + @override + String toString() => json.encode(toJson()); + + @override + bool operator ==(dynamic other) => (other is SignableKey && + other.userId == userId && + other.identifier == identifier); +} + +class CrossSigningKey extends SignableKey { + @override + String? identifier; + + String? get publicKey => identifier; + late List usage; + + bool get isValid => + userId.isNotEmpty && + publicKey != null && + keys.isNotEmpty && + ed25519Key != null; + + @override + Future setVerified(bool newVerified, [bool sign = true]) async { + if (!isValid) { + throw Exception('setVerified called on invalid key'); + } + await super.setVerified(newVerified, sign); + await client.database + ?.setVerifiedUserCrossSigningKey(newVerified, userId, publicKey!); + } + + @override + Future setBlocked(bool newBlocked) async { + if (!isValid) { + throw Exception('setBlocked called on invalid key'); + } + _blocked = newBlocked; + await client.database + ?.setBlockedUserCrossSigningKey(newBlocked, userId, publicKey!); + } + + CrossSigningKey.fromMatrixCrossSigningKey(MatrixCrossSigningKey k, Client cl) + : super.fromJson(k.toJson().copy(), cl) { + final json = toJson(); + identifier = k.publicKey; + usage = json['usage'].cast(); + } + + CrossSigningKey.fromDbJson(Map dbEntry, Client cl) + : super.fromJson(Event.getMapFromPayload(dbEntry['content']), cl) { + final json = toJson(); + identifier = dbEntry['public_key']; + usage = json['usage'].cast(); + _verified = dbEntry['verified']; + _blocked = dbEntry['blocked']; + } + + CrossSigningKey.fromJson(Map json, Client cl) + : super.fromJson(json.copy(), cl) { + final json = toJson(); + usage = json['usage'].cast(); + if (keys.isNotEmpty) { + identifier = keys.values.first; + } + } +} + +class DeviceKeys extends SignableKey { + @override + String? identifier; + + String? get deviceId => identifier; + late List algorithms; + late DateTime lastActive; + + String? get curve25519Key => keys['curve25519:$deviceId']; + String? get deviceDisplayName => unsigned?['device_display_name']; + + bool? _validSelfSignature; + bool get selfSigned => + _validSelfSignature ?? + (_validSelfSignature = (deviceId != null && + signatures + ?.tryGetMap(userId) + ?.tryGet('ed25519:$deviceId') == + null + ? false + // without libolm we still want to be able to add devices. In that case we ofc just can't + // verify the signature + : _verifySignature( + ed25519Key!, signatures![userId]!['ed25519:$deviceId']!, + isSignatureWithoutLibolmValid: true))); + + @override + bool get blocked => super.blocked || !selfSigned; + + bool get isValid => + deviceId != null && + keys.isNotEmpty && + curve25519Key != null && + ed25519Key != null && + selfSigned; + + @override + Future setVerified(bool newVerified, [bool sign = true]) async { + if (!isValid) { + //throw Exception('setVerified called on invalid key'); + return; + } + await super.setVerified(newVerified, sign); + await client.database + ?.setVerifiedUserDeviceKey(newVerified, userId, deviceId!); + } + + @override + Future setBlocked(bool newBlocked) async { + if (!isValid) { + //throw Exception('setBlocked called on invalid key'); + return; + } + _blocked = newBlocked; + await client.database + ?.setBlockedUserDeviceKey(newBlocked, userId, deviceId!); + } + + DeviceKeys.fromMatrixDeviceKeys(MatrixDeviceKeys k, Client cl, + [DateTime? lastActiveTs]) + : super.fromJson(k.toJson().copy(), cl) { + final json = toJson(); + identifier = k.deviceId; + algorithms = json['algorithms'].cast(); + lastActive = lastActiveTs ?? DateTime.now(); + } + + DeviceKeys.fromDb(Map dbEntry, Client cl) + : super.fromJson(Event.getMapFromPayload(dbEntry['content']), cl) { + final json = toJson(); + identifier = dbEntry['device_id']; + algorithms = json['algorithms'].cast(); + _verified = dbEntry['verified']; + _blocked = dbEntry['blocked']; + lastActive = + DateTime.fromMillisecondsSinceEpoch(dbEntry['last_active'] ?? 0); + } + + DeviceKeys.fromJson(Map json, Client cl) + : super.fromJson(json.copy(), cl) { + final json = toJson(); + identifier = json['device_id']; + algorithms = json['algorithms'].cast(); + lastActive = DateTime.fromMillisecondsSinceEpoch(0); + } + + KeyVerification startVerification() { + if (!isValid) { + throw Exception('setVerification called on invalid key'); + } + final encryption = client.encryption; + if (encryption == null) { + throw Exception('setVerification called with disabled encryption'); + } + + final request = KeyVerification( + encryption: encryption, userId: userId, deviceId: deviceId!); + + request.start(); + encryption.keyVerificationManager.addRequest(request); + return request; + } +} diff --git a/lib/src/utils/event_localizations.dart b/lib/src/utils/event_localizations.dart new file mode 100644 index 0000000..fcc5488 --- /dev/null +++ b/lib/src/utils/event_localizations.dart @@ -0,0 +1,224 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:collection/collection.dart'; + +import '../../encryption.dart'; +import '../../matrix.dart'; + +abstract class EventLocalizations { + // As we need to create the localized body off of a different set of parameters, we + // might create it with `event.plaintextBody`, maybe with `event.body`, maybe with the + // reply fallback stripped, and maybe with the new body in `event.content['m.new_content']`. + // Thus, it seems easier to offload that logic into `Event.getLocalizedBody()` and pass the + // `body` variable around here. + static String _localizedBodyNormalMessage( + Event event, MatrixLocalizations i18n, String body) { + switch (event.messageType) { + case MessageTypes.Image: + return i18n.sentAPicture(event.sender.calcDisplayname()); + case MessageTypes.File: + return i18n.sentAFile(event.sender.calcDisplayname()); + case MessageTypes.Audio: + return i18n.sentAnAudio(event.sender.calcDisplayname()); + case MessageTypes.Video: + return i18n.sentAVideo(event.sender.calcDisplayname()); + case MessageTypes.Location: + return i18n.sharedTheLocation(event.sender.calcDisplayname()); + case MessageTypes.Sticker: + return i18n.sentASticker(event.sender.calcDisplayname()); + case MessageTypes.Emote: + return '* $body'; + case MessageTypes.BadEncrypted: + String errorText; + switch (event.body) { + case DecryptException.channelCorrupted: + errorText = i18n.channelCorruptedDecryptError + '.'; + break; + case DecryptException.notEnabled: + errorText = i18n.encryptionNotEnabled + '.'; + break; + case DecryptException.unknownAlgorithm: + errorText = i18n.unknownEncryptionAlgorithm + '.'; + break; + case DecryptException.unknownSession: + errorText = i18n.noPermission + '.'; + break; + default: + errorText = body; + break; + } + return i18n.couldNotDecryptMessage(errorText); + case MessageTypes.Text: + case MessageTypes.Notice: + case MessageTypes.None: + default: + return body; + } + } + + // This map holds how to localize event types, and thus which event types exist. + // If an event exists but it does not have a localized body, set its callback to null + static final Map + localizationsMap = { + EventTypes.Sticker: (event, i18n, body) => + i18n.sentASticker(event.sender.calcDisplayname()), + EventTypes.Redaction: (event, i18n, body) => + i18n.redactedAnEvent(event.sender.calcDisplayname()), + EventTypes.RoomAliases: (event, i18n, body) => + i18n.changedTheRoomAliases(event.sender.calcDisplayname()), + EventTypes.RoomCanonicalAlias: (event, i18n, body) => + i18n.changedTheRoomInvitationLink(event.sender.calcDisplayname()), + EventTypes.RoomCreate: (event, i18n, body) => + i18n.createdTheChat(event.sender.calcDisplayname()), + EventTypes.RoomTombstone: (event, i18n, body) => i18n.roomHasBeenUpgraded, + EventTypes.RoomJoinRules: (event, i18n, body) { + final joinRules = JoinRules.values.firstWhereOrNull((r) => + r.toString().replaceAll('JoinRules.', '') == + event.content['join_rule']); + if (joinRules == null) { + return i18n.changedTheJoinRules(event.sender.calcDisplayname()); + } else { + return i18n.changedTheJoinRulesTo( + event.sender.calcDisplayname(), joinRules.getLocalizedString(i18n)); + } + }, + EventTypes.RoomMember: (event, i18n, body) { + var text = 'Failed to parse member event'; + final targetName = event.stateKeyUser?.calcDisplayname() ?? ''; + // Has the membership changed? + final newMembership = event.content['membership'] ?? ''; + final oldMembership = event.prevContent?['membership'] ?? ''; + + if (newMembership != oldMembership) { + if (oldMembership == 'invite' && newMembership == 'join') { + text = i18n.acceptedTheInvitation(targetName); + } else if (oldMembership == 'invite' && newMembership == 'leave') { + if (event.stateKey == event.senderId) { + text = i18n.rejectedTheInvitation(targetName); + } else { + text = i18n.hasWithdrawnTheInvitationFor( + event.sender.calcDisplayname(), targetName); + } + } else if (oldMembership == 'leave' && newMembership == 'join') { + text = i18n.joinedTheChat(targetName); + } else if (oldMembership == 'join' && newMembership == 'ban') { + text = + i18n.kickedAndBanned(event.sender.calcDisplayname(), targetName); + } else if (oldMembership == 'join' && + newMembership == 'leave' && + event.stateKey != event.senderId) { + text = i18n.kicked(event.sender.calcDisplayname(), targetName); + } else if (oldMembership == 'join' && + newMembership == 'leave' && + event.stateKey == event.senderId) { + text = i18n.userLeftTheChat(targetName); + } else if (oldMembership == 'invite' && newMembership == 'ban') { + text = i18n.bannedUser(event.sender.calcDisplayname(), targetName); + } else if (oldMembership == 'leave' && newMembership == 'ban') { + text = i18n.bannedUser(event.sender.calcDisplayname(), targetName); + } else if (oldMembership == 'ban' && newMembership == 'leave') { + text = i18n.unbannedUser(event.sender.calcDisplayname(), targetName); + } else if (newMembership == 'invite') { + text = i18n.invitedUser(event.sender.calcDisplayname(), targetName); + } else if (newMembership == 'join') { + text = i18n.joinedTheChat(targetName); + } + } else if (newMembership == 'join') { + final newAvatar = event.content.tryGet('avatar_url') ?? ''; + final oldAvatar = event.prevContent?.tryGet('avatar_url') ?? ''; + + final newDisplayname = + event.content.tryGet('displayname') ?? ''; + final oldDisplayname = + event.prevContent?.tryGet('displayname') ?? ''; + final stateKey = event.stateKey; + + // Has the user avatar changed? + if (newAvatar != oldAvatar) { + text = i18n.changedTheProfileAvatar(targetName); + } + // Has the user displayname changed? + else if (newDisplayname != oldDisplayname && stateKey != null) { + text = i18n.changedTheDisplaynameTo(oldDisplayname, newDisplayname); + } + } + return text; + }, + EventTypes.RoomPowerLevels: (event, i18n, body) => + i18n.changedTheChatPermissions(event.sender.calcDisplayname()), + EventTypes.RoomName: (event, i18n, body) => i18n.changedTheChatNameTo( + event.sender.calcDisplayname(), event.content['name']), + EventTypes.RoomTopic: (event, i18n, body) => + i18n.changedTheChatDescriptionTo( + event.sender.calcDisplayname(), event.content['topic']), + EventTypes.RoomAvatar: (event, i18n, body) => + i18n.changedTheChatAvatar(event.sender.calcDisplayname()), + EventTypes.GuestAccess: (event, i18n, body) { + final guestAccess = GuestAccess.values.firstWhereOrNull((r) => + r.toString().replaceAll('GuestAccess.', '') == + event.content['guest_access']); + if (guestAccess == null) { + return i18n.changedTheGuestAccessRules(event.sender.calcDisplayname()); + } else { + return i18n.changedTheGuestAccessRulesTo(event.sender.calcDisplayname(), + guestAccess.getLocalizedString(i18n)); + } + }, + EventTypes.HistoryVisibility: (event, i18n, body) { + final historyVisibility = HistoryVisibility.values.firstWhereOrNull((r) => + r.toString().replaceAll('HistoryVisibility.', '') == + event.content['history_visibility']); + if (historyVisibility == null) { + return i18n.changedTheHistoryVisibility(event.sender.calcDisplayname()); + } else { + return i18n.changedTheHistoryVisibilityTo( + event.sender.calcDisplayname(), + historyVisibility.getLocalizedString(i18n)); + } + }, + EventTypes.Encryption: (event, i18n, body) { + var localizedBody = + i18n.activatedEndToEndEncryption(event.sender.calcDisplayname()); + if (event.room.client.encryptionEnabled == false) { + localizedBody += '. ' + i18n.needPantalaimonWarning; + } + return localizedBody; + }, + EventTypes.CallAnswer: (event, i18n, body) => + i18n.answeredTheCall(event.sender.calcDisplayname()), + EventTypes.CallHangup: (event, i18n, body) => + i18n.endedTheCall(event.sender.calcDisplayname()), + EventTypes.CallInvite: (event, i18n, body) => + i18n.startedACall(event.sender.calcDisplayname()), + EventTypes.CallCandidates: (event, i18n, body) => + i18n.sentCallInformations(event.sender.calcDisplayname()), + EventTypes.Encrypted: (event, i18n, body) => + _localizedBodyNormalMessage(event, i18n, body), + EventTypes.Message: (event, i18n, body) => + _localizedBodyNormalMessage(event, i18n, body), + EventTypes.Reaction: (event, i18n, body) => i18n.sentReaction( + event.sender.calcDisplayname(), + event.content + .tryGetMap('m.relates_to') + ?.tryGet('key') ?? + body, + ), + }; +} diff --git a/lib/src/utils/event_update.dart b/lib/src/utils/event_update.dart new file mode 100644 index 0000000..922e90e --- /dev/null +++ b/lib/src/utils/event_update.dart @@ -0,0 +1,72 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import '../../matrix.dart'; + +enum EventUpdateType { + timeline, + state, + history, + accountData, + ephemeral, + inviteState +} + +/// Represents a new event (e.g. a message in a room) or an update for an +/// already known event. +class EventUpdate { + /// Usually 'timeline', 'state' or whatever. + final EventUpdateType type; + + /// Most events belong to a room. If not, this equals to eventType. + final String roomID; + + @Deprecated("Use `content['type']` instead.") + String get eventType => content['type']; + + // The json payload of the content of this event. + final Map content; + + EventUpdate({ + required this.roomID, + required this.type, + required this.content, + }); + + Future decrypt(Room room, {bool store = false}) async { + final encryption = room.client.encryption; + if (content['type'] != EventTypes.Encrypted || + !room.client.encryptionEnabled || + encryption == null) { + return this; + } + try { + final decrpytedEvent = await encryption.decryptRoomEvent( + room.id, Event.fromJson(content, room), + store: store, updateType: type); + return EventUpdate( + roomID: roomID, + type: type, + content: decrpytedEvent.toJson(), + ); + } catch (e, s) { + Logs().e('[LibOlm] Could not decrypt megolm event', e, s); + return this; + } + } +} diff --git a/lib/src/utils/html_to_text.dart b/lib/src/utils/html_to_text.dart new file mode 100644 index 0000000..aa4d0bf --- /dev/null +++ b/lib/src/utils/html_to_text.dart @@ -0,0 +1,259 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:collection/collection.dart'; + +import 'package:html/parser.dart'; +import 'package:html/dom.dart'; +import 'package:html_unescape/html_unescape.dart'; + +class HtmlToText { + /// Convert an HTML string to a pseudo-markdown plain text representation, with + /// `data-mx-spoiler` spans redacted + static String convert(String html) { + // riot-web is notorious for creating bad reply fallback events from invalid messages which, if + // not handled properly, can lead to impersonation. As such, we strip the entire `` tags + // here already, to prevent that from happening. + // We do *not* do this in an AST and just with simple regex here, as riot-web tends to create + // miss-matching tags, and this way we actually correctly identify what we want to strip and, well, + // strip it. + final renderHtml = html.replaceAll( + RegExp('.*<\/mx-reply>', + caseSensitive: false, multiLine: false, dotAll: true), + ''); + + final opts = _ConvertOpts(); + var reply = _walkNode(opts, parseFragment(renderHtml)); + reply = reply.replaceAll(RegExp(r'\s*$', multiLine: false), ''); + return reply; + } + + static String _parsePreContent(_ConvertOpts opts, Element node) { + var text = node.innerHtml; + final match = + RegExp(r'^]*)>', multiLine: false, caseSensitive: false) + .firstMatch(text); + if (match == null) { + text = HtmlUnescape().convert(text); + if (text.isNotEmpty) { + if (text[0] != '\n') { + text = '\n$text'; + } + if (text[text.length - 1] != '\n') { + text += '\n'; + } + } + return text; + } + // remove opening tag + text = text.substring(match.end); + // remove the closing tag + text = text.replaceAll( + RegExp(r'$', multiLine: false, caseSensitive: false), ''); + text = HtmlUnescape().convert(text); + if (text.isNotEmpty) { + if (text[0] != '\n') { + text = '\n$text'; + } + if (text[text.length - 1] != '\n') { + text += '\n'; + } + } + final language = + RegExp(r'language-(\w+)', multiLine: false, caseSensitive: false) + .firstMatch(match.group(1)!); + if (language != null) { + text = language.group(1)! + text; + } + return text; + } + + static String _parseBlockquoteContent(_ConvertOpts opts, Element node) { + final msg = _walkChildNodes(opts, node); + return msg.split('\n').map((s) => '> $s').join('\n') + '\n'; + } + + static String _parseSpanContent(_ConvertOpts opts, Element node) { + final content = _walkChildNodes(opts, node); + if (node.attributes['data-mx-spoiler'] is String) { + var spoiler = '█' * content.length; + final reason = node.attributes['data-mx-spoiler']; + if (reason != '') { + spoiler = '($reason) $spoiler'; + } + return spoiler; + } + return content; + } + + static String _parseUlContent(_ConvertOpts opts, Element node) { + opts.listDepth++; + final entries = _listChildNodes(opts, node, {'li'}); + opts.listDepth--; + final bulletPoint = + _listBulletPoints[opts.listDepth % _listBulletPoints.length]; + + return entries + .map((s) => + (' ' * opts.listDepth) + + bulletPoint + + ' ' + + s.replaceAll('\n', '\n' + (' ' * opts.listDepth) + ' ')) + .join('\n'); + } + + static String _parseOlContent(_ConvertOpts opts, Element node) { + opts.listDepth++; + final entries = _listChildNodes(opts, node, {'li'}); + opts.listDepth--; + final startStr = node.attributes['start']; + final start = (startStr is String && + RegExp(r'^[0-9]+$', multiLine: false).hasMatch(startStr)) + ? int.parse(startStr) + : 1; + + return entries + .mapIndexed((index, s) => + (' ' * opts.listDepth) + + '${start + index}. ' + + s.replaceAll('\n', '\n' + (' ' * opts.listDepth) + ' ')) + .join('\n'); + } + + static const _listBulletPoints = ['●', '○', '■', '‣']; + + static List _listChildNodes(_ConvertOpts opts, Element node, + [Iterable? types]) { + final replies = []; + for (final child in node.nodes) { + if (types != null && + types.isNotEmpty && + ((child is Text) || + ((child is Element) && + !types.contains(child.localName!.toLowerCase())))) { + continue; + } + replies.add(_walkNode(opts, child)); + } + return replies; + } + + static const _blockTags = { + 'blockquote', + 'ul', + 'ol', + 'h1', + 'h2', + 'h3', + 'h4', + 'h5', + 'h6', + 'pre', + }; + + static String _walkChildNodes(_ConvertOpts opts, Node node) { + var reply = ''; + var lastTag = ''; + for (final child in node.nodes) { + final thisTag = child is Element ? child.localName!.toLowerCase() : ''; + if (thisTag == 'p' && lastTag == 'p') { + reply += '\n\n'; + } else if (_blockTags.contains(thisTag) && + reply.isNotEmpty && + reply[reply.length - 1] != '\n') { + reply += '\n'; + } + reply += _walkNode(opts, child); + if (thisTag.isNotEmpty) { + lastTag = thisTag; + } + } + return reply; + } + + static String _walkNode(_ConvertOpts opts, Node node) { + if (node is Text) { + // ignore \n between single nodes + return node.text == '\n' ? '' : node.text; + } else if (node is Element) { + final tag = node.localName!.toLowerCase(); + switch (tag) { + case 'em': + case 'i': + return '*${_walkChildNodes(opts, node)}*'; + case 'strong': + case 'b': + return '**${_walkChildNodes(opts, node)}**'; + case 'u': + case 'ins': + return '__${_walkChildNodes(opts, node)}__'; + case 'del': + case 'strike': + case 's': + return '~~${_walkChildNodes(opts, node)}~~'; + case 'code': + return '`${node.text}`'; + case 'pre': + return '```${_parsePreContent(opts, node)}```\n'; + case 'a': + final href = node.attributes['href'] ?? ''; + final content = _walkChildNodes(opts, node); + if (href.toLowerCase().startsWith('https://matrix.to/#/') || + href.toLowerCase().startsWith('matrix:')) { + return content; + } + return '🔗$content'; + case 'img': + return node.attributes['alt'] ?? + node.attributes['title'] ?? + node.attributes['src'] ?? + ''; + case 'br': + return '\n'; + case 'blockquote': + return _parseBlockquoteContent(opts, node); + case 'ul': + return _parseUlContent(opts, node); + case 'ol': + return _parseOlContent(opts, node); + case 'mx-reply': + return ''; + case 'hr': + return '\n----------\n'; + case 'h1': + case 'h2': + case 'h3': + case 'h4': + case 'h5': + case 'h6': + final mark = '#' * int.parse(tag[1]); + return '$mark ${_walkChildNodes(opts, node)}\n'; + case 'span': + return _parseSpanContent(opts, node); + default: + return _walkChildNodes(opts, node); + } + } else { + return _walkChildNodes(opts, node); + } + } +} + +class _ConvertOpts { + int listDepth = 0; +} diff --git a/lib/src/utils/http_timeout.dart b/lib/src/utils/http_timeout.dart new file mode 100644 index 0000000..04ca5f5 --- /dev/null +++ b/lib/src/utils/http_timeout.dart @@ -0,0 +1,115 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:async'; + +import 'package:http/http.dart' as http; + +import '../../matrix.dart'; + +/// Stream.timeout fails if no progress is made in timeLimit. +/// In contrast, streamTotalTimeout fails if the stream isn't completed +/// until timeoutFuture. +Stream streamTotalTimeout( + Stream stream, Future timeoutFuture) async* { + final si = StreamIterator(stream); + while (await Future.any([si.moveNext(), timeoutFuture])) { + yield si.current; + } +} + +http.StreamedResponse replaceStream( + http.StreamedResponse base, Stream> stream) => + http.StreamedResponse( + http.ByteStream(stream), + base.statusCode, + contentLength: base.contentLength, + request: base.request, + headers: base.headers, + isRedirect: base.isRedirect, + persistentConnection: base.persistentConnection, + reasonPhrase: base.reasonPhrase, + ); + +/// Http Client that enforces a timeout on requests. +/// Timeout calculation is done in a subclass. +abstract class TimeoutHttpClient extends http.BaseClient { + TimeoutHttpClient(this.inner); + + http.Client inner; + + Duration get timeout; + + @override + Future send(http.BaseRequest request) async { + final timeoutFuture = Completer().future.timeout(timeout); + final response = await Future.any([inner.send(request), timeoutFuture]); + return replaceStream( + response, streamTotalTimeout(response.stream, timeoutFuture)); + } +} + +class FixedTimeoutHttpClient extends TimeoutHttpClient { + FixedTimeoutHttpClient(http.Client inner, this.timeout) : super(inner); + @override + Duration timeout; + + @override + Future send(http.BaseRequest request) => + super.send(request); +} + +class VariableTimeoutHttpClient extends TimeoutHttpClient { + /// Matrix synchronisation is done with https long polling. This needs a + /// timeout which is usually 30 seconds. + int syncTimeoutSec; + + int _timeoutFactor = 1; + + @override + Duration get timeout => + Duration(seconds: _timeoutFactor * syncTimeoutSec + 5); + + VariableTimeoutHttpClient(http.Client inner, [this.syncTimeoutSec = 30]) + : super(inner); + + @override + Future send(http.BaseRequest request) async { + try { + final response = await super.send(request); + return replaceStream(response, (() async* { + try { + await for (final chunk in response.stream) { + yield chunk; + } + _timeoutFactor = 1; + } on TimeoutException catch (e, s) { + _timeoutFactor *= 2; + throw MatrixConnectionException(e, s); + } catch (e, s) { + throw MatrixConnectionException(e, s); + } + })()); + } on TimeoutException catch (e, s) { + _timeoutFactor *= 2; + throw MatrixConnectionException(e, s); + } catch (e, s) { + throw MatrixConnectionException(e, s); + } + } +} diff --git a/lib/src/utils/image_pack_extension.dart b/lib/src/utils/image_pack_extension.dart new file mode 100644 index 0000000..92b3b92 --- /dev/null +++ b/lib/src/utils/image_pack_extension.dart @@ -0,0 +1,95 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:slugify/slugify.dart'; +import 'package:matrix_api_lite/matrix_api_lite.dart'; + +import '../room.dart'; + +extension ImagePackRoomExtension on Room { + /// Get all the active image packs for the specified [usage], mapped by their slug + Map getImagePacks([ImagePackUsage? usage]) { + final allMxcs = {}; // used for easy deduplication + final packs = {}; + final addImagePack = (BasicEvent? event, {Room? room, String? slug}) { + if (event == null) return; + final imagePack = event.parsedImagePackContent; + final finalSlug = slugify(slug ?? 'pack'); + for (final entry in imagePack.images.entries) { + final image = entry.value; + if (allMxcs.contains(image.url)) { + continue; + } + final imageUsage = image.usage ?? imagePack.pack.usage; + if (usage != null && + imageUsage != null && + !imageUsage.contains(usage)) { + continue; + } + packs + .putIfAbsent( + finalSlug, + () => ImagePackContent.fromJson({}) + ..pack.displayName = imagePack.pack.displayName ?? + room?.displayname ?? + finalSlug + ..pack.avatarUrl = imagePack.pack.avatarUrl ?? room?.avatar + ..pack.attribution = imagePack.pack.attribution) + .images[entry.key] = image; + allMxcs.add(image.url); + } + }; + // first we add the user image pack + addImagePack(client.accountData['im.ponies.user_emotes'], slug: 'user'); + // next we add all the external image packs + final packRooms = client.accountData['im.ponies.emote_rooms']; + if (packRooms != null && packRooms.content['rooms'] is Map) { + for (final roomEntry in packRooms.content['rooms'].entries) { + final roomId = roomEntry.key; + final room = client.getRoomById(roomId); + if (room != null && roomEntry.value is Map) { + for (final stateKeyEntry in roomEntry.value.entries) { + final stateKey = stateKeyEntry.key; + final fallbackSlug = + '${room.displayname}-${stateKey.isNotEmpty ? '$stateKey-' : ''}${room.id}'; + addImagePack(room.getState('im.ponies.room_emotes', stateKey), + room: room, slug: fallbackSlug); + } + } + } + } + // finally we add all of this rooms state + final allRoomEmotes = states['im.ponies.room_emotes']; + if (allRoomEmotes != null) { + for (final entry in allRoomEmotes.entries) { + addImagePack(entry.value, + room: this, + slug: (entry.value.stateKey?.isNotEmpty == true) + ? entry.value.stateKey + : 'room'); + } + } + return packs; + } + + /// Get a flat view of all the image packs of a specified [usage], that is a map of all + /// slugs to a map of the image code to their mxc url + Map> getImagePacksFlat([ImagePackUsage? usage]) => + getImagePacks(usage).map((k, v) => + MapEntry(k, v.images.map((k, v) => MapEntry(k, v.url.toString())))); +} diff --git a/lib/src/utils/map_copy_extension.dart b/lib/src/utils/map_copy_extension.dart new file mode 100644 index 0000000..520294f --- /dev/null +++ b/lib/src/utils/map_copy_extension.dart @@ -0,0 +1,33 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +extension MapCopyExtension on Map { + /// Deep-copies a given json map + Map copy() { + final copy = Map.from(this); + for (final entry in copy.entries) { + if (entry.value is Map) { + copy[entry.key] = (entry.value as Map).copy(); + } + if (entry.value is List) { + copy[entry.key] = List.from(entry.value); + } + } + return copy; + } +} diff --git a/lib/src/utils/markdown.dart b/lib/src/utils/markdown.dart new file mode 100644 index 0000000..0924e9d --- /dev/null +++ b/lib/src/utils/markdown.dart @@ -0,0 +1,256 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:markdown/markdown.dart'; +import 'dart:convert'; + +const htmlAttrEscape = HtmlEscape(HtmlEscapeMode.attribute); + +class LinebreakSyntax extends InlineSyntax { + LinebreakSyntax() : super(r'\n'); + + @override + bool onMatch(InlineParser parser, Match match) { + parser.addNode(Element.empty('br')); + return true; + } +} + +class SpoilerSyntax extends TagSyntax { + SpoilerSyntax() : super(r'\|\|', requiresDelimiterRun: true); + + @override + Node close(InlineParser parser, Delimiter opener, Delimiter closer, + {required List Function() getChildren}) { + final children = getChildren(); + final newChildren = []; + var searchingForReason = true; + var reason = ''; + for (final child in children) { + // If we already found a reason, let's just use our child nodes as-is + if (!searchingForReason) { + newChildren.add(child); + continue; + } + if (child is Text) { + final ix = child.text.indexOf('|'); + if (ix > 0) { + reason += child.text.substring(0, ix); + newChildren.add(Text(child.text.substring(ix + 1))); + searchingForReason = false; + } else { + reason += child.text; + } + } else { + // if we don't have a text node as reason we just want to cancel this whole thing + break; + } + } + // if we were still searching for a reason that means there was none - use the original children! + final element = + Element('span', searchingForReason ? children : newChildren); + element.attributes['data-mx-spoiler'] = + searchingForReason ? '' : htmlAttrEscape.convert(reason); + return element; + } +} + +class EmoteSyntax extends InlineSyntax { + final Map> Function()? getEmotePacks; + Map>? emotePacks; + EmoteSyntax(this.getEmotePacks) : super(r':(?:([-\w]+)~)?([-\w]+):'); + + @override + bool onMatch(InlineParser parser, Match match) { + final emotePacks = this.emotePacks ??= getEmotePacks?.call() ?? {}; + final pack = match[1] ?? ''; + final emote = match[2]; + String? mxc; + if (pack.isEmpty) { + // search all packs + for (final emotePack in emotePacks.values) { + mxc = emotePack[emote]; + if (mxc != null) { + break; + } + } + } else { + mxc = emotePacks[pack]?[emote]; + } + if (mxc == null) { + // emote not found. Insert the whole thing as plain text + parser.addNode(Text(match[0]!)); + return true; + } + final element = Element.empty('img'); + element.attributes['data-mx-emoticon'] = ''; + element.attributes['src'] = htmlAttrEscape.convert(mxc); + element.attributes['alt'] = htmlAttrEscape.convert(':$emote:'); + element.attributes['title'] = htmlAttrEscape.convert(':$emote:'); + element.attributes['height'] = '32'; + element.attributes['vertical-align'] = 'middle'; + parser.addNode(element); + return true; + } +} + +class InlineLatexSyntax extends TagSyntax { + InlineLatexSyntax() : super(r'\$([^\s$]([^\$]*[^\s$])?)\$'); + + @override + bool onMatch(InlineParser parser, Match match) { + final element = + Element('span', [Element.text('code', htmlEscape.convert(match[1]!))]); + element.attributes['data-mx-maths'] = htmlAttrEscape.convert(match[1]!); + parser.addNode(element); + return true; + } +} + +// We also want to allow single-lines of like "$$latex$$" +class BlockLatexSyntax extends BlockSyntax { + @override + RegExp get pattern => RegExp(r'^[ ]{0,3}\$\$(.*)$'); + + final endPattern = RegExp(r'^(.*)\$\$\s*$'); + + @override + List parseChildLines(BlockParser parser) { + final childLines = []; + var first = true; + while (!parser.isDone) { + final match = endPattern.firstMatch(parser.current); + if (match == null || (first && match[1]!.trim().isEmpty)) { + childLines.add(parser.current); + parser.advance(); + } else { + childLines.add(match[1]!); + parser.advance(); + break; + } + first = false; + } + return childLines; + } + + @override + Node parse(BlockParser parser) { + final childLines = parseChildLines(parser); + // we use .substring(2) as childLines will *always* contain the first two '$$' + final latex = childLines.join('\n').trim().substring(2).trim(); + final element = Element('div', [ + Element('pre', [Element.text('code', htmlEscape.convert(latex))]) + ]); + element.attributes['data-mx-maths'] = htmlAttrEscape.convert(latex); + return element; + } +} + +class PillSyntax extends InlineSyntax { + PillSyntax() + : super( + r'([@#!][^\s:]*:(?:[^\s]+\.\w+|[\d\.]+|\[[a-fA-F0-9:]+\])(?::\d+)?)'); + + @override + bool onMatch(InlineParser parser, Match match) { + if (match.start > 0 && + !RegExp(r'[\s.!?:;\(]').hasMatch(match.input[match.start - 1])) { + parser.addNode(Text(match[0]!)); + return true; + } + final identifier = match[1]!; + final element = Element.text('a', htmlEscape.convert(identifier)); + element.attributes['href'] = + htmlAttrEscape.convert('https://matrix.to/#/$identifier'); + parser.addNode(element); + return true; + } +} + +class MentionSyntax extends InlineSyntax { + final String? Function(String)? getMention; + MentionSyntax(this.getMention) : super(r'(@(?:\[[^\]:]+\]|\w+)(?:#\w+)?)'); + + @override + bool onMatch(InlineParser parser, Match match) { + final mention = getMention?.call(match[1]!); + if ((match.start > 0 && + !RegExp(r'[\s.!?:;\(]').hasMatch(match.input[match.start - 1])) || + mention == null) { + parser.addNode(Text(match[0]!)); + return true; + } + final element = Element.text('a', htmlEscape.convert(match[1]!)); + element.attributes['href'] = + htmlAttrEscape.convert('https://matrix.to/#/$mention'); + parser.addNode(element); + return true; + } +} + +String markdown( + String text, { + Map> Function()? getEmotePacks, + String? Function(String)? getMention, +}) { + var ret = markdownToHtml( + text, + extensionSet: ExtensionSet.commonMark, + blockSyntaxes: [ + BlockLatexSyntax(), + ], + inlineSyntaxes: [ + StrikethroughSyntax(), + LinebreakSyntax(), + SpoilerSyntax(), + EmoteSyntax(getEmotePacks), + PillSyntax(), + MentionSyntax(getMention), + InlineLatexSyntax(), + ], + ); + + var stripPTags = '

'.allMatches(ret).length <= 1; + if (stripPTags) { + const otherBlockTags = { + 'table', + 'pre', + 'ol', + 'ul', + 'h1', + 'h2', + 'h3', + 'h4', + 'h5', + 'h6', + 'blockquote', + 'div', + }; + for (final tag in otherBlockTags) { + // we check for the close tag as the opening one might have attributes + if (ret.contains('')) { + stripPTags = false; + break; + } + } + } + if (stripPTags) { + ret = ret.replaceAll('

', '').replaceAll('

', ''); + } + return ret.trim().replaceAll(RegExp(r'(
)+$'), ''); +} diff --git a/lib/src/utils/marked_unread.dart b/lib/src/utils/marked_unread.dart new file mode 100644 index 0000000..0ab9809 --- /dev/null +++ b/lib/src/utils/marked_unread.dart @@ -0,0 +1,34 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:matrix_api_lite/src/utils/try_get_map_extension.dart'; + +mixin EventType { + static const String markedUnread = 'com.famedly.marked_unread'; +} + +class MarkedUnread { + final bool unread; + + const MarkedUnread(this.unread); + + MarkedUnread.fromJson(Map json) + : unread = json.tryGet('unread') ?? false; + + Map toJson() => {'unread': unread}; +} diff --git a/lib/src/utils/matrix_file.dart b/lib/src/utils/matrix_file.dart new file mode 100644 index 0000000..7f36091 --- /dev/null +++ b/lib/src/utils/matrix_file.dart @@ -0,0 +1,290 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +/// Workaround until [File] in dart:io and dart:html is unified + +import 'dart:async'; +import 'dart:typed_data'; + +import 'package:blurhash_dart/blurhash_dart.dart'; +import 'package:image/image.dart'; +import 'package:mime/mime.dart'; + +import '../../matrix.dart'; + +class MatrixFile { + final Uint8List bytes; + final String name; + final String mimeType; + + /// Encrypts this file and returns the + /// encryption information as an [EncryptedFile]. + Future encrypt() async { + return await encryptFile(bytes); + } + + MatrixFile({required this.bytes, required String name, String? mimeType}) + : mimeType = mimeType ?? + lookupMimeType(name, headerBytes: bytes) ?? + 'application/octet-stream', + name = name.split('/').last.toLowerCase(); + + int get size => bytes.length; + + String get msgType { + if (mimeType.toLowerCase().startsWith('image/')) { + return MessageTypes.Image; + } + if (mimeType.toLowerCase().startsWith('video/')) { + return MessageTypes.Video; + } + if (mimeType.toLowerCase().startsWith('audio/')) { + return MessageTypes.Audio; + } + return MessageTypes.File; + } + + Map get info => ({ + 'mimetype': mimeType, + 'size': size, + }); +} + +class MatrixImageFile extends MatrixFile { + MatrixImageFile({ + required Uint8List bytes, + required String name, + String? mimeType, + this.width, + this.height, + this.blurhash, + }) : super(bytes: bytes, name: name, mimeType: mimeType); + + /// Creates a new image file and calculates the width, height and blurhash. + static Future create( + {required Uint8List bytes, + required String name, + String? mimeType, + Future Function(FutureOr Function(U arg) function, U arg)? + compute}) async { + final metaData = compute != null + ? await compute(_calcMetadata, bytes) + : _calcMetadata(bytes); + + return MatrixImageFile( + bytes: metaData?.bytes ?? bytes, + name: name, + mimeType: mimeType, + width: metaData?.width, + height: metaData?.height, + blurhash: metaData?.blurhash, + ); + } + + /// builds a [MatrixImageFile] and shrinks it in order to reduce traffic + /// + /// in case shrinking does not work (e.g. for unsupported MIME types), the + /// initial image is simply preserved + static Future shrink( + {required Uint8List bytes, + required String name, + int maxDimension = 1600, + String? mimeType, + Future Function(FutureOr Function(U arg) function, U arg)? + compute}) async { + final arguments = _ResizeArguments( + bytes: bytes, + maxDimension: maxDimension, + fileName: name, + calcBlurhash: true, + ); + final resizedData = compute != null + ? await compute(_resize, arguments) + : _resize(arguments); + + if (resizedData == null) { + return MatrixImageFile(bytes: bytes, name: name, mimeType: mimeType); + } + + final thumbnailFile = MatrixImageFile( + bytes: resizedData.bytes, + name: name, + mimeType: mimeType, + width: resizedData.width, + height: resizedData.height, + blurhash: resizedData.blurhash, + ); + return thumbnailFile; + } + + /// returns the width of the image + final int? width; + + /// returns the height of the image + final int? height; + + /// generates the blur hash for the image + final String? blurhash; + + @override + String get msgType => 'm.image'; + @override + Map get info => ({ + ...super.info, + if (width != null) 'w': width, + if (height != null) 'h': height, + if (blurhash != null) 'xyz.amorgan.blurhash': blurhash, + }); + + /// computes a thumbnail for the image + Future generateThumbnail( + {int dimension = Client.defaultThumbnailSize, + Future Function(FutureOr Function(U arg) function, U arg)? + compute}) async { + final thumbnailFile = await shrink( + bytes: bytes, + name: name, + mimeType: mimeType, + compute: compute, + maxDimension: dimension, + ); + // the thumbnail should rather return null than the unshrinked image + if ((thumbnailFile.width ?? 0) > dimension || + (thumbnailFile.height ?? 0) > dimension) { + return null; + } + return thumbnailFile; + } + + static _ResizedResponse? _calcMetadata(Uint8List bytes) { + final image = decodeImage(bytes); + if (image == null) return null; + + return _ResizedResponse( + bytes: bytes, + width: image.width, + height: image.height, + blurhash: BlurHash.encode( + image, + numCompX: 4, + numCompY: 3, + ).hash, + ); + } + + static _ResizedResponse? _resize(_ResizeArguments arguments) { + final image = decodeImage(arguments.bytes); + + final resized = copyResize(image!, + height: image.height > image.width ? arguments.maxDimension : null, + width: image.width >= image.height ? arguments.maxDimension : null); + + final encoded = encodeNamedImage(resized, arguments.fileName); + if (encoded == null) return null; + final bytes = Uint8List.fromList(encoded); + return _ResizedResponse( + bytes: bytes, + width: resized.width, + height: resized.height, + blurhash: arguments.calcBlurhash + ? BlurHash.encode( + resized, + numCompX: 4, + numCompY: 3, + ).hash + : null, + ); + } +} + +class _ResizedResponse { + final Uint8List bytes; + final int width; + final int height; + final String? blurhash; + + const _ResizedResponse({ + required this.bytes, + required this.width, + required this.height, + this.blurhash, + }); +} + +class _ResizeArguments { + final Uint8List bytes; + final int maxDimension; + final String fileName; + final bool calcBlurhash; + + const _ResizeArguments({ + required this.bytes, + required this.maxDimension, + required this.fileName, + required this.calcBlurhash, + }); +} + +class MatrixVideoFile extends MatrixFile { + final int? width; + final int? height; + final int? duration; + + MatrixVideoFile( + {required Uint8List bytes, + required String name, + String? mimeType, + this.width, + this.height, + this.duration}) + : super(bytes: bytes, name: name, mimeType: mimeType); + @override + String get msgType => 'm.video'; + @override + Map get info => ({ + ...super.info, + if (width != null) 'w': width, + if (height != null) 'h': height, + if (duration != null) 'duration': duration, + }); +} + +class MatrixAudioFile extends MatrixFile { + final int? duration; + + MatrixAudioFile( + {required Uint8List bytes, + required String name, + String? mimeType, + this.duration}) + : super(bytes: bytes, name: name, mimeType: mimeType); + @override + String get msgType => 'm.audio'; + @override + Map get info => ({ + ...super.info, + if (duration != null) 'duration': duration, + }); +} + +extension ToMatrixFile on EncryptedFile { + MatrixFile toMatrixFile() { + return MatrixFile( + bytes: data, name: 'crypt', mimeType: 'application/octet-stream'); + } +} diff --git a/lib/src/utils/matrix_id_string_extension.dart b/lib/src/utils/matrix_id_string_extension.dart new file mode 100644 index 0000000..5ac0582 --- /dev/null +++ b/lib/src/utils/matrix_id_string_extension.dart @@ -0,0 +1,136 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +const Set validSigils = {'@', '!', '#', '\$', '+'}; + +const int maxLength = 255; + +extension MatrixIdExtension on String { + List _getParts() { + final s = substring(1); + final ix = s.indexOf(':'); + if (ix == -1) { + return [substring(1)]; + } + return [s.substring(0, ix), s.substring(ix + 1)]; + } + + bool get isValidMatrixId { + if (isEmpty) return false; + if (length > maxLength) return false; + if (!validSigils.contains(substring(0, 1))) { + return false; + } + // event IDs do not have to have a domain + if (substring(0, 1) == '\$') { + return true; + } + // all other matrix IDs have to have a domain + final parts = _getParts(); + // the localpart can be an empty string, e.g. for aliases + if (parts.length != 2 || parts[1].isEmpty) { + return false; + } + return true; + } + + String? get sigil => isValidMatrixId ? substring(0, 1) : null; + + String? get localpart => isValidMatrixId ? _getParts().first : null; + + String? get domain => isValidMatrixId ? _getParts().last : null; + + bool equals(String? other) => toLowerCase() == other?.toLowerCase(); + + /// Parse a matrix identifier string into a Uri. Primary and secondary identifiers + /// are stored in pathSegments. The query string is stored as such. + Uri? _parseIdentifierIntoUri() { + const matrixUriPrefix = 'matrix:'; + const matrixToPrefix = 'https://matrix.to/#/'; + if (toLowerCase().startsWith(matrixUriPrefix)) { + final uri = Uri.tryParse(this); + if (uri == null) return null; + final pathSegments = uri.pathSegments; + final identifiers = []; + for (var i = 0; i < pathSegments.length - 1; i += 2) { + final thisSigil = { + 'u': '@', + 'roomid': '!', + 'r': '#', + 'e': '\$', + }[pathSegments[i].toLowerCase()]; + if (thisSigil == null) { + break; + } + identifiers.add(thisSigil + pathSegments[i + 1]); + } + return uri.replace(pathSegments: identifiers); + } else if (toLowerCase().startsWith(matrixToPrefix)) { + return Uri.tryParse('//' + + substring(matrixToPrefix.length - 1) + .replaceAllMapped( + RegExp(r'(?<=/)[#!@+][^:]*:|(\?.*$)'), + (m) => m[0]!.replaceAllMapped( + RegExp(m.group(1) != null ? '' : '[/?]'), + (m) => Uri.encodeComponent(m.group(0)!))) + .replaceAll('#', '%23')); + } else { + return Uri( + pathSegments: RegExp(r'/((?:[#!@+][^:]*:)?[^/?]*)(?:\?.*$)?') + .allMatches('/$this') + .map((m) => m[1]!), + query: RegExp(r'(?:/(?:[#!@+][^:]*:)?[^/?]*)*\?(.*$)') + .firstMatch('/$this')?[1]); + } + } + + /// Separate a matrix identifier string into a primary indentifier, a secondary identifier, + /// a query string and already parsed `via` parameters. A matrix identifier string + /// can be an mxid, a matrix.to-url or a matrix-uri. + MatrixIdentifierStringExtensionResults? parseIdentifierIntoParts() { + final uri = _parseIdentifierIntoUri(); + if (uri == null) return null; + final primary = uri.pathSegments.isNotEmpty ? uri.pathSegments[0] : null; + if (primary == null || !primary.isValidMatrixId) return null; + final secondary = uri.pathSegments.length > 1 ? uri.pathSegments[1] : null; + if (secondary != null && !secondary.isValidMatrixId) return null; + + return MatrixIdentifierStringExtensionResults( + primaryIdentifier: primary, + secondaryIdentifier: secondary, + queryString: uri.query.isNotEmpty ? uri.query : null, + via: (uri.queryParametersAll['via'] ?? []).toSet(), + action: uri.queryParameters['action'], + ); + } +} + +class MatrixIdentifierStringExtensionResults { + final String primaryIdentifier; + final String? secondaryIdentifier; + final String? queryString; + final Set via; + final String? action; + + MatrixIdentifierStringExtensionResults( + {required this.primaryIdentifier, + this.secondaryIdentifier, + this.queryString, + this.via = const {}, + this.action}); +} diff --git a/lib/src/utils/matrix_localizations.dart b/lib/src/utils/matrix_localizations.dart new file mode 100644 index 0000000..3db5e3e --- /dev/null +++ b/lib/src/utils/matrix_localizations.dart @@ -0,0 +1,179 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import '../room.dart'; + +abstract class MatrixLocalizations { + const MatrixLocalizations(); + String get emptyChat; + + String get invitedUsersOnly; + + String get fromTheInvitation; + + String get fromJoining; + + String get visibleForAllParticipants; + + String get visibleForEveryone; + + String get guestsCanJoin; + + String get guestsAreForbidden; + + String get anyoneCanJoin; + + String get needPantalaimonWarning; + + String get channelCorruptedDecryptError; + + String get encryptionNotEnabled; + + String get unknownEncryptionAlgorithm; + + String get noPermission; + + String get you; + + String get roomHasBeenUpgraded; + + String groupWith(String displayname); + + String removedBy(String calcDisplayname); + + String sentASticker(String senderName); + + String redactedAnEvent(String senderName); + + String changedTheRoomAliases(String senderName); + + String changedTheRoomInvitationLink(String senderName); + + String createdTheChat(String senderName); + + String changedTheJoinRules(String senderName); + + String changedTheJoinRulesTo(String senderName, String localizedString); + + String acceptedTheInvitation(String targetName); + + String rejectedTheInvitation(String targetName); + + String hasWithdrawnTheInvitationFor(String senderName, String targetName); + + String joinedTheChat(String targetName); + + String kickedAndBanned(String senderName, String targetName); + + String kicked(String senderName, String targetName); + + String userLeftTheChat(String targetName); + + String bannedUser(String senderName, String targetName); + + String unbannedUser(String senderName, String targetName); + + String invitedUser(String senderName, String targetName); + + String changedTheProfileAvatar(String targetName); + + String changedTheDisplaynameTo(String targetName, String newDisplayname); + + String changedTheChatPermissions(String senderName); + + String changedTheChatNameTo(String senderName, String content); + + String changedTheChatDescriptionTo(String senderName, String content); + + String changedTheChatAvatar(String senderName); + + String changedTheGuestAccessRules(String senderName); + + String changedTheGuestAccessRulesTo( + String senderName, String localizedString); + + String changedTheHistoryVisibility(String senderName); + + String changedTheHistoryVisibilityTo( + String senderName, String localizedString); + + String activatedEndToEndEncryption(String senderName); + + String sentAPicture(String senderName); + + String sentAFile(String senderName); + + String sentAnAudio(String senderName); + + String sentAVideo(String senderName); + + String sentReaction(String senderName, String reactionKey); + + String sharedTheLocation(String senderName); + + String couldNotDecryptMessage(String errorText); + + String unknownEvent(String typeKey); + + String startedACall(String senderName); + + String endedTheCall(String senderName); + + String answeredTheCall(String senderName); + + String sentCallInformations(String senderName); +} + +extension HistoryVisibilityDisplayString on HistoryVisibility { + String getLocalizedString(MatrixLocalizations i18n) { + switch (this) { + case HistoryVisibility.invited: + return i18n.fromTheInvitation; + case HistoryVisibility.joined: + return i18n.fromJoining; + case HistoryVisibility.shared: + return i18n.visibleForAllParticipants; + case HistoryVisibility.worldReadable: + return i18n.visibleForEveryone; + } + } +} + +extension GuestAccessDisplayString on GuestAccess { + String getLocalizedString(MatrixLocalizations i18n) { + switch (this) { + case GuestAccess.canJoin: + return i18n.guestsCanJoin; + case GuestAccess.forbidden: + return i18n.guestsAreForbidden; + } + } +} + +extension JoinRulesDisplayString on JoinRules { + String getLocalizedString(MatrixLocalizations i18n) { + switch (this) { + case JoinRules.public: + return i18n.anyoneCanJoin; + case JoinRules.invite: + return i18n.invitedUsersOnly; + default: + return toString().replaceAll('JoinRules.', ''); + } + } +} diff --git a/lib/src/utils/multilock.dart b/lib/src/utils/multilock.dart new file mode 100644 index 0000000..035232e --- /dev/null +++ b/lib/src/utils/multilock.dart @@ -0,0 +1,68 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +import 'dart:async'; + +/// Lock management class. It allows to lock and unlock multiple keys at once. The keys have +/// the type [T] +class MultiLock { + final Map> _completers = {}; + + /// Set a number of [keys] locks, awaiting them to be released previously. + Future lock(Iterable keys) async { + // An iterable might have duplicate entries. A set is guaranteed not to, and we need + // unique entries, as else a lot of things might go bad. + final uniqueKeys = keys.toSet(); + // we want to make sure that there are no existing completers for any of the locks + // we are trying to set. So, we await all the completers until they are all gone. + // We can't just assume they are all gone after one go, due to rare race conditions + // which could then result in a deadlock. + while (_completers.keys.any((k) => uniqueKeys.contains(k))) { + // Here we try to build all the futures to wait for single completers and then await + // them at the same time, in parallel + final futures = >[]; + for (final key in uniqueKeys) { + if (_completers[key] != null) { + futures.add(() async { + while (_completers[key] != null) { + await _completers[key]!.future; + } + }()); + } + } + await Future.wait(futures); + } + // And finally set all the completers + for (final key in uniqueKeys) { + _completers[key] = Completer(); + } + } + + /// Unlock all [keys] locks. Typically these should be the same keys as called + /// in `.lock(keys)`` + void unlock(Iterable keys) { + final uniqueKeys = keys.toSet(); + // we just have to simply unlock all the completers + for (final key in uniqueKeys) { + if (_completers[key] != null) { + final completer = _completers[key]!; + _completers.remove(key); + completer.complete(); + } + } + } +} diff --git a/lib/src/utils/queued_to_device_event.dart b/lib/src/utils/queued_to_device_event.dart new file mode 100644 index 0000000..3296510 --- /dev/null +++ b/lib/src/utils/queued_to_device_event.dart @@ -0,0 +1,50 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +import 'dart:convert'; + +class QueuedToDeviceEvent { + final int id; + final String type; + final String txnId; + final Map content; + + QueuedToDeviceEvent({ + required this.id, + required this.type, + required this.txnId, + required this.content, + }); + + factory QueuedToDeviceEvent.fromJson(Map json) => + QueuedToDeviceEvent( + id: json['id'], + type: json['type'], + txnId: json['txn_id'], + // Temporary fix to stay compatible to Moor AND a key value store + content: json['content'] is String + ? jsonDecode(json['content']) + : json['content'], + ); + + Map toJson() => { + 'id': id, + 'type': type, + 'txn_id': txnId, + 'content': content, + }; +} diff --git a/lib/src/utils/receipt.dart b/lib/src/utils/receipt.dart new file mode 100644 index 0000000..60bc471 --- /dev/null +++ b/lib/src/utils/receipt.dart @@ -0,0 +1,33 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import '../user.dart'; + +/// Represents a receipt. +/// This [user] has read an event at the given [time]. +class Receipt { + final User user; + final DateTime time; + + const Receipt(this.user, this.time); + + @override + bool operator ==(dynamic other) => (other is Receipt && + other.user == user && + other.time.microsecondsSinceEpoch == time.microsecondsSinceEpoch); +} diff --git a/lib/src/utils/run_benchmarked.dart b/lib/src/utils/run_benchmarked.dart new file mode 100644 index 0000000..af5a85b --- /dev/null +++ b/lib/src/utils/run_benchmarked.dart @@ -0,0 +1,43 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:matrix/matrix.dart'; + +/// Calculates some benchmarks for this function. Give it a [name] and a [func] +/// to call and it will calculate the needed milliseconds. Give it an optional +/// [itemCount] to let it also calculate the needed milliseconds per item. +Future runBenchmarked( + String name, + Future Function() func, [ + int? itemCount, +]) async { + if (Logs().level.index < Level.debug.index) { + return func(); + } + final start = DateTime.now(); + final result = await func(); + final milliseconds = + DateTime.now().millisecondsSinceEpoch - start.millisecondsSinceEpoch; + var message = 'Benchmark: $name -> $milliseconds ms'; + if (itemCount != null) { + message += + ' ($itemCount items, ${itemCount > 0 ? milliseconds / itemCount : milliseconds} ms/item)'; + } + Logs().d(message); + return result; +} diff --git a/lib/src/utils/run_in_root.dart b/lib/src/utils/run_in_root.dart new file mode 100644 index 0000000..6fa6e94 --- /dev/null +++ b/lib/src/utils/run_in_root.dart @@ -0,0 +1,32 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:async'; + +import '../../matrix.dart'; + +Future runInRoot(FutureOr Function() fn) async { + return await Zone.root.run(() async { + try { + return await fn(); + } catch (e, s) { + Logs().e('Error thrown in root zone', e, s); + } + return null; + }); +} diff --git a/lib/src/utils/space_child.dart b/lib/src/utils/space_child.dart new file mode 100644 index 0000000..bb40980 --- /dev/null +++ b/lib/src/utils/space_child.dart @@ -0,0 +1,47 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:matrix_api_lite/matrix_api_lite.dart'; + +import '../event.dart'; + +class SpaceChild { + final String? roomId; + final List? via; + final String order; + final bool? suggested; + + SpaceChild.fromState(Event state) + : assert(state.type == EventTypes.spaceChild), + roomId = state.stateKey, + via = state.content.tryGetList('via'), + order = state.content.tryGet('order') ?? '', + suggested = state.content.tryGet('suggested'); +} + +class SpaceParent { + final String? roomId; + final List? via; + final bool? canonical; + + SpaceParent.fromState(Event state) + : assert(state.type == EventTypes.spaceParent), + roomId = state.stateKey, + via = state.content.tryGetList('via'), + canonical = state.content.tryGet('canonical'); +} diff --git a/lib/src/utils/sync_update_extension.dart b/lib/src/utils/sync_update_extension.dart new file mode 100644 index 0000000..82dfb42 --- /dev/null +++ b/lib/src/utils/sync_update_extension.dart @@ -0,0 +1,44 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import '../../matrix.dart'; + +/// This extension adds easy-to-use filters for the sync update, meant to be used on the `client.onSync` stream, e.g. +/// `client.onSync.stream.where((s) => s.hasRoomUpdate)`. Multiple filters can easily be +/// combind with boolean logic: `client.onSync.stream.where((s) => s.hasRoomUpdate || s.hasPresenceUpdate)` +extension SyncUpdateFilters on SyncUpdate { + /// Returns true if this sync updat has a room update + /// That means there is account data, if there is a room in one of the `join`, `leave` or `invite` blocks of the sync or if there is a to_device event. + bool get hasRoomUpdate { + // if we have an account data change we need to re-render, as `m.direct` might have changed + if (accountData?.isNotEmpty ?? false) { + return true; + } + // check for a to_device event + if (toDevice?.isNotEmpty ?? false) { + return true; + } + // return if there are rooms to update + return (rooms?.join?.isNotEmpty ?? false) || + (rooms?.invite?.isNotEmpty ?? false) || + (rooms?.leave?.isNotEmpty ?? false); + } + + /// Returns if this sync update has presence updates + bool get hasPresenceUpdate => presence?.isNotEmpty ?? false; +} diff --git a/lib/src/utils/sync_update_item_count.dart b/lib/src/utils/sync_update_item_count.dart new file mode 100644 index 0000000..b980fae --- /dev/null +++ b/lib/src/utils/sync_update_item_count.dart @@ -0,0 +1,41 @@ +import 'package:matrix/matrix.dart'; + +extension SyncUpdateItemCount on SyncUpdate { + int get itemCount { + var count = 0; + count += accountData?.length ?? 0; + count += deviceLists?.changed?.length ?? 0; + count += deviceLists?.left?.length ?? 0; + count += toDevice?.length ?? 0; + count += presence?.length ?? 0; + count += _joinRoomsItemCount; + count += _inviteRoomsItemCount; + count += _leaveRoomsItemCount; + return count; + } + + int get _joinRoomsItemCount => + rooms?.join?.values.fold( + 0, + (prev, room) => + prev + + (room.accountData?.length ?? 0) + + (room.state?.length ?? 0) + + (room.timeline?.events?.length ?? 0)) ?? + 0; + + int get _inviteRoomsItemCount => + rooms?.invite?.values.fold( + 0, (prev, room) => prev + (room.inviteState?.length ?? 0)) ?? + 0; + + int get _leaveRoomsItemCount => + rooms?.leave?.values.fold( + 0, + (prev, room) => + prev + + (room.accountData?.length ?? 0) + + (room.state?.length ?? 0) + + (room.timeline?.events?.length ?? 0)) ?? + 0; +} diff --git a/lib/src/utils/to_device_event.dart b/lib/src/utils/to_device_event.dart new file mode 100644 index 0000000..137906a --- /dev/null +++ b/lib/src/utils/to_device_event.dart @@ -0,0 +1,53 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import '../../matrix.dart'; + +class ToDeviceEvent extends BasicEventWithSender { + Map? encryptedContent; + + String get sender => senderId; + set sender(String sender) => senderId = sender; + + ToDeviceEvent({ + required String sender, + required String type, + required Map content, + this.encryptedContent, + }) : super(senderId: sender, type: type, content: content); + + factory ToDeviceEvent.fromJson(Map json) { + final event = BasicEventWithSender.fromJson(json); + return ToDeviceEvent( + sender: event.senderId, type: event.type, content: event.content); + } +} + +class ToDeviceEventDecryptionError extends ToDeviceEvent { + Exception exception; + StackTrace? stackTrace; + ToDeviceEventDecryptionError({ + required ToDeviceEvent toDeviceEvent, + required this.exception, + this.stackTrace, + }) : super( + sender: toDeviceEvent.senderId, + content: toDeviceEvent.content, + type: toDeviceEvent.type, + ); +} diff --git a/lib/src/utils/uia_request.dart b/lib/src/utils/uia_request.dart new file mode 100644 index 0000000..174cc10 --- /dev/null +++ b/lib/src/utils/uia_request.dart @@ -0,0 +1,120 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import '../../matrix.dart'; + +enum UiaRequestState { + /// The request is done + done, + + /// The request has failed + fail, + + /// The request is currently loading + loading, + + /// The request is waiting for user interaction + waitForUser, +} + +/// Wrapper to handle User interactive authentication requests +class UiaRequest { + void Function(UiaRequestState state)? onUpdate; + final Future Function(AuthenticationData? auth) request; + String? session; + UiaRequestState _state = UiaRequestState.loading; + T? result; + Exception? error; + Set nextStages = {}; + Map params = {}; + + UiaRequestState get state => _state; + + set state(UiaRequestState newState) { + if (_state == newState) return; + _state = newState; + onUpdate?.call(newState); + } + + UiaRequest({this.onUpdate, required this.request}) { + _run(); + } + + Future _run([AuthenticationData? auth]) async { + state = UiaRequestState.loading; + try { + final res = await request(auth); + state = UiaRequestState.done; + result = res; + return res; + } on MatrixException catch (err) { + if (err.session == null) { + error = err; + state = UiaRequestState.fail; + return null; + } + session ??= err.session; + final completed = err.completedAuthenticationFlows; + final flows = err.authenticationFlows ?? []; + params = err.authenticationParams ?? {}; + nextStages = getNextStages(flows, completed); + if (nextStages.isEmpty) { + error = err; + state = UiaRequestState.fail; + return null; + } + return null; + } catch (err) { + error = err is Exception ? err : Exception(err); + state = UiaRequestState.fail; + return null; + } finally { + if (state == UiaRequestState.loading) { + state = UiaRequestState.waitForUser; + } + } + } + + Future completeStage(AuthenticationData auth) => _run(auth); + + /// Cancel this uia request for example if the app can not handle this stage. + void cancel([Exception? err]) { + error = err ?? Exception('Request has been canceled'); + state = UiaRequestState.fail; + } + + Set getNextStages( + List flows, List completed) { + final nextStages = {}; + for (final flow in flows) { + final stages = flow.stages; + final nextStage = stages[completed.length]; + var stagesValid = true; + for (var i = 0; i < completed.length; i++) { + if (stages[i] != completed[i]) { + stagesValid = false; + break; + } + } + if (stagesValid) { + nextStages.add(nextStage); + } + } + return nextStages; + } +} diff --git a/lib/src/utils/uri_extension.dart b/lib/src/utils/uri_extension.dart new file mode 100644 index 0000000..a668ec9 --- /dev/null +++ b/lib/src/utils/uri_extension.dart @@ -0,0 +1,63 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:core'; + +import '../client.dart'; + +extension MxcUriExtension on Uri { + /// Returns a download Link to this content. + Uri getDownloadLink(Client matrix) => isScheme('mxc') + ? matrix.homeserver != null + ? matrix.homeserver?.resolve( + '_matrix/media/r0/download/$host${hasPort ? ':$port' : ''}$path') ?? + Uri() + : Uri() + : this; + + /// Returns a scaled thumbnail link to this content with the given `width` and + /// `height`. `method` can be `ThumbnailMethod.crop` or + /// `ThumbnailMethod.scale` and defaults to `ThumbnailMethod.scale`. + /// If `animated` (default false) is set to true, an animated thumbnail is requested + /// as per MSC2705. Thumbnails only animate if the media repository supports that. + Uri getThumbnail(Client matrix, + {num? width, + num? height, + ThumbnailMethod? method = ThumbnailMethod.crop, + bool? animated = false}) { + if (!isScheme('mxc')) return this; + final homeserver = matrix.homeserver; + if (homeserver == null) { + return Uri(); + } + return Uri( + scheme: homeserver.scheme, + host: homeserver.host, + path: '/_matrix/media/r0/thumbnail/$host${hasPort ? ':$port' : ''}$path', + port: homeserver.port, + queryParameters: { + if (width != null) 'width': width.round().toString(), + if (height != null) 'height': height.round().toString(), + if (method != null) 'method': method.toString().split('.').last, + if (animated != null) 'animated': animated.toString(), + }, + ); + } +} + +enum ThumbnailMethod { crop, scale } diff --git a/lib/src/voip.dart b/lib/src/voip.dart new file mode 100644 index 0000000..b86ade5 --- /dev/null +++ b/lib/src/voip.dart @@ -0,0 +1,1508 @@ +import 'dart:async'; +import 'dart:core'; + +import 'package:webrtc_interface/webrtc_interface.dart'; +import 'package:sdp_transform/sdp_transform.dart' as sdp_transform; + +import '../matrix.dart'; + +/// Delegate WebRTC basic functionality. +abstract class WebRTCDelegate { + MediaDevices get mediaDevices; + Future createPeerConnection( + Map configuration, + [Map constraints = const {}]); + VideoRenderer createRenderer(); + void playRingtone(); + void stopRingtone(); + void handleNewCall(CallSession session); + void handleCallEnded(CallSession session); + + bool get isBackgroud; + bool get isWeb; +} + +/// The default life time for call events, in millisecond. +const lifetimeMs = 10 * 1000; + +/// The length of time a call can be ringing for. +const callTimeoutSec = 60; + +/// Wrapped MediaStream, used to adapt Widget to display +class WrappedMediaStream { + MediaStream? stream; + final String userId; + final Room room; + + /// Current stream type, usermedia or screen-sharing + String purpose; + bool audioMuted; + bool videoMuted; + final Client client; + VideoRenderer renderer; + final bool isWeb; + + /// for debug + String get title => '$displayName:$purpose:a[$audioMuted]:v[$videoMuted]'; + bool stopped = false; + void Function(bool audioMuted, bool videoMuted)? onMuteStateChanged; + void Function(MediaStream stream)? onNewStream; + + WrappedMediaStream( + {this.stream, + required this.renderer, + required this.room, + required this.userId, + required this.purpose, + required this.client, + required this.audioMuted, + required this.videoMuted, + required this.isWeb}); + + /// Initialize the video renderer + Future initialize() async { + await renderer.initialize(); + renderer.srcObject = stream; + renderer.onResize = () { + Logs().i( + 'onResize [${stream!.id.substring(0, 8)}] ${renderer.videoWidth} x ${renderer.videoHeight}'); + }; + } + + Future dispose() async { + renderer.srcObject = null; + if (isLocal() && stream != null) { + if (isWeb) { + stream!.getTracks().forEach((element) { + element.stop(); + }); + } + await stream?.dispose(); + stream = null; + } + } + + String get avatarName => + getUser().calcDisplayname(mxidLocalPartFallback: false); + + String? get displayName => getUser().displayName; + + User getUser() { + return room.getUserByMXIDSync(userId); + } + + bool isLocal() { + return userId == client.userID; + } + + bool isAudioMuted() { + return (stream != null && stream!.getAudioTracks().isEmpty) || audioMuted; + } + + bool isVideoMuted() { + return (stream != null && stream!.getVideoTracks().isEmpty) || videoMuted; + } + + void setNewStream(MediaStream newStream) { + stream = newStream; + renderer.srcObject = stream; + if (onNewStream != null) { + onNewStream?.call(stream!); + } + } + + void setAudioMuted(bool muted) { + audioMuted = muted; + if (onMuteStateChanged != null) { + onMuteStateChanged?.call(audioMuted, videoMuted); + } + } + + void setVideoMuted(bool muted) { + videoMuted = muted; + if (onMuteStateChanged != null) { + onMuteStateChanged?.call(audioMuted, videoMuted); + } + } +} + +// Call state +enum CallState { + /// The call is inilalized but not yet started + kFledgling, + + /// The first time an invite is sent, the local has createdOffer + kInviteSent, + + /// getUserMedia or getDisplayMedia has been called, + /// but MediaStream has not yet been returned + kWaitLocalMedia, + + /// The local has createdOffer + kCreateOffer, + + /// Received a remote offer message and created a local Answer + kCreateAnswer, + + /// Answer sdp is set, but ice is not connected + kConnecting, + + /// WebRTC media stream is connected + kConnected, + + /// The call was received, but no processing has been done yet. + kRinging, + + /// End of call + kEnded, +} + +class CallErrorCode { + /// The user chose to end the call + static String UserHangup = 'user_hangup'; + + /// An error code when the local client failed to create an offer. + static String LocalOfferFailed = 'local_offer_failed'; + + /// An error code when there is no local mic/camera to use. This may be because + /// the hardware isn't plugged in, or the user has explicitly denied access. + static String NoUserMedia = 'no_user_media'; + + /// Error code used when a call event failed to send + /// because unknown devices were present in the room + static String UnknownDevices = 'unknown_devices'; + + /// Error code used when we fail to send the invite + /// for some reason other than there being unknown devices + static String SendInvite = 'send_invite'; + + /// An answer could not be created + + static String CreateAnswer = 'create_answer'; + + /// Error code used when we fail to send the answer + /// for some reason other than there being unknown devices + + static String SendAnswer = 'send_answer'; + + /// The session description from the other side could not be set + static String SetRemoteDescription = 'set_remote_description'; + + /// The session description from this side could not be set + static String SetLocalDescription = 'set_local_description'; + + /// A different device answered the call + static String AnsweredElsewhere = 'answered_elsewhere'; + + /// No media connection could be established to the other party + static String IceFailed = 'ice_failed'; + + /// The invite timed out whilst waiting for an answer + static String InviteTimeout = 'invite_timeout'; + + /// The call was replaced by another call + static String Replaced = 'replaced'; + + /// Signalling for the call could not be sent (other than the initial invite) + static String SignallingFailed = 'signalling_timeout'; + + /// The remote party is busy + static String UserBusy = 'user_busy'; + + /// We transferred the call off to somewhere else + static String Transfered = 'transferred'; +} + +class CallError extends Error { + final String code; + final String msg; + final dynamic err; + CallError(this.code, this.msg, this.err); + + @override + String toString() { + return '[$code] $msg, err: ${err.toString()}'; + } +} + +enum CallEvent { + /// The call was hangup by the local|remote user. + kHangup, + + /// The call state has changed + kState, + + /// The call got some error. + kError, + + /// Call transfer + kReplaced, + + /// The value of isLocalOnHold() has changed + kLocalHoldUnhold, + + /// The value of isRemoteOnHold() has changed + kRemoteHoldUnhold, + + /// Feeds have changed + kFeedsChanged, + + /// For sip calls. support in the future. + kAssertedIdentityChanged, +} + +enum CallType { kVoice, kVideo } + +enum CallDirection { kIncoming, kOutgoing } + +enum CallParty { kLocal, kRemote } + +/// Initialization parameters of the call session. +class CallOptions { + late String callId; + late CallType type; + late CallDirection dir; + late String localPartyId; + late VoIP voip; + late Room room; + late List> iceServers; +} + +/// A call session object +class CallSession { + CallSession(this.opts); + CallOptions opts; + CallType get type => opts.type; + Room get room => opts.room; + VoIP get voip => opts.voip; + String get callId => opts.callId; + String get localPartyId => opts.localPartyId; + String? get displayName => room.displayname; + CallDirection get direction => opts.dir; + CallState state = CallState.kFledgling; + bool get isOutgoing => direction == CallDirection.kOutgoing; + bool get isRinging => state == CallState.kRinging; + RTCPeerConnection? pc; + List remoteCandidates = []; + List localCandidates = []; + late AssertedIdentity remoteAssertedIdentity; + bool get callHasEnded => state == CallState.kEnded; + bool iceGatheringFinished = false; + bool inviteOrAnswerSent = false; + bool localHold = false; + bool remoteOnHold = false; + bool _answeredByUs = false; + bool speakerOn = false; + bool makingOffer = false; + bool ignoreOffer = false; + String facingMode = 'user'; + Client get client => opts.room.client; + String? remotePartyId; + late User remoteUser; + late CallParty hangupParty; + late String hangupReason; + late CallError lastError; + + SDPStreamMetadata? remoteSDPStreamMetadata; + List usermediaSenders = []; + List screensharingSenders = []; + List streams = []; + List get getLocalStreams => + streams.where((element) => element.isLocal()).toList(); + List get getRemoteStreams => + streams.where((element) => !element.isLocal()).toList(); + + WrappedMediaStream? get localUserMediaStream { + final stream = getLocalStreams.where( + (element) => element.purpose == SDPStreamMetadataPurpose.Usermedia); + if (stream.isNotEmpty) { + return stream.first; + } + return null; + } + + WrappedMediaStream? get localScreenSharingStream { + final stream = getLocalStreams.where( + (element) => element.purpose == SDPStreamMetadataPurpose.Screenshare); + if (stream.isNotEmpty) { + return stream.first; + } + return null; + } + + WrappedMediaStream? get remoteUserMediaStream { + final stream = getRemoteStreams.where( + (element) => element.purpose == SDPStreamMetadataPurpose.Usermedia); + if (stream.isNotEmpty) { + return stream.first; + } + return null; + } + + WrappedMediaStream? get remoteScreenSharingStream { + final stream = getRemoteStreams.where( + (element) => element.purpose == SDPStreamMetadataPurpose.Screenshare); + if (stream.isNotEmpty) { + return stream.first; + } + return null; + } + + final _callStateController = + StreamController.broadcast(sync: true); + Stream get onCallStateChanged => _callStateController.stream; + final _callEventController = + StreamController.broadcast(sync: true); + Stream get onCallEventChanged => _callEventController.stream; + Timer? inviteTimer; + Timer? ringingTimer; + + Future initOutboundCall(CallType type) async { + await _preparePeerConnection(); + setCallState(CallState.kCreateOffer); + final stream = await _getUserMedia(type); + if (stream != null) { + _addLocalStream(stream, SDPStreamMetadataPurpose.Usermedia); + } + } + + Future initWithInvite(CallType type, RTCSessionDescription offer, + SDPStreamMetadata? metadata, int lifetime) async { + await _preparePeerConnection(); + + final stream = await _getUserMedia(type); + if (stream != null) { + _addLocalStream(stream, SDPStreamMetadataPurpose.Usermedia); + } + + if (metadata != null) { + _updateRemoteSDPStreamMetadata(metadata); + } + + await pc!.setRemoteDescription(offer); + + setCallState(CallState.kRinging); + + ringingTimer = Timer(Duration(milliseconds: 30000 - lifetime), () { + if (state == CallState.kRinging) { + Logs().v('[VOIP] Call invite has expired. Hanging up.'); + hangupParty = CallParty.kRemote; // effectively + fireCallEvent(CallEvent.kHangup); + hangup(CallErrorCode.InviteTimeout); + } + ringingTimer?.cancel(); + ringingTimer = null; + }); + } + + void initWithHangup() { + setCallState(CallState.kEnded); + } + + void onAnswerReceived( + RTCSessionDescription answer, SDPStreamMetadata? metadata) async { + if (metadata != null) { + _updateRemoteSDPStreamMetadata(metadata); + } + + if (direction == CallDirection.kOutgoing) { + setCallState(CallState.kConnecting); + await pc!.setRemoteDescription(answer); + remoteCandidates.forEach((candidate) => pc!.addCandidate(candidate)); + } + } + + void onNegotiateReceived( + SDPStreamMetadata? metadata, RTCSessionDescription description) async { + final polite = direction == CallDirection.kIncoming; + + // Here we follow the perfect negotiation logic from + // https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API/Perfect_negotiation + final offerCollision = ((description.type == 'offer') && + (makingOffer || + pc!.signalingState != RTCSignalingState.RTCSignalingStateStable)); + + ignoreOffer = !polite && offerCollision; + if (ignoreOffer) { + Logs().i('Ignoring colliding negotiate event because we\'re impolite'); + return; + } + + final prevLocalOnHold = await isLocalOnHold(); + + if (metadata != null) { + _updateRemoteSDPStreamMetadata(metadata); + } + + try { + await pc!.setRemoteDescription(description); + if (description.type == 'offer') { + final answer = await pc!.createAnswer({}); + await room.sendCallNegotiate( + callId, lifetimeMs, localPartyId, answer.sdp!, + type: answer.type!); + await pc!.setLocalDescription(answer); + } + } catch (e) { + _getLocalOfferFailed(e); + Logs().e('[VOIP] onNegotiateReceived => ${e.toString()}'); + return; + } + + final newLocalOnHold = await isLocalOnHold(); + if (prevLocalOnHold != newLocalOnHold) { + localHold = newLocalOnHold; + fireCallEvent(CallEvent.kLocalHoldUnhold); + } + } + + void _updateRemoteSDPStreamMetadata(SDPStreamMetadata metadata) { + remoteSDPStreamMetadata = metadata; + remoteSDPStreamMetadata!.sdpStreamMetadatas + .forEach((streamId, sdpStreamMetadata) { + Logs().i( + 'Stream purpose update: \nid = "$streamId", \npurpose = "${sdpStreamMetadata.purpose}", \naudio_muted = ${sdpStreamMetadata.audio_muted}, \nvideo_muted = ${sdpStreamMetadata.video_muted}'); + }); + getRemoteStreams.forEach((wpstream) { + final streamId = wpstream.stream!.id; + final purpose = metadata.sdpStreamMetadatas[streamId]; + if (purpose != null) { + wpstream + .setAudioMuted(metadata.sdpStreamMetadatas[streamId]!.audio_muted); + wpstream + .setVideoMuted(metadata.sdpStreamMetadatas[streamId]!.video_muted); + wpstream.purpose = metadata.sdpStreamMetadatas[streamId]!.purpose; + } else { + Logs().i('Not found purpose for remote stream $streamId, remove it?'); + wpstream.stopped = true; + fireCallEvent(CallEvent.kFeedsChanged); + } + }); + } + + void onSDPStreamMetadataReceived(SDPStreamMetadata metadata) async { + _updateRemoteSDPStreamMetadata(metadata); + fireCallEvent(CallEvent.kFeedsChanged); + } + + void onCandidatesReceived(List candidates) { + candidates.forEach((json) async { + final candidate = RTCIceCandidate( + json['candidate'], + json['sdpMid'] ?? '', + json['sdpMLineIndex']?.round() ?? 0, + ); + + if (pc != null && inviteOrAnswerSent && remotePartyId != null) { + try { + await pc!.addCandidate(candidate); + } catch (e) { + Logs().e('[VOIP] onCandidatesReceived => ${e.toString()}'); + } + } else { + remoteCandidates.add(candidate); + } + }); + + if (pc != null && + pc!.iceConnectionState == + RTCIceConnectionState.RTCIceConnectionStateDisconnected) { + restartIce(); + } + } + + void onAssertedIdentityReceived(AssertedIdentity identity) async { + remoteAssertedIdentity = identity; + fireCallEvent(CallEvent.kAssertedIdentityChanged); + } + + bool get screensharingEnabled => localScreenSharingStream != null; + + Future setScreensharingEnabled(bool enabled) async { + // Skip if there is nothing to do + if (enabled && localScreenSharingStream != null) { + Logs().w( + 'There is already a screensharing stream - there is nothing to do!'); + return true; + } else if (!enabled && localScreenSharingStream == null) { + Logs().w( + 'There already isn\'t a screensharing stream - there is nothing to do!'); + return false; + } + + Logs().d('Set screensharing enabled? $enabled'); + + if (enabled) { + try { + final stream = await _getDisplayMedia(); + if (stream == null) { + return false; + } + stream.getVideoTracks().forEach((track) { + track.onEnded = () { + setScreensharingEnabled(false); + }; + }); + _addLocalStream(stream, SDPStreamMetadataPurpose.Screenshare); + return true; + } catch (err) { + fireCallEvent(CallEvent.kError); + lastError = CallError(CallErrorCode.NoUserMedia, + 'Failed to get screen-sharing stream: ', err); + return false; + } + } else { + for (final sender in screensharingSenders) { + await pc!.removeTrack(sender); + } + for (final track in localScreenSharingStream!.stream!.getTracks()) { + await track.stop(); + } + localScreenSharingStream!.stopped = true; + await _removeStream(localScreenSharingStream!.stream!); + fireCallEvent(CallEvent.kFeedsChanged); + return false; + } + } + + void _addLocalStream(MediaStream stream, String purpose, + {bool addToPeerConnection = true}) async { + final existingStream = + getLocalStreams.where((element) => element.purpose == purpose); + if (existingStream.isNotEmpty) { + existingStream.first.setNewStream(stream); + } else { + final newStream = WrappedMediaStream( + renderer: voip.delegate.createRenderer(), + userId: client.userID!, + room: opts.room, + stream: stream, + purpose: purpose, + client: client, + audioMuted: stream.getAudioTracks().isEmpty, + videoMuted: stream.getVideoTracks().isEmpty, + isWeb: voip.delegate.isWeb, + ); + await newStream.initialize(); + streams.add(newStream); + fireCallEvent(CallEvent.kFeedsChanged); + } + + if (addToPeerConnection) { + if (purpose == SDPStreamMetadataPurpose.Screenshare) { + screensharingSenders.clear(); + stream.getTracks().forEach((track) async { + screensharingSenders.add(await pc!.addTrack(track, stream)); + }); + } else if (purpose == SDPStreamMetadataPurpose.Usermedia) { + usermediaSenders.clear(); + stream.getTracks().forEach((track) async { + usermediaSenders.add(await pc!.addTrack(track, stream)); + }); + } + fireCallEvent(CallEvent.kFeedsChanged); + } + + if (purpose == SDPStreamMetadataPurpose.Usermedia) { + speakerOn = type == CallType.kVideo; + if (!voip.delegate.isWeb && !voip.delegate.isBackgroud) { + final audioTrack = stream.getAudioTracks()[0]; + audioTrack.enableSpeakerphone(speakerOn); + } + } + } + + void _addRemoteStream(MediaStream stream) async { + //const userId = this.getOpponentMember().userId; + final metadata = remoteSDPStreamMetadata!.sdpStreamMetadatas[stream.id]; + if (metadata == null) { + Logs().i( + 'Ignoring stream with id ${stream.id} because we didn\'t get any metadata about it'); + return; + } + + final purpose = metadata.purpose; + final audioMuted = metadata.audio_muted; + final videoMuted = metadata.video_muted; + + // Try to find a feed with the same purpose as the new stream, + // if we find it replace the old stream with the new one + final existingStream = + getRemoteStreams.where((element) => element.purpose == purpose); + if (existingStream.isNotEmpty) { + existingStream.first.setNewStream(stream); + } else { + final newStream = WrappedMediaStream( + renderer: voip.delegate.createRenderer(), + userId: remoteUser.id, + room: opts.room, + stream: stream, + purpose: purpose, + client: client, + audioMuted: audioMuted, + videoMuted: videoMuted, + isWeb: voip.delegate.isWeb, + ); + await newStream.initialize(); + streams.add(newStream); + } + fireCallEvent(CallEvent.kFeedsChanged); + Logs().i('Pushed remote stream (id="${stream.id}", purpose=$purpose)'); + } + + void setCallState(CallState newState) { + state = newState; + _callStateController.add(newState); + fireCallEvent(CallEvent.kState); + } + + void setLocalVideoMuted(bool muted) { + localUserMediaStream?.setVideoMuted(muted); + _updateMuteStatus(); + } + + bool get isLocalVideoMuted => localUserMediaStream?.isVideoMuted() ?? false; + + void setMicrophoneMuted(bool muted) { + localUserMediaStream?.setAudioMuted(muted); + _updateMuteStatus(); + } + + bool get isMicrophoneMuted => localUserMediaStream?.isAudioMuted() ?? false; + + void setRemoteOnHold(bool onHold) async { + if (isRemoteOnHold == onHold) return; + remoteOnHold = onHold; + final transceivers = await pc!.getTransceivers(); + for (final transceiver in transceivers) { + await transceiver.setDirection(onHold + ? TransceiverDirection.SendOnly + : TransceiverDirection.SendRecv); + } + _updateMuteStatus(); + fireCallEvent(CallEvent.kRemoteHoldUnhold); + } + + bool get isRemoteOnHold => remoteOnHold; + + Future isLocalOnHold() async { + if (state != CallState.kConnected) return false; + var callOnHold = true; + // We consider a call to be on hold only if *all* the tracks are on hold + // (is this the right thing to do?) + final transceivers = await pc!.getTransceivers(); + for (final transceiver in transceivers) { + final currentDirection = await transceiver.getCurrentDirection(); + Logs() + .i('transceiver.currentDirection = ${currentDirection?.toString()}'); + final trackOnHold = (currentDirection == TransceiverDirection.Inactive || + currentDirection == TransceiverDirection.RecvOnly); + if (!trackOnHold) { + callOnHold = false; + } + } + return callOnHold; + } + + void answer() async { + if (inviteOrAnswerSent) { + return; + } + // stop play ringtone + voip.delegate.stopRingtone(); + + if (direction == CallDirection.kIncoming) { + setCallState(CallState.kCreateAnswer); + + final answer = await pc!.createAnswer({}); + remoteCandidates.forEach((candidate) => pc!.addCandidate(candidate)); + + final callCapabilities = CallCapabilities() + ..dtmf = false + ..transferee = false; + + final metadata = SDPStreamMetadata({ + localUserMediaStream!.stream!.id: SDPStreamPurpose( + purpose: SDPStreamMetadataPurpose.Usermedia, + audio_muted: localUserMediaStream!.stream!.getAudioTracks().isEmpty, + video_muted: localUserMediaStream!.stream!.getVideoTracks().isEmpty) + }); + + final res = await room.answerCall(callId, answer.sdp!, localPartyId, + type: answer.type!, + capabilities: callCapabilities, + metadata: metadata); + Logs().v('[VOIP] answer res => $res'); + await pc!.setLocalDescription(answer); + setCallState(CallState.kConnecting); + inviteOrAnswerSent = true; + _answeredByUs = true; + } + } + + /// Reject a call + /// This used to be done by calling hangup, but is a separate method and protocol + /// event as of MSC2746. + /// + void reject() { + if (state != CallState.kRinging) { + Logs().e('[VOIP] Call must be in \'ringing\' state to reject!'); + return; + } + Logs().d('[VOIP] Rejecting call: $callId'); + terminate(CallParty.kLocal, CallErrorCode.UserHangup, true); + room.sendCallReject(callId, lifetimeMs, localPartyId); + } + + void hangup([String? reason, bool suppressEvent = true]) async { + // stop play ringtone + voip.delegate.stopRingtone(); + + terminate( + CallParty.kLocal, reason ?? CallErrorCode.UserHangup, !suppressEvent); + + try { + final res = await room.hangupCall(callId, localPartyId, 'userHangup'); + Logs().v('[VOIP] hangup res => $res'); + } catch (e) { + Logs().v('[VOIP] hangup error => ${e.toString()}'); + } + } + + void sendDTMF(String tones) async { + final senders = await pc!.getSenders(); + for (final sender in senders) { + if (sender.track != null && sender.track!.kind == 'audio') { + await sender.dtmfSender.insertDTMF(tones); + return; + } + } + Logs().e('Unable to find a track to send DTMF on'); + } + + void terminate(CallParty party, String hangupReason, bool shouldEmit) async { + if (state == CallState.kEnded) { + return; + } + + inviteTimer?.cancel(); + inviteTimer = null; + + ringingTimer?.cancel(); + ringingTimer = null; + + hangupParty = party; + hangupReason = hangupReason; + + setCallState(CallState.kEnded); + voip.currentCID = null; + voip.calls.remove(callId); + cleanUp(); + voip.delegate.handleCallEnded(this); + if (shouldEmit) { + fireCallEvent(CallEvent.kHangup); + } + } + + void onRejectReceived(String? reason) { + Logs().v('[VOIP] Reject received for call ID ' + callId); + // No need to check party_id for reject because if we'd received either + // an answer or reject, we wouldn't be in state InviteSent + final shouldTerminate = (state == CallState.kFledgling && + direction == CallDirection.kIncoming) || + CallState.kInviteSent == state || + CallState.kRinging == state; + + if (shouldTerminate) { + terminate(CallParty.kRemote, reason ?? CallErrorCode.UserHangup, true); + } else { + Logs().e('Call is in state: ${state.toString()}: ignoring reject'); + } + } + + Future _gotLocalOffer(RTCSessionDescription offer) async { + if (callHasEnded) { + Logs().d( + 'Ignoring newly created offer on call ID ${opts.callId} because the call has ended'); + return; + } + + try { + await pc!.setLocalDescription(offer); + } catch (err) { + Logs().d('Error setting local description! ${err.toString()}'); + terminate(CallParty.kLocal, CallErrorCode.SetLocalDescription, true); + return; + } + + if (callHasEnded) return; + + final callCapabilities = CallCapabilities() + ..dtmf = false + ..transferee = false; + final metadata = _getLocalSDPStreamMetadata(); + if (state == CallState.kCreateOffer) { + await room.inviteToCall( + callId, lifetimeMs, localPartyId, null, offer.sdp!, + capabilities: callCapabilities, metadata: metadata); + inviteOrAnswerSent = true; + setCallState(CallState.kInviteSent); + + inviteTimer = Timer(Duration(seconds: callTimeoutSec), () { + if (state == CallState.kInviteSent) { + hangup(CallErrorCode.InviteTimeout, false); + } + inviteTimer?.cancel(); + inviteTimer = null; + }); + } else { + await room.sendCallNegotiate(callId, lifetimeMs, localPartyId, offer.sdp!, + type: offer.type!, + capabilities: callCapabilities, + metadata: metadata); + } + } + + void onNegotiationNeeded() async { + Logs().i('Negotiation is needed!'); + makingOffer = true; + try { + final offer = await pc!.createOffer({}); + await _gotLocalOffer(offer); + } catch (e) { + _getLocalOfferFailed(e); + return; + } finally { + makingOffer = false; + } + } + + Future _preparePeerConnection() async { + try { + pc = await _createPeerConnection(); + + pc!.onRenegotiationNeeded = onNegotiationNeeded; + + pc!.onIceCandidate = (RTCIceCandidate candidate) async { + //Logs().v('[VOIP] onIceCandidate => ${candidate.toMap().toString()}'); + localCandidates.add(candidate); + }; + pc!.onIceGatheringState = (RTCIceGatheringState state) async { + Logs().v('[VOIP] IceGatheringState => ${state.toString()}'); + if (state == RTCIceGatheringState.RTCIceGatheringStateGathering) { + Timer(Duration(seconds: 3), () async { + if (!iceGatheringFinished) { + iceGatheringFinished = true; + await _candidateReady(); + } + }); + } + if (state == RTCIceGatheringState.RTCIceGatheringStateComplete) { + if (!iceGatheringFinished) { + iceGatheringFinished = true; + await _candidateReady(); + } + } + }; + pc!.onIceConnectionState = (RTCIceConnectionState state) { + Logs().v('[VOIP] RTCIceConnectionState => ${state.toString()}'); + if (state == RTCIceConnectionState.RTCIceConnectionStateConnected) { + localCandidates.clear(); + remoteCandidates.clear(); + setCallState(CallState.kConnected); + } else if (state == RTCIceConnectionState.RTCIceConnectionStateFailed) { + hangup(CallErrorCode.IceFailed, false); + } + }; + } catch (e) { + Logs().v('[VOIP] prepareMediaStream error => ${e.toString()}'); + } + } + + void onAnsweredElsewhere(String msg) { + Logs().d('Call ID $callId answered elsewhere'); + terminate(CallParty.kRemote, CallErrorCode.AnsweredElsewhere, true); + } + + void cleanUp() async { + streams.forEach((stream) { + stream.dispose(); + }); + streams.clear(); + if (pc != null) { + await pc!.close(); + await pc!.dispose(); + } + } + + void _updateMuteStatus() async { + final micShouldBeMuted = (localUserMediaStream != null && + localUserMediaStream!.isAudioMuted()) || + remoteOnHold; + final vidShouldBeMuted = (localUserMediaStream != null && + localUserMediaStream!.isVideoMuted()) || + remoteOnHold; + + _setTracksEnabled(localUserMediaStream?.stream!.getAudioTracks() ?? [], + !micShouldBeMuted); + _setTracksEnabled(localUserMediaStream?.stream!.getVideoTracks() ?? [], + !vidShouldBeMuted); + + await opts.room.sendSDPStreamMetadataChanged( + callId, localPartyId, _getLocalSDPStreamMetadata()); + } + + void _setTracksEnabled(List tracks, bool enabled) { + tracks.forEach((track) async { + track.enabled = enabled; + }); + } + + SDPStreamMetadata _getLocalSDPStreamMetadata() { + final sdpStreamMetadatas = {}; + for (final wpstream in getLocalStreams) { + sdpStreamMetadatas[wpstream.stream!.id] = SDPStreamPurpose( + purpose: wpstream.purpose, + audio_muted: wpstream.audioMuted, + video_muted: wpstream.videoMuted); + } + final metadata = SDPStreamMetadata(sdpStreamMetadatas); + Logs().v('Got local SDPStreamMetadata ${metadata.toJson().toString()}'); + return metadata; + } + + void restartIce() async { + Logs().v('[VOIP] iceRestart.'); + // Needs restart ice on session.pc and renegotiation. + iceGatheringFinished = false; + final desc = + await pc!.createOffer(_getOfferAnswerConstraints(iceRestart: true)); + await pc!.setLocalDescription(desc); + localCandidates.clear(); + } + + Future _getUserMedia(CallType type) async { + final mediaConstraints = { + 'audio': true, + 'video': type == CallType.kVideo + ? { + 'mandatory': { + 'minWidth': '640', + 'minHeight': '480', + 'minFrameRate': '30', + }, + 'facingMode': 'user', + 'optional': [], + } + : false, + }; + try { + return await voip.delegate.mediaDevices.getUserMedia(mediaConstraints); + } catch (e) { + _getUserMediaFailed(e); + } + return null; + } + + Future _getDisplayMedia() async { + final mediaConstraints = { + 'audio': false, + 'video': true, + }; + try { + return await voip.delegate.mediaDevices.getDisplayMedia(mediaConstraints); + } catch (e) { + _getUserMediaFailed(e); + } + return null; + } + + Future _createPeerConnection() async { + final configuration = { + 'iceServers': opts.iceServers, + 'sdpSemantics': 'unified-plan' + }; + final pc = await voip.delegate.createPeerConnection(configuration); + pc.onTrack = (RTCTrackEvent event) { + if (event.streams.isNotEmpty) { + final stream = event.streams[0]; + _addRemoteStream(stream); + } + }; + return pc; + } + + void tryRemoveStopedStreams() { + final removedStreams = {}; + streams.forEach((stream) { + if (stream.stopped) { + removedStreams[stream.stream!.id] = stream; + } + }); + streams + .removeWhere((stream) => removedStreams.containsKey(stream.stream!.id)); + removedStreams.forEach((id, element) { + _removeStream(element.stream!); + }); + } + + Future _removeStream(MediaStream stream) async { + Logs().v('Removing feed with stream id ${stream.id}'); + + final it = streams.where((element) => element.stream!.id == stream.id); + if (it.isEmpty) { + Logs().v('Didn\'t find the feed with stream id ${stream.id} to delete'); + return; + } + final wpstream = it.first; + streams.removeWhere((element) => element.stream!.id == stream.id); + fireCallEvent(CallEvent.kFeedsChanged); + await wpstream.dispose(); + } + + Map _getOfferAnswerConstraints({bool iceRestart = false}) { + return { + 'mandatory': {if (iceRestart) 'IceRestart': true}, + 'optional': [], + }; + } + + Future _candidateReady() async { + /* + Currently, trickle-ice is not supported, so it will take a + long time to wait to collect all the canidates, set the + timeout for collection canidates to speed up the connection. + */ + try { + final candidates = >[]; + localCandidates.forEach((element) { + candidates.add(element.toMap()); + }); + final res = + await room.sendCallCandidates(callId, localPartyId, candidates); + Logs().v('[VOIP] sendCallCandidates res => $res'); + } catch (e) { + Logs().v('[VOIP] sendCallCandidates e => ${e.toString()}'); + } + } + + void fireCallEvent(CallEvent event) { + _callEventController.add(event); + Logs().i('CallEvent: ${event.toString()}'); + switch (event) { + case CallEvent.kFeedsChanged: + break; + case CallEvent.kState: + Logs().i('CallState: ${state.toString()}'); + break; + case CallEvent.kError: + break; + case CallEvent.kHangup: + break; + case CallEvent.kReplaced: + break; + case CallEvent.kLocalHoldUnhold: + break; + case CallEvent.kRemoteHoldUnhold: + break; + case CallEvent.kAssertedIdentityChanged: + break; + } + } + + void _getLocalOfferFailed(dynamic err) { + Logs().e('Failed to get local offer ${err.toString()}'); + fireCallEvent(CallEvent.kError); + lastError = CallError( + CallErrorCode.LocalOfferFailed, 'Failed to get local offer!', err); + terminate(CallParty.kLocal, CallErrorCode.LocalOfferFailed, false); + } + + void _getUserMediaFailed(dynamic err) { + if (state != CallState.kConnected) { + Logs().w('Failed to get user media - ending call ${err.toString()}'); + fireCallEvent(CallEvent.kError); + lastError = CallError( + CallErrorCode.NoUserMedia, + 'Couldn\'t start capturing media! Is your microphone set up and does this app have permission?', + err); + terminate(CallParty.kLocal, CallErrorCode.NoUserMedia, false); + } + } + + void onSelectAnswerReceived(String? selectedPartyId) { + if (direction != CallDirection.kIncoming) { + Logs().w('Got select_answer for an outbound call: ignoring'); + return; + } + if (selectedPartyId == null) { + Logs().w( + 'Got nonsensical select_answer with null/undefined selected_party_id: ignoring'); + return; + } + + if (selectedPartyId != localPartyId) { + Logs().w( + 'Got select_answer for party ID $selectedPartyId: we are party ID $localPartyId.'); + // The other party has picked somebody else's answer + terminate(CallParty.kRemote, CallErrorCode.AnsweredElsewhere, true); + } + } +} + +class VoIP { + TurnServerCredentials? _turnServerCredentials; + Map calls = {}; + String? currentCID; + String? get localPartyId => client.deviceID; + final Client client; + final WebRTCDelegate delegate; + + VoIP(this.client, this.delegate) : super() { + client.onCallInvite.stream.listen(onCallInvite); + client.onCallAnswer.stream.listen(onCallAnswer); + client.onCallCandidates.stream.listen(onCallCandidates); + client.onCallHangup.stream.listen(onCallHangup); + client.onCallReject.stream.listen(onCallReject); + client.onCallNegotiate.stream.listen(onCallNegotiate); + client.onCallReplaces.stream.listen(onCallReplaces); + client.onCallSelectAnswer.stream.listen(onCallSelectAnswer); + client.onSDPStreamMetadataChangedReceived.stream + .listen(onSDPStreamMetadataChangedReceived); + client.onAssertedIdentityReceived.stream.listen(onAssertedIdentityReceived); + } + + Future onCallInvite(Event event) async { + if (event.senderId == client.userID) { + // Ignore messages to yourself. + return; + } + + Logs().v( + '[VOIP] onCallInvite ${event.senderId} => ${client.userID}, \ncontent => ${event.content.toString()}'); + + final String callId = event.content['call_id']; + final String partyId = event.content['party_id']; + final int lifetime = event.content['lifetime']; + + if (currentCID != null) { + // Only one session at a time. + Logs().v('[VOIP] onCallInvite: There is already a session.'); + await event.room.hangupCall(callId, localPartyId!, 'userBusy'); + return; + } + if (calls[callId] != null) { + // Session already exist. + Logs().v('[VOIP] onCallInvite: Session [$callId] already exist.'); + return; + } + + if (event.content['capabilities'] != null) { + final capabilities = + CallCapabilities.fromJson(event.content['capabilities']); + Logs().v( + '[VOIP] CallCapabilities: dtmf => ${capabilities.dtmf}, transferee => ${capabilities.transferee}'); + } + + var callType = CallType.kVoice; + SDPStreamMetadata? sdpStreamMetadata; + if (event.content[sdpStreamMetadataKey] != null) { + sdpStreamMetadata = + SDPStreamMetadata.fromJson(event.content[sdpStreamMetadataKey]); + sdpStreamMetadata.sdpStreamMetadatas + .forEach((streamId, SDPStreamPurpose purpose) { + Logs().v( + '[VOIP] [$streamId] => purpose: ${purpose.purpose}, audioMuted: ${purpose.audio_muted}, videoMuted: ${purpose.video_muted}'); + + if (!purpose.video_muted) { + callType = CallType.kVideo; + } + }); + } else { + callType = getCallType(event.content['offer']['sdp']); + } + + final opts = CallOptions() + ..voip = this + ..callId = callId + ..dir = CallDirection.kIncoming + ..type = callType + ..room = event.room + ..localPartyId = localPartyId! + ..iceServers = await getIceSevers(); + + final newCall = createNewCall(opts); + newCall.remotePartyId = partyId; + newCall.remoteUser = event.sender; + final offer = RTCSessionDescription( + event.content['offer']['sdp'], + event.content['offer']['type'], + ); + await newCall + .initWithInvite(callType, offer, sdpStreamMetadata, lifetime) + .then((_) { + // Popup CallingPage for incoming call. + if (!delegate.isBackgroud) { + delegate.handleNewCall(newCall); + } + }); + currentCID = callId; + + if (delegate.isBackgroud) { + /// Forced to enable signaling synchronization until the end of the call. + client.backgroundSync = true; + + ///TODO: notify the callkeep that the call is incoming. + } + // Play ringtone + delegate.playRingtone(); + } + + void onCallAnswer(Event event) async { + Logs().v('[VOIP] onCallAnswer => ${event.content.toString()}'); + final String callId = event.content['call_id']; + final String partyId = event.content['party_id']; + + final call = calls[callId]; + if (call != null) { + if (event.senderId == client.userID) { + // Ignore messages to yourself. + if (!call._answeredByUs) { + delegate.stopRingtone(); + } + if (call.state == CallState.kRinging) { + call.onAnsweredElsewhere('Call ID ' + callId + ' answered elsewhere'); + } + return; + } + + call.remotePartyId = partyId; + call.remoteUser = event.sender; + + final answer = RTCSessionDescription( + event.content['answer']['sdp'], event.content['answer']['type']); + + SDPStreamMetadata? metadata; + if (event.content[sdpStreamMetadataKey] != null) { + metadata = + SDPStreamMetadata.fromJson(event.content[sdpStreamMetadataKey]); + } + call.onAnswerReceived(answer, metadata); + + /// Send select_answer event. + await event.room.selectCallAnswer( + callId, lifetimeMs, localPartyId!, call.remotePartyId!); + } else { + Logs().v('[VOIP] onCallAnswer: Session [$callId] not found!'); + } + } + + void onCallCandidates(Event event) async { + if (event.senderId == client.userID) { + // Ignore messages to yourself. + return; + } + Logs().v('[VOIP] onCallCandidates => ${event.content.toString()}'); + final String callId = event.content['call_id']; + final call = calls[callId]; + if (call != null) { + call.onCandidatesReceived(event.content['candidates']); + } else { + Logs().v('[VOIP] onCallCandidates: Session [$callId] not found!'); + } + } + + void onCallHangup(Event event) async { + // stop play ringtone, if this is an incoming call + if (!delegate.isBackgroud) { + delegate.stopRingtone(); + } + Logs().v('[VOIP] onCallHangup => ${event.content.toString()}'); + final String callId = event.content['call_id']; + final call = calls[callId]; + if (call != null) { + // hangup in any case, either if the other party hung up or we did on another device + call.terminate(CallParty.kRemote, + event.content['reason'] ?? CallErrorCode.UserHangup, true); + } else { + Logs().v('[VOIP] onCallHangup: Session [$callId] not found!'); + } + currentCID = null; + } + + void onCallReject(Event event) async { + if (event.senderId == client.userID) { + // Ignore messages to yourself. + return; + } + final String callId = event.content['call_id']; + Logs().d('Reject received for call ID ' + callId); + + final call = calls[callId]; + if (call != null) { + call.onRejectReceived(event.content['reason']); + } else { + Logs().v('[VOIP] onCallHangup: Session [$callId] not found!'); + } + } + + void onCallReplaces(Event event) async { + if (event.senderId == client.userID) { + // Ignore messages to yourself. + return; + } + final String callId = event.content['call_id']; + Logs().d('onCallReplaces received for call ID ' + callId); + final call = calls[callId]; + if (call != null) { + //TODO: handle replaces + } + } + + void onCallSelectAnswer(Event event) async { + if (event.senderId == client.userID) { + // Ignore messages to yourself. + return; + } + final String callId = event.content['call_id']; + Logs().d('SelectAnswer received for call ID ' + callId); + final call = calls[callId]; + final String selectedPartyId = event.content['selected_party_id']; + + if (call != null) { + call.onSelectAnswerReceived(selectedPartyId); + } + } + + void onSDPStreamMetadataChangedReceived(Event event) async { + if (event.senderId == client.userID) { + // Ignore messages to yourself. + return; + } + final String callId = event.content['call_id']; + Logs().d('SDP Stream metadata received for call ID ' + callId); + final call = calls[callId]; + if (call != null) { + if (event.content[sdpStreamMetadataKey] == null) { + Logs().d('SDP Stream metadata is null'); + return; + } + call.onSDPStreamMetadataReceived( + SDPStreamMetadata.fromJson(event.content[sdpStreamMetadataKey])); + } + } + + void onAssertedIdentityReceived(Event event) async { + if (event.senderId == client.userID) { + // Ignore messages to yourself. + return; + } + final String callId = event.content['call_id']; + Logs().d('Asserted identity received for call ID ' + callId); + final call = calls[callId]; + if (call != null) { + if (event.content['asserted_identity'] == null) { + Logs().d('asserted_identity is null '); + return; + } + call.onAssertedIdentityReceived( + AssertedIdentity.fromJson(event.content['asserted_identity'])); + } + } + + void onCallNegotiate(Event event) async { + if (event.senderId == client.userID) { + // Ignore messages to yourself. + return; + } + final String callId = event.content['call_id']; + Logs().d('Negotiate received for call ID ' + callId); + final call = calls[callId]; + if (call != null) { + final description = event.content['description']; + try { + SDPStreamMetadata? metadata; + if (event.content[sdpStreamMetadataKey] != null) { + metadata = + SDPStreamMetadata.fromJson(event.content[sdpStreamMetadataKey]); + } + call.onNegotiateReceived(metadata, + RTCSessionDescription(description['sdp'], description['type'])); + } catch (err) { + Logs().e('Failed to complete negotiation ${err.toString()}'); + } + } + } + + CallType getCallType(String sdp) { + try { + final session = sdp_transform.parse(sdp); + if (session['media'].indexWhere((e) => e['type'] == 'video') != -1) { + return CallType.kVideo; + } + } catch (err) { + Logs().e('Failed to getCallType ${err.toString()}'); + } + + return CallType.kVoice; + } + + Future requestTurnServerCredentials() async { + return true; + } + + Future>> getIceSevers() async { + if (_turnServerCredentials == null) { + try { + _turnServerCredentials = await client.getTurnServer(); + } catch (e) { + Logs().v('[VOIP] getTurnServerCredentials error => ${e.toString()}'); + } + } + + if (_turnServerCredentials == null) { + return []; + } + + return [ + { + 'username': _turnServerCredentials!.username, + 'credential': _turnServerCredentials!.password, + 'urls': _turnServerCredentials!.uris[0] + } + ]; + } + + Future inviteToCall(String roomId, CallType type) async { + final room = client.getRoomById(roomId); + if (room == null) { + Logs().v('[VOIP] Invalid room id [$roomId].'); + return Null as CallSession; + } + final callId = 'cid${DateTime.now().millisecondsSinceEpoch}'; + final opts = CallOptions() + ..callId = callId + ..type = type + ..dir = CallDirection.kOutgoing + ..room = room + ..voip = this + ..localPartyId = localPartyId! + ..iceServers = await getIceSevers(); + + final newCall = createNewCall(opts); + currentCID = callId; + await newCall.initOutboundCall(type).then((_) { + if (!delegate.isBackgroud) { + delegate.handleNewCall(newCall); + } + }); + currentCID = callId; + return newCall; + } + + CallSession createNewCall(CallOptions opts) { + final call = CallSession(opts); + calls[opts.callId] = call; + return call; + } +} diff --git a/lib/src/voip_content.dart b/lib/src/voip_content.dart new file mode 100644 index 0000000..c39dce7 --- /dev/null +++ b/lib/src/voip_content.dart @@ -0,0 +1,160 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +class CallReplacesTarget { + String? id; + String? display_name; + String? avatar_url; + + CallReplacesTarget({this.id, this.display_name, this.avatar_url}); + factory CallReplacesTarget.fromJson(Map json) => + CallReplacesTarget( + id: json['id'].toString(), + display_name: json['display_name'].toString(), + avatar_url: json['avatar_url'].toString(), + ); + + Map toJson() => { + if (id != null) 'id': id, + if (display_name != null) 'display_name': display_name, + if (avatar_url != null) 'avatar_url': avatar_url, + }; +} + +/// MSC2747: VoIP call transfers +/// https://github.com/matrix-org/matrix-doc/pull/2747 +class CallReplaces { + String? replacement_id; + CallReplacesTarget? target_user; + String? create_call; + String? await_call; + String? target_room; + + CallReplaces({ + this.replacement_id, + this.target_user, + this.create_call, + this.await_call, + this.target_room, + }); + factory CallReplaces.fromJson(Map json) => CallReplaces( + replacement_id: json['replacement_id']?.toString(), + create_call: json['create_call']?.toString(), + await_call: json['await_call']?.toString(), + target_room: json['target_room']?.toString(), + target_user: CallReplacesTarget.fromJson(json['target_user']), + ); + + Map toJson() => { + if (replacement_id != null) 'replacement_id': replacement_id, + if (target_user != null) 'target_user': target_user!.toJson(), + if (create_call != null) 'create_call': create_call, + if (await_call != null) 'await_call': await_call, + if (target_room != null) 'target_room': target_room, + }; +} + +// TODO: Change to "sdp_stream_metadata" when MSC3077 is merged +const String sdpStreamMetadataKey = 'org.matrix.msc3077.sdp_stream_metadata'; + +/// https://github.com/matrix-org/matrix-doc/blob/dbkr/msc2747/proposals/2747-voip-call-transfer.md#capability-advertisment +/// https://github.com/matrix-org/matrix-doc/blob/dbkr/msc2746/proposals/2746-reliable-voip.md#add-dtmf +class CallCapabilities { + bool transferee; + bool dtmf; + CallCapabilities({this.transferee = false, this.dtmf = false}); + factory CallCapabilities.fromJson(Map json) => + CallCapabilities( + dtmf: json['m.call.dtmf'] as bool? ?? false, + transferee: json['m.call.transferee'] as bool? ?? false, + ); + Map toJson() => { + 'm.call.transferee': transferee, + 'm.call.dtmf': dtmf, + }; +} + +/// MSC3077: Support for multi-stream VoIP +/// https://github.com/matrix-org/matrix-doc/pull/3077 +/// +/// MSC3291: Muting in VoIP calls +/// https://github.com/SimonBrandner/matrix-doc/blob/msc/muting/proposals/3291-muting.md +/// +/// This MSC proposes adding an sdp_stream_metadata field +/// to the events containing a session description i.e.: +/// m.call.invite, m.call.answer, m.call.negotiate +/// +class SDPStreamPurpose { + // SDPStreamMetadataPurpose + String purpose; + bool audio_muted; + bool video_muted; + + SDPStreamPurpose( + {required this.purpose, + this.audio_muted = false, + this.video_muted = false}); + factory SDPStreamPurpose.fromJson(Map json) => + SDPStreamPurpose( + audio_muted: json['audio_muted'] as bool? ?? false, + video_muted: json['video_muted'] as bool? ?? false, + purpose: json['purpose'] as String, + ); + + Map toJson() => { + 'purpose': purpose, + 'audio_muted': audio_muted, + 'video_muted': video_muted, + }; +} + +class SDPStreamMetadataPurpose { + static String Usermedia = 'm.usermedia'; + static String Screenshare = 'm.screenshare'; +} + +class SDPStreamMetadata { + Map sdpStreamMetadatas; + SDPStreamMetadata(this.sdpStreamMetadatas); + + factory SDPStreamMetadata.fromJson(Map json) => + SDPStreamMetadata(json.map( + (key, value) => MapEntry(key, SDPStreamPurpose.fromJson(value)))); + Map toJson() => + sdpStreamMetadatas.map((key, value) => MapEntry(key, value.toJson())); +} + +/// MSC3086: Asserted identity on VoIP calls +/// https://github.com/matrix-org/matrix-doc/pull/3086 +class AssertedIdentity { + String? id; + String? displayName; + String? avatarUrl; + AssertedIdentity({this.id, this.displayName, this.avatarUrl}); + factory AssertedIdentity.fromJson(Map json) => + AssertedIdentity( + displayName: json['display_name'] as String?, + id: json['id'] as String?, + avatarUrl: json['avatar_url'] as String?, + ); + Map toJson() => { + if (displayName != null) 'display_name': displayName, + if (id != null) 'id': id, + if (avatarUrl != null) 'avatar_url': avatarUrl, + }; +} diff --git a/lib/widget.dart b/lib/widget.dart new file mode 100644 index 0000000..028904f --- /dev/null +++ b/lib/widget.dart @@ -0,0 +1,69 @@ +import 'package:matrix/src/room.dart'; + +class MatrixWidget { + final Room room; + final String? creatorUserId; + final Map? data; + final String? id; + final String? name; + final String type; + + /// use [buildWidgetUrl] instead + final String url; + final bool waitForIframeLoad; + + MatrixWidget({ + required this.room, + this.creatorUserId, + this.data = const {}, + this.id, + required this.name, + required this.type, + required this.url, + this.waitForIframeLoad = false, + }); + + factory MatrixWidget.fromJson(Map json, Room room) => + MatrixWidget( + room: room, + creatorUserId: + json.containsKey('creatorUserId') ? json['creatorUserId'] : null, + data: json.containsKey('data') ? json['data'] : {}, + id: json.containsKey('id') ? json['id'] : null, + name: json['name'], + type: json['type'], + url: json['url'], + waitForIframeLoad: json.containsKey('waitForIframeLoad') + ? json['waitForIframeLoad'] + : false, + ); + + Future buildWidgetUrl() async { + // See https://github.com/matrix-org/matrix-doc/issues/1236 for a + // description, specifically the section + // `What does the other stuff in content mean?` + final userProfile = await room.client.ownProfile; + var parsedUri = url; + + // a key-value map with the strings to be replaced + final replaceMap = { + r'$matrix_user_id': userProfile.userId, + r'$matrix_room_id': room.id, + r'$matrix_display_name': userProfile.displayName ?? '', + r'$matrix_avatar_url': userProfile.avatarUrl?.toString() ?? '', + // removing potentially dangerous keys containing anything but + // `[a-zA-Z0-9_-]` as well as non string values + if (data != null) + ...Map.from(data!) + ..removeWhere((key, value) => + !RegExp(r'^[\w-]+$').hasMatch(key) || !value is String) + ..map((key, value) => MapEntry('\$key', value)), + }; + + replaceMap.forEach((key, value) { + parsedUri = parsedUri.replaceAll(key, Uri.encodeComponent(value)); + }); + + return Uri.parse(parsedUri); + } +} diff --git a/pubspec.yaml b/pubspec.yaml new file mode 100644 index 0000000..7b20cd6 --- /dev/null +++ b/pubspec.yaml @@ -0,0 +1,39 @@ +name: matrix +description: Matrix Dart SDK +version: 0.8.13 +homepage: https://famedly.com +repository: https://gitlab.com/famedly/company/frontend/famedlysdk.git + +environment: + sdk: ">=2.12.0 <3.0.0" + +dependencies: + blurhash_dart: ^1.1.0 + http: ^0.13.0 + mime: ^1.0.0 + canonical_json: ^1.1.0 + markdown: ^4.0.0 + html_unescape: ^2.0.0 + random_string: ^2.3.1 + crypto: ^3.0.0 + base58check: ^2.0.0 + olm: ^2.0.0 + matrix_api_lite: ^0.5.1 + hive: ^2.0.4 + image: ^3.1.1 + ffi: ^1.0.0 + js: ^0.6.3 + slugify: ^2.0.0 + html: ^0.15.0 + collection: ^1.15.0 + webrtc_interface: ^1.0.1 + sdp_transform: ^0.3.2 + fluffybox: ^0.4.3 + +dev_dependencies: + dart_code_metrics: ^4.10.1 + pedantic: ^1.11.0 + test: ^1.15.7 + coverage: ">=0.15.0 <2.0.0" + file: ^6.1.1 + #flutter_test: {sdk: flutter} diff --git a/scripts/prepare.sh b/scripts/prepare.sh new file mode 100644 index 0000000..a619b42 --- /dev/null +++ b/scripts/prepare.sh @@ -0,0 +1,27 @@ +#!/bin/sh + +mkdir js +cd js +curl -O 'https://packages.matrix.org/npm/olm/olm-3.1.4.tgz' +tar xaf olm-3.1.4.tgz +cd .. + +if [ -f /usr/lib/x86_64-linux-gnu/libolm.so.3 ] +then +mkdir -p ffi/olm/ +ln -sf /usr/lib/x86_64-linux-gnu/libolm.so.3 ffi/olm/libolm.so +else +cd ffi +pushd ffi +git clone --depth 1 https://gitlab.matrix.org/matrix-org/olm.git +cd olm +cmake -DCMAKE_BUILD_TYPE=Release . +cmake --build . +cd .. +fi + +if which flutter >/dev/null; then + flutter pub get +else + pub get +fi diff --git a/scripts/test.sh b/scripts/test.sh new file mode 100644 index 0000000..0c3f63a --- /dev/null +++ b/scripts/test.sh @@ -0,0 +1,6 @@ +#!/bin/sh -e +# pub run test -p vm +flutter test --coverage +flutter pub global activate remove_from_coverage +flutter pub global run remove_from_coverage:remove_from_coverage -f coverage/lcov.info -r '\.g\.dart$' +genhtml -o coverage coverage/lcov.info || true diff --git a/scripts/test_driver.sh b/scripts/test_driver.sh new file mode 100644 index 0000000..50a1e9b --- /dev/null +++ b/scripts/test_driver.sh @@ -0,0 +1,2 @@ +#!/bin/sh -e +pub run test_driver/matrixsdk_test.dart -p vm \ No newline at end of file diff --git a/test/canonical_json_test.dart b/test/canonical_json_test.dart new file mode 100644 index 0000000..8787dce --- /dev/null +++ b/test/canonical_json_test.dart @@ -0,0 +1,54 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:canonical_json/canonical_json.dart'; +import 'package:matrix/matrix.dart'; +import 'package:test/test.dart'; + +void main() { + /// All Tests related to the ChatTime + group('Canonical Json', () { + Logs().level = Level.error; + final textMap = >{ + '{}': {}, + '{"one":1,"two":"Two"}': {'one': 1, 'two': 'Two'}, + '{"a":"1","b":"2"}': {'b': '2', 'a': '1'}, + '{"auth":{"mxid":"@john.doe:example.com","profile":{"display_name":"John Doe","three_pids":[{"address":"john.doe@example.org","medium":"email"},{"address":"123456789","medium":"msisdn"}]},"success":true}}': + { + 'auth': { + 'success': true, + 'mxid': '@john.doe:example.com', + 'profile': { + 'display_name': 'John Doe', + 'three_pids': [ + {'medium': 'email', 'address': 'john.doe@example.org'}, + {'medium': 'msisdn', 'address': '123456789'} + ] + } + } + }, + '{"a":null}': {'a': null}, + }; + for (final entry in textMap.entries) { + test(entry.key, () async { + expect( + entry.key, String.fromCharCodes(canonicalJson.encode(entry.value))); + }); + } + }); +} diff --git a/test/client_test.dart b/test/client_test.dart new file mode 100644 index 0000000..4f6963a --- /dev/null +++ b/test/client_test.dart @@ -0,0 +1,921 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:async'; +import 'dart:convert'; +import 'dart:typed_data'; + +import 'package:matrix/matrix.dart'; + +import 'package:olm/olm.dart' as olm; +import 'package:test/test.dart'; +import 'package:canonical_json/canonical_json.dart'; + +import 'fake_client.dart'; +import 'fake_database.dart'; +import 'fake_matrix_api.dart'; + +void main() { + late Client matrix; + + Future> eventUpdateListFuture; + Future> toDeviceUpdateListFuture; + + // key @test:fakeServer.notExisting + const pickledOlmAccount = + 'N2v1MkIFGcl0mQpo2OCwSopxPQJ0wnl7oe7PKiT4141AijfdTIhRu+ceXzXKy3Kr00nLqXtRv7kid6hU4a+V0rfJWLL0Y51+3Rp/ORDVnQy+SSeo6Fn4FHcXrxifJEJ0djla5u98fBcJ8BSkhIDmtXRPi5/oJAvpiYn+8zMjFHobOeZUAxYR0VfQ9JzSYBsSovoQ7uFkNks1M4EDUvHtuyg3RxViwdNxs3718fyAqQ/VSwbXsY0Nl+qQbF+nlVGHenGqk5SuNl1P6e1PzZxcR0IfXA94Xij1Ob5gDv5YH4UCn9wRMG0abZsQP0YzpDM0FLaHSCyo9i5JD/vMlhH+nZWrgAzPPCTNGYewNV8/h3c+VyJh8ZTx/fVi6Yq46Fv+27Ga2ETRZ3Qn+Oyx6dLBjnBZ9iUvIhqpe2XqaGA1PopOz8iDnaZitw'; + const identityKey = '7rvl3jORJkBiK4XX1e5TnGnqz068XfYJ0W++Ml63rgk'; + const fingerprintKey = 'gjL//fyaFHADt9KBADGag8g7F8Up78B/K1zXeiEPLJo'; + + /// All Tests related to the Login + group('Client', () { + Logs().level = Level.error; + + /// Check if all Elements get created + + matrix = Client( + 'testclient', + httpClient: FakeMatrixApi(), + databaseBuilder: getDatabase, + ); + + eventUpdateListFuture = matrix.onEvent.stream.toList(); + toDeviceUpdateListFuture = matrix.onToDeviceEvent.stream.toList(); + + var olmEnabled = true; + + test('Login', () async { + try { + await olm.init(); + olm.get_library_version(); + } catch (e) { + olmEnabled = false; + Logs().w('[LibOlm] Failed to load LibOlm', e); + } + Logs().w('[LibOlm] Enabled: $olmEnabled'); + + var presenceCounter = 0; + var accountDataCounter = 0; + matrix.onPresence.stream.listen((Presence data) { + presenceCounter++; + }); + matrix.onAccountData.stream.listen((BasicEvent data) { + accountDataCounter++; + }); + + expect(matrix.homeserver, null); + + try { + await matrix.checkHomeserver('https://fakeserver.wrongaddress'); + } catch (exception) { + expect(exception.toString().isNotEmpty, true); + } + await matrix.checkHomeserver('https://fakeserver.notexisting', + checkWellKnown: false); + expect(matrix.homeserver.toString(), 'https://fakeserver.notexisting'); + + final available = await matrix.checkUsernameAvailability('testuser'); + expect(available, true); + + final loginStateFuture = matrix.onLoginStateChanged.stream.first; + final firstSyncFuture = matrix.onFirstSync.stream.first; + final syncFuture = matrix.onSync.stream.first; + + await matrix.init( + newToken: 'abcd', + newUserID: '@test:fakeServer.notExisting', + newHomeserver: matrix.homeserver, + newDeviceName: 'Text Matrix Client', + newDeviceID: 'GHTYAJCE', + newOlmAccount: pickledOlmAccount, + ); + + await Future.delayed(Duration(milliseconds: 50)); + + final loginState = await loginStateFuture; + final firstSync = await firstSyncFuture; + final sync = await syncFuture; + + expect(loginState, LoginState.loggedIn); + expect(firstSync, true); + expect(matrix.encryptionEnabled, olmEnabled); + if (olmEnabled) { + expect(matrix.identityKey, identityKey); + expect(matrix.fingerprintKey, fingerprintKey); + } + expect(sync.nextBatch == matrix.prevBatch, true); + + expect(matrix.accountData.length, 9); + expect(matrix.getDirectChatFromUserId('@bob:example.com'), + '!726s6s6q:example.com'); + expect(matrix.rooms[1].directChatMatrixID, '@bob:example.com'); + expect(matrix.directChats, matrix.accountData['m.direct']?.content); + expect(matrix.presences.length, 1); + expect(matrix.rooms[1].ephemerals.length, 2); + expect(matrix.rooms[1].typingUsers.length, 1); + expect(matrix.rooms[1].typingUsers[0].id, '@alice:example.com'); + expect(matrix.rooms[1].roomAccountData.length, 3); + expect(matrix.rooms[1].encrypted, true); + expect(matrix.rooms[1].encryptionAlgorithm, + Client.supportedGroupEncryptionAlgorithms.first); + expect( + matrix.rooms[1].roomAccountData['m.receipt'] + ?.content['@alice:example.com']['ts'], + 1436451550453); + expect( + matrix.rooms[1].roomAccountData['m.receipt'] + ?.content['@alice:example.com']['event_id'], + '7365636s6r6432:example.com'); + + final inviteRoom = matrix.rooms + .singleWhere((room) => room.membership == Membership.invite); + expect(inviteRoom.name, 'My Room Name'); + expect(inviteRoom.states[EventTypes.RoomMember]?.length, 1); + expect(matrix.rooms.length, 2); + expect(matrix.rooms[1].canonicalAlias, + "#famedlyContactDiscovery:${matrix.userID!.split(":")[1]}"); + expect(matrix.presences['@alice:example.com']?.presence.presence, + PresenceType.online); + expect(presenceCounter, 1); + expect(accountDataCounter, 9); + await Future.delayed(Duration(milliseconds: 50)); + expect(matrix.userDeviceKeys.length, 4); + expect(matrix.userDeviceKeys['@alice:example.com']?.outdated, false); + expect(matrix.userDeviceKeys['@alice:example.com']?.deviceKeys.length, 2); + expect( + matrix.userDeviceKeys['@alice:example.com']?.deviceKeys['JLAFKJWSCS'] + ?.verified, + false); + + await matrix.handleSync(SyncUpdate.fromJson({ + 'next_batch': 'fakesync', + 'device_lists': { + 'changed': [ + '@alice:example.com', + ], + 'left': [ + '@bob:example.com', + ], + } + })); + await Future.delayed(Duration(milliseconds: 50)); + expect(matrix.userDeviceKeys.length, 3); + expect(matrix.userDeviceKeys['@alice:example.com']?.outdated, true); + + await matrix.handleSync(SyncUpdate.fromJson({ + 'next_batch': 'fakesync', + 'rooms': { + 'join': { + '!726s6s6q:example.com': { + 'state': { + 'events': [ + { + 'sender': '@alice:example.com', + 'type': 'm.room.canonical_alias', + 'content': {'alias': ''}, + 'state_key': '', + 'origin_server_ts': 1417731086799, + 'event_id': '66697273743033:example.com' + } + ] + } + } + } + } + })); + await Future.delayed(Duration(milliseconds: 50)); + + expect( + matrix.getRoomByAlias( + "#famedlyContactDiscovery:${matrix.userID!.split(":")[1]}"), + null); + }); + + test('Logout', () async { + final loginStateFuture = matrix.onLoginStateChanged.stream.first; + await matrix.logout(); + + expect(matrix.accessToken == null, true); + expect(matrix.homeserver == null, true); + expect(matrix.userID == null, true); + expect(matrix.deviceID == null, true); + expect(matrix.deviceName == null, true); + expect(matrix.prevBatch == null, true); + + final loginState = await loginStateFuture; + expect(loginState, LoginState.loggedOut); + }); + + test('Event Update Test', () async { + await matrix.onEvent.close(); + + final eventUpdateList = await eventUpdateListFuture; + + expect(eventUpdateList.length, 14); + + expect(eventUpdateList[0].content['type'], 'm.room.member'); + expect(eventUpdateList[0].roomID, '!726s6s6q:example.com'); + expect(eventUpdateList[0].type, EventUpdateType.state); + + expect(eventUpdateList[1].content['type'], 'm.room.canonical_alias'); + expect(eventUpdateList[1].roomID, '!726s6s6q:example.com'); + expect(eventUpdateList[1].type, EventUpdateType.state); + + expect(eventUpdateList[2].content['type'], 'm.room.encryption'); + expect(eventUpdateList[2].roomID, '!726s6s6q:example.com'); + expect(eventUpdateList[2].type, EventUpdateType.state); + + expect(eventUpdateList[3].content['type'], 'm.room.pinned_events'); + expect(eventUpdateList[3].roomID, '!726s6s6q:example.com'); + expect(eventUpdateList[3].type, EventUpdateType.state); + + expect(eventUpdateList[4].content['type'], 'm.room.member'); + expect(eventUpdateList[4].roomID, '!726s6s6q:example.com'); + expect(eventUpdateList[4].type, EventUpdateType.timeline); + + expect(eventUpdateList[5].content['type'], 'm.room.message'); + expect(eventUpdateList[5].roomID, '!726s6s6q:example.com'); + expect(eventUpdateList[5].type, EventUpdateType.timeline); + + expect(eventUpdateList[6].content['type'], 'm.typing'); + expect(eventUpdateList[6].roomID, '!726s6s6q:example.com'); + expect(eventUpdateList[6].type, EventUpdateType.ephemeral); + + expect(eventUpdateList[7].content['type'], 'm.receipt'); + expect(eventUpdateList[7].roomID, '!726s6s6q:example.com'); + expect(eventUpdateList[7].type, EventUpdateType.ephemeral); + + expect(eventUpdateList[8].content['type'], 'm.receipt'); + expect(eventUpdateList[8].roomID, '!726s6s6q:example.com'); + expect(eventUpdateList[8].type, EventUpdateType.accountData); + + expect(eventUpdateList[9].content['type'], 'm.tag'); + expect(eventUpdateList[9].roomID, '!726s6s6q:example.com'); + expect(eventUpdateList[9].type, EventUpdateType.accountData); + + expect(eventUpdateList[10].content['type'], + 'org.example.custom.room.config'); + expect(eventUpdateList[10].roomID, '!726s6s6q:example.com'); + expect(eventUpdateList[10].type, EventUpdateType.accountData); + + expect(eventUpdateList[11].content['type'], 'm.room.name'); + expect(eventUpdateList[11].roomID, '!696r7674:example.com'); + expect(eventUpdateList[11].type, EventUpdateType.inviteState); + + expect(eventUpdateList[12].content['type'], 'm.room.member'); + expect(eventUpdateList[12].roomID, '!696r7674:example.com'); + expect(eventUpdateList[12].type, EventUpdateType.inviteState); + }); + + test('To Device Update Test', () async { + await matrix.onToDeviceEvent.close(); + + final eventUpdateList = await toDeviceUpdateListFuture; + + expect(eventUpdateList.length, 2); + + expect(eventUpdateList[0].type, 'm.new_device'); + if (olmEnabled) { + expect(eventUpdateList[1].type, 'm.room_key'); + } else { + expect(eventUpdateList[1].type, 'm.room.encrypted'); + } + }); + + test('Login', () async { + matrix = Client( + 'testclient', + httpClient: FakeMatrixApi(), + databaseBuilder: getDatabase, + ); + + eventUpdateListFuture = matrix.onEvent.stream.toList(); + + await matrix.checkHomeserver('https://fakeserver.notexisting', + checkWellKnown: false); + + final loginResp = await matrix.login(LoginType.mLoginPassword, + identifier: AuthenticationUserIdentifier(user: 'test'), + password: '1234'); + + expect(loginResp.userId != null, true); + }); + + test('setAvatar', () async { + final testFile = MatrixFile(bytes: Uint8List(0), name: 'file.jpeg'); + await matrix.setAvatar(testFile); + }); + + test('setMuteAllPushNotifications', () async { + await matrix.setMuteAllPushNotifications(false); + }); + + test('createSpace', () async { + await matrix.createSpace( + name: 'space', + topic: 'My test space', + spaceAliasName: '#myspace:example.invalid', + invite: ['@alice:example.invalid'], + roomVersion: '3', + ); + }); + + test('get archive', () async { + final archive = await matrix.loadArchive(); + + await Future.delayed(Duration(milliseconds: 50)); + expect(archive.length, 2); + expect(archive[0].id, '!5345234234:example.com'); + expect(archive[0].membership, Membership.leave); + expect(archive[0].name, 'The room name'); + expect(archive[0].lastEvent?.body, 'This is an example text message'); + expect(archive[0].roomAccountData.length, 1); + expect(archive[1].id, '!5345234235:example.com'); + expect(archive[1].membership, Membership.leave); + expect(archive[1].name, 'The room name 2'); + }); + + test('sync state event in-memory handling', () async { + final roomId = '!726s6s6q:example.com'; + final room = matrix.getRoomById(roomId)!; + // put an important state event in-memory + await matrix.handleSync(SyncUpdate.fromJson({ + 'next_batch': 'fakesync', + 'rooms': { + 'join': { + roomId: { + 'state': { + 'events': [ + { + 'sender': '@alice:example.com', + 'type': 'm.room.name', + 'content': {'name': 'foxies'}, + 'state_key': '', + 'origin_server_ts': 1417731086799, + 'event_id': '66697273743033:example.com' + } + ] + } + } + } + } + })); + expect(room.getState('m.room.name')?.content['name'], 'foxies'); + + // drop an unimportant state event from in-memory handling + await matrix.handleSync(SyncUpdate.fromJson({ + 'next_batch': 'fakesync', + 'rooms': { + 'join': { + roomId: { + 'state': { + 'events': [ + { + 'sender': '@alice:example.com', + 'type': 'com.famedly.custom', + 'content': {'name': 'foxies'}, + 'state_key': '', + 'origin_server_ts': 1417731086799, + 'event_id': '66697273743033:example.com' + } + ] + } + } + } + } + })); + expect(room.getState('com.famedly.custom'), null); + + // persist normal room messages + await matrix.handleSync(SyncUpdate.fromJson({ + 'next_batch': 'fakesync', + 'rooms': { + 'join': { + roomId: { + 'timeline': { + 'events': [ + { + 'sender': '@alice:example.com', + 'type': 'm.room.message', + 'content': { + 'msgtype': 'm.text', + 'body': 'meow' + }, + 'origin_server_ts': 1417731086799, + 'event_id': '\$last:example.com' + } + ] + } + } + } + } + })); + expect(room.getState('m.room.message')!.content['body'], 'meow'); + + // ignore edits + await matrix.handleSync(SyncUpdate.fromJson({ + 'next_batch': 'fakesync', + 'rooms': { + 'join': { + roomId: { + 'timeline': { + 'events': [ + { + 'sender': '@alice:example.com', + 'type': 'm.room.message', + 'content': { + 'msgtype': 'm.text', + 'body': '* floooof', + 'm.new_content': { + 'msgtype': 'm.text', + 'body': 'floooof', + }, + 'm.relates_to': { + 'rel_type': 'm.replace', + 'event_id': '\$other:example.com' + }, + }, + 'origin_server_ts': 1417731086799, + 'event_id': '\$edit:example.com' + } + ] + } + } + } + } + })); + expect(room.getState('m.room.message')!.content['body'], 'meow'); + + // accept edits to the last event + await matrix.handleSync(SyncUpdate.fromJson({ + 'next_batch': 'fakesync', + 'rooms': { + 'join': { + roomId: { + 'timeline': { + 'events': [ + { + 'sender': '@alice:example.com', + 'type': 'm.room.message', + 'content': { + 'msgtype': 'm.text', + 'body': '* floooof', + 'm.new_content': { + 'msgtype': 'm.text', + 'body': 'floooof', + }, + 'm.relates_to': { + 'rel_type': 'm.replace', + 'event_id': '\$last:example.com' + }, + }, + 'origin_server_ts': 1417731086799, + 'event_id': '\$edit:example.com' + } + ] + } + } + } + } + })); + expect(room.getState('m.room.message')!.content['body'], '* floooof'); + + // accepts a consecutive edit + await matrix.handleSync(SyncUpdate.fromJson({ + 'next_batch': 'fakesync', + 'rooms': { + 'join': { + roomId: { + 'timeline': { + 'events': [ + { + 'sender': '@alice:example.com', + 'type': 'm.room.message', + 'content': { + 'msgtype': 'm.text', + 'body': '* foxies', + 'm.new_content': { + 'msgtype': 'm.text', + 'body': 'foxies', + }, + 'm.relates_to': { + 'rel_type': 'm.replace', + 'event_id': '\$last:example.com' + }, + }, + 'origin_server_ts': 1417731086799, + 'event_id': '\$edit2:example.com' + } + ] + } + } + } + } + })); + expect(room.getState('m.room.message')!.content['body'], '* foxies'); + }); + + test('getProfileFromUserId', () async { + final profile = await matrix.getProfileFromUserId('@getme:example.com', + getFromRooms: false); + expect(profile.avatarUrl.toString(), 'mxc://test'); + expect(profile.displayName, 'You got me'); + final aliceProfile = + await matrix.getProfileFromUserId('@alice:example.com'); + expect(aliceProfile.avatarUrl.toString(), + 'mxc://example.org/SEsfnsuifSDFSSEF'); + expect(aliceProfile.displayName, 'Alice Margatroid'); + }); + test('ownProfile', () async { + final client = await getClient(); + await client.abortSync(); + client.rooms.clear(); + await client.database?.clearCache(); + await client.handleSync(SyncUpdate.fromJson(jsonDecode( + '{\"next_batch\":\"s82_571_2_6_39_1_2_34_1\",\"account_data\":{\"events\":[{\"type\":\"m.push_rules\",\"content\":{\"global\":{\"underride\":[{\"conditions\":[{\"kind\":\"event_match\",\"key\":\"type\",\"pattern\":\"m.call.invite\"}],\"actions\":[\"notify\",{\"set_tweak\":\"sound\",\"value\":\"ring\"},{\"set_tweak\":\"highlight\",\"value\":false}],\"rule_id\":\".m.rule.call\",\"default\":true,\"enabled\":true},{\"conditions\":[{\"kind\":\"room_member_count\",\"is\":\"2\"},{\"kind\":\"event_match\",\"key\":\"type\",\"pattern\":\"m.room.message\"}],\"actions\":[\"notify\",{\"set_tweak\":\"sound\",\"value\":\"default\"},{\"set_tweak\":\"highlight\",\"value\":false}],\"rule_id\":\".m.rule.room_one_to_one\",\"default\":true,\"enabled\":true},{\"conditions\":[{\"kind\":\"room_member_count\",\"is\":\"2\"},{\"kind\":\"event_match\",\"key\":\"type\",\"pattern\":\"m.room.encrypted\"}],\"actions\":[\"notify\",{\"set_tweak\":\"sound\",\"value\":\"default\"},{\"set_tweak\":\"highlight\",\"value\":false}],\"rule_id\":\".m.rule.encrypted_room_one_to_one\",\"default\":true,\"enabled\":true},{\"conditions\":[{\"kind\":\"event_match\",\"key\":\"type\",\"pattern\":\"m.room.message\"}],\"actions\":[\"notify\",{\"set_tweak\":\"highlight\",\"value\":false}],\"rule_id\":\".m.rule.message\",\"default\":true,\"enabled\":true},{\"conditions\":[{\"kind\":\"event_match\",\"key\":\"type\",\"pattern\":\"m.room.encrypted\"}],\"actions\":[\"notify\",{\"set_tweak\":\"highlight\",\"value\":false}],\"rule_id\":\".m.rule.encrypted\",\"default\":true,\"enabled\":true},{\"conditions\":[{\"kind\":\"event_match\",\"key\":\"type\",\"pattern\":\"im.vector.modular.widgets\"},{\"kind\":\"event_match\",\"key\":\"content.type\",\"pattern\":\"jitsi\"},{\"kind\":\"event_match\",\"key\":\"state_key\",\"pattern\":\"*\"}],\"actions\":[\"notify\",{\"set_tweak\":\"highlight\",\"value\":false}],\"rule_id\":\".im.vector.jitsi\",\"default\":true,\"enabled\":true}],\"sender\":[],\"room\":[],\"content\":[{\"actions\":[\"notify\",{\"set_tweak\":\"sound\",\"value\":\"default\"},{\"set_tweak\":\"highlight\"}],\"pattern\":\"056d6976-fb61-47cf-86f0-147387461565\",\"rule_id\":\".m.rule.contains_user_name\",\"default\":true,\"enabled\":true}],\"override\":[{\"conditions\":[],\"actions\":[\"dont_notify\"],\"rule_id\":\".m.rule.master\",\"default\":true,\"enabled\":false},{\"conditions\":[{\"kind\":\"event_match\",\"key\":\"content.msgtype\",\"pattern\":\"m.notice\"}],\"actions\":[\"dont_notify\"],\"rule_id\":\".m.rule.suppress_notices\",\"default\":true,\"enabled\":true},{\"conditions\":[{\"kind\":\"event_match\",\"key\":\"type\",\"pattern\":\"m.room.member\"},{\"kind\":\"event_match\",\"key\":\"content.membership\",\"pattern\":\"invite\"},{\"kind\":\"event_match\",\"key\":\"state_key\",\"pattern\":\"@056d6976-fb61-47cf-86f0-147387461565:c3d35860-36fe-45d1-8e16-936cf50513fb.gedisa-staging.famedly.de\"}],\"actions\":[\"notify\",{\"set_tweak\":\"sound\",\"value\":\"default\"},{\"set_tweak\":\"highlight\",\"value\":false}],\"rule_id\":\".m.rule.invite_for_me\",\"default\":true,\"enabled\":true},{\"conditions\":[{\"kind\":\"event_match\",\"key\":\"type\",\"pattern\":\"m.room.member\"}],\"actions\":[\"dont_notify\"],\"rule_id\":\".m.rule.member_event\",\"default\":true,\"enabled\":true},{\"conditions\":[{\"kind\":\"contains_display_name\"}],\"actions\":[\"notify\",{\"set_tweak\":\"sound\",\"value\":\"default\"},{\"set_tweak\":\"highlight\"}],\"rule_id\":\".m.rule.contains_display_name\",\"default\":true,\"enabled\":true},{\"conditions\":[{\"kind\":\"event_match\",\"key\":\"content.body\",\"pattern\":\"@room\"},{\"kind\":\"sender_notification_permission\",\"key\":\"room\"}],\"actions\":[\"notify\",{\"set_tweak\":\"highlight\",\"value\":true}],\"rule_id\":\".m.rule.roomnotif\",\"default\":true,\"enabled\":true},{\"conditions\":[{\"kind\":\"event_match\",\"key\":\"type\",\"pattern\":\"m.room.tombstone\"},{\"kind\":\"event_match\",\"key\":\"state_key\",\"pattern\":\"\"}],\"actions\":[\"notify\",{\"set_tweak\":\"highlight\",\"value\":true}],\"rule_id\":\".m.rule.tombstone\",\"default\":true,\"enabled\":true},{\"conditions\":[{\"kind\":\"event_match\",\"key\":\"type\",\"pattern\":\"m.reaction\"}],\"actions\":[\"dont_notify\"],\"rule_id\":\".m.rule.reaction\",\"default\":true,\"enabled\":true}]},\"device\":{}}}]},\"presence\":{\"events\":[{\"type\":\"m.presence\",\"sender\":\"@056d6976-fb61-47cf-86f0-147387461565:c3d35860-36fe-45d1-8e16-936cf50513fb.gedisa-staging.famedly.de\",\"content\":{\"presence\":\"online\",\"last_active_ago\":43,\"currently_active\":true}}]},\"device_one_time_keys_count\":{\"signed_curve25519\":66},\"org.matrix.msc2732.device_unused_fallback_key_types\":[\"signed_curve25519\"],\"device_unused_fallback_key_types\":[\"signed_curve25519\"],\"rooms\":{\"join\":{\"!MEgZosbiZqjSjbHFqI:c3d35860-36fe-45d1-8e16-936cf50513fb.gedisa-staging.famedly.de\":{\"timeline\":{\"events\":[{\"type\":\"m.room.member\",\"sender\":\"@8640f1e6-a824-4f9c-9924-2d8fc40bc030:c3d35860-36fe-45d1-8e16-936cf50513fb.gedisa-staging.famedly.de\",\"content\":{\"membership\":\"join\",\"displayname\":\"Lars Kaiser\"},\"state_key\":\"@8640f1e6-a824-4f9c-9924-2d8fc40bc030:c3d35860-36fe-45d1-8e16-936cf50513fb.gedisa-staging.famedly.de\",\"origin_server_ts\":1647296944593,\"unsigned\":{\"age\":545455},\"event_id\":\"\$mk9kFUEAKBZJgarWApLyYqOZQQocLIVV8tWp_gJEZFU\"},{\"type\":\"m.room.power_levels\",\"sender\":\"@8640f1e6-a824-4f9c-9924-2d8fc40bc030:c3d35860-36fe-45d1-8e16-936cf50513fb.gedisa-staging.famedly.de\",\"content\":{\"users\":{\"@8640f1e6-a824-4f9c-9924-2d8fc40bc030:c3d35860-36fe-45d1-8e16-936cf50513fb.gedisa-staging.famedly.de\":100},\"users_default\":0,\"events\":{\"m.room.name\":50,\"m.room.power_levels\":100,\"m.room.history_visibility\":100,\"m.room.canonical_alias\":50,\"m.room.avatar\":50,\"m.room.tombstone\":100,\"m.room.server_acl\":100,\"m.room.encryption\":100},\"events_default\":0,\"state_default\":50,\"ban\":50,\"kick\":50,\"redact\":50,\"invite\":50,\"historical\":100},\"state_key\":\"\",\"origin_server_ts\":1647296944690,\"unsigned\":{\"age\":545358},\"event_id\":\"\$3wL2YgVNQzgfl8y_ksi3BPMqRs94jb_m0WRonL1HNpY\"},{\"type\":\"m.room.canonical_alias\",\"sender\":\"@8640f1e6-a824-4f9c-9924-2d8fc40bc030:c3d35860-36fe-45d1-8e16-936cf50513fb.gedisa-staging.famedly.de\",\"content\":{\"alias\":\"#user-discovery:c3d35860-36fe-45d1-8e16-936cf50513fb.gedisa-staging.famedly.de\"},\"state_key\":\"\",\"origin_server_ts\":1647296944806,\"unsigned\":{\"age\":545242},\"event_id\":\"\$yXaVETL9F4jSN9rpRNyT_kUoctzD07n5Z4AIHziP7DQ\"},{\"type\":\"m.room.join_rules\",\"sender\":\"@8640f1e6-a824-4f9c-9924-2d8fc40bc030:c3d35860-36fe-45d1-8e16-936cf50513fb.gedisa-staging.famedly.de\",\"content\":{\"join_rule\":\"public\"},\"state_key\":\"\",\"origin_server_ts\":1647296944894,\"unsigned\":{\"age\":545154},\"event_id\":\"\$jBDHhgpNqr125eWUsGVw4r7ZG2hgr0BTzzR77S-ubvY\"},{\"type\":\"m.room.history_visibility\",\"sender\":\"@8640f1e6-a824-4f9c-9924-2d8fc40bc030:c3d35860-36fe-45d1-8e16-936cf50513fb.gedisa-staging.famedly.de\",\"content\":{\"history_visibility\":\"shared\"},\"state_key\":\"\",\"origin_server_ts\":1647296944965,\"unsigned\":{\"age\":545083},\"event_id\":\"\$kMessP7gAphUKW7mzOLlJT6NT8IsVGPmGir3_1uBNCE\"},{\"type\":\"m.room.name\",\"sender\":\"@8640f1e6-a824-4f9c-9924-2d8fc40bc030:c3d35860-36fe-45d1-8e16-936cf50513fb.gedisa-staging.famedly.de\",\"content\":{\"name\":\"User Discovery\"},\"state_key\":\"\",\"origin_server_ts\":1647296945062,\"unsigned\":{\"age\":544986},\"event_id\":\"\$Bo9Ut_0vcr3FuxCRye4IHEMxUxIIcSwc-ePnMzx-hYU\"},{\"type\":\"m.room.member\",\"sender\":\"@test:fakeServer.notExisting\",\"content\":{\"membership\":\"join\",\"displayname\":\"1c2e5c2b-f958-45a5-9fcb-eef3969c31df\"},\"state_key\":\"@test:fakeServer.notExisting\",\"origin_server_ts\":1647296989893,\"unsigned\":{\"age\":500155},\"event_id\":\"\$fYCf2qtlHwzcdLgwjHb2EOdStv3isAlIUy2Esh5qfVE\"},{\"type\":\"m.room.member\",\"sender\":\"@test:fakeServer.notExisting\",\"content\":{\"membership\":\"join\",\"displayname\":\"Some First Name Some Last Name\"},\"state_key\":\"@test:fakeServer.notExisting\",\"origin_server_ts\":1647296990076,\"unsigned\":{\"replaces_state\":\"\$fYCf2qtlHwzcdLgwjHb2EOdStv3isAlIUy2Esh5qfVE\",\"prev_content\":{\"membership\":\"join\",\"displayname\":\"1c2e5c2b-f958-45a5-9fcb-eef3969c31df\"},\"prev_sender\":\"@test:fakeServer.notExisting\",\"age\":499972},\"event_id\":\"\$3Ut97nFBgOtsrnRPW-pqr28z7ETNMttj7GcjkIv4zWw\"},{\"type\":\"m.room.member\",\"sender\":\"@056d6976-fb61-47cf-86f0-147387461565:c3d35860-36fe-45d1-8e16-936cf50513fb.gedisa-staging.famedly.de\",\"content\":{\"membership\":\"join\",\"displayname\":\"056d6976-fb61-47cf-86f0-147387461565\"},\"state_key\":\"@056d6976-fb61-47cf-86f0-147387461565:c3d35860-36fe-45d1-8e16-936cf50513fb.gedisa-staging.famedly.de\",\"origin_server_ts\":1647297489154,\"unsigned\":{\"age\":894},\"event_id\":\"\$6EsjHSLQDVDW9WDH1c5Eu57VaPGZmOPtNRjCjtWPLV0\"},{\"type\":\"m.room.member\",\"sender\":\"@056d6976-fb61-47cf-86f0-147387461565:c3d35860-36fe-45d1-8e16-936cf50513fb.gedisa-staging.famedly.de\",\"content\":{\"membership\":\"join\",\"displayname\":\"Another User\"},\"state_key\":\"@056d6976-fb61-47cf-86f0-147387461565:c3d35860-36fe-45d1-8e16-936cf50513fb.gedisa-staging.famedly.de\",\"origin_server_ts\":1647297489290,\"unsigned\":{\"replaces_state\":\"\$6EsjHSLQDVDW9WDH1c5Eu57VaPGZmOPtNRjCjtWPLV0\",\"prev_content\":{\"membership\":\"join\",\"displayname\":\"056d6976-fb61-47cf-86f0-147387461565\"},\"prev_sender\":\"@056d6976-fb61-47cf-86f0-147387461565:c3d35860-36fe-45d1-8e16-936cf50513fb.gedisa-staging.famedly.de\",\"age\":758},\"event_id\":\"\$dtQblqCbjr3TGc3WmrQ4YTkHaXJ2PcO0TAYDr9K7iQc\"}],\"prev_batch\":\"t2-62_571_2_6_39_1_2_34_1\",\"limited\":true},\"state\":{\"events\":[{\"type\":\"m.room.create\",\"sender\":\"@8640f1e6-a824-4f9c-9924-2d8fc40bc030:c3d35860-36fe-45d1-8e16-936cf50513fb.gedisa-staging.famedly.de\",\"content\":{\"m.federate\":false,\"room_version\":\"9\",\"creator\":\"@8640f1e6-a824-4f9c-9924-2d8fc40bc030:c3d35860-36fe-45d1-8e16-936cf50513fb.gedisa-staging.famedly.de\"},\"state_key\":\"\",\"origin_server_ts\":1647296944511,\"unsigned\":{\"age\":545537},\"event_id\":\"\$PAWKKULBVOLnqfrAAtXZz8tHEPXXjgRVbJJLifwQWbE\"}]},\"account_data\":{\"events\":[]},\"ephemeral\":{\"events\":[]},\"unread_notifications\":{\"notification_count\":0,\"highlight_count\":0},\"summary\":{\"m.joined_member_count\":3,\"m.invited_member_count\":0},\"org.matrix.msc2654.unread_count\":0}}}}'))); + final profile = await client.ownProfile; + expect(profile.displayName, 'Some First Name Some Last Name'); + }); + test('sendToDeviceEncrypted', () async { + if (!olmEnabled) { + return; + } + FakeMatrixApi.calledEndpoints.clear(); + await matrix.sendToDeviceEncrypted( + matrix.userDeviceKeys['@alice:example.com']!.deviceKeys.values + .toList(), + 'm.message', + { + 'msgtype': 'm.text', + 'body': 'Hello world', + }); + expect( + FakeMatrixApi.calledEndpoints.keys.any( + (k) => k.startsWith('/client/r0/sendToDevice/m.room.encrypted')), + true); + }); + test('sendToDeviceEncryptedChunked', () async { + if (!olmEnabled) { + return; + } + FakeMatrixApi.calledEndpoints.clear(); + await matrix.sendToDeviceEncryptedChunked( + matrix.userDeviceKeys['@alice:example.com']!.deviceKeys.values + .toList(), + 'm.message', + { + 'msgtype': 'm.text', + 'body': 'Hello world', + }); + await Future.delayed(Duration(milliseconds: 100)); + expect( + FakeMatrixApi.calledEndpoints.keys + .where((k) => + k.startsWith('/client/r0/sendToDevice/m.room.encrypted')) + .length, + 1); + + final deviceKeys = []; + for (var i = 0; i < 30; i++) { + final account = olm.Account(); + account.create(); + final keys = json.decode(account.identity_keys()); + final userId = '@testuser:example.org'; + final deviceId = 'DEVICE$i'; + final keyObj = { + 'user_id': userId, + 'device_id': deviceId, + 'algorithms': [ + 'm.olm.v1.curve25519-aes-sha2', + 'm.megolm.v1.aes-sha2', + ], + 'keys': { + 'curve25519:$deviceId': keys['curve25519'], + 'ed25519:$deviceId': keys['ed25519'], + }, + }; + final signature = + account.sign(String.fromCharCodes(canonicalJson.encode(keyObj))); + keyObj['signatures'] = { + userId: { + 'ed25519:$deviceId': signature, + }, + }; + account.free(); + deviceKeys.add(DeviceKeys.fromJson(keyObj, matrix)); + } + FakeMatrixApi.calledEndpoints.clear(); + await matrix.sendToDeviceEncryptedChunked(deviceKeys, 'm.message', { + 'msgtype': 'm.text', + 'body': 'Hello world', + }); + // it should send the first chunk right away + expect( + FakeMatrixApi.calledEndpoints.keys + .where((k) => + k.startsWith('/client/r0/sendToDevice/m.room.encrypted')) + .length, + 1); + await Future.delayed(Duration(milliseconds: 100)); + expect( + FakeMatrixApi.calledEndpoints.keys + .where((k) => + k.startsWith('/client/r0/sendToDevice/m.room.encrypted')) + .length, + 2); + }); + test('send to_device queue', () async { + // we test: + // send fox --> fail + // send raccoon --> fox & raccoon sent + // send bunny --> only bunny sent + final client = await getClient(); + FakeMatrixApi.failToDevice = true; + final foxContent = { + '@fox:example.org': { + '*': { + 'fox': 'hole', + }, + }, + }; + final raccoonContent = { + '@fox:example.org': { + '*': { + 'raccoon': 'mask', + }, + }, + }; + final bunnyContent = { + '@fox:example.org': { + '*': { + 'bunny': 'burrow', + }, + }, + }; + await client + .sendToDevice('foxies', 'floof_txnid', foxContent) + .catchError((e) => null); // ignore the error + FakeMatrixApi.failToDevice = false; + FakeMatrixApi.calledEndpoints.clear(); + await client.sendToDevice('raccoon', 'raccoon_txnid', raccoonContent); + expect( + json.decode(FakeMatrixApi + .calledEndpoints['/client/r0/sendToDevice/foxies/floof_txnid'] + ?[0])['messages'], + foxContent); + expect( + json.decode(FakeMatrixApi.calledEndpoints[ + '/client/r0/sendToDevice/raccoon/raccoon_txnid']?[0])['messages'], + raccoonContent); + FakeMatrixApi.calledEndpoints.clear(); + await client.sendToDevice('bunny', 'bunny_txnid', bunnyContent); + expect( + FakeMatrixApi + .calledEndpoints['/client/r0/sendToDevice/foxies/floof_txnid'], + null); + expect( + FakeMatrixApi + .calledEndpoints['/client/r0/sendToDevice/raccoon/raccoon_txnid'], + null); + expect( + json.decode(FakeMatrixApi + .calledEndpoints['/client/r0/sendToDevice/bunny/bunny_txnid'] + ?[0])['messages'], + bunnyContent); + await client.dispose(closeDatabase: true); + }); + test('send to_device queue multiple', () async { + // we test: + // send fox --> fail + // send raccoon --> fail + // send bunny --> all sent + final client = await getClient(); + FakeMatrixApi.failToDevice = true; + final foxContent = { + '@fox:example.org': { + '*': { + 'fox': 'hole', + }, + }, + }; + final raccoonContent = { + '@fox:example.org': { + '*': { + 'raccoon': 'mask', + }, + }, + }; + final bunnyContent = { + '@fox:example.org': { + '*': { + 'bunny': 'burrow', + }, + }, + }; + await client + .sendToDevice('foxies', 'floof_txnid', foxContent) + .catchError((e) => null); // ignore the error + await client + .sendToDevice('raccoon', 'raccoon_txnid', raccoonContent) + .catchError((e) => null); + FakeMatrixApi.failToDevice = false; + FakeMatrixApi.calledEndpoints.clear(); + await client.sendToDevice('bunny', 'bunny_txnid', bunnyContent); + expect( + json.decode(FakeMatrixApi + .calledEndpoints['/client/r0/sendToDevice/foxies/floof_txnid'] + ?[0])['messages'], + foxContent); + expect( + json.decode(FakeMatrixApi.calledEndpoints[ + '/client/r0/sendToDevice/raccoon/raccoon_txnid']?[0])['messages'], + raccoonContent); + expect( + json.decode(FakeMatrixApi + .calledEndpoints['/client/r0/sendToDevice/bunny/bunny_txnid'] + ?[0])['messages'], + bunnyContent); + await client.dispose(closeDatabase: true); + }); + test('startDirectChat', () async { + await matrix.startDirectChat('@alice:example.com', waitForSync: false); + }); + test('createGroupChat', () async { + await matrix.createGroupChat(groupName: 'Testgroup', waitForSync: false); + }); + test('Test the fake store api', () async { + final database = await getDatabase(null); + final client1 = Client( + 'testclient', + httpClient: FakeMatrixApi(), + databaseBuilder: (_) => database, + ); + + await client1.init( + newToken: 'abc123', + newUserID: '@test:fakeServer.notExisting', + newHomeserver: Uri.parse('https://fakeServer.notExisting'), + newDeviceName: 'Text Matrix Client', + newDeviceID: 'GHTYAJCE', + newOlmAccount: pickledOlmAccount, + ); + + await Future.delayed(Duration(milliseconds: 500)); + + expect(client1.isLogged(), true); + expect(client1.rooms.length, 2); + + final client2 = Client( + 'testclient', + httpClient: FakeMatrixApi(), + databaseBuilder: (_) => database, + ); + + await client2.init(); + await Future.delayed(Duration(milliseconds: 500)); + + expect(client2.isLogged(), true); + expect(client2.accessToken, client1.accessToken); + expect(client2.userID, client1.userID); + expect(client2.homeserver, client1.homeserver); + expect(client2.deviceID, client1.deviceID); + expect(client2.deviceName, client1.deviceName); + expect(client2.rooms.length, 2); + if (client2.encryptionEnabled) { + expect(client2.encryption?.fingerprintKey, + client1.encryption?.fingerprintKey); + expect( + client2.encryption?.identityKey, client1.encryption?.identityKey); + expect(client2.rooms[1].id, client1.rooms[1].id); + } + + await client1.logout(); + await client2.logout(); + }); + test('changePassword', () async { + await matrix.changePassword('1234', oldPassword: '123456'); + }); + test('ignoredUsers', () async { + expect(matrix.ignoredUsers, []); + matrix.accountData['m.ignored_user_list'] = + BasicEvent(type: 'm.ignored_user_list', content: { + 'ignored_users': { + '@charley:stupid.abc': {}, + }, + }); + expect(matrix.ignoredUsers, ['@charley:stupid.abc']); + }); + test('ignoredUsers', () async { + await matrix.ignoreUser('@charley2:stupid.abc'); + await matrix.unignoreUser('@charley:stupid.abc'); + }); + test('upload', () async { + final client = await getClient(); + final response = + await client.uploadContent(Uint8List(0), filename: 'file.jpeg'); + expect(response.toString(), 'mxc://example.com/AQwafuaFswefuhsfAFAgsw'); + expect(await client.database?.getFile(response) != null, + client.database?.supportsFileStoring); + await client.dispose(closeDatabase: true); + }); + + test('object equality', () async { + final time1 = DateTime.fromMillisecondsSinceEpoch(1); + final time2 = DateTime.fromMillisecondsSinceEpoch(0); + final user1 = + User('@user1:example.org', room: Room(id: '!room1', client: matrix)); + final user2 = + User('@user2:example.org', room: Room(id: '!room1', client: matrix)); + // receipts + expect(Receipt(user1, time1) == Receipt(user1, time1), true); + expect(Receipt(user1, time1) == Receipt(user1, time2), false); + expect(Receipt(user1, time1) == Receipt(user2, time1), false); + // ignore: unrelated_type_equality_checks + expect(Receipt(user1, time1) == 'beep', false); + // users + expect(user1 == user1, true); + expect(user1 == user2, false); + expect( + user1 == + User('@user1:example.org', + room: Room(id: '!room2', client: matrix)), + false); + expect( + user1 == + User('@user1:example.org', + room: Room(id: '!room1', client: matrix), + membership: 'leave'), + false); + // ignore: unrelated_type_equality_checks + expect(user1 == 'beep', false); + // rooms + expect( + Room(id: '!room1', client: matrix) == + Room(id: '!room1', client: matrix), + true); + expect( + Room(id: '!room1', client: matrix) == + Room(id: '!room2', client: matrix), + false); + // ignore: unrelated_type_equality_checks + expect(Room(id: '!room1', client: matrix) == 'beep', false); + }); + + test('clearCache', () async { + final client = await getClient(); + client.backgroundSync = true; + await client.clearCache(); + }); + + test('dispose', () async { + await matrix.dispose(closeDatabase: true); + }); + + test('Database Migration', () async { + final database = await getDatabase(null); + final moorClient = Client( + 'testclient', + httpClient: FakeMatrixApi(), + databaseBuilder: (_) => database, + ); + FakeMatrixApi.client = moorClient; + await moorClient.checkHomeserver('https://fakeServer.notExisting', + checkWellKnown: false); + await moorClient.init( + newToken: 'abcd', + newUserID: '@test:fakeServer.notExisting', + newHomeserver: moorClient.homeserver, + newDeviceName: 'Text Matrix Client', + newDeviceID: 'GHTYAJCE', + newOlmAccount: pickledOlmAccount, + ); + await Future.delayed(Duration(milliseconds: 200)); + await moorClient.dispose(closeDatabase: false); + + final hiveClient = Client( + 'testclient', + httpClient: FakeMatrixApi(), + databaseBuilder: getDatabase, + legacyDatabaseBuilder: (_) => database, + ); + await hiveClient.init(); + await Future.delayed(Duration(milliseconds: 200)); + expect(hiveClient.isLogged(), true); + }); + }); +} diff --git a/test/commands_test.dart b/test/commands_test.dart new file mode 100644 index 0000000..30ae47f --- /dev/null +++ b/test/commands_test.dart @@ -0,0 +1,323 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:convert'; + +import 'package:test/test.dart'; +import 'package:olm/olm.dart' as olm; +import 'package:matrix/matrix.dart'; +import 'fake_client.dart'; +import 'fake_matrix_api.dart'; + +void main() { + group('Commands', () { + late Client client; + late Room room; + var olmEnabled = true; + + final getLastMessagePayload = + ([String type = 'm.room.message', String? stateKey]) { + final state = stateKey != null; + return json.decode(FakeMatrixApi.calledEndpoints.entries + .firstWhere((e) => e.key.startsWith( + '/client/r0/rooms/${Uri.encodeComponent(room.id)}/${state ? 'state' : 'send'}/${Uri.encodeComponent(type)}${state && stateKey?.isNotEmpty == true ? '/' + Uri.encodeComponent(stateKey!) : ''}')) + .value + .first); + }; + + test('setupClient', () async { + try { + await olm.init(); + olm.get_library_version(); + } catch (e) { + olmEnabled = false; + } + client = await getClient(); + room = Room(id: '!1234:fakeServer.notExisting', client: client); + room.setState(Event( + type: 'm.room.power_levels', + content: {}, + room: room, + stateKey: '', + eventId: '\$fakeeventid', + originServerTs: DateTime.now(), + senderId: '\@fakeuser:fakeServer.notExisting', + )); + room.setState(Event( + type: 'm.room.member', + content: {'membership': 'join'}, + room: room, + stateKey: client.userID, + eventId: '\$fakeeventid', + originServerTs: DateTime.now(), + senderId: '\@fakeuser:fakeServer.notExisting', + )); + }); + + test('send', () async { + FakeMatrixApi.calledEndpoints.clear(); + await room.sendTextEvent('/send Hello World'); + var sent = getLastMessagePayload(); + expect(sent, { + 'msgtype': 'm.text', + 'body': 'Hello World', + }); + + FakeMatrixApi.calledEndpoints.clear(); + await room.sendTextEvent('Beep Boop'); + sent = getLastMessagePayload(); + expect(sent, { + 'msgtype': 'm.text', + 'body': 'Beep Boop', + }); + + FakeMatrixApi.calledEndpoints.clear(); + await room.sendTextEvent('Beep *Boop*'); + sent = getLastMessagePayload(); + expect(sent, { + 'msgtype': 'm.text', + 'body': 'Beep *Boop*', + 'format': 'org.matrix.custom.html', + 'formatted_body': 'Beep Boop', + }); + + FakeMatrixApi.calledEndpoints.clear(); + await room.sendTextEvent('//send Hello World'); + sent = getLastMessagePayload(); + expect(sent, { + 'msgtype': 'm.text', + 'body': '/send Hello World', + }); + }); + + test('me', () async { + FakeMatrixApi.calledEndpoints.clear(); + await room.sendTextEvent('/me heya'); + final sent = getLastMessagePayload(); + expect(sent, { + 'msgtype': 'm.emote', + 'body': 'heya', + }); + }); + + test('plain', () async { + FakeMatrixApi.calledEndpoints.clear(); + await room.sendTextEvent('/plain *floof*'); + final sent = getLastMessagePayload(); + expect(sent, { + 'msgtype': 'm.text', + 'body': '*floof*', + }); + }); + + test('html', () async { + FakeMatrixApi.calledEndpoints.clear(); + await room.sendTextEvent('/html yay'); + final sent = getLastMessagePayload(); + expect(sent, { + 'msgtype': 'm.text', + 'body': 'yay', + 'format': 'org.matrix.custom.html', + 'formatted_body': 'yay', + }); + }); + + test('react', () async { + FakeMatrixApi.calledEndpoints.clear(); + await room.sendTextEvent('/react 🦊', + inReplyTo: Event( + eventId: '\$event', + type: 'm.room.message', + content: { + 'msgtype': 'm.text', + 'body': 'yay', + 'format': 'org.matrix.custom.html', + 'formatted_body': 'yay', + }, + originServerTs: DateTime.now(), + senderId: client.userID!, + room: room, + )); + final sent = getLastMessagePayload('m.reaction'); + expect(sent, { + 'm.relates_to': { + 'rel_type': 'm.annotation', + 'event_id': '\$event', + 'key': '🦊', + }, + }); + }); + + test('join', () async { + FakeMatrixApi.calledEndpoints.clear(); + await room.sendTextEvent('/join !newroom:example.com'); + expect( + FakeMatrixApi + .calledEndpoints['/client/r0/join/!newroom%3Aexample.com'] + ?.first != + null, + true); + }); + + test('leave', () async { + FakeMatrixApi.calledEndpoints.clear(); + await room.sendTextEvent('/leave'); + expect( + FakeMatrixApi + .calledEndpoints[ + '/client/r0/rooms/!1234%3AfakeServer.notExisting/leave'] + ?.first != + null, + true); + }); + + test('op', () async { + FakeMatrixApi.calledEndpoints.clear(); + await room.sendTextEvent('/op @user:example.org'); + var sent = getLastMessagePayload('m.room.power_levels', ''); + expect(sent, { + 'users': {'@user:example.org': 50} + }); + + FakeMatrixApi.calledEndpoints.clear(); + await room.sendTextEvent('/op @user:example.org 100'); + sent = getLastMessagePayload('m.room.power_levels', ''); + expect(sent, { + 'users': {'@user:example.org': 100} + }); + }); + + test('kick', () async { + FakeMatrixApi.calledEndpoints.clear(); + await room.sendTextEvent('/kick @baduser:example.org'); + expect( + json.decode(FakeMatrixApi + .calledEndpoints[ + '/client/r0/rooms/!1234%3AfakeServer.notExisting/kick'] + ?.first), + { + 'user_id': '@baduser:example.org', + }); + }); + + test('ban', () async { + FakeMatrixApi.calledEndpoints.clear(); + await room.sendTextEvent('/ban @baduser:example.org'); + expect( + json.decode(FakeMatrixApi + .calledEndpoints[ + '/client/r0/rooms/!1234%3AfakeServer.notExisting/ban'] + ?.first), + { + 'user_id': '@baduser:example.org', + }); + }); + + test('unban', () async { + FakeMatrixApi.calledEndpoints.clear(); + await room.sendTextEvent('/unban @baduser:example.org'); + expect( + json.decode(FakeMatrixApi + .calledEndpoints[ + '/client/r0/rooms/!1234%3AfakeServer.notExisting/unban'] + ?.first), + { + 'user_id': '@baduser:example.org', + }); + }); + + test('invite', () async { + FakeMatrixApi.calledEndpoints.clear(); + await room.sendTextEvent('/invite @baduser:example.org'); + expect( + json.decode(FakeMatrixApi + .calledEndpoints[ + '/client/r0/rooms/!1234%3AfakeServer.notExisting/invite'] + ?.first), + { + 'user_id': '@baduser:example.org', + }); + }); + + test('myroomnick', () async { + FakeMatrixApi.calledEndpoints.clear(); + await room.sendTextEvent('/myroomnick Foxies~'); + final sent = getLastMessagePayload('m.room.member', client.userID); + expect(sent, { + 'displayname': 'Foxies~', + 'membership': 'join', + }); + }); + + test('myroomavatar', () async { + FakeMatrixApi.calledEndpoints.clear(); + await room.sendTextEvent('/myroomavatar mxc://beep/boop'); + final sent = getLastMessagePayload('m.room.member', client.userID); + expect(sent, { + 'avatar_url': 'mxc://beep/boop', + 'membership': 'join', + }); + }); + + test('dm', () async { + FakeMatrixApi.calledEndpoints.clear(); + await room.sendTextEvent('/dm @alice:example.com --no-encryption'); + expect( + json.decode( + FakeMatrixApi.calledEndpoints['/client/r0/createRoom']?.first), + { + 'invite': ['@alice:example.com'], + 'is_direct': true, + 'preset': 'trusted_private_chat' + }); + }); + + test('create', () async { + FakeMatrixApi.calledEndpoints.clear(); + await room.sendTextEvent('/create @alice:example.com --no-encryption'); + expect( + json.decode( + FakeMatrixApi.calledEndpoints['/client/r0/createRoom']?.first), + {'preset': 'private_chat'}); + }); + + test('discardsession', () async { + if (olmEnabled) { + await client.encryption?.keyManager.createOutboundGroupSession(room.id); + expect( + client.encryption?.keyManager.getOutboundGroupSession(room.id) != + null, + true); + await room.sendTextEvent('/discardsession'); + expect( + client.encryption?.keyManager.getOutboundGroupSession(room.id) != + null, + false); + } + }); + + test('create', () async { + await room.sendTextEvent('/clearcache'); + expect(room.client.prevBatch, null); + }); + + test('dispose client', () async { + await client.dispose(closeDatabase: true); + }); + }); +} diff --git a/test/database_api_test.dart b/test/database_api_test.dart new file mode 100644 index 0000000..0d809a6 --- /dev/null +++ b/test/database_api_test.dart @@ -0,0 +1,485 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +import 'dart:convert'; +import 'dart:typed_data'; +import 'dart:async'; + +import 'package:matrix/matrix.dart'; +import 'package:test/test.dart'; +import 'package:olm/olm.dart' as olm; + +import 'fake_database.dart'; + +void main() { + group('FluffyBox Database Test', () { + testDatabase( + getFluffyBoxDatabase(null), + ); + }); + group('Hive Database Test', () { + testDatabase( + getHiveDatabase(null), + ); + }); +} + +Future olmEnabled() async { + var olmEnabled = true; + try { + await olm.init(); + olm.get_library_version(); + } catch (e) { + olmEnabled = false; + } + return olmEnabled; +} + +void testDatabase( + Future futureDatabase, +) { + late DatabaseApi database; + late int toDeviceQueueIndex; + test('Open', () async { + database = await futureDatabase; + }); + test('transaction', () async { + var counter = 0; + await database.transaction(() async { + expect(counter++, 0); + await database.transaction(() async { + expect(counter++, 1); + await Future.delayed(Duration(milliseconds: 50)); + expect(counter++, 2); + }); + expect(counter++, 3); + }); + + // we can't use Zone.root.run inside of tests so we abuse timers instead + Timer(Duration(milliseconds: 50), () async { + await database.transaction(() async { + expect(counter++, 6); + }); + }); + await database.transaction(() async { + expect(counter++, 4); + await Future.delayed(Duration(milliseconds: 100)); + expect(counter++, 5); + }); + }); + test('insertIntoToDeviceQueue', () async { + toDeviceQueueIndex = await database.insertIntoToDeviceQueue( + 'm.test', + 'txnId', + '{"foo":"bar"}', + ); + }); + test('getToDeviceEventQueue', () async { + final toDeviceQueue = await database.getToDeviceEventQueue(); + expect(toDeviceQueue.first.type, 'm.test'); + }); + test('deleteFromToDeviceQueue', () async { + await database.deleteFromToDeviceQueue(toDeviceQueueIndex); + final toDeviceQueue = await database.getToDeviceEventQueue(); + expect(toDeviceQueue.isEmpty, true); + }); + test('storeFile', () async { + await database.storeFile( + Uri.parse('mxc://test'), Uint8List.fromList([0]), 0); + final file = await database.getFile(Uri.parse('mxc://test')); + expect(file != null, database.supportsFileStoring); + }); + test('getFile', () async { + await database.getFile(Uri.parse('mxc://test')); + }); + test('deleteOldFiles', () async { + await database.deleteOldFiles(1); + final file = await database.getFile(Uri.parse('mxc://test')); + expect(file == null, true); + }); + test('storeRoomUpdate', () async { + final roomUpdate = JoinedRoomUpdate.fromJson({ + 'highlight_count': 0, + 'notification_count': 0, + 'limited_timeline': false, + 'membership': Membership.join, + }); + final client = Client('testclient'); + await database.storeRoomUpdate('!testroom', roomUpdate, client); + final rooms = await database.getRoomList(client); + expect(rooms.single.id, '!testroom'); + }); + test('getRoomList', () async { + final list = await database.getRoomList(Client('testclient')); + expect(list.single.id, '!testroom'); + }); + test('setRoomPrevBatch', () async { + final client = Client('testclient'); + await database.setRoomPrevBatch('1234', '!testroom', client); + final rooms = await database.getRoomList(client); + expect(rooms.single.prev_batch, '1234'); + }); + test('forgetRoom', () async { + await database.forgetRoom('!testroom'); + final rooms = await database.getRoomList(Client('testclient')); + expect(rooms.isEmpty, true); + }); + test('getClient', () async { + await database.getClient('name'); + }); + test('insertClient', () async { + await database.insertClient( + 'name', + 'homeserverUrl', + 'token', + 'userId', + 'deviceId', + 'deviceName', + 'prevBatch', + 'olmAccount', + ); + + final client = await database.getClient('name'); + expect(client?['token'], 'token'); + }); + test('updateClient', () async { + await database.updateClient( + 'homeserverUrl', + 'token_different', + 'userId', + 'deviceId', + 'deviceName', + 'prevBatch', + 'olmAccount', + ); + final client = await database.getClient('name'); + expect(client?['token'], 'token_different'); + }); + test('updateClientKeys', () async { + await database.updateClientKeys( + 'olmAccount2', + ); + final client = await database.getClient('name'); + expect(client?['olm_account'], 'olmAccount2'); + }); + test('storeSyncFilterId', () async { + await database.storeSyncFilterId( + '1234', + ); + final client = await database.getClient('name'); + expect(client?['sync_filter_id'], '1234'); + }); + test('getAccountData', () async { + await database.getAccountData(); + }); + test('storeAccountData', () async { + await database.storeAccountData('m.test', '{"foo":"bar"}'); + final events = await database.getAccountData(); + expect(events.values.single.type, 'm.test'); + + await database.storeAccountData('m.abc+de', '{"foo":"bar"}'); + final events2 = await database.getAccountData(); + expect(events2.values.any((element) => element.type == 'm.abc+de'), true); + }); + test('storeEventUpdate', () async { + await database.storeEventUpdate( + EventUpdate( + roomID: '!testroom:example.com', + type: EventUpdateType.timeline, + content: { + 'type': EventTypes.Message, + 'content': { + 'body': '* edit 3', + 'msgtype': 'm.text', + 'm.new_content': { + 'body': 'edit 3', + 'msgtype': 'm.text', + }, + 'm.relates_to': { + 'event_id': '\$source', + 'rel_type': RelationshipTypes.edit, + }, + }, + 'event_id': '\$event:example.com', + 'sender': '@bob:example.org', + }, + ), + Client('testclient')); + }); + test('getEventById', () async { + final event = await database.getEventById('\$event:example.com', + Room(id: '!testroom:example.com', client: Client('testclient'))); + expect(event?.type, EventTypes.Message); + }); + test('getEventList', () async { + final events = await database.getEventList( + Room(id: '!testroom:example.com', client: Client('testclient'))); + expect(events.single.type, EventTypes.Message); + }); + test('getUser', () async { + final user = await database.getUser('@bob:example.org', + Room(id: '!testroom:example.com', client: Client('testclient'))); + expect(user, null); + }); + test('getUsers', () async { + final users = await database.getUsers( + Room(id: '!testroom:example.com', client: Client('testclient'))); + expect(users.isEmpty, true); + }); + test('removeEvent', () async { + await database.removeEvent('\$event:example.com', '!testroom:example.com'); + final event = await database.getEventById('\$event:example.com', + Room(id: '!testroom:example.com', client: Client('testclient'))); + expect(event, null); + }); + test('getAllInboundGroupSessions', () async { + final result = await database.getAllInboundGroupSessions(); + expect(result.isEmpty, true); + }); + test('getInboundGroupSession', () async { + await database.getInboundGroupSession('!testroom:example.com', 'sessionId'); + }); + test('getInboundGroupSessionsToUpload', () async { + await database.getInboundGroupSessionsToUpload(); + }); + test('storeInboundGroupSession', () async { + await database.storeInboundGroupSession( + '!testroom:example.com', + 'sessionId', + 'pickle', + '{"foo":"bar"}', + '{}', + '{}', + 'senderKey', + '{}', + ); + final session = await database.getInboundGroupSession( + '!testroom:example.com', + 'sessionId', + ); + expect(jsonDecode(session!.content)['foo'], 'bar'); + }); + test('markInboundGroupSessionAsUploaded', () async { + await database.markInboundGroupSessionAsUploaded( + '!testroom:example.com', 'sessionId'); + }); + test('markInboundGroupSessionsAsNeedingUpload', () async { + await database.markInboundGroupSessionsAsNeedingUpload(); + }); + test('updateInboundGroupSessionAllowedAtIndex', () async { + await database.updateInboundGroupSessionAllowedAtIndex( + '{}', + '!testroom:example.com', + 'sessionId', + ); + }); + test('updateInboundGroupSessionIndexes', () async { + await database.updateInboundGroupSessionIndexes( + '{}', + '!testroom:example.com', + 'sessionId', + ); + }); + test('getSSSSCache', () async { + final cache = await database.getSSSSCache('type'); + expect(cache, null); + }); + test('storeSSSSCache', () async { + await database.storeSSSSCache('type', 'keyId', 'ciphertext', '{}'); + final cache = (await database.getSSSSCache('type'))!; + expect(cache.type, 'type'); + expect(cache.keyId, 'keyId'); + expect(cache.ciphertext, 'ciphertext'); + expect(cache.content, '{}'); + }); + test('getOlmSessions', () async { + final olm = await database.getOlmSessions( + 'identityKey', + 'userId', + ); + expect(olm.isEmpty, true); + }); + test('getAllOlmSessions', () async { + var sessions = await database.getAllOlmSessions(); + expect(sessions.isEmpty, true); + await database.storeOlmSession( + 'identityKey', + 'sessionId', + 'pickle', + 0, + ); + await database.storeOlmSession( + 'identityKey', + 'sessionId2', + 'pickle', + 0, + ); + sessions = await database.getAllOlmSessions(); + expect( + sessions, + { + 'identityKey': { + 'sessionId': { + 'identity_key': 'identityKey', + 'pickle': 'pickle', + 'session_id': 'sessionId', + 'last_received': 0 + }, + 'sessionId2': { + 'identity_key': 'identityKey', + 'pickle': 'pickle', + 'session_id': 'sessionId2', + 'last_received': 0 + } + } + }, + ); + }); + test('getOlmSessionsForDevices', () async { + final olm = await database.getOlmSessionsForDevices( + ['identityKeys'], + 'userId', + ); + expect(olm.isEmpty, true); + }); + test('storeOlmSession', () async { + if (!(await olmEnabled())) return; + await database.storeOlmSession( + 'identityKey', + 'sessionId', + 'pickle', + 0, + ); + final olm = await database.getOlmSessions( + 'identityKey', + 'userId', + ); + expect(olm.isNotEmpty, true); + }); + test('getOutboundGroupSession', () async { + final session = await database.getOutboundGroupSession( + '!testroom:example.com', + '@alice:example.com', + ); + expect(session, null); + }); + test('storeOutboundGroupSession', () async { + if (!(await olmEnabled())) return; + await database.storeOutboundGroupSession( + '!testroom:example.com', + 'pickle', + '{}', + 0, + ); + final session = await database.getOutboundGroupSession( + '!testroom:example.com', + '@alice:example.com', + ); + expect(session?.devices.isEmpty, true); + }); + test('getLastSentMessageUserDeviceKey', () async { + final list = await database.getLastSentMessageUserDeviceKey( + 'userId', + 'deviceId', + ); + expect(list.isEmpty, true); + }); + test('getUnimportantRoomEventStatesForRoom', () async { + final events = await database.getUnimportantRoomEventStatesForRoom( + ['events'], + Room(id: '!mep', client: Client('testclient')), + ); + expect(events.isEmpty, true); + }); + test('getUserDeviceKeys', () async { + await database.getUserDeviceKeys(Client('testclient')); + }); + test('storeUserCrossSigningKey', () async { + await database.storeUserCrossSigningKey( + '@alice:example.com', + 'publicKey', + '{}', + false, + false, + ); + }); + test('setVerifiedUserCrossSigningKey', () async { + await database.setVerifiedUserCrossSigningKey( + true, + '@alice:example.com', + 'publicKey', + ); + }); + test('setBlockedUserCrossSigningKey', () async { + await database.setBlockedUserCrossSigningKey( + true, + '@alice:example.com', + 'publicKey', + ); + }); + test('removeUserCrossSigningKey', () async { + await database.removeUserCrossSigningKey( + '@alice:example.com', + 'publicKey', + ); + }); + test('storeUserDeviceKeysInfo', () async { + await database.storeUserDeviceKeysInfo( + '@alice:example.com', + true, + ); + }); + test('storeUserDeviceKey', () async { + await database.storeUserDeviceKey( + '@alice:example.com', + 'deviceId', + '{}', + false, + false, + 0, + ); + }); + test('setVerifiedUserDeviceKey', () async { + await database.setVerifiedUserDeviceKey( + true, + '@alice:example.com', + 'deviceId', + ); + }); + test('setBlockedUserDeviceKey', () async { + await database.setBlockedUserDeviceKey( + true, + '@alice:example.com', + 'deviceId', + ); + }); + + // Clearing up from here + test('clearSSSSCache', () async { + await database.clearSSSSCache(); + }); + test('clearCache', () async { + await database.clearCache(); + }); + test('clear', () async { + await database.clear(); + }); + test('Close', () async { + await database.close(); + }); + return; +} diff --git a/test/device_keys_list_test.dart b/test/device_keys_list_test.dart new file mode 100644 index 0000000..13d24f5 --- /dev/null +++ b/test/device_keys_list_test.dart @@ -0,0 +1,265 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:convert'; + +import 'package:matrix/matrix.dart'; + +import 'package:test/test.dart'; +import 'package:olm/olm.dart' as olm; + +import './fake_client.dart'; +import './fake_matrix_api.dart'; + +void main() { + /// All Tests related to device keys + group('Device keys', () { + Logs().level = Level.error; + + var olmEnabled = true; + + late Client client; + + test('setupClient', () async { + try { + await olm.init(); + olm.get_library_version(); + } catch (e) { + olmEnabled = false; + Logs().w('[LibOlm] Failed to load LibOlm', e); + } + Logs().i('[LibOlm] Enabled: $olmEnabled'); + if (!olmEnabled) return; + + client = await getClient(); + }); + + test('fromJson', () async { + if (!olmEnabled) return; + var rawJson = { + 'user_id': '@alice:example.com', + 'device_id': 'JLAFKJWSCS', + 'algorithms': [ + AlgorithmTypes.olmV1Curve25519AesSha2, + AlgorithmTypes.megolmV1AesSha2 + ], + 'keys': { + 'curve25519:JLAFKJWSCS': + '3C5BFWi2Y8MaVvjM8M22DBmh24PmgR0nPvJOIArzgyI', + 'ed25519:JLAFKJWSCS': 'lEuiRJBit0IG6nUf5pUzWTUEsRVVe/HJkoKuEww9ULI' + }, + 'signatures': { + '@alice:example.com': { + 'ed25519:JLAFKJWSCS': + 'dSO80A01XiigH3uBiDVx/EjzaoycHcjq9lfQX0uWsqxl2giMIiSPR8a4d291W1ihKJL/a+myXS367WT6NAIcBA' + } + }, + 'unsigned': {'device_display_name': "Alice's mobile phone"}, + }; + + final key = DeviceKeys.fromJson(rawJson, client); + // NOTE(Nico): this actually doesn't do anything, because the device signature is invalid... + await key.setVerified(false, false); + await key.setBlocked(true); + expect(json.encode(key.toJson()), json.encode(rawJson)); + expect(key.directVerified, false); + expect(key.blocked, true); + + rawJson = { + 'user_id': '@test:fakeServer.notExisting', + 'usage': ['master'], + 'keys': { + 'ed25519:82mAXjsmbTbrE6zyShpR869jnrANO75H8nYY0nDLoJ8': + '82mAXjsmbTbrE6zyShpR869jnrANO75H8nYY0nDLoJ8', + }, + 'signatures': {}, + }; + final crossKey = CrossSigningKey.fromJson(rawJson, client); + expect(json.encode(crossKey.toJson()), json.encode(rawJson)); + expect(crossKey.usage.first, 'master'); + }); + + test('reject devices without self-signature', () async { + if (!olmEnabled) return; + var key = DeviceKeys.fromJson({ + 'user_id': '@test:fakeServer.notExisting', + 'device_id': 'BADDEVICE', + 'algorithms': [ + AlgorithmTypes.olmV1Curve25519AesSha2, + AlgorithmTypes.megolmV1AesSha2 + ], + 'keys': { + 'curve25519:BADDEVICE': 'ds6+bItpDiWyRaT/b0ofoz1R+GCy7YTbORLJI4dmYho', + 'ed25519:BADDEVICE': 'CdDKVf44LO2QlfWopP6VWmqedSrRaf9rhHKvdVyH38w' + }, + }, client); + expect(key.isValid, false); + expect(key.selfSigned, false); + key = DeviceKeys.fromJson({ + 'user_id': '@test:fakeServer.notExisting', + 'device_id': 'BADDEVICE', + 'algorithms': [ + AlgorithmTypes.olmV1Curve25519AesSha2, + AlgorithmTypes.megolmV1AesSha2 + ], + 'keys': { + 'curve25519:BADDEVICE': 'ds6+bItpDiWyRaT/b0ofoz1R+GCy7YTbORLJI4dmYho', + 'ed25519:BADDEVICE': 'CdDKVf44LO2QlfWopP6VWmqedSrRaf9rhHKvdVyH38w' + }, + 'signatures': { + '@test:fakeServer.notExisting': { + 'ed25519:BADDEVICE': 'invalid', + }, + }, + }, client); + expect(key.isValid, false); + expect(key.selfSigned, false); + }); + + test('set blocked / verified', () async { + if (!olmEnabled) return; + final key = + client.userDeviceKeys[client.userID]!.deviceKeys['OTHERDEVICE']!; + client.userDeviceKeys[client.userID]?.deviceKeys['UNSIGNEDDEVICE'] = + DeviceKeys.fromJson({ + 'user_id': '@test:fakeServer.notExisting', + 'device_id': 'UNSIGNEDDEVICE', + 'algorithms': [ + AlgorithmTypes.olmV1Curve25519AesSha2, + AlgorithmTypes.megolmV1AesSha2 + ], + 'keys': { + 'curve25519:UNSIGNEDDEVICE': + 'ds6+bItpDiWyRaT/b0ofoz1R+GCy7YTbORLJI4dmYho', + 'ed25519:UNSIGNEDDEVICE': + 'CdDKVf44LO2QlfWopP6VWmqedSrRaf9rhHKvdVyH38w' + }, + 'signatures': { + '@test:fakeServer.notExisting': { + 'ed25519:UNSIGNEDDEVICE': + 'f2p1kv6PIz+hnoFYnHEurhUKIyRsdxwR2RTKT1EnQ3aF2zlZOjmnndOCtIT24Q8vs2PovRw+/jkHKj4ge2yDDw', + }, + }, + }, client); + final masterKey = client.userDeviceKeys[client.userID]!.masterKey!; + masterKey.setDirectVerified(true); + // we need to populate the ssss cache to be able to test signing easily + final handle = client.encryption!.ssss.open(); + await handle.unlock(recoveryKey: ssssKey); + await handle.maybeCacheAll(); + + expect(key.verified, true); + expect(key.encryptToDevice, true); + await key.setBlocked(true); + expect(key.verified, false); + expect(key.encryptToDevice, false); + await key.setBlocked(false); + expect(key.directVerified, false); + expect(key.verified, true); // still verified via cross-sgining + expect(key.encryptToDevice, true); + expect( + client.userDeviceKeys[client.userID]?.deviceKeys['UNSIGNEDDEVICE'] + ?.encryptToDevice, + false); + + expect(masterKey.verified, true); + await masterKey.setBlocked(true); + expect(masterKey.verified, false); + expect( + client.userDeviceKeys[client.userID]?.deviceKeys['UNSIGNEDDEVICE'] + ?.encryptToDevice, + true); + await masterKey.setBlocked(false); + expect(masterKey.verified, true); + + FakeMatrixApi.calledEndpoints.clear(); + await key.setVerified(true); + await Future.delayed(Duration(milliseconds: 10)); + expect( + FakeMatrixApi.calledEndpoints.keys + .any((k) => k == '/client/r0/keys/signatures/upload'), + true); + expect(key.directVerified, true); + + FakeMatrixApi.calledEndpoints.clear(); + await key.setVerified(false); + await Future.delayed(Duration(milliseconds: 10)); + expect( + FakeMatrixApi.calledEndpoints.keys + .any((k) => k == '/client/r0/keys/signatures/upload'), + false); + expect(key.directVerified, false); + client.userDeviceKeys[client.userID]?.deviceKeys.remove('UNSIGNEDDEVICE'); + }); + + test('verification based on signatures', () async { + if (!olmEnabled) return; + final user = client.userDeviceKeys[client.userID]!; + user.masterKey?.setDirectVerified(true); + expect(user.deviceKeys['GHTYAJCE']?.crossVerified, true); + expect(user.deviceKeys['GHTYAJCE']?.signed, true); + expect(user.getKey('GHTYAJCE')?.crossVerified, true); + expect(user.deviceKeys['OTHERDEVICE']?.crossVerified, true); + expect(user.selfSigningKey?.crossVerified, true); + expect( + user + .getKey('F9ypFzgbISXCzxQhhSnXMkc1vq12Luna3Nw5rqViOJY') + ?.crossVerified, + true); + expect(user.userSigningKey?.crossVerified, true); + expect(user.verified, UserVerifiedStatus.verified); + user.masterKey?.setDirectVerified(false); + expect(user.deviceKeys['GHTYAJCE']?.crossVerified, false); + expect(user.deviceKeys['OTHERDEVICE']?.crossVerified, false); + expect(user.verified, UserVerifiedStatus.unknown); + + user.deviceKeys['OTHERDEVICE']?.setDirectVerified(true); + expect(user.verified, UserVerifiedStatus.verified); + user.deviceKeys['OTHERDEVICE']?.setDirectVerified(false); + + user.masterKey?.setDirectVerified(true); + user.deviceKeys['GHTYAJCE']?.signatures?[client.userID] + ?.removeWhere((k, v) => k != 'ed25519:GHTYAJCE'); + expect(user.deviceKeys['GHTYAJCE']?.verified, + true); // it's our own device, should be direct verified + expect(user.deviceKeys['GHTYAJCE']?.signed, + false); // not verified for others + user.deviceKeys['OTHERDEVICE']?.signatures?.clear(); + expect(user.verified, UserVerifiedStatus.unknownDevice); + }); + + test('start verification', () async { + if (!olmEnabled) return; + var req = client + .userDeviceKeys['@alice:example.com']?.deviceKeys['JLAFKJWSCS'] + ?.startVerification(); + expect(req != null, true); + expect(req?.room != null, false); + + req = await client.userDeviceKeys['@alice:example.com'] + ?.startVerification(newDirectChatEnableEncryption: false); + expect(req != null, true); + expect(req?.room != null, true); + }); + + test('dispose client', () async { + if (!olmEnabled) return; + await client.dispose(closeDatabase: true); + }); + }); +} diff --git a/test/encryption/bootstrap_test.dart b/test/encryption/bootstrap_test.dart new file mode 100644 index 0000000..69a643f --- /dev/null +++ b/test/encryption/bootstrap_test.dart @@ -0,0 +1,275 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:async'; +import 'dart:convert'; + +import 'package:matrix/matrix.dart'; +import 'package:matrix/encryption.dart'; + +import 'package:test/test.dart'; +import 'package:olm/olm.dart' as olm; + +import '../fake_client.dart'; + +void main() { + group('Bootstrap', () { + Logs().level = Level.error; + var olmEnabled = true; + + late Client client; + late Map oldSecret; + late String origKeyId; + + test('setupClient', () async { + client = await getClient(); + await client.abortSync(); + }); + + test('setup', () async { + try { + await olm.init(); + olm.get_library_version(); + } catch (e) { + olmEnabled = false; + Logs().w('[LibOlm] Failed to load LibOlm', e); + } + Logs().i('[LibOlm] Enabled: $olmEnabled'); + if (!olmEnabled) return; + + Bootstrap? bootstrap; + bootstrap = client.encryption!.bootstrap( + onUpdate: () async { + while (bootstrap == null) { + await Future.delayed(Duration(milliseconds: 5)); + } + if (bootstrap.state == BootstrapState.askWipeSsss) { + bootstrap.wipeSsss(true); + } else if (bootstrap.state == BootstrapState.askNewSsss) { + await bootstrap.newSsss('foxies'); + } else if (bootstrap.state == BootstrapState.askWipeCrossSigning) { + bootstrap.wipeCrossSigning(true); + } else if (bootstrap.state == BootstrapState.askSetupCrossSigning) { + await bootstrap.askSetupCrossSigning( + setupMasterKey: true, + setupSelfSigningKey: true, + setupUserSigningKey: true, + ); + } else if (bootstrap.state == BootstrapState.askWipeOnlineKeyBackup) { + bootstrap.wipeOnlineKeyBackup(true); + } else if (bootstrap.state == + BootstrapState.askSetupOnlineKeyBackup) { + await bootstrap.askSetupOnlineKeyBackup(true); + } + }, + ); + while (bootstrap.state != BootstrapState.done) { + await Future.delayed(Duration(milliseconds: 50)); + } + final defaultKey = client.encryption!.ssss.open(); + await defaultKey.unlock(passphrase: 'foxies'); + + // test all the x-signing keys match up + for (final keyType in {'master', 'user_signing', 'self_signing'}) { + final privateKey = base64 + .decode(await defaultKey.getStored('m.cross_signing.$keyType')); + final keyObj = olm.PkSigning(); + try { + final pubKey = keyObj.init_with_seed(privateKey); + expect( + pubKey, + client.userDeviceKeys[client.userID] + ?.getCrossSigningKey(keyType) + ?.publicKey); + } finally { + keyObj.free(); + } + } + + await defaultKey.store('foxes', 'floof'); + await Future.delayed(Duration(milliseconds: 50)); + oldSecret = + json.decode(json.encode(client.accountData['foxes']!.content)); + origKeyId = defaultKey.keyId; + }, timeout: Timeout(Duration(minutes: 2))); + + test('change recovery passphrase', () async { + if (!olmEnabled) return; + Bootstrap? bootstrap; + bootstrap = client.encryption!.bootstrap( + onUpdate: () async { + while (bootstrap == null) { + await Future.delayed(Duration(milliseconds: 5)); + } + if (bootstrap.state == BootstrapState.askWipeSsss) { + bootstrap.wipeSsss(false); + } else if (bootstrap.state == BootstrapState.askUseExistingSsss) { + bootstrap.useExistingSsss(false); + } else if (bootstrap.state == BootstrapState.askUnlockSsss) { + await bootstrap.oldSsssKeys![client.encryption!.ssss.defaultKeyId]! + .unlock(passphrase: 'foxies'); + bootstrap.unlockedSsss(); + } else if (bootstrap.state == BootstrapState.askNewSsss) { + await bootstrap.newSsss('newfoxies'); + } else if (bootstrap.state == BootstrapState.askWipeCrossSigning) { + bootstrap.wipeCrossSigning(false); + } else if (bootstrap.state == BootstrapState.askWipeOnlineKeyBackup) { + bootstrap.wipeOnlineKeyBackup(false); + } + }, + ); + while (bootstrap.state != BootstrapState.done) { + await Future.delayed(Duration(milliseconds: 50)); + } + final defaultKey = client.encryption!.ssss.open(); + await defaultKey.unlock(passphrase: 'newfoxies'); + + // test all the x-signing keys match up + for (final keyType in {'master', 'user_signing', 'self_signing'}) { + final privateKey = base64 + .decode(await defaultKey.getStored('m.cross_signing.$keyType')); + final keyObj = olm.PkSigning(); + try { + final pubKey = keyObj.init_with_seed(privateKey); + expect( + pubKey, + client.userDeviceKeys[client.userID] + ?.getCrossSigningKey(keyType) + ?.publicKey); + } finally { + keyObj.free(); + } + } + + expect(await defaultKey.getStored('foxes'), 'floof'); + }, timeout: Timeout(Duration(minutes: 2))); + + test('change passphrase with multiple keys', () async { + if (!olmEnabled) return; + await client.setAccountData(client.userID!, 'foxes', oldSecret); + await Future.delayed(Duration(milliseconds: 50)); + + Bootstrap? bootstrap; + bootstrap = client.encryption!.bootstrap( + onUpdate: () async { + while (bootstrap == null) { + await Future.delayed(Duration(milliseconds: 5)); + } + if (bootstrap.state == BootstrapState.askWipeSsss) { + bootstrap.wipeSsss(false); + } else if (bootstrap.state == BootstrapState.askUseExistingSsss) { + bootstrap.useExistingSsss(false); + } else if (bootstrap.state == BootstrapState.askUnlockSsss) { + await bootstrap.oldSsssKeys![client.encryption!.ssss.defaultKeyId]! + .unlock(passphrase: 'newfoxies'); + await bootstrap.oldSsssKeys![origKeyId]! + .unlock(passphrase: 'foxies'); + bootstrap.unlockedSsss(); + } else if (bootstrap.state == BootstrapState.askNewSsss) { + await bootstrap.newSsss('supernewfoxies'); + } else if (bootstrap.state == BootstrapState.askWipeCrossSigning) { + bootstrap.wipeCrossSigning(false); + } else if (bootstrap.state == BootstrapState.askWipeOnlineKeyBackup) { + bootstrap.wipeOnlineKeyBackup(false); + } + }, + ); + while (bootstrap.state != BootstrapState.done) { + await Future.delayed(Duration(milliseconds: 50)); + } + final defaultKey = client.encryption!.ssss.open(); + await defaultKey.unlock(passphrase: 'supernewfoxies'); + + // test all the x-signing keys match up + for (final keyType in {'master', 'user_signing', 'self_signing'}) { + final privateKey = base64 + .decode(await defaultKey.getStored('m.cross_signing.$keyType')); + final keyObj = olm.PkSigning(); + try { + final pubKey = keyObj.init_with_seed(privateKey); + expect( + pubKey, + client.userDeviceKeys[client.userID] + ?.getCrossSigningKey(keyType) + ?.publicKey); + } finally { + keyObj.free(); + } + } + + expect(await defaultKey.getStored('foxes'), 'floof'); + }, timeout: Timeout(Duration(minutes: 2))); + + test('setup new ssss', () async { + if (!olmEnabled) return; + client.accountData.clear(); + Bootstrap? bootstrap; + bootstrap = client.encryption!.bootstrap( + onUpdate: () async { + while (bootstrap == null) { + await Future.delayed(Duration(milliseconds: 5)); + } + if (bootstrap.state == BootstrapState.askNewSsss) { + await bootstrap.newSsss('thenewestfoxies'); + } else if (bootstrap.state == BootstrapState.askSetupCrossSigning) { + await bootstrap.askSetupCrossSigning(); + } else if (bootstrap.state == + BootstrapState.askSetupOnlineKeyBackup) { + await bootstrap.askSetupOnlineKeyBackup(false); + } + }, + ); + while (bootstrap.state != BootstrapState.done) { + await Future.delayed(Duration(milliseconds: 50)); + } + final defaultKey = client.encryption!.ssss.open(); + await defaultKey.unlock(passphrase: 'thenewestfoxies'); + }, timeout: Timeout(Duration(minutes: 2))); + + test('bad ssss', () async { + if (!olmEnabled) return; + client.accountData.clear(); + await client.setAccountData(client.userID!, 'foxes', oldSecret); + await Future.delayed(Duration(milliseconds: 50)); + var askedBadSsss = false; + Bootstrap? bootstrap; + bootstrap = client.encryption!.bootstrap( + onUpdate: () async { + while (bootstrap == null) { + await Future.delayed(Duration(milliseconds: 5)); + } + if (bootstrap.state == BootstrapState.askWipeSsss) { + bootstrap.wipeSsss(false); + } else if (bootstrap.state == BootstrapState.askBadSsss) { + askedBadSsss = true; + bootstrap.ignoreBadSecrets(false); + } + }, + ); + while (bootstrap.state != BootstrapState.error) { + await Future.delayed(Duration(milliseconds: 50)); + } + expect(askedBadSsss, true); + }); + + test('dispose client', () async { + if (!olmEnabled) return; + await client.dispose(closeDatabase: true); + }); + }); +} diff --git a/test/encryption/cross_signing_test.dart b/test/encryption/cross_signing_test.dart new file mode 100644 index 0000000..260dff9 --- /dev/null +++ b/test/encryption/cross_signing_test.dart @@ -0,0 +1,122 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:convert'; + +import 'package:matrix/matrix.dart'; + +import 'package:test/test.dart'; +import 'package:olm/olm.dart' as olm; + +import '../fake_client.dart'; +import '../fake_matrix_api.dart'; + +void main() { + group('Cross Signing', () { + Logs().level = Level.error; + var olmEnabled = true; + + late Client client; + + test('setupClient', () async { + try { + await olm.init(); + olm.get_library_version(); + } catch (e) { + olmEnabled = false; + Logs().w('[LibOlm] Failed to load LibOlm', e); + } + Logs().i('[LibOlm] Enabled: $olmEnabled'); + if (!olmEnabled) return; + + client = await getClient(); + }); + + test('basic things', () async { + if (!olmEnabled) return; + expect(client.encryption?.crossSigning.enabled, true); + }); + + test('selfSign', () async { + if (!olmEnabled) return; + final key = client.userDeviceKeys[client.userID]!.masterKey!; + key.setDirectVerified(false); + FakeMatrixApi.calledEndpoints.clear(); + await client.encryption!.crossSigning.selfSign(recoveryKey: ssssKey); + expect(key.directVerified, true); + expect( + FakeMatrixApi.calledEndpoints + .containsKey('/client/r0/keys/signatures/upload'), + true); + expect(await client.encryption!.crossSigning.isCached(), true); + }); + + test('signable', () async { + if (!olmEnabled) return; + expect( + client.encryption!.crossSigning + .signable([client.userDeviceKeys[client.userID!]!.masterKey!]), + true); + expect( + client.encryption!.crossSigning.signable([ + client.userDeviceKeys[client.userID!]!.deviceKeys[client.deviceID!]! + ]), + false); + expect( + client.encryption!.crossSigning.signable([ + client.userDeviceKeys[client.userID!]!.deviceKeys['OTHERDEVICE']! + ]), + true); + expect( + client.encryption!.crossSigning.signable([ + client + .userDeviceKeys['@alice:example.com']!.deviceKeys['JLAFKJWSCS']! + ]), + false); + }); + + test('sign', () async { + if (!olmEnabled) return; + FakeMatrixApi.calledEndpoints.clear(); + await client.encryption!.crossSigning.sign([ + client.userDeviceKeys[client.userID!]!.masterKey!, + client.userDeviceKeys[client.userID!]!.deviceKeys['OTHERDEVICE']!, + client.userDeviceKeys['@othertest:fakeServer.notExisting']!.masterKey! + ]); + final body = json.decode(FakeMatrixApi + .calledEndpoints['/client/r0/keys/signatures/upload']!.first); + expect(body['@test:fakeServer.notExisting']?.containsKey('OTHERDEVICE'), + true); + expect( + body['@test:fakeServer.notExisting'].containsKey( + client.userDeviceKeys[client.userID]!.masterKey!.publicKey), + true); + expect( + body['@othertest:fakeServer.notExisting'].containsKey(client + .userDeviceKeys['@othertest:fakeServer.notExisting'] + ?.masterKey + ?.publicKey), + true); + }); + + test('dispose client', () async { + if (!olmEnabled) return; + await client.dispose(closeDatabase: true); + }); + }); +} diff --git a/test/encryption/encrypt_decrypt_room_message_test.dart b/test/encryption/encrypt_decrypt_room_message_test.dart new file mode 100644 index 0000000..bc2e007 --- /dev/null +++ b/test/encryption/encrypt_decrypt_room_message_test.dart @@ -0,0 +1,107 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:matrix/matrix.dart'; + +import 'package:test/test.dart'; +import 'package:olm/olm.dart' as olm; + +import '../fake_client.dart'; + +void main() { + group('Encrypt/Decrypt room message', () { + Logs().level = Level.error; + var olmEnabled = true; + + late Client client; + final roomId = '!726s6s6q:example.com'; + late Room room; + late Map payload; + final now = DateTime.now(); + + test('setupClient', () async { + try { + await olm.init(); + olm.get_library_version(); + } catch (e) { + olmEnabled = false; + Logs().w('[LibOlm] Failed to load LibOlm', e); + } + Logs().i('[LibOlm] Enabled: $olmEnabled'); + if (!olmEnabled) return; + + client = await getClient(); + room = client.getRoomById(roomId)!; + }); + + test('encrypt payload', () async { + if (!olmEnabled) return; + payload = await client.encryption!.encryptGroupMessagePayload(roomId, { + 'msgtype': 'm.text', + 'text': 'Hello foxies!', + }); + expect(payload['algorithm'], AlgorithmTypes.megolmV1AesSha2); + expect(payload['ciphertext'] is String, true); + expect(payload['device_id'], client.deviceID); + expect(payload['sender_key'], client.identityKey); + expect(payload['session_id'] is String, true); + }); + + test('decrypt payload', () async { + if (!olmEnabled) return; + final encryptedEvent = Event( + type: EventTypes.Encrypted, + content: payload, + room: room, + originServerTs: now, + eventId: '\$event', + senderId: client.userID!, + ); + final decryptedEvent = + await client.encryption!.decryptRoomEvent(roomId, encryptedEvent); + expect(decryptedEvent.type, 'm.room.message'); + expect(decryptedEvent.content['msgtype'], 'm.text'); + expect(decryptedEvent.content['text'], 'Hello foxies!'); + }); + + test('decrypt payload nocache', () async { + if (!olmEnabled) return; + client.encryption!.keyManager.clearInboundGroupSessions(); + final encryptedEvent = Event( + type: EventTypes.Encrypted, + content: payload, + room: room, + originServerTs: now, + eventId: '\$event', + senderId: '@alice:example.com', + ); + final decryptedEvent = + await client.encryption!.decryptRoomEvent(roomId, encryptedEvent); + expect(decryptedEvent.type, 'm.room.message'); + expect(decryptedEvent.content['msgtype'], 'm.text'); + expect(decryptedEvent.content['text'], 'Hello foxies!'); + await client.encryption! + .decryptRoomEvent(roomId, encryptedEvent, store: true); + }); + + test('dispose client', () async { + if (!olmEnabled) return; + await client.dispose(closeDatabase: true); + }); + }); +} diff --git a/test/encryption/encrypt_decrypt_to_device_test.dart b/test/encryption/encrypt_decrypt_to_device_test.dart new file mode 100644 index 0000000..6982806 --- /dev/null +++ b/test/encryption/encrypt_decrypt_to_device_test.dart @@ -0,0 +1,124 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:matrix/matrix.dart'; + +import 'package:test/test.dart'; +import 'package:olm/olm.dart' as olm; + +import '../fake_client.dart'; +import '../fake_database.dart'; +import '../fake_matrix_api.dart'; + +void main() { + // key @othertest:fakeServer.notExisting + const otherPickledOlmAccount = + 'VWhVApbkcilKAEGppsPDf9nNVjaK8/IxT3asSR0sYg0S5KgbfE8vXEPwoiKBX2cEvwX3OessOBOkk+ZE7TTbjlrh/KEd31p8Wo+47qj0AP+Ky+pabnhi+/rTBvZy+gfzTqUfCxZrkzfXI9Op4JnP6gYmy7dVX2lMYIIs9WCO1jcmIXiXum5jnfXu1WLfc7PZtO2hH+k9CDKosOFaXRBmsu8k/BGXPSoWqUpvu6WpEG9t5STk4FeAzA'; + + group('Encrypt/Decrypt to-device messages', () { + Logs().level = Level.error; + var olmEnabled = true; + + late Client client; + final otherClient = Client('othertestclient', + httpClient: FakeMatrixApi(), databaseBuilder: getDatabase); + late DeviceKeys device; + late Map payload; + + test('setupClient', () async { + try { + await olm.init(); + olm.get_library_version(); + } catch (e) { + olmEnabled = false; + Logs().w('[LibOlm] Failed to load LibOlm', e); + } + Logs().i('[LibOlm] Enabled: $olmEnabled'); + if (!olmEnabled) return; + + client = await getClient(); + await client.abortSync(); + await otherClient.checkHomeserver('https://fakeserver.notexisting', + checkWellKnown: false); + await otherClient.init( + newToken: 'abc', + newUserID: '@othertest:fakeServer.notExisting', + newHomeserver: otherClient.homeserver, + newDeviceName: 'Text Matrix Client', + newDeviceID: 'FOXDEVICE', + newOlmAccount: otherPickledOlmAccount, + ); + await otherClient.abortSync(); + + await Future.delayed(Duration(milliseconds: 10)); + device = DeviceKeys.fromJson({ + 'user_id': client.userID, + 'device_id': client.deviceID, + 'algorithms': [ + AlgorithmTypes.olmV1Curve25519AesSha2, + AlgorithmTypes.megolmV1AesSha2 + ], + 'keys': { + 'curve25519:${client.deviceID}': client.identityKey, + 'ed25519:${client.deviceID}': client.fingerprintKey, + }, + }, client); + }); + + test('encryptToDeviceMessage', () async { + if (!olmEnabled) return; + payload = await otherClient.encryption! + .encryptToDeviceMessage([device], 'm.to_device', {'hello': 'foxies'}); + }); + + test('decryptToDeviceEvent', () async { + if (!olmEnabled) return; + final encryptedEvent = ToDeviceEvent( + sender: '@othertest:fakeServer.notExisting', + type: EventTypes.Encrypted, + content: payload[client.userID][client.deviceID], + ); + final decryptedEvent = + await client.encryption!.decryptToDeviceEvent(encryptedEvent); + expect(decryptedEvent.type, 'm.to_device'); + expect(decryptedEvent.content['hello'], 'foxies'); + }); + + test('decryptToDeviceEvent nocache', () async { + if (!olmEnabled) return; + client.encryption!.olmManager.olmSessions.clear(); + payload = await otherClient.encryption!.encryptToDeviceMessage( + [device], 'm.to_device', {'hello': 'superfoxies'}); + final encryptedEvent = ToDeviceEvent( + sender: '@othertest:fakeServer.notExisting', + type: EventTypes.Encrypted, + content: payload[client.userID][client.deviceID], + ); + final decryptedEvent = + await client.encryption!.decryptToDeviceEvent(encryptedEvent); + expect(decryptedEvent.type, 'm.to_device'); + expect(decryptedEvent.content['hello'], 'superfoxies'); + }); + + test('dispose client', () async { + if (!olmEnabled) return; + await client.dispose(closeDatabase: true); + await otherClient.dispose(closeDatabase: true); + }); + }); +} diff --git a/test/encryption/key_manager_test.dart b/test/encryption/key_manager_test.dart new file mode 100644 index 0000000..5d9b9a2 --- /dev/null +++ b/test/encryption/key_manager_test.dart @@ -0,0 +1,577 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:convert'; + +import 'package:matrix/matrix.dart'; + +import 'package:test/test.dart'; +import 'package:olm/olm.dart' as olm; + +import '../fake_client.dart'; +import '../fake_matrix_api.dart'; + +void main() { + group('Key Manager', () { + Logs().level = Level.error; + var olmEnabled = true; + + late Client client; + + test('setupClient', () async { + try { + await olm.init(); + olm.get_library_version(); + } catch (e) { + olmEnabled = false; + Logs().w('[LibOlm] Failed to load LibOlm', e); + } + Logs().i('[LibOlm] Enabled: $olmEnabled'); + if (!olmEnabled) return; + + client = await getClient(); + }); + + test('handle new m.room_key', () async { + if (!olmEnabled) return; + final validSessionId = 'ciM/JWTPrmiWPPZNkRLDPQYf9AW/I46bxyLSr+Bx5oU'; + final validSenderKey = 'JBG7ZaPn54OBC7TuIEiylW3BZ+7WcGQhFBPB9pogbAg'; + final sessionKey = + 'AgAAAAAQcQ6XrFJk6Prm8FikZDqfry/NbDz8Xw7T6e+/9Yf/q3YHIPEQlzv7IZMNcYb51ifkRzFejVvtphS7wwG2FaXIp4XS2obla14iKISR0X74ugB2vyb1AydIHE/zbBQ1ic5s3kgjMFlWpu/S3FQCnCrv+DPFGEt3ERGWxIl3Bl5X53IjPyVkz65oljz2TZESwz0GH/QFvyOOm8ci0q/gceaF3S7Dmafg3dwTKYwcA5xkcc+BLyrLRzB6Hn+oMAqSNSscnm4mTeT5zYibIhrzqyUTMWr32spFtI9dNR/RFSzfCw'; + + client.encryption!.keyManager.clearInboundGroupSessions(); + var event = ToDeviceEvent( + sender: '@alice:example.com', + type: 'm.room_key', + content: { + 'algorithm': AlgorithmTypes.megolmV1AesSha2, + 'room_id': '!726s6s6q:example.com', + 'session_id': validSessionId, + 'session_key': sessionKey, + }, + encryptedContent: { + 'sender_key': validSenderKey, + }); + await client.encryption!.keyManager.handleToDeviceEvent(event); + expect( + client.encryption!.keyManager.getInboundGroupSession( + '!726s6s6q:example.com', validSessionId, validSenderKey) != + null, + true); + + // now test a few invalid scenarios + + // not encrypted + client.encryption!.keyManager.clearInboundGroupSessions(); + event = ToDeviceEvent( + sender: '@alice:example.com', + type: 'm.room_key', + content: { + 'algorithm': AlgorithmTypes.megolmV1AesSha2, + 'room_id': '!726s6s6q:example.com', + 'session_id': validSessionId, + 'session_key': sessionKey, + }); + await client.encryption!.keyManager.handleToDeviceEvent(event); + expect( + client.encryption!.keyManager.getInboundGroupSession( + '!726s6s6q:example.com', validSessionId, validSenderKey) != + null, + false); + }); + + test('outbound group session', () async { + if (!olmEnabled) return; + final roomId = '!726s6s6q:example.com'; + expect( + client.encryption!.keyManager.getOutboundGroupSession(roomId) != null, + false); + var sess = await client.encryption!.keyManager + .createOutboundGroupSession(roomId); + expect( + client.encryption!.keyManager.getOutboundGroupSession(roomId) != null, + true); + await client.encryption!.keyManager + .clearOrUseOutboundGroupSession(roomId); + expect( + client.encryption!.keyManager.getOutboundGroupSession(roomId) != null, + true); + var inbound = client.encryption!.keyManager.getInboundGroupSession( + roomId, sess.outboundGroupSession!.session_id(), client.identityKey); + expect(inbound != null, true); + expect( + inbound!.allowedAtIndex['@alice:example.com'] + ?['L+4+JCl8MD63dgo8z5Ta+9QAHXiANyOVSfgbHA5d3H8'], + 0); + expect( + inbound.allowedAtIndex['@alice:example.com'] + ?['wMIDhiQl5jEXQrTB03ePOSQfR8sA/KMrW0CIfFfXKEE'], + 0); + + // rotate after too many messages + Iterable.generate(300).forEach((_) { + sess.outboundGroupSession!.encrypt('some string'); + }); + await client.encryption!.keyManager + .clearOrUseOutboundGroupSession(roomId); + expect( + client.encryption!.keyManager.getOutboundGroupSession(roomId) != null, + false); + + // rotate if device is blocked + sess = await client.encryption!.keyManager + .createOutboundGroupSession(roomId); + client.userDeviceKeys['@alice:example.com']!.deviceKeys['JLAFKJWSCS']! + .blocked = true; + await client.encryption!.keyManager + .clearOrUseOutboundGroupSession(roomId); + expect( + client.encryption!.keyManager.getOutboundGroupSession(roomId) != null, + false); + client.userDeviceKeys['@alice:example.com']!.deviceKeys['JLAFKJWSCS']! + .blocked = false; + + // lazy-create if it would rotate + sess = await client.encryption!.keyManager + .createOutboundGroupSession(roomId); + final oldSessKey = sess.outboundGroupSession!.session_key(); + client.userDeviceKeys['@alice:example.com']!.deviceKeys['JLAFKJWSCS']! + .blocked = true; + await client.encryption!.keyManager.prepareOutboundGroupSession(roomId); + expect( + client.encryption!.keyManager.getOutboundGroupSession(roomId) != null, + true); + expect( + client.encryption!.keyManager + .getOutboundGroupSession(roomId)! + .outboundGroupSession! + .session_key() != + oldSessKey, + true); + client.userDeviceKeys['@alice:example.com']!.deviceKeys['JLAFKJWSCS']! + .blocked = false; + + // rotate if too far in the past + sess = await client.encryption!.keyManager + .createOutboundGroupSession(roomId); + sess.creationTime = DateTime.now().subtract(Duration(days: 30)); + await client.encryption!.keyManager + .clearOrUseOutboundGroupSession(roomId); + expect( + client.encryption!.keyManager.getOutboundGroupSession(roomId) != null, + false); + + // rotate if user leaves + sess = await client.encryption!.keyManager + .createOutboundGroupSession(roomId); + final room = client.getRoomById(roomId)!; + final member = room.getState('m.room.member', '@alice:example.com'); + member!.content['membership'] = 'leave'; + room.summary.mJoinedMemberCount = room.summary.mJoinedMemberCount! - 1; + await client.encryption!.keyManager + .clearOrUseOutboundGroupSession(roomId); + expect( + client.encryption!.keyManager.getOutboundGroupSession(roomId) != null, + false); + member.content['membership'] = 'join'; + room.summary.mJoinedMemberCount = room.summary.mJoinedMemberCount! + 1; + + // do not rotate if new device is added + sess = await client.encryption!.keyManager + .createOutboundGroupSession(roomId); + sess.outboundGroupSession!.encrypt( + 'foxies'); // so that the new device will have a different index + client.userDeviceKeys['@alice:example.com']?.deviceKeys['NEWDEVICE'] = + DeviceKeys.fromJson({ + 'user_id': '@alice:example.com', + 'device_id': 'NEWDEVICE', + 'algorithms': [ + AlgorithmTypes.olmV1Curve25519AesSha2, + AlgorithmTypes.megolmV1AesSha2 + ], + 'keys': { + 'curve25519:NEWDEVICE': 'bnKQp6pPW0l9cGoIgHpBoK5OUi4h0gylJ7upc4asFV8', + 'ed25519:NEWDEVICE': 'ZZhPdvWYg3MRpGy2MwtI+4MHXe74wPkBli5hiEOUi8Y' + }, + 'signatures': { + '@alice:example.com': { + 'ed25519:NEWDEVICE': + '94GSg8N9vNB8wyWHJtKaaX3MGNWPVOjBatJM+TijY6B1RlDFJT5Cl1h/tjr17AoQz0CDdOf6uFhrYsBkH1/ABg' + } + } + }, client); + await client.encryption!.keyManager + .clearOrUseOutboundGroupSession(roomId); + expect( + client.encryption!.keyManager.getOutboundGroupSession(roomId) != null, + true); + inbound = client.encryption!.keyManager.getInboundGroupSession( + roomId, sess.outboundGroupSession!.session_id(), client.identityKey); + expect( + inbound!.allowedAtIndex['@alice:example.com'] + ?['L+4+JCl8MD63dgo8z5Ta+9QAHXiANyOVSfgbHA5d3H8'], + 0); + expect( + inbound.allowedAtIndex['@alice:example.com'] + ?['wMIDhiQl5jEXQrTB03ePOSQfR8sA/KMrW0CIfFfXKEE'], + 0); + expect( + inbound.allowedAtIndex['@alice:example.com'] + ?['bnKQp6pPW0l9cGoIgHpBoK5OUi4h0gylJ7upc4asFV8'], + 1); + + // do not rotate if new user is added + member.content['membership'] = 'leave'; + room.summary.mJoinedMemberCount = room.summary.mJoinedMemberCount! - 1; + sess = await client.encryption!.keyManager + .createOutboundGroupSession(roomId); + member.content['membership'] = 'join'; + room.summary.mJoinedMemberCount = room.summary.mJoinedMemberCount! + 1; + await client.encryption!.keyManager + .clearOrUseOutboundGroupSession(roomId); + expect( + client.encryption!.keyManager.getOutboundGroupSession(roomId) != null, + true); + + // force wipe + sess = await client.encryption!.keyManager + .createOutboundGroupSession(roomId); + await client.encryption!.keyManager + .clearOrUseOutboundGroupSession(roomId, wipe: true); + expect( + client.encryption!.keyManager.getOutboundGroupSession(roomId) != null, + false); + + // load from database + sess = await client.encryption!.keyManager + .createOutboundGroupSession(roomId); + client.encryption!.keyManager.clearOutboundGroupSessions(); + expect( + client.encryption!.keyManager.getOutboundGroupSession(roomId) != null, + false); + await client.encryption!.keyManager.loadOutboundGroupSession(roomId); + expect( + client.encryption!.keyManager.getOutboundGroupSession(roomId) != null, + true); + }); + + test('inbound group session', () async { + if (!olmEnabled) return; + final roomId = '!726s6s6q:example.com'; + final sessionId = 'ciM/JWTPrmiWPPZNkRLDPQYf9AW/I46bxyLSr+Bx5oU'; + final senderKey = 'JBG7ZaPn54OBC7TuIEiylW3BZ+7WcGQhFBPB9pogbAg'; + final sessionContent = { + 'algorithm': AlgorithmTypes.megolmV1AesSha2, + 'room_id': '!726s6s6q:example.com', + 'session_id': 'ciM/JWTPrmiWPPZNkRLDPQYf9AW/I46bxyLSr+Bx5oU', + 'session_key': + 'AgAAAAAQcQ6XrFJk6Prm8FikZDqfry/NbDz8Xw7T6e+/9Yf/q3YHIPEQlzv7IZMNcYb51ifkRzFejVvtphS7wwG2FaXIp4XS2obla14iKISR0X74ugB2vyb1AydIHE/zbBQ1ic5s3kgjMFlWpu/S3FQCnCrv+DPFGEt3ERGWxIl3Bl5X53IjPyVkz65oljz2TZESwz0GH/QFvyOOm8ci0q/gceaF3S7Dmafg3dwTKYwcA5xkcc+BLyrLRzB6Hn+oMAqSNSscnm4mTeT5zYibIhrzqyUTMWr32spFtI9dNR/RFSzfCw' + }; + client.encryption!.keyManager.clearInboundGroupSessions(); + expect( + client.encryption!.keyManager + .getInboundGroupSession(roomId, sessionId, senderKey) != + null, + false); + client.encryption!.keyManager + .setInboundGroupSession(roomId, sessionId, senderKey, sessionContent); + await Future.delayed(Duration(milliseconds: 10)); + expect( + client.encryption!.keyManager + .getInboundGroupSession(roomId, sessionId, senderKey) != + null, + true); + expect( + client.encryption!.keyManager + .getInboundGroupSession(roomId, sessionId, 'invalid') != + null, + false); + + expect( + client.encryption!.keyManager + .getInboundGroupSession(roomId, sessionId, senderKey) != + null, + true); + expect( + client.encryption!.keyManager + .getInboundGroupSession('otherroom', sessionId, senderKey) != + null, + true); + expect( + client.encryption!.keyManager + .getInboundGroupSession('otherroom', sessionId, 'invalid') != + null, + false); + expect( + client.encryption!.keyManager + .getInboundGroupSession('otherroom', 'invalid', senderKey) != + null, + false); + + client.encryption!.keyManager.clearInboundGroupSessions(); + expect( + client.encryption!.keyManager + .getInboundGroupSession(roomId, sessionId, senderKey) != + null, + false); + await client.encryption!.keyManager + .loadInboundGroupSession(roomId, sessionId, senderKey); + expect( + client.encryption!.keyManager + .getInboundGroupSession(roomId, sessionId, senderKey) != + null, + true); + + client.encryption!.keyManager.clearInboundGroupSessions(); + expect( + client.encryption!.keyManager + .getInboundGroupSession(roomId, sessionId, senderKey) != + null, + false); + await client.encryption!.keyManager + .loadInboundGroupSession(roomId, sessionId, 'invalid'); + expect( + client.encryption!.keyManager + .getInboundGroupSession(roomId, sessionId, 'invalid') != + null, + false); + }); + + test('setInboundGroupSession', () async { + if (!olmEnabled) return; + final session = olm.OutboundGroupSession(); + session.create(); + final inbound = olm.InboundGroupSession(); + inbound.create(session.session_key()); + final senderKey = client.identityKey; + final roomId = '!someroom:example.org'; + final sessionId = inbound.session_id(); + final room = Room(id: roomId, client: client); + client.rooms.add(room); + // we build up an encrypted message so that we can test if it successfully decrypted afterwards + room.setState( + Event( + senderId: '@test:example.com', + type: 'm.room.encrypted', + room: room, + eventId: '12345', + originServerTs: DateTime.now(), + content: { + 'algorithm': AlgorithmTypes.megolmV1AesSha2, + 'ciphertext': session.encrypt(json.encode({ + 'type': 'm.room.message', + 'content': {'msgtype': 'm.text', 'body': 'foxies'}, + })), + 'device_id': client.deviceID, + 'sender_key': client.identityKey, + 'session_id': sessionId, + }, + stateKey: '', + ), + ); + expect(room.lastEvent?.type, 'm.room.encrypted'); + // set a payload... + var sessionPayload = { + 'algorithm': AlgorithmTypes.megolmV1AesSha2, + 'room_id': roomId, + 'forwarding_curve25519_key_chain': [client.identityKey], + 'session_id': sessionId, + 'session_key': inbound.export_session(1), + 'sender_key': senderKey, + 'sender_claimed_ed25519_key': client.fingerprintKey, + }; + client.encryption!.keyManager.setInboundGroupSession( + roomId, sessionId, senderKey, sessionPayload, + forwarded: true); + expect( + client.encryption!.keyManager + .getInboundGroupSession(roomId, sessionId, senderKey) + ?.inboundGroupSession + ?.first_known_index(), + 1); + expect( + client.encryption!.keyManager + .getInboundGroupSession(roomId, sessionId, senderKey) + ?.forwardingCurve25519KeyChain + .length, + 1); + + // not set one with a higher first known index + sessionPayload = { + 'algorithm': AlgorithmTypes.megolmV1AesSha2, + 'room_id': roomId, + 'forwarding_curve25519_key_chain': [client.identityKey], + 'session_id': sessionId, + 'session_key': inbound.export_session(2), + 'sender_key': senderKey, + 'sender_claimed_ed25519_key': client.fingerprintKey, + }; + client.encryption!.keyManager.setInboundGroupSession( + roomId, sessionId, senderKey, sessionPayload, + forwarded: true); + expect( + client.encryption!.keyManager + .getInboundGroupSession(roomId, sessionId, senderKey) + ?.inboundGroupSession + ?.first_known_index(), + 1); + expect( + client.encryption!.keyManager + .getInboundGroupSession(roomId, sessionId, senderKey) + ?.forwardingCurve25519KeyChain + .length, + 1); + + // set one with a lower first known index + sessionPayload = { + 'algorithm': AlgorithmTypes.megolmV1AesSha2, + 'room_id': roomId, + 'forwarding_curve25519_key_chain': [client.identityKey], + 'session_id': sessionId, + 'session_key': inbound.export_session(0), + 'sender_key': senderKey, + 'sender_claimed_ed25519_key': client.fingerprintKey, + }; + client.encryption!.keyManager.setInboundGroupSession( + roomId, sessionId, senderKey, sessionPayload, + forwarded: true); + expect( + client.encryption!.keyManager + .getInboundGroupSession(roomId, sessionId, senderKey) + ?.inboundGroupSession + ?.first_known_index(), + 0); + expect( + client.encryption!.keyManager + .getInboundGroupSession(roomId, sessionId, senderKey) + ?.forwardingCurve25519KeyChain + .length, + 1); + + // not set one with a longer forwarding chain + sessionPayload = { + 'algorithm': AlgorithmTypes.megolmV1AesSha2, + 'room_id': roomId, + 'forwarding_curve25519_key_chain': [client.identityKey, 'beep'], + 'session_id': sessionId, + 'session_key': inbound.export_session(0), + 'sender_key': senderKey, + 'sender_claimed_ed25519_key': client.fingerprintKey, + }; + client.encryption!.keyManager.setInboundGroupSession( + roomId, sessionId, senderKey, sessionPayload, + forwarded: true); + expect( + client.encryption!.keyManager + .getInboundGroupSession(roomId, sessionId, senderKey) + ?.inboundGroupSession + ?.first_known_index(), + 0); + expect( + client.encryption!.keyManager + .getInboundGroupSession(roomId, sessionId, senderKey) + ?.forwardingCurve25519KeyChain + .length, + 1); + + // set one with a shorter forwarding chain + sessionPayload = { + 'algorithm': AlgorithmTypes.megolmV1AesSha2, + 'room_id': roomId, + 'forwarding_curve25519_key_chain': [], + 'session_id': sessionId, + 'session_key': inbound.export_session(0), + 'sender_key': senderKey, + 'sender_claimed_ed25519_key': client.fingerprintKey, + }; + client.encryption!.keyManager.setInboundGroupSession( + roomId, sessionId, senderKey, sessionPayload, + forwarded: true); + expect( + client.encryption!.keyManager + .getInboundGroupSession(roomId, sessionId, senderKey) + ?.inboundGroupSession + ?.first_known_index(), + 0); + expect( + client.encryption!.keyManager + .getInboundGroupSession(roomId, sessionId, senderKey) + ?.forwardingCurve25519KeyChain + .length, + 0); + + // test that it decrypted the last event + expect(room.lastEvent?.type, 'm.room.message'); + expect(room.lastEvent?.content['body'], 'foxies'); + + inbound.free(); + session.free(); + }); + + test('Reused deviceID attack', () async { + if (!olmEnabled) return; + Logs().level = Level.warning; + + // Ensure the device came from sync + expect( + client.userDeviceKeys['@alice:example.com'] + ?.deviceKeys['JLAFKJWSCS'] != + null, + true); + + // Alice removes her device + client.userDeviceKeys['@alice:example.com']?.deviceKeys + .remove('JLAFKJWSCS'); + + // Alice adds her device with same device ID but different keys + final oldResp = FakeMatrixApi.api['POST']?['/client/r0/keys/query'](null); + FakeMatrixApi.api['POST']?['/client/r0/keys/query'] = (_) { + oldResp['device_keys']['@alice:example.com']['JLAFKJWSCS'] = { + 'user_id': '@alice:example.com', + 'device_id': 'JLAFKJWSCS', + 'algorithms': [ + 'm.olm.v1.curve25519-aes-sha2', + 'm.megolm.v1.aes-sha2' + ], + 'keys': { + 'curve25519:JLAFKJWSCS': + 'WbwrNyD7nvtmcLQ0TTuVPFGJq6JznfjrVsjIpmBqvDw', + 'ed25519:JLAFKJWSCS': 'vl0d54pTVRcvBgUzoQFa8e6TldHWG9O8bh0iuIvgd/I' + }, + 'signatures': { + '@alice:example.com': { + 'ed25519:JLAFKJWSCS': + 's/L86jLa8BTroL8GsBeqO0gRLC3ZrSA7Gch6UoLI2SefC1+1ycmnP9UGbLPh3qBJOmlhczMpBLZwelg87qNNDA' + } + } + }; + return oldResp; + }; + client.userDeviceKeys['@alice:example.com']!.outdated = true; + await client.updateUserDeviceKeys(); + expect( + client.userDeviceKeys['@alice:example.com']?.deviceKeys['JLAFKJWSCS'], + null); + }); + + test('dispose client', () async { + if (!olmEnabled) return; + await client.dispose(closeDatabase: false); + }); + }); +} diff --git a/test/encryption/key_request_test.dart b/test/encryption/key_request_test.dart new file mode 100644 index 0000000..01d0265 --- /dev/null +++ b/test/encryption/key_request_test.dart @@ -0,0 +1,404 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:convert'; +import 'package:matrix/matrix.dart'; + +import 'package:test/test.dart'; +import 'package:olm/olm.dart' as olm; + +import '../fake_client.dart'; +import '../fake_matrix_api.dart'; + +Map jsonDecode(dynamic payload) { + if (payload is String) { + try { + return json.decode(payload); + } catch (e) { + return {}; + } + } + if (payload is Map) return payload; + return {}; +} + +void main() { + /// All Tests related to device keys + group('Key Request', () { + Logs().level = Level.error; + var olmEnabled = true; + + final validSessionId = 'ciM/JWTPrmiWPPZNkRLDPQYf9AW/I46bxyLSr+Bx5oU'; + final validSenderKey = 'JBG7ZaPn54OBC7TuIEiylW3BZ+7WcGQhFBPB9pogbAg'; + test('Create Request', () async { + try { + await olm.init(); + olm.get_library_version(); + } catch (e) { + olmEnabled = false; + Logs().w('[LibOlm] Failed to load LibOlm', e); + } + Logs().i('[LibOlm] Enabled: $olmEnabled'); + if (!olmEnabled) return; + + final matrix = await getClient(); + final requestRoom = matrix.getRoomById('!726s6s6q:example.com')!; + await matrix.encryption!.keyManager.request( + requestRoom, 'sessionId', validSenderKey, + tryOnlineBackup: false); + var foundEvent = false; + for (final entry in FakeMatrixApi.calledEndpoints.entries) { + final payload = jsonDecode(entry.value.first); + if (entry.key + .startsWith('/client/r0/sendToDevice/m.room_key_request') && + (payload['messages'] is Map) && + (payload['messages']['@alice:example.com'] is Map) && + (payload['messages']['@alice:example.com']['*'] is Map)) { + final content = payload['messages']['@alice:example.com']['*']; + if (content['action'] == 'request' && + content['body']['room_id'] == '!726s6s6q:example.com' && + content['body']['sender_key'] == validSenderKey && + content['body']['session_id'] == 'sessionId') { + foundEvent = true; + break; + } + } + } + expect(foundEvent, true); + await matrix.dispose(closeDatabase: true); + }); + test('Reply To Request', () async { + if (!olmEnabled) return; + final matrix = await getClient(); + matrix.setUserId('@alice:example.com'); // we need to pretend to be alice + FakeMatrixApi.calledEndpoints.clear(); + await matrix + .userDeviceKeys['@alice:example.com']!.deviceKeys['OTHERDEVICE']! + .setBlocked(false); + await matrix + .userDeviceKeys['@alice:example.com']!.deviceKeys['OTHERDEVICE']! + .setVerified(true); + final session = await matrix.encryption!.keyManager + .loadInboundGroupSession( + '!726s6s6q:example.com', validSessionId, validSenderKey); + // test a successful share + var event = ToDeviceEvent( + sender: '@alice:example.com', + type: 'm.room_key_request', + content: { + 'action': 'request', + 'body': { + 'algorithm': AlgorithmTypes.megolmV1AesSha2, + 'room_id': '!726s6s6q:example.com', + 'sender_key': validSenderKey, + 'session_id': validSessionId, + }, + 'request_id': 'request_1', + 'requesting_device_id': 'OTHERDEVICE', + }); + await matrix.encryption!.keyManager.handleToDeviceEvent(event); + Logs().i(FakeMatrixApi.calledEndpoints.keys.toString()); + expect( + FakeMatrixApi.calledEndpoints.keys.any( + (k) => k.startsWith('/client/r0/sendToDevice/m.room.encrypted')), + true); + + // test a successful foreign share + FakeMatrixApi.calledEndpoints.clear(); + session!.allowedAtIndex['@test:fakeServer.notExisting'] = { + matrix.userDeviceKeys['@test:fakeServer.notExisting']! + .deviceKeys['OTHERDEVICE']!.curve25519Key!: 0, + }; + event = ToDeviceEvent( + sender: '@test:fakeServer.notExisting', + type: 'm.room_key_request', + content: { + 'action': 'request', + 'body': { + 'algorithm': AlgorithmTypes.megolmV1AesSha2, + 'room_id': '!726s6s6q:example.com', + 'sender_key': validSenderKey, + 'session_id': validSessionId, + }, + 'request_id': 'request_a1', + 'requesting_device_id': 'OTHERDEVICE', + }); + await matrix.encryption!.keyManager.handleToDeviceEvent(event); + Logs().i(FakeMatrixApi.calledEndpoints.keys.toString()); + expect( + FakeMatrixApi.calledEndpoints.keys.any( + (k) => k.startsWith('/client/r0/sendToDevice/m.room.encrypted')), + true); + session.allowedAtIndex.remove('@test:fakeServer.notExisting'); + + // test various fail scenarios + + // unknown person + FakeMatrixApi.calledEndpoints.clear(); + event = ToDeviceEvent( + sender: '@test:fakeServer.notExisting', + type: 'm.room_key_request', + content: { + 'action': 'request', + 'body': { + 'algorithm': AlgorithmTypes.megolmV1AesSha2, + 'room_id': '!726s6s6q:example.com', + 'sender_key': validSenderKey, + 'session_id': validSessionId, + }, + 'request_id': 'request_a2', + 'requesting_device_id': 'OTHERDEVICE', + }); + await matrix.encryption!.keyManager.handleToDeviceEvent(event); + Logs().i(FakeMatrixApi.calledEndpoints.keys.toString()); + expect( + FakeMatrixApi.calledEndpoints.keys.any( + (k) => k.startsWith('/client/r0/sendToDevice/m.room.encrypted')), + false); + + // no body + FakeMatrixApi.calledEndpoints.clear(); + event = ToDeviceEvent( + sender: '@alice:example.com', + type: 'm.room_key_request', + content: { + 'action': 'request', + 'request_id': 'request_2', + 'requesting_device_id': 'OTHERDEVICE', + }); + await matrix.encryption!.keyManager.handleToDeviceEvent(event); + expect( + FakeMatrixApi.calledEndpoints.keys.any( + (k) => k.startsWith('/client/r0/sendToDevice/m.room.encrypted')), + false); + + // request by ourself + FakeMatrixApi.calledEndpoints.clear(); + event = ToDeviceEvent( + sender: '@alice:example.com', + type: 'm.room_key_request', + content: { + 'action': 'request', + 'body': { + 'algorithm': AlgorithmTypes.megolmV1AesSha2, + 'room_id': '!726s6s6q:example.com', + 'sender_key': validSenderKey, + 'session_id': validSessionId, + }, + 'request_id': 'request_3', + 'requesting_device_id': 'JLAFKJWSCS', + }); + await matrix.encryption!.keyManager.handleToDeviceEvent(event); + expect( + FakeMatrixApi.calledEndpoints.keys.any( + (k) => k.startsWith('/client/r0/sendToDevice/m.room.encrypted')), + false); + + // device not found + FakeMatrixApi.calledEndpoints.clear(); + event = ToDeviceEvent( + sender: '@alice:example.com', + type: 'm.room_key_request', + content: { + 'action': 'request', + 'body': { + 'algorithm': AlgorithmTypes.megolmV1AesSha2, + 'room_id': '!726s6s6q:example.com', + 'sender_key': validSenderKey, + 'session_id': validSessionId, + }, + 'request_id': 'request_4', + 'requesting_device_id': 'blubb', + }); + await matrix.encryption!.keyManager.handleToDeviceEvent(event); + expect( + FakeMatrixApi.calledEndpoints.keys.any( + (k) => k.startsWith('/client/r0/sendToDevice/m.room.encrypted')), + false); + + // unknown room + FakeMatrixApi.calledEndpoints.clear(); + event = ToDeviceEvent( + sender: '@alice:example.com', + type: 'm.room_key_request', + content: { + 'action': 'request', + 'body': { + 'algorithm': AlgorithmTypes.megolmV1AesSha2, + 'room_id': '!invalid:example.com', + 'sender_key': validSenderKey, + 'session_id': validSessionId, + }, + 'request_id': 'request_5', + 'requesting_device_id': 'OTHERDEVICE', + }); + await matrix.encryption!.keyManager.handleToDeviceEvent(event); + expect( + FakeMatrixApi.calledEndpoints.keys.any( + (k) => k.startsWith('/client/r0/sendToDevice/m.room.encrypted')), + false); + + // unknwon session + FakeMatrixApi.calledEndpoints.clear(); + event = ToDeviceEvent( + sender: '@alice:example.com', + type: 'm.room_key_request', + content: { + 'action': 'request', + 'body': { + 'algorithm': AlgorithmTypes.megolmV1AesSha2, + 'room_id': '!726s6s6q:example.com', + 'sender_key': validSenderKey, + 'session_id': 'invalid', + }, + 'request_id': 'request_6', + 'requesting_device_id': 'OTHERDEVICE', + }); + await matrix.encryption!.keyManager.handleToDeviceEvent(event); + expect( + FakeMatrixApi.calledEndpoints.keys.any( + (k) => k.startsWith('/client/r0/sendToDevice/m.room.encrypted')), + false); + + FakeMatrixApi.calledEndpoints.clear(); + await matrix.dispose(closeDatabase: true); + }); + test('Receive shared keys', () async { + if (!olmEnabled) return; + final matrix = await getClient(); + final requestRoom = matrix.getRoomById('!726s6s6q:example.com')!; + await matrix.encryption!.keyManager.request( + requestRoom, validSessionId, validSenderKey, + tryOnlineBackup: false); + + final session = (await matrix.encryption!.keyManager + .loadInboundGroupSession( + requestRoom.id, validSessionId, validSenderKey))!; + final sessionKey = session.inboundGroupSession! + .export_session(session.inboundGroupSession!.first_known_index()); + matrix.encryption!.keyManager.clearInboundGroupSessions(); + var event = ToDeviceEvent( + sender: '@alice:example.com', + type: 'm.forwarded_room_key', + content: { + 'algorithm': AlgorithmTypes.megolmV1AesSha2, + 'room_id': '!726s6s6q:example.com', + 'session_id': validSessionId, + 'session_key': sessionKey, + 'sender_key': validSenderKey, + 'forwarding_curve25519_key_chain': [], + 'sender_claimed_ed25519_key': + 'L+4+JCl8MD63dgo8z5Ta+9QAHXiANyOVSfgbHA5d3H8', + }, + encryptedContent: { + 'sender_key': 'L+4+JCl8MD63dgo8z5Ta+9QAHXiANyOVSfgbHA5d3H8', + }); + await matrix.encryption!.keyManager.handleToDeviceEvent(event); + expect( + matrix.encryption!.keyManager.getInboundGroupSession( + requestRoom.id, validSessionId, validSenderKey) != + null, + true); + + // now test a few invalid scenarios + + // request not found + matrix.encryption!.keyManager.clearInboundGroupSessions(); + event = ToDeviceEvent( + sender: '@alice:example.com', + type: 'm.forwarded_room_key', + content: { + 'algorithm': AlgorithmTypes.megolmV1AesSha2, + 'room_id': '!726s6s6q:example.com', + 'session_id': validSessionId, + 'session_key': sessionKey, + 'sender_key': validSenderKey, + 'forwarding_curve25519_key_chain': [], + 'sender_claimed_ed25519_key': + 'L+4+JCl8MD63dgo8z5Ta+9QAHXiANyOVSfgbHA5d3H8', + }, + encryptedContent: { + 'sender_key': 'L+4+JCl8MD63dgo8z5Ta+9QAHXiANyOVSfgbHA5d3H8', + }); + await matrix.encryption!.keyManager.handleToDeviceEvent(event); + expect( + matrix.encryption!.keyManager.getInboundGroupSession( + requestRoom.id, validSessionId, validSenderKey) != + null, + false); + + // unknown device + await matrix.encryption!.keyManager.request( + requestRoom, validSessionId, validSenderKey, + tryOnlineBackup: false); + matrix.encryption!.keyManager.clearInboundGroupSessions(); + event = ToDeviceEvent( + sender: '@alice:example.com', + type: 'm.forwarded_room_key', + content: { + 'algorithm': AlgorithmTypes.megolmV1AesSha2, + 'room_id': '!726s6s6q:example.com', + 'session_id': validSessionId, + 'session_key': sessionKey, + 'sender_key': validSenderKey, + 'forwarding_curve25519_key_chain': [], + 'sender_claimed_ed25519_key': + 'L+4+JCl8MD63dgo8z5Ta+9QAHXiANyOVSfgbHA5d3H8', + }, + encryptedContent: { + 'sender_key': 'invalid', + }); + await matrix.encryption!.keyManager.handleToDeviceEvent(event); + expect( + matrix.encryption!.keyManager.getInboundGroupSession( + requestRoom.id, validSessionId, validSenderKey) != + null, + false); + + // no encrypted content + await matrix.encryption!.keyManager.request( + requestRoom, validSessionId, validSenderKey, + tryOnlineBackup: false); + matrix.encryption!.keyManager.clearInboundGroupSessions(); + event = ToDeviceEvent( + sender: '@alice:example.com', + type: 'm.forwarded_room_key', + content: { + 'algorithm': AlgorithmTypes.megolmV1AesSha2, + 'room_id': '!726s6s6q:example.com', + 'session_id': validSessionId, + 'session_key': sessionKey, + 'sender_key': validSenderKey, + 'forwarding_curve25519_key_chain': [], + 'sender_claimed_ed25519_key': + 'L+4+JCl8MD63dgo8z5Ta+9QAHXiANyOVSfgbHA5d3H8', + }); + await matrix.encryption!.keyManager.handleToDeviceEvent(event); + expect( + matrix.encryption!.keyManager.getInboundGroupSession( + requestRoom.id, validSessionId, validSenderKey) != + null, + false); + + // There is a non awaiting setInboundGroupSession call on the database + await Future.delayed(Duration(seconds: 1)); + + await matrix.dispose(closeDatabase: true); + }); + }); +} diff --git a/test/encryption/key_verification_test.dart b/test/encryption/key_verification_test.dart new file mode 100644 index 0000000..770af7a --- /dev/null +++ b/test/encryption/key_verification_test.dart @@ -0,0 +1,486 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:convert'; + +import 'package:matrix/matrix.dart'; +import 'package:matrix/encryption.dart'; + +import 'package:test/test.dart'; +import 'package:olm/olm.dart' as olm; + +import '../fake_client.dart'; +import '../fake_database.dart'; +import '../fake_matrix_api.dart'; + +class MockSSSS extends SSSS { + MockSSSS(Encryption encryption) : super(encryption); + + bool requestedSecrets = false; + @override + Future maybeRequestAll([List? devices]) async { + requestedSecrets = true; + final handle = open(); + await handle.unlock(recoveryKey: ssssKey); + await handle.maybeCacheAll(); + } +} + +EventUpdate getLastSentEvent(KeyVerification req) { + final entry = FakeMatrixApi.calledEndpoints.entries + .firstWhere((p) => p.key.contains('/send/')); + final type = entry.key.split('/')[6]; + final content = json.decode(entry.value.first); + return EventUpdate( + content: { + 'event_id': req.transactionId, + 'type': type, + 'content': content, + 'origin_server_ts': DateTime.now().millisecondsSinceEpoch, + 'sender': req.client.userID, + }, + type: EventUpdateType.timeline, + roomID: req.room!.id, + ); +} + +void main() { + /// All Tests related to the ChatTime + group('Key Verification', () { + Logs().level = Level.error; + var olmEnabled = true; + + // key @othertest:fakeServer.notExisting + const otherPickledOlmAccount = + 'VWhVApbkcilKAEGppsPDf9nNVjaK8/IxT3asSR0sYg0S5KgbfE8vXEPwoiKBX2cEvwX3OessOBOkk+ZE7TTbjlrh/KEd31p8Wo+47qj0AP+Ky+pabnhi+/rTBvZy+gfzTqUfCxZrkzfXI9Op4JnP6gYmy7dVX2lMYIIs9WCO1jcmIXiXum5jnfXu1WLfc7PZtO2hH+k9CDKosOFaXRBmsu8k/BGXPSoWqUpvu6WpEG9t5STk4FeAzA'; + + late Client client1; + late Client client2; + + test('setupClient', () async { + try { + await olm.init(); + olm.get_library_version(); + } catch (e) { + olmEnabled = false; + Logs().w('[LibOlm] Failed to load LibOlm', e); + } + Logs().i('[LibOlm] Enabled: $olmEnabled'); + if (!olmEnabled) return; + + client1 = await getClient(); + client2 = Client( + 'othertestclient', + httpClient: FakeMatrixApi(), + databaseBuilder: getDatabase, + ); + await client2.checkHomeserver('https://fakeserver.notexisting', + checkWellKnown: false); + await client2.init( + newToken: 'abc', + newUserID: '@othertest:fakeServer.notExisting', + newHomeserver: client2.homeserver, + newDeviceName: 'Text Matrix Client', + newDeviceID: 'FOXDEVICE', + newOlmAccount: otherPickledOlmAccount, + ); + await Future.delayed(Duration(milliseconds: 10)); + client1.verificationMethods = { + KeyVerificationMethod.emoji, + KeyVerificationMethod.numbers + }; + client2.verificationMethods = { + KeyVerificationMethod.emoji, + KeyVerificationMethod.numbers + }; + }); + + test('Run emoji / number verification', () async { + if (!olmEnabled) return; + // for a full run we test in-room verification in a cleartext room + // because then we can easily intercept the payloads and inject in the other client + FakeMatrixApi.calledEndpoints.clear(); + // make sure our master key is *not* verified to not triger SSSS for now + client1.userDeviceKeys[client1.userID]!.masterKey! + .setDirectVerified(false); + final req1 = + await client1.userDeviceKeys[client2.userID]!.startVerification( + newDirectChatEnableEncryption: false, + ); + var evt = getLastSentEvent(req1); + expect(req1.state, KeyVerificationState.waitingAccept); + + late KeyVerification req2; + final sub = client2.onKeyVerificationRequest.stream.listen((req) { + req2 = req; + }); + await client2.encryption!.keyVerificationManager.handleEventUpdate(evt); + await Future.delayed(Duration(milliseconds: 10)); + await sub.cancel(); + + expect( + client2.encryption!.keyVerificationManager + .getRequest(req2.transactionId!), + req2); + + // send ready + FakeMatrixApi.calledEndpoints.clear(); + await req2.acceptVerification(); + evt = getLastSentEvent(req2); + expect(req2.state, KeyVerificationState.waitingAccept); + + // send start + FakeMatrixApi.calledEndpoints.clear(); + await client1.encryption!.keyVerificationManager.handleEventUpdate(evt); + evt = getLastSentEvent(req1); + + // send accept + FakeMatrixApi.calledEndpoints.clear(); + await client2.encryption!.keyVerificationManager.handleEventUpdate(evt); + evt = getLastSentEvent(req2); + + // send key + FakeMatrixApi.calledEndpoints.clear(); + await client1.encryption!.keyVerificationManager.handleEventUpdate(evt); + evt = getLastSentEvent(req1); + + // send key + FakeMatrixApi.calledEndpoints.clear(); + await client2.encryption!.keyVerificationManager.handleEventUpdate(evt); + evt = getLastSentEvent(req2); + + // receive last key + FakeMatrixApi.calledEndpoints.clear(); + await client1.encryption!.keyVerificationManager.handleEventUpdate(evt); + + // compare emoji + expect(req1.state, KeyVerificationState.askSas); + expect(req2.state, KeyVerificationState.askSas); + expect(req1.sasTypes[0], 'emoji'); + expect(req1.sasTypes[1], 'decimal'); + expect(req2.sasTypes[0], 'emoji'); + expect(req2.sasTypes[1], 'decimal'); + // compare emoji + final emoji1 = req1.sasEmojis; + final emoji2 = req2.sasEmojis; + for (var i = 0; i < 7; i++) { + expect(emoji1[i].emoji, emoji2[i].emoji); + expect(emoji1[i].name, emoji2[i].name); + } + // compare numbers + final numbers1 = req1.sasNumbers; + final numbers2 = req2.sasNumbers; + for (var i = 0; i < 3; i++) { + expect(numbers1[i], numbers2[i]); + } + + // alright, they match + + // send mac + FakeMatrixApi.calledEndpoints.clear(); + await req1.acceptSas(); + evt = getLastSentEvent(req1); + await client2.encryption!.keyVerificationManager.handleEventUpdate(evt); + expect(req1.state, KeyVerificationState.waitingSas); + + // send mac + FakeMatrixApi.calledEndpoints.clear(); + await req2.acceptSas(); + evt = getLastSentEvent(req2); + await client1.encryption!.keyVerificationManager.handleEventUpdate(evt); + + expect(req1.state, KeyVerificationState.done); + expect(req2.state, KeyVerificationState.done); + expect( + client1.userDeviceKeys[client2.userID]?.deviceKeys[client2.deviceID] + ?.directVerified, + true); + expect( + client2.userDeviceKeys[client1.userID]?.deviceKeys[client1.deviceID] + ?.directVerified, + true); + await client1.encryption!.keyVerificationManager.cleanup(); + await client2.encryption!.keyVerificationManager.cleanup(); + }); + + test('ask SSSS start', () async { + if (!olmEnabled) return; + client1.userDeviceKeys[client1.userID]!.masterKey! + .setDirectVerified(true); + await client1.encryption!.ssss.clearCache(); + final req1 = await client1.userDeviceKeys[client2.userID]! + .startVerification(newDirectChatEnableEncryption: false); + expect(req1.state, KeyVerificationState.askSSSS); + await req1.openSSSS(recoveryKey: ssssKey); + await Future.delayed(Duration(seconds: 1)); + expect(req1.state, KeyVerificationState.waitingAccept); + + await req1.cancel(); + await client1.encryption!.keyVerificationManager.cleanup(); + }); + + test('ask SSSS end', () async { + if (!olmEnabled) return; + FakeMatrixApi.calledEndpoints.clear(); + // make sure our master key is *not* verified to not triger SSSS for now + client1.userDeviceKeys[client1.userID]!.masterKey! + .setDirectVerified(false); + // the other one has to have their master key verified to trigger asking for ssss + client2.userDeviceKeys[client2.userID]!.masterKey! + .setDirectVerified(true); + final req1 = await client1.userDeviceKeys[client2.userID]! + .startVerification(newDirectChatEnableEncryption: false); + var evt = getLastSentEvent(req1); + expect(req1.state, KeyVerificationState.waitingAccept); + + late KeyVerification req2; + final sub = client2.onKeyVerificationRequest.stream.listen((req) { + req2 = req; + }); + await client2.encryption!.keyVerificationManager.handleEventUpdate(evt); + await Future.delayed(Duration(milliseconds: 10)); + await sub.cancel(); + + // send ready + FakeMatrixApi.calledEndpoints.clear(); + await req2.acceptVerification(); + evt = getLastSentEvent(req2); + expect(req2.state, KeyVerificationState.waitingAccept); + + // send start + FakeMatrixApi.calledEndpoints.clear(); + await client1.encryption!.keyVerificationManager.handleEventUpdate(evt); + evt = getLastSentEvent(req1); + + // send accept + FakeMatrixApi.calledEndpoints.clear(); + await client2.encryption!.keyVerificationManager.handleEventUpdate(evt); + evt = getLastSentEvent(req2); + + // send key + FakeMatrixApi.calledEndpoints.clear(); + await client1.encryption!.keyVerificationManager.handleEventUpdate(evt); + evt = getLastSentEvent(req1); + + // send key + FakeMatrixApi.calledEndpoints.clear(); + await client2.encryption!.keyVerificationManager.handleEventUpdate(evt); + evt = getLastSentEvent(req2); + + // receive last key + FakeMatrixApi.calledEndpoints.clear(); + await client1.encryption!.keyVerificationManager.handleEventUpdate(evt); + + // compare emoji + expect(req1.state, KeyVerificationState.askSas); + expect(req2.state, KeyVerificationState.askSas); + // compare emoji + final emoji1 = req1.sasEmojis; + final emoji2 = req2.sasEmojis; + for (var i = 0; i < 7; i++) { + expect(emoji1[i].emoji, emoji2[i].emoji); + expect(emoji1[i].name, emoji2[i].name); + } + // compare numbers + final numbers1 = req1.sasNumbers; + final numbers2 = req2.sasNumbers; + for (var i = 0; i < 3; i++) { + expect(numbers1[i], numbers2[i]); + } + + // alright, they match + client1.userDeviceKeys[client1.userID]!.masterKey! + .setDirectVerified(true); + await client1.encryption!.ssss.clearCache(); + + // send mac + FakeMatrixApi.calledEndpoints.clear(); + await req1.acceptSas(); + evt = getLastSentEvent(req1); + await client2.encryption!.keyVerificationManager.handleEventUpdate(evt); + expect(req1.state, KeyVerificationState.waitingSas); + + // send mac + FakeMatrixApi.calledEndpoints.clear(); + await req2.acceptSas(); + evt = getLastSentEvent(req2); + await client1.encryption!.keyVerificationManager.handleEventUpdate(evt); + + expect(req1.state, KeyVerificationState.askSSSS); + expect(req2.state, KeyVerificationState.done); + + await req1.openSSSS(recoveryKey: ssssKey); + await Future.delayed(Duration(milliseconds: 10)); + expect(req1.state, KeyVerificationState.done); + + client1.encryption!.ssss = MockSSSS(client1.encryption!); + (client1.encryption!.ssss as MockSSSS).requestedSecrets = false; + await client1.encryption!.ssss.clearCache(); + await req1.maybeRequestSSSSSecrets(); + await Future.delayed(Duration(milliseconds: 10)); + expect((client1.encryption!.ssss as MockSSSS).requestedSecrets, true); + // delay for 12 seconds to be sure no other tests clear the ssss cache + await Future.delayed(Duration(seconds: 12)); + + await client1.encryption!.keyVerificationManager.cleanup(); + await client2.encryption!.keyVerificationManager.cleanup(); + }); + + test('reject verification', () async { + if (!olmEnabled) return; + FakeMatrixApi.calledEndpoints.clear(); + // make sure our master key is *not* verified to not triger SSSS for now + client1.userDeviceKeys[client1.userID]!.masterKey! + .setDirectVerified(false); + final req1 = await client1.userDeviceKeys[client2.userID]! + .startVerification(newDirectChatEnableEncryption: false); + var evt = getLastSentEvent(req1); + expect(req1.state, KeyVerificationState.waitingAccept); + + late KeyVerification req2; + final sub = client2.onKeyVerificationRequest.stream.listen((req) { + req2 = req; + }); + await client2.encryption!.keyVerificationManager.handleEventUpdate(evt); + await Future.delayed(Duration(milliseconds: 10)); + await sub.cancel(); + + FakeMatrixApi.calledEndpoints.clear(); + await req2.rejectVerification(); + evt = getLastSentEvent(req2); + await client1.encryption!.keyVerificationManager.handleEventUpdate(evt); + expect(req1.state, KeyVerificationState.error); + expect(req2.state, KeyVerificationState.error); + + await client1.encryption!.keyVerificationManager.cleanup(); + await client2.encryption!.keyVerificationManager.cleanup(); + }); + + test('reject sas', () async { + if (!olmEnabled) return; + FakeMatrixApi.calledEndpoints.clear(); + // make sure our master key is *not* verified to not triger SSSS for now + client1.userDeviceKeys[client1.userID]!.masterKey! + .setDirectVerified(false); + final req1 = await client1.userDeviceKeys[client2.userID]! + .startVerification(newDirectChatEnableEncryption: false); + var evt = getLastSentEvent(req1); + expect(req1.state, KeyVerificationState.waitingAccept); + + late KeyVerification req2; + final sub = client2.onKeyVerificationRequest.stream.listen((req) { + req2 = req; + }); + await client2.encryption!.keyVerificationManager.handleEventUpdate(evt); + await Future.delayed(Duration(milliseconds: 10)); + await sub.cancel(); + + // send ready + FakeMatrixApi.calledEndpoints.clear(); + await req2.acceptVerification(); + evt = getLastSentEvent(req2); + expect(req2.state, KeyVerificationState.waitingAccept); + + // send start + FakeMatrixApi.calledEndpoints.clear(); + await client1.encryption!.keyVerificationManager.handleEventUpdate(evt); + evt = getLastSentEvent(req1); + + // send accept + FakeMatrixApi.calledEndpoints.clear(); + await client2.encryption!.keyVerificationManager.handleEventUpdate(evt); + evt = getLastSentEvent(req2); + + // send key + FakeMatrixApi.calledEndpoints.clear(); + await client1.encryption!.keyVerificationManager.handleEventUpdate(evt); + evt = getLastSentEvent(req1); + + // send key + FakeMatrixApi.calledEndpoints.clear(); + await client2.encryption!.keyVerificationManager.handleEventUpdate(evt); + evt = getLastSentEvent(req2); + + // receive last key + FakeMatrixApi.calledEndpoints.clear(); + await client1.encryption!.keyVerificationManager.handleEventUpdate(evt); + + await req1.acceptSas(); + FakeMatrixApi.calledEndpoints.clear(); + await req2.rejectSas(); + evt = getLastSentEvent(req2); + await client1.encryption!.keyVerificationManager.handleEventUpdate(evt); + expect(req1.state, KeyVerificationState.error); + expect(req2.state, KeyVerificationState.error); + + await client1.encryption!.keyVerificationManager.cleanup(); + await client2.encryption!.keyVerificationManager.cleanup(); + }); + + test('other device accepted', () async { + if (!olmEnabled) return; + FakeMatrixApi.calledEndpoints.clear(); + // make sure our master key is *not* verified to not triger SSSS for now + client1.userDeviceKeys[client1.userID]!.masterKey! + .setDirectVerified(false); + final req1 = await client1.userDeviceKeys[client2.userID]! + .startVerification(newDirectChatEnableEncryption: false); + final evt = getLastSentEvent(req1); + expect(req1.state, KeyVerificationState.waitingAccept); + + late KeyVerification req2; + final sub = client2.onKeyVerificationRequest.stream.listen((req) { + req2 = req; + }); + await client2.encryption!.keyVerificationManager.handleEventUpdate(evt); + await Future.delayed(Duration(milliseconds: 10)); + await sub.cancel(); + + await client2.encryption!.keyVerificationManager + .handleEventUpdate(EventUpdate( + content: { + 'event_id': req2.transactionId, + 'type': 'm.key.verification.ready', + 'content': { + 'methods': ['m.sas.v1'], + 'from_device': 'SOMEOTHERDEVICE', + 'm.relates_to': { + 'rel_type': 'm.reference', + 'event_id': req2.transactionId, + }, + }, + 'origin_server_ts': DateTime.now().millisecondsSinceEpoch, + 'sender': client2.userID, + }, + type: EventUpdateType.timeline, + roomID: req2.room!.id, + )); + expect(req2.state, KeyVerificationState.error); + + await req2.cancel(); + await client1.encryption!.keyVerificationManager.cleanup(); + await client2.encryption!.keyVerificationManager.cleanup(); + }); + + test('dispose client', () async { + if (!olmEnabled) return; + await client1.dispose(closeDatabase: true); + await client2.dispose(closeDatabase: true); + }); + }); +} diff --git a/test/encryption/olm_manager_test.dart b/test/encryption/olm_manager_test.dart new file mode 100644 index 0000000..40ebc08 --- /dev/null +++ b/test/encryption/olm_manager_test.dart @@ -0,0 +1,264 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:convert'; +import 'package:matrix/matrix.dart'; + +import 'package:test/test.dart'; +import 'package:olm/olm.dart' as olm; +import 'package:matrix/encryption/utils/json_signature_check_extension.dart'; + +import '../fake_client.dart'; +import '../fake_matrix_api.dart'; + +void main() { + group('Olm Manager', () { + Logs().level = Level.error; + var olmEnabled = true; + + late Client client; + + test('setupClient', () async { + try { + await olm.init(); + olm.get_library_version(); + } catch (e) { + olmEnabled = false; + Logs().w('[LibOlm] Failed to load LibOlm', e); + } + Logs().i('[LibOlm] Enabled: $olmEnabled'); + if (!olmEnabled) return; + + client = await getClient(); + }); + + test('signatures', () async { + if (!olmEnabled) return; + final payload = { + 'fox': 'floof', + }; + final signedPayload = client.encryption!.olmManager.signJson(payload); + expect( + signedPayload.checkJsonSignature( + client.fingerprintKey, client.userID!, client.deviceID!), + true); + }); + + test('uploadKeys', () async { + if (!olmEnabled) return; + FakeMatrixApi.calledEndpoints.clear(); + final res = await client.encryption!.olmManager + .uploadKeys(uploadDeviceKeys: true); + expect(res, true); + var sent = json.decode( + FakeMatrixApi.calledEndpoints['/client/r0/keys/upload']!.first); + expect(sent['device_keys'] != null, true); + expect(sent['one_time_keys'] != null, true); + expect(sent['one_time_keys'].keys.length, 66); + expect(sent['fallback_keys'] != null, true); + expect(sent['fallback_keys'].keys.length, 1); + FakeMatrixApi.calledEndpoints.clear(); + await client.encryption!.olmManager.uploadKeys(); + sent = json.decode( + FakeMatrixApi.calledEndpoints['/client/r0/keys/upload']!.first); + expect(sent['device_keys'] != null, false); + expect(sent['fallback_keys'].keys.length, 1); + FakeMatrixApi.calledEndpoints.clear(); + await client.encryption!.olmManager + .uploadKeys(oldKeyCount: 20, unusedFallbackKey: true); + sent = json.decode( + FakeMatrixApi.calledEndpoints['/client/r0/keys/upload']!.first); + expect(sent['one_time_keys'].keys.length, 46); + expect(sent['fallback_keys'].keys.length, 0); + }); + + test('handleDeviceOneTimeKeysCount', () async { + if (!olmEnabled) return; + FakeMatrixApi.calledEndpoints.clear(); + client.encryption!.olmManager + .handleDeviceOneTimeKeysCount({'signed_curve25519': 20}, null); + await Future.delayed(Duration(milliseconds: 50)); + expect( + FakeMatrixApi.calledEndpoints.containsKey('/client/r0/keys/upload'), + true); + + FakeMatrixApi.calledEndpoints.clear(); + client.encryption!.olmManager + .handleDeviceOneTimeKeysCount({'signed_curve25519': 70}, null); + await Future.delayed(Duration(milliseconds: 50)); + expect( + FakeMatrixApi.calledEndpoints.containsKey('/client/r0/keys/upload'), + false); + + FakeMatrixApi.calledEndpoints.clear(); + client.encryption!.olmManager.handleDeviceOneTimeKeysCount(null, []); + await Future.delayed(Duration(milliseconds: 50)); + expect( + FakeMatrixApi.calledEndpoints.containsKey('/client/r0/keys/upload'), + true); + + // this will upload keys because we assume the key count is 0, if the server doesn't send one + FakeMatrixApi.calledEndpoints.clear(); + client.encryption!.olmManager + .handleDeviceOneTimeKeysCount(null, ['signed_curve25519']); + await Future.delayed(Duration(milliseconds: 50)); + expect( + FakeMatrixApi.calledEndpoints.containsKey('/client/r0/keys/upload'), + true); + }); + + test('restoreOlmSession', () async { + if (!olmEnabled) return; + client.encryption!.olmManager.olmSessions.clear(); + await client.encryption!.olmManager + .restoreOlmSession(client.userID!, client.identityKey); + expect(client.encryption!.olmManager.olmSessions.length, 1); + + client.encryption!.olmManager.olmSessions.clear(); + await client.encryption!.olmManager + .restoreOlmSession(client.userID!, 'invalid'); + expect(client.encryption!.olmManager.olmSessions.length, 0); + + client.encryption!.olmManager.olmSessions.clear(); + await client.encryption!.olmManager + .restoreOlmSession('invalid', client.identityKey); + expect(client.encryption!.olmManager.olmSessions.length, 0); + }); + + test('startOutgoingOlmSessions', () async { + if (!olmEnabled) return; + // start an olm session.....with ourself! + client.encryption!.olmManager.olmSessions.clear(); + await client.encryption!.olmManager.startOutgoingOlmSessions([ + client.userDeviceKeys[client.userID!]!.deviceKeys[client.deviceID]! + ]); + expect( + client.encryption!.olmManager.olmSessions + .containsKey(client.identityKey), + true); + }); + + test('replay to_device events', () async { + if (!olmEnabled) return; + final userId = '@alice:example.com'; + final deviceId = 'JLAFKJWSCS'; + final senderKey = 'L+4+JCl8MD63dgo8z5Ta+9QAHXiANyOVSfgbHA5d3H8'; + FakeMatrixApi.calledEndpoints.clear(); + await client.database!.setLastSentMessageUserDeviceKey( + json.encode({ + 'type': 'm.foxies', + 'content': { + 'floof': 'foxhole', + }, + }), + userId, + deviceId); + var event = ToDeviceEvent( + sender: userId, + type: 'm.dummy', + content: {}, + encryptedContent: { + 'sender_key': senderKey, + }, + ); + await client.encryption!.olmManager.handleToDeviceEvent(event); + expect( + FakeMatrixApi.calledEndpoints.keys.any( + (k) => k.startsWith('/client/r0/sendToDevice/m.room.encrypted')), + true); + + // fail scenarios + + // not encrypted + FakeMatrixApi.calledEndpoints.clear(); + await client.database!.setLastSentMessageUserDeviceKey( + json.encode({ + 'type': 'm.foxies', + 'content': { + 'floof': 'foxhole', + }, + }), + userId, + deviceId); + event = ToDeviceEvent( + sender: userId, + type: 'm.dummy', + content: {}, + encryptedContent: null, + ); + await client.encryption!.olmManager.handleToDeviceEvent(event); + expect( + FakeMatrixApi.calledEndpoints.keys.any( + (k) => k.startsWith('/client/r0/sendToDevice/m.room.encrypted')), + false); + + // device not found + FakeMatrixApi.calledEndpoints.clear(); + await client.database!.setLastSentMessageUserDeviceKey( + json.encode({ + 'type': 'm.foxies', + 'content': { + 'floof': 'foxhole', + }, + }), + userId, + deviceId); + event = ToDeviceEvent( + sender: userId, + type: 'm.dummy', + content: {}, + encryptedContent: { + 'sender_key': 'invalid', + }, + ); + await client.encryption!.olmManager.handleToDeviceEvent(event); + expect( + FakeMatrixApi.calledEndpoints.keys.any( + (k) => k.startsWith('/client/r0/sendToDevice/m.room.encrypted')), + false); + + // don't replay if the last event is m.dummy itself + FakeMatrixApi.calledEndpoints.clear(); + await client.database!.setLastSentMessageUserDeviceKey( + json.encode({ + 'type': 'm.dummy', + 'content': {}, + }), + userId, + deviceId); + event = ToDeviceEvent( + sender: userId, + type: 'm.dummy', + content: {}, + encryptedContent: { + 'sender_key': senderKey, + }, + ); + await client.encryption!.olmManager.handleToDeviceEvent(event); + expect( + FakeMatrixApi.calledEndpoints.keys.any( + (k) => k.startsWith('/client/r0/sendToDevice/m.room.encrypted')), + false); + }); + + test('dispose client', () async { + if (!olmEnabled) return; + await client.dispose(closeDatabase: true); + }); + }); +} diff --git a/test/encryption/online_key_backup_test.dart b/test/encryption/online_key_backup_test.dart new file mode 100644 index 0000000..48f03ef --- /dev/null +++ b/test/encryption/online_key_backup_test.dart @@ -0,0 +1,124 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:convert'; + +import 'package:matrix/matrix.dart'; + +import 'package:test/test.dart'; +import 'package:olm/olm.dart' as olm; + +import '../fake_client.dart'; +import '../fake_matrix_api.dart'; + +void main() { + group('Online Key Backup', () { + Logs().level = Level.error; + var olmEnabled = true; + + late Client client; + + final roomId = '!726s6s6q:example.com'; + final sessionId = 'ciM/JWTPrmiWPPZNkRLDPQYf9AW/I46bxyLSr+Bx5oU'; + final senderKey = 'JBG7ZaPn54OBC7TuIEiylW3BZ+7WcGQhFBPB9pogbAg'; + + test('setupClient', () async { + try { + await olm.init(); + olm.get_library_version(); + } catch (e) { + olmEnabled = false; + Logs().w('[LibOlm] Failed to load LibOlm', e); + } + Logs().i('[LibOlm] Enabled: $olmEnabled'); + if (!olmEnabled) return; + + client = await getClient(); + }); + + test('basic things', () async { + if (!olmEnabled) return; + expect(client.encryption!.keyManager.enabled, true); + expect(await client.encryption!.keyManager.isCached(), false); + final handle = client.encryption!.ssss.open(); + await handle.unlock(recoveryKey: ssssKey); + await handle.maybeCacheAll(); + expect(await client.encryption!.keyManager.isCached(), true); + }); + + test('load key', () async { + if (!olmEnabled) return; + client.encryption!.keyManager.clearInboundGroupSessions(); + await client.encryption!.keyManager + .request(client.getRoomById(roomId)!, sessionId, senderKey); + expect( + client.encryption!.keyManager + .getInboundGroupSession(roomId, sessionId, senderKey) != + null, + true); + }); + + test('upload key', () async { + if (!olmEnabled) return; + final session = olm.OutboundGroupSession(); + session.create(); + final inbound = olm.InboundGroupSession(); + inbound.create(session.session_key()); + final senderKey = client.identityKey; + final roomId = '!someroom:example.org'; + final sessionId = inbound.session_id(); + // set a payload... + final sessionPayload = { + 'algorithm': AlgorithmTypes.megolmV1AesSha2, + 'room_id': roomId, + 'forwarding_curve25519_key_chain': [client.identityKey], + 'session_id': sessionId, + 'session_key': inbound.export_session(1), + 'sender_key': senderKey, + 'sender_claimed_ed25519_key': client.fingerprintKey, + }; + FakeMatrixApi.calledEndpoints.clear(); + client.encryption!.keyManager.setInboundGroupSession( + roomId, sessionId, senderKey, sessionPayload, + forwarded: true); + await Future.delayed(Duration(milliseconds: 500)); + var dbSessions = await client.database!.getInboundGroupSessionsToUpload(); + expect(dbSessions.isNotEmpty, true); + await client.encryption!.keyManager.backgroundTasks(); + final payload = FakeMatrixApi + .calledEndpoints['/client/unstable/room_keys/keys?version=5']!.first; + dbSessions = await client.database!.getInboundGroupSessionsToUpload(); + expect(dbSessions.isEmpty, true); + + final onlineKeys = RoomKeys.fromJson(json.decode(payload)); + client.encryption!.keyManager.clearInboundGroupSessions(); + var ret = client.encryption!.keyManager + .getInboundGroupSession(roomId, sessionId, senderKey); + expect(ret, null); + await client.encryption!.keyManager.loadFromResponse(onlineKeys); + ret = client.encryption!.keyManager + .getInboundGroupSession(roomId, sessionId, senderKey); + expect(ret != null, true); + }); + + test('dispose client', () async { + if (!olmEnabled) return; + await client.dispose(closeDatabase: false); + }); + }); +} diff --git a/test/encryption/ssss_test.dart b/test/encryption/ssss_test.dart new file mode 100644 index 0000000..093f8ca --- /dev/null +++ b/test/encryption/ssss_test.dart @@ -0,0 +1,501 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:typed_data'; +import 'dart:convert'; +import 'dart:math'; + +import 'package:matrix/matrix.dart'; +import 'package:matrix/encryption.dart'; + +import 'package:test/test.dart'; +import 'package:olm/olm.dart' as olm; + +import '../fake_client.dart'; +import '../fake_matrix_api.dart'; + +Uint8List secureRandomBytes(int len) { + final rng = Random.secure(); + final list = Uint8List(len); + list.setAll(0, Iterable.generate(list.length, (i) => rng.nextInt(256))); + return list; +} + +class MockSSSS extends SSSS { + MockSSSS(Encryption encryption) : super(encryption); + + bool requestedSecrets = false; + @override + Future maybeRequestAll([List? devices]) async { + requestedSecrets = true; + final handle = open(); + await handle.unlock(recoveryKey: ssssKey); + await handle.maybeCacheAll(); + } +} + +void main() { + group('SSSS', () { + Logs().level = Level.error; + var olmEnabled = true; + + late Client client; + + test('setupClient', () async { + try { + await olm.init(); + olm.get_library_version(); + } catch (e) { + olmEnabled = false; + Logs().w('[LibOlm] Failed to load LibOlm', e); + } + Logs().i('[LibOlm] Enabled: $olmEnabled'); + if (!olmEnabled) return; + + client = await getClient(); + }); + + test('basic things', () async { + if (!olmEnabled) return; + expect(client.encryption!.ssss.defaultKeyId, + '0FajDWYaM6wQ4O60OZnLvwZfsBNu4Bu3'); + }); + + test('encrypt / decrypt', () async { + if (!olmEnabled) return; + final key = Uint8List.fromList(secureRandomBytes(32)); + + final enc = await SSSS.encryptAes('secret foxies', key, 'name'); + final dec = await SSSS.decryptAes(enc, key, 'name'); + expect(dec, 'secret foxies'); + }); + + test('store', () async { + if (!olmEnabled) return; + final handle = client.encryption!.ssss.open(); + var failed = false; + try { + await handle.unlock(passphrase: 'invalid'); + } catch (_) { + failed = true; + } + expect(failed, true); + expect(handle.isUnlocked, false); + failed = false; + try { + await handle.unlock(recoveryKey: 'invalid'); + } catch (_) { + failed = true; + } + expect(failed, true); + expect(handle.isUnlocked, false); + await handle.unlock(passphrase: ssssPassphrase); + await handle.unlock(recoveryKey: ssssKey); + expect(handle.isUnlocked, true); + FakeMatrixApi.calledEndpoints.clear(); + await handle.store('best animal', 'foxies'); + // alright, since we don't properly sync we will manually have to update + // account_data for this test + final content = FakeMatrixApi + .calledEndpoints[ + '/client/r0/user/%40test%3AfakeServer.notExisting/account_data/best%20animal']! + .first; + client.accountData['best animal'] = BasicEvent.fromJson({ + 'type': 'best animal', + 'content': json.decode(content), + }); + expect(await handle.getStored('best animal'), 'foxies'); + }); + + test('encode / decode recovery key', () async { + if (!olmEnabled) return; + final key = Uint8List.fromList(secureRandomBytes(32)); + final encoded = SSSS.encodeRecoveryKey(key); + var decoded = SSSS.decodeRecoveryKey(encoded); + expect(key, decoded); + + decoded = SSSS.decodeRecoveryKey(encoded + ' \n\t'); + expect(key, decoded); + + final handle = client.encryption!.ssss.open(); + await handle.unlock(recoveryKey: ssssKey); + expect(handle.recoveryKey, ssssKey); + }); + + test('cache', () async { + if (!olmEnabled) return; + await client.encryption!.ssss.clearCache(); + final handle = + client.encryption!.ssss.open(EventTypes.CrossSigningSelfSigning); + await handle.unlock(recoveryKey: ssssKey, postUnlock: false); + expect( + (await client.encryption!.ssss + .getCached(EventTypes.CrossSigningSelfSigning)) != + null, + false); + expect( + (await client.encryption!.ssss + .getCached(EventTypes.CrossSigningUserSigning)) != + null, + false); + await handle.getStored(EventTypes.CrossSigningSelfSigning); + expect( + (await client.encryption!.ssss + .getCached(EventTypes.CrossSigningSelfSigning)) != + null, + true); + await handle.maybeCacheAll(); + expect( + (await client.encryption!.ssss + .getCached(EventTypes.CrossSigningUserSigning)) != + null, + true); + expect( + (await client.encryption!.ssss.getCached(EventTypes.MegolmBackup)) != + null, + true); + }); + + test('postUnlock', () async { + if (!olmEnabled) return; + await client.encryption!.ssss.clearCache(); + client.userDeviceKeys[client.userID!]!.masterKey! + .setDirectVerified(false); + final handle = + client.encryption!.ssss.open(EventTypes.CrossSigningSelfSigning); + await handle.unlock(recoveryKey: ssssKey); + expect( + (await client.encryption!.ssss + .getCached(EventTypes.CrossSigningSelfSigning)) != + null, + true); + expect( + (await client.encryption!.ssss + .getCached(EventTypes.CrossSigningUserSigning)) != + null, + true); + expect( + (await client.encryption!.ssss.getCached(EventTypes.MegolmBackup)) != + null, + true); + expect(client.userDeviceKeys[client.userID!]!.masterKey!.directVerified, + true); + }); + + test('make share requests', () async { + if (!olmEnabled) return; + final key = + client.userDeviceKeys[client.userID!]!.deviceKeys['OTHERDEVICE']!; + key.setDirectVerified(true); + FakeMatrixApi.calledEndpoints.clear(); + await client.encryption!.ssss.request('some.type', [key]); + expect( + FakeMatrixApi.calledEndpoints.keys.any( + (k) => k.startsWith('/client/r0/sendToDevice/m.room.encrypted')), + true); + }); + + test('answer to share requests', () async { + if (!olmEnabled) return; + var event = ToDeviceEvent( + sender: client.userID!, + type: 'm.secret.request', + content: { + 'action': 'request', + 'requesting_device_id': 'OTHERDEVICE', + 'name': EventTypes.CrossSigningSelfSigning, + 'request_id': '1', + }, + ); + FakeMatrixApi.calledEndpoints.clear(); + await client.encryption!.ssss.handleToDeviceEvent(event); + expect( + FakeMatrixApi.calledEndpoints.keys.any( + (k) => k.startsWith('/client/r0/sendToDevice/m.room.encrypted')), + true); + + // now test some fail scenarios + + // not by us + event = ToDeviceEvent( + sender: '@someotheruser:example.org', + type: 'm.secret.request', + content: { + 'action': 'request', + 'requesting_device_id': 'OTHERDEVICE', + 'name': EventTypes.CrossSigningSelfSigning, + 'request_id': '1', + }, + ); + FakeMatrixApi.calledEndpoints.clear(); + await client.encryption!.ssss.handleToDeviceEvent(event); + expect( + FakeMatrixApi.calledEndpoints.keys.any( + (k) => k.startsWith('/client/r0/sendToDevice/m.room.encrypted')), + false); + + // secret not cached + event = ToDeviceEvent( + sender: client.userID!, + type: 'm.secret.request', + content: { + 'action': 'request', + 'requesting_device_id': 'OTHERDEVICE', + 'name': 'm.unknown.secret', + 'request_id': '1', + }, + ); + FakeMatrixApi.calledEndpoints.clear(); + await client.encryption!.ssss.handleToDeviceEvent(event); + expect( + FakeMatrixApi.calledEndpoints.keys.any( + (k) => k.startsWith('/client/r0/sendToDevice/m.room.encrypted')), + false); + + // is a cancelation + event = ToDeviceEvent( + sender: client.userID!, + type: 'm.secret.request', + content: { + 'action': 'request_cancellation', + 'requesting_device_id': 'OTHERDEVICE', + 'name': EventTypes.CrossSigningSelfSigning, + 'request_id': '1', + }, + ); + FakeMatrixApi.calledEndpoints.clear(); + await client.encryption!.ssss.handleToDeviceEvent(event); + expect( + FakeMatrixApi.calledEndpoints.keys.any( + (k) => k.startsWith('/client/r0/sendToDevice/m.room.encrypted')), + false); + + // device not verified + final key = + client.userDeviceKeys[client.userID!]!.deviceKeys['OTHERDEVICE']!; + key.setDirectVerified(false); + client.userDeviceKeys[client.userID!]!.masterKey! + .setDirectVerified(false); + event = ToDeviceEvent( + sender: client.userID!, + type: 'm.secret.request', + content: { + 'action': 'request', + 'requesting_device_id': 'OTHERDEVICE', + 'name': EventTypes.CrossSigningSelfSigning, + 'request_id': '1', + }, + ); + FakeMatrixApi.calledEndpoints.clear(); + await client.encryption!.ssss.handleToDeviceEvent(event); + expect( + FakeMatrixApi.calledEndpoints.keys.any( + (k) => k.startsWith('/client/r0/sendToDevice/m.room.encrypted')), + false); + key.setDirectVerified(true); + }); + + test('receive share requests', () async { + if (!olmEnabled) return; + final key = + client.userDeviceKeys[client.userID!]!.deviceKeys['OTHERDEVICE']!; + key.setDirectVerified(true); + final handle = + client.encryption!.ssss.open(EventTypes.CrossSigningSelfSigning); + await handle.unlock(recoveryKey: ssssKey); + + await client.encryption!.ssss.clearCache(); + client.encryption!.ssss.pendingShareRequests.clear(); + await client.encryption!.ssss.request('best animal', [key]); + var event = ToDeviceEvent( + sender: client.userID!, + type: 'm.secret.send', + content: { + 'request_id': client.encryption!.ssss.pendingShareRequests.keys.first, + 'secret': 'foxies!', + }, + encryptedContent: { + 'sender_key': key.curve25519Key, + }, + ); + await client.encryption!.ssss.handleToDeviceEvent(event); + expect(await client.encryption!.ssss.getCached('best animal'), 'foxies!'); + + // test the different validators + for (final type in [ + EventTypes.CrossSigningSelfSigning, + EventTypes.CrossSigningUserSigning, + EventTypes.MegolmBackup + ]) { + final secret = await handle.getStored(type); + await client.encryption!.ssss.clearCache(); + client.encryption!.ssss.pendingShareRequests.clear(); + await client.encryption!.ssss.request(type, [key]); + event = ToDeviceEvent( + sender: client.userID!, + type: 'm.secret.send', + content: { + 'request_id': + client.encryption!.ssss.pendingShareRequests.keys.first, + 'secret': secret, + }, + encryptedContent: { + 'sender_key': key.curve25519Key, + }, + ); + await client.encryption!.ssss.handleToDeviceEvent(event); + expect(await client.encryption!.ssss.getCached(type), secret); + } + + // test different fail scenarios + + // not encrypted + await client.encryption!.ssss.clearCache(); + client.encryption!.ssss.pendingShareRequests.clear(); + await client.encryption!.ssss.request('best animal', [key]); + event = ToDeviceEvent( + sender: client.userID!, + type: 'm.secret.send', + content: { + 'request_id': client.encryption!.ssss.pendingShareRequests.keys.first, + 'secret': 'foxies!', + }, + ); + await client.encryption!.ssss.handleToDeviceEvent(event); + expect(await client.encryption!.ssss.getCached('best animal'), null); + + // unknown request id + await client.encryption!.ssss.clearCache(); + client.encryption!.ssss.pendingShareRequests.clear(); + await client.encryption!.ssss.request('best animal', [key]); + event = ToDeviceEvent( + sender: client.userID!, + type: 'm.secret.send', + content: { + 'request_id': 'invalid', + 'secret': 'foxies!', + }, + encryptedContent: { + 'sender_key': key.curve25519Key, + }, + ); + await client.encryption!.ssss.handleToDeviceEvent(event); + expect(await client.encryption!.ssss.getCached('best animal'), null); + + // not from a device we sent the request to + await client.encryption!.ssss.clearCache(); + client.encryption!.ssss.pendingShareRequests.clear(); + await client.encryption!.ssss.request('best animal', [key]); + event = ToDeviceEvent( + sender: client.userID!, + type: 'm.secret.send', + content: { + 'request_id': client.encryption!.ssss.pendingShareRequests.keys.first, + 'secret': 'foxies!', + }, + encryptedContent: { + 'sender_key': 'invalid', + }, + ); + await client.encryption!.ssss.handleToDeviceEvent(event); + expect(await client.encryption!.ssss.getCached('best animal'), null); + + // secret not a string + await client.encryption!.ssss.clearCache(); + client.encryption!.ssss.pendingShareRequests.clear(); + await client.encryption!.ssss.request('best animal', [key]); + event = ToDeviceEvent( + sender: client.userID!, + type: 'm.secret.send', + content: { + 'request_id': client.encryption!.ssss.pendingShareRequests.keys.first, + 'secret': 42, + }, + encryptedContent: { + 'sender_key': key.curve25519Key, + }, + ); + await client.encryption!.ssss.handleToDeviceEvent(event); + expect(await client.encryption!.ssss.getCached('best animal'), null); + + // validator doesn't check out + await client.encryption!.ssss.clearCache(); + client.encryption!.ssss.pendingShareRequests.clear(); + await client.encryption!.ssss.request(EventTypes.MegolmBackup, [key]); + event = ToDeviceEvent( + sender: client.userID!, + type: 'm.secret.send', + content: { + 'request_id': client.encryption!.ssss.pendingShareRequests.keys.first, + 'secret': 'foxies!', + }, + encryptedContent: { + 'sender_key': key.curve25519Key, + }, + ); + await client.encryption!.ssss.handleToDeviceEvent(event); + expect(await client.encryption!.ssss.getCached(EventTypes.MegolmBackup), + null); + }); + + test('request all', () async { + if (!olmEnabled) return; + final key = + client.userDeviceKeys[client.userID!]!.deviceKeys['OTHERDEVICE']!; + key.setDirectVerified(true); + await client.encryption!.ssss.clearCache(); + client.encryption!.ssss.pendingShareRequests.clear(); + await client.encryption!.ssss.maybeRequestAll([key]); + expect(client.encryption!.ssss.pendingShareRequests.length, 3); + }); + + test('periodicallyRequestMissingCache', () async { + if (!olmEnabled) return; + client.userDeviceKeys[client.userID!]!.masterKey!.setDirectVerified(true); + client.encryption!.ssss = MockSSSS(client.encryption!); + (client.encryption!.ssss as MockSSSS).requestedSecrets = false; + await client.encryption!.ssss.periodicallyRequestMissingCache(); + expect((client.encryption!.ssss as MockSSSS).requestedSecrets, true); + // it should only retry once every 15 min + (client.encryption!.ssss as MockSSSS).requestedSecrets = false; + await client.encryption!.ssss.periodicallyRequestMissingCache(); + expect((client.encryption!.ssss as MockSSSS).requestedSecrets, false); + }); + + test('createKey', () async { + if (!olmEnabled) return; + // with passphrase + var newKey = await client.encryption!.ssss.createKey('test'); + expect(client.encryption!.ssss.isKeyValid(newKey.keyId), true); + var testKey = client.encryption!.ssss.open(newKey.keyId); + await testKey.unlock(passphrase: 'test'); + await testKey.setPrivateKey(newKey.privateKey!); + + // without passphrase + newKey = await client.encryption!.ssss.createKey(); + expect(client.encryption!.ssss.isKeyValid(newKey.keyId), true); + testKey = client.encryption!.ssss.open(newKey.keyId); + await testKey.setPrivateKey(newKey.privateKey!); + }); + + test('dispose client', () async { + if (!olmEnabled) return; + await client.dispose(closeDatabase: true); + }); + }); +} diff --git a/test/encryption/utils_test.dart b/test/encryption/utils_test.dart new file mode 100644 index 0000000..b21d099 --- /dev/null +++ b/test/encryption/utils_test.dart @@ -0,0 +1,76 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2022 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:convert'; + +import 'package:matrix/encryption/utils/base64_unpadded.dart'; +import 'package:matrix/matrix.dart'; +import 'package:test/test.dart'; + +void main() { + group('Utils', () { + const base64input = 'foobar'; + final utf8codec = Utf8Codec(); + test('base64 padded', () { + final paddedBase64 = base64.encode(base64input.codeUnits); + + final decodedPadded = + utf8codec.decode(base64decodeUnpadded(paddedBase64)); + expect(decodedPadded, base64input, reason: 'Padded base64 decode'); + }); + + test('base64 unpadded', () { + const unpaddedBase64 = 'Zm9vYmFy'; + final decodedUnpadded = + utf8codec.decode(base64decodeUnpadded(unpaddedBase64)); + expect(decodedUnpadded, base64input, reason: 'Unpadded base64 decode'); + }); + }); + + group('MatrixFile', () { + test('MatrixImageFile', () async { + const base64Image = + 'iVBORw0KGgoAAAANSUhEUgAAANwAAADcCAYAAAAbWs+BAAAGwElEQVR4Ae3cwZFbNxBFUY5rkrDTmKAUk5QT03Aa44U22KC7NHptw+DRikVAXf8fzC3u8Hj4R4AAAQIECBAgQIAAAQIECBAgQIAAAQIECBAgQIAAAQIECBAgQIAAAQIECBAgQIAAAQIECBAgQIAAAQIECBAgQIAAAQIECBAgQIAAgZzAW26USQT+e4HPx+Mz+RRvj0e0kT+SD2cWAQK1gOBqH6sEogKCi3IaRqAWEFztY5VAVEBwUU7DCNQCgqt9rBKICgguymkYgVpAcLWPVQJRAcFFOQ0jUAsIrvaxSiAqILgop2EEagHB1T5WCUQFBBflNIxALSC42scqgaiA4KKchhGoBQRX+1glEBUQXJTTMAK1gOBqH6sEogKCi3IaRqAWeK+Xb1z9iN558fHxcSPS9p2ezx/ROz4e4TtIHt+3j/61hW9f+2+7/+UXbifjewIDAoIbQDWSwE5AcDsZ3xMYEBDcAKqRBHYCgtvJ+J7AgIDgBlCNJLATENxOxvcEBgQEN4BqJIGdgOB2Mr4nMCAguAFUIwnsBAS3k/E9gQEBwQ2gGklgJyC4nYzvCQwICG4A1UgCOwHB7WR8T2BAQHADqEYS2AkIbifjewIDAoIbQDWSwE5AcDsZ3xMYEEjfTzHwiK91B8npd6Q8n8/oGQ/ckRJ9vvQwv3BpUfMIFAKCK3AsEUgLCC4tah6BQkBwBY4lAmkBwaVFzSNQCAiuwLFEIC0guLSoeQQKAcEVOJYIpAUElxY1j0AhILgCxxKBtIDg0qLmESgEBFfgWCKQFhBcWtQ8AoWA4AocSwTSAoJLi5pHoBAQXIFjiUBaQHBpUfMIFAKCK3AsEUgLCC4tah6BQmDgTpPsHSTFs39p6fQ7Q770UsV/Ov19X+2OFL9wxR+rJQJpAcGlRc0jUAgIrsCxRCAtILi0qHkECgHBFTiWCKQFBJcWNY9AISC4AscSgbSA4NKi5hEoBARX4FgikBYQXFrUPAKFgOAKHEsE0gKCS4uaR6AQEFyBY4lAWkBwaVHzCBQCgitwLBFICwguLWoegUJAcAWOJQJpAcGlRc0jUAgIrsCxRCAt8J4eePq89B0ar3ZnyOnve/rfn1+400/I810lILirjtPLnC4guNNPyPNdJSC4q47Ty5wuILjTT8jzXSUguKuO08ucLiC400/I810lILirjtPLnC4guNNPyPNdJSC4q47Ty5wuILjTT8jzXSUguKuO08ucLiC400/I810lILirjtPLnC4guNNPyPNdJSC4q47Ty5wuILjTT8jzXSUguKuO08ucLiC400/I810l8JZ/m78+szP/zI47fJo7Q37vgJ7PHwN/07/3TOv/9gu3avhMYFhAcMPAxhNYBQS3avhMYFhAcMPAxhNYBQS3avhMYFhAcMPAxhNYBQS3avhMYFhAcMPAxhNYBQS3avhMYFhAcMPAxhNYBQS3avhMYFhAcMPAxhNYBQS3avhMYFhAcMPAxhNYBQS3avhMYFhAcMPAxhNYBQS3avhMYFhAcMPAxhNYBQS3avhMYFhg4P6H9J0maYHXuiMlrXf+vOfA33Turf3C5SxNItAKCK4lsoFATkBwOUuTCLQCgmuJbCCQExBcztIkAq2A4FoiGwjkBASXszSJQCsguJbIBgI5AcHlLE0i0AoIriWygUBOQHA5S5MItAKCa4lsIJATEFzO0iQCrYDgWiIbCOQEBJezNIlAKyC4lsgGAjkBweUsTSLQCgiuJbKBQE5AcDlLkwi0Akff//Dz6U+/I6U1/sUNr3bnytl3kPzi4bXb/cK1RDYQyAkILmdpEoFWQHAtkQ0EcgKCy1maRKAVEFxLZAOBnIDgcpYmEWgFBNcS2UAgJyC4nKVJBFoBwbVENhDICQguZ2kSgVZAcC2RDQRyAoLLWZpEoBUQXEtkA4GcgOByliYRaAUE1xLZQCAnILicpUkEWgHBtUQ2EMgJCC5naRKBVkBwLZENBHIC/4M7TXIv+3PS22d24qvdQfL3C/7N5P5i/MLlLE0i0AoIriWygUBOQHA5S5MItAKCa4lsIJATEFzO0iQCrYDgWiIbCOQEBJezNIlAKyC4lsgGAjkBweUsTSLQCgiuJbKBQE5AcDlLkwi0AoJriWwgkBMQXM7SJAKtgOBaIhsI5AQEl7M0iUArILiWyAYCOQHB5SxNItAKCK4lsoFATkBwOUuTCBAgQIAAAQIECBAgQIAAAQIECBAgQIAAAQIECBAgQIAAAQIECBAgQIAAAQIECBAgQIAAAQIECBAgQIAAAQIECBAgQIAAAQIECBAgQIDAvyrwDySEJ2VQgUSoAAAAAElFTkSuQmCC'; + final data = base64Decode(base64Image); + + final image = await MatrixImageFile.create( + bytes: data, + name: 'bomb.png', + mimeType: 'image/png', + ); + expect(image.width, 220, reason: 'Unexpected image width'); + expect(image.height, 220, reason: 'Unexpected image heigth'); + expect(image.blurhash, 'L75NyU5krSbx=zAF#kSNZxOZ%4NE', + reason: 'Unexpected image blur'); + + final thumbnail = await image.generateThumbnail(dimension: 64); + expect(thumbnail!.height, 64, reason: 'Unexpected thumbnail height'); + + final shrinkedImage = await MatrixImageFile.shrink( + bytes: data, + name: 'bomb.png', + mimeType: 'image/png', + maxDimension: 150); + expect(shrinkedImage.width, 150, reason: 'Unexpected scaled image width'); + expect(shrinkedImage.height, 150, + reason: 'Unexpected scaled image heigth'); + expect(shrinkedImage.blurhash, 'L75NyU5kvvbx^7AF#kSgZxOZ%5NE', + reason: 'Unexpected scaled image blur'); + }); + }); +} diff --git a/test/event_test.dart b/test/event_test.dart new file mode 100644 index 0000000..2a7aa80 --- /dev/null +++ b/test/event_test.dart @@ -0,0 +1,1625 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:convert'; +import 'dart:typed_data'; + +import 'package:matrix/encryption.dart'; +import 'package:matrix/matrix.dart'; +import 'package:olm/olm.dart' as olm; +import 'package:test/test.dart'; + +import 'fake_client.dart'; +import 'fake_matrix_api.dart'; +import 'matrix_default_localizations.dart'; + +void main() { + /// All Tests related to the Event + group('Event', () { + Logs().level = Level.error; + var olmEnabled = true; + + final timestamp = DateTime.now().millisecondsSinceEpoch; + final id = '!4fsdfjisjf:server.abc'; + final senderID = '@alice:server.abc'; + final type = 'm.room.message'; + final msgtype = 'm.text'; + final body = 'Hello World'; + final formatted_body = 'Hello World'; + + final contentJson = + '{"msgtype":"$msgtype","body":"$body","formatted_body":"$formatted_body","m.relates_to":{"m.in_reply_to":{"event_id":"\$1234:example.com"}}}'; + + final jsonObj = { + 'event_id': id, + 'sender': senderID, + 'origin_server_ts': timestamp, + 'type': type, + 'room_id': '!testroom:example.abc', + 'status': EventStatus.synced.intValue, + 'content': contentJson, + }; + final client = Client('testclient', httpClient: FakeMatrixApi()); + final room = Room(id: '!testroom:example.abc', client: client); + final event = Event.fromJson( + jsonObj, Room(id: '!testroom:example.abc', client: client)); + + test('setup', () async { + try { + await olm.init(); + olm.get_library_version(); + } catch (e) { + olmEnabled = false; + Logs().w('[LibOlm] Failed to load LibOlm', e); + } + Logs().i('[LibOlm] Enabled: $olmEnabled'); + }); + + test('Create from json', () async { + jsonObj.remove('status'); + jsonObj['content'] = json.decode(contentJson); + expect(event.toJson(), jsonObj); + jsonObj['content'] = contentJson; + + expect(event.eventId, id); + expect(event.senderId, senderID); + expect(event.status, EventStatus.synced); + expect(event.text, body); + expect(event.formattedText, formatted_body); + expect(event.body, body); + expect(event.type, EventTypes.Message); + expect(event.relationshipType, RelationshipTypes.reply); + jsonObj['state_key'] = ''; + final state = Event.fromJson(jsonObj, room); + expect(state.eventId, id); + expect(state.stateKey, ''); + expect(state.status, EventStatus.synced); + }); + test('Test all EventTypes', () async { + Event event; + + jsonObj['type'] = 'm.room.avatar'; + event = Event.fromJson(jsonObj, room); + expect(event.type, EventTypes.RoomAvatar); + + jsonObj['type'] = 'm.room.name'; + event = Event.fromJson(jsonObj, room); + expect(event.type, EventTypes.RoomName); + + jsonObj['type'] = 'm.room.topic'; + event = Event.fromJson(jsonObj, room); + expect(event.type, EventTypes.RoomTopic); + + jsonObj['type'] = 'm.room.aliases'; + event = Event.fromJson(jsonObj, room); + expect(event.type, EventTypes.RoomAliases); + + jsonObj['type'] = 'm.room.canonical_alias'; + event = Event.fromJson(jsonObj, room); + expect(event.type, EventTypes.RoomCanonicalAlias); + + jsonObj['type'] = 'm.room.create'; + event = Event.fromJson(jsonObj, room); + expect(event.type, EventTypes.RoomCreate); + + jsonObj['type'] = 'm.room.join_rules'; + event = Event.fromJson(jsonObj, room); + expect(event.type, EventTypes.RoomJoinRules); + + jsonObj['type'] = 'm.room.member'; + event = Event.fromJson(jsonObj, room); + expect(event.type, EventTypes.RoomMember); + + jsonObj['type'] = 'm.room.power_levels'; + event = Event.fromJson(jsonObj, room); + expect(event.type, EventTypes.RoomPowerLevels); + + jsonObj['type'] = 'm.room.guest_access'; + event = Event.fromJson(jsonObj, room); + expect(event.type, EventTypes.GuestAccess); + + jsonObj['type'] = 'm.room.history_visibility'; + event = Event.fromJson(jsonObj, room); + expect(event.type, EventTypes.HistoryVisibility); + + jsonObj['type'] = 'm.room.message'; + jsonObj['content'] = json.decode(jsonObj['content']); + + jsonObj['content'].remove('m.relates_to'); + jsonObj['content']['msgtype'] = 'm.notice'; + event = Event.fromJson(jsonObj, room); + expect(event.messageType, MessageTypes.Notice); + + jsonObj['content']['msgtype'] = 'm.emote'; + event = Event.fromJson(jsonObj, room); + expect(event.messageType, MessageTypes.Emote); + + jsonObj['content']['msgtype'] = 'm.image'; + event = Event.fromJson(jsonObj, room); + expect(event.messageType, MessageTypes.Image); + + jsonObj['content']['msgtype'] = 'm.video'; + event = Event.fromJson(jsonObj, room); + expect(event.messageType, MessageTypes.Video); + + jsonObj['content']['msgtype'] = 'm.audio'; + event = Event.fromJson(jsonObj, room); + expect(event.messageType, MessageTypes.Audio); + + jsonObj['content']['msgtype'] = 'm.file'; + event = Event.fromJson(jsonObj, room); + expect(event.messageType, MessageTypes.File); + + jsonObj['content']['msgtype'] = 'm.location'; + event = Event.fromJson(jsonObj, room); + expect(event.messageType, MessageTypes.Location); + + jsonObj['type'] = 'm.sticker'; + jsonObj['content']['msgtype'] = null; + event = Event.fromJson(jsonObj, room); + expect(event.messageType, MessageTypes.Sticker); + + jsonObj['type'] = 'm.room.message'; + jsonObj['content']['msgtype'] = 'm.text'; + jsonObj['content']['m.relates_to'] = {}; + jsonObj['content']['m.relates_to']['m.in_reply_to'] = { + 'event_id': '1234', + }; + event = Event.fromJson(jsonObj, room); + expect(event.messageType, MessageTypes.Text); + expect(event.relationshipType, RelationshipTypes.reply); + expect(event.relationshipEventId, '1234'); + }); + + test('relationship types', () async { + Event event; + + jsonObj['content'] = { + 'msgtype': 'm.text', + 'text': 'beep', + }; + event = Event.fromJson(jsonObj, room); + expect(event.relationshipType, null); + expect(event.relationshipEventId, null); + + jsonObj['content']['m.relates_to'] = { + 'rel_type': 'm.replace', + 'event_id': 'abc', + }; + event = Event.fromJson(jsonObj, room); + expect(event.relationshipType, RelationshipTypes.edit); + expect(event.relationshipEventId, 'abc'); + + jsonObj['content']['m.relates_to']['rel_type'] = 'm.annotation'; + event = Event.fromJson(jsonObj, room); + expect(event.relationshipType, RelationshipTypes.reaction); + expect(event.relationshipEventId, 'abc'); + + jsonObj['content']['m.relates_to'] = { + 'm.in_reply_to': { + 'event_id': 'def', + }, + }; + event = Event.fromJson(jsonObj, room); + expect(event.relationshipType, RelationshipTypes.reply); + expect(event.relationshipEventId, 'def'); + }); + + test('redact', () async { + final redactJsonObj = Map.from(jsonObj); + final testTypes = [ + EventTypes.RoomMember, + EventTypes.RoomCreate, + EventTypes.RoomJoinRules, + EventTypes.RoomPowerLevels, + EventTypes.RoomAliases, + EventTypes.HistoryVisibility, + ]; + for (final testType in testTypes) { + redactJsonObj['type'] = testType; + final room = Room(id: '1234', client: Client('testclient')); + final redactionEventJson = { + 'content': {'reason': 'Spamming'}, + 'event_id': '143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'redacts': id, + 'room_id': '1234', + 'sender': '@example:example.org', + 'type': 'm.room.redaction', + 'unsigned': {'age': 1234} + }; + final redactedBecause = Event.fromJson(redactionEventJson, room); + final event = Event.fromJson(redactJsonObj, room); + event.setRedactionEvent(redactedBecause); + expect(event.redacted, true); + expect(event.redactedBecause?.toJson(), redactedBecause.toJson()); + expect(event.content.isEmpty, true); + redactionEventJson.remove('redacts'); + expect(event.unsigned?['redacted_because'], redactionEventJson); + } + }); + + test('remove', () async { + final event = Event.fromJson( + jsonObj, Room(id: '1234', client: Client('testclient'))); + final removed1 = await event.remove(); + event.status = EventStatus.sending; + final removed2 = await event.remove(); + expect(removed1, false); + expect(removed2, true); + }); + + test('sendAgain', () async { + final matrix = Client('testclient', httpClient: FakeMatrixApi()); + await matrix.checkHomeserver('https://fakeserver.notexisting', + checkWellKnown: false); + await matrix.login(LoginType.mLoginPassword, + identifier: AuthenticationUserIdentifier(user: 'test'), + password: '1234'); + + final event = Event.fromJson( + jsonObj, Room(id: '!1234:example.com', client: matrix)); + final resp1 = await event.sendAgain(); + event.status = EventStatus.error; + final resp2 = await event.sendAgain(txid: '1234'); + expect(resp1, null); + expect(resp2?.startsWith('\$event'), true); + + await matrix.dispose(closeDatabase: true); + }); + + test('requestKey', () async { + final matrix = Client('testclient', httpClient: FakeMatrixApi()); + await matrix.checkHomeserver('https://fakeserver.notexisting', + checkWellKnown: false); + await matrix.login(LoginType.mLoginPassword, + identifier: AuthenticationUserIdentifier(user: 'test'), + password: '1234'); + + final event = Event.fromJson( + jsonObj, Room(id: '!1234:example.com', client: matrix)); + String? exception; + try { + await event.requestKey(); + } catch (e) { + exception = e.toString(); + } + expect(exception, 'Session key not requestable'); + + final event2 = Event.fromJson({ + 'event_id': id, + 'sender': senderID, + 'origin_server_ts': timestamp, + 'type': 'm.room.encrypted', + 'room_id': '1234', + 'status': EventStatus.synced.intValue, + 'content': json.encode({ + 'msgtype': 'm.bad.encrypted', + 'body': DecryptException.unknownSession, + 'can_request_session': true, + 'algorithm': AlgorithmTypes.megolmV1AesSha2, + 'ciphertext': 'AwgAEnACgAkLmt6qF84IK++J7UDH2Za1YVchHyprqTqsg...', + 'device_id': 'RJYKSTBOIE', + 'sender_key': 'IlRMeOPX2e0MurIyfWEucYBRVOEEUMrOHqn/8mLqMjA', + 'session_id': 'X3lUlvLELLYxeTx4yOVu6UDpasGEVO0Jbu+QFnm0cKQ' + }), + }, Room(id: '!1234:example.com', client: matrix)); + + await event2.requestKey(); + + await matrix.dispose(closeDatabase: true); + }); + test('requestKey', () async { + jsonObj['state_key'] = '@alice:example.com'; + final event = Event.fromJson( + jsonObj, Room(id: '!localpart:server.abc', client: client)); + expect(event.stateKeyUser?.id, '@alice:example.com'); + }); + test('canRedact', () async { + expect(event.canRedact, true); + }); + test('getLocalizedBody, isEventKnown', () async { + final matrix = Client('testclient', httpClient: FakeMatrixApi()); + final room = Room(id: '!1234:example.com', client: matrix); + var event = Event.fromJson({ + 'content': { + 'avatar_url': 'mxc://example.org/SEsfnsuifSDFSSEF', + 'displayname': 'Alice Margatroid', + 'membership': 'join' + }, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'state_key': '@alice:example.org', + 'type': 'm.room.member', + 'unsigned': { + 'age': 1234, + 'redacted_because': { + 'content': {'reason': 'Spamming'}, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'redacts': '\$143273582443PhrSn:example.org', + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'type': 'm.room.redaction', + 'unsigned': {'age': 1234} + } + } + }, room); + expect(event.getLocalizedBody(MatrixDefaultLocalizations()), + 'Removed by Example'); + expect(event.isEventTypeKnown, true); + + event = Event.fromJson({ + 'content': { + 'avatar_url': + 'mxc://pixelthefox.net/bmGuC44Eeb3BomfkZTP02DVnGaRp4dek', + 'displayname': [ + [ + [[]] + ] + ], + 'membership': 'join' + }, + 'origin_server_ts': 1636487843183, + 'room_id': '!watercooler-v9:maunium.net', + 'sender': '@nyaaori:pixelthefox.net', + 'state_key': '@nyaaori:pixelthefox.net', + 'type': 'm.room.member', + 'unsigned': { + 'prev_content': { + 'avatar_url': + 'mxc://pixelthefox.net/bmGuC44Eeb3BomfkZTP02DVnGaRp4dek', + 'displayname': 1, + 'membership': 'join' + }, + 'prev_sender': '@nyaaori:pixelthefox.net', + 'replaces_state': '\$kcqn2k6kXQKOM45t_p8OA03PQRR3KB2N_PN4HUq1GiY' + }, + 'event_id': '\$21DJjleMGcviLoT4L9wvxawMlOXSQ9yW6R8mrhlbhfU', + 'user_id': '@nyaaori:pixelthefox.net', + 'replaces_state': '\$kcqn2k6kXQKOM45t_p8OA03PQRR3KB2N_PN4HUq1GiY', + 'prev_content': { + 'avatar_url': + 'mxc://pixelthefox.net/bmGuC44Eeb3BomfkZTP02DVnGaRp4dek', + 'displayname': 1, + 'membership': 'join' + } + }, room); + expect(event.isEventTypeKnown, true); + + event = Event.fromJson({ + 'content': { + 'body': 'Landing', + 'info': { + 'h': 200, + 'mimetype': 'image/png', + 'size': 73602, + 'thumbnail_info': { + 'h': 200, + 'mimetype': 'image/png', + 'size': 73602, + 'w': 140 + }, + 'thumbnail_url': 'mxc://matrix.org/sHhqkFCvSkFwtmvtETOtKnLP', + 'w': 140 + }, + 'url': 'mxc://matrix.org/sHhqkFCvSkFwtmvtETOtKnLP' + }, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'type': 'm.sticker', + 'unsigned': {'age': 1234} + }, room); + expect(event.getLocalizedBody(MatrixDefaultLocalizations()), + 'Example sent a sticker'); + expect(event.isEventTypeKnown, true); + + event = Event.fromJson({ + 'content': {'reason': 'Spamming'}, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'redacts': '\$143273582443PhrSn:example.org', + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'type': 'm.room.redaction', + 'unsigned': {'age': 1234} + }, room); + expect(event.getLocalizedBody(MatrixDefaultLocalizations()), + 'Example redacted an event'); + expect(event.isEventTypeKnown, true); + + event = Event.fromJson({ + 'content': { + 'aliases': ['#somewhere:example.org', '#another:example.org'] + }, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'state_key': 'example.org', + 'type': 'm.room.aliases', + 'unsigned': {'age': 1234} + }, room); + expect(event.getLocalizedBody(MatrixDefaultLocalizations()), + 'Example changed the room aliases'); + expect(event.isEventTypeKnown, true); + + event = Event.fromJson({ + 'content': { + 'aliases': ['#somewhere:example.org', '#another:example.org'] + }, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'state_key': 'example.org', + 'type': 'm.room.aliases', + 'unsigned': {'age': 1234} + }, room); + expect(event.getLocalizedBody(MatrixDefaultLocalizations()), + 'Example changed the room aliases'); + expect(event.isEventTypeKnown, true); + + event = Event.fromJson({ + 'content': {'alias': '#somewhere:localhost'}, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'state_key': '', + 'type': 'm.room.canonical_alias', + 'unsigned': {'age': 1234} + }, room); + expect(event.getLocalizedBody(MatrixDefaultLocalizations()), + 'Example changed the room invitation link'); + expect(event.isEventTypeKnown, true); + + event = Event.fromJson({ + 'content': { + 'creator': '@example:example.org', + 'm.federate': true, + 'predecessor': { + 'event_id': '\$something:example.org', + 'room_id': '!oldroom:example.org' + }, + 'room_version': '1' + }, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'state_key': '', + 'type': 'm.room.create', + 'unsigned': {'age': 1234} + }, room); + expect(event.getLocalizedBody(MatrixDefaultLocalizations()), + 'Example created the chat'); + expect(event.isEventTypeKnown, true); + + event = Event.fromJson({ + 'content': { + 'body': 'This room has been replaced', + 'replacement_room': '!newroom:example.org' + }, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'state_key': '', + 'type': 'm.room.tombstone', + 'unsigned': {'age': 1234} + }, room); + expect(event.getLocalizedBody(MatrixDefaultLocalizations()), + 'Room has been upgraded'); + expect(event.isEventTypeKnown, true); + + event = Event.fromJson({ + 'content': {'join_rule': 'public'}, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'state_key': '', + 'type': 'm.room.join_rules', + 'unsigned': {'age': 1234} + }, room); + expect(event.getLocalizedBody(MatrixDefaultLocalizations()), + 'Example changed the join rules to Anyone can join'); + expect(event.isEventTypeKnown, true); + + event = Event.fromJson({ + 'content': { + 'avatar_url': 'mxc://example.org/SEsfnsuifSDFSSEF', + 'displayname': 'Alice Margatroid', + 'membership': 'join' + }, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'state_key': '@alice:example.org', + 'type': 'm.room.member', + 'unsigned': {'age': 1234} + }, room); + expect(event.getLocalizedBody(MatrixDefaultLocalizations()), + 'Alice joined the chat'); + expect(event.isEventTypeKnown, true); + + event = Event.fromJson({ + 'content': {'membership': 'invite'}, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'state_key': '@alice:example.org', + 'type': 'm.room.member' + }, room); + expect(event.getLocalizedBody(MatrixDefaultLocalizations()), + 'Example has invited Alice'); + expect(event.isEventTypeKnown, true); + + event = Event.fromJson({ + 'content': {'membership': 'leave'}, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'state_key': '@alice:example.org', + 'type': 'm.room.member', + 'unsigned': { + 'prev_content': {'membership': 'join'}, + } + }, room); + expect(event.getLocalizedBody(MatrixDefaultLocalizations()), + 'Example kicked Alice'); + expect(event.isEventTypeKnown, true); + + event = Event.fromJson({ + 'content': {'membership': 'ban'}, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'state_key': '@alice:example.org', + 'type': 'm.room.member', + 'unsigned': { + 'prev_content': {'membership': 'join'}, + } + }, room); + expect(event.getLocalizedBody(MatrixDefaultLocalizations()), + 'Example banned Alice'); + expect(event.isEventTypeKnown, true); + + event = Event.fromJson({ + 'content': {'membership': 'join'}, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'state_key': '@alice:example.org', + 'type': 'm.room.member', + 'unsigned': { + 'prev_content': {'membership': 'invite'}, + } + }, room); + expect(event.getLocalizedBody(MatrixDefaultLocalizations()), + 'Alice accepted the invitation'); + expect(event.isEventTypeKnown, true); + + event = Event.fromJson({ + 'content': {'membership': 'invite'}, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'state_key': '@alice:example.org', + 'type': 'm.room.member', + 'unsigned': { + 'prev_content': {'membership': 'join'}, + } + }, room); + expect(event.getLocalizedBody(MatrixDefaultLocalizations()), + 'Example has invited Alice'); + expect(event.isEventTypeKnown, true); + + event = Event.fromJson({ + 'content': {'membership': 'leave'}, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'state_key': '@alice:example.org', + 'type': 'm.room.member', + 'unsigned': { + 'prev_content': {'membership': 'invite'}, + } + }, room); + expect(event.getLocalizedBody(MatrixDefaultLocalizations()), + 'Example has withdrawn the invitation for Alice'); + expect(event.isEventTypeKnown, true); + + event = Event.fromJson({ + 'content': {'membership': 'leave'}, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@alice:example.org', + 'state_key': '@alice:example.org', + 'type': 'm.room.member', + 'unsigned': { + 'prev_content': {'membership': 'invite'}, + } + }, room); + expect(event.getLocalizedBody(MatrixDefaultLocalizations()), + 'Alice rejected the invitation'); + expect(event.isEventTypeKnown, true); + + event = Event.fromJson({ + 'content': { + 'ban': 50, + 'events': {'m.room.name': 100, 'm.room.power_levels': 100}, + 'events_default': 0, + 'invite': 50, + 'kick': 50, + 'notifications': {'room': 20}, + 'redact': 50, + 'state_default': 50, + 'users': {'@example:localhost': 100}, + 'users_default': 0 + }, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'state_key': '', + 'type': 'm.room.power_levels', + 'unsigned': {'age': 1234} + }, room); + expect(event.getLocalizedBody(MatrixDefaultLocalizations()), + 'Example changed the chat permissions'); + expect(event.isEventTypeKnown, true); + + event = Event.fromJson({ + 'content': {'name': 'The room name'}, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'state_key': '', + 'type': 'm.room.name', + 'unsigned': {'age': 1234} + }, room); + expect(event.getLocalizedBody(MatrixDefaultLocalizations()), + 'Example changed the chat name to The room name'); + expect(event.isEventTypeKnown, true); + + event = Event.fromJson({ + 'content': {'topic': 'A room topic'}, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'state_key': '', + 'type': 'm.room.topic', + 'unsigned': {'age': 1234} + }, room); + expect(event.getLocalizedBody(MatrixDefaultLocalizations()), + 'Example changed the chat description to A room topic'); + expect(event.isEventTypeKnown, true); + + event = Event.fromJson({ + 'content': { + 'info': {'h': 398, 'mimetype': 'image/jpeg', 'size': 31037, 'w': 394}, + 'url': 'mxc://example.org/JWEIFJgwEIhweiWJE' + }, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'state_key': '', + 'type': 'm.room.avatar', + 'unsigned': {'age': 1234} + }, room); + expect(event.getLocalizedBody(MatrixDefaultLocalizations()), + 'Example changed the chat avatar'); + expect(event.isEventTypeKnown, true); + + event = Event.fromJson({ + 'content': {'history_visibility': 'shared'}, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'state_key': '', + 'type': 'm.room.history_visibility', + 'unsigned': {'age': 1234} + }, room); + expect(event.getLocalizedBody(MatrixDefaultLocalizations()), + 'Example changed the history visibility to Visible for all participants'); + expect(event.isEventTypeKnown, true); + + event = Event.fromJson({ + 'content': { + 'algorithm': AlgorithmTypes.megolmV1AesSha2, + 'rotation_period_ms': 604800000, + 'rotation_period_msgs': 100 + }, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'state_key': '', + 'type': 'm.room.encryption', + 'unsigned': {'age': 1234} + }, room); + expect(event.getLocalizedBody(MatrixDefaultLocalizations()), + 'Example activated end to end encryption. Need pantalaimon'); + expect(event.isEventTypeKnown, true); + + event = Event.fromJson({ + 'content': { + 'body': 'This is an example text message', + 'format': 'org.matrix.custom.html', + 'formatted_body': 'This is an example text message', + 'msgtype': 'm.text' + }, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'type': 'm.room.message', + 'unsigned': {'age': 1234} + }, room); + expect(event.getLocalizedBody(MatrixDefaultLocalizations()), + 'This is an example text message'); + expect(event.isEventTypeKnown, true); + + event = Event.fromJson({ + 'content': { + 'body': 'thinks this is an example emote', + 'format': 'org.matrix.custom.html', + 'formatted_body': 'thinks this is an example emote', + 'msgtype': 'm.emote' + }, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'type': 'm.room.message', + 'unsigned': {'age': 1234} + }, room); + expect(event.getLocalizedBody(MatrixDefaultLocalizations()), + '* thinks this is an example emote'); + expect(event.isEventTypeKnown, true); + + event = Event.fromJson({ + 'content': { + 'body': 'This is an example notice', + 'format': 'org.matrix.custom.html', + 'formatted_body': 'This is an example notice', + 'msgtype': 'm.notice' + }, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'type': 'm.room.message', + 'unsigned': {'age': 1234} + }, room); + expect(event.getLocalizedBody(MatrixDefaultLocalizations()), + 'This is an example notice'); + expect(event.isEventTypeKnown, true); + + event = Event.fromJson({ + 'content': { + 'body': 'filename.jpg', + 'info': {'h': 398, 'mimetype': 'image/jpeg', 'size': 31037, 'w': 394}, + 'msgtype': 'm.image', + 'url': 'mxc://example.org/JWEIFJgwEIhweiWJE' + }, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'type': 'm.room.message', + 'unsigned': {'age': 1234} + }, room); + expect(event.getLocalizedBody(MatrixDefaultLocalizations()), + 'Example sent a picture'); + expect(event.isEventTypeKnown, true); + + event = Event.fromJson({ + 'content': { + 'body': 'something-important.doc', + 'filename': 'something-important.doc', + 'info': {'mimetype': 'application/msword', 'size': 46144}, + 'msgtype': 'm.file', + 'url': 'mxc://example.org/FHyPlCeYUSFFxlgbQYZmoEoe' + }, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'type': 'm.room.message', + 'unsigned': {'age': 1234} + }, room); + expect(event.getLocalizedBody(MatrixDefaultLocalizations()), + 'Example sent a file'); + expect(event.isEventTypeKnown, true); + + event = Event.fromJson({ + 'content': { + 'body': 'Bee Gees - Stayin Alive', + 'info': { + 'duration': 2140786, + 'mimetype': 'audio/mpeg', + 'size': 1563685 + }, + 'msgtype': 'm.audio', + 'url': 'mxc://example.org/ffed755USFFxlgbQYZGtryd' + }, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'type': 'm.room.message', + 'unsigned': {'age': 1234} + }, room); + expect(event.getLocalizedBody(MatrixDefaultLocalizations()), + 'Example sent an audio'); + expect(event.isEventTypeKnown, true); + + event = Event.fromJson({ + 'content': { + 'body': 'Big Ben, London, UK', + 'geo_uri': 'geo:51.5008,0.1247', + 'info': { + 'thumbnail_info': { + 'h': 300, + 'mimetype': 'image/jpeg', + 'size': 46144, + 'w': 300 + }, + 'thumbnail_url': 'mxc://example.org/FHyPlCeYUSFFxlgbQYZmoEoe' + }, + 'msgtype': 'm.location' + }, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'type': 'm.room.message', + 'unsigned': {'age': 1234} + }, room); + expect(event.getLocalizedBody(MatrixDefaultLocalizations()), + 'Example shared the location'); + expect(event.isEventTypeKnown, true); + + event = Event.fromJson({ + 'content': { + 'body': 'Gangnam Style', + 'info': { + 'duration': 2140786, + 'h': 320, + 'mimetype': 'video/mp4', + 'size': 1563685, + 'thumbnail_info': { + 'h': 300, + 'mimetype': 'image/jpeg', + 'size': 46144, + 'w': 300 + }, + 'thumbnail_url': 'mxc://example.org/FHyPlCeYUSFFxlgbQYZmoEoe', + 'w': 480 + }, + 'msgtype': 'm.video', + 'url': 'mxc://example.org/a526eYUSFFxlgbQYZmo442' + }, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'type': 'm.room.message', + 'unsigned': {'age': 1234} + }, room); + expect(event.getLocalizedBody(MatrixDefaultLocalizations()), + 'Example sent a video'); + expect(event.isEventTypeKnown, true); + + event = Event.fromJson({ + 'content': {'beep': 'boop'}, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'type': 'unknown.event.type', + 'unsigned': {'age': 1234} + }, room); + expect(event.getLocalizedBody(MatrixDefaultLocalizations()), + 'Unknown event unknown.event.type'); + expect(event.isEventTypeKnown, false); + }); + + test('getLocalizedBody, parameters', () { + final matrix = Client('testclient', httpClient: FakeMatrixApi()); + final room = Room(id: '!1234:example.com', client: matrix); + var event = Event.fromJson({ + 'content': { + 'body': 'This is an example text message', + 'format': 'org.matrix.custom.html', + 'formatted_body': 'This is an example text message', + 'msgtype': 'm.text' + }, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'type': 'm.room.message', + 'unsigned': {'age': 1234} + }, room); + expect( + event.getLocalizedBody(MatrixDefaultLocalizations(), + plaintextBody: true), + '**This is an example text message**'); + + event = Event.fromJson({ + 'content': { + 'body': '* This is an example text message', + 'format': 'org.matrix.custom.html', + 'formatted_body': '* This is an example text message', + 'msgtype': 'm.text', + 'm.relates_to': { + 'rel_type': 'm.replace', + 'event_id': '\$some_event', + }, + 'm.new_content': { + 'body': 'This is an example text message', + 'format': 'org.matrix.custom.html', + 'formatted_body': 'This is an example text message', + 'msgtype': 'm.text' + }, + }, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'type': 'm.room.message', + 'unsigned': {'age': 1234} + }, room); + expect( + event.getLocalizedBody(MatrixDefaultLocalizations(), hideEdit: true), + 'This is an example text message'); + expect( + event.getLocalizedBody(MatrixDefaultLocalizations(), + hideEdit: true, plaintextBody: true), + '**This is an example text message**'); + + event = Event.fromJson({ + 'content': { + 'body': '> <@user:example.org> beep\n\nhmm, fox', + 'format': 'org.matrix.custom.html', + 'formatted_body': 'beephmm, fox', + 'msgtype': 'm.text' + }, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'type': 'm.room.message', + 'unsigned': {'age': 1234} + }, room); + expect( + event.getLocalizedBody(MatrixDefaultLocalizations(), hideReply: true), + 'hmm, fox'); + expect( + event.getLocalizedBody(MatrixDefaultLocalizations(), + hideReply: true, plaintextBody: true), + 'hmm, *fox*'); + }); + + test('aggregations', () { + final room = Room(id: '!1234', client: client); + final event = Event.fromJson({ + 'content': { + 'body': 'blah', + 'msgtype': 'm.text', + }, + 'type': 'm.room.message', + 'sender': '@example:example.org', + 'event_id': '\$source', + }, room); + final edit1 = Event.fromJson({ + 'content': { + 'body': 'blah', + 'msgtype': 'm.text', + 'm.relates_to': { + 'event_id': '\$source', + 'rel_type': RelationshipTypes.edit, + }, + }, + 'type': 'm.room.message', + 'sender': '@example:example.org', + 'event_id': '\$edit1', + }, room); + final edit2 = Event.fromJson({ + 'content': { + 'body': 'blah', + 'msgtype': 'm.text', + 'm.relates_to': { + 'event_id': '\$source', + 'rel_type': RelationshipTypes.edit, + }, + }, + 'type': 'm.room.message', + 'sender': '@example:example.org', + 'event_id': '\$edit2', + }, room); + final timeline = + Timeline(events: [event, edit1, edit2], room: room); + expect(event.hasAggregatedEvents(timeline, RelationshipTypes.edit), true); + expect(event.aggregatedEvents(timeline, RelationshipTypes.edit), + {edit1, edit2}); + expect(event.aggregatedEvents(timeline, RelationshipTypes.reaction), + {}); + expect(event.hasAggregatedEvents(timeline, RelationshipTypes.reaction), + false); + + timeline.removeAggregatedEvent(edit2); + expect(event.aggregatedEvents(timeline, RelationshipTypes.edit), {edit1}); + timeline.addAggregatedEvent(edit2); + expect(event.aggregatedEvents(timeline, RelationshipTypes.edit), + {edit1, edit2}); + + timeline.removeAggregatedEvent(event); + expect( + event.aggregatedEvents(timeline, RelationshipTypes.edit), {}); + }); + test('plaintextBody', () { + final event = Event.fromJson({ + 'type': EventTypes.Message, + 'content': { + 'body': 'blah', + 'msgtype': 'm.text', + 'format': 'org.matrix.custom.html', + 'formatted_body': 'blah', + }, + 'event_id': '\$source', + 'sender': '@alice:example.org', + }, room); + expect(event.plaintextBody, '**blah**'); + }); + test('getDisplayEvent', () { + final room = Room(id: '!1234', client: client); + var event = Event.fromJson({ + 'type': EventTypes.Message, + 'content': { + 'body': 'blah', + 'msgtype': 'm.text', + }, + 'event_id': '\$source', + 'sender': '@alice:example.org', + }, room); + final edit1 = Event.fromJson({ + 'type': EventTypes.Message, + 'content': { + 'body': '* edit 1', + 'msgtype': 'm.text', + 'm.new_content': { + 'body': 'edit 1', + 'msgtype': 'm.text', + }, + 'm.relates_to': { + 'event_id': '\$source', + 'rel_type': RelationshipTypes.edit, + }, + }, + 'event_id': '\$edit1', + 'sender': '@alice:example.org', + }, room); + final edit2 = Event.fromJson({ + 'type': EventTypes.Message, + 'content': { + 'body': '* edit 2', + 'msgtype': 'm.text', + 'm.new_content': { + 'body': 'edit 2', + 'msgtype': 'm.text', + }, + 'm.relates_to': { + 'event_id': '\$source', + 'rel_type': RelationshipTypes.edit, + }, + }, + 'event_id': '\$edit2', + 'sender': '@alice:example.org', + }, room); + final edit3 = Event.fromJson({ + 'type': EventTypes.Message, + 'content': { + 'body': '* edit 3', + 'msgtype': 'm.text', + 'm.new_content': { + 'body': 'edit 3', + 'msgtype': 'm.text', + }, + 'm.relates_to': { + 'event_id': '\$source', + 'rel_type': RelationshipTypes.edit, + }, + }, + 'event_id': '\$edit3', + 'sender': '@bob:example.org', + }, room); + // no edits + var displayEvent = + event.getDisplayEvent(Timeline(events: [event], room: room)); + expect(displayEvent.body, 'blah'); + // one edit + displayEvent = event + .getDisplayEvent(Timeline(events: [event, edit1], room: room)); + expect(displayEvent.body, 'edit 1'); + // two edits + displayEvent = event.getDisplayEvent( + Timeline(events: [event, edit1, edit2], room: room)); + expect(displayEvent.body, 'edit 2'); + // foreign edit + displayEvent = event + .getDisplayEvent(Timeline(events: [event, edit3], room: room)); + expect(displayEvent.body, 'blah'); + // mixed foreign and non-foreign + displayEvent = event.getDisplayEvent( + Timeline(events: [event, edit1, edit2, edit3], room: room)); + expect(displayEvent.body, 'edit 2'); + event = Event.fromJson({ + 'type': EventTypes.Message, + 'content': { + 'body': 'blah', + 'msgtype': 'm.text', + }, + 'event_id': '\$source', + 'sender': '@alice:example.org', + 'unsigned': { + 'redacted_because': { + 'event_id': '\$redact', + 'sender': '@alice:example.org', + 'type': 'm.room.redaction', + }, + }, + }, room); + displayEvent = event.getDisplayEvent( + Timeline(events: [event, edit1, edit2, edit3], room: room)); + expect(displayEvent.body, 'Redacted'); + }); + test('attachments', () async { + final FILE_BUFF = Uint8List.fromList([0]); + final THUMBNAIL_BUFF = Uint8List.fromList([2]); + final downloadCallback = (Uri uri) async { + return { + '/_matrix/media/r0/download/example.org/file': FILE_BUFF, + '/_matrix/media/r0/download/example.org/thumb': THUMBNAIL_BUFF, + }[uri.path]!; + }; + await client.checkHomeserver('https://fakeserver.notexisting', + checkWellKnown: false); + final room = Room(id: '!localpart:server.abc', client: client); + var event = Event.fromJson({ + 'type': EventTypes.Message, + 'content': { + 'body': 'image', + 'msgtype': 'm.image', + 'url': 'mxc://example.org/file', + }, + 'event_id': '\$edit2', + 'sender': '@alice:example.org', + }, room); + var buffer = await event.downloadAndDecryptAttachment( + downloadCallback: downloadCallback); + expect(buffer.bytes, FILE_BUFF); + expect(event.attachmentOrThumbnailMxcUrl().toString(), + 'mxc://example.org/file'); + expect(event.attachmentOrThumbnailMxcUrl(getThumbnail: true).toString(), + 'mxc://example.org/file'); + + event = Event.fromJson({ + 'type': EventTypes.Message, + 'content': { + 'body': 'image', + 'msgtype': 'm.image', + 'url': 'mxc://example.org/file', + 'info': { + 'size': 8000000, + 'thumbnail_url': 'mxc://example.org/thumb', + 'thumbnail_info': { + 'mimetype': 'thumbnail/mimetype', + }, + 'mimetype': 'application/octet-stream', + }, + }, + 'event_id': '\$edit2', + 'sender': '@alice:example.org', + }, room); + expect(event.hasAttachment, true); + expect(event.hasThumbnail, true); + expect(event.isAttachmentEncrypted, false); + expect(event.isThumbnailEncrypted, false); + expect(event.attachmentMimetype, 'application/octet-stream'); + expect(event.thumbnailMimetype, 'thumbnail/mimetype'); + expect(event.attachmentMxcUrl.toString(), 'mxc://example.org/file'); + expect(event.thumbnailMxcUrl.toString(), 'mxc://example.org/thumb'); + expect(event.attachmentOrThumbnailMxcUrl().toString(), + 'mxc://example.org/file'); + expect(event.attachmentOrThumbnailMxcUrl(getThumbnail: true).toString(), + 'mxc://example.org/thumb'); + expect(event.getAttachmentUrl().toString(), + 'https://fakeserver.notexisting/_matrix/media/r0/download/example.org/file'); + expect(event.getAttachmentUrl(getThumbnail: true).toString(), + 'https://fakeserver.notexisting/_matrix/media/r0/thumbnail/example.org/file?width=800&height=800&method=scale&animated=false'); + expect(event.getAttachmentUrl(useThumbnailMxcUrl: true).toString(), + 'https://fakeserver.notexisting/_matrix/media/r0/download/example.org/thumb'); + expect( + event + .getAttachmentUrl(getThumbnail: true, useThumbnailMxcUrl: true) + .toString(), + 'https://fakeserver.notexisting/_matrix/media/r0/thumbnail/example.org/thumb?width=800&height=800&method=scale&animated=false'); + expect( + event + .getAttachmentUrl(getThumbnail: true, minNoThumbSize: 9000000) + .toString(), + 'https://fakeserver.notexisting/_matrix/media/r0/download/example.org/file'); + + buffer = await event.downloadAndDecryptAttachment( + downloadCallback: downloadCallback); + expect(buffer.bytes, FILE_BUFF); + + buffer = await event.downloadAndDecryptAttachment( + getThumbnail: true, downloadCallback: downloadCallback); + expect(buffer.bytes, THUMBNAIL_BUFF); + }); + test('encrypted attachments', () async { + if (!olmEnabled) return; + + final FILE_BUFF_ENC = Uint8List.fromList([0x3B, 0x6B, 0xB2, 0x8C, 0xAF]); + final FILE_BUFF_DEC = Uint8List.fromList([0x74, 0x65, 0x73, 0x74, 0x0A]); + final THUMB_BUFF_ENC = + Uint8List.fromList([0x55, 0xD7, 0xEB, 0x72, 0x05, 0x13]); + final THUMB_BUFF_DEC = + Uint8List.fromList([0x74, 0x68, 0x75, 0x6D, 0x62, 0x0A]); + final downloadCallback = (Uri uri) async { + return { + '/_matrix/media/r0/download/example.com/file': FILE_BUFF_ENC, + '/_matrix/media/r0/download/example.com/thumb': THUMB_BUFF_ENC, + }[uri.path]!; + }; + final room = Room(id: '!localpart:server.abc', client: await getClient()); + var event = Event.fromJson({ + 'type': EventTypes.Message, + 'content': { + 'body': 'image', + 'msgtype': 'm.image', + 'file': { + 'v': 'v2', + 'key': { + 'alg': 'A256CTR', + 'ext': true, + 'k': '7aPRNIDPeUAUqD6SPR3vVX5W9liyMG98NexVJ9udnCc', + 'key_ops': ['encrypt', 'decrypt'], + 'kty': 'oct' + }, + 'iv': 'Wdsf+tnOHIoAAAAAAAAAAA', + 'hashes': {'sha256': 'WgC7fw2alBC5t+xDx+PFlZxfFJXtIstQCg+j0WDaXxE'}, + 'url': 'mxc://example.com/file', + 'mimetype': 'text/plain' + }, + }, + 'event_id': '\$edit2', + 'sender': '@alice:example.org', + }, room); + var buffer = await event.downloadAndDecryptAttachment( + downloadCallback: downloadCallback); + expect(buffer.bytes, FILE_BUFF_DEC); + + event = Event.fromJson({ + 'type': EventTypes.Message, + 'content': { + 'body': 'image', + 'msgtype': 'm.image', + 'file': { + 'v': 'v2', + 'key': { + 'alg': 'A256CTR', + 'ext': true, + 'k': '7aPRNIDPeUAUqD6SPR3vVX5W9liyMG98NexVJ9udnCc', + 'key_ops': ['encrypt', 'decrypt'], + 'kty': 'oct' + }, + 'iv': 'Wdsf+tnOHIoAAAAAAAAAAA', + 'hashes': {'sha256': 'WgC7fw2alBC5t+xDx+PFlZxfFJXtIstQCg+j0WDaXxE'}, + 'url': 'mxc://example.com/file', + 'mimetype': 'text/plain' + }, + 'info': { + 'thumbnail_file': { + 'v': 'v2', + 'key': { + 'alg': 'A256CTR', + 'ext': true, + 'k': 'TmF-rZYetZbxpL5yjDPE21UALQJcpEE6X-nvUDD5rA0', + 'key_ops': ['encrypt', 'decrypt'], + 'kty': 'oct' + }, + 'iv': '41ZqNRZSLFUAAAAAAAAAAA', + 'hashes': { + 'sha256': 'zccOwXiOTAYhGXyk0Fra7CRreBF6itjiCKdd+ov8mO4' + }, + 'url': 'mxc://example.com/thumb', + 'mimetype': 'text/plain' + } + }, + }, + 'event_id': '\$edit2', + 'sender': '@alice:example.org', + }, room); + expect(event.hasAttachment, true); + expect(event.hasThumbnail, true); + expect(event.isAttachmentEncrypted, true); + expect(event.isThumbnailEncrypted, true); + expect(event.attachmentMimetype, 'text/plain'); + expect(event.thumbnailMimetype, 'text/plain'); + expect(event.attachmentMxcUrl.toString(), 'mxc://example.com/file'); + expect(event.thumbnailMxcUrl.toString(), 'mxc://example.com/thumb'); + buffer = await event.downloadAndDecryptAttachment( + downloadCallback: downloadCallback); + expect(buffer.bytes, FILE_BUFF_DEC); + + buffer = await event.downloadAndDecryptAttachment( + getThumbnail: true, downloadCallback: downloadCallback); + expect(buffer.bytes, THUMB_BUFF_DEC); + + await room.client.dispose(closeDatabase: true); + }); + test('downloadAndDecryptAttachment store', () async { + final FILE_BUFF = Uint8List.fromList([0]); + var serverHits = 0; + final downloadCallback = (Uri uri) async { + serverHits++; + return { + '/_matrix/media/r0/download/example.org/newfile': FILE_BUFF, + }[uri.path]!; + }; + await client.checkHomeserver('https://fakeserver.notexisting', + checkWellKnown: false); + final room = Room(id: '!localpart:server.abc', client: await getClient()); + final event = Event.fromJson({ + 'type': EventTypes.Message, + 'content': { + 'body': 'image', + 'msgtype': 'm.image', + 'url': 'mxc://example.org/newfile', + 'info': { + 'size': 5, + }, + }, + 'event_id': '\$edit2', + 'sender': '@alice:example.org', + }, room); + expect(await event.isAttachmentInLocalStore(), false); + var buffer = await event.downloadAndDecryptAttachment( + downloadCallback: downloadCallback); + expect(await event.isAttachmentInLocalStore(), + event.room.client.database?.supportsFileStoring); + expect(buffer.bytes, FILE_BUFF); + expect(serverHits, 1); + buffer = await event.downloadAndDecryptAttachment( + downloadCallback: downloadCallback); + expect(buffer.bytes, FILE_BUFF); + expect( + serverHits, event.room.client.database!.supportsFileStoring ? 1 : 2); + + await room.client.dispose(closeDatabase: true); + }); + test('emote detection', () async { + var event = Event.fromJson({ + 'type': EventTypes.Message, + 'content': { + 'msgtype': 'm.text', + 'body': 'normal message', + }, + 'event_id': '\$edit2', + 'sender': '@alice:example.org', + }, room); + expect(event.onlyEmotes, false); + expect(event.numberEmotes, 0); + event = Event.fromJson({ + 'type': EventTypes.Message, + 'content': { + 'msgtype': 'm.text', + 'body': 'normal message\n\nvery normal', + }, + 'event_id': '\$edit2', + 'sender': '@alice:example.org', + }, room); + expect(event.onlyEmotes, false); + expect(event.numberEmotes, 0); + event = Event.fromJson({ + 'type': EventTypes.Message, + 'content': { + 'msgtype': 'm.text', + 'body': 'normal message with emoji 🦊', + }, + 'event_id': '\$edit2', + 'sender': '@alice:example.org', + }, room); + expect(event.onlyEmotes, false); + expect(event.numberEmotes, 1); + event = Event.fromJson({ + 'type': EventTypes.Message, + 'content': { + 'msgtype': 'm.text', + 'body': '🦊', + }, + 'event_id': '\$edit2', + 'sender': '@alice:example.org', + }, room); + expect(event.onlyEmotes, true); + expect(event.numberEmotes, 1); + event = Event.fromJson({ + 'type': EventTypes.Message, + 'content': { + 'msgtype': 'm.text', + 'body': '🦊🦊 🦊\n🦊🦊', + }, + 'event_id': '\$edit2', + 'sender': '@alice:example.org', + }, room); + expect(event.onlyEmotes, true); + expect(event.numberEmotes, 5); + event = Event.fromJson({ + 'type': EventTypes.Message, + 'content': { + 'msgtype': 'm.text', + 'body': 'rich message', + 'format': 'org.matrix.custom.html', + 'formatted_body': 'rich message' + }, + 'event_id': '\$edit2', + 'sender': '@alice:example.org', + }, room); + expect(event.onlyEmotes, false); + expect(event.numberEmotes, 0); + event = Event.fromJson({ + 'type': EventTypes.Message, + 'content': { + 'msgtype': 'm.text', + 'body': '🦊', + 'format': 'org.matrix.custom.html', + 'formatted_body': '🦊' + }, + 'event_id': '\$edit2', + 'sender': '@alice:example.org', + }, room); + expect(event.onlyEmotes, true); + expect(event.numberEmotes, 1); + event = Event.fromJson({ + 'type': EventTypes.Message, + 'content': { + 'msgtype': 'm.text', + 'body': ':blah:', + 'format': 'org.matrix.custom.html', + 'formatted_body': '' + }, + 'event_id': '\$edit2', + 'sender': '@alice:example.org', + }, room); + expect(event.onlyEmotes, true); + expect(event.numberEmotes, 1); + event = Event.fromJson({ + 'type': EventTypes.Message, + 'content': { + 'msgtype': 'm.text', + 'body': '🦊 :blah:', + 'format': 'org.matrix.custom.html', + 'formatted_body': '🦊 ' + }, + 'event_id': '\$edit2', + 'sender': '@alice:example.org', + }, room); + expect(event.onlyEmotes, true); + expect(event.numberEmotes, 2); + // with variant selector + event = Event.fromJson({ + 'type': EventTypes.Message, + 'content': { + 'msgtype': 'm.text', + 'body': '❤️', + }, + 'event_id': '\$edit2', + 'sender': '@alice:example.org', + }, room); + expect(event.onlyEmotes, true); + expect(event.numberEmotes, 1); + event = Event.fromJson({ + 'type': EventTypes.Message, + 'content': { + 'msgtype': 'm.text', + 'body': '''> <@alice:example.org> 😒😒 + + ❤❤❤''', + 'format': 'org.matrix.custom.html', + 'formatted_body': + '\
In reply to @alice:example.org
😒😒
❤❤❤' + }, + 'event_id': '\$edit2', + 'sender': '@alice:example.org', + }, room); + expect(event.onlyEmotes, true); + expect(event.numberEmotes, 3); + event = Event.fromJson({ + 'type': EventTypes.Message, + 'content': { + 'msgtype': 'm.text', + 'body': '''> <@alice:example.org> A 😒 + + ❤❤''', + 'format': 'org.matrix.custom.html', + 'formatted_body': + '\
In reply to @alice:example.org
A 😒
❤❤' + }, + 'event_id': '\$edit2', + 'sender': '@alice:example.org', + }, room); + expect(event.onlyEmotes, true); + expect(event.numberEmotes, 2); + event = Event.fromJson({ + 'type': EventTypes.Message, + 'content': { + 'msgtype': 'm.text', + 'body': '''> <@alice:example.org> 😒😒😒 + + ❤A❤''', + 'format': 'org.matrix.custom.html', + 'formatted_body': + '\
In reply to @alice:example.org
😒😒😒
❤A❤' + }, + 'event_id': '\$edit2', + 'sender': '@alice:example.org', + }, room); + expect(event.onlyEmotes, false); + expect(event.numberEmotes, 2); + event = Event.fromJson({ + 'type': EventTypes.Message, + 'content': { + 'msgtype': 'm.text', + 'body': '''> <@alice:example.org> A😒 + + ❤A❤''', + 'format': 'org.matrix.custom.html', + 'formatted_body': + '\
In reply to @alice:example.org
A😒
❤A❤' + }, + 'event_id': '\$edit2', + 'sender': '@alice:example.org', + }, room); + expect(event.onlyEmotes, false); + expect(event.numberEmotes, 2); + }); + }); +} diff --git a/test/fake_client.dart b/test/fake_client.dart new file mode 100644 index 0000000..0a13c70 --- /dev/null +++ b/test/fake_client.dart @@ -0,0 +1,50 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:matrix/matrix.dart'; + +import 'fake_matrix_api.dart'; +import 'fake_database.dart'; + +const ssssPassphrase = 'nae7ahDiequ7ohniufah3ieS2je1thohX4xeeka7aixohsho9O'; +const ssssKey = 'EsT9 RzbW VhPW yqNp cC7j ViiW 5TZB LuY4 ryyv 9guN Ysmr WDPH'; + +// key @test:fakeServer.notExisting +const pickledOlmAccount = + 'N2v1MkIFGcl0mQpo2OCwSopxPQJ0wnl7oe7PKiT4141AijfdTIhRu+ceXzXKy3Kr00nLqXtRv7kid6hU4a+V0rfJWLL0Y51+3Rp/ORDVnQy+SSeo6Fn4FHcXrxifJEJ0djla5u98fBcJ8BSkhIDmtXRPi5/oJAvpiYn+8zMjFHobOeZUAxYR0VfQ9JzSYBsSovoQ7uFkNks1M4EDUvHtuyg3RxViwdNxs3718fyAqQ/VSwbXsY0Nl+qQbF+nlVGHenGqk5SuNl1P6e1PzZxcR0IfXA94Xij1Ob5gDv5YH4UCn9wRMG0abZsQP0YzpDM0FLaHSCyo9i5JD/vMlhH+nZWrgAzPPCTNGYewNV8/h3c+VyJh8ZTx/fVi6Yq46Fv+27Ga2ETRZ3Qn+Oyx6dLBjnBZ9iUvIhqpe2XqaGA1PopOz8iDnaZitw'; + +Future getClient() async { + final client = Client( + 'testclient', + httpClient: FakeMatrixApi(), + databaseBuilder: getDatabase, + ); + FakeMatrixApi.client = client; + await client.checkHomeserver('https://fakeServer.notExisting', + checkWellKnown: false); + await client.init( + newToken: 'abcd', + newUserID: '@test:fakeServer.notExisting', + newHomeserver: client.homeserver, + newDeviceName: 'Text Matrix Client', + newDeviceID: 'GHTYAJCE', + newOlmAccount: pickledOlmAccount, + ); + await Future.delayed(Duration(milliseconds: 10)); + return client; +} diff --git a/test/fake_database.dart b/test/fake_database.dart new file mode 100644 index 0000000..d97fa66 --- /dev/null +++ b/test/fake_database.dart @@ -0,0 +1,55 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:io'; +import 'dart:math'; + +import 'package:matrix/matrix.dart'; +import 'package:file/memory.dart'; +import 'package:hive/hive.dart'; + +Future getDatabase(Client? _) => getHiveDatabase(_); + +bool hiveInitialized = false; + +Future getFluffyBoxDatabase(Client? c) async { + final fileSystem = MemoryFileSystem(); + final testHivePath = + '${fileSystem.path}/build/.test_store/${Random().nextDouble()}'; + Directory(testHivePath).createSync(recursive: true); + final db = FluffyBoxDatabase( + 'unit_test.${c?.hashCode}', + testHivePath, + ); + await db.open(); + return db; +} + +Future getHiveDatabase(Client? c) async { + if (!hiveInitialized) { + final fileSystem = MemoryFileSystem(); + final testHivePath = + '${fileSystem.path}/build/.test_store/${Random().nextDouble()}'; + Directory(testHivePath).createSync(recursive: true); + Hive.init(testHivePath); + hiveInitialized = true; + } + final db = FamedlySdkHiveDatabase('unit_test.${c?.hashCode}'); + await db.open(); + return db; +} diff --git a/test/fake_matrix_api.dart b/test/fake_matrix_api.dart new file mode 100644 index 0000000..93fad68 --- /dev/null +++ b/test/fake_matrix_api.dart @@ -0,0 +1,2277 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:convert'; +import 'dart:core'; +import 'dart:math'; + +import 'package:http/http.dart'; +import 'package:http/testing.dart'; +import 'package:matrix/matrix.dart' as sdk; +import 'package:matrix/matrix.dart'; + +Map decodeJson(dynamic data) { + if (data is String) { + return json.decode(data); + } + if (data.isEmpty) { + return {}; + } + return data; +} + +class FakeMatrixApi extends MockClient { + static final calledEndpoints = >{}; + static int eventCounter = 0; + static sdk.Client? client; + static bool failToDevice = false; + + FakeMatrixApi() + : super((request) async { + // Collect data from Request + var action = request.url.path; + if (request.url.path.contains('/_matrix')) { + action = request.url.path.split('/_matrix').last + + '?' + + request.url.query; + } + + if (action.endsWith('?')) { + action = action.substring(0, action.length - 1); + } + if (action.endsWith('?server_name')) { + // This can be removed after matrix_api_lite is released with: + // https://gitlab.com/famedly/libraries/matrix_api_lite/-/merge_requests/16 + action = action.substring(0, action.length - 12); + } + if (action.endsWith('/')) { + action = action.substring(0, action.length - 1); + } + final method = request.method; + final dynamic data = + method == 'GET' ? request.url.queryParameters : request.body; + dynamic res = {}; + var statusCode = 200; + + //print('\$method request to $action with Data: $data'); + + // Sync requests with timeout + if (data is Map && data['timeout'] is String) { + await Future.delayed(Duration(seconds: 5)); + } + + if (request.url.origin != 'https://fakeserver.notexisting') { + return Response( + 'Not found...', 404); + } + + // Call API + if (!calledEndpoints.containsKey(action)) { + calledEndpoints[action] = []; + } + calledEndpoints[action]?.add(data); + final act = api[method]?[action]; + if (act != null) { + res = act(data); + if (res is Map && res.containsKey('errcode')) { + if (res['errcode'] == 'M_NOT_FOUND') { + statusCode = 404; + } else { + statusCode = 405; + } + } + } else if (method == 'PUT' && + action.contains('/client/r0/sendToDevice/')) { + res = {}; + if (failToDevice) { + statusCode = 500; + } + } else if (method == 'GET' && + action.contains('/client/r0/rooms/') && + action.contains('/state/m.room.member/')) { + res = {'displayname': ''}; + } else if (method == 'PUT' && + action.contains( + '/client/r0/rooms/!1234%3AfakeServer.notExisting/send/')) { + res = {'event_id': '\$event${FakeMatrixApi.eventCounter++}'}; + } else if (method == 'PUT' && + action.contains( + '/client/r0/rooms/!1234%3AfakeServer.notExisting/state/')) { + res = {'event_id': '\$event${FakeMatrixApi.eventCounter++}'}; + } else if (action.contains('/client/r0/sync')) { + res = { + 'next_batch': DateTime.now().millisecondsSinceEpoch.toString(), + }; + } else if (method == 'PUT' && + client != null && + action.contains('/account_data/') && + !action.contains('/room/')) { + final type = Uri.decodeComponent(action.split('/').last); + final syncUpdate = sdk.SyncUpdate( + nextBatch: '', + accountData: [ + sdk.BasicEvent(content: decodeJson(data), type: type) + ], + ); + if (client?.database != null) { + await client?.database?.transaction(() async { + await client?.handleSync(syncUpdate); + }); + } else { + await client?.handleSync(syncUpdate); + } + res = {}; + } else { + res = { + 'errcode': 'M_UNRECOGNIZED', + 'error': 'Unrecognized request' + }; + statusCode = 405; + } + + return Response.bytes(utf8.encode(json.encode(res)), statusCode); + }); + + static Map messagesResponse = { + 'start': 't47429-4392820_219380_26003_2265', + 'end': 't47409-4357353_219380_26003_2265', + 'chunk': [ + { + 'content': { + 'body': 'This is an example text message', + 'msgtype': 'm.text', + 'format': 'org.matrix.custom.html', + 'formatted_body': 'This is an example text message' + }, + 'type': 'm.room.message', + 'event_id': '3143273582443PhrSn:example.org', + 'room_id': '!1234:example.com', + 'sender': '@example:example.org', + 'origin_server_ts': 1432735824653, + 'unsigned': {'age': 1234} + }, + { + 'content': {'name': 'The room name'}, + 'type': 'm.room.name', + 'event_id': '2143273582443PhrSn:example.org', + 'room_id': '!1234:example.com', + 'sender': '@example:example.org', + 'origin_server_ts': 1432735824653, + 'unsigned': {'age': 1234}, + 'state_key': '' + }, + { + 'content': { + 'body': 'Gangnam Style', + 'url': 'mxc://example.org/a526eYUSFFxlgbQYZmo442', + 'info': { + 'thumbnail_url': 'mxc://example.org/FHyPlCeYUSFFxlgbQYZmoEoe', + 'thumbnail_info': { + 'mimetype': 'image/jpeg', + 'size': 46144, + 'w': 300, + 'h': 300 + }, + 'w': 480, + 'h': 320, + 'duration': 2140786, + 'size': 1563685, + 'mimetype': 'video/mp4' + }, + 'msgtype': 'm.video' + }, + 'type': 'm.room.message', + 'event_id': '1143273582443PhrSn:example.org', + 'room_id': '!1234:example.com', + 'sender': '@example:example.org', + 'origin_server_ts': 1432735824653, + 'unsigned': {'age': 1234} + } + ], + 'state': [], + }; + + static Map syncResponse = { + 'next_batch': Random().nextDouble().toString(), + 'rooms': { + 'join': { + '!726s6s6q:example.com': { + 'summary': { + 'm.heroes': ['@alice:example.com', '@bob:example.com'], + 'm.joined_member_count': 2, + 'm.invited_member_count': 0 + }, + 'unread_notifications': { + 'highlight_count': 2, + 'notification_count': 2, + }, + 'state': { + 'events': [ + { + 'sender': '@alice:example.com', + 'type': 'm.room.member', + 'state_key': '@alice:example.com', + 'content': { + 'membership': 'join', + 'avatar_url': 'mxc://example.org/SEsfnsuifSDFSSEF', + 'displayname': 'Alice Margatroid', + }, + 'origin_server_ts': 1417731086795, + 'event_id': '66697273743031:example.com' + }, + { + 'sender': '@alice:example.com', + 'type': 'm.room.canonical_alias', + 'content': { + 'alias': '#famedlyContactDiscovery:fakeServer.notExisting' + }, + 'state_key': '', + 'origin_server_ts': 1417731086796, + 'event_id': '66697273743032:example.com' + }, + { + 'sender': '@alice:example.com', + 'type': 'm.room.encryption', + 'state_key': '', + 'content': {'algorithm': AlgorithmTypes.megolmV1AesSha2}, + 'origin_server_ts': 1417731086795, + 'event_id': '666972737430353:example.com' + }, + { + 'content': { + 'pinned': ['1234:bla'] + }, + 'type': 'm.room.pinned_events', + 'event_id': '21432735824443PhrSn:example.org', + 'room_id': '!1234:example.com', + 'sender': '@example:example.org', + 'origin_server_ts': 1432735824653, + 'unsigned': {'age': 1234}, + 'state_key': '' + }, + ] + }, + 'timeline': { + 'events': [ + { + 'sender': '@bob:example.com', + 'type': 'm.room.member', + 'state_key': '@bob:example.com', + 'content': {'membership': 'join'}, + 'prev_content': {'membership': 'invite'}, + 'origin_server_ts': 1417731086795, + 'event_id': '7365636s6r6432:example.com', + 'unsigned': {'foo': 'bar'} + }, + { + 'sender': '@alice:example.com', + 'type': 'm.room.message', + 'content': {'body': 'I am a fish', 'msgtype': 'm.text'}, + 'origin_server_ts': 1417731086797, + 'event_id': '74686972643033:example.com' + } + ], + 'limited': true, + 'prev_batch': 't34-23535_0_0' + }, + 'ephemeral': { + 'events': [ + { + 'type': 'm.typing', + 'content': { + 'user_ids': ['@alice:example.com'] + } + }, + { + 'content': { + '7365636s6r6432:example.com': { + 'm.read': { + '@alice:example.com': {'ts': 1436451550453} + } + } + }, + 'room_id': '!726s6s6q:example.com', + 'type': 'm.receipt' + } + ] + }, + 'account_data': { + 'events': [ + { + 'type': 'm.tag', + 'content': { + 'tags': { + 'work': {'order': 1} + } + } + }, + { + 'type': 'org.example.custom.room.config', + 'content': {'custom_config_key': 'custom_config_value'} + } + ] + } + } + }, + 'invite': { + '!696r7674:example.com': { + 'invite_state': { + 'events': [ + { + 'sender': '@alice:example.com', + 'type': 'm.room.name', + 'state_key': '', + 'content': {'name': 'My Room Name'} + }, + { + 'sender': '@alice:example.com', + 'type': 'm.room.member', + 'state_key': '@bob:example.com', + 'content': {'membership': 'invite'} + } + ] + } + } + }, + 'leave': { + '!726s6s6f:example.com': { + 'state': { + 'events': [ + { + 'sender': '@charley:example.com', + 'type': 'm.room.name', + 'state_key': '', + 'content': {'name': 'left room'}, + 'origin_server_ts': 1417731086795, + 'event_id': '66697273743031:example.com' + }, + ] + }, + 'timeline': { + 'events': [ + { + 'sender': '@bob:example.com', + 'type': 'm.room.message', + 'content': {'text': 'Hallo'}, + 'origin_server_ts': 1417731086795, + 'event_id': '7365636s6r64300:example.com', + 'unsigned': {'foo': 'bar'} + }, + ], + 'limited': true, + 'prev_batch': 't34-23535_0_0' + }, + 'account_data': { + 'events': [ + { + 'type': 'm.tag', + 'content': { + 'tags': { + 'work': {'order': 1} + } + } + }, + { + 'type': 'org.example.custom.room.config', + 'content': {'custom_config_key': 'custom_config_value'} + } + ] + } + } + }, + }, + 'presence': { + 'events': [ + { + 'sender': '@alice:example.com', + 'type': 'm.presence', + 'content': {'presence': 'online'} + } + ] + }, + 'account_data': { + 'events': [ + { + 'content': { + 'global': { + 'content': [ + { + 'actions': [ + 'notify', + {'set_tweak': 'sound', 'value': 'default'}, + {'set_tweak': 'highlight'} + ], + 'default': true, + 'enabled': true, + 'pattern': 'alice', + 'rule_id': '.m.rule.contains_user_name' + } + ], + 'override': [ + { + 'actions': ['dont_notify'], + 'conditions': [], + 'default': true, + 'enabled': false, + 'rule_id': '.m.rule.master' + }, + { + 'actions': ['dont_notify'], + 'conditions': [ + { + 'key': 'content.msgtype', + 'kind': 'event_match', + 'pattern': 'm.notice' + } + ], + 'default': true, + 'enabled': true, + 'rule_id': '.m.rule.suppress_notices' + } + ], + 'room': [ + { + 'actions': ['dont_notify'], + 'conditions': [ + { + 'key': 'room_id', + 'kind': 'event_match', + 'pattern': '!localpart:server.abc', + } + ], + 'default': true, + 'enabled': true, + 'rule_id': '!localpart:server.abc' + } + ], + 'sender': [], + 'underride': [ + { + 'actions': [ + 'notify', + {'set_tweak': 'sound', 'value': 'ring'}, + {'set_tweak': 'highlight', 'value': false} + ], + 'conditions': [ + { + 'key': 'type', + 'kind': 'event_match', + 'pattern': 'm.call.invite' + } + ], + 'default': true, + 'enabled': true, + 'rule_id': '.m.rule.call' + }, + { + 'actions': [ + 'notify', + {'set_tweak': 'sound', 'value': 'default'}, + {'set_tweak': 'highlight'} + ], + 'conditions': [ + {'kind': 'contains_display_name'} + ], + 'default': true, + 'enabled': true, + 'rule_id': '.m.rule.contains_display_name' + }, + { + 'actions': [ + 'notify', + {'set_tweak': 'sound', 'value': 'default'}, + {'set_tweak': 'highlight', 'value': false} + ], + 'conditions': [ + {'is': '2', 'kind': 'room_member_count'}, + { + 'key': 'type', + 'kind': 'event_match', + 'pattern': 'm.room.message' + } + ], + 'default': true, + 'enabled': true, + 'rule_id': '.m.rule.room_one_to_one' + }, + { + 'actions': [ + 'notify', + {'set_tweak': 'sound', 'value': 'default'}, + {'set_tweak': 'highlight', 'value': false} + ], + 'conditions': [ + { + 'key': 'type', + 'kind': 'event_match', + 'pattern': 'm.room.member' + }, + { + 'key': 'content.membership', + 'kind': 'event_match', + 'pattern': 'invite' + }, + { + 'key': 'state_key', + 'kind': 'event_match', + 'pattern': '@alice:example.com' + } + ], + 'default': true, + 'enabled': true, + 'rule_id': '.m.rule.invite_for_me' + }, + { + 'actions': [ + 'notify', + {'set_tweak': 'highlight', 'value': false} + ], + 'conditions': [ + { + 'key': 'type', + 'kind': 'event_match', + 'pattern': 'm.room.member' + } + ], + 'default': true, + 'enabled': true, + 'rule_id': '.m.rule.member_event' + }, + { + 'actions': [ + 'notify', + {'set_tweak': 'highlight', 'value': false} + ], + 'conditions': [ + { + 'key': 'type', + 'kind': 'event_match', + 'pattern': 'm.room.message' + } + ], + 'default': true, + 'enabled': true, + 'rule_id': '.m.rule.message' + } + ] + } + }, + 'type': 'm.push_rules' + }, + { + 'type': 'org.example.custom.config', + 'content': {'custom_config_key': 'custom_config_value'} + }, + { + 'content': { + '@bob:example.com': [ + '!726s6s6q:example.com', + '!hgfedcba:example.com' + ] + }, + 'type': 'm.direct' + }, + { + 'type': EventTypes.SecretStorageDefaultKey, + 'content': {'key': '0FajDWYaM6wQ4O60OZnLvwZfsBNu4Bu3'} + }, + { + 'type': 'm.secret_storage.key.0FajDWYaM6wQ4O60OZnLvwZfsBNu4Bu3', + 'content': { + 'algorithm': AlgorithmTypes.secretStorageV1AesHmcSha2, + 'passphrase': { + 'algorithm': AlgorithmTypes.pbkdf2, + 'iterations': 500000, + 'salt': 'F4jJ80mr0Fc8mRwU9JgA3lQDyjPuZXQL' + }, + 'iv': 'HjbTgIoQH2pI7jQo19NUzA==', + 'mac': 'QbJjQzDnAggU0cM4RBnDxw2XyarRGjdahcKukP9xVlk=' + } + }, + { + 'type': 'm.cross_signing.master', + 'content': { + 'encrypted': { + '0FajDWYaM6wQ4O60OZnLvwZfsBNu4Bu3': { + 'iv': 'eIb2IITxtmcq+1TrT8D5eQ==', + 'ciphertext': + 'lWRTPo5qxf4LAVwVPzGHOyMcP181n7bb9/B0lvkLDC2Oy4DvAL0eLx2x3bY=', + 'mac': 'Ynx89tIxPkx0o6ljMgxszww17JOgB4tg4etmNnMC9XI=' + } + } + } + }, + { + 'type': EventTypes.CrossSigningSelfSigning, + 'content': { + 'encrypted': { + '0FajDWYaM6wQ4O60OZnLvwZfsBNu4Bu3': { + 'iv': 'YqU2XIjYulYZl+bkZtGgVw==', + 'ciphertext': + 'kM2TSoy/jR/4d357ZoRPbpPypxQl6XRLo3FsEXz+f7vIOp82GeRp28RYb3k=', + 'mac': 'F+DZa5tAFmWsYSryw5EuEpzTmmABRab4GETkM85bGGo=' + } + } + } + }, + { + 'type': EventTypes.CrossSigningUserSigning, + 'content': { + 'encrypted': { + '0FajDWYaM6wQ4O60OZnLvwZfsBNu4Bu3': { + 'iv': 'D7AM3LXFu7ZlyGOkR+OeqQ==', + 'ciphertext': + 'bYA2+OMgsO6QB1E31aY+ESAWrT0fUBTXqajy4qmL7bVDSZY4Uj64EXNbHuA=', + 'mac': 'j2UtyPo/UBSoiaQCWfzCiRZXp3IRt0ZZujuXgUMjnw4=' + } + } + } + }, + { + 'type': EventTypes.MegolmBackup, + 'content': { + 'encrypted': { + '0FajDWYaM6wQ4O60OZnLvwZfsBNu4Bu3': { + 'iv': 'cL/0MJZaiEd3fNU+I9oJrw==', + 'ciphertext': + 'WL73Pzdk5wZdaaSpaeRH0uZYKcxkuV8IS6Qa2FEfA1+vMeRLuHcWlXbMX0w=', + 'mac': '+xozp909S6oDX8KRV8D8ZFVRyh7eEYQpPP76f+DOsnw=' + } + } + } + } + ] + }, + 'to_device': { + 'events': [ + { + 'sender': '@alice:example.com', + 'type': 'm.new_device', + 'content': { + 'device_id': 'XYZABCDE', + 'rooms': ['!726s6s6q:example.com'] + } + }, +// { +// 'sender': '@othertest:fakeServer.notExisting', +// 'content': { +// 'algorithm': AlgorithmTypes.megolmV1AesSha2, +// 'room_id': '!726s6s6q:example.com', +// 'session_id': 'ciM/JWTPrmiWPPZNkRLDPQYf9AW/I46bxyLSr+Bx5oU', +// 'session_key': +// 'AgAAAAAQcQ6XrFJk6Prm8FikZDqfry/NbDz8Xw7T6e+/9Yf/q3YHIPEQlzv7IZMNcYb51ifkRzFejVvtphS7wwG2FaXIp4XS2obla14iKISR0X74ugB2vyb1AydIHE/zbBQ1ic5s3kgjMFlWpu/S3FQCnCrv+DPFGEt3ERGWxIl3Bl5X53IjPyVkz65oljz2TZESwz0GH/QFvyOOm8ci0q/gceaF3S7Dmafg3dwTKYwcA5xkcc+BLyrLRzB6Hn+oMAqSNSscnm4mTeT5zYibIhrzqyUTMWr32spFtI9dNR/RFSzfCw' +// }, +// 'type': 'm.room_key' +// }, + { + // this is the commented out m.room_key event - only encrypted + 'sender': '@othertest:fakeServer.notExisting', + 'content': { + 'algorithm': AlgorithmTypes.olmV1Curve25519AesSha2, + 'sender_key': 'JBG7ZaPn54OBC7TuIEiylW3BZ+7WcGQhFBPB9pogbAg', + 'ciphertext': { + '7rvl3jORJkBiK4XX1e5TnGnqz068XfYJ0W++Ml63rgk': { + 'type': 0, + 'body': + 'Awogyh7K4iLUQjcOxIfi7q7LhBBqv9w0mQ6JI9+U9tv7iF4SIHC6xb5YFWf9voRnmDBbd+0vxD/xDlVNRDlPIKliLGkYGiAkEbtlo+fng4ELtO4gSLKVbcFn7tZwZCEUE8H2miBsCCKABgMKIFrKDJwB7gM3lXPt9yVoh6gQksafKt7VFCNRN5KLKqsDEAAi0AX5EfTV7jJ1ZWAbxftjoSN6kCVIxzGclbyg1HjchmNCX7nxNCHWl+q5ZgqHYZVu2n2mCVmIaKD0kvoEZeY3tV1Itb6zf67BLaU0qgW/QzHCHg5a44tNLjucvL2mumHjIG8k0BY2uh+52HeiMCvSOvtDwHg7nzCASGdqPVCj9Kzw6z7F6nL4e3mYim8zvJd7f+mD9z3ARrypUOLGkTGYbB2PQOovf0Do8WzcaRzfaUCnuu/YVZWKK7DPgG8uhw/TjR6XtraAKZysF+4DJYMG9SQWx558r6s7Z5EUOF5CU2M35w1t1Xxllb3vrS83dtf9LPCrBhLsEBeYEUBE2+bTBfl0BDKqLiB0Cc0N0ixOcHIt6e40wAvW622/gMgHlpNSx8xG12u0s6h6EMWdCXXLWd9fy2q6glFUHvA67A35q7O+M8DVml7Y9xG55Y3DHkMDc9cwgwFkBDCAYQe6pQF1nlKytcVCGREpBs/gq69gHAStMQ8WEg38Lf8u8eBr2DFexrN4U+QAk+S//P3fJgf0bQx/Eosx4fvWSz9En41iC+ADCsWQpMbwHn4JWvtAbn3oW0XmL/OgThTkJMLiCymduYAa1Hnt7a3tP0KTL2/x11F02ggQHL28cCjq5W4zUGjWjl5wo2PsKB6t8aAvMg2ujGD2rCjb4yrv5VIzAKMOZLyj7K0vSK9gwDLQ/4vq+QnKUBG5zrcOze0hX+kz2909/tmAdeCH61Ypw7gbPUJAKnmKYUiB/UgwkJvzMJSsk/SEs5SXosHDI+HsJHJp4Mp4iKD0xRMst+8f9aTjaWwh8ZvELE1ZOhhCbF3RXhxi3x2Nu8ORIz+vhEQ1NOlMc7UIo98Fk/96T36vL/fviowT4C/0AlaapZDJBmKwhmwqisMjY2n1vY29oM2p5BzY1iwP7q9BYdRFst6xwo57TNSuRwQw7IhFsf0k+ABuPEZy5xB5nPHyIRTf/pr3Hw', + }, + }, + }, + 'type': 'm.room.encrypted', + }, + ] + }, + 'device_lists': { + 'changed': [ + '@alice:example.com', + ], + 'left': [ + '@bob:example.com', + ], + }, + 'device_one_time_keys_count': {'curve25519': 10, 'signed_curve25519': 20}, + }; + + static Map archiveSyncResponse = { + 'next_batch': Random().nextDouble().toString(), + 'presence': {'events': []}, + 'account_data': {'events': []}, + 'to_device': {'events': []}, + 'rooms': { + 'join': {}, + 'invite': {}, + 'leave': { + '!5345234234:example.com': { + 'timeline': { + 'events': [ + { + 'content': { + 'body': 'This is an example text message', + 'msgtype': 'm.text', + 'format': 'org.matrix.custom.html', + 'formatted_body': 'This is an example text message' + }, + 'type': 'm.room.message', + 'event_id': '143273582443PhrSn:example.org', + 'room_id': '!5345234234:example.com', + 'sender': '@example:example.org', + 'origin_server_ts': 1432735824653, + 'unsigned': {'age': 1234} + }, + ] + }, + 'state': { + 'events': [ + { + 'content': {'name': 'The room name'}, + 'type': 'm.room.name', + 'event_id': '2143273582443PhrSn:example.org', + 'room_id': '!5345234234:example.com', + 'sender': '@example:example.org', + 'origin_server_ts': 1432735824653, + 'unsigned': {'age': 1234}, + 'state_key': '' + }, + ] + }, + 'account_data': { + 'events': [ + { + 'type': 'test.type.data', + 'content': {'foo': 'bar'}, + }, + ], + }, + }, + '!5345234235:example.com': { + 'timeline': {'events': []}, + 'state': { + 'events': [ + { + 'content': {'name': 'The room name 2'}, + 'type': 'm.room.name', + 'event_id': '2143273582443PhrSn:example.org', + 'room_id': '!5345234235:example.com', + 'sender': '@example:example.org', + 'origin_server_ts': 1432735824653, + 'unsigned': {'age': 1234}, + 'state_key': '' + }, + ] + } + }, + }, + } + }; + + static final Map> api = { + 'GET': { + '/path/to/auth/error': (var req) => { + 'errcode': 'M_FORBIDDEN', + 'error': 'Blabla', + }, + '/media/r0/preview_url?url=https%3A%2F%2Fmatrix.org&ts=10': (var req) => { + 'og:title': 'Matrix Blog Post', + 'og:description': 'This is a really cool blog post from matrix.org', + 'og:image': 'mxc://example.com/ascERGshawAWawugaAcauga', + 'og:image:type': 'image/png', + 'og:image:height': 48, + 'og:image:width': 48, + 'matrix:image:size': 102400 + }, + '/media/r0/config': (var req) => {'m.upload.size': 50000000}, + '/.well-known/matrix/client': (var req) => { + 'm.homeserver': {'base_url': 'https://matrix.example.com'}, + 'm.identity_server': {'base_url': 'https://identity.example.com'}, + 'org.example.custom.property': { + 'app_url': 'https://custom.app.example.org' + } + }, + '/client/r0/user/%40alice%3Aexample.com/rooms/!localpart%3Aexample.com/tags': + (var req) => { + 'tags': { + 'm.favourite': {'order': 0.1}, + 'u.Work': {'order': 0.7}, + 'u.Customers': {} + } + }, + '/client/r0/events?from=1234&timeout=10&roomId=%211234': (var req) => { + 'start': 's3456_9_0', + 'end': 's3457_9_0', + 'chunk': [ + { + 'content': { + 'body': 'This is an example text message', + 'msgtype': 'm.text', + 'format': 'org.matrix.custom.html', + 'formatted_body': 'This is an example text message' + }, + 'type': 'm.room.message', + 'event_id': '\$143273582443PhrSn:example.org', + 'room_id': '!somewhere:over.the.rainbow', + 'sender': '@example:example.org', + 'origin_server_ts': 1432735824653, + 'unsigned': {'age': 1234} + } + ] + }, + '/client/r0/thirdparty/location?alias=1234': (var req) => [ + { + 'alias': '#freenode_#matrix:matrix.org', + 'protocol': 'irc', + 'fields': {'network': 'freenode', 'channel': '#matrix'} + } + ], + '/client/r0/thirdparty/location/irc': (var req) => [ + { + 'alias': '#freenode_#matrix:matrix.org', + 'protocol': 'irc', + 'fields': {'network': 'freenode', 'channel': '#matrix'} + } + ], + '/client/r0/thirdparty/user/irc': (var req) => [ + { + 'userid': '@_gitter_jim:matrix.org', + 'protocol': 'gitter', + 'fields': {'user': 'jim'} + } + ], + '/client/r0/thirdparty/user?userid=1234': (var req) => [ + { + 'userid': '@_gitter_jim:matrix.org', + 'protocol': 'gitter', + 'fields': {'user': 'jim'} + } + ], + '/client/r0/thirdparty/protocol/irc': (var req) => { + 'user_fields': ['network', 'nickname'], + 'location_fields': ['network', 'channel'], + 'icon': 'mxc://example.org/aBcDeFgH', + 'field_types': { + 'network': { + 'regexp': '([a-z0-9]+\\.)*[a-z0-9]+', + 'placeholder': 'irc.example.org' + }, + 'nickname': {'regexp': '[^\\s#]+', 'placeholder': 'username'}, + 'channel': {'regexp': '#[^\\s]+', 'placeholder': '#foobar'} + }, + 'instances': [ + { + 'desc': 'Freenode', + 'icon': 'mxc://example.org/JkLmNoPq', + 'fields': {'network': 'freenode'}, + 'network_id': 'freenode' + } + ] + }, + '/client/r0/thirdparty/protocols': (var req) => { + 'irc': { + 'user_fields': ['network', 'nickname'], + 'location_fields': ['network', 'channel'], + 'icon': 'mxc://example.org/aBcDeFgH', + 'field_types': { + 'network': { + 'regexp': '([a-z0-9]+\\.)*[a-z0-9]+', + 'placeholder': 'irc.example.org' + }, + 'nickname': {'regexp': '[^\\s]+', 'placeholder': 'username'}, + 'channel': {'regexp': '#[^\\s]+', 'placeholder': '#foobar'} + }, + 'instances': [ + { + 'network_id': 'freenode', + 'desc': 'Freenode', + 'icon': 'mxc://example.org/JkLmNoPq', + 'fields': {'network': 'freenode.net'} + } + ] + }, + 'gitter': { + 'user_fields': ['username'], + 'location_fields': ['room'], + 'icon': 'mxc://example.org/aBcDeFgH', + 'field_types': { + 'username': {'regexp': '@[^\\s]+', 'placeholder': '@username'}, + 'room': { + 'regexp': '[^\\s]+\\/[^\\s]+', + 'placeholder': 'matrix-org/matrix-doc' + } + }, + 'instances': [ + { + 'network_id': 'gitter', + 'desc': 'Gitter', + 'icon': 'mxc://example.org/zXyWvUt', + 'fields': {} + } + ] + } + }, + '/client/r0/account/whoami': (var req) => + {'user_id': 'alice@example.com'}, + '/client/r0/capabilities': (var req) => { + 'capabilities': { + 'm.change_password': {'enabled': false}, + 'm.room_versions': { + 'default': '1', + 'available': { + '1': 'stable', + '2': 'stable', + '3': 'unstable', + 'test-version': 'unstable' + } + }, + 'com.example.custom.ratelimit': {'max_requests_per_hour': 600} + } + }, + '/client/r0/rooms/1234/context/1234?filter=%7B%7D&limit=10': (var req) => + { + 'end': 't29-57_2_0_2', + 'events_after': [ + { + 'content': { + 'body': 'This is an example text message', + 'msgtype': 'm.text', + 'format': 'org.matrix.custom.html', + 'formatted_body': 'This is an example text message' + }, + 'type': 'm.room.message', + 'event_id': '\$143273582443PhrSn:example.org', + 'room_id': '!636q39766251:example.com', + 'sender': '@example:example.org', + 'origin_server_ts': 1432735824653, + 'unsigned': {'age': 1234} + } + ], + 'event': { + 'content': { + 'body': 'filename.jpg', + 'info': { + 'h': 398, + 'w': 394, + 'mimetype': 'image/jpeg', + 'size': 31037 + }, + 'url': 'mxc://example.org/JWEIFJgwEIhweiWJE', + 'msgtype': 'm.image' + }, + 'type': 'm.room.message', + 'event_id': '\$f3h4d129462ha:example.com', + 'room_id': '!636q39766251:example.com', + 'sender': '@example:example.org', + 'origin_server_ts': 1432735824653, + 'unsigned': {'age': 1234} + }, + 'events_before': [ + { + 'content': { + 'body': 'something-important.doc', + 'filename': 'something-important.doc', + 'info': {'mimetype': 'application/msword', 'size': 46144}, + 'msgtype': 'm.file', + 'url': 'mxc://example.org/FHyPlCeYUSFFxlgbQYZmoEoe' + }, + 'type': 'm.room.message', + 'event_id': '\$143273582443PhrSn:example.org', + 'room_id': '!636q39766251:example.com', + 'sender': '@example:example.org', + 'origin_server_ts': 1432735824653, + 'unsigned': {'age': 1234} + } + ], + 'start': 't27-54_2_0_2', + 'state': [ + { + 'content': { + 'creator': '@example:example.org', + 'room_version': '1', + 'm.federate': true, + 'predecessor': { + 'event_id': '\$something:example.org', + 'room_id': '!oldroom:example.org' + } + }, + 'type': 'm.room.create', + 'event_id': '\$143273582443PhrSn:example.org', + 'room_id': '!636q39766251:example.com', + 'sender': '@example:example.org', + 'origin_server_ts': 1432735824653, + 'unsigned': {'age': 1234}, + 'state_key': '' + }, + { + 'content': { + 'membership': 'join', + 'avatar_url': 'mxc://example.org/SEsfnsuifSDFSSEF', + 'displayname': 'Alice Margatroid' + }, + 'type': 'm.room.member', + 'event_id': '\$143273582443PhrSn:example.org', + 'room_id': '!636q39766251:example.com', + 'sender': '@example:example.org', + 'origin_server_ts': 1432735824653, + 'unsigned': {'age': 1234}, + 'state_key': '@alice:example.org' + } + ] + }, + '/client/r0/admin/whois/%40alice%3Aexample.com': (var req) => { + 'user_id': '@peter:rabbit.rocks', + 'devices': { + 'teapot': { + 'sessions': [ + { + 'connections': [ + { + 'ip': '127.0.0.1', + 'last_seen': 1411996332123, + 'user_agent': 'curl/7.31.0-DEV' + }, + { + 'ip': '10.0.0.2', + 'last_seen': 1411996332123, + 'user_agent': + 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36' + } + ] + } + ] + } + } + }, + '/client/r0/user/%40alice%3Aexample.com/account_data/test.account.data': + (var req) => {'foo': 'bar'}, + '/client/r0/user/%40alice%3Aexample.com/rooms/1234/account_data/test.account.data': + (var req) => {'foo': 'bar'}, + '/client/r0/directory/room/%23testalias%3Aexample.com': (var reqI) => { + 'room_id': '!abnjk1jdasj98:capuchins.com', + 'servers': ['capuchins.com', 'matrix.org', 'another.com'] + }, + '/client/r0/account/3pid': (var req) => { + 'threepids': [ + { + 'medium': 'email', + 'address': 'monkey@banana.island', + 'validated_at': 1535176800000, + 'added_at': 1535336848756 + } + ] + }, + '/client/r0/devices': (var req) => { + 'devices': [ + { + 'device_id': 'QBUAZIFURK', + 'display_name': 'android', + 'last_seen_ip': '1.2.3.4', + 'last_seen_ts': 1474491775024 + } + ] + }, + '/client/r0/notifications?from=1234&limit=10&only=1234': (var req) => { + 'next_token': 'abcdef', + 'notifications': [ + { + 'actions': ['notify'], + 'profile_tag': 'hcbvkzxhcvb', + 'read': true, + 'room_id': '!abcdefg:example.com', + 'ts': 1475508881945, + 'event': { + 'content': { + 'body': 'This is an example text message', + 'msgtype': 'm.text', + 'format': 'org.matrix.custom.html', + 'formatted_body': 'This is an example text message' + }, + 'type': 'm.room.message', + 'event_id': '\$143273582443PhrSn:example.org', + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'origin_server_ts': 1432735824653, + 'unsigned': {'age': 1234} + } + } + ] + }, + '/client/r0/devices/QBUAZIFURK': (var req) => { + 'device_id': 'QBUAZIFURK', + 'display_name': 'android', + 'last_seen_ip': '1.2.3.4', + 'last_seen_ts': 1474491775024 + }, + '/client/r0/profile/%40alice%3Aexample.com/displayname': (var reqI) => + {'displayname': 'Alice M'}, + '/client/r0/profile/%40alice%3Aexample.com/avatar_url': (var reqI) => + {'avatar_url': 'mxc://test'}, + '/client/r0/profile/%40alice%3Aexample.com': (var reqI) => { + 'avatar_url': 'mxc://test', + 'displayname': 'Alice M', + }, + '/client/r0/voip/turnServer': (var req) => { + 'username': '1443779631:@user:example.com', + 'password': 'JlKfBy1QwLrO20385QyAtEyIv0=', + 'uris': [ + 'turn:turn.example.com:3478?transport=udp', + 'turn:10.20.30.40:3478?transport=tcp', + 'turns:10.20.30.40:443?transport=tcp' + ], + 'ttl': 86400 + }, + '/client/r0/presence/${Uri.encodeComponent('@alice:example.com')}/status': + (var req) => { + 'presence': 'unavailable', + 'last_active_ago': 420845, + 'status_msg': 'test', + 'currently_active': false + }, + '/client/r0/keys/changes?from=1234&to=1234': (var req) => { + 'changed': ['@alice:example.com', '@bob:example.org'], + 'left': ['@clara:example.com', '@doug:example.org'] + }, + '/client/r0/pushers': (var req) => { + 'pushers': [ + { + 'pushkey': 'Xp/MzCt8/9DcSNE9cuiaoT5Ac55job3TdLSSmtmYl4A=', + 'kind': 'http', + 'app_id': 'face.mcapp.appy.prod', + 'app_display_name': 'Appy McAppface', + 'device_display_name': 'Alices Phone', + 'profile_tag': 'xyz', + 'lang': 'en-US', + 'data': { + 'url': 'https://example.com/_matrix/push/v1/notify', + 'format': 'event_id_only', + } + } + ] + }, + '/client/r0/publicRooms?limit=10&since=1234&server=example.com': + (var req) => { + 'chunk': [ + { + 'aliases': ['#murrays:cheese.bar'], + 'canonical_alias': '#murrays:cheese.bar', + 'avatar_url': 'mxc://bleeker.street/CHEDDARandBRIE', + 'guest_can_join': false, + 'name': 'CHEESE', + 'num_joined_members': 37, + 'room_id': '!ol19s:bleecker.street', + 'topic': 'Tasty tasty cheese', + 'world_readable': true + } + ], + 'next_batch': 'p190q', + 'prev_batch': 'p1902', + 'total_room_count_estimate': 115 + }, + '/client/r0/room/!localpart%3Aexample.com/aliases': (var req) => { + 'aliases': [ + '#somewhere:example.com', + '#another:example.com', + '#hat_trick:example.com' + ] + }, + '/client/r0/joined_rooms': (var req) => { + 'joined_rooms': ['!foo:example.com'] + }, + '/client/r0/directory/list/room/!localpart%3Aexample.com': (var req) => + {'visibility': 'public'}, + '/client/r0/rooms/1/state/m.room.member/@alice:example.com': (var req) => + {'displayname': 'Alice'}, + '/client/r0/profile/%40getme%3Aexample.com': (var req) => { + 'avatar_url': 'mxc://test', + 'displayname': 'You got me', + }, + '/client/r0/rooms/!localpart%3Aserver.abc/state/m.room.member/@getme%3Aexample.com': + (var req) => { + 'avatar_url': 'mxc://test', + 'displayname': 'You got me', + }, + '/client/r0/rooms/!localpart%3Aserver.abc/state': (var req) => [ + { + 'content': {'join_rule': 'public'}, + 'type': 'm.room.join_rules', + 'event_id': '\$143273582443PhrSn:example.org', + 'room_id': '!636q39766251:example.com', + 'sender': '@example:example.org', + 'origin_server_ts': 1432735824653, + 'unsigned': {'age': 1234}, + 'state_key': '' + }, + { + 'content': { + 'membership': 'join', + 'avatar_url': 'mxc://example.org/SEsfnsuifSDFSSEF', + 'displayname': 'Alice Margatroid' + }, + 'type': 'm.room.member', + 'event_id': '\$143273582443PhrSn:example.org', + 'room_id': '!636q39766251:example.com', + 'sender': '@example:example.org', + 'origin_server_ts': 1432735824653, + 'unsigned': {'age': 1234}, + 'state_key': '@alice:example.org' + }, + { + 'content': { + 'creator': '@example:example.org', + 'room_version': '1', + 'm.federate': true, + 'predecessor': { + 'event_id': '\$something:example.org', + 'room_id': '!oldroom:example.org' + } + }, + 'type': 'm.room.create', + 'event_id': '\$143273582443PhrSn:example.org', + 'room_id': '!636q39766251:example.com', + 'sender': '@example:example.org', + 'origin_server_ts': 1432735824653, + 'unsigned': {'age': 1234}, + 'state_key': '' + }, + { + 'content': { + 'ban': 50, + 'events': {'m.room.name': 100, 'm.room.power_levels': 100}, + 'events_default': 0, + 'invite': 50, + 'kick': 50, + 'redact': 50, + 'state_default': 50, + 'users': {'@example:localhost': 100}, + 'users_default': 0, + 'notifications': {'room': 20} + }, + 'type': 'm.room.power_levels', + 'event_id': '\$143273582443PhrSn:example.org', + 'room_id': '!636q39766251:example.com', + 'sender': '@example:example.org', + 'origin_server_ts': 1432735824653, + 'unsigned': {'age': 1234}, + 'state_key': '' + } + ], + '/client/r0/rooms/!localpart:server.abc/state/m.room.member/@getme:example.com': + (var req) => { + 'avatar_url': 'mxc://test', + 'displayname': 'You got me', + }, + '/client/r0/rooms/!localpart:server.abc/event/1234': (var req) => { + 'content': { + 'body': 'This is an example text message', + 'msgtype': 'm.text', + 'format': 'org.matrix.custom.html', + 'formatted_body': 'This is an example text message' + }, + 'type': 'm.room.message', + 'event_id': '143273582443PhrSn:example.org', + 'room_id': '!localpart:server.abc', + 'sender': '@example:example.org', + 'origin_server_ts': 1432735824653, + 'unsigned': {'age': 1234} + }, + '/client/r0/rooms/!localpart%3Aserver.abc/event/1234': (var req) => { + 'content': { + 'body': 'This is an example text message', + 'msgtype': 'm.text', + 'format': 'org.matrix.custom.html', + 'formatted_body': 'This is an example text message' + }, + 'type': 'm.room.message', + 'event_id': '143273582443PhrSn:example.org', + 'room_id': '!localpart:server.abc', + 'sender': '@example:example.org', + 'origin_server_ts': 1432735824653, + 'unsigned': {'age': 1234} + }, + '/client/r0/rooms/!1234%3Aexample.com/event/not_found': (var req) => { + 'errcode': 'M_NOT_FOUND', + 'error': 'Event not found', + }, + '/client/r0/rooms/!1234%3Aexample.com/event/unencrypted_event': + (var req) => { + 'content': { + 'body': 'This is an example text message', + 'msgtype': 'm.text', + 'format': 'org.matrix.custom.html', + 'formatted_body': 'This is an example text message' + }, + 'type': 'm.room.message', + 'event_id': '143273582443PhrSn:example.org', + 'room_id': '!localpart:server.abc', + 'sender': '@example:example.org', + 'origin_server_ts': 1432735824653, + 'unsigned': {'age': 1234} + }, + '/client/r0/rooms/!1234%3Aexample.com/event/encrypted_event': (var req) => + { + 'content': { + 'algorithm': 'm.megolm.v1.aes-sha2', + 'ciphertext': 'invalid', + 'device_id': 'SOME_DEVICE', + 'sender_key': 'invalid', + 'session_id': 'not_found' + }, + 'type': 'm.room.encrypted', + 'event_id': '143273582443PhrSn:example.org', + 'room_id': '!localpart:server.abc', + 'sender': '@example:example.org', + 'origin_server_ts': 1432735824653, + 'unsigned': {'age': 1234} + }, + '/client/r0/rooms/!localpart%3Aserver.abc/messages?from=1234&dir=b&to=1234&limit=10&filter=%7B%22lazy_load_members%22%3Atrue%7D': + (var req) => messagesResponse, + '/client/r0/rooms/!localpart%3Aserver.abc/messages?from=&dir=b&limit=10&filter=%7B%22lazy_load_members%22%3Atrue%7D': + (var req) => messagesResponse, + '/client/r0/rooms/!1234%3Aexample.com/messages?from=1234&dir=b&limit=30&filter=%7B%22lazy_load_members%22%3Atrue%7D': + (var req) => messagesResponse, + '/client/versions': (var req) => { + 'versions': [ + 'r0.0.1', + 'r0.1.0', + 'r0.2.0', + 'r0.3.0', + 'r0.4.0', + 'r0.5.0' + ], + 'unstable_features': {'m.lazy_load_members': true}, + }, + '/client/r0/login': (var req) => { + 'flows': [ + {'type': 'm.login.password'} + ] + }, + '/client/r0/rooms/!localpart%3Aserver.abc/joined_members': (var req) => { + 'joined': { + '@bar:example.com': { + 'display_name': 'Bar', + 'avatar_url': 'mxc://riot.ovh/printErCATzZijQsSDWorRaK' + } + } + }, + '/client/r0/rooms/!localpart%3Aserver.abc/members?at=1234&membership=join¬_membership=leave': + (var req) => { + 'chunk': [ + { + 'content': { + 'membership': 'join', + 'avatar_url': 'mxc://example.org/SEsfnsuifSDFSSEF', + 'displayname': 'Alice Margatroid' + }, + 'type': 'm.room.member', + 'event_id': '§143273582443PhrSn:example.org', + 'room_id': '!636q39766251:example.com', + 'sender': '@alice:example.com', + 'origin_server_ts': 1432735824653, + 'unsigned': {'age': 1234}, + 'state_key': '@alice:example.com' + } + ] + }, + '/client/r0/rooms/!696r7674:example.com/members': (var req) => { + 'chunk': [ + { + 'content': { + 'membership': 'join', + 'avatar_url': 'mxc://example.org/SEsfnsuifSDFSSEF', + 'displayname': 'Alice Margatroid' + }, + 'type': 'm.room.member', + 'event_id': '§143273582443PhrSn:example.org', + 'room_id': '!636q39766251:example.com', + 'sender': '@alice:example.com', + 'origin_server_ts': 1432735824653, + 'unsigned': {'age': 1234}, + 'state_key': '@alice:example.com' + } + ] + }, + '/client/r0/rooms/!726s6s6q:example.com/members': (var req) => { + 'chunk': [ + { + 'content': { + 'membership': 'join', + 'avatar_url': 'mxc://example.org/SEsfnsuifSDFSSEF', + 'displayname': 'Alice Margatroid' + }, + 'type': 'm.room.member', + 'event_id': '§143273582443PhrSn:example.org', + 'room_id': '!636q39766251:example.com', + 'sender': '@alice:example.com', + 'origin_server_ts': 1432735824653, + 'unsigned': {'age': 1234}, + 'state_key': '@alice:example.com' + } + ] + }, + '/client/r0/rooms/!localpart%3Aserver.abc/members': (var req) => { + 'chunk': [ + { + 'content': { + 'membership': 'join', + 'avatar_url': 'mxc://example.org/SEsfnsuifSDFSSEF', + 'displayname': 'Alice Margatroid' + }, + 'type': 'm.room.member', + 'event_id': '§143273582443PhrSn:example.org', + 'room_id': '!636q39766251:example.com', + 'sender': '@example:example.org', + 'origin_server_ts': 1432735824653, + 'unsigned': {'age': 1234}, + 'state_key': '@alice:example.org' + } + ] + }, + '/client/r0/pushrules/global/content/nocake': (var req) => { + 'actions': ['dont_notify'], + 'pattern': 'cake*lie', + 'rule_id': 'nocake', + 'enabled': true, + 'default': false + }, + '/client/r0/pushrules/global/content/nocake/enabled': (var req) => { + 'enabled': true, + }, + '/client/r0/pushrules/global/content/nocake/actions': (var req) => { + 'actions': ['notify'] + }, + '/client/r0/pushrules': (var req) => { + 'global': { + 'content': [ + { + 'actions': [ + 'notify', + {'set_tweak': 'sound', 'value': 'default'}, + {'set_tweak': 'highlight'} + ], + 'default': true, + 'enabled': true, + 'pattern': 'alice', + 'rule_id': '.m.rule.contains_user_name' + } + ], + 'override': [ + { + 'actions': ['dont_notify'], + 'conditions': [], + 'default': true, + 'enabled': false, + 'rule_id': '.m.rule.master' + }, + { + 'actions': ['dont_notify'], + 'conditions': [ + { + 'key': 'content.msgtype', + 'kind': 'event_match', + 'pattern': 'm.notice' + } + ], + 'default': true, + 'enabled': true, + 'rule_id': '.m.rule.suppress_notices' + } + ], + 'room': [], + 'sender': [], + 'underride': [ + { + 'actions': [ + 'notify', + {'set_tweak': 'sound', 'value': 'ring'}, + {'set_tweak': 'highlight', 'value': false} + ], + 'conditions': [ + { + 'key': 'type', + 'kind': 'event_match', + 'pattern': 'm.call.invite' + } + ], + 'default': true, + 'enabled': true, + 'rule_id': '.m.rule.call' + }, + { + 'actions': [ + 'notify', + {'set_tweak': 'sound', 'value': 'default'}, + {'set_tweak': 'highlight'} + ], + 'conditions': [ + {'kind': 'contains_display_name'} + ], + 'default': true, + 'enabled': true, + 'rule_id': '.m.rule.contains_display_name' + }, + { + 'actions': [ + 'notify', + {'set_tweak': 'sound', 'value': 'default'}, + {'set_tweak': 'highlight', 'value': false} + ], + 'conditions': [ + {'is': '2', 'kind': 'room_member_count'} + ], + 'default': true, + 'enabled': true, + 'rule_id': '.m.rule.room_one_to_one' + }, + { + 'actions': [ + 'notify', + {'set_tweak': 'sound', 'value': 'default'}, + {'set_tweak': 'highlight', 'value': false} + ], + 'conditions': [ + { + 'key': 'type', + 'kind': 'event_match', + 'pattern': 'm.room.member' + }, + { + 'key': 'content.membership', + 'kind': 'event_match', + 'pattern': 'invite' + }, + { + 'key': 'state_key', + 'kind': 'event_match', + 'pattern': '@alice:example.com' + } + ], + 'default': true, + 'enabled': true, + 'rule_id': '.m.rule.invite_for_me' + }, + { + 'actions': [ + 'notify', + {'set_tweak': 'highlight', 'value': false} + ], + 'conditions': [ + { + 'key': 'type', + 'kind': 'event_match', + 'pattern': 'm.room.member' + } + ], + 'default': true, + 'enabled': true, + 'rule_id': '.m.rule.member_event' + }, + { + 'actions': [ + 'notify', + {'set_tweak': 'highlight', 'value': false} + ], + 'conditions': [ + { + 'key': 'type', + 'kind': 'event_match', + 'pattern': 'm.room.message' + } + ], + 'default': true, + 'enabled': true, + 'rule_id': '.m.rule.message' + } + ] + } + }, + '/client/r0/sync?filter=%7B%22room%22%3A%7B%22include_leave%22%3Atrue%2C%22timeline%22%3A%7B%22limit%22%3A10%7D%7D%7D&timeout=0': + (var req) => archiveSyncResponse, + '/client/r0/sync?filter=1234': (var req) => syncResponse, + '/client/r0/sync?filter=1234&since=1234&full_state=false&set_presence=unavailable&timeout=15': + (var req) => syncResponse, + '/client/r0/register/available?username=testuser': (var req) => + {'available': true}, + '/client/r0/user/${Uri.encodeComponent('@test:fakeServer.notExisting')}/filter/1234': + (var req) => { + 'room': { + 'state': { + 'types': ['m.room.*'], + 'not_rooms': ['!726s6s6q:example.com'] + }, + 'timeline': { + 'limit': 10, + 'types': ['m.room.message'], + 'not_rooms': ['!726s6s6q:example.com'], + 'not_senders': ['@spam:example.com'] + }, + 'ephemeral': { + 'types': ['m.receipt', 'm.typing'], + 'not_rooms': ['!726s6s6q:example.com'], + 'not_senders': ['@spam:example.com'] + }, + 'account_data': { + 'types': ['m.receipt', 'm.typing'], + 'not_rooms': ['!726s6s6q:example.com'], + 'not_senders': ['@spam:example.com'] + } + }, + 'presence': { + 'types': ['m.presence'], + 'not_senders': ['@alice:example.com'] + }, + 'event_format': 'client', + 'event_fields': ['type', 'content', 'sender'] + }, + '/client/unstable/room_keys/version': (var req) => { + 'algorithm': AlgorithmTypes.megolmBackupV1Curve25519AesSha2, + 'auth_data': { + 'public_key': 'GXYaxqhNhUK28zUdxOmEsFRguz+PzBsDlTLlF0O0RkM', + 'signatures': {}, + }, + 'count': 0, + 'etag': '0', + 'version': '5', + }, + '/client/unstable/room_keys/keys/${Uri.encodeComponent('!726s6s6q:example.com')}/${Uri.encodeComponent('ciM/JWTPrmiWPPZNkRLDPQYf9AW/I46bxyLSr+Bx5oU')}?version=5': + (var req) => { + 'first_message_index': 0, + 'forwarded_count': 0, + 'is_verified': true, + 'session_data': { + 'ephemeral': 'fwRxYh+seqLykz5mQCLypJ4/59URdcFJ2s69OU1dGRc', + 'ciphertext': + '19jkQYlbgdP+VL9DH3qY/Dvpk6onJZgf+6frZFl1TinPCm9OMK9AZZLuM1haS9XLAUK1YsREgjBqfl6T+Tq8JlJ5ONZGg2Wttt24sGYc0iTMZJ8rXcNDeKMZhM96ETyjufJSeYoXLqifiVLDw9rrVBmNStF7PskYp040em+0OZ4pF85Cwsdf7l9V7MMynzh9BoXqVUCBiwT03PNYH9AEmNUxXX+6ZwCpe/saONv8MgGt5uGXMZIK29phA3D8jD6uV/WOHsB8NjHNq9FrfSEAsl+dAcS4uiYie4BKSSeQN+zGAQqu1MMW4OAdxGOuf8WpIINx7n+7cKQfxlmc/Cgg5+MmIm2H0oDwQ+Xu7aSxp1OCUzbxQRdjz6+tnbYmZBuH0Ov2RbEvC5tDb261LRqKXpub0llg5fqKHl01D0ahv4OAQgRs5oU+4mq+H2QGTwIFGFqP9tCRo0I+aICawpxYOfoLJpFW6KvEPnM2Lr3sl6Nq2fmkz6RL5F7nUtzxN8OKazLQpv8DOYzXbi7+ayEsqS0/EINetq7RfCqgjrEUgfNWYuFXWqvUT8lnxLdNu+8cyrJqh1UquFjXWTw1kWcJ0pkokVeBtK9YysCnF1UYh/Iv3rl2ZoYSSLNtuvMSYlYHggZ8xV8bz9S3X2/NwBycBiWIy5Ou/OuSX7trIKgkkmda0xjBWEM1a2acVuqu2OFbMn2zFxm2a3YwKP//OlIgMg', + 'mac': 'QzKV/fgAs4U', + }, + }, + '/client/unstable/room_keys/keys/${Uri.encodeComponent('!726s6s6q:example.com')}?version=5': + (var req) => { + 'sessions': { + 'ciM/JWTPrmiWPPZNkRLDPQYf9AW/I46bxyLSr+Bx5oU': { + 'first_message_index': 0, + 'forwarded_count': 0, + 'is_verified': true, + 'session_data': { + 'ephemeral': + 'fwRxYh+seqLykz5mQCLypJ4/59URdcFJ2s69OU1dGRc', + 'ciphertext': + '19jkQYlbgdP+VL9DH3qY/Dvpk6onJZgf+6frZFl1TinPCm9OMK9AZZLuM1haS9XLAUK1YsREgjBqfl6T+Tq8JlJ5ONZGg2Wttt24sGYc0iTMZJ8rXcNDeKMZhM96ETyjufJSeYoXLqifiVLDw9rrVBmNStF7PskYp040em+0OZ4pF85Cwsdf7l9V7MMynzh9BoXqVUCBiwT03PNYH9AEmNUxXX+6ZwCpe/saONv8MgGt5uGXMZIK29phA3D8jD6uV/WOHsB8NjHNq9FrfSEAsl+dAcS4uiYie4BKSSeQN+zGAQqu1MMW4OAdxGOuf8WpIINx7n+7cKQfxlmc/Cgg5+MmIm2H0oDwQ+Xu7aSxp1OCUzbxQRdjz6+tnbYmZBuH0Ov2RbEvC5tDb261LRqKXpub0llg5fqKHl01D0ahv4OAQgRs5oU+4mq+H2QGTwIFGFqP9tCRo0I+aICawpxYOfoLJpFW6KvEPnM2Lr3sl6Nq2fmkz6RL5F7nUtzxN8OKazLQpv8DOYzXbi7+ayEsqS0/EINetq7RfCqgjrEUgfNWYuFXWqvUT8lnxLdNu+8cyrJqh1UquFjXWTw1kWcJ0pkokVeBtK9YysCnF1UYh/Iv3rl2ZoYSSLNtuvMSYlYHggZ8xV8bz9S3X2/NwBycBiWIy5Ou/OuSX7trIKgkkmda0xjBWEM1a2acVuqu2OFbMn2zFxm2a3YwKP//OlIgMg', + 'mac': 'QzKV/fgAs4U', + }, + }, + }, + }, + '/client/unstable/room_keys/keys?version=5': (var req) => { + 'rooms': { + '!726s6s6q:example.com': { + 'sessions': { + 'ciM/JWTPrmiWPPZNkRLDPQYf9AW/I46bxyLSr+Bx5oU': { + 'first_message_index': 0, + 'forwarded_count': 0, + 'is_verified': true, + 'session_data': { + 'ephemeral': + 'fwRxYh+seqLykz5mQCLypJ4/59URdcFJ2s69OU1dGRc', + 'ciphertext': + '19jkQYlbgdP+VL9DH3qY/Dvpk6onJZgf+6frZFl1TinPCm9OMK9AZZLuM1haS9XLAUK1YsREgjBqfl6T+Tq8JlJ5ONZGg2Wttt24sGYc0iTMZJ8rXcNDeKMZhM96ETyjufJSeYoXLqifiVLDw9rrVBmNStF7PskYp040em+0OZ4pF85Cwsdf7l9V7MMynzh9BoXqVUCBiwT03PNYH9AEmNUxXX+6ZwCpe/saONv8MgGt5uGXMZIK29phA3D8jD6uV/WOHsB8NjHNq9FrfSEAsl+dAcS4uiYie4BKSSeQN+zGAQqu1MMW4OAdxGOuf8WpIINx7n+7cKQfxlmc/Cgg5+MmIm2H0oDwQ+Xu7aSxp1OCUzbxQRdjz6+tnbYmZBuH0Ov2RbEvC5tDb261LRqKXpub0llg5fqKHl01D0ahv4OAQgRs5oU+4mq+H2QGTwIFGFqP9tCRo0I+aICawpxYOfoLJpFW6KvEPnM2Lr3sl6Nq2fmkz6RL5F7nUtzxN8OKazLQpv8DOYzXbi7+ayEsqS0/EINetq7RfCqgjrEUgfNWYuFXWqvUT8lnxLdNu+8cyrJqh1UquFjXWTw1kWcJ0pkokVeBtK9YysCnF1UYh/Iv3rl2ZoYSSLNtuvMSYlYHggZ8xV8bz9S3X2/NwBycBiWIy5Ou/OuSX7trIKgkkmda0xjBWEM1a2acVuqu2OFbMn2zFxm2a3YwKP//OlIgMg', + 'mac': 'QzKV/fgAs4U', + }, + }, + }, + }, + }, + }, + }, + 'POST': { + '/client/r0/delete_devices': (var req) => {}, + '/client/r0/account/3pid/add': (var req) => {}, + '/client/r0/account/3pid/bind': (var req) => {}, + '/client/r0/account/3pid/delete': (var req) => + {'id_server_unbind_result': 'success'}, + '/client/r0/account/3pid/unbind': (var req) => + {'id_server_unbind_result': 'success'}, + '/client/r0/account/password': (var req) => {}, + '/client/r0/rooms/1234/report/1234': (var req) => {}, + '/client/r0/search': (var req) => { + 'search_categories': { + 'room_events': { + 'groups': { + 'room_id': { + '!qPewotXpIctQySfjSy:localhost': { + 'order': 1, + 'next_batch': 'BdgFsdfHSf-dsFD', + 'results': ['\$144429830826TWwbB:localhost'] + } + } + }, + 'highlights': ['martians', 'men'], + 'next_batch': '5FdgFsd234dfgsdfFD', + 'count': 1224, + 'results': [ + { + 'rank': 0.00424866, + 'result': { + 'content': { + 'body': 'This is an example text message', + 'msgtype': 'm.text', + 'format': 'org.matrix.custom.html', + 'formatted_body': + 'This is an example text message' + }, + 'type': 'm.room.message', + 'event_id': '\$144429830826TWwbB:localhost', + 'room_id': '!qPewotXpIctQySfjSy:localhost', + 'sender': '@example:example.org', + 'origin_server_ts': 1432735824653, + 'unsigned': {'age': 1234} + } + } + ] + } + } + }, + '/client/r0/account/deactivate': (var req) => + {'id_server_unbind_result': 'success'}, + '/client/r0/user_directory/search': (var req) => { + 'results': [ + { + 'user_id': '@foo:bar.com', + 'display_name': 'Foo', + 'avatar_url': 'mxc://bar.com/foo' + } + ], + 'limited': false + }, + '/client/r0/register/email/requestToken': (var req) => { + 'sid': '123abc', + 'submit_url': 'https://example.org/path/to/submitToken' + }, + '/client/r0/register/msisdn/requestToken': (var req) => { + 'sid': '123abc', + 'submit_url': 'https://example.org/path/to/submitToken' + }, + '/client/r0/account/password/email/requestToken': (var req) => { + 'sid': '123abc', + 'submit_url': 'https://example.org/path/to/submitToken' + }, + '/client/r0/account/password/msisdn/requestToken': (var req) => { + 'sid': '123abc', + 'submit_url': 'https://example.org/path/to/submitToken' + }, + '/client/r0/account/3pid/email/requestToken': (var req) => { + 'sid': '123abc', + 'submit_url': 'https://example.org/path/to/submitToken' + }, + '/client/r0/account/3pid/msisdn/requestToken': (var req) => { + 'sid': '123abc', + 'submit_url': 'https://example.org/path/to/submitToken' + }, + '/client/r0/rooms/!localpart%3Aexample.com/receipt/m.read/%241234%3Aexample.com': + (var req) => {}, + '/client/r0/rooms/!localpart%3Aexample.com/read_markers': (var req) => {}, + '/client/r0/rooms/!1234%3Aexample.com/read_markers': (var req) => {}, + '/client/r0/user/${Uri.encodeComponent('@othertest:fakeServer.notExisting')}/filter': + (var req) => {'filter_id': '1234'}, + '/client/r0/user/${Uri.encodeComponent('@test:fakeServer.notExisting')}/filter': + (var req) => {'filter_id': '1234'}, + '/client/r0/publicRooms?server=example.com': (var req) => { + 'chunk': [ + { + 'aliases': ['#murrays:cheese.bar'], + 'canonical_alias': '#murrays:cheese.bar', + 'avatar_url': 'mxc://bleeker.street/CHEDDARandBRIE', + 'guest_can_join': false, + 'name': 'CHEESE', + 'num_joined_members': 37, + 'room_id': '!ol19s:bleecker.street', + 'topic': 'Tasty tasty cheese', + 'world_readable': true + } + ], + 'next_batch': 'p190q', + 'prev_batch': 'p1902', + 'total_room_count_estimate': 115 + }, + '/client/r0/keys/claim': (var req) => { + 'failures': {}, + 'one_time_keys': { + if (decodeJson(req)['one_time_keys']['@alice:example.com'] != + null) + '@alice:example.com': { + 'JLAFKJWSCS': { + 'signed_curve25519:AAAAAQ': { + 'key': 'ikMXajRlkS7Xi9CROrAh3jXnbygk8mLBdSaY9/al0X0', + 'signatures': { + '@alice:example.com': { + 'ed25519:JLAFKJWSCS': + 'XdboCa0Ljoh0Y0i/IVnmMqy/+T1hJyu8BA/nRYniJMQ7QWh/pGS5AsWswdARD+MAX+r4u98Qzk0y27HUddZXDA' + } + } + } + } + }, + if (decodeJson(req)['one_time_keys'] + ['@test:fakeServer.notExisting'] != + null) + '@test:fakeServer.notExisting': { + 'GHTYAJCE': { + 'signed_curve25519:AAAAAQ': { + 'key': 'qc72ve94cA28iuE0fXa98QO3uls39DHWdQlYyvvhGh0', + 'signatures': { + '@test:fakeServer.notExisting': { + 'ed25519:GHTYAJCE': + 'dFwffr5kTKefO7sjnWLMhTzw7oV31nkPIDRxFy5OQT2OP5++Ao0KRbaBZ6qfuT7lW1owKK0Xk3s7QTBvc/eNDA', + }, + }, + }, + }, + }, + } + }, + '/client/r0/rooms/!localpart%3Aexample.com/invite': (var req) => {}, + '/client/r0/rooms/!1234%3AfakeServer.notExisting/invite': (var req) => {}, + '/client/r0/rooms/!localpart%3Aexample.com/leave': (var req) => {}, + '/client/r0/rooms/!1234%3AfakeServer.notExisting/leave': (var req) => {}, + '/client/r0/rooms/!localpart%3Aexample.com/forget': (var req) => {}, + '/client/r0/rooms/!localpart%3Aserver.abc/kick': (var req) => {}, + '/client/r0/rooms/!1234%3AfakeServer.notExisting/kick': (var req) => {}, + '/client/r0/rooms/!localpart%3Aexample.com/kick': (var req) => {}, + '/client/r0/rooms/!localpart%3Aexample.com/ban': (var req) => {}, + '/client/r0/rooms/!1234%3AfakeServer.notExisting/ban': (var req) => {}, + '/client/r0/rooms/!localpart%3Aexample.com/unban': (var req) => {}, + '/client/r0/rooms/!1234%3AfakeServer.notExisting/unban': (var req) => {}, + '/client/r0/rooms/!localpart%3Aexample.com/join': (var req) => + {'room_id': '!localpart:example.com'}, + '/client/r0/join/!localpart%3Aexample.com?server_name=example.com&server_name=example.abc': + (var req) => {'room_id': '!localpart:example.com'}, + '/client/r0/join/!newroom%3Aexample.com': (var req) => + {'room_id': '!newroom%3A:example.com'}, + '/client/r0/keys/upload': (var req) => { + 'one_time_key_counts': { + 'curve25519': 10, + 'signed_curve25519': + decodeJson(req)['one_time_keys']?.keys?.length ?? 0, + } + }, + '/client/r0/keys/query': (var req) => { + 'failures': {}, + 'device_keys': { + '@alice:example.com': { + 'JLAFKJWSCS': { + 'user_id': '@alice:example.com', + 'device_id': 'JLAFKJWSCS', + 'algorithms': [ + AlgorithmTypes.olmV1Curve25519AesSha2, + AlgorithmTypes.megolmV1AesSha2 + ], + 'keys': { + 'curve25519:JLAFKJWSCS': + 'L+4+JCl8MD63dgo8z5Ta+9QAHXiANyOVSfgbHA5d3H8', + 'ed25519:JLAFKJWSCS': + 'rUFJftIWpFF/jqqz3bexGGYiG8UobKhzkeabqw1v0zM' + }, + 'signatures': { + '@alice:example.com': { + 'ed25519:JLAFKJWSCS': + 'go3mi5o3Ile+Ik+lCEpHmBmyJmKWfnRDCBBvfaVlKsMyha5IORuYcxwEUrAeLyAeeeHvkWDFX+No5eY1jYeKBw' + } + }, + 'unsigned': {'device_display_name': 'Alices mobile phone'} + }, + 'OTHERDEVICE': { + 'user_id': '@alice:example.com', + 'device_id': 'OTHERDEVICE', + 'algorithms': [ + AlgorithmTypes.olmV1Curve25519AesSha2, + AlgorithmTypes.megolmV1AesSha2 + ], + 'keys': { + 'curve25519:OTHERDEVICE': + 'wMIDhiQl5jEXQrTB03ePOSQfR8sA/KMrW0CIfFfXKEE', + 'ed25519:OTHERDEVICE': + '2Lyaj5NB7HPqKZMjZpA/pECXuQ+9wi8AGFdw33y3DuQ' + }, + 'signatures': { + '@alice:example.com': { + 'ed25519:OTHERDEVICE': + 'bwHd6ylISP13AICdDPd0HQd4V6dvvd4vno8/OwUNdm9UAprr3YjkDqVw425I74u2UQAarq9bytBqVqFyD6trAw', + } + }, + }, + }, + '@test:fakeServer.notExisting': { + 'GHTYAJCE': { + 'user_id': '@test:fakeServer.notExisting', + 'device_id': 'GHTYAJCE', + 'algorithms': [ + AlgorithmTypes.olmV1Curve25519AesSha2, + AlgorithmTypes.megolmV1AesSha2 + ], + 'keys': { + 'curve25519:GHTYAJCE': + '7rvl3jORJkBiK4XX1e5TnGnqz068XfYJ0W++Ml63rgk', + 'ed25519:GHTYAJCE': + 'gjL//fyaFHADt9KBADGag8g7F8Up78B/K1zXeiEPLJo' + }, + 'signatures': { + '@test:fakeServer.notExisting': { + 'ed25519:GHTYAJCE': + 'NEQeTgv7ew1IZSLQphWd0y60EdHdcNfHgvoaMQco5XKeIYyiUZIWd7F4x/mkPDjUizv6yWMbTDCWdSg5XcgNBA', + 'ed25519:F9ypFzgbISXCzxQhhSnXMkc1vq12Luna3Nw5rqViOJY': + 'Q4/55vZjEJD7M2EC40bgZqd9Zuy/4C75UPVopJdXeioQVaKtFf6EF0nUUuql0yD+r3hinsZcock0wO6Q2xcoAQ', + }, + }, + }, + 'OTHERDEVICE': { + 'user_id': '@test:fakeServer.notExisting', + 'device_id': 'OTHERDEVICE', + 'algorithms': [ + AlgorithmTypes.olmV1Curve25519AesSha2, + AlgorithmTypes.megolmV1AesSha2 + ], + 'keys': { + 'curve25519:OTHERDEVICE': + 'R96BA0qE1+QAWLp7E1jyWSTJ1VXMLpEdiM2SZHlKMXM', + 'ed25519:OTHERDEVICE': + 'EQo9eYbSygIbOR+tVJziqAY1NI6Gga+JQOVIqJe4mr4' + }, + 'signatures': { + '@test:fakeServer.notExisting': { + 'ed25519:OTHERDEVICE': + '/rT6pVRypJWxGos1QcI7jHL9HwcA83nkHLHqMcRPeLSxXHh4oHWvC0/tl0Xg06ogyiGw4NuB7TpOISvJBdt7BA', + 'ed25519:F9ypFzgbISXCzxQhhSnXMkc1vq12Luna3Nw5rqViOJY': + 'qnjiLl36h/1jlLvcAgt46Igaod2T9lOSnoSVkV0KC+c7vYIjG4QBzXpH+hycfufOT/y+a/kl52dUTLQWctMKCA', + }, + }, + }, + }, + '@othertest:fakeServer.notExisting': { + 'FOXDEVICE': { + 'user_id': '@othertest:fakeServer.notExisting', + 'device_id': 'FOXDEVICE', + 'algorithms': [ + AlgorithmTypes.olmV1Curve25519AesSha2, + AlgorithmTypes.megolmV1AesSha2 + ], + 'keys': { + 'curve25519:FOXDEVICE': + 'JBG7ZaPn54OBC7TuIEiylW3BZ+7WcGQhFBPB9pogbAg', + 'ed25519:FOXDEVICE': + 'R5/p04tticvdlNIxiiBIP0j9OQWv8ep6eEU6/lWKDxw', + }, + 'signatures': { + '@othertest:fakeServer.notExisting': { + 'ed25519:FOXDEVICE': + '2lJ3atmRIWgkyQNC9gvWEpxwuozsBQsg33M2IMDJqLhx/+g3Ds1vQ683dJsYIu04ORa4U0L9TqieHVpV/7qqDA', + }, + }, + }, + }, + }, + 'master_keys': { + '@test:fakeServer.notExisting': { + 'user_id': '@test:fakeServer.notExisting', + 'usage': ['master'], + 'keys': { + 'ed25519:82mAXjsmbTbrE6zyShpR869jnrANO75H8nYY0nDLoJ8': + '82mAXjsmbTbrE6zyShpR869jnrANO75H8nYY0nDLoJ8', + }, + 'signatures': {}, + }, + '@othertest:fakeServer.notExisting': { + 'user_id': '@othertest:fakeServer.notExisting', + 'usage': ['master'], + 'keys': { + 'ed25519:master': 'master', + }, + 'signatures': {}, + }, + }, + 'self_signing_keys': { + '@test:fakeServer.notExisting': { + 'user_id': '@test:fakeServer.notExisting', + 'usage': ['self_signing'], + 'keys': { + 'ed25519:F9ypFzgbISXCzxQhhSnXMkc1vq12Luna3Nw5rqViOJY': + 'F9ypFzgbISXCzxQhhSnXMkc1vq12Luna3Nw5rqViOJY', + }, + 'signatures': { + '@test:fakeServer.notExisting': { + 'ed25519:82mAXjsmbTbrE6zyShpR869jnrANO75H8nYY0nDLoJ8': + 'afkrbGvPn5Zb5zc7Lk9cz2skI3QrzI/L0st1GS+/GATxNjMzc6vKmGu7r9cMb1GJxy4RdeUpfH3L7Fs/fNL1Dw', + }, + }, + }, + '@othertest:fakeServer.notExisting': { + 'user_id': '@othertest:fakeServer.notExisting', + 'usage': ['self_signing'], + 'keys': { + 'ed25519:self_signing': 'self_signing', + }, + 'signatures': {}, + }, + }, + 'user_signing_keys': { + '@test:fakeServer.notExisting': { + 'user_id': '@test:fakeServer.notExisting', + 'usage': ['user_signing'], + 'keys': { + 'ed25519:0PiwulzJ/RU86LlzSSZ8St80HUMN3dqjKa/orIJoA0g': + '0PiwulzJ/RU86LlzSSZ8St80HUMN3dqjKa/orIJoA0g', + }, + 'signatures': { + '@test:fakeServer.notExisting': { + 'ed25519:82mAXjsmbTbrE6zyShpR869jnrANO75H8nYY0nDLoJ8': + 'pvgbZxEbllaElhpiRnb7/uOIUhrglvHCFnpoxr3/5ZrWa0EK/uaefhex9eEV4uBLrHjHg2ymwdNaM7ap9+sBBg', + }, + }, + }, + '@othertest:fakeServer.notExisting': { + 'user_id': '@othertest:fakeServer.notExisting', + 'usage': ['user_signing'], + 'keys': { + 'ed25519:user_signing': 'user_signing', + }, + 'signatures': {}, + }, + }, + }, + '/client/r0/register': (var req) => { + 'user_id': '@testuser:example.com', + 'access_token': '1234', + 'device_id': 'ABCD', + }, + '/client/r0/register?kind=user': (var req) => + {'user_id': '@testuser:example.com'}, + '/client/r0/register?kind=guest': (var req) => + {'user_id': '@testuser:example.com'}, + '/client/r0/rooms/1234/upgrade': (var req) => {}, + '/client/r0/user/1234/openid/request_token': (var req) => { + 'access_token': 'SomeT0kenHere', + 'token_type': 'Bearer', + 'matrix_server_name': 'example.com', + 'expires_in': 3600.0 + }, + '/client/r0/user/@test:fakeServer.notExisting/openid/request_token': + (var req) => { + 'access_token': 'SomeT0kenHere', + 'token_type': 'Bearer', + 'matrix_server_name': 'example.com', + 'expires_in': 3600 + }, + '/client/r0/login': (var req) => { + 'user_id': '@test:fakeServer.notExisting', + 'access_token': 'abc123', + 'device_id': 'GHTYAJCE', + 'well_known': { + 'm.homeserver': {'base_url': 'https://example.org'}, + 'm.identity_server': {'base_url': 'https://id.example.org'} + } + }, + '/media/r0/upload?filename=file.jpeg': (var req) => + {'content_uri': 'mxc://example.com/AQwafuaFswefuhsfAFAgsw'}, + '/client/r0/logout': (var reqI) => {}, + '/client/r0/pushers/set': (var reqI) => {}, + '/client/r0/join/1234': (var reqI) => {'room_id': '1234'}, + '/client/r0/logout/all': (var reqI) => {}, + '/client/r0/createRoom': (var reqI) => { + 'room_id': '!1234:fakeServer.notExisting', + }, + '/client/r0/rooms/!localpart%3Aserver.abc/read_markers': (var reqI) => {}, + '/client/r0/rooms/!localpart:server.abc/kick': (var reqI) => {}, + '/client/r0/rooms/!localpart%3Aserver.abc/ban': (var reqI) => {}, + '/client/r0/rooms/!localpart%3Aserver.abc/unban': (var reqI) => {}, + '/client/r0/rooms/!localpart%3Aserver.abc/invite': (var reqI) => {}, + '/client/unstable/keys/device_signing/upload': (var reqI) { + if (client != null) { + final jsonBody = decodeJson(reqI); + for (final keyType in { + 'master_key', + 'self_signing_key', + 'user_signing_key' + }) { + if (jsonBody[keyType] != null) { + final key = + sdk.CrossSigningKey.fromJson(jsonBody[keyType], client!); + client!.userDeviceKeys[client!.userID!]?.crossSigningKeys + .removeWhere((k, v) => v.usage.contains(key.usage.first)); + client!.userDeviceKeys[client!.userID!] + ?.crossSigningKeys[key.publicKey!] = key; + } + } + // and generate a fake sync + client!.handleSync(sdk.SyncUpdate(nextBatch: '')); + } + return {}; + }, + '/client/r0/keys/signatures/upload': (var reqI) => {'failures': {}}, + '/client/unstable/room_keys/version': (var reqI) => {'version': '5'}, + }, + 'PUT': { + '/client/r0/user/%40test%3AfakeServer.notExisting/account_data/m.ignored_user_list': + (var req) => {}, + '/client/r0/presence/${Uri.encodeComponent('@alice:example.com')}/status': + (var req) => {}, + '/client/r0/pushrules/global/content/nocake/enabled': (var req) => {}, + '/client/r0/pushrules/global/content/nocake/actions': (var req) => {}, + '/client/r0/rooms/!localpart%3Aserver.abc/state/m.room.history_visibility': + (var req) => {'event_id': '1234'}, + '/client/r0/rooms/!localpart%3Aserver.abc/state/m.room.join_rules': + (var req) => {'event_id': '1234'}, + '/client/r0/rooms/!localpart%3Aserver.abc/state/m.room.guest_access': + (var req) => {'event_id': '1234'}, + '/client/r0/rooms/!localpart%3Aserver.abc/send/m.call.invite/1234': + (var req) => {'event_id': '1234'}, + '/client/r0/rooms/!localpart%3Aserver.abc/send/m.call.answer/1234': + (var req) => {'event_id': '1234'}, + '/client/r0/rooms/!localpart%3Aserver.abc/send/m.call.select_answer/1234': + (var req) => {'event_id': '1234'}, + '/client/r0/rooms/!localpart%3Aserver.abc/send/m.call.reject/1234': + (var req) => {'event_id': '1234'}, + '/client/r0/rooms/!localpart%3Aserver.abc/send/m.call.negotiate/1234': + (var req) => {'event_id': '1234'}, + '/client/r0/rooms/!localpart%3Aserver.abc/send/m.call.candidates/1234': + (var req) => {'event_id': '1234'}, + '/client/r0/rooms/!localpart%3Aserver.abc/send/m.call.hangup/1234': + (var req) => {'event_id': '1234'}, + '/client/r0/rooms/!localpart%3Aserver.abc/send/m.call.replaces/1234': + (var req) => {'event_id': '1234'}, + '/client/r0/rooms/!localpart%3Aserver.abc/send/m.call.asserted_identity/1234': + (var req) => {'event_id': '1234'}, + '/client/r0/rooms/!localpart%3Aserver.abc/send/m.call.sdp_stream_metadata_changed/1234': + (var req) => {'event_id': '1234'}, + '/client/r0/rooms/!localpart%3Aserver.abc/send/org.matrix.call.sdp_stream_metadata_changed/1234': + (var req) => {'event_id': '1234'}, + '/client/r0/rooms/!localpart%3Aserver.abc/send/org.matrix.call.asserted_identity/1234': + (var req) => {'event_id': '1234'}, + '/client/r0/rooms/!1234%3Aexample.com/redact/1143273582443PhrSn%3Aexample.org/1234': + (var req) => {'event_id': '1234'}, + '/client/r0/pushrules/global/room/!localpart%3Aserver.abc': (var req) => + {}, + '/client/r0/pushrules/global/override/.m.rule.master/enabled': + (var req) => {}, + '/client/r0/pushrules/global/content/nocake?before=1&after=2': + (var req) => {}, + '/client/r0/devices/QBUAZIFURK': (var req) => {}, + '/client/r0/directory/room/%23testalias%3Aexample.com': (var reqI) => {}, + '/client/r0/rooms/!localpart%3Aserver.abc/send/m.room.message/testtxid': + (var reqI) => { + 'event_id': '\$event${FakeMatrixApi.eventCounter++}', + }, + '/client/r0/rooms/!localpart%3Aserver.abc/send/m.reaction/testtxid': + (var reqI) => { + 'event_id': '\$event${FakeMatrixApi.eventCounter++}', + }, + '/client/r0/rooms/!localpart%3Aexample.com/typing/%40alice%3Aexample.com': + (var req) => {}, + '/client/r0/rooms/!1234%3Aexample.com/send/m.room.message/1234': + (var reqI) => { + 'event_id': '\$event${FakeMatrixApi.eventCounter++}', + }, + '/client/r0/rooms/!1234%3Aexample.com/send/m.room.message/newresend': + (var reqI) => { + 'event_id': '\$event${FakeMatrixApi.eventCounter++}', + }, + '/client/r0/user/%40test%3AfakeServer.notExisting/rooms/!localpart%3Aserver.abc/tags/m.favourite': + (var req) => {}, + '/client/r0/user/%40alice%3Aexample.com/rooms/!localpart%3Aexample.com/tags/testtag': + (var req) => {}, + '/client/r0/user/%40alice%3Aexample.com/account_data/test.account.data': + (var req) => {}, + '/client/r0/user/%40test%3AfakeServer.notExisting/account_data/best%20animal': + (var req) => {}, + '/client/r0/user/%40alice%3Aexample.com/rooms/1234/account_data/test.account.data': + (var req) => {}, + '/client/r0/user/%40test%3AfakeServer.notExisting/rooms/!localpart%3Aserver.abc/account_data/com.famedly.marked_unread': + (var req) => {}, + '/client/r0/user/%40test%3AfakeServer.notExisting/account_data/m.direct': + (var req) => {}, + '/client/r0/user/%40othertest%3AfakeServer.notExisting/account_data/m.direct': + (var req) => {}, + '/client/r0/profile/%40alice%3Aexample.com/displayname': (var reqI) => {}, + '/client/r0/profile/%40alice%3Aexample.com/avatar_url': (var reqI) => {}, + '/client/r0/profile/%40test%3AfakeServer.notExisting/avatar_url': + (var reqI) => {}, + '/client/r0/rooms/!localpart%3Aserver.abc/state/m.room.encryption': + (var reqI) => {'event_id': 'YUwRidLecu:example.com'}, + '/client/r0/rooms/!localpart%3Aserver.abc/state/m.room.avatar': + (var reqI) => {'event_id': 'YUwRidLecu:example.com'}, + '/client/r0/rooms/!localpart%3Aserver.abc/send/m.room.message/1234': + (var reqI) => {'event_id': 'YUwRidLecu:example.com'}, + '/client/r0/rooms/!localpart%3Aserver.abc/redact/1234/1234': (var reqI) => + {'event_id': 'YUwRidLecu:example.com'}, + '/client/r0/rooms/!localpart%3Aserver.abc/state/m.room.name': + (var reqI) => { + 'event_id': '42', + }, + '/client/r0/rooms/!localpart%3Aserver.abc/state/m.room.topic': + (var reqI) => { + 'event_id': '42', + }, + '/client/r0/rooms/!localpart%3Aserver.abc/state/m.room.pinned_events': + (var reqI) => { + 'event_id': '42', + }, + '/client/r0/rooms/!localpart%3Aserver.abc/state/m.room.power_levels': + (var reqI) => { + 'event_id': '42', + }, + '/client/r0/directory/list/room/!localpart%3Aexample.com': (var req) => + {}, + '/client/unstable/room_keys/version/5': (var req) => {}, + '/client/unstable/room_keys/keys/${Uri.encodeComponent('!726s6s6q:example.com')}/${Uri.encodeComponent('ciM/JWTPrmiWPPZNkRLDPQYf9AW/I46bxyLSr+Bx5oU')}?version=5': + (var req) => { + 'etag': 'asdf', + 'count': 1, + }, + '/client/unstable/room_keys/keys/${Uri.encodeComponent('!726s6s6q:example.com')}?version=5': + (var req) => { + 'etag': 'asdf', + 'count': 1, + }, + '/client/unstable/room_keys/keys?version=5': (var req) => { + 'etag': 'asdf', + 'count': 1, + }, + }, + 'DELETE': { + '/unknown/token': (var req) => {'errcode': 'M_UNKNOWN_TOKEN'}, + '/client/r0/devices/QBUAZIFURK': (var req) => {}, + '/client/r0/directory/room/%23testalias%3Aexample.com': (var reqI) => {}, + '/client/r0/pushrules/global/content/nocake': (var req) => {}, + '/client/r0/pushrules/global/override/!localpart%3Aserver.abc': + (var req) => {}, + '/client/r0/user/%40test%3AfakeServer.notExisting/rooms/!localpart%3Aserver.abc/tags/m.favourite': + (var req) => {}, + '/client/r0/user/%40alice%3Aexample.com/rooms/!localpart%3Aexample.com/tags/testtag': + (var req) => {}, + '/client/unstable/room_keys/version/5': (var req) => {}, + '/client/unstable/room_keys/keys/${Uri.encodeComponent('!726s6s6q:example.com')}/${Uri.encodeComponent('ciM/JWTPrmiWPPZNkRLDPQYf9AW/I46bxyLSr+Bx5oU')}?version=5': + (var req) => { + 'etag': 'asdf', + 'count': 1, + }, + '/client/unstable/room_keys/keys/${Uri.encodeComponent('!726s6s6q:example.com')}?version=5': + (var req) => { + 'etag': 'asdf', + 'count': 1, + }, + '/client/unstable/room_keys/keys?version=5': (var req) => { + 'etag': 'asdf', + 'count': 1, + }, + }, + }; +} diff --git a/test/html_to_text_test.dart b/test/html_to_text_test.dart new file mode 100644 index 0000000..9dde56e --- /dev/null +++ b/test/html_to_text_test.dart @@ -0,0 +1,101 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:matrix/src/utils/html_to_text.dart'; +import 'package:test/test.dart'; + +void main() { + group('htmlToText', () { + final testMap = { + '': '', + 'hello world\nthis is a test': 'hello world\nthis is a test', + 'That\'s not a test, this is a test': + '*That\'s* not a test, **this** is a test', + 'Visit our website (outdated)': + 'Visit ~~🔗our website~~ (outdated)', + '(cw spiders) spiders are pretty cool': + '(cw spiders) ███████████████████████', + 'spiders are pretty cool': + '(cw spiders) ███████████████████████', + 'a test case': 'a test case', + 'List of cute animals:\n
    \n
  • Kittens
  • \n
  • Puppies
  • \n
  • Snakes
    (I think they\'re cute!)
  • \n
\n(This list is incomplete, you can help by adding to it!)': + 'List of cute animals:\n● Kittens\n● Puppies\n● Snakes\n (I think they\'re cute!)\n(This list is incomplete, you can help by adding to it!)', + 'fox': '*fox*', + 'fox': '*fox*', + 'fox': '**fox**', + 'fox': '**fox**', + 'fox': '__fox__', + 'fox': '__fox__', + 'fox': '~~fox~~', + 'fox': '~~fox~~', + 'fox': '~~fox~~', + '>fox': '`>fox`', + '
meep
': '```\nmeep\n```', + '
meep\n
': '```\nmeep\n```', + '
meep
': + '```floof\nmeep\n```', + 'before
code
after': 'before\n```\ncode\n```\nafter', + '

before

code

after

': + 'before\n```\ncode\n```\nafter', + '

fox

': 'fox', + '

fox

floof

': 'fox\n\nfloof', + 'website': '🔗website', + 'fox': 'fox', + 'fox': 'fox', + ':wave:': ':wave:', + 'fox
floof': 'fox\nfloof', + '
fox
floof': '> fox\nfloof', + '

fox

floof': '> fox\nfloof', + '

fox

floof

': '> fox\nfloof', + 'a
fox
floof': 'a\n> fox\nfloof', + '
fox
floof
fluff': + '> > fox\n> floof\nfluff', + '
  • hey
    • a
    • b
  • foxies
': + '● hey\n ○ a\n ○ b\n● foxies', + '
  1. a
  2. b
': '1. a\n2. b', + '
  1. a
  2. b
': '42. a\n43. b', + '
  1. a
    1. aa
    2. bb
  2. b
': + '1. a\n 1. aa\n 2. bb\n2. b', + '
  1. a
    • aa
    • bb
  2. b
': + '1. a\n ○ aa\n ○ bb\n2. b', + '
  • a
    1. aa
    2. bb
  • b
': + '● a\n 1. aa\n 2. bb\n● b', + 'bunnyfox': 'fox', + 'fox
floof': 'fox\n----------\nfloof', + '

fox


floof

': 'fox\n----------\nfloof', + '

fox

floof': '# fox\nfloof', + '

fox

floof

': '# fox\nfloof', + 'floof

fox

': 'floof\n# fox', + '

floof

fox

': 'floof\n# fox', + '

fox

': '## fox', + '

fox

': '### fox', + '

fox

': '#### fox', + '
fox
': '##### fox', + '
fox
': '###### fox', + 'fox': 'fox', + '

fox

\n

floof

': 'fox\n\nfloof', + 'beep

fox

\n

floof

': 'fox\n\nfloof', + '
': '``````', + }; + for (final entry in testMap.entries) { + test(entry.key, () async { + expect(HtmlToText.convert(entry.key), entry.value); + }); + } + }); +} diff --git a/test/image_pack_test.dart b/test/image_pack_test.dart new file mode 100644 index 0000000..513dd7c --- /dev/null +++ b/test/image_pack_test.dart @@ -0,0 +1,266 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:test/test.dart'; +import 'package:matrix/matrix.dart'; +import 'fake_client.dart'; + +void main() { + group('Image Pack', () { + late Client client; + late Room room; + late Room room2; + + test('setupClient', () async { + client = await getClient(); + room = Room(id: '!1234:fakeServer.notExisting', client: client); + room2 = Room(id: '!abcd:fakeServer.notExisting', client: client); + room.setState(Event( + type: 'm.room.power_levels', + content: {}, + room: room, + stateKey: '', + senderId: client.userID!, + eventId: '\$fakeid1:fakeServer.notExisting', + originServerTs: DateTime.now(), + )); + room.setState(Event( + type: 'm.room.member', + content: {'membership': 'join'}, + room: room, + stateKey: client.userID, + senderId: '\@fakeuser:fakeServer.notExisting', + eventId: '\$fakeid2:fakeServer.notExisting', + originServerTs: DateTime.now(), + )); + room2.setState(Event( + type: 'm.room.power_levels', + content: {}, + room: room, + stateKey: '', + senderId: client.userID!, + eventId: '\$fakeid3:fakeServer.notExisting', + originServerTs: DateTime.now(), + )); + room2.setState(Event( + type: 'm.room.member', + content: {'membership': 'join'}, + room: room, + stateKey: client.userID, + senderId: '\@fakeuser:fakeServer.notExisting', + eventId: '\$fakeid4:fakeServer.notExisting', + originServerTs: DateTime.now(), + )); + client.rooms.add(room); + client.rooms.add(room2); + }); + + test('Single room', () async { + room.setState(Event( + type: 'im.ponies.room_emotes', + content: { + 'images': { + 'room_plain': {'url': 'mxc://room_plain'} + } + }, + room: room, + stateKey: '', + senderId: '\@fakeuser:fakeServer.notExisting', + eventId: '\$fakeid5:fakeServer.notExisting', + originServerTs: DateTime.now(), + )); + final packs = room.getImagePacks(); + expect(packs.length, 1); + expect(packs['room']?.images.length, 1); + expect(packs['room']?.images['room_plain']?.url.toString(), + 'mxc://room_plain'); + var packsFlat = room.getImagePacksFlat(); + expect(packsFlat, { + 'room': {'room_plain': 'mxc://room_plain'} + }); + room.setState(Event( + type: 'im.ponies.room_emotes', + content: { + 'images': { + 'emote': { + 'url': 'mxc://emote', + 'usage': ['emoticon'] + }, + 'sticker': { + 'url': 'mxc://sticker', + 'usage': ['sticker'] + }, + } + }, + room: room, + stateKey: '', + senderId: '\@fakeuser:fakeServer.notExisting', + eventId: '\$fakeid6:fakeServer.notExisting', + originServerTs: DateTime.now(), + )); + packsFlat = room.getImagePacksFlat(ImagePackUsage.emoticon); + expect(packsFlat, { + 'room': {'emote': 'mxc://emote'} + }); + packsFlat = room.getImagePacksFlat(ImagePackUsage.sticker); + expect(packsFlat, { + 'room': {'sticker': 'mxc://sticker'} + }); + room.setState(Event( + type: 'im.ponies.room_emotes', + content: { + 'images': { + 'emote': {'url': 'mxc://emote'}, + 'sticker': {'url': 'mxc://sticker'}, + }, + 'pack': { + 'usage': ['emoticon'], + } + }, + room: room, + stateKey: '', + senderId: '\@fakeuser:fakeServer.notExisting', + eventId: '\$fakeid7:fakeServer.notExisting', + originServerTs: DateTime.now(), + )); + packsFlat = room.getImagePacksFlat(ImagePackUsage.emoticon); + expect(packsFlat, { + 'room': {'emote': 'mxc://emote', 'sticker': 'mxc://sticker'} + }); + packsFlat = room.getImagePacksFlat(ImagePackUsage.sticker); + expect(packsFlat, {}); + + room.setState(Event( + type: 'im.ponies.room_emotes', + content: { + 'images': { + 'fox': {'url': 'mxc://fox'}, + }, + 'pack': { + 'usage': ['emoticon'], + } + }, + room: room, + stateKey: 'fox', + senderId: '\@fakeuser:fakeServer.notExisting', + eventId: '\$fakeid8:fakeServer.notExisting', + originServerTs: DateTime.now(), + )); + packsFlat = room.getImagePacksFlat(ImagePackUsage.emoticon); + expect(packsFlat, { + 'room': {'emote': 'mxc://emote', 'sticker': 'mxc://sticker'}, + 'fox': {'fox': 'mxc://fox'}, + }); + }); + + test('user pack', () async { + client.accountData['im.ponies.user_emotes'] = BasicEvent.fromJson({ + 'type': 'im.ponies.user_emotes', + 'content': { + 'images': { + 'user': { + 'url': 'mxc://user', + } + }, + }, + }); + final packsFlat = room.getImagePacksFlat(ImagePackUsage.emoticon); + expect(packsFlat, { + 'room': {'emote': 'mxc://emote', 'sticker': 'mxc://sticker'}, + 'fox': {'fox': 'mxc://fox'}, + 'user': {'user': 'mxc://user'}, + }); + }); + + test('other rooms', () async { + room2.setState(Event( + type: 'im.ponies.room_emotes', + content: { + 'images': { + 'other_room_emote': {'url': 'mxc://other_room_emote'}, + }, + 'pack': { + 'usage': ['emoticon'], + } + }, + room: room2, + stateKey: '', + senderId: '\@fakeuser:fakeServer.notExisting', + eventId: '\$fakeid9:fakeServer.notExisting', + originServerTs: DateTime.now(), + )); + client.accountData['im.ponies.emote_rooms'] = BasicEvent.fromJson({ + 'type': 'im.ponies.emote_rooms', + 'content': { + 'rooms': { + '!abcd:fakeServer.notExisting': {'': {}}, + }, + }, + }); + var packsFlat = room.getImagePacksFlat(ImagePackUsage.emoticon); + expect(packsFlat, { + 'room': {'emote': 'mxc://emote', 'sticker': 'mxc://sticker'}, + 'fox': {'fox': 'mxc://fox'}, + 'user': {'user': 'mxc://user'}, + 'empty-chat-abcdfakeservernotexisting': { + 'other_room_emote': 'mxc://other_room_emote' + }, + }); + room2.setState(Event( + type: 'im.ponies.room_emotes', + content: { + 'images': { + 'other_fox': {'url': 'mxc://other_fox'}, + }, + 'pack': { + 'usage': ['emoticon'], + } + }, + room: room2, + stateKey: 'fox', + senderId: '\@fakeuser:fakeServer.notExisting', + eventId: '\$fakeid10:fakeServer.notExisting', + originServerTs: DateTime.now(), + )); + client.accountData['im.ponies.emote_rooms'] = BasicEvent.fromJson({ + 'type': 'im.ponies.emote_rooms', + 'content': { + 'rooms': { + '!abcd:fakeServer.notExisting': {'': {}, 'fox': {}}, + }, + }, + }); + packsFlat = room.getImagePacksFlat(ImagePackUsage.emoticon); + expect(packsFlat, { + 'room': {'emote': 'mxc://emote', 'sticker': 'mxc://sticker'}, + 'fox': {'fox': 'mxc://fox'}, + 'user': {'user': 'mxc://user'}, + 'empty-chat-abcdfakeservernotexisting': { + 'other_room_emote': 'mxc://other_room_emote' + }, + 'empty-chat-fox-abcdfakeservernotexisting': { + 'other_fox': 'mxc://other_fox' + }, + }); + }); + + test('dispose client', () async { + await client.dispose(closeDatabase: true); + }); + }); +} diff --git a/test/markdown_test.dart b/test/markdown_test.dart new file mode 100644 index 0000000..2c3dab8 --- /dev/null +++ b/test/markdown_test.dart @@ -0,0 +1,130 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:matrix/src/utils/markdown.dart'; +import 'package:test/test.dart'; + +void main() { + group('markdown', () { + final emotePacks = { + 'room': { + 'fox': 'mxc://roomfox', + 'bunny': 'mxc://roombunny', + }, + 'user': { + 'fox': 'mxc://userfox', + 'bunny': 'mxc://userbunny', + 'raccoon': 'mxc://raccoon', + }, + }; + final mentionMap = { + '@Bob': '@bob:example.org', + '@[Bob Ross]': '@bobross:example.org', + '@Fox#123': '@fox:example.org', + '@[Fast Fox]#123': '@fastfox:example.org', + '@[">]': '@blah:example.org', + }; + final getMention = (mention) => mentionMap[mention]; + test('simple markdown', () { + expect(markdown('hey *there* how are **you** doing?'), + 'hey there how are you doing?'); + expect(markdown('wha ~~strike~~ works!'), 'wha strike works!'); + }); + test('spoilers', () { + expect(markdown('Snape killed ||Dumbledoor||'), + 'Snape killed Dumbledoor'); + expect(markdown('Snape killed ||Story|Dumbledoor||'), + 'Snape killed Dumbledoor'); + expect(markdown('Snape killed ||Some dumb loser|Dumbledoor||'), + 'Snape killed Dumbledoor'); + expect(markdown('Snape killed ||Some dumb loser|Dumbledoor **bold**||'), + 'Snape killed Dumbledoor bold'); + expect(markdown('Snape killed ||Dumbledoor **bold**||'), + 'Snape killed Dumbledoor bold'); + }); + test('multiple paragraphs', () { + expect(markdown('Heya!\n\nBeep'), '

Heya!

\n

Beep

'); + }); + test('Other block elements', () { + expect(markdown('# blah\n\nblubb'), '

blah

\n

blubb

'); + }); + test('linebreaks', () { + expect(markdown('foxies\ncute'), 'foxies
\ncute'); + }); + test('emotes', () { + expect(markdown(':fox:', getEmotePacks: () => emotePacks), + ':fox:'); + expect(markdown(':user~fox:', getEmotePacks: () => emotePacks), + ':fox:'); + expect(markdown(':raccoon:', getEmotePacks: () => emotePacks), + ':raccoon:'); + expect( + markdown(':invalid:', getEmotePacks: () => emotePacks), ':invalid:'); + expect(markdown(':invalid:?!', getEmotePacks: () => emotePacks), + ':invalid:?!'); + expect(markdown(':room~invalid:', getEmotePacks: () => emotePacks), + ':room~invalid:'); + }); + test('pills', () { + expect(markdown('Hey @sorunome:sorunome.de!'), + 'Hey @sorunome:sorunome.de!'); + expect(markdown('#fox:sorunome.de: you all are awesome'), + '#fox:sorunome.de: you all are awesome'); + expect(markdown('!blah:example.org'), + '!blah:example.org'); + expect(markdown('https://matrix.to/#/#fox:sorunome.de'), + 'https://matrix.to/#/#fox:sorunome.de'); + expect(markdown('Hey @sorunome:sorunome.de:1234!'), + 'Hey @sorunome:sorunome.de:1234!'); + expect(markdown('Hey @sorunome:127.0.0.1!'), + 'Hey @sorunome:127.0.0.1!'); + expect(markdown('Hey @sorunome:[::1]!'), + 'Hey @sorunome:[::1]!'); + }); + test('mentions', () { + expect(markdown('Hey @Bob!', getMention: getMention), + 'Hey @Bob!'); + expect(markdown('How is @[Bob Ross] doing?', getMention: getMention), + 'How is @[Bob Ross] doing?'); + expect( + markdown('Hey @invalid!', getMention: getMention), 'Hey @invalid!'); + expect(markdown('Hey @Fox#123!', getMention: getMention), + 'Hey @Fox#123!'); + expect(markdown('Hey @[Fast Fox]#123!', getMention: getMention), + 'Hey @[Fast Fox]#123!'); + expect(markdown('Hey @[">]!', getMention: getMention), + 'Hey @[">]!'); + }); + test('latex', () { + expect(markdown('meep \$\\frac{2}{3}\$'), + 'meep \\frac{2}{3}'); + expect(markdown('meep \$hmm *yay*\$'), + 'meep hmm *yay*'); + expect(markdown('you have \$somevar and \$someothervar'), + 'you have \$somevar and \$someothervar'); + expect(markdown('meep ||\$\\frac{2}{3}\$||'), + 'meep \\frac{2}{3}'); + expect(markdown('meep `\$\\frac{2}{3}\$`'), + 'meep \$\\frac{2}{3}\$'); + expect(markdown('hey\n\$\$beep\$\$\nmeow'), + '

hey

\n
\n
beep
\n
\n

meow

'); + expect(markdown('hey\n\$\$\nbeep\nboop\n\$\$\nmeow'), + '

hey

\n
\n
beep\nboop
\n
\n

meow

'); + }); + }); +} diff --git a/test/matrix_api/map_copy_extension_test.dart b/test/matrix_api/map_copy_extension_test.dart new file mode 100644 index 0000000..e702795 --- /dev/null +++ b/test/matrix_api/map_copy_extension_test.dart @@ -0,0 +1,39 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:matrix/src/utils/map_copy_extension.dart'; +import 'package:test/test.dart'; + +void main() { + group('Map-copy-extension', () { + test('it should work', () { + final original = { + 'attr': 'fox', + 'child': { + 'attr': 'bunny', + 'list': [1, 2], + }, + }; + final copy = original.copy(); + original['child']['attr'] = 'raccoon'; + expect(copy['child']['attr'], 'bunny'); + original['child']['list'].add(3); + expect(copy['child']['list'], [1, 2]); + }); + }); +} diff --git a/test/matrix_api/try_get_map_extension_test.dart b/test/matrix_api/try_get_map_extension_test.dart new file mode 100644 index 0000000..886700e --- /dev/null +++ b/test/matrix_api/try_get_map_extension_test.dart @@ -0,0 +1,45 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:matrix_api_lite/matrix_api_lite.dart'; +import 'package:test/test.dart'; + +void main() { + group('Try-get-map-extension', () { + test('it should work', () { + final data = { + 'str': 'foxies', + 'int': 42, + 'list': [2, 3, 4], + 'map': { + 'beep': 'boop', + }, + }; + expect(data.tryGet('str'), 'foxies'); + expect(data.tryGet('str'), null); + expect(data.tryGet('int'), 42); + expect(data.tryGet('list'), [2, 3, 4]); + expect(data.tryGet>('map')?.tryGet('beep'), + 'boop'); + expect(data.tryGet>('map')?.tryGet('meep'), + null); + expect(data.tryGet>('pam')?.tryGet('beep'), + null); + }); + }); +} diff --git a/test/matrix_database_test.dart b/test/matrix_database_test.dart new file mode 100644 index 0000000..0c88c61 --- /dev/null +++ b/test/matrix_database_test.dart @@ -0,0 +1,175 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +import 'package:matrix/matrix.dart'; + +import 'package:test/test.dart'; +import 'fake_database.dart'; + +void main() { + group('Databse', () { + Logs().level = Level.error; + final room = Room(id: '!room:blubb', client: Client('testclient')); + test('setupDatabase', () async { + final database = await getDatabase(null); + await database.insertClient( + 'testclient', + 'https://example.org', + 'blubb', + '@test:example.org', + null, + null, + null, + null, + ); + }); + + test('storeEventUpdate', () async { + final client = Client('testclient'); + final database = await getDatabase(client); + // store a simple update + var update = EventUpdate( + type: EventUpdateType.timeline, + roomID: room.id, + content: { + 'type': 'm.room.message', + 'origin_server_ts': 100, + 'content': {'blah': 'blubb'}, + 'event_id': '\$event-1', + 'sender': '@blah:blubb', + }, + ); + await database.storeEventUpdate(update, client); + var event = await database.getEventById('\$event-1', room); + expect(event?.eventId, '\$event-1'); + + // insert a transaction id + update = EventUpdate( + type: EventUpdateType.timeline, + roomID: room.id, + content: { + 'type': 'm.room.message', + 'origin_server_ts': 100, + 'content': {'blah': 'blubb'}, + 'event_id': 'transaction-1', + 'sender': '@blah:blubb', + 'status': EventStatus.sending.intValue, + }, + ); + await database.storeEventUpdate(update, client); + event = await database.getEventById('transaction-1', room); + expect(event?.eventId, 'transaction-1'); + update = EventUpdate( + type: EventUpdateType.timeline, + roomID: room.id, + content: { + 'type': 'm.room.message', + 'origin_server_ts': 100, + 'content': {'blah': 'blubb'}, + 'event_id': '\$event-2', + 'sender': '@blah:blubb', + 'unsigned': { + 'transaction_id': 'transaction-1', + }, + 'status': EventStatus.sent.intValue, + }, + ); + await database.storeEventUpdate(update, client); + event = await database.getEventById('transaction-1', room); + expect(event, null); + event = await database.getEventById('\$event-2', room); + + // insert a transaction id if the event id for it already exists + update = EventUpdate( + type: EventUpdateType.timeline, + roomID: room.id, + content: { + 'type': 'm.room.message', + 'origin_server_ts': 100, + 'content': {'blah': 'blubb'}, + 'event_id': '\$event-3', + 'sender': '@blah:blubb', + 'status': EventStatus.sending.intValue, + }, + ); + await database.storeEventUpdate(update, client); + event = await database.getEventById('\$event-3', room); + expect(event?.eventId, '\$event-3'); + update = EventUpdate( + type: EventUpdateType.timeline, + roomID: room.id, + content: { + 'type': 'm.room.message', + 'origin_server_ts': 100, + 'content': {'blah': 'blubb'}, + 'event_id': '\$event-3', + 'sender': '@blah:blubb', + 'status': EventStatus.sent.intValue, + 'unsigned': { + 'transaction_id': 'transaction-2', + }, + }, + ); + await database.storeEventUpdate(update, client); + event = await database.getEventById('\$event-3', room); + expect(event?.eventId, '\$event-3'); + expect(event?.status, EventStatus.sent); + event = await database.getEventById('transaction-2', room); + expect(event, null); + + // insert transaction id and not update status + update = EventUpdate( + type: EventUpdateType.timeline, + roomID: room.id, + content: { + 'type': 'm.room.message', + 'origin_server_ts': 100, + 'content': {'blah': 'blubb'}, + 'event_id': '\$event-4', + 'sender': '@blah:blubb', + 'status': EventStatus.synced.intValue, + }, + ); + await database.storeEventUpdate(update, client); + event = await database.getEventById('\$event-4', room); + expect(event?.eventId, '\$event-4'); + update = EventUpdate( + type: EventUpdateType.timeline, + roomID: room.id, + content: { + 'type': 'm.room.message', + 'origin_server_ts': 100, + 'content': {'blah': 'blubb'}, + 'event_id': '\$event-4', + 'sender': '@blah:blubb', + 'status': EventStatus.sent.intValue, + 'unsigned': { + 'transaction_id': 'transaction-3', + }, + }, + ); + await database.storeEventUpdate(update, client); + event = await database.getEventById('\$event-4', room); + expect(event?.eventId, '\$event-4'); + expect(event?.status, EventStatus.synced); + event = await database.getEventById('transaction-3', room); + expect(event, null); + }); + }); +} diff --git a/test/matrix_default_localizations.dart b/test/matrix_default_localizations.dart new file mode 100644 index 0000000..4b35696 --- /dev/null +++ b/test/matrix_default_localizations.dart @@ -0,0 +1,233 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +import 'package:matrix/matrix.dart'; + +class MatrixDefaultLocalizations extends MatrixLocalizations { + const MatrixDefaultLocalizations(); + @override + String acceptedTheInvitation(String targetName) => + '$targetName accepted the invitation'; + + @override + String activatedEndToEndEncryption(String senderName) => + '$senderName activated end to end encryption'; + + @override + String get anyoneCanJoin => 'Anyone can join'; + + @override + String bannedUser(String senderName, String targetName) => + '$senderName banned $targetName'; + + @override + String changedTheChatAvatar(String senderName) => + '$senderName changed the chat avatar'; + + @override + String changedTheChatDescriptionTo(String senderName, String content) => + '$senderName changed the chat description to $content'; + + @override + String changedTheChatNameTo(String senderName, String content) => + '$senderName changed the chat name to $content'; + + @override + String changedTheChatPermissions(String senderName) => + '$senderName changed the chat permissions'; + + @override + String changedTheDisplaynameTo(String targetName, String newDisplayname) => + '$targetName changed the displayname to $newDisplayname'; + + @override + String changedTheGuestAccessRules(String senderName) => + '$senderName changed the guest access rules'; + + @override + String changedTheGuestAccessRulesTo( + String senderName, String localizedString) => + '$senderName changed the guest access rules to $localizedString'; + + @override + String changedTheHistoryVisibility(String senderName) => + '$senderName changed the history visibility'; + + @override + String changedTheHistoryVisibilityTo( + String senderName, String localizedString) => + '$senderName changed the history visibility to $localizedString'; + + @override + String changedTheJoinRules(String senderName) => + '$senderName changed the join rules'; + + @override + String changedTheJoinRulesTo(String senderName, String localizedString) => + '$senderName changed the join rules to $localizedString'; + + @override + String changedTheProfileAvatar(String targetName) => + '$targetName changed the profile avatar'; + + @override + String changedTheRoomAliases(String senderName) => + '$senderName changed the room aliases'; + + @override + String changedTheRoomInvitationLink(String senderName) => + '$senderName changed the room invitation link'; + + @override + String get channelCorruptedDecryptError => + 'The secure channel has been corrupted'; + + @override + String couldNotDecryptMessage(String errorText) => + 'Could not decrypt message: $errorText'; + + @override + String createdTheChat(String senderName) => '$senderName created the chat'; + + @override + String get emptyChat => 'Empty chat'; + + @override + String get encryptionNotEnabled => 'Encryption not enabled'; + + @override + String get fromJoining => 'From joining'; + + @override + String get fromTheInvitation => 'From the invitation'; + + @override + String groupWith(String displayname) => 'Group with $displayname'; + + @override + String get guestsAreForbidden => 'Guests are forbidden'; + + @override + String get guestsCanJoin => 'Guests can join'; + + @override + String hasWithdrawnTheInvitationFor(String senderName, String targetName) => + '$senderName has withdrawn the invitation for $targetName'; + + @override + String invitedUser(String senderName, String targetName) => + '$senderName has invited $targetName'; + + @override + String get invitedUsersOnly => 'Invited users only'; + + @override + String joinedTheChat(String targetName) => '$targetName joined the chat'; + + @override + String kicked(String senderName, String targetName) => + '$senderName kicked $targetName'; + + @override + String kickedAndBanned(String senderName, String targetName) => + '$senderName banned $targetName'; + + @override + String get needPantalaimonWarning => 'Need pantalaimon'; + + @override + String get noPermission => 'No permission'; + + @override + String redactedAnEvent(String senderName) => '$senderName redacted an event'; + + @override + String rejectedTheInvitation(String targetName) => + '$targetName rejected the invitation'; + + @override + String removedBy(String calcDisplayname) => 'Removed by $calcDisplayname'; + + @override + String get roomHasBeenUpgraded => 'Room has been upgraded'; + + @override + String sentAFile(String senderName) => '$senderName sent a file'; + + @override + String sentAPicture(String senderName) => '$senderName sent a picture'; + + @override + String sentASticker(String senderName) => '$senderName sent a sticker'; + + @override + String sentAVideo(String senderName) => '$senderName sent a video'; + + @override + String sentAnAudio(String senderName) => '$senderName sent an audio'; + + @override + String sharedTheLocation(String senderName) => + '$senderName shared the location'; + + @override + String unbannedUser(String senderName, String targetName) => + '$senderName unbanned $targetName'; + + @override + String get unknownEncryptionAlgorithm => 'Unknown encryption algorithm'; + + @override + String unknownEvent(String typeKey) => 'Unknown event $typeKey'; + + @override + String userLeftTheChat(String targetName) => '$targetName left the chat'; + + @override + String get visibleForAllParticipants => 'Visible for all participants'; + + @override + String get visibleForEveryone => 'Visible for everyone'; + + @override + String get you => 'You'; + + @override + String answeredTheCall(String senderName) { + return 'answeredTheCall'; + } + + @override + String endedTheCall(String senderName) { + return 'endedTheCall'; + } + + @override + String sentCallInformations(String senderName) { + return 'sentCallInformations'; + } + + @override + String startedACall(String senderName) { + return 'startedACall'; + } + + @override + String sentReaction(String senderName, String reactionKey) { + return '$senderName reacted with $reactionKey'; + } +} diff --git a/test/matrix_exception_test.dart b/test/matrix_exception_test.dart new file mode 100644 index 0000000..5378f32 --- /dev/null +++ b/test/matrix_exception_test.dart @@ -0,0 +1,68 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +import 'package:matrix/matrix.dart'; +import 'package:http/http.dart'; + +import 'package:test/test.dart'; + +void main() { + /// All Tests related to device keys + group('Matrix Exception', () { + Logs().level = Level.error; + test('Matrix Exception', () async { + final matrixException = MatrixException( + Response( + '{"flows":[{"stages":["example.type.foo"]}],"params":{"example.type.baz":{"example_key":"foobar"}},"session":"xxxxxxyz","completed":["example.type.foo"]}', + 401, + ), + ); + expect(matrixException.errcode, 'M_FORBIDDEN'); + final flows = matrixException.authenticationFlows; + expect(flows?.length, 1); + expect(flows?.first.stages.length, 1); + expect(flows?.first.stages.first, 'example.type.foo'); + expect( + matrixException.authenticationParams?['example.type.baz'], + {'example_key': 'foobar'}, + ); + expect(matrixException.completedAuthenticationFlows.length, 1); + expect(matrixException.completedAuthenticationFlows.first, + 'example.type.foo'); + expect(matrixException.session, 'xxxxxxyz'); + }); + test('Unknown Exception', () async { + final matrixException = MatrixException( + Response( + '{"errcode":"M_HAHA","error":"HAHA","retry_after_ms":500}', + 401, + ), + ); + expect(matrixException.error, MatrixError.M_UNKNOWN); + expect(matrixException.retryAfterMs, 500); + }); + test('Missing Exception', () async { + final matrixException = MatrixException( + Response( + '{"error":"HAHA"}', + 401, + ), + ); + expect(matrixException.error, MatrixError.M_UNKNOWN); + }); + }); +} diff --git a/test/matrix_file_test.dart b/test/matrix_file_test.dart new file mode 100644 index 0000000..d932bcb --- /dev/null +++ b/test/matrix_file_test.dart @@ -0,0 +1,49 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:typed_data'; + +import 'package:matrix/matrix.dart'; + +import 'package:test/test.dart'; +import 'package:olm/olm.dart' as olm; + +void main() { + /// All Tests related to device keys + group('Matrix File', () { + Logs().level = Level.error; + test('Decrypt', () async { + final text = 'hello world'; + final file = MatrixFile( + name: 'file.txt', + bytes: Uint8List.fromList(text.codeUnits), + ); + var olmEnabled = true; + try { + await olm.init(); + olm.Account(); + } catch (_) { + olmEnabled = false; + } + if (olmEnabled) { + final encryptedFile = await file.encrypt(); + expect(encryptedFile.data.isNotEmpty, true); + } + }); + }); +} diff --git a/test/matrix_id_string_extension_test.dart b/test/matrix_id_string_extension_test.dart new file mode 100644 index 0000000..e5fd284 --- /dev/null +++ b/test/matrix_id_string_extension_test.dart @@ -0,0 +1,119 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:test/test.dart'; +import 'package:matrix/src/utils/matrix_id_string_extension.dart'; + +void main() { + /// All Tests related to the ChatTime + group('Matrix ID String Extension', () { + test('Matrix ID String Extension', () async { + final mxId = '@test:example.com'; + expect(mxId.isValidMatrixId, true); + expect('#test:example.com'.isValidMatrixId, true); + expect('!test:example.com'.isValidMatrixId, true); + expect('+test:example.com'.isValidMatrixId, true); + expect('\$test:example.com'.isValidMatrixId, true); + expect('\$testevent'.isValidMatrixId, true); + expect('test:example.com'.isValidMatrixId, false); + expect('@testexample.com'.isValidMatrixId, false); + expect('@:example.com'.isValidMatrixId, true); + expect('@test:'.isValidMatrixId, false); + expect(mxId.sigil, '@'); + expect('#test:example.com'.sigil, '#'); + expect('!test:example.com'.sigil, '!'); + expect('+test:example.com'.sigil, '+'); + expect('\$test:example.com'.sigil, '\$'); + expect(mxId.localpart, 'test'); + expect(mxId.domain, 'example.com'); + expect(mxId.equals('@Test:example.com'), true); + expect(mxId.equals('@test:example.org'), false); + expect('@user:domain:8448'.localpart, 'user'); + expect('@user:domain:8448'.domain, 'domain:8448'); + }); + test('parseIdentifierIntoParts', () { + var res = '#alias:beep'.parseIdentifierIntoParts()!; + expect(res.primaryIdentifier, '#alias:beep'); + expect(res.secondaryIdentifier, null); + expect(res.queryString, null); + expect('blha'.parseIdentifierIntoParts(), null); + res = '#alias:beep/\$event'.parseIdentifierIntoParts()!; + expect(res.primaryIdentifier, '#alias:beep'); + expect(res.secondaryIdentifier, '\$event'); + expect(res.queryString, null); + res = '#alias:beep?blubb'.parseIdentifierIntoParts()!; + expect(res.primaryIdentifier, '#alias:beep'); + expect(res.secondaryIdentifier, null); + expect(res.queryString, 'blubb'); + res = '#alias:beep/\$event?blubb'.parseIdentifierIntoParts()!; + expect(res.primaryIdentifier, '#alias:beep'); + expect(res.secondaryIdentifier, '\$event'); + expect(res.queryString, 'blubb'); + res = '#/\$?:beep/\$event?blubb?b'.parseIdentifierIntoParts()!; + expect(res.primaryIdentifier, '#/\$?:beep'); + expect(res.secondaryIdentifier, '\$event'); + expect(res.queryString, 'blubb?b'); + + res = 'https://matrix.to/#/#alias:beep'.parseIdentifierIntoParts()!; + expect(res.primaryIdentifier, '#alias:beep'); + expect(res.secondaryIdentifier, null); + expect(res.queryString, null); + res = 'https://matrix.to/#/#🦊:beep'.parseIdentifierIntoParts()!; + expect(res.primaryIdentifier, '#🦊:beep'); + expect(res.secondaryIdentifier, null); + expect(res.queryString, null); + res = 'https://matrix.to/#/%23alias%3abeep'.parseIdentifierIntoParts()!; + expect(res.primaryIdentifier, '#alias:beep'); + expect(res.secondaryIdentifier, null); + expect(res.queryString, null); + res = 'https://matrix.to/#/%23alias%3abeep?boop%F0%9F%A7%A1%F0%9F%A6%8A' + .parseIdentifierIntoParts()!; + expect(res.primaryIdentifier, '#alias:beep'); + expect(res.secondaryIdentifier, null); + expect(res.queryString, 'boop%F0%9F%A7%A1%F0%9F%A6%8A'); + + res = 'https://matrix.to/#/#alias:beep?via=fox.com&via=fox.org' + .parseIdentifierIntoParts()!; + expect(res.via, {'fox.com', 'fox.org'}); + + res = 'matrix:u/her:example.org'.parseIdentifierIntoParts()!; + expect(res.primaryIdentifier, '@her:example.org'); + expect(res.secondaryIdentifier, null); + expect('matrix:u/bad'.parseIdentifierIntoParts(), null); + res = 'matrix:roomid/rid:example.org'.parseIdentifierIntoParts()!; + expect(res.primaryIdentifier, '!rid:example.org'); + expect(res.secondaryIdentifier, null); + expect(res.action, null); + res = 'matrix:r/us:example.org?action=chat'.parseIdentifierIntoParts()!; + expect(res.primaryIdentifier, '#us:example.org'); + expect(res.secondaryIdentifier, null); + expect(res.action, 'chat'); + res = 'matrix:r/us:example.org/e/lol823y4bcp3qo4' + .parseIdentifierIntoParts()!; + expect(res.primaryIdentifier, '#us:example.org'); + expect(res.secondaryIdentifier, '\$lol823y4bcp3qo4'); + res = 'matrix:roomid/rid:example.org?via=fox.com&via=fox.org' + .parseIdentifierIntoParts()!; + expect(res.primaryIdentifier, '!rid:example.org'); + expect(res.secondaryIdentifier, null); + expect(res.via, {'fox.com', 'fox.org'}); + expect('matrix:beep/boop:example.org'.parseIdentifierIntoParts(), null); + expect('matrix:boop'.parseIdentifierIntoParts(), null); + }); + }); +} diff --git a/test/matrix_localizations_test.dart b/test/matrix_localizations_test.dart new file mode 100644 index 0000000..6f92b37 --- /dev/null +++ b/test/matrix_localizations_test.dart @@ -0,0 +1,60 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +import 'package:matrix/matrix.dart'; +import 'package:test/test.dart'; + +import 'matrix_default_localizations.dart'; + +void main() { + /// All Tests related to device keys + group('Matrix Localizations', () { + test('Matrix Localizations', () { + expect( + HistoryVisibility.invited + .getLocalizedString(MatrixDefaultLocalizations()), + 'From the invitation'); + expect( + HistoryVisibility.joined + .getLocalizedString(MatrixDefaultLocalizations()), + 'From joining'); + expect( + HistoryVisibility.shared + .getLocalizedString(MatrixDefaultLocalizations()), + 'Visible for all participants'); + expect( + HistoryVisibility.worldReadable + .getLocalizedString(MatrixDefaultLocalizations()), + 'Visible for everyone'); + expect( + GuestAccess.canJoin.getLocalizedString(MatrixDefaultLocalizations()), + 'Guests can join'); + expect( + GuestAccess.forbidden + .getLocalizedString(MatrixDefaultLocalizations()), + 'Guests are forbidden'); + expect(JoinRules.invite.getLocalizedString(MatrixDefaultLocalizations()), + 'Invited users only'); + expect(JoinRules.public.getLocalizedString(MatrixDefaultLocalizations()), + 'Anyone can join'); + expect(JoinRules.private.getLocalizedString(MatrixDefaultLocalizations()), + 'private'); + expect(JoinRules.knock.getLocalizedString(MatrixDefaultLocalizations()), + 'knock'); + }); + }); +} diff --git a/test/multilock_test.dart b/test/multilock_test.dart new file mode 100644 index 0000000..292b1e4 --- /dev/null +++ b/test/multilock_test.dart @@ -0,0 +1,62 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:matrix/src/utils/multilock.dart'; +import 'package:test/test.dart'; + +void main() { + group('lock', () { + final lock = MultiLock(); + test('lock and unlock', () async { + // lock and unlock + await lock.lock(['fox']); + lock.unlock(['fox']); + expect(true, true); // we were able to reach this line of code! + }); + test('lock the same lock', () async { + var counter = 0; + await lock.lock(['fox']); + final future = lock.lock(['fox']).then((_) { + counter++; + lock.unlock(['fox']); + }); + await Future.delayed(Duration(milliseconds: 50)); + expect(counter, 0); + lock.unlock(['fox']); + await future; + expect(counter, 1); + }); + test('multilock', () async { + var counter = 0; + await lock.lock(['fox']); + final future1 = lock.lock(['fox', 'raccoon']).then((_) { + counter++; + lock.unlock(['fox', 'raccoon']); + }); + await Future.delayed(Duration(milliseconds: 50)); + expect(counter, 0); + await lock.lock(['raccoon']); + lock.unlock(['fox']); + await Future.delayed(Duration(milliseconds: 50)); + expect(counter, 0); + lock.unlock(['raccoon']); + await future1; + expect(counter, 1); + }); + }); +} diff --git a/test/mxc_uri_extension_test.dart b/test/mxc_uri_extension_test.dart new file mode 100644 index 0000000..21ea321 --- /dev/null +++ b/test/mxc_uri_extension_test.dart @@ -0,0 +1,88 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:matrix/matrix.dart'; + +import 'package:test/test.dart'; + +import 'fake_matrix_api.dart'; + +void main() { + /// All Tests related to the MxContent + group('MxContent', () { + Logs().level = Level.error; + test('Formatting', () async { + final client = Client('testclient', httpClient: FakeMatrixApi()); + await client.checkHomeserver('https://fakeserver.notexisting', + checkWellKnown: false); + final mxc = 'mxc://exampleserver.abc/abcdefghijklmn'; + final content = Uri.parse(mxc); + expect(content.isScheme('mxc'), true); + + expect(content.getDownloadLink(client).toString(), + '${client.homeserver.toString()}/_matrix/media/r0/download/exampleserver.abc/abcdefghijklmn'); + expect(content.getThumbnail(client, width: 50, height: 50).toString(), + '${client.homeserver.toString()}/_matrix/media/r0/thumbnail/exampleserver.abc/abcdefghijklmn?width=50&height=50&method=crop&animated=false'); + expect( + content + .getThumbnail(client, + width: 50, + height: 50, + method: ThumbnailMethod.scale, + animated: true) + .toString(), + '${client.homeserver.toString()}/_matrix/media/r0/thumbnail/exampleserver.abc/abcdefghijklmn?width=50&height=50&method=scale&animated=true'); + }); + test('other port', () async { + final client = Client('testclient', httpClient: FakeMatrixApi()); + await client.checkHomeserver('https://fakeserver.notexisting', + checkWellKnown: false); + client.homeserver = Uri.parse('https://fakeserver.notexisting:1337'); + final mxc = 'mxc://exampleserver.abc/abcdefghijklmn'; + final content = Uri.parse(mxc); + expect(content.isScheme('mxc'), true); + + expect(content.getDownloadLink(client).toString(), + '${client.homeserver.toString()}/_matrix/media/r0/download/exampleserver.abc/abcdefghijklmn'); + expect(content.getThumbnail(client, width: 50, height: 50).toString(), + '${client.homeserver.toString()}/_matrix/media/r0/thumbnail/exampleserver.abc/abcdefghijklmn?width=50&height=50&method=crop&animated=false'); + expect( + content + .getThumbnail(client, + width: 50, + height: 50, + method: ThumbnailMethod.scale, + animated: true) + .toString(), + 'https://fakeserver.notexisting:1337/_matrix/media/r0/thumbnail/exampleserver.abc/abcdefghijklmn?width=50&height=50&method=scale&animated=true'); + }); + test('other remote port', () async { + final client = Client('testclient', httpClient: FakeMatrixApi()); + await client.checkHomeserver('https://fakeserver.notexisting', + checkWellKnown: false); + final mxc = 'mxc://exampleserver.abc:1234/abcdefghijklmn'; + final content = Uri.parse(mxc); + expect(content.isScheme('mxc'), true); + + expect(content.getDownloadLink(client).toString(), + '${client.homeserver.toString()}/_matrix/media/r0/download/exampleserver.abc:1234/abcdefghijklmn'); + expect(content.getThumbnail(client, width: 50, height: 50).toString(), + '${client.homeserver.toString()}/_matrix/media/r0/thumbnail/exampleserver.abc:1234/abcdefghijklmn?width=50&height=50&method=crop&animated=false'); + }); + }); +} diff --git a/test/room_test.dart b/test/room_test.dart new file mode 100644 index 0000000..c47ae44 --- /dev/null +++ b/test/room_test.dart @@ -0,0 +1,1138 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:convert'; +import 'dart:typed_data'; + +import 'package:matrix/matrix.dart'; + +import 'package:test/test.dart'; + +import 'fake_client.dart'; +import 'fake_matrix_api.dart'; + +void main() { + late Client matrix; + late Room room; + + /// All Tests related to the Event + group('Room', () { + Logs().level = Level.error; + test('Login', () async { + matrix = await getClient(); + }); + + test('Create from json', () async { + final id = '!localpart:server.abc'; + final membership = Membership.join; + final notificationCount = 2; + final highlightCount = 1; + final heroes = [ + '@alice:matrix.org', + '@bob:example.com', + '@charley:example.org' + ]; + + room = Room( + client: matrix, + id: id, + membership: membership, + highlightCount: highlightCount, + notificationCount: notificationCount, + prev_batch: '', + newestSortOrder: 0.0, + oldestSortOrder: 0.0, + summary: RoomSummary.fromJson({ + 'm.joined_member_count': 2, + 'm.invited_member_count': 2, + 'm.heroes': heroes, + }), + roomAccountData: { + 'com.test.foo': BasicRoomEvent( + type: 'com.test.foo', + content: {'foo': 'bar'}, + ), + }, + ); + room.setState(Event( + room: room, + eventId: '143273582443PhrSn:example.org', + originServerTs: DateTime.fromMillisecondsSinceEpoch(1432735824653), + senderId: '@example:example.org', + type: 'm.room.join_rules', + unsigned: {'age': 1234}, + content: {'join_rule': 'public'}, + stateKey: '', + )); + + expect(room.id, id); + expect(room.membership, membership); + expect(room.notificationCount, notificationCount); + expect(room.highlightCount, highlightCount); + expect(room.summary.mJoinedMemberCount, notificationCount); + expect(room.summary.mInvitedMemberCount, notificationCount); + expect(room.summary.mHeroes, heroes); + expect(room.displayname, 'Alice, Bob, Charley'); + expect( + room.getState('m.room.join_rules')?.content['join_rule'], 'public'); + expect(room.roomAccountData['com.test.foo']?.content['foo'], 'bar'); + + room.setState( + Event( + senderId: '@test:example.com', + type: 'm.room.canonical_alias', + room: room, + eventId: '123', + content: {'alias': '#testalias:example.com'}, + originServerTs: DateTime.now(), + stateKey: ''), + ); + expect(room.displayname, 'testalias'); + expect(room.canonicalAlias, '#testalias:example.com'); + + room.setState( + Event( + senderId: '@test:example.com', + type: 'm.room.name', + room: room, + eventId: '123', + content: {'name': 'testname'}, + originServerTs: DateTime.now(), + stateKey: ''), + ); + expect(room.displayname, 'testname'); + + expect(room.topic, ''); + room.setState( + Event( + senderId: '@test:example.com', + type: 'm.room.topic', + room: room, + eventId: '123', + content: {'topic': 'testtopic'}, + originServerTs: DateTime.now(), + stateKey: ''), + ); + expect(room.topic, 'testtopic'); + + expect(room.avatar, null); + room.setState( + Event( + senderId: '@test:example.com', + type: 'm.room.avatar', + room: room, + eventId: '123', + content: {'url': 'mxc://testurl'}, + originServerTs: DateTime.now(), + stateKey: ''), + ); + expect(room.avatar.toString(), 'mxc://testurl'); + + expect(room.pinnedEventIds, []); + room.setState( + Event( + senderId: '@test:example.com', + type: 'm.room.pinned_events', + room: room, + eventId: '123', + content: { + 'pinned': ['1234'] + }, + originServerTs: DateTime.now(), + stateKey: ''), + ); + expect(room.pinnedEventIds.first, '1234'); + room.setState( + Event( + senderId: '@test:example.com', + type: 'm.room.message', + room: room, + eventId: '12345', + originServerTs: DateTime.now(), + content: {'msgtype': 'm.text', 'body': 'abc'}, + stateKey: '', + ), + ); + expect(room.lastEvent?.eventId, '12345'); + expect(room.lastEvent?.body, 'abc'); + expect(room.timeCreated, room.lastEvent?.originServerTs); + }); + + test('lastEvent is set properly', () { + room.setState( + Event( + senderId: '@test:example.com', + type: 'm.room.encrypted', + room: room, + eventId: '1', + originServerTs: DateTime.now(), + content: {'msgtype': 'm.text', 'body': 'cd'}, + stateKey: '', + ), + ); + expect(room.hasNewMessages, true); + expect(room.lastEvent?.body, 'cd'); + room.setState( + Event( + senderId: '@test:example.com', + type: 'm.room.encrypted', + room: room, + eventId: '2', + originServerTs: DateTime.now(), + content: {'msgtype': 'm.text', 'body': 'cdc'}, + stateKey: '', + ), + ); + expect(room.lastEvent?.body, 'cdc'); + room.setState( + Event( + senderId: '@test:example.com', + type: 'm.room.encrypted', + room: room, + eventId: '3', + originServerTs: DateTime.now(), + content: { + 'm.new_content': {'msgtype': 'm.text', 'body': 'test ok'}, + 'm.relates_to': {'rel_type': 'm.replace', 'event_id': '1'}, + 'msgtype': 'm.text', + 'body': '* test ok', + }, + stateKey: '', + ), + ); + expect(room.lastEvent?.body, 'cdc'); // because we edited the "cd" message + room.setState( + Event( + senderId: '@test:example.com', + type: 'm.room.encrypted', + room: room, + eventId: '4', + originServerTs: DateTime.now(), + content: { + 'msgtype': 'm.text', + 'body': 'edited cdc', + 'm.new_content': {'msgtype': 'm.text', 'body': 'edited cdc'}, + 'm.relates_to': {'rel_type': 'm.replace', 'event_id': '2'}, + }, + stateKey: '', + status: EventStatus.sending, + ), + ); + expect(room.lastEvent?.body, 'edited cdc'); + expect(room.lastEvent?.status, EventStatus.sending); + expect(room.lastEvent?.eventId, '4'); + + // Status update on edits working? + room.setState( + Event( + senderId: '@test:example.com', + type: 'm.room.encrypted', + room: room, + eventId: '5', + unsigned: {'transaction_id': '4'}, + originServerTs: DateTime.now(), + content: { + 'msgtype': 'm.text', + 'body': 'edited cdc', + 'm.new_content': {'msgtype': 'm.text', 'body': 'edited cdc'}, + 'm.relates_to': {'rel_type': 'm.replace', 'event_id': '2'}, + }, + stateKey: '', + status: EventStatus.sent, + ), + ); + expect(room.lastEvent?.eventId, '5'); + expect(room.lastEvent?.body, 'edited cdc'); + expect(room.lastEvent?.status, EventStatus.sent); + // Are reactions coming through? + room.setState( + Event( + senderId: '@test:example.com', + type: EventTypes.Reaction, + room: room, + eventId: '123456', + originServerTs: DateTime.now(), + content: { + 'm.relates_to': { + 'rel_type': RelationshipTypes.reaction, + 'event_id': '1234', + 'key': ':-)', + } + }, + stateKey: '', + ), + ); + expect(room.lastEvent?.eventId, '5'); + expect(room.lastEvent?.body, 'edited cdc'); + expect(room.lastEvent?.status, EventStatus.sent); + }); + test('lastEvent when reply parent edited', () async { + room.setState( + Event( + senderId: '@test:example.com', + type: 'm.room.encrypted', + room: room, + eventId: '5', + originServerTs: DateTime.now(), + content: {'msgtype': 'm.text', 'body': 'A'}, + stateKey: '', + ), + ); + expect(room.lastEvent?.body, 'A'); + + room.setState( + Event( + senderId: '@test:example.com', + type: 'm.room.encrypted', + room: room, + eventId: '6', + originServerTs: DateTime.now(), + content: { + 'msgtype': 'm.text', + 'body': 'B', + 'm.relates_to': {'rel_type': 'm.in_reply_to', 'event_id': '5'} + }, + stateKey: '', + ), + ); + expect(room.lastEvent?.body, 'B'); + room.setState( + Event( + senderId: '@test:example.com', + type: 'm.room.encrypted', + room: room, + eventId: '7', + originServerTs: DateTime.now(), + content: { + 'msgtype': 'm.text', + 'body': 'edited A', + 'm.new_content': {'msgtype': 'm.text', 'body': 'edited A'}, + 'm.relates_to': {'rel_type': 'm.replace', 'event_id': '5'}, + }, + stateKey: '', + ), + ); + expect(room.lastEvent?.body, 'B'); + }); + test('sendReadMarker', () async { + await room.setReadMarker('§1234:fakeServer.notExisting'); + }); + + test('requestParticipants', () async { + final participants = await room.requestParticipants(); + expect(participants.length, 1); + final user = participants[0]; + expect(user.id, '@alice:example.org'); + expect(user.displayName, 'Alice Margatroid'); + expect(user.membership, Membership.join); + expect(user.avatarUrl.toString(), 'mxc://example.org/SEsfnsuifSDFSSEF'); + expect(user.room.id, '!localpart:server.abc'); + }); + + test('getEventByID', () async { + final event = await room.getEventById('1234'); + expect(event?.eventId, '143273582443PhrSn:example.org'); + }); + + test('setName', () async { + final eventId = await room.setName('Testname'); + expect(eventId, '42'); + }); + + test('setDescription', () async { + final eventId = await room.setDescription('Testname'); + expect(eventId, '42'); + }); + + test('kick', () async { + await room.kick('Testname'); + }); + + test('ban', () async { + await room.ban('Testname'); + }); + + test('unban', () async { + await room.unban('Testname'); + }); + + test('PowerLevels', () async { + room.setState( + Event( + senderId: '@test:example.com', + type: 'm.room.power_levels', + room: room, + eventId: '123', + content: { + 'ban': 50, + 'events': {'m.room.name': 100, 'm.room.power_levels': 100}, + 'events_default': 0, + 'invite': 50, + 'kick': 50, + 'notifications': {'room': 20}, + 'redact': 50, + 'state_default': 50, + 'users': {'@test:fakeServer.notExisting': 100}, + 'users_default': 10 + }, + originServerTs: DateTime.now(), + stateKey: ''), + ); + expect(room.ownPowerLevel, 100); + expect(room.getPowerLevelByUserId(matrix.userID!), room.ownPowerLevel); + expect(room.getPowerLevelByUserId('@nouser:example.com'), 10); + expect(room.ownPowerLevel, 100); + expect(room.canBan, true); + expect(room.canInvite, true); + expect(room.canKick, true); + expect(room.canRedact, true); + expect(room.canSendDefaultMessages, true); + expect(room.canSendDefaultStates, true); + expect(room.canChangePowerLevel, true); + expect(room.canSendEvent('m.room.name'), true); + expect(room.canSendEvent('m.room.power_levels'), true); + expect(room.canSendEvent('m.room.member'), true); + expect(room.powerLevels, + room.getState('m.room.power_levels')?.content['users']); + + room.setState( + Event( + senderId: '@test:example.com', + type: 'm.room.power_levels', + room: room, + eventId: '123abc', + content: { + 'ban': 50, + 'events': {'m.room.name': 0, 'm.room.power_levels': 100}, + 'events_default': 0, + 'invite': 50, + 'kick': 50, + 'notifications': {'room': 20}, + 'redact': 50, + 'state_default': 50, + 'users': {}, + 'users_default': 0 + }, + originServerTs: DateTime.now(), + stateKey: '', + ), + ); + expect(room.ownPowerLevel, 0); + expect(room.canBan, false); + expect(room.canInvite, false); + expect(room.canKick, false); + expect(room.canRedact, false); + expect(room.canSendDefaultMessages, true); + expect(room.canSendDefaultStates, false); + expect(room.canChangePowerLevel, false); + expect(room.canSendEvent('m.room.name'), true); + expect(room.canSendEvent('m.room.power_levels'), false); + expect(room.canSendEvent('m.room.member'), false); + expect(room.canSendEvent('m.room.message'), true); + final resp = await room.setPower('@test:fakeServer.notExisting', 90); + expect(resp, '42'); + }); + + test('invite', () async { + await room.invite('Testname'); + }); + + test('getParticipants', () async { + var userList = room.getParticipants(); + expect(userList.length, 4); + // add new user + room.setState(Event( + senderId: '@alice:test.abc', + type: 'm.room.member', + room: room, + eventId: '12345', + originServerTs: DateTime.now(), + content: {'displayname': 'alice'}, + stateKey: '@alice:test.abc')); + userList = room.getParticipants(); + expect(userList.length, 5); + expect(userList[4].displayName, 'alice'); + }); + + test('addToDirectChat', () async { + await room.addToDirectChat('Testname'); + }); + + test('getTimeline', () async { + final timeline = await room.getTimeline(); + expect(timeline.events.length, 0); + }); + + test('getUserByMXID', () async { + User? user; + try { + user = await room.requestUser('@getme:example.com'); + } catch (_) {} + expect(user?.stateKey, '@getme:example.com'); + expect(user?.calcDisplayname(), 'Getme'); + }); + + test('setAvatar', () async { + final testFile = MatrixFile(bytes: Uint8List(0), name: 'file.jpeg'); + final dynamic resp = await room.setAvatar(testFile); + expect(resp, 'YUwRidLecu:example.com'); + }); + + test('sendEvent', () async { + final dynamic resp = await room.sendEvent( + {'msgtype': 'm.text', 'body': 'hello world'}, + txid: 'testtxid'); + expect(resp?.startsWith('\$event'), true); + }); + + test('sendEvent', () async { + FakeMatrixApi.calledEndpoints.clear(); + final dynamic resp = + await room.sendTextEvent('Hello world', txid: 'testtxid'); + expect(resp?.startsWith('\$event'), true); + final entry = FakeMatrixApi.calledEndpoints.entries + .firstWhere((p) => p.key.contains('/send/m.room.message/')); + final content = json.decode(entry.value.first); + expect(content, { + 'body': 'Hello world', + 'msgtype': 'm.text', + }); + }); + + test('send edit', () async { + FakeMatrixApi.calledEndpoints.clear(); + final dynamic resp = await room.sendTextEvent('Hello world', + txid: 'testtxid', editEventId: '\$otherEvent'); + expect(resp?.startsWith('\$event'), true); + final entry = FakeMatrixApi.calledEndpoints.entries + .firstWhere((p) => p.key.contains('/send/m.room.message/')); + final content = json.decode(entry.value.first); + expect(content, { + 'body': '* Hello world', + 'msgtype': 'm.text', + 'm.new_content': { + 'body': 'Hello world', + 'msgtype': 'm.text', + }, + 'm.relates_to': { + 'event_id': '\$otherEvent', + 'rel_type': 'm.replace', + }, + }); + }); + + test('send reply', () async { + var event = Event.fromJson({ + 'event_id': '\$replyEvent', + 'content': { + 'body': 'Blah', + 'msgtype': 'm.text', + }, + 'type': 'm.room.message', + 'sender': '@alice:example.org', + }, room); + FakeMatrixApi.calledEndpoints.clear(); + var resp = await room.sendTextEvent('Hello world', + txid: 'testtxid', inReplyTo: event); + expect(resp?.startsWith('\$event'), true); + var entry = FakeMatrixApi.calledEndpoints.entries + .firstWhere((p) => p.key.contains('/send/m.room.message/')); + var content = json.decode(entry.value.first); + expect(content, { + 'body': '> <@alice:example.org> Blah\n\nHello world', + 'msgtype': 'm.text', + 'format': 'org.matrix.custom.html', + 'formatted_body': + '
In reply to @alice:example.org
Blah
Hello world', + 'm.relates_to': { + 'm.in_reply_to': { + 'event_id': '\$replyEvent', + }, + }, + }); + + event = Event.fromJson({ + 'event_id': '\$replyEvent', + 'content': { + 'body': 'Blah\nbeep', + 'msgtype': 'm.text', + }, + 'type': 'm.room.message', + 'sender': '@alice:example.org', + }, room); + FakeMatrixApi.calledEndpoints.clear(); + resp = await room.sendTextEvent('Hello world\nfox', + txid: 'testtxid', inReplyTo: event); + expect(resp?.startsWith('\$event'), true); + entry = FakeMatrixApi.calledEndpoints.entries + .firstWhere((p) => p.key.contains('/send/m.room.message/')); + content = json.decode(entry.value.first); + expect(content, { + 'body': + '> <@alice:example.org> Blah\n> beep\n\nHello world\nfox', + 'msgtype': 'm.text', + 'format': 'org.matrix.custom.html', + 'formatted_body': + '
In reply to @alice:example.org
<b>Blah</b>
beep
Hello world
fox', + 'm.relates_to': { + 'm.in_reply_to': { + 'event_id': '\$replyEvent', + }, + }, + }); + + event = Event.fromJson({ + 'event_id': '\$replyEvent', + 'content': { + 'format': 'org.matrix.custom.html', + 'formatted_body': 'heyameow', + 'body': 'plaintext meow', + 'msgtype': 'm.text', + }, + 'type': 'm.room.message', + 'sender': '@alice:example.org', + }, room); + FakeMatrixApi.calledEndpoints.clear(); + resp = await room.sendTextEvent('Hello world', + txid: 'testtxid', inReplyTo: event); + expect(resp?.startsWith('\$event'), true); + entry = FakeMatrixApi.calledEndpoints.entries + .firstWhere((p) => p.key.contains('/send/m.room.message/')); + content = json.decode(entry.value.first); + expect(content, { + 'body': '> <@alice:example.org> plaintext meow\n\nHello world', + 'msgtype': 'm.text', + 'format': 'org.matrix.custom.html', + 'formatted_body': + '
In reply to @alice:example.org
meow
Hello world', + 'm.relates_to': { + 'm.in_reply_to': { + 'event_id': '\$replyEvent', + }, + }, + }); + + event = Event.fromJson({ + 'event_id': '\$replyEvent', + 'content': { + 'body': 'Hey @room', + 'msgtype': 'm.text', + }, + 'type': 'm.room.message', + 'sender': '@alice:example.org', + }, room); + FakeMatrixApi.calledEndpoints.clear(); + resp = await room.sendTextEvent('Hello world', + txid: 'testtxid', inReplyTo: event); + expect(resp?.startsWith('\$event'), true); + entry = FakeMatrixApi.calledEndpoints.entries + .firstWhere((p) => p.key.contains('/send/m.room.message/')); + content = json.decode(entry.value.first); + expect(content, { + 'body': '> <@alice:example.org> Hey @\u{200b}room\n\nHello world', + 'msgtype': 'm.text', + 'format': 'org.matrix.custom.html', + 'formatted_body': + '
In reply to @alice:example.org
Hey @room
Hello world', + 'm.relates_to': { + 'm.in_reply_to': { + 'event_id': '\$replyEvent', + }, + }, + }); + + // Reply to a reply + event = Event.fromJson({ + 'event_id': '\$replyEvent', + 'content': { + 'body': '> <@alice:example.org> Hey\n\nHello world', + 'msgtype': 'm.text', + 'format': 'org.matrix.custom.html', + 'formatted_body': + '
In reply to @alice:example.org
Hey
Hello world', + 'm.relates_to': { + 'm.in_reply_to': { + 'event_id': '\$replyEvent', + }, + }, + }, + 'type': 'm.room.message', + 'sender': '@alice:example.org', + }, room); + FakeMatrixApi.calledEndpoints.clear(); + resp = + await room.sendTextEvent('Fox', txid: 'testtxid', inReplyTo: event); + expect(resp?.startsWith('\$event'), true); + entry = FakeMatrixApi.calledEndpoints.entries + .firstWhere((p) => p.key.contains('/send/m.room.message/')); + content = json.decode(entry.value.first); + expect(content, { + 'body': '> <@alice:example.org> Hello world\n\nFox', + 'msgtype': 'm.text', + 'format': 'org.matrix.custom.html', + 'formatted_body': + '
In reply to @alice:example.org
Hello world
Fox', + 'm.relates_to': { + 'm.in_reply_to': { + 'event_id': '\$replyEvent', + }, + }, + }); + }); + + test('send reaction', () async { + FakeMatrixApi.calledEndpoints.clear(); + final dynamic resp = + await room.sendReaction('\$otherEvent', '🦊', txid: 'testtxid'); + expect(resp?.startsWith('\$event'), true); + final entry = FakeMatrixApi.calledEndpoints.entries + .firstWhere((p) => p.key.contains('/send/m.reaction/')); + final content = json.decode(entry.value.first); + expect(content, { + 'm.relates_to': { + 'event_id': '\$otherEvent', + 'rel_type': 'm.annotation', + 'key': '🦊', + }, + }); + }); + + test('send location', () async { + FakeMatrixApi.calledEndpoints.clear(); + + final body = 'Middle of the ocean'; + final geoUri = 'geo:0.0,0.0'; + final dynamic resp = + await room.sendLocation(body, geoUri, txid: 'testtxid'); + expect(resp?.startsWith('\$event'), true); + + final entry = FakeMatrixApi.calledEndpoints.entries + .firstWhere((p) => p.key.contains('/send/m.room.message/')); + final content = json.decode(entry.value.first); + expect(content, { + 'msgtype': 'm.location', + 'body': body, + 'geo_uri': geoUri, + }); + }); + + // Not working because there is no real file to test it... + /*test('sendImageEvent', () async { + final File testFile = File.fromUri(Uri.parse("fake/path/file.jpeg")); + final dynamic resp = + await room.sendImageEvent(testFile, txid: "testtxid"); + expect(resp, "42"); + });*/ + + test('sendFileEvent', () async { + final testFile = MatrixFile(bytes: Uint8List(0), name: 'file.jpeg'); + final dynamic resp = await room.sendFileEvent(testFile, txid: 'testtxid'); + expect(resp.toString(), 'mxc://example.com/AQwafuaFswefuhsfAFAgsw'); + }); + + test('pushRuleState', () async { + expect(room.pushRuleState, PushRuleState.mentionsOnly); + matrix.accountData['m.push_rules']?.content['global']['override'].add( + matrix.accountData['m.push_rules']?.content['global']['room'][0]); + expect(room.pushRuleState, PushRuleState.dontNotify); + }); + + test('Test call methods', () async { + await room.inviteToCall('1234', 1234, '4567', '7890', 'sdp', + txid: '1234'); + await room.answerCall('1234', 'sdp', '4567', txid: '1234'); + await room.sendCallCandidates('1234', '4567', [], txid: '1234'); + await room.selectCallAnswer('1234', 1234, '4567', '6789', txid: '1234'); + await room.sendCallReject('1234', 1234, '4567', txid: '1234'); + await room.sendCallNegotiate('1234', 1234, '4567', 'sdp', txid: '1234'); + await room.hangupCall('1234', '4567', 'user_hangup', txid: '1234'); + await room.sendAssertedIdentity( + '1234', + '4567', + AssertedIdentity() + ..displayName = 'name' + ..id = 'some_id', + txid: '1234'); + await room.sendCallReplaces('1234', '4567', CallReplaces(), txid: '1234'); + await room.sendSDPStreamMetadataChanged( + '1234', '4567', SDPStreamMetadata({}), + txid: '1234'); + }); + + test('enableEncryption', () async { + await room.enableEncryption(); + }); + + test('Enable encryption', () async { + room.setState( + Event( + senderId: '@alice:test.abc', + type: 'm.room.encryption', + room: room, + eventId: '12345', + originServerTs: DateTime.now(), + content: { + 'algorithm': AlgorithmTypes.megolmV1AesSha2, + 'rotation_period_ms': 604800000, + 'rotation_period_msgs': 100 + }, + stateKey: ''), + ); + expect(room.encrypted, true); + expect(room.encryptionAlgorithm, AlgorithmTypes.megolmV1AesSha2); + }); + + test('setPushRuleState', () async { + await room.setPushRuleState(PushRuleState.notify); + await room.setPushRuleState(PushRuleState.dontNotify); + await room.setPushRuleState(PushRuleState.mentionsOnly); + await room.setPushRuleState(PushRuleState.notify); + }); + + test('Test tag methods', () async { + await room.addTag(TagType.favourite, order: 0.1); + await room.removeTag(TagType.favourite); + expect(room.isFavourite, false); + room.roomAccountData['m.tag'] = BasicRoomEvent.fromJson({ + 'content': { + 'tags': { + 'm.favourite': {'order': 0.1}, + 'm.wrong': {'order': 0.2}, + } + }, + 'type': 'm.tag' + }); + expect(room.tags.length, 1); + expect(room.tags[TagType.favourite]?.order, 0.1); + expect(room.isFavourite, true); + await room.setFavourite(false); + }); + + test('Test marked unread room', () async { + await room.markUnread(true); + await room.markUnread(false); + expect(room.markedUnread, false); + room.roomAccountData['com.famedly.marked_unread'] = + BasicRoomEvent.fromJson({ + 'content': {'unread': true}, + 'type': 'com.famedly.marked_unread' + }); + expect(room.markedUnread, true); + }); + + test('joinRules', () async { + expect(room.canChangeJoinRules, false); + expect(room.joinRules, JoinRules.public); + room.setState(Event.fromJson( + { + 'content': {'join_rule': 'invite'}, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'state_key': '', + 'type': 'm.room.join_rules', + 'unsigned': {'age': 1234} + }, + room, + )); + expect(room.joinRules, JoinRules.invite); + await room.setJoinRules(JoinRules.invite); + }); + + test('guestAccess', () async { + expect(room.canChangeGuestAccess, false); + expect(room.guestAccess, GuestAccess.forbidden); + room.setState(Event.fromJson( + { + 'content': {'guest_access': 'can_join'}, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'state_key': '', + 'type': 'm.room.guest_access', + 'unsigned': {'age': 1234} + }, + room, + )); + expect(room.guestAccess, GuestAccess.canJoin); + await room.setGuestAccess(GuestAccess.canJoin); + }); + + test('historyVisibility', () async { + expect(room.canChangeHistoryVisibility, false); + expect(room.historyVisibility, null); + room.setState(Event.fromJson( + { + 'content': {'history_visibility': 'shared'}, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'state_key': '', + 'type': 'm.room.history_visibility', + 'unsigned': {'age': 1234} + }, + room, + )); + expect(room.historyVisibility, HistoryVisibility.shared); + await room.setHistoryVisibility(HistoryVisibility.joined); + }); + + test('setState', () async { + // not set non-state-events + room.setState(Event.fromJson( + { + 'content': {'history_visibility': 'shared'}, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'type': 'm.custom', + 'unsigned': {'age': 1234} + }, + room, + )); + expect(room.getState('m.custom') != null, false); + + // set state events + room.setState(Event.fromJson( + { + 'content': {'history_visibility': 'shared'}, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'state_key': '', + 'type': 'm.custom', + 'unsigned': {'age': 1234} + }, + room, + )); + expect(room.getState('m.custom') != null, true); + + // sets messages as state events + room.setState(Event.fromJson( + { + 'content': {'history_visibility': 'shared'}, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'type': 'm.room.message', + 'unsigned': {'age': 1234} + }, + room, + )); + expect(room.getState('m.room.message') != null, true); + }); + + test('Widgets', () { + expect(room.widgets.isEmpty, true); + room.states['m.widget'] = { + 'test': Event.fromJson({ + 'content': { + 'creatorUserId': '@rxl881:matrix.org', + 'data': {'title': 'Bridges Dashboard', 'dateRange': '1y'}, + 'id': 'grafana_@rxl881:matrix.org_1514573757015', + 'name': 'Grafana', + 'type': 'm.grafana', + 'url': 'https://matrix.org/grafana/whatever', + 'waitForIframeLoad': true + }, + 'room_id': '!foo:bar', + 'event_id': '\$15104760642668662QICBu:matrix.org', + 'sender': '@rxl881:matrix.org', + 'state_key': 'test', + 'origin_server_ts': 1432735824653, + 'type': 'm.widget' + }, room), + }; + expect(room.widgets.length, 1); + room.states['m.widget'] = { + 'test2': Event.fromJson({ + 'content': { + 'creatorUserId': '@rxl881:matrix.org', + 'data': {'title': 'Bridges Dashboard', 'dateRange': '1y'}, + 'id': 'grafana_@rxl881:matrix.org_1514573757016', + 'type': 'm.grafana', + 'url': 'https://matrix.org/grafana/whatever', + 'waitForIframeLoad': true + }, + 'room_id': '!foo:bar', + 'event_id': '\$15104760642668663QICBu:matrix.org', + 'sender': '@rxl881:matrix.org', + 'state_key': 'test2', + 'origin_server_ts': 1432735824653, + 'type': 'm.widget' + }, room), + }; + expect(room.widgets.length, 1); + room.states['m.widget'] = { + 'test3': Event.fromJson({ + 'content': { + 'creatorUserId': '@rxl881:matrix.org', + 'data': {'title': 'Bridges Dashboard', 'dateRange': '1y'}, + 'type': 'm.grafana', + 'waitForIframeLoad': true + }, + 'room_id': '!foo:bar', + 'event_id': '\$15104760642668662QICBu:matrix.org', + 'sender': '@rxl881:matrix.org', + 'state_key': 'test3', + 'origin_server_ts': 1432735824655, + 'type': 'm.widget' + }, room), + }; + expect(room.widgets.length, 0); + }); + + test('Spaces', () async { + expect(room.isSpace, false); + room.states['m.room.create'] = { + '': Event.fromJson( + { + 'content': {'type': 'm.space'}, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'type': 'm.room.create', + 'unsigned': {'age': 1234}, + 'state_key': '', + }, + room, + ), + }; + expect(room.isSpace, true); + + expect(room.spaceParents.isEmpty, true); + room.states[EventTypes.spaceParent] = { + '!1234:example.invalid': Event.fromJson( + { + 'content': { + 'via': ['example.invalid'] + }, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'type': EventTypes.spaceParent, + 'unsigned': {'age': 1234}, + 'state_key': '!1234:example.invalid', + }, + room, + ), + }; + expect(room.spaceParents.length, 1); + + expect(room.spaceChildren.isEmpty, true); + room.states[EventTypes.spaceChild] = { + '!b:example.invalid': Event.fromJson( + { + 'content': { + 'via': ['example.invalid'], + 'order': 'b', + }, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'type': EventTypes.spaceChild, + 'unsigned': {'age': 1234}, + 'state_key': '!b:example.invalid', + }, + room, + ), + '!c:example.invalid': Event.fromJson( + { + 'content': { + 'via': ['example.invalid'], + 'order': 'c', + }, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'type': EventTypes.spaceChild, + 'unsigned': {'age': 1234}, + 'state_key': '!c:example.invalid', + }, + room, + ), + '!noorder:example.invalid': Event.fromJson( + { + 'content': { + 'via': ['example.invalid'], + }, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'type': EventTypes.spaceChild, + 'unsigned': {'age': 1234}, + 'state_key': '!noorder:example.invalid', + }, + room, + ), + '!a:example.invalid': Event.fromJson( + { + 'content': { + 'via': ['example.invalid'], + 'order': 'a', + }, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'type': EventTypes.spaceChild, + 'unsigned': {'age': 1234}, + 'state_key': '!a:example.invalid', + }, + room, + ), + }; + expect(room.spaceChildren.length, 4); + + expect(room.spaceChildren[0].roomId, '!a:example.invalid'); + expect(room.spaceChildren[1].roomId, '!b:example.invalid'); + expect(room.spaceChildren[2].roomId, '!c:example.invalid'); + expect(room.spaceChildren[3].roomId, '!noorder:example.invalid'); + + // TODO: Implement a more generic fake api + /*await room.setSpaceChild( + '!jEsUZKDJdhlrceRyVU:example.org', + via: ['example.invalid'], + order: '5', + suggested: true, + ); + await room.removeSpaceChild('!1234:example.invalid');*/ + }); + + test('getMention', () async { + expect(room.getMention('@invalid'), null); + expect(room.getMention('@[Alice Margatroid]'), '@alice:example.org'); + expect(room.getMention('@[Alice Margatroid]#1754'), '@alice:example.org'); + }); + + test('logout', () async { + await matrix.logout(); + }); + }); +} diff --git a/test/sync_filter_test.dart b/test/sync_filter_test.dart new file mode 100644 index 0000000..bb0b263 --- /dev/null +++ b/test/sync_filter_test.dart @@ -0,0 +1,169 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:matrix/matrix.dart'; + +import 'package:test/test.dart'; + +const updates = { + 'empty': { + 'next_batch': 'blah', + 'account_data': { + 'events': [], + }, + 'presences': { + 'events': [], + }, + 'rooms': { + 'join': {}, + 'leave': {}, + 'invite': {}, + }, + 'to_device': { + 'events': [], + }, + }, + 'presence': { + 'next_batch': 'blah', + 'presence': { + 'events': [ + { + 'content': { + 'avatar_url': 'mxc://localhost:wefuiwegh8742w', + 'last_active_ago': 2478593, + 'presence': 'online', + 'currently_active': false, + 'status_msg': 'Making cupcakes' + }, + 'type': 'm.presence', + 'sender': '@example:localhost', + }, + ], + }, + }, + 'account_data': { + 'next_batch': 'blah', + 'account_data': { + 'events': [ + { + 'type': 'blah', + 'content': { + 'beep': 'boop', + }, + }, + ], + }, + }, + 'invite': { + 'next_batch': 'blah', + 'rooms': { + 'invite': { + '!room': { + 'invite_state': { + 'events': [], + }, + }, + }, + }, + }, + 'leave': { + 'next_batch': 'blah', + 'rooms': { + 'leave': { + '!room': {}, + }, + }, + }, + 'join': { + 'next_batch': 'blah', + 'rooms': { + 'join': { + '!room': { + 'timeline': { + 'events': [], + }, + 'state': { + 'events': [], + }, + 'account_data': { + 'events': [], + }, + 'ephemeral': { + 'events': [], + }, + 'unread_notifications': {}, + 'summary': {}, + }, + }, + }, + }, + 'to_device': { + 'next_batch': 'blah', + 'to_device': { + 'events': [ + { + 'type': 'beep', + 'sender': '@example:localhost', + 'content': { + 'blah': 'blubb', + }, + }, + ], + }, + }, +}; + +void testUpdates(bool Function(SyncUpdate s) test, Map expected) { + for (final update in updates.entries) { + final sync = SyncUpdate.fromJson(update.value); + expect(test(sync), expected[update.key]); + } +} + +void main() { + group('Sync Filters', () { + Logs().level = Level.error; + test('room update', () { + final testFn = (SyncUpdate s) => s.hasRoomUpdate; + final expected = { + 'empty': false, + 'presence': false, + 'account_data': true, + 'invite': true, + 'leave': true, + 'join': true, + 'to_device': true, + }; + testUpdates(testFn, expected); + }); + + test('presence update', () { + final testFn = (SyncUpdate s) => s.hasPresenceUpdate; + final expected = { + 'empty': false, + 'presence': true, + 'account_data': false, + 'invite': false, + 'leave': false, + 'join': false, + 'to_device': false, + }; + testUpdates(testFn, expected); + }); + }); +} diff --git a/test/timeline_test.dart b/test/timeline_test.dart new file mode 100644 index 0000000..44fadf8 --- /dev/null +++ b/test/timeline_test.dart @@ -0,0 +1,620 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:matrix/matrix.dart'; + +import 'package:test/test.dart'; +import 'package:olm/olm.dart' as olm; +import 'fake_client.dart'; + +void main() { + group('Timeline', () { + Logs().level = Level.error; + final roomID = '!1234:example.com'; + final testTimeStamp = DateTime.now().millisecondsSinceEpoch; + var updateCount = 0; + final insertList = []; + final changeList = []; + final removeList = []; + var olmEnabled = true; + + late Client client; + late Room room; + late Timeline timeline; + test('create stuff', () async { + try { + await olm.init(); + olm.get_library_version(); + } catch (e) { + olmEnabled = false; + Logs().w('[LibOlm] Failed to load LibOlm', e); + } + Logs().i('[LibOlm] Enabled: $olmEnabled'); + client = await getClient(); + client.sendMessageTimeoutSeconds = 5; + + room = Room( + id: roomID, client: client, prev_batch: '1234', roomAccountData: {}); + timeline = Timeline( + room: room, + events: [], + onUpdate: () { + updateCount++; + }, + onInsert: insertList.add, + onChange: changeList.add, + onRemove: removeList.add, + ); + }); + + test('Create', () async { + await client.checkHomeserver('https://fakeserver.notexisting', + checkWellKnown: false); + + client.onEvent.add(EventUpdate( + type: EventUpdateType.timeline, + roomID: roomID, + content: { + 'type': 'm.room.message', + 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, + 'sender': '@alice:example.com', + 'status': EventStatus.synced.intValue, + 'event_id': '2', + 'origin_server_ts': testTimeStamp - 1000 + }, + )); + client.onEvent.add(EventUpdate( + type: EventUpdateType.timeline, + roomID: roomID, + content: { + 'type': 'm.room.message', + 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, + 'sender': '@alice:example.com', + 'status': EventStatus.synced.intValue, + 'event_id': '1', + 'origin_server_ts': testTimeStamp + }, + )); + + expect(timeline.sub != null, true); + + await Future.delayed(Duration(milliseconds: 50)); + + expect(updateCount, 2); + expect(insertList, [0, 0]); + expect(insertList.length, timeline.events.length); + expect(changeList, []); + expect(removeList, []); + expect(timeline.events.length, 2); + expect(timeline.events[0].eventId, '1'); + expect(timeline.events[0].sender.id, '@alice:example.com'); + expect(timeline.events[0].originServerTs.millisecondsSinceEpoch, + testTimeStamp); + expect(timeline.events[0].body, 'Testcase'); + expect( + timeline.events[0].originServerTs.millisecondsSinceEpoch > + timeline.events[1].originServerTs.millisecondsSinceEpoch, + true); + expect(timeline.events[0].receipts, []); + + room.roomAccountData['m.receipt'] = BasicRoomEvent.fromJson({ + 'type': 'm.receipt', + 'content': { + '@alice:example.com': { + 'event_id': '1', + 'ts': 1436451550453, + } + }, + 'room_id': roomID, + }); + + await Future.delayed(Duration(milliseconds: 50)); + + expect(timeline.events[0].receipts.length, 1); + expect(timeline.events[0].receipts[0].user.id, '@alice:example.com'); + + client.onEvent.add(EventUpdate( + type: EventUpdateType.timeline, + roomID: roomID, + content: { + 'type': 'm.room.redaction', + 'content': {'reason': 'spamming'}, + 'sender': '@alice:example.com', + 'redacts': '2', + 'event_id': '3', + 'origin_server_ts': testTimeStamp + 1000 + }, + )); + + await Future.delayed(Duration(milliseconds: 50)); + + expect(updateCount, 3); + expect(insertList, [0, 0, 0]); + expect(insertList.length, timeline.events.length); + expect(changeList, [2]); + expect(removeList, []); + expect(timeline.events.length, 3); + expect(timeline.events[2].redacted, true); + }); + + test('Send message', () async { + await room.sendTextEvent('test', txid: '1234'); + + await Future.delayed(Duration(milliseconds: 50)); + + expect(updateCount, 5); + expect(insertList, [0, 0, 0, 0]); + expect(insertList.length, timeline.events.length); + final eventId = timeline.events[0].eventId; + expect(eventId.startsWith('\$event'), true); + expect(timeline.events[0].status, EventStatus.sent); + + client.onEvent.add(EventUpdate( + type: EventUpdateType.timeline, + roomID: roomID, + content: { + 'type': 'm.room.message', + 'content': {'msgtype': 'm.text', 'body': 'test'}, + 'sender': '@alice:example.com', + 'status': EventStatus.synced.intValue, + 'event_id': eventId, + 'unsigned': {'transaction_id': '1234'}, + 'origin_server_ts': DateTime.now().millisecondsSinceEpoch + }, + )); + + await Future.delayed(Duration(milliseconds: 50)); + + expect(updateCount, 6); + expect(insertList, [0, 0, 0, 0]); + expect(insertList.length, timeline.events.length); + expect(timeline.events[0].eventId, eventId); + expect(timeline.events[0].status, EventStatus.synced); + }); + + test('Send message with error', () async { + client.onEvent.add(EventUpdate( + type: EventUpdateType.timeline, + roomID: roomID, + content: { + 'type': 'm.room.message', + 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, + 'sender': '@alice:example.com', + 'status': EventStatus.sending.intValue, + 'event_id': 'abc', + 'origin_server_ts': testTimeStamp + }, + )); + await Future.delayed(Duration(milliseconds: 50)); + + expect(updateCount, 7); + await room.sendTextEvent('test', txid: 'errortxid'); + await Future.delayed(Duration(milliseconds: 50)); + + expect(updateCount, 9); + await room.sendTextEvent('test', txid: 'errortxid2'); + await Future.delayed(Duration(milliseconds: 50)); + await room.sendTextEvent('test', txid: 'errortxid3'); + await Future.delayed(Duration(milliseconds: 50)); + + expect(updateCount, 13); + expect(insertList, [0, 0, 0, 0, 0, 0, 1, 2]); + expect(insertList.length, timeline.events.length); + expect(changeList, [2, 0, 0, 0, 1, 2]); + expect(removeList, []); + expect(timeline.events[0].status, EventStatus.error); + expect(timeline.events[1].status, EventStatus.error); + expect(timeline.events[2].status, EventStatus.error); + }); + + test('Remove message', () async { + await timeline.events[0].remove(); + + await Future.delayed(Duration(milliseconds: 50)); + + expect(updateCount, 14); + + expect(insertList, [0, 0, 0, 0, 0, 0, 1, 2]); + expect(changeList, [2, 0, 0, 0, 1, 2]); + expect(removeList, [0]); + expect(timeline.events.length, 7); + expect(timeline.events[0].status, EventStatus.error); + }); + + test('getEventById', () async { + var event = await timeline.getEventById('abc'); + expect(event?.content, {'msgtype': 'm.text', 'body': 'Testcase'}); + + event = await timeline.getEventById('not_found'); + expect(event, null); + + event = await timeline.getEventById('unencrypted_event'); + expect(event?.body, 'This is an example text message'); + + if (olmEnabled) { + event = await timeline.getEventById('encrypted_event'); + // the event is invalid but should have traces of attempting to decrypt + expect(event?.messageType, MessageTypes.BadEncrypted); + } + }); + + test('Resend message', () async { + timeline.events.clear(); + client.onEvent.add(EventUpdate( + type: EventUpdateType.timeline, + roomID: roomID, + content: { + 'type': 'm.room.message', + 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, + 'sender': '@alice:example.com', + 'status': EventStatus.error.intValue, + 'event_id': 'new-test-event', + 'origin_server_ts': testTimeStamp, + 'unsigned': {'transaction_id': 'newresend'}, + }, + )); + await Future.delayed(Duration(milliseconds: 50)); + expect(timeline.events[0].status, EventStatus.error); + await timeline.events[0].sendAgain(); + + await Future.delayed(Duration(milliseconds: 50)); + + expect(updateCount, 17); + + expect(insertList, [0, 0, 0, 0, 0, 0, 1, 2, 0]); + expect(changeList, [2, 0, 0, 0, 1, 2, 0, 0]); + expect(removeList, [0]); + expect(timeline.events.length, 1); + expect(timeline.events[0].status, EventStatus.sent); + }); + + test('Request history', () async { + timeline.events.clear(); + expect(timeline.canRequestHistory, true); + await room.requestHistory(); + + await Future.delayed(Duration(milliseconds: 50)); + + expect(updateCount, 20); + expect(insertList, [0, 0, 0, 0, 0, 0, 1, 2, 0, 0, 1, 2]); + expect(timeline.events.length, 3); + expect(timeline.events[0].eventId, '3143273582443PhrSn:example.org'); + expect(timeline.events[1].eventId, '2143273582443PhrSn:example.org'); + expect(timeline.events[2].eventId, '1143273582443PhrSn:example.org'); + expect(room.prev_batch, 't47409-4357353_219380_26003_2265'); + await timeline.events[2].redactEvent(reason: 'test', txid: '1234'); + }); + + test('Clear cache on limited timeline', () async { + client.onSync.add( + SyncUpdate( + nextBatch: '1234', + rooms: RoomsUpdate( + join: { + roomID: JoinedRoomUpdate( + timeline: TimelineUpdate( + limited: true, + prevBatch: 'blah', + ), + unreadNotifications: UnreadNotificationCounts( + highlightCount: 0, + notificationCount: 0, + ), + ), + }, + ), + ), + ); + await Future.delayed(Duration(milliseconds: 50)); + expect(timeline.events.isEmpty, true); + }); + + test('sort errors on top', () async { + timeline.events.clear(); + client.onEvent.add(EventUpdate( + type: EventUpdateType.timeline, + roomID: roomID, + content: { + 'type': 'm.room.message', + 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, + 'sender': '@alice:example.com', + 'status': EventStatus.error.intValue, + 'event_id': 'abc', + 'origin_server_ts': testTimeStamp + }, + )); + client.onEvent.add(EventUpdate( + type: EventUpdateType.timeline, + roomID: roomID, + content: { + 'type': 'm.room.message', + 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, + 'sender': '@alice:example.com', + 'status': EventStatus.synced.intValue, + 'event_id': 'def', + 'origin_server_ts': testTimeStamp + 5 + }, + )); + await Future.delayed(Duration(milliseconds: 50)); + expect(timeline.events[0].status, EventStatus.error); + expect(timeline.events[1].status, EventStatus.synced); + }); + + test('sending event to failed update', () async { + timeline.events.clear(); + client.onEvent.add(EventUpdate( + type: EventUpdateType.timeline, + roomID: roomID, + content: { + 'type': 'm.room.message', + 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, + 'sender': '@alice:example.com', + 'status': EventStatus.sending.intValue, + 'event_id': 'will-fail', + 'origin_server_ts': DateTime.now().millisecondsSinceEpoch, + }, + )); + await Future.delayed(Duration(milliseconds: 50)); + expect(timeline.events[0].status, EventStatus.sending); + expect(timeline.events.length, 1); + client.onEvent.add(EventUpdate( + type: EventUpdateType.timeline, + roomID: roomID, + content: { + 'type': 'm.room.message', + 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, + 'sender': '@alice:example.com', + 'status': EventStatus.error.intValue, + 'event_id': 'will-fail', + 'origin_server_ts': testTimeStamp + }, + )); + await Future.delayed(Duration(milliseconds: 50)); + expect(timeline.events[0].status, EventStatus.error); + expect(timeline.events.length, 1); + }); + test('setReadMarker', () async { + client.onEvent.add(EventUpdate( + type: EventUpdateType.timeline, + roomID: roomID, + content: { + 'type': 'm.room.message', + 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, + 'sender': '@alice:example.com', + 'status': EventStatus.synced.intValue, + 'event_id': 'will-work', + 'origin_server_ts': DateTime.now().millisecondsSinceEpoch, + }, + )); + await Future.delayed(Duration(milliseconds: 50)); + room.notificationCount = 1; + await timeline.setReadMarker(); + expect(room.notificationCount, 0); + }); + test('sending an event and the http request finishes first, 0 -> 1 -> 2', + () async { + timeline.events.clear(); + client.onEvent.add(EventUpdate( + type: EventUpdateType.timeline, + roomID: roomID, + content: { + 'type': 'm.room.message', + 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, + 'sender': '@alice:example.com', + 'status': EventStatus.sending.intValue, + 'event_id': 'transaction', + 'origin_server_ts': DateTime.now().millisecondsSinceEpoch, + }, + )); + await Future.delayed(Duration(milliseconds: 50)); + expect(timeline.events[0].status, EventStatus.sending); + expect(timeline.events.length, 1); + client.onEvent.add(EventUpdate( + type: EventUpdateType.timeline, + roomID: roomID, + content: { + 'type': 'm.room.message', + 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, + 'sender': '@alice:example.com', + 'status': EventStatus.sent.intValue, + 'event_id': '\$event', + 'origin_server_ts': testTimeStamp, + 'unsigned': {'transaction_id': 'transaction'} + }, + )); + await Future.delayed(Duration(milliseconds: 50)); + expect(timeline.events[0].status, EventStatus.sent); + expect(timeline.events.length, 1); + client.onEvent.add(EventUpdate( + type: EventUpdateType.timeline, + roomID: roomID, + content: { + 'type': 'm.room.message', + 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, + 'sender': '@alice:example.com', + 'status': EventStatus.synced.intValue, + 'event_id': '\$event', + 'origin_server_ts': testTimeStamp, + 'unsigned': {'transaction_id': 'transaction'} + }, + )); + await Future.delayed(Duration(milliseconds: 50)); + expect(timeline.events[0].status, EventStatus.synced); + expect(timeline.events.length, 1); + }); + test('sending an event where the sync reply arrives first, 0 -> 2 -> 1', + () async { + timeline.events.clear(); + client.onEvent.add(EventUpdate( + type: EventUpdateType.timeline, + roomID: roomID, + content: { + 'type': 'm.room.message', + 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, + 'sender': '@alice:example.com', + 'event_id': 'transaction', + 'origin_server_ts': DateTime.now().millisecondsSinceEpoch, + 'unsigned': { + messageSendingStatusKey: EventStatus.sending.intValue, + 'transaction_id': 'transaction', + }, + }, + )); + await Future.delayed(Duration(milliseconds: 50)); + expect(timeline.events[0].status, EventStatus.sending); + expect(timeline.events.length, 1); + client.onEvent.add(EventUpdate( + type: EventUpdateType.timeline, + roomID: roomID, + content: { + 'type': 'm.room.message', + 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, + 'sender': '@alice:example.com', + 'event_id': '\$event', + 'origin_server_ts': testTimeStamp, + 'unsigned': { + 'transaction_id': 'transaction', + messageSendingStatusKey: EventStatus.synced.intValue, + }, + }, + )); + await Future.delayed(Duration(milliseconds: 50)); + expect(timeline.events[0].status, EventStatus.synced); + expect(timeline.events.length, 1); + client.onEvent.add(EventUpdate( + type: EventUpdateType.timeline, + roomID: roomID, + content: { + 'type': 'm.room.message', + 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, + 'sender': '@alice:example.com', + 'event_id': '\$event', + 'origin_server_ts': testTimeStamp, + 'unsigned': { + 'transaction_id': 'transaction', + messageSendingStatusKey: EventStatus.sent.intValue, + }, + }, + )); + await Future.delayed(Duration(milliseconds: 50)); + expect(timeline.events[0].status, EventStatus.synced); + expect(timeline.events.length, 1); + }); + test('sending an event 0 -> -1 -> 2', () async { + timeline.events.clear(); + client.onEvent.add(EventUpdate( + type: EventUpdateType.timeline, + roomID: roomID, + content: { + 'type': 'm.room.message', + 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, + 'sender': '@alice:example.com', + 'status': EventStatus.sending.intValue, + 'event_id': 'transaction', + 'origin_server_ts': DateTime.now().millisecondsSinceEpoch, + }, + )); + await Future.delayed(Duration(milliseconds: 50)); + expect(timeline.events[0].status, EventStatus.sending); + expect(timeline.events.length, 1); + client.onEvent.add(EventUpdate( + type: EventUpdateType.timeline, + roomID: roomID, + content: { + 'type': 'm.room.message', + 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, + 'sender': '@alice:example.com', + 'status': EventStatus.error.intValue, + 'origin_server_ts': testTimeStamp, + 'unsigned': {'transaction_id': 'transaction'}, + }, + )); + await Future.delayed(Duration(milliseconds: 50)); + expect(timeline.events[0].status, EventStatus.error); + expect(timeline.events.length, 1); + client.onEvent.add(EventUpdate( + type: EventUpdateType.timeline, + roomID: roomID, + content: { + 'type': 'm.room.message', + 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, + 'sender': '@alice:example.com', + 'status': EventStatus.synced.intValue, + 'event_id': '\$event', + 'origin_server_ts': testTimeStamp, + 'unsigned': {'transaction_id': 'transaction'}, + }, + )); + await Future.delayed(Duration(milliseconds: 50)); + expect(timeline.events[0].status, EventStatus.synced); + expect(timeline.events.length, 1); + }); + test('sending an event 0 -> 2 -> -1', () async { + timeline.events.clear(); + client.onEvent.add(EventUpdate( + type: EventUpdateType.timeline, + roomID: roomID, + content: { + 'type': 'm.room.message', + 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, + 'sender': '@alice:example.com', + 'status': EventStatus.sending.intValue, + 'event_id': 'transaction', + 'origin_server_ts': DateTime.now().millisecondsSinceEpoch, + }, + )); + await Future.delayed(Duration(milliseconds: 50)); + expect(timeline.events[0].status, EventStatus.sending); + expect(timeline.events.length, 1); + client.onEvent.add(EventUpdate( + type: EventUpdateType.timeline, + roomID: roomID, + content: { + 'type': 'm.room.message', + 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, + 'sender': '@alice:example.com', + 'status': EventStatus.synced.intValue, + 'event_id': '\$event', + 'origin_server_ts': testTimeStamp, + 'unsigned': {'transaction_id': 'transaction'}, + }, + )); + await Future.delayed(Duration(milliseconds: 50)); + expect(timeline.events[0].status, EventStatus.synced); + expect(timeline.events.length, 1); + client.onEvent.add(EventUpdate( + type: EventUpdateType.timeline, + roomID: roomID, + content: { + 'type': 'm.room.message', + 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, + 'sender': '@alice:example.com', + 'status': EventStatus.error.intValue, + 'origin_server_ts': testTimeStamp, + 'unsigned': {'transaction_id': 'transaction'}, + }, + )); + await Future.delayed(Duration(milliseconds: 50)); + expect(timeline.events[0].status, EventStatus.synced); + expect(timeline.events.length, 1); + }); + test('logout', () async { + await client.logout(); + }); + }); +} diff --git a/test/uia_test.dart b/test/uia_test.dart new file mode 100644 index 0000000..f830093 --- /dev/null +++ b/test/uia_test.dart @@ -0,0 +1,107 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:async'; + +import 'package:matrix/matrix.dart'; + +import 'package:test/test.dart'; + +void main() { + group('UIA', () { + Logs().level = Level.error; + test('it should work', () async { + final completed = []; + var updated = 0; + var finished = false; + final request = UiaRequest( + request: (auth) async { + if (auth != null && + auth.session != null && + auth.session != 'foxies') { + throw MatrixException.fromJson({}); + } + if (auth != null && auth.type == 'stage1') { + if (completed.isEmpty) { + completed.add('stage1'); + } + } else if (auth != null && auth.type == 'stage2') { + if (completed.length == 1 && completed[0] == 'stage1') { + // okay, we are done! + return 'FOXIES ARE FLOOOOOFY!!!!!'; + } + } + final res = { + 'session': 'foxies', + 'completed': completed, + 'flows': [ + { + 'stages': ['stage1', 'stage2'], + } + ], + 'params': {}, + }; + throw MatrixException.fromJson(res); + }, + onUpdate: (state) { + if (state == UiaRequestState.done) { + finished = true; + } + updated++; + }, + ); + await Future.delayed(Duration(milliseconds: 50)); + expect(request.nextStages.contains('stage1'), true); + expect(request.nextStages.length, 1); + expect(updated, 1); + expect(finished, false); + await request.completeStage(AuthenticationData(type: 'stage1')); + expect(request.nextStages.contains('stage2'), true); + expect(request.nextStages.length, 1); + expect(updated, 3); + expect(finished, false); + final res = + await request.completeStage(AuthenticationData(type: 'stage2')); + expect(res, 'FOXIES ARE FLOOOOOFY!!!!!'); + expect(request.result, 'FOXIES ARE FLOOOOOFY!!!!!'); + expect(request.state, UiaRequestState.done); + expect(updated, 5); + expect(finished, true); + }); + test('it should throw errors', () async { + var updated = false; + var finished = false; + final request = UiaRequest( + request: (auth) async { + throw Exception('nope'); + }, + onUpdate: (state) { + if (state == UiaRequestState.fail) { + finished = true; + } + updated = true; + }, + ); + await Future.delayed(Duration(milliseconds: 50)); + expect(request.state, UiaRequestState.fail); + expect(updated, true); + expect(finished, true); + expect(request.error.toString(), Exception('nope').toString()); + }); + }); +} diff --git a/test/user_test.dart b/test/user_test.dart new file mode 100644 index 0000000..e33b8e2 --- /dev/null +++ b/test/user_test.dart @@ -0,0 +1,168 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:matrix/matrix.dart'; + +import 'package:test/test.dart'; + +import 'fake_matrix_api.dart'; + +void main() { + /// All Tests related to the Event + group('User', () { + Logs().level = Level.error; + final client = Client('testclient', httpClient: FakeMatrixApi()); + final room = Room(id: '!localpart:server.abc', client: client); + final user1 = User( + '@alice:example.com', + membership: 'join', + displayName: 'Alice M', + avatarUrl: 'mxc://bla', + room: room, + ); + final user2 = User( + '@bob:example.com', + membership: 'join', + displayName: 'Bob', + avatarUrl: 'mxc://bla', + room: room, + ); + room.setState(user1); + room.setState(user2); + setUp(() async { + await client.checkHomeserver('https://fakeserver.notexisting', + checkWellKnown: false); + await client.login(LoginType.mLoginPassword, + identifier: AuthenticationUserIdentifier(user: 'test'), + password: '1234'); + }); + tearDown(() async { + await client.logout(); + }); + test('create', () async { + expect(user1.powerLevel, 0); + expect(user1.stateKey, '@alice:example.com'); + expect(user1.id, '@alice:example.com'); + expect(user1.membership, Membership.join); + expect(user1.avatarUrl.toString(), 'mxc://bla'); + expect(user1.displayName, 'Alice M'); + }); + test('Create from json', () async { + final id = '@alice:server.abc'; + final membership = Membership.join; + final displayName = 'Alice'; + final avatarUrl = ''; + + final jsonObj = { + 'content': { + 'membership': 'join', + 'avatar_url': avatarUrl, + 'displayname': displayName + }, + 'type': 'm.room.member', + 'event_id': '143273582443PhrSn:example.org', + 'room_id': '!636q39766251:example.com', + 'sender': id, + 'origin_server_ts': 1432735824653, + 'unsigned': {'age': 1234}, + 'state_key': id + }; + + final user = Event.fromJson(jsonObj, room).asUser; + + expect(user.id, id); + expect(user.membership, membership); + expect(user.displayName, displayName); + expect(user.avatarUrl.toString(), avatarUrl); + expect(user.calcDisplayname(), displayName); + }); + + test('calcDisplayname', () async { + final user1 = User('@alice:example.com', room: room); + final user2 = User('@SuperAlice:example.com', room: room); + final user3 = User('@alice_mep:example.com', room: room); + expect(user1.calcDisplayname(), 'Alice'); + expect(user2.calcDisplayname(), 'SuperAlice'); + expect(user3.calcDisplayname(), 'Alice Mep'); + expect(user3.calcDisplayname(formatLocalpart: false), 'alice_mep'); + expect( + user3.calcDisplayname(mxidLocalPartFallback: false), 'Unknown user'); + }); + test('kick', () async { + await user1.kick(); + }); + test('ban', () async { + await user1.ban(); + }); + test('unban', () async { + await user1.unban(); + }); + test('setPower', () async { + await user1.setPower(50); + }); + test('startDirectChat', () async { + await user1.startDirectChat(waitForSync: false); + }); + test('getPresence', () async { + await client.handleSync(SyncUpdate.fromJson({ + 'next_batch': 'fake', + 'presence': { + 'events': [ + { + 'sender': '@alice:example.com', + 'type': 'm.presence', + 'content': {'presence': 'online'} + } + ] + } + })); + expect(user1.presence?.presence.presence, PresenceType.online); + }); + test('canBan', () async { + expect(user1.canBan, false); + }); + test('canKick', () async { + expect(user1.canKick, false); + }); + test('canChangePowerLevel', () async { + expect(user1.canChangePowerLevel, false); + }); + test('mention', () async { + expect(user1.mention, '@[Alice M]'); + expect(user2.mention, '@Bob'); + user1.content['displayname'] = '[Alice M]'; + expect(user1.mention, '@alice:example.com'); + user1.content['displayname'] = 'Alice:M'; + expect(user1.mention, '@alice:example.com'); + user1.content['displayname'] = 'Alice M'; + user2.content['displayname'] = 'Alice M'; + expect(user1.mention, '@[Alice M]#1745'); + user1.content['displayname'] = 'Bob'; + user2.content['displayname'] = 'Bob'; + expect(user1.mention, '@Bob#1745'); + user1.content['displayname'] = 'Alice M'; + }); + test('mentionFragments', () async { + expect(user1.mentionFragments, {'@[Alice M]', '@[Alice M]#1745'}); + expect(user2.mentionFragments, {'@Bob', '@Bob#1542'}); + }); + test('dispose client', () async { + await client.dispose(closeDatabase: true); + }); + }); +} diff --git a/test_driver/matrixsdk_test.dart b/test_driver/matrixsdk_test.dart new file mode 100644 index 0000000..2e3294b --- /dev/null +++ b/test_driver/matrixsdk_test.dart @@ -0,0 +1,339 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020, 2021 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +import 'package:matrix/matrix.dart'; +import '../test/fake_database.dart'; +import 'test_config.dart'; +import 'package:olm/olm.dart' as olm; + +void main() => test(); +const String testMessage = 'Hello world'; +const String testMessage2 = 'Hello moon'; +const String testMessage3 = 'Hello sun'; +const String testMessage4 = 'Hello star'; +const String testMessage5 = 'Hello earth'; +const String testMessage6 = 'Hello mars'; + +void test() async { + Client? testClientA, testClientB; + + try { + await olm.init(); + olm.Account(); + Logs().i('[LibOlm] Enabled'); + + Logs().i('++++ Login Alice at ++++'); + testClientA = Client('TestClientA', databaseBuilder: getDatabase); + await testClientA.checkHomeserver(TestUser.homeserver); + await testClientA.login(LoginType.mLoginPassword, + identifier: AuthenticationUserIdentifier(user: TestUser.username), + password: TestUser.password); + assert(testClientA.encryptionEnabled); + + Logs().i('++++ Login Bob ++++'); + testClientB = Client('TestClientB', databaseBuilder: getDatabase); + await testClientB.checkHomeserver(TestUser.homeserver); + await testClientB.login(LoginType.mLoginPassword, + identifier: AuthenticationUserIdentifier(user: TestUser.username2), + password: TestUser.password); + assert(testClientB.encryptionEnabled); + + Logs().i('++++ (Alice) Leave all rooms ++++'); + while (testClientA.rooms.isNotEmpty) { + final room = testClientA.rooms.first; + if (room.canonicalAlias.isNotEmpty) { + break; + } + try { + await room.leave(); + await room.forget(); + } catch (_) {} + } + + Logs().i('++++ (Bob) Leave all rooms ++++'); + for (var i = 0; i < 3; i++) { + if (testClientB.rooms.isNotEmpty) { + final room = testClientB.rooms.first; + try { + await room.leave(); + await room.forget(); + } catch (_) {} + } + } + + Logs().i('++++ Check if own olm device is verified by default ++++'); + assert(testClientA.userDeviceKeys.containsKey(TestUser.username)); + assert(testClientA.userDeviceKeys[TestUser.username]!.deviceKeys + .containsKey(testClientA.deviceID)); + assert(testClientA.userDeviceKeys[TestUser.username]! + .deviceKeys[testClientA.deviceID!]!.verified); + assert(!testClientA.userDeviceKeys[TestUser.username]! + .deviceKeys[testClientA.deviceID!]!.blocked); + assert(testClientB.userDeviceKeys.containsKey(TestUser.username2)); + assert(testClientB.userDeviceKeys[TestUser.username2]!.deviceKeys + .containsKey(testClientB.deviceID)); + assert(testClientB.userDeviceKeys[TestUser.username2]! + .deviceKeys[testClientB.deviceID!]!.verified); + assert(!testClientB.userDeviceKeys[TestUser.username2]! + .deviceKeys[testClientB.deviceID!]!.blocked); + + Logs().i('++++ (Alice) Create room and invite Bob ++++'); + await testClientA.createRoom(invite: [TestUser.username2]); + await Future.delayed(Duration(seconds: 1)); + final room = testClientA.rooms.first; + final roomId = room.id; + + Logs().i('++++ (Bob) Join room ++++'); + final inviteRoom = testClientB.getRoomById(roomId)!; + await inviteRoom.join(); + await Future.delayed(Duration(seconds: 1)); + assert(inviteRoom.membership == Membership.join); + + Logs().i('++++ (Alice) Enable encryption ++++'); + assert(room.encrypted == false); + await room.enableEncryption(); + await Future.delayed(Duration(seconds: 5)); + assert(room.encrypted == true); + assert( + room.client.encryption!.keyManager.getOutboundGroupSession(room.id) == + null); + + Logs().i('++++ (Alice) Check known olm devices ++++'); + assert(testClientA.userDeviceKeys.containsKey(TestUser.username2)); + assert(testClientA.userDeviceKeys[TestUser.username2]!.deviceKeys + .containsKey(testClientB.deviceID)); + assert(!testClientA.userDeviceKeys[TestUser.username2]! + .deviceKeys[testClientB.deviceID!]!.verified); + assert(!testClientA.userDeviceKeys[TestUser.username2]! + .deviceKeys[testClientB.deviceID!]!.blocked); + assert(testClientB.userDeviceKeys.containsKey(TestUser.username)); + assert(testClientB.userDeviceKeys[TestUser.username]!.deviceKeys + .containsKey(testClientA.deviceID)); + assert(!testClientB.userDeviceKeys[TestUser.username]! + .deviceKeys[testClientA.deviceID!]!.verified); + assert(!testClientB.userDeviceKeys[TestUser.username]! + .deviceKeys[testClientA.deviceID!]!.blocked); + await testClientA + .userDeviceKeys[TestUser.username2]!.deviceKeys[testClientB.deviceID!]! + .setVerified(true); + + Logs().i('++++ Check if own olm device is verified by default ++++'); + assert(testClientA.userDeviceKeys.containsKey(TestUser.username)); + assert(testClientA.userDeviceKeys[TestUser.username]!.deviceKeys + .containsKey(testClientA.deviceID)); + assert(testClientA.userDeviceKeys[TestUser.username]! + .deviceKeys[testClientA.deviceID!]!.verified); + assert(testClientB.userDeviceKeys.containsKey(TestUser.username2)); + assert(testClientB.userDeviceKeys[TestUser.username2]!.deviceKeys + .containsKey(testClientB.deviceID)); + assert(testClientB.userDeviceKeys[TestUser.username2]! + .deviceKeys[testClientB.deviceID!]!.verified); + + Logs().i("++++ (Alice) Send encrypted message: '$testMessage' ++++"); + await room.sendTextEvent(testMessage); + await Future.delayed(Duration(seconds: 5)); + assert( + room.client.encryption!.keyManager.getOutboundGroupSession(room.id) != + null); + var currentSessionIdA = room.client.encryption!.keyManager + .getOutboundGroupSession(room.id)! + .outboundGroupSession! + .session_id(); + /*assert(room.client.encryption.keyManager + .getInboundGroupSession(room.id, currentSessionIdA, '') != + null);*/ + assert(testClientA.encryption!.olmManager + .olmSessions[testClientB.identityKey]!.length == + 1); + assert(testClientB.encryption!.olmManager + .olmSessions[testClientA.identityKey]!.length == + 1); + assert(testClientA.encryption!.olmManager + .olmSessions[testClientB.identityKey]!.first.sessionId == + testClientB.encryption!.olmManager.olmSessions[testClientA.identityKey]! + .first.sessionId); + /*assert(inviteRoom.client.encryption.keyManager + .getInboundGroupSession(inviteRoom.id, currentSessionIdA, '') != + null);*/ + assert(room.lastEvent!.body == testMessage); + assert(inviteRoom.lastEvent!.body == testMessage); + Logs().i( + "++++ (Bob) Received decrypted message: '${inviteRoom.lastEvent!.body}' ++++"); + + Logs().i("++++ (Alice) Send again encrypted message: '$testMessage2' ++++"); + await room.sendTextEvent(testMessage2); + await Future.delayed(Duration(seconds: 5)); + assert(testClientA.encryption!.olmManager + .olmSessions[testClientB.identityKey]!.length == + 1); + assert(testClientB.encryption!.olmManager + .olmSessions[testClientA.identityKey]!.length == + 1); + assert(testClientA.encryption!.olmManager + .olmSessions[testClientB.identityKey]!.first.sessionId == + testClientB.encryption!.olmManager.olmSessions[testClientA.identityKey]! + .first.sessionId); + + assert(room.client.encryption!.keyManager + .getOutboundGroupSession(room.id)! + .outboundGroupSession! + .session_id() == + currentSessionIdA); + /*assert(room.client.encryption.keyManager + .getInboundGroupSession(room.id, currentSessionIdA, '') != + null);*/ + assert(room.lastEvent!.body == testMessage2); + assert(inviteRoom.lastEvent!.body == testMessage2); + Logs().i( + "++++ (Bob) Received decrypted message: '${inviteRoom.lastEvent!.body}' ++++"); + + Logs().i("++++ (Bob) Send again encrypted message: '$testMessage3' ++++"); + await inviteRoom.sendTextEvent(testMessage3); + await Future.delayed(Duration(seconds: 5)); + assert(testClientA.encryption!.olmManager + .olmSessions[testClientB.identityKey]!.length == + 1); + assert(testClientB.encryption!.olmManager + .olmSessions[testClientA.identityKey]!.length == + 1); + assert(room.client.encryption!.keyManager + .getOutboundGroupSession(room.id)! + .outboundGroupSession! + .session_id() == + currentSessionIdA); + final inviteRoomOutboundGroupSession = inviteRoom + .client.encryption!.keyManager + .getOutboundGroupSession(inviteRoom.id)!; + + assert(inviteRoomOutboundGroupSession.isValid); + /*assert(inviteRoom.client.encryption.keyManager.getInboundGroupSession( + inviteRoom.id, + inviteRoomOutboundGroupSession.outboundGroupSession.session_id(), + '') != + null); + assert(room.client.encryption.keyManager.getInboundGroupSession( + room.id, + inviteRoomOutboundGroupSession.outboundGroupSession.session_id(), + '') != + null);*/ + assert(inviteRoom.lastEvent!.body == testMessage3); + assert(room.lastEvent!.body == testMessage3); + Logs().i( + "++++ (Alice) Received decrypted message: '${room.lastEvent!.body}' ++++"); + + Logs().i('++++ Login Bob in another client ++++'); + final testClientC = Client('TestClientC', databaseBuilder: getDatabase); + await testClientC.checkHomeserver(TestUser.homeserver); + await testClientC.login(LoginType.mLoginPassword, + identifier: AuthenticationUserIdentifier(user: TestUser.username2), + password: TestUser.password); + await Future.delayed(Duration(seconds: 3)); + + Logs().i("++++ (Alice) Send again encrypted message: '$testMessage4' ++++"); + await room.sendTextEvent(testMessage4); + await Future.delayed(Duration(seconds: 5)); + assert(testClientA.encryption!.olmManager + .olmSessions[testClientB.identityKey]!.length == + 1); + assert(testClientB.encryption!.olmManager + .olmSessions[testClientA.identityKey]!.length == + 1); + assert(testClientA.encryption!.olmManager + .olmSessions[testClientB.identityKey]!.first.sessionId == + testClientB.encryption!.olmManager.olmSessions[testClientA.identityKey]! + .first.sessionId); + assert(testClientA.encryption!.olmManager + .olmSessions[testClientC.identityKey]!.length == + 1); + assert(testClientC.encryption!.olmManager + .olmSessions[testClientA.identityKey]!.length == + 1); + assert(testClientA.encryption!.olmManager + .olmSessions[testClientC.identityKey]!.first.sessionId == + testClientC.encryption!.olmManager.olmSessions[testClientA.identityKey]! + .first.sessionId); + assert(room.client.encryption!.keyManager + .getOutboundGroupSession(room.id)! + .outboundGroupSession! + .session_id() != + currentSessionIdA); + currentSessionIdA = room.client.encryption!.keyManager + .getOutboundGroupSession(room.id)! + .outboundGroupSession! + .session_id(); + /*assert(inviteRoom.client.encryption.keyManager + .getInboundGroupSession(inviteRoom.id, currentSessionIdA, '') != + null);*/ + assert(room.lastEvent!.body == testMessage4); + assert(inviteRoom.lastEvent!.body == testMessage4); + Logs().i( + "++++ (Bob) Received decrypted message: '${inviteRoom.lastEvent!.body}' ++++"); + + Logs().i('++++ Logout Bob another client ++++'); + await testClientC.dispose(closeDatabase: false); + await testClientC.logout(); + await Future.delayed(Duration(seconds: 5)); + + Logs().i("++++ (Alice) Send again encrypted message: '$testMessage6' ++++"); + await room.sendTextEvent(testMessage6); + await Future.delayed(Duration(seconds: 5)); + assert(testClientA.encryption!.olmManager + .olmSessions[testClientB.identityKey]!.length == + 1); + assert(testClientB.encryption!.olmManager + .olmSessions[testClientA.identityKey]!.length == + 1); + assert(testClientA.encryption!.olmManager + .olmSessions[testClientB.identityKey]!.first.sessionId == + testClientB.encryption!.olmManager.olmSessions[testClientA.identityKey]! + .first.sessionId); + assert(room.client.encryption!.keyManager + .getOutboundGroupSession(room.id)! + .outboundGroupSession! + .session_id() != + currentSessionIdA); + currentSessionIdA = room.client.encryption!.keyManager + .getOutboundGroupSession(room.id)! + .outboundGroupSession! + .session_id(); + /*assert(inviteRoom.client.encryption.keyManager + .getInboundGroupSession(inviteRoom.id, currentSessionIdA, '') != + null);*/ + assert(room.lastEvent!.body == testMessage6); + assert(inviteRoom.lastEvent!.body == testMessage6); + Logs().i( + "++++ (Bob) Received decrypted message: '${inviteRoom.lastEvent!.body}' ++++"); + + await room.leave(); + await room.forget(); + await inviteRoom.leave(); + await inviteRoom.forget(); + await Future.delayed(Duration(seconds: 1)); + } catch (e, s) { + Logs().e('Test failed', e, s); + rethrow; + } finally { + Logs().i('++++ Logout Alice and Bob ++++'); + if (testClientA?.isLogged() ?? false) await testClientA!.logoutAll(); + if (testClientA?.isLogged() ?? false) await testClientB!.logoutAll(); + await testClientA?.dispose(closeDatabase: false); + await testClientB?.dispose(closeDatabase: false); + testClientA = null; + testClientB = null; + } + return; +} diff --git a/test_driver/test_config.dart b/test_driver/test_config.dart new file mode 100644 index 0000000..8014255 --- /dev/null +++ b/test_driver/test_config.dart @@ -0,0 +1,6 @@ +class TestUser { + static const String homeserver = 'https://enter-your-server.here'; + static const String username = 'alice'; + static const String username2 = 'bob'; + static const String password = '1234'; +}