├── .travis.yml ├── docs ├── Bot-sdk.md └── Client-sdk.md ├── bot-impl ├── src │ └── main │ │ └── java │ │ ├── module-info.java │ │ └── io │ │ └── github │ │ └── ma1uta │ │ └── matrix │ │ ├── bot │ │ ├── AccessPolicy.java │ │ ├── ReceiptPolicy.java │ │ ├── LoopState.java │ │ ├── RunState.java │ │ ├── BotState.java │ │ ├── BotDao.java │ │ ├── PersistentService.java │ │ ├── command │ │ │ ├── Leave.java │ │ │ ├── Pong.java │ │ │ ├── OwnerCommand.java │ │ │ ├── Prefix.java │ │ │ ├── NewName.java │ │ │ ├── SetAccessPolicy.java │ │ │ └── DefaultCommand.java │ │ ├── Command.java │ │ └── AbstractStandaloneBotPool.java │ │ └── appservice │ │ ├── TransactionDao.java │ │ └── Transaction.java └── pom.xml ├── common-impl └── src │ └── main │ └── java │ ├── module-info.java │ └── io │ └── github │ └── ma1uta │ └── matrix │ └── impl │ ├── package-info.java │ ├── exception │ ├── package-info.java │ ├── MatrixException.java │ └── ExceptionHandler.java │ ├── RestClientBuilderConfigurer.java │ └── Deserializer.java ├── .gitignore ├── .github └── workflows │ └── maven.yml ├── client-impl ├── src │ ├── main │ │ └── java │ │ │ ├── module-info.java │ │ │ └── io │ │ │ └── github │ │ │ └── ma1uta │ │ │ └── matrix │ │ │ └── client │ │ │ ├── package-info.java │ │ │ ├── methods │ │ │ ├── package-info.java │ │ │ ├── async │ │ │ │ ├── package-info.java │ │ │ │ ├── VoipAsyncMethods.java │ │ │ │ ├── VersionAsyncMethods.java │ │ │ │ ├── CapabilityAsyncMethods.java │ │ │ │ ├── AdminAsyncMethods.java │ │ │ │ ├── OpenIdAsyncMethods.java │ │ │ │ ├── SendToDeviceAsyncMethods.java │ │ │ │ ├── SearchAsyncMethods.java │ │ │ │ ├── UserDirectoryAsyncMethods.java │ │ │ │ ├── EventContextAsyncMethods.java │ │ │ │ ├── ReportAsyncMethods.java │ │ │ │ ├── ReceiptAsyncMethods.java │ │ │ │ └── FilterAsyncMethods.java │ │ │ └── blocked │ │ │ │ ├── package-info.java │ │ │ │ ├── VoipMethods.java │ │ │ │ ├── VersionMethods.java │ │ │ │ ├── CapabilityMethods.java │ │ │ │ ├── AdminMethods.java │ │ │ │ ├── OpenIdMethods.java │ │ │ │ ├── SendToDeviceMethods.java │ │ │ │ ├── SearchMethods.java │ │ │ │ ├── UserDirectoryMethods.java │ │ │ │ ├── EventContextMethods.java │ │ │ │ ├── ReceiptMethods.java │ │ │ │ ├── FilterMethods.java │ │ │ │ ├── ReportMethods.java │ │ │ │ ├── SyncMethods.java │ │ │ │ └── TypingMethods.java │ │ │ ├── rest │ │ │ ├── package-info.java │ │ │ ├── async │ │ │ │ ├── package-info.java │ │ │ │ ├── VoipApi.java │ │ │ │ ├── VersionApi.java │ │ │ │ ├── AdminApi.java │ │ │ │ ├── CapabilitiesApi.java │ │ │ │ ├── EventContextApi.java │ │ │ │ ├── ServerDiscoveryApi.java │ │ │ │ ├── ReportApi.java │ │ │ │ ├── SearchApi.java │ │ │ │ └── UserDirectoryApi.java │ │ │ └── blocked │ │ │ │ ├── package-info.java │ │ │ │ ├── VoipApi.java │ │ │ │ ├── VersionApi.java │ │ │ │ ├── AdminApi.java │ │ │ │ ├── CapabilitiesApi.java │ │ │ │ ├── EventContextApi.java │ │ │ │ ├── ServerDiscoveryApi.java │ │ │ │ ├── ReportApi.java │ │ │ │ ├── SearchApi.java │ │ │ │ ├── UserDirectoryApi.java │ │ │ │ └── OpenIdApi.java │ │ │ ├── sync │ │ │ └── package-info.java │ │ │ ├── filter │ │ │ ├── package-info.java │ │ │ ├── ContentTypeFilter.java │ │ │ ├── UserIdClientFilter.java │ │ │ └── AuthorizationFilter.java │ │ │ ├── ResolvedHomeserver.java │ │ │ ├── ClientHomeServerResolver.java │ │ │ ├── FederationHomeServerResolver.java │ │ │ ├── AbstractClientBuilder.java │ │ │ └── ContentUriModel.java │ └── test │ │ └── java │ │ └── io │ │ └── github │ │ └── ma1uta │ │ └── matrix │ │ └── client │ │ ├── ContentUriModelTest.java │ │ ├── MockServer.java │ │ ├── VersionAsyncMethodsTest.java │ │ ├── HomeServerResolverTest.java │ │ └── ContentAsyncMethodsTest.java └── Readme.md ├── examples ├── README.md ├── src │ └── main │ │ └── java │ │ └── io │ │ └── github │ │ └── ma1uta │ │ └── matrix │ │ └── example │ │ └── SimpleExample.java └── pom.xml ├── Readme.md ├── jsonb-support └── src │ └── main │ ├── resources │ └── META-INF │ │ └── services │ │ ├── io.github.ma1uta.matrix.impl.Deserializer │ │ └── io.github.ma1uta.matrix.impl.RestClientBuilderConfigurer │ └── java │ ├── io │ └── github │ │ └── ma1uta │ │ └── matrix │ │ └── support │ │ └── jsonb │ │ ├── JsonbRestClientBuilderConfigurer.java │ │ ├── DefaultJsonbProvider.java │ │ ├── JsonbProvider.java │ │ ├── EventDeserializer.java │ │ ├── RoomMessageContentDeserializer.java │ │ └── JsonbDeserializer.java │ └── module-info.java └── jackson-support └── src └── main ├── resources └── META-INF │ └── services │ ├── io.github.ma1uta.matrix.impl.Deserializer │ └── io.github.ma1uta.matrix.impl.RestClientBuilderConfigurer └── java ├── io └── github │ └── ma1uta │ └── matrix │ └── support │ └── jackson │ ├── JacksonSupportProvider.java │ ├── JacksonRestClientBuilderConfigurer.java │ ├── JacksonContextResolver.java │ ├── ObjectMapperProvider.java │ ├── JacksonDeserializer.java │ ├── workaround │ └── ReceiptTsDeserialized4898.java │ ├── DefaultObjectMapperProvider.java │ └── RoomEncryptedContentDeserializer.java └── module-info.java /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: 3 | - openjdk8 4 | - openjdk11 # fail jsonb-support due to https://github.com/mapstruct/mapstruct-examples/issues/69 5 | - openjdk13 6 | -------------------------------------------------------------------------------- /docs/Bot-sdk.md: -------------------------------------------------------------------------------- 1 | # Bot sdk 2 | 3 | Common classes to write custom bots and appservice. 4 | 5 | ## Not documented yet. This code under heavy development and isn't ready for production. 6 | -------------------------------------------------------------------------------- /bot-impl/src/main/java/module-info.java: -------------------------------------------------------------------------------- 1 | module matrix.bot { 2 | exports io.github.ma1uta.matrix.appservice; 3 | exports io.github.ma1uta.matrix.bot; 4 | exports io.github.ma1uta.matrix.bot.command; 5 | 6 | requires transitive jakarta.persistence; 7 | requires matrix.client.impl; 8 | } 9 | -------------------------------------------------------------------------------- /common-impl/src/main/java/module-info.java: -------------------------------------------------------------------------------- 1 | module matrix.common.impl { 2 | exports io.github.ma1uta.matrix.impl; 3 | exports io.github.ma1uta.matrix.impl.exception; 4 | 5 | requires transitive matrix.common.api; 6 | requires transitive org.slf4j; 7 | requires transitive java.ws.rs; 8 | requires transitive microprofile.rest.client.api; 9 | } 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # Package Files # 8 | *.war 9 | *.ear 10 | *.zip 11 | *.tar.gz 12 | *.rar 13 | 14 | **/.project 15 | **/.idea/ 16 | **/.classpath 17 | **/.checkstyle 18 | **/.settings/ 19 | **/*.iml 20 | **/target/ 21 | **/build/ 22 | **/.gradle/ 23 | **/checkstyle-cachefile 24 | 25 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 26 | hs_err_pid* 27 | -------------------------------------------------------------------------------- /.github/workflows/maven.yml: -------------------------------------------------------------------------------- 1 | name: Java CI 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | 8 | runs-on: ubuntu-latest 9 | strategy: 10 | matrix: 11 | java: [ 8, 11, 13] 12 | name: Java ${{ matrix.java }} build 13 | steps: 14 | - uses: actions/checkout@v2 15 | - name: Set up JDK 16 | uses: actions/setup-java@v1 17 | with: 18 | java-version: ${{ matrix.java }} 19 | - name: Build with Maven 20 | run: mvn -B package --file pom.xml 21 | -------------------------------------------------------------------------------- /client-impl/src/main/java/module-info.java: -------------------------------------------------------------------------------- 1 | module matrix.client.impl { 2 | uses io.github.ma1uta.matrix.impl.Deserializer; 3 | uses io.github.ma1uta.matrix.impl.RestClientBuilderConfigurer; 4 | 5 | exports io.github.ma1uta.matrix.client; 6 | exports io.github.ma1uta.matrix.client.methods.blocked; 7 | exports io.github.ma1uta.matrix.client.methods.async; 8 | exports io.github.ma1uta.matrix.client.sync; 9 | 10 | requires transitive matrix.client.api; 11 | requires transitive matrix.common.impl; 12 | requires transitive java.naming; 13 | requires transitive microprofile.rest.client.api; 14 | } 15 | -------------------------------------------------------------------------------- /examples/README.md: -------------------------------------------------------------------------------- 1 | # Matrix client examples 2 | 3 | * https://github.com/ma1uta/jmsdk/blob/master/examples/src/main/java/io/github/ma1uta/matrix/example/SimpleExample.java - example with login 4 | * https://github.com/ma1uta/jmsdk/blob/master/examples/src/main/java/io/github/ma1uta/matrix/example/RegistrationExample.java - sync and async registration example 5 | * https://github.com/ma1uta/jmsdk/blob/master/examples/src/main/java/io/github/ma1uta/matrix/example/AvatarUrlExample.java - example with API without authorization 6 | * https://github.com/ma1uta/jmsdk/blob/master/examples/src/main/java/io/github/ma1uta/matrix/example/SyncExample.java - SyncLoop example to get incoming events in the separate thread. 7 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # Matrix Java sdk. 2 | 3 | A set of the SDK to write client and server implementation of the Matrix protocol. 4 | Based on [Jeon API](https://github.com/ma1uta/jeon). 5 | 6 | ## Modules 7 | 8 | ### [client-sdk](https://github.com/ma1uta/jmsdk/blob/master/client-impl/Readme.md) 9 | 10 | Matrix client with the full support of the Client-Server API. 11 | 12 | ### [bot-sdk](https://github.com/ma1uta/jmsdk/blob/master/docs/Bot-sdk.md) 13 | 14 | Core classes to write bots and appservice. (WIP) 15 | 16 | ### common-backend 17 | 18 | Common classes used in all sdk and implementations. (WIP) 19 | 20 | ### homeserver-backend 21 | 22 | Core classes to write custom homeserver. (Empty) 23 | 24 | ### identity-backend 25 | 26 | Core classes to write custom identity server. (WIP) 27 | -------------------------------------------------------------------------------- /jsonb-support/src/main/resources/META-INF/services/io.github.ma1uta.matrix.impl.Deserializer: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright Anatoliy Sablin tolya@sablin.xyz 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | io.github.ma1uta.matrix.support.jsonb.JsonbDeserializer 18 | -------------------------------------------------------------------------------- /jackson-support/src/main/resources/META-INF/services/io.github.ma1uta.matrix.impl.Deserializer: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright Anatoliy Sablin tolya@sablin.xyz 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | io.github.ma1uta.matrix.support.jackson.JacksonDeserializer 18 | -------------------------------------------------------------------------------- /jsonb-support/src/main/resources/META-INF/services/io.github.ma1uta.matrix.impl.RestClientBuilderConfigurer: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright Anatoliy Sablin tolya@sablin.xyz 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | io.github.ma1uta.matrix.support.jsonb.JsonbRestClientBuilderConfigurer 18 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /** 18 | * Main client classes. 19 | */ 20 | 21 | package io.github.ma1uta.matrix.client; 22 | -------------------------------------------------------------------------------- /jackson-support/src/main/resources/META-INF/services/io.github.ma1uta.matrix.impl.RestClientBuilderConfigurer: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright Anatoliy Sablin tolya@sablin.xyz 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | io.github.ma1uta.matrix.support.jackson.JacksonRestClientBuilderConfigurer 18 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/methods/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /** 18 | * Client methods. 19 | */ 20 | 21 | package io.github.ma1uta.matrix.client.methods; 22 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/rest/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /** 18 | * Client API interfaces. 19 | */ 20 | 21 | package io.github.ma1uta.matrix.client.rest; 22 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/sync/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /** 18 | * Client sync classes. 19 | */ 20 | 21 | package io.github.ma1uta.matrix.client.sync; 22 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/filter/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /** 18 | * REST Client filters. 19 | */ 20 | 21 | package io.github.ma1uta.matrix.client.filter; 22 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/methods/async/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /** 18 | * Async methods. 19 | */ 20 | 21 | package io.github.ma1uta.matrix.client.methods.async; 22 | -------------------------------------------------------------------------------- /common-impl/src/main/java/io/github/ma1uta/matrix/impl/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /** 18 | * Reference implementation and common classes. 19 | */ 20 | 21 | package io.github.ma1uta.matrix.impl; 22 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/methods/blocked/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /** 18 | * Blocked methods. 19 | */ 20 | 21 | package io.github.ma1uta.matrix.client.methods.blocked; 22 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/rest/async/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /** 18 | * Async Client API interfaces. 19 | */ 20 | 21 | package io.github.ma1uta.matrix.client.rest.async; 22 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/rest/blocked/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /** 18 | * Blocked Client API interfaces. 19 | */ 20 | 21 | package io.github.ma1uta.matrix.client.rest.blocked; 22 | -------------------------------------------------------------------------------- /common-impl/src/main/java/io/github/ma1uta/matrix/impl/exception/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /** 18 | * General exceptions and exception handler. 19 | */ 20 | 21 | package io.github.ma1uta.matrix.impl.exception; 22 | -------------------------------------------------------------------------------- /bot-impl/src/main/java/io/github/ma1uta/matrix/bot/AccessPolicy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.bot; 18 | 19 | /** 20 | * Config who can invoke commands. 21 | */ 22 | public enum AccessPolicy { 23 | /** 24 | * Only owner can invoke commands. 25 | */ 26 | OWNER, 27 | /** 28 | * Anybody can invoke commands. 29 | */ 30 | ALL 31 | } 32 | -------------------------------------------------------------------------------- /bot-impl/src/main/java/io/github/ma1uta/matrix/bot/ReceiptPolicy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.bot; 18 | 19 | /** 20 | * Specify when bot should send a receipt. 21 | */ 22 | public enum ReceiptPolicy { 23 | 24 | /** 25 | * Send receipt when read event. 26 | */ 27 | READ, 28 | /** 29 | * Send receipt only when executed command. 30 | */ 31 | EXECUTED 32 | } 33 | -------------------------------------------------------------------------------- /bot-impl/src/main/java/io/github/ma1uta/matrix/bot/LoopState.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.bot; 18 | 19 | /** 20 | * Loop state. 21 | */ 22 | public enum LoopState { 23 | /** 24 | * Stop running and exit from loop. 25 | */ 26 | EXIT, 27 | /** 28 | * Running. 29 | */ 30 | RUN, 31 | /** 32 | * Proceed to next state. 33 | */ 34 | NEXT_STATE 35 | } 36 | -------------------------------------------------------------------------------- /bot-impl/src/main/java/io/github/ma1uta/matrix/bot/RunState.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.bot; 18 | 19 | /** 20 | * Run state. 21 | */ 22 | public enum RunState { 23 | /** 24 | * The bot is run in the separate thread as a normal user client. 25 | */ 26 | STANDALONE, 27 | /** 28 | * The bot isn't run in the separate thread, only response to PUT message. 29 | */ 30 | APPLICATION_SERVICE 31 | } 32 | -------------------------------------------------------------------------------- /bot-impl/src/main/java/io/github/ma1uta/matrix/bot/BotState.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.bot; 18 | 19 | /** 20 | * Bot's states. 21 | */ 22 | public enum BotState { 23 | /** 24 | * New. 25 | */ 26 | NEW, 27 | /** 28 | * Registered. 29 | */ 30 | REGISTERED, 31 | /** 32 | * Joined to room. 33 | */ 34 | JOINED, 35 | /** 36 | * Deleted. 37 | */ 38 | DELETED 39 | } 40 | -------------------------------------------------------------------------------- /common-impl/src/main/java/io/github/ma1uta/matrix/impl/RestClientBuilderConfigurer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.impl; 18 | 19 | import org.eclipse.microprofile.rest.client.RestClientBuilder; 20 | 21 | /** 22 | * Configurer for {@link org.eclipse.microprofile.rest.client.RestClientBuilder}. 23 | */ 24 | public interface RestClientBuilderConfigurer { 25 | 26 | /** 27 | * Configure the builder. 28 | *
29 | * Configurer modifies the argument. 30 | * 31 | * @param builder builder. 32 | */ 33 | void configure(RestClientBuilder builder); 34 | } 35 | -------------------------------------------------------------------------------- /jsonb-support/src/main/java/io/github/ma1uta/matrix/support/jsonb/JsonbRestClientBuilderConfigurer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.support.jsonb; 18 | 19 | import io.github.ma1uta.matrix.impl.RestClientBuilderConfigurer; 20 | import org.eclipse.microprofile.rest.client.RestClientBuilder; 21 | 22 | 23 | /** 24 | * Provides Jsonb's object mapper for REST clients. 25 | */ 26 | public class JsonbRestClientBuilderConfigurer implements RestClientBuilderConfigurer { 27 | 28 | @Override 29 | public void configure(RestClientBuilder builder) { 30 | builder.register(new JsonbSupportProvider()); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /jackson-support/src/main/java/io/github/ma1uta/matrix/support/jackson/JacksonSupportProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.support.jackson; 18 | 19 | import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider; 20 | 21 | import javax.ws.rs.Consumes; 22 | import javax.ws.rs.Produces; 23 | import javax.ws.rs.ext.Provider; 24 | 25 | 26 | /** 27 | * JAX-RS provider for serialization/deserialization. 28 | */ 29 | @Provider 30 | @Consumes( {"application/json", "application/*+json", "text/json"}) 31 | @Produces( {"application/json", "application/*+json", "text/json"}) 32 | public class JacksonSupportProvider extends JacksonJaxbJsonProvider { 33 | } 34 | -------------------------------------------------------------------------------- /jsonb-support/src/main/java/io/github/ma1uta/matrix/support/jsonb/DefaultJsonbProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.support.jsonb; 18 | 19 | import javax.json.bind.Jsonb; 20 | import javax.json.bind.JsonbBuilder; 21 | import javax.json.bind.JsonbConfig; 22 | 23 | /** 24 | * Default implementation. 25 | */ 26 | public class DefaultJsonbProvider implements JsonbProvider { 27 | 28 | private final Jsonb jsonb = JsonbBuilder 29 | .create(new JsonbConfig().withNullValues(false).withDeserializers(new EventDeserializer(), new RoomMessageContentDeserializer())); 30 | 31 | @Override 32 | public Jsonb get() { 33 | return jsonb; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /bot-impl/src/main/java/io/github/ma1uta/matrix/appservice/TransactionDao.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.appservice; 18 | 19 | /** 20 | * DAO for transactions. 21 | * 22 | * @param actual class of the transaction. 23 | */ 24 | public interface TransactionDao { 25 | 26 | /** 27 | * Save new transaction. 28 | * 29 | * @param transaction transaction. 30 | */ 31 | void save(T transaction); 32 | 33 | /** 34 | * Check the specified transaction exists. 35 | * 36 | * @param txnId transaction id. 37 | * @return {@code true} if exist else {@code false}. 38 | */ 39 | boolean exist(String txnId); 40 | } 41 | -------------------------------------------------------------------------------- /jsonb-support/src/main/java/io/github/ma1uta/matrix/support/jsonb/JsonbProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.support.jsonb; 18 | 19 | import java.util.Iterator; 20 | import java.util.ServiceLoader; 21 | import java.util.function.Supplier; 22 | import javax.json.bind.Jsonb; 23 | 24 | /** 25 | * Supplier for ObjectMapper. 26 | */ 27 | public interface JsonbProvider extends Supplier { 28 | 29 | /** 30 | * Get ObjectMapperProvides. Search first a customer provider. 31 | * 32 | * @return {@link JsonbProvider}. 33 | */ 34 | static JsonbProvider getInstance() { 35 | Iterator iterator = ServiceLoader.load(JsonbProvider.class).iterator(); 36 | return iterator.hasNext() ? iterator.next() : new DefaultJsonbProvider(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /client-impl/src/test/java/io/github/ma1uta/matrix/client/ContentUriModelTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client; 18 | 19 | import static org.junit.jupiter.api.Assertions.*; 20 | 21 | import org.junit.jupiter.api.Test; 22 | 23 | class ContentUriModelTest { 24 | 25 | @Test 26 | public void parseTest() { 27 | ContentUriModel uriModel = ContentUriModel.valueOf("mxc://ru-matrix.org/123"); 28 | 29 | assertNotNull(uriModel); 30 | assertEquals("ru-matrix.org", uriModel.getServer()); 31 | assertEquals("123", uriModel.getMediaId()); 32 | } 33 | 34 | @Test 35 | public void toStringTest() { 36 | ContentUriModel model = new ContentUriModel("ru-matrix.org", "123"); 37 | assertEquals("mxc://ru-matrix.org/123", model.toString()); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /jsonb-support/src/main/java/io/github/ma1uta/matrix/support/jsonb/EventDeserializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.support.jsonb; 18 | 19 | import static io.github.ma1uta.matrix.support.jsonb.mapper.EventMapper.INSTANCE; 20 | 21 | import io.github.ma1uta.matrix.event.Event; 22 | 23 | import java.lang.reflect.Type; 24 | import javax.json.bind.serializer.DeserializationContext; 25 | import javax.json.bind.serializer.JsonbDeserializer; 26 | import javax.json.stream.JsonParser; 27 | 28 | /** 29 | * The Deserializer to the Event. 30 | */ 31 | public class EventDeserializer implements JsonbDeserializer { 32 | 33 | @Override 34 | public Event deserialize(JsonParser parser, DeserializationContext ctx, Type rtType) { 35 | return INSTANCE.deserialize(parser.getObject()); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /jackson-support/src/main/java/io/github/ma1uta/matrix/support/jackson/JacksonRestClientBuilderConfigurer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.support.jackson; 18 | 19 | import io.github.ma1uta.matrix.impl.RestClientBuilderConfigurer; 20 | import org.eclipse.microprofile.rest.client.RestClientBuilder; 21 | 22 | /** 23 | * Provides Jackson's object mapper for REST clients. 24 | */ 25 | public class JacksonRestClientBuilderConfigurer implements RestClientBuilderConfigurer { 26 | 27 | private static final int JSON_PROVIDER_PRIORITY = 100; 28 | 29 | @Override 30 | public void configure(RestClientBuilder builder) { 31 | JacksonSupportProvider provider = new JacksonSupportProvider(); 32 | provider.setMapper(ObjectMapperProvider.getInstance().get()); 33 | 34 | builder.register(provider, JSON_PROVIDER_PRIORITY); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /jackson-support/src/main/java/io/github/ma1uta/matrix/support/jackson/JacksonContextResolver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.support.jackson; 18 | 19 | import com.fasterxml.jackson.databind.ObjectMapper; 20 | 21 | import javax.ws.rs.Consumes; 22 | import javax.ws.rs.Produces; 23 | import javax.ws.rs.core.MediaType; 24 | import javax.ws.rs.ext.ContextResolver; 25 | import javax.ws.rs.ext.Provider; 26 | 27 | /** 28 | * Provides Jackson ObjectMapper with custom deserializers. 29 | */ 30 | @Provider 31 | @Produces(MediaType.APPLICATION_JSON) 32 | @Consumes(MediaType.APPLICATION_JSON) 33 | public class JacksonContextResolver implements ContextResolver { 34 | 35 | private final ObjectMapper mapper = ObjectMapperProvider.getInstance().get(); 36 | 37 | @Override 38 | public ObjectMapper getContext(Class type) { 39 | return mapper; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /jackson-support/src/main/java/io/github/ma1uta/matrix/support/jackson/ObjectMapperProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.support.jackson; 18 | 19 | import com.fasterxml.jackson.databind.ObjectMapper; 20 | 21 | import java.util.Iterator; 22 | import java.util.ServiceLoader; 23 | import java.util.function.Supplier; 24 | 25 | /** 26 | * Supplier for ObjectMapper. 27 | */ 28 | public interface ObjectMapperProvider extends Supplier { 29 | 30 | /** 31 | * Get ObjectMapperProvides. Search first a customer provider. 32 | * 33 | * @return {@link ObjectMapperProvider}. 34 | */ 35 | static ObjectMapperProvider getInstance() { 36 | Iterator iterator = ServiceLoader.load(ObjectMapperProvider.class).iterator(); 37 | return iterator.hasNext() ? iterator.next() : new DefaultObjectMapperProvider(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /jsonb-support/src/main/java/io/github/ma1uta/matrix/support/jsonb/RoomMessageContentDeserializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.support.jsonb; 18 | 19 | import static io.github.ma1uta.matrix.support.jsonb.mapper.EventMapper.INSTANCE; 20 | 21 | import io.github.ma1uta.matrix.event.content.RoomMessageContent; 22 | 23 | import java.lang.reflect.Type; 24 | import javax.json.bind.serializer.DeserializationContext; 25 | import javax.json.bind.serializer.JsonbDeserializer; 26 | import javax.json.stream.JsonParser; 27 | 28 | /** 29 | * The Deserializer to the Event. 30 | */ 31 | public class RoomMessageContentDeserializer implements JsonbDeserializer { 32 | 33 | @Override 34 | public RoomMessageContent deserialize(JsonParser parser, DeserializationContext ctx, Type rtType) { 35 | return INSTANCE.roomMessageContent(parser.getObject()); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/methods/blocked/VoipMethods.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.methods.blocked; 18 | 19 | import io.github.ma1uta.matrix.client.model.voip.VoipResponse; 20 | import io.github.ma1uta.matrix.client.rest.blocked.VoipApi; 21 | import org.eclipse.microprofile.rest.client.RestClientBuilder; 22 | 23 | /** 24 | * Voip methods. 25 | */ 26 | public class VoipMethods { 27 | 28 | private final VoipApi voipApi; 29 | 30 | public VoipMethods(RestClientBuilder restClientBuilder) { 31 | voipApi = restClientBuilder.build(VoipApi.class); 32 | } 33 | 34 | /** 35 | * This API provides credentials for the client to use when initiating calls. 36 | * 37 | * @return The TURN server credentials. 38 | */ 39 | public VoipResponse turnServers() { 40 | return voipApi.turnServer(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/methods/blocked/VersionMethods.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.methods.blocked; 18 | 19 | import io.github.ma1uta.matrix.client.model.version.VersionsResponse; 20 | import io.github.ma1uta.matrix.client.rest.blocked.VersionApi; 21 | import org.eclipse.microprofile.rest.client.RestClientBuilder; 22 | 23 | /** 24 | * Version methods. 25 | */ 26 | public class VersionMethods { 27 | 28 | private final VersionApi versionApi; 29 | 30 | public VersionMethods(RestClientBuilder restClientBuilder) { 31 | this.versionApi = restClientBuilder.build(VersionApi.class); 32 | } 33 | 34 | /** 35 | * Gets the versions of the specification supported by the server. 36 | * 37 | * @return The versions supported by the server. 38 | */ 39 | public VersionsResponse versions() { 40 | return versionApi.versions(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/filter/ContentTypeFilter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.filter; 18 | 19 | import static javax.ws.rs.core.HttpHeaders.CONTENT_TYPE; 20 | 21 | import java.io.IOException; 22 | import javax.ws.rs.client.ClientRequestContext; 23 | import javax.ws.rs.client.ClientRequestFilter; 24 | import javax.ws.rs.ext.Provider; 25 | 26 | /** 27 | * Filter to specify custom headers. 28 | */ 29 | @Provider 30 | public class ContentTypeFilter implements ClientRequestFilter { 31 | 32 | @Override 33 | public void filter(ClientRequestContext requestContext) throws IOException { 34 | String contentType = requestContext.getHeaderString("X-Content-Type"); 35 | if (contentType != null) { 36 | requestContext.getHeaders().putSingle(CONTENT_TYPE, contentType); 37 | requestContext.getHeaders().remove("X-Content-Type"); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/rest/blocked/VoipApi.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.rest.blocked; 18 | 19 | import io.github.ma1uta.matrix.client.model.voip.VoipResponse; 20 | 21 | import javax.ws.rs.GET; 22 | import javax.ws.rs.Path; 23 | import javax.ws.rs.Produces; 24 | import javax.ws.rs.core.MediaType; 25 | 26 | /** 27 | * The homeserver MAY provide a TURN server which clients can use to contact the remote party. The following HTTP API endpoints will 28 | * be used by clients in order to get information about the TURN server. 29 | */ 30 | @Path("/_matrix/client/r0/voip") 31 | @Produces(MediaType.APPLICATION_JSON) 32 | public interface VoipApi { 33 | 34 | /** 35 | * This API provides credentials for the client to use when initiating calls. 36 | * 37 | * @return {@link VoipResponse}. 38 | */ 39 | @GET 40 | @Path("/turnServer") 41 | VoipResponse turnServer(); 42 | } 43 | -------------------------------------------------------------------------------- /bot-impl/src/main/java/io/github/ma1uta/matrix/bot/BotDao.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.bot; 18 | 19 | import java.util.List; 20 | 21 | /** 22 | * DAO of the bot configuration. 23 | * 24 | * @param class of the configuration. 25 | */ 26 | public interface BotDao { 27 | /** 28 | * Find all matrix bots. 29 | * 30 | * @return matrix bots. 31 | */ 32 | List findAll(); 33 | 34 | /** 35 | * Check that specified user exists. 36 | * 37 | * @param userId mxid. 38 | * @return {@code true} if user exists or {@code false}. 39 | */ 40 | boolean user(String userId); 41 | 42 | /** 43 | * Save new bot's data. 44 | * 45 | * @param data bot's data. 46 | * @return saved entity. 47 | */ 48 | C save(C data); 49 | 50 | /** 51 | * Delete bot's data. 52 | * 53 | * @param data data to delete. 54 | */ 55 | void delete(C data); 56 | } 57 | -------------------------------------------------------------------------------- /jsonb-support/src/main/java/module-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import io.github.ma1uta.matrix.impl.Deserializer; 18 | import io.github.ma1uta.matrix.impl.RestClientBuilderConfigurer; 19 | import io.github.ma1uta.matrix.support.jsonb.JsonbDeserializer; 20 | import io.github.ma1uta.matrix.support.jsonb.JsonbRestClientBuilderConfigurer; 21 | 22 | module matrix.support.jsonb { 23 | uses io.github.ma1uta.matrix.support.jsonb.JsonbProvider; 24 | exports io.github.ma1uta.matrix.support.jsonb; 25 | exports io.github.ma1uta.matrix.support.jsonb.mapper to org.mapstruct; 26 | 27 | requires transitive matrix.common.api; 28 | requires transitive matrix.common.impl; 29 | requires transitive java.json; 30 | requires org.mapstruct; 31 | requires java.annotation; 32 | requires java.sql; 33 | requires org.slf4j; 34 | 35 | provides Deserializer with JsonbDeserializer; 36 | provides RestClientBuilderConfigurer with JsonbRestClientBuilderConfigurer; 37 | } 38 | -------------------------------------------------------------------------------- /examples/src/main/java/io/github/ma1uta/matrix/example/SimpleExample.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.example; 18 | 19 | import io.github.ma1uta.matrix.client.StandaloneClient; 20 | 21 | public class SimpleExample { 22 | 23 | public static void main(String[] args) { 24 | String domain = "ru-matrix.org"; 25 | String localpart = "ma1uta"; 26 | char[] password = "my_very_secret_password".toCharArray(); 27 | 28 | StandaloneClient mxClient = new StandaloneClient.Builder().domain(domain).build(); 29 | 30 | // login 31 | String userId = mxClient.auth().login(localpart, password).getUserId(); 32 | 33 | // set display name via profile api 34 | System.out.println(mxClient.profile().showDisplayName(userId)); 35 | 36 | // retrieve all joined rooms 37 | mxClient.room().joinedRooms().getJoinedRooms().forEach(System.out::println); 38 | 39 | // logout 40 | mxClient.auth().logout(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /jackson-support/src/main/java/module-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import io.github.ma1uta.matrix.impl.Deserializer; 18 | import io.github.ma1uta.matrix.impl.RestClientBuilderConfigurer; 19 | import io.github.ma1uta.matrix.support.jackson.JacksonDeserializer; 20 | import io.github.ma1uta.matrix.support.jackson.JacksonRestClientBuilderConfigurer; 21 | import io.github.ma1uta.matrix.support.jackson.ObjectMapperProvider; 22 | 23 | module matrix.support.jackson { 24 | uses ObjectMapperProvider; 25 | exports io.github.ma1uta.matrix.support.jackson; 26 | 27 | requires transitive matrix.common.api; 28 | requires transitive matrix.common.impl; 29 | requires transitive com.fasterxml.jackson.core; 30 | requires transitive com.fasterxml.jackson.databind; 31 | requires transitive com.fasterxml.jackson.jaxrs.json; 32 | requires org.slf4j; 33 | 34 | provides Deserializer with JacksonDeserializer; 35 | provides RestClientBuilderConfigurer with JacksonRestClientBuilderConfigurer; 36 | } 37 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/methods/async/VoipAsyncMethods.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.methods.async; 18 | 19 | import io.github.ma1uta.matrix.client.model.voip.VoipResponse; 20 | import io.github.ma1uta.matrix.client.rest.async.VoipApi; 21 | import org.eclipse.microprofile.rest.client.RestClientBuilder; 22 | 23 | import java.util.concurrent.CompletableFuture; 24 | 25 | /** 26 | * Voip methods. 27 | */ 28 | public class VoipAsyncMethods { 29 | 30 | private final VoipApi voipApi; 31 | 32 | public VoipAsyncMethods(RestClientBuilder restClientBuilder) { 33 | voipApi = restClientBuilder.build(VoipApi.class); 34 | } 35 | 36 | /** 37 | * This API provides credentials for the client to use when initiating calls. 38 | * 39 | * @return The TURN server credentials. 40 | */ 41 | public CompletableFuture turnServers() { 42 | return voipApi.turnServer().toCompletableFuture(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/methods/blocked/CapabilityMethods.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.methods.blocked; 18 | 19 | import io.github.ma1uta.matrix.client.model.capability.CapabilitiesResponse; 20 | import io.github.ma1uta.matrix.client.rest.blocked.CapabilitiesApi; 21 | import org.eclipse.microprofile.rest.client.RestClientBuilder; 22 | 23 | /** 24 | * Capability methods. 25 | */ 26 | public class CapabilityMethods { 27 | 28 | private final CapabilitiesApi capabilitiesApi; 29 | 30 | public CapabilityMethods(RestClientBuilder restClientBuilder) { 31 | this.capabilitiesApi = restClientBuilder.build(CapabilitiesApi.class); 32 | } 33 | 34 | /** 35 | * Gets information about the server's supported feature set and other relevant capabilities. 36 | * 37 | * @return server capabilities. 38 | */ 39 | public CapabilitiesResponse capabilities() { 40 | return capabilitiesApi.capabilities(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/rest/async/VoipApi.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.rest.async; 18 | 19 | import io.github.ma1uta.matrix.client.model.voip.VoipResponse; 20 | 21 | import java.util.concurrent.CompletionStage; 22 | import javax.ws.rs.GET; 23 | import javax.ws.rs.Path; 24 | import javax.ws.rs.Produces; 25 | import javax.ws.rs.core.MediaType; 26 | 27 | /** 28 | * The homeserver MAY provide a TURN server which clients can use to contact the remote party. The following HTTP API endpoints will 29 | * be used by clients in order to get information about the TURN server. 30 | */ 31 | @Path("/_matrix/client/r0/voip") 32 | @Produces(MediaType.APPLICATION_JSON) 33 | public interface VoipApi { 34 | 35 | /** 36 | * This API provides credentials for the client to use when initiating calls. 37 | * 38 | * @return {@link VoipResponse}. 39 | */ 40 | @GET 41 | @Path("/turnServer") 42 | CompletionStage turnServer(); 43 | } 44 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/rest/blocked/VersionApi.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.rest.blocked; 18 | 19 | import io.github.ma1uta.matrix.client.model.version.VersionsResponse; 20 | 21 | import javax.ws.rs.GET; 22 | import javax.ws.rs.Path; 23 | import javax.ws.rs.Produces; 24 | import javax.ws.rs.core.MediaType; 25 | 26 | /** 27 | * Gets the versions of the specification supported by the server. 28 | *
29 | * Values will take the form rX.Y.Z. 30 | *
31 | * Only the latest Z value will be reported for each supported X.Y value. 32 | * i.e. if the server implements r0.0.0, r0.0.1, and r1.2.0, it will report r0.0.1 and r1.2.0. 33 | */ 34 | @Path("/_matrix/client/versions") 35 | @Produces(MediaType.APPLICATION_JSON) 36 | public interface VersionApi { 37 | 38 | /** 39 | * Gets the versions of the specification supported by the server. 40 | * 41 | * @return {@link VersionsResponse}. 42 | */ 43 | @GET 44 | VersionsResponse versions(); 45 | } 46 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/ResolvedHomeserver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client; 18 | 19 | import java.net.URL; 20 | import java.util.Optional; 21 | import javax.net.ssl.HostnameVerifier; 22 | 23 | public class ResolvedHomeserver { 24 | 25 | private final URL url; 26 | private final HostnameVerifier hostnameVerifier; 27 | 28 | public ResolvedHomeserver(URL url) { 29 | this(url, null); 30 | } 31 | 32 | public ResolvedHomeserver(URL url, HostnameVerifier hostnameVerifier) { 33 | this.url = url; 34 | this.hostnameVerifier = hostnameVerifier; 35 | } 36 | 37 | public URL getUrl() { 38 | return url; 39 | } 40 | 41 | public Optional getOptionalHostnameVerifier() { 42 | return Optional.ofNullable(hostnameVerifier); 43 | } 44 | 45 | @Override 46 | public String toString() { 47 | return "ResolvedHomeserver{url=" + url + ", hostnameVerifier=" + hostnameVerifier + '}'; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/methods/async/VersionAsyncMethods.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.methods.async; 18 | 19 | import io.github.ma1uta.matrix.client.model.version.VersionsResponse; 20 | import io.github.ma1uta.matrix.client.rest.async.VersionApi; 21 | import org.eclipse.microprofile.rest.client.RestClientBuilder; 22 | 23 | import java.util.concurrent.CompletableFuture; 24 | 25 | /** 26 | * Version methods. 27 | */ 28 | public class VersionAsyncMethods { 29 | 30 | private final VersionApi versionApi; 31 | 32 | public VersionAsyncMethods(RestClientBuilder restClientBuilder) { 33 | this.versionApi = restClientBuilder.build(VersionApi.class); 34 | } 35 | 36 | /** 37 | * Gets the versions of the specification supported by the server. 38 | * 39 | * @return The versions supported by the server. 40 | */ 41 | public CompletableFuture versions() { 42 | return versionApi.versions().toCompletableFuture(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /bot-impl/src/main/java/io/github/ma1uta/matrix/bot/PersistentService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.bot; 18 | 19 | import java.util.function.Consumer; 20 | import java.util.function.Function; 21 | 22 | /** 23 | * Persistent operations with bot data. 24 | *
25 | * 26 | * @param Dao type. 27 | */ 28 | public class PersistentService { 29 | 30 | private final D dao; 31 | 32 | public PersistentService(D dao) { 33 | this.dao = dao; 34 | } 35 | 36 | public D getDao() { 37 | return dao; 38 | } 39 | 40 | /** 41 | * Execute within transaction with result. 42 | * 43 | * @param action action. 44 | * @param result's type. 45 | * @return result. 46 | */ 47 | public R invoke(Function action) { 48 | return action.apply(getDao()); 49 | } 50 | 51 | /** 52 | * Execute within transaction without result. 53 | * 54 | * @param action action. 55 | */ 56 | public void invoke(Consumer action) { 57 | action.accept(getDao()); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /jackson-support/src/main/java/io/github/ma1uta/matrix/support/jackson/JacksonDeserializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.support.jackson; 18 | 19 | import com.fasterxml.jackson.databind.ObjectMapper; 20 | import io.github.ma1uta.matrix.event.content.EventContent; 21 | import io.github.ma1uta.matrix.impl.Deserializer; 22 | 23 | import java.io.IOException; 24 | 25 | /** 26 | * Matrix Jackson-based deserializer. 27 | */ 28 | public class JacksonDeserializer implements Deserializer { 29 | 30 | private final ObjectMapper mapper = ObjectMapperProvider.getInstance().get(); 31 | private final EventContentDeserializer eventContentDeserializer = new EventContentDeserializer(); 32 | 33 | @Override 34 | public T deserialize(byte[] bytes, Class clazz) throws IOException { 35 | return mapper.readValue(bytes, clazz); 36 | } 37 | 38 | @Override 39 | public EventContent deserializeEventContent(byte[] bytes, String eventType) throws IOException { 40 | return eventContentDeserializer.deserialize(bytes, eventType, mapper); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/rest/async/VersionApi.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.rest.async; 18 | 19 | import io.github.ma1uta.matrix.client.model.version.VersionsResponse; 20 | 21 | import java.util.concurrent.CompletionStage; 22 | import javax.ws.rs.GET; 23 | import javax.ws.rs.Path; 24 | import javax.ws.rs.Produces; 25 | import javax.ws.rs.core.MediaType; 26 | 27 | /** 28 | * Gets the versions of the specification supported by the server. 29 | *
30 | * Values will take the form rX.Y.Z. 31 | *
32 | * Only the latest Z value will be reported for each supported X.Y value. 33 | * i.e. if the server implements r0.0.0, r0.0.1, and r1.2.0, it will report r0.0.1 and r1.2.0. 34 | */ 35 | @Path("/_matrix/client/versions") 36 | @Produces(MediaType.APPLICATION_JSON) 37 | public interface VersionApi { 38 | 39 | /** 40 | * Gets the versions of the specification supported by the server. 41 | * 42 | * @return {@link VersionsResponse}. 43 | */ 44 | @GET 45 | CompletionStage versions(); 46 | } 47 | -------------------------------------------------------------------------------- /common-impl/src/main/java/io/github/ma1uta/matrix/impl/Deserializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.impl; 18 | 19 | import io.github.ma1uta.matrix.event.content.EventContent; 20 | 21 | import java.io.IOException; 22 | 23 | /** 24 | * Deserializer. 25 | */ 26 | public interface Deserializer { 27 | 28 | /** 29 | * Deserialize bytes to class. 30 | * 31 | * @param bytes instance content. 32 | * @param clazz class name. 33 | * @param class type. 34 | * @return deserialized object. 35 | * @throws IOException when unable to deserialize object. 36 | */ 37 | T deserialize(byte[] bytes, Class clazz) throws IOException; 38 | 39 | /** 40 | * Deserialize bytes to {@link io.github.ma1uta.matrix.event.content.EventContent}. 41 | * 42 | * @param bytes instance content. 43 | * @param eventType event type. 44 | * @return event content. 45 | * @throws IOException when unable to deserialize object. 46 | */ 47 | EventContent deserializeEventContent(byte[] bytes, String eventType) throws IOException; 48 | } 49 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/methods/async/CapabilityAsyncMethods.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.methods.async; 18 | 19 | import io.github.ma1uta.matrix.client.model.capability.CapabilitiesResponse; 20 | import io.github.ma1uta.matrix.client.rest.async.CapabilitiesApi; 21 | import org.eclipse.microprofile.rest.client.RestClientBuilder; 22 | 23 | import java.util.concurrent.CompletableFuture; 24 | 25 | /** 26 | * Capability methods. 27 | */ 28 | public class CapabilityAsyncMethods { 29 | 30 | private final CapabilitiesApi capabilitiesApi; 31 | 32 | public CapabilityAsyncMethods(RestClientBuilder restClientBuilder) { 33 | this.capabilitiesApi = restClientBuilder.build(CapabilitiesApi.class); 34 | } 35 | 36 | /** 37 | * Gets information about the server's supported feature set and other relevant capabilities. 38 | * 39 | * @return server capabilities. 40 | */ 41 | public CompletableFuture capabilities() { 42 | return capabilitiesApi.capabilities().toCompletableFuture(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /jsonb-support/src/main/java/io/github/ma1uta/matrix/support/jsonb/JsonbDeserializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.support.jsonb; 18 | 19 | import io.github.ma1uta.matrix.event.content.EventContent; 20 | import io.github.ma1uta.matrix.impl.Deserializer; 21 | import io.github.ma1uta.matrix.support.jsonb.mapper.EventMapper; 22 | 23 | import java.io.ByteArrayInputStream; 24 | import java.io.IOException; 25 | import javax.json.JsonObject; 26 | import javax.json.bind.Jsonb; 27 | 28 | /** 29 | * Matrix Jsonb-based deserializer. 30 | */ 31 | public class JsonbDeserializer implements Deserializer { 32 | 33 | private final Jsonb jsonb = JsonbProvider.getInstance().get(); 34 | 35 | @Override 36 | public T deserialize(byte[] bytes, Class clazz) throws IOException { 37 | return jsonb.fromJson(new ByteArrayInputStream(bytes), clazz); 38 | } 39 | 40 | @Override 41 | public EventContent deserializeEventContent(byte[] bytes, String eventType) throws IOException { 42 | return EventMapper.INSTANCE.deserializeEventContent(jsonb.fromJson(new ByteArrayInputStream(bytes), JsonObject.class), eventType); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/methods/blocked/AdminMethods.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.methods.blocked; 18 | 19 | import io.github.ma1uta.matrix.client.model.admin.AdminResponse; 20 | import io.github.ma1uta.matrix.client.rest.blocked.AdminApi; 21 | import org.eclipse.microprofile.rest.client.RestClientBuilder; 22 | 23 | import java.util.Objects; 24 | 25 | /** 26 | * Admin methods. 27 | */ 28 | public class AdminMethods { 29 | 30 | private final AdminApi adminApi; 31 | 32 | public AdminMethods(RestClientBuilder restClientBuilder) { 33 | this.adminApi = restClientBuilder.build(AdminApi.class); 34 | } 35 | 36 | /** 37 | * This API may be restricted to only be called by the user being looked up, or by a server admin. Server-local administrator 38 | * privileges are not specified in this document. 39 | * 40 | * @param userId The user mxid 41 | * @return The user information. 42 | */ 43 | public AdminResponse whois(String userId) { 44 | Objects.requireNonNull(userId, "UserId cannot be empty."); 45 | 46 | return adminApi.whois(userId); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/filter/UserIdClientFilter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.filter; 18 | 19 | import io.github.ma1uta.matrix.client.ConnectionInfo; 20 | 21 | import java.io.IOException; 22 | import java.util.Objects; 23 | import javax.ws.rs.client.ClientRequestContext; 24 | import javax.ws.rs.client.ClientRequestFilter; 25 | import javax.ws.rs.core.UriBuilder; 26 | import javax.ws.rs.ext.Provider; 27 | 28 | /** 29 | * Filter to specify custom headers. 30 | */ 31 | @Provider 32 | public class UserIdClientFilter implements ClientRequestFilter { 33 | 34 | private final ConnectionInfo connectionInfo; 35 | 36 | public UserIdClientFilter(ConnectionInfo connectionInfo) { 37 | this.connectionInfo = Objects.requireNonNull(connectionInfo, "Connection info must be specified."); 38 | } 39 | 40 | @Override 41 | public void filter(ClientRequestContext requestContext) throws IOException { 42 | if (connectionInfo.getUserId() != null) { 43 | requestContext.setUri(UriBuilder.fromUri(requestContext.getUri()).queryParam("user_id", connectionInfo.getUserId()).build()); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/filter/AuthorizationFilter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.filter; 18 | 19 | import static javax.ws.rs.core.HttpHeaders.AUTHORIZATION; 20 | 21 | import io.github.ma1uta.matrix.client.ConnectionInfo; 22 | 23 | import java.io.IOException; 24 | import java.util.Objects; 25 | import javax.ws.rs.client.ClientRequestContext; 26 | import javax.ws.rs.client.ClientRequestFilter; 27 | import javax.ws.rs.ext.Provider; 28 | 29 | /** 30 | * Filter to specify custom headers. 31 | */ 32 | @Provider 33 | public class AuthorizationFilter implements ClientRequestFilter { 34 | 35 | private final ConnectionInfo connectionInfo; 36 | 37 | public AuthorizationFilter(ConnectionInfo connectionInfo) { 38 | this.connectionInfo = Objects.requireNonNull(connectionInfo, "Connection info must be specified."); 39 | } 40 | 41 | @Override 42 | public void filter(ClientRequestContext requestContext) throws IOException { 43 | if (connectionInfo.getAccessToken() != null) { 44 | requestContext.getHeaders().putSingle(AUTHORIZATION, "Bearer " + connectionInfo.getAccessToken()); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/rest/blocked/AdminApi.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.rest.blocked; 18 | 19 | import io.github.ma1uta.matrix.client.model.admin.AdminResponse; 20 | 21 | import javax.ws.rs.GET; 22 | import javax.ws.rs.Path; 23 | import javax.ws.rs.PathParam; 24 | import javax.ws.rs.Produces; 25 | import javax.ws.rs.core.MediaType; 26 | 27 | /** 28 | * Gets information about a particular user. 29 | */ 30 | @Path("/_matrix/client/r0/admin") 31 | @Produces(MediaType.APPLICATION_JSON) 32 | public interface AdminApi { 33 | 34 | /** 35 | * This API may be restricted to only be called by the user being looked up, or by a server admin. Server-local administrator 36 | * privileges are not specified in this document. 37 | *
38 | * Requires auth: Yes. 39 | *
40 | * Return: {@link AdminResponse}. 41 | *

Status code 200: The lookup was successful.

42 | * 43 | * @param userId Required. The user to look up. 44 | * @return {@link AdminResponse} 45 | */ 46 | @GET 47 | @Path("/whois/{userId}") 48 | AdminResponse whois( 49 | @PathParam("userId") String userId 50 | ); 51 | } 52 | -------------------------------------------------------------------------------- /bot-impl/src/main/java/io/github/ma1uta/matrix/bot/command/Leave.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.bot.command; 18 | 19 | import io.github.ma1uta.matrix.bot.BotConfig; 20 | import io.github.ma1uta.matrix.bot.BotDao; 21 | import io.github.ma1uta.matrix.bot.Context; 22 | import io.github.ma1uta.matrix.bot.PersistentService; 23 | import io.github.ma1uta.matrix.event.RoomEvent; 24 | 25 | /** 26 | * Leave current room. 27 | * 28 | * @param bot configuration. 29 | * @param bot dao. 30 | * @param bot service. 31 | * @param extra data. 32 | */ 33 | public class Leave, S extends PersistentService, E> extends OwnerCommand { 34 | @Override 35 | public String name() { 36 | return "leave"; 37 | } 38 | 39 | @Override 40 | public boolean ownerInvoke(Context context, String roomId, RoomEvent event, String arguments) { 41 | context.getMatrixClient().room().leave(roomId); 42 | return true; 43 | } 44 | 45 | @Override 46 | public String help() { 47 | return "leave room (invoked only by owner)."; 48 | } 49 | 50 | @Override 51 | public String usage() { 52 | return "leave"; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /client-impl/src/test/java/io/github/ma1uta/matrix/client/MockServer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client; 18 | 19 | import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; 20 | 21 | import com.github.tomakehurst.wiremock.WireMockServer; 22 | import com.github.tomakehurst.wiremock.common.ConsoleNotifier; 23 | import org.junit.jupiter.api.AfterEach; 24 | import org.junit.jupiter.api.BeforeEach; 25 | 26 | public class MockServer { 27 | 28 | public static final String ACCESS_TOKEN = "$ome_sEcreT_t0keN"; 29 | 30 | private StandaloneClient matrixClient; 31 | public WireMockServer wireMockServer; 32 | 33 | public StandaloneClient getMatrixClient() { 34 | return matrixClient; 35 | } 36 | 37 | @BeforeEach 38 | public void setUp() { 39 | System.setProperty("jmsdk.resolver.homeserver.verification.disable", "true"); 40 | matrixClient = new StandaloneClient("http://localhost:8089"); 41 | wireMockServer = new WireMockServer(options().port(8089).notifier(new ConsoleNotifier(true))); 42 | wireMockServer.start(); 43 | } 44 | 45 | @AfterEach 46 | public void shutdown() { 47 | matrixClient.close(); 48 | wireMockServer.stop(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /bot-impl/src/main/java/io/github/ma1uta/matrix/bot/command/Pong.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.bot.command; 18 | 19 | import io.github.ma1uta.matrix.bot.BotConfig; 20 | import io.github.ma1uta.matrix.bot.BotDao; 21 | import io.github.ma1uta.matrix.bot.Command; 22 | import io.github.ma1uta.matrix.bot.Context; 23 | import io.github.ma1uta.matrix.bot.PersistentService; 24 | import io.github.ma1uta.matrix.event.RoomEvent; 25 | 26 | /** 27 | * Pong command. 28 | * 29 | * @param bot configuration. 30 | * @param bot dao. 31 | * @param bot service. 32 | * @param extra data. 33 | */ 34 | public class Pong, S extends PersistentService, E> implements Command { 35 | 36 | @Override 37 | public String name() { 38 | return "ping"; 39 | } 40 | 41 | @Override 42 | public boolean invoke(Context context, String roomId, RoomEvent event, String arguments) { 43 | context.getMatrixClient().event().sendNotice(roomId, "pong"); 44 | return true; 45 | } 46 | 47 | @Override 48 | public String help() { 49 | return "send pong."; 50 | } 51 | 52 | @Override 53 | public String usage() { 54 | return "ping"; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/methods/async/AdminAsyncMethods.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.methods.async; 18 | 19 | import io.github.ma1uta.matrix.client.model.admin.AdminResponse; 20 | import io.github.ma1uta.matrix.client.rest.async.AdminApi; 21 | import org.eclipse.microprofile.rest.client.RestClientBuilder; 22 | 23 | import java.util.Objects; 24 | import java.util.concurrent.CompletableFuture; 25 | 26 | /** 27 | * Admin methods. 28 | */ 29 | public class AdminAsyncMethods { 30 | 31 | private final AdminApi adminApi; 32 | 33 | public AdminAsyncMethods(RestClientBuilder restClientBuilder) { 34 | this.adminApi = restClientBuilder.build(AdminApi.class); 35 | } 36 | 37 | /** 38 | * This API may be restricted to only be called by the user being looked up, or by a server admin. Server-local administrator 39 | * privileges are not specified in this document. 40 | * 41 | * @param userId The user mxid 42 | * @return The user information. 43 | */ 44 | public CompletableFuture whois(String userId) { 45 | Objects.requireNonNull(userId, "UserId cannot be empty."); 46 | 47 | return adminApi.whois(userId).toCompletableFuture(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /bot-impl/src/main/java/io/github/ma1uta/matrix/bot/Command.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.bot; 18 | 19 | import io.github.ma1uta.matrix.event.RoomEvent; 20 | 21 | /** 22 | * Command interface. 23 | * 24 | * @param actual class of the configuration. 25 | * @param bot's dao. 26 | * @param bot's service. 27 | * @param extra data. 28 | */ 29 | public interface Command, S extends PersistentService, E> { 30 | 31 | /** 32 | * Command name. 33 | * 34 | * @return name. 35 | */ 36 | String name(); 37 | 38 | /** 39 | * Invoking command. 40 | * 41 | * @param context bot's context. 42 | * @param roomId room id. 43 | * @param event event with command. 44 | * @param arguments (without command). 45 | * @return {@code true} if invoked, else {@code false}. 46 | */ 47 | boolean invoke(Context context, String roomId, RoomEvent event, String arguments); 48 | 49 | /** 50 | * Help information. 51 | * 52 | * @return help information. 53 | */ 54 | String help(); 55 | 56 | /** 57 | * Usage information. 58 | * 59 | * @return usage information. 60 | */ 61 | String usage(); 62 | } 63 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/rest/async/AdminApi.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.rest.async; 18 | 19 | import io.github.ma1uta.matrix.client.model.admin.AdminResponse; 20 | 21 | import java.util.concurrent.CompletionStage; 22 | import javax.ws.rs.GET; 23 | import javax.ws.rs.Path; 24 | import javax.ws.rs.PathParam; 25 | import javax.ws.rs.Produces; 26 | import javax.ws.rs.core.MediaType; 27 | 28 | /** 29 | * Gets information about a particular user. 30 | */ 31 | @Path("/_matrix/client/r0/admin") 32 | @Produces(MediaType.APPLICATION_JSON) 33 | public interface AdminApi { 34 | 35 | /** 36 | * This API may be restricted to only be called by the user being looked up, or by a server admin. Server-local administrator 37 | * privileges are not specified in this document. 38 | *
39 | * Requires auth: Yes. 40 | *
41 | * Return: {@link AdminResponse}. 42 | *

Status code 200: The lookup was successful.

43 | * 44 | * @param userId Required. The user to look up. 45 | * @return {@link AdminResponse} 46 | */ 47 | @GET 48 | @Path("/whois/{userId}") 49 | CompletionStage whois( 50 | @PathParam("userId") String userId 51 | ); 52 | } 53 | -------------------------------------------------------------------------------- /client-impl/src/test/java/io/github/ma1uta/matrix/client/VersionAsyncMethodsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client; 18 | 19 | import static com.github.tomakehurst.wiremock.client.WireMock.get; 20 | import static com.github.tomakehurst.wiremock.client.WireMock.okJson; 21 | import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching; 22 | import static org.junit.jupiter.api.Assertions.assertEquals; 23 | import static org.junit.jupiter.api.Assertions.assertNotNull; 24 | 25 | import io.github.ma1uta.matrix.client.model.version.VersionsResponse; 26 | import org.junit.jupiter.api.Test; 27 | 28 | import java.util.List; 29 | 30 | class VersionAsyncMethodsTest extends MockServer { 31 | 32 | @Test 33 | void versions() { 34 | wireMockServer.stubFor(get(urlMatching("/_matrix/client/versions/?")) 35 | .willReturn(okJson("{\"versions\":[\"r0.4.0\",\"r0.3.0\"]}") 36 | ) 37 | ); 38 | 39 | List versions = getMatrixClient().versionsAsync().versions().thenApply(VersionsResponse::getVersions).join(); 40 | assertNotNull(versions); 41 | assertEquals(2, versions.size()); 42 | assertEquals("r0.4.0", versions.get(0)); 43 | assertEquals("r0.3.0", versions.get(1)); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/methods/blocked/OpenIdMethods.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.methods.blocked; 18 | 19 | import io.github.ma1uta.matrix.client.ConnectionInfo; 20 | import io.github.ma1uta.matrix.client.model.openid.OpenIdResponse; 21 | import io.github.ma1uta.matrix.client.rest.blocked.OpenIdApi; 22 | import org.eclipse.microprofile.rest.client.RestClientBuilder; 23 | 24 | /** 25 | * OpenID methods. 26 | */ 27 | public class OpenIdMethods { 28 | 29 | private final OpenIdApi openIdApi; 30 | private final ConnectionInfo connectionInfo; 31 | 32 | public OpenIdMethods(RestClientBuilder restClientBuilder, ConnectionInfo connectionInfo) { 33 | this.openIdApi = restClientBuilder.build(OpenIdApi.class); 34 | this.connectionInfo = connectionInfo; 35 | } 36 | 37 | /** 38 | * Gets an OpenID token object that the requester may supply to another service to verify their identity in Matrix. 39 | * The generated token is only valid for exchanging for user information from the federation API for OpenID. 40 | * 41 | * @return OpenID token. 42 | */ 43 | public OpenIdResponse requestToken() { 44 | return openIdApi.requestToken(connectionInfo.getUserId()); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /bot-impl/src/main/java/io/github/ma1uta/matrix/appservice/Transaction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.appservice; 18 | 19 | import jakarta.persistence.Id; 20 | import jakarta.persistence.MappedSuperclass; 21 | 22 | import java.time.LocalDateTime; 23 | import java.util.Objects; 24 | 25 | /** 26 | * Transaction info. 27 | */ 28 | @MappedSuperclass 29 | public class Transaction { 30 | 31 | @Id 32 | private String id; 33 | 34 | private LocalDateTime processed; 35 | 36 | public String getId() { 37 | return id; 38 | } 39 | 40 | public void setId(String id) { 41 | this.id = id; 42 | } 43 | 44 | public LocalDateTime getProcessed() { 45 | return processed; 46 | } 47 | 48 | public void setProcessed(LocalDateTime processed) { 49 | this.processed = processed; 50 | } 51 | 52 | @Override 53 | public boolean equals(Object o) { 54 | if (this == o) { 55 | return true; 56 | } 57 | if (o == null || getClass() != o.getClass()) { 58 | return false; 59 | } 60 | Transaction that = (Transaction) o; 61 | return Objects.equals(id, that.id); 62 | } 63 | 64 | @Override 65 | public int hashCode() { 66 | return Objects.hash(id); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /bot-impl/src/main/java/io/github/ma1uta/matrix/bot/command/OwnerCommand.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.bot.command; 18 | 19 | import io.github.ma1uta.matrix.bot.BotConfig; 20 | import io.github.ma1uta.matrix.bot.BotDao; 21 | import io.github.ma1uta.matrix.bot.Command; 22 | import io.github.ma1uta.matrix.bot.Context; 23 | import io.github.ma1uta.matrix.bot.PersistentService; 24 | import io.github.ma1uta.matrix.event.RoomEvent; 25 | 26 | /** 27 | * Provide checking that current command was invoked by owner. 28 | * 29 | * @param bot configuration. 30 | * @param bot dao. 31 | * @param bot service. 32 | * @param extra data. 33 | */ 34 | public abstract class OwnerCommand, S extends PersistentService, E> implements 35 | Command { 36 | 37 | @Override 38 | public boolean invoke(Context context, String roomId, RoomEvent event, String arguments) { 39 | C config = context.getConfig(); 40 | if (config.getOwner() != null && !config.getOwner().equals(event.getSender())) { 41 | return false; 42 | } 43 | 44 | return ownerInvoke(context, roomId, event, arguments); 45 | } 46 | 47 | protected abstract boolean ownerInvoke(Context context, String roomId, RoomEvent event, String arguments); 48 | } 49 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/ClientHomeServerResolver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client; 18 | 19 | import org.slf4j.Logger; 20 | import org.slf4j.LoggerFactory; 21 | 22 | import java.util.Optional; 23 | 24 | /** 25 | * Home server resolver. 26 | */ 27 | public class ClientHomeServerResolver extends AbstractHomeServerResolver { 28 | 29 | private static final Logger LOGGER = LoggerFactory.getLogger(ClientHomeServerResolver.class); 30 | 31 | public ClientHomeServerResolver() { 32 | super(null); 33 | } 34 | 35 | public ClientHomeServerResolver(Boolean homeserverVerificationDisabled) { 36 | super(homeserverVerificationDisabled); 37 | } 38 | 39 | /** 40 | * Resolve homeserver url. 41 | * 42 | * @param domain homeserver domain. 43 | * @return homeserver url. 44 | */ 45 | @Override 46 | public Optional resolveDomain(String domain) { 47 | Optional resolvedHomeserver = tryParseIPAddresses(domain); 48 | if (!resolvedHomeserver.isPresent()) { 49 | resolvedHomeserver = tryWellKnown(domain); 50 | } 51 | if (!resolvedHomeserver.isPresent()) { 52 | resolvedHomeserver = tryDirectUrl(domain); 53 | } 54 | return resolvedHomeserver; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/methods/blocked/SendToDeviceMethods.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.methods.blocked; 18 | 19 | import io.github.ma1uta.matrix.client.model.sendtodevice.SendToDeviceRequest; 20 | import io.github.ma1uta.matrix.client.rest.blocked.SendToDeviceApi; 21 | import io.github.ma1uta.matrix.common.EmptyResponse; 22 | import org.eclipse.microprofile.rest.client.RestClientBuilder; 23 | 24 | import java.util.Objects; 25 | 26 | /** 27 | * Send to device method. 28 | */ 29 | public class SendToDeviceMethods { 30 | 31 | private final SendToDeviceApi sendToDeviceApi; 32 | 33 | public SendToDeviceMethods(RestClientBuilder restClientBuilder) { 34 | this.sendToDeviceApi = restClientBuilder.build(SendToDeviceApi.class); 35 | } 36 | 37 | /** 38 | * This endpoint is used to send send-to-device events to a set of client devices. 39 | * 40 | * @param eventType The type of event to send. 41 | * @param request The sending data. 42 | * @return The empty response. 43 | */ 44 | public EmptyResponse sendToDevice(String eventType, SendToDeviceRequest request) { 45 | Objects.requireNonNull(eventType, "RoomId cannot be empty."); 46 | 47 | return sendToDeviceApi.send(eventType, Long.toString(System.currentTimeMillis()), request); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /client-impl/src/test/java/io/github/ma1uta/matrix/client/HomeServerResolverTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client; 18 | 19 | import static org.junit.jupiter.api.Assertions.assertEquals; 20 | import static org.junit.jupiter.api.Assertions.assertTrue; 21 | 22 | import org.junit.jupiter.api.Test; 23 | 24 | import java.net.URL; 25 | import java.util.Optional; 26 | 27 | public class HomeServerResolverTest { 28 | 29 | @Test 30 | public void wellKnownTest() { 31 | ClientHomeServerResolver resolver = new ClientHomeServerResolver(); 32 | 33 | Optional resolvedHomeserver = resolver.resolve("ru-matrix.org"); 34 | assertTrue(resolvedHomeserver.isPresent()); 35 | URL url = resolvedHomeserver.get().getUrl(); 36 | assertEquals("https://ru-matrix.org:8448", url.toString()); 37 | 38 | resolvedHomeserver = resolver.resolve("matrix.org"); 39 | assertTrue(resolvedHomeserver.isPresent()); 40 | url = resolvedHomeserver.get().getUrl(); 41 | assertEquals("https://matrix-client.matrix.org", url.toString()); 42 | 43 | resolvedHomeserver = resolver.resolve("sibnsk.net"); 44 | assertTrue(resolvedHomeserver.isPresent()); 45 | url = resolvedHomeserver.get().getUrl(); 46 | assertEquals("https://matrix.sibnsk.net", url.toString()); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/methods/blocked/SearchMethods.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.methods.blocked; 18 | 19 | import io.github.ma1uta.matrix.client.model.search.SearchRequest; 20 | import io.github.ma1uta.matrix.client.model.search.SearchResponse; 21 | import io.github.ma1uta.matrix.client.rest.blocked.SearchApi; 22 | import org.eclipse.microprofile.rest.client.RestClientBuilder; 23 | 24 | import java.util.Objects; 25 | 26 | /** 27 | * Search methods. 28 | */ 29 | public class SearchMethods { 30 | 31 | private final SearchApi searchApi; 32 | 33 | public SearchMethods(RestClientBuilder restClientBuilder) { 34 | this.searchApi = restClientBuilder.build(SearchApi.class); 35 | } 36 | 37 | /** 38 | * Performs a full text search across different categories. 39 | * 40 | * @param request The search request. 41 | * @param nextBatch The point to return events from. If given, this should be a next_batch result from a previous call 42 | * to this endpoint. 43 | * @return Results of the search. 44 | */ 45 | public SearchResponse search(SearchRequest request, String nextBatch) { 46 | Objects.requireNonNull(request.getSearchCategories(), "Search categories cannot be empty."); 47 | 48 | return searchApi.search(nextBatch, request); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/methods/async/OpenIdAsyncMethods.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.methods.async; 18 | 19 | import io.github.ma1uta.matrix.client.ConnectionInfo; 20 | import io.github.ma1uta.matrix.client.model.openid.OpenIdResponse; 21 | import io.github.ma1uta.matrix.client.rest.async.OpenIdApi; 22 | import org.eclipse.microprofile.rest.client.RestClientBuilder; 23 | 24 | import java.util.concurrent.CompletableFuture; 25 | 26 | /** 27 | * OpenID methods. 28 | */ 29 | public class OpenIdAsyncMethods { 30 | 31 | private final OpenIdApi openIdApi; 32 | private final ConnectionInfo connectionInfo; 33 | 34 | public OpenIdAsyncMethods(RestClientBuilder restClientBuilder, ConnectionInfo connectionInfo) { 35 | this.openIdApi = restClientBuilder.build(OpenIdApi.class); 36 | this.connectionInfo = connectionInfo; 37 | } 38 | 39 | /** 40 | * Gets an OpenID token object that the requester may supply to another service to verify their identity in Matrix. 41 | * The generated token is only valid for exchanging for user information from the federation API for OpenID. 42 | * 43 | * @return OpenID token. 44 | */ 45 | public CompletableFuture requestToken() { 46 | return openIdApi.requestToken(connectionInfo.getUserId()).toCompletableFuture(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/rest/blocked/CapabilitiesApi.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.rest.blocked; 18 | 19 | import io.github.ma1uta.matrix.client.model.capability.CapabilitiesResponse; 20 | 21 | import javax.ws.rs.GET; 22 | import javax.ws.rs.Path; 23 | import javax.ws.rs.Produces; 24 | import javax.ws.rs.core.MediaType; 25 | 26 | /** 27 | * A homeserver may not support certain operations and clients must be able to query for what the homeserver can and can't offer. 28 | * For example, a homeserver may not support users changing their password as it is configured to perform authentication against 29 | * an external system. 30 | */ 31 | @Path("/_matrix/client/r0") 32 | @Produces(MediaType.APPLICATION_JSON) 33 | public interface CapabilitiesApi { 34 | 35 | /** 36 | * Gets information about the server's supported feature set and other relevant capabilities. 37 | *
38 | * Requires auth: Yes. 39 | *
40 | * Rate-limited: Yes. 41 | *
42 | * Return: {@link CapabilitiesResponse}. 43 | *

Status code 200: The capabilities of the server.

44 | *

Status code 429: This request was rate-limited.

45 | * 46 | * @return {@link CapabilitiesResponse}. 47 | */ 48 | @GET 49 | @Path("/capabilities") 50 | CapabilitiesResponse capabilities(); 51 | } 52 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/methods/blocked/UserDirectoryMethods.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.methods.blocked; 18 | 19 | import io.github.ma1uta.matrix.client.model.userdirectory.SearchRequest; 20 | import io.github.ma1uta.matrix.client.model.userdirectory.SearchResponse; 21 | import io.github.ma1uta.matrix.client.rest.blocked.UserDirectoryApi; 22 | import org.eclipse.microprofile.rest.client.RestClientBuilder; 23 | 24 | import java.util.Objects; 25 | 26 | /** 27 | * User directory methods. 28 | */ 29 | public class UserDirectoryMethods { 30 | 31 | private final UserDirectoryApi userDirectoryApi; 32 | 33 | public UserDirectoryMethods(RestClientBuilder restClientBuilder) { 34 | this.userDirectoryApi = restClientBuilder.build(UserDirectoryApi.class); 35 | } 36 | 37 | /** 38 | * This API performs a server-side search over all users registered on the server. It searches user ID and displayname 39 | * case-insensitively for users that you share a room with or that are in public rooms. 40 | * 41 | * @param request The search request. 42 | * @return The result of the search. 43 | */ 44 | public SearchResponse search(SearchRequest request) { 45 | Objects.requireNonNull(request.getSearchTerm(), "SearchTerm cannot be empty."); 46 | 47 | return userDirectoryApi.searchUsers(request); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /common-impl/src/main/java/io/github/ma1uta/matrix/impl/exception/MatrixException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.impl.exception; 18 | 19 | import io.github.ma1uta.matrix.common.ExceptionResponse; 20 | 21 | import java.net.HttpURLConnection; 22 | 23 | /** 24 | * Common matrix implementation exception. 25 | */ 26 | public class MatrixException extends RuntimeException { 27 | 28 | /** 29 | * Error code for all other unknown exceptions. 30 | */ 31 | public static final String M_INTERNAL = "M_INTERNAL"; 32 | 33 | /** 34 | * Response status. 35 | */ 36 | private Integer status = HttpURLConnection.HTTP_INTERNAL_ERROR; 37 | 38 | private ExceptionResponse response; 39 | 40 | public MatrixException(String error, ExceptionResponse response) { 41 | super(error); 42 | this.response = response; 43 | } 44 | 45 | public MatrixException(String error, ExceptionResponse response, Integer status) { 46 | this(error, response); 47 | this.status = status; 48 | } 49 | 50 | public ExceptionResponse getResponse() { 51 | return response; 52 | } 53 | 54 | public void setResponse(ExceptionResponse response) { 55 | this.response = response; 56 | } 57 | 58 | public Integer getStatus() { 59 | return status; 60 | } 61 | 62 | public void setStatus(Integer status) { 63 | this.status = status; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/rest/async/CapabilitiesApi.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.rest.async; 18 | 19 | import io.github.ma1uta.matrix.client.model.capability.CapabilitiesResponse; 20 | 21 | import java.util.concurrent.CompletionStage; 22 | import javax.ws.rs.GET; 23 | import javax.ws.rs.Path; 24 | import javax.ws.rs.Produces; 25 | import javax.ws.rs.core.MediaType; 26 | 27 | /** 28 | * A homeserver may not support certain operations and clients must be able to query for what the homeserver can and can't offer. 29 | * For example, a homeserver may not support users changing their password as it is configured to perform authentication against 30 | * an external system. 31 | */ 32 | @Path("/_matrix/client/r0") 33 | @Produces(MediaType.APPLICATION_JSON) 34 | public interface CapabilitiesApi { 35 | 36 | /** 37 | * Gets information about the server's supported feature set and other relevant capabilities. 38 | *
39 | * Requires auth: Yes. 40 | *
41 | * Rate-limited: Yes. 42 | *
43 | * Return: {@link CapabilitiesResponse}. 44 | *

Status code 200: The capabilities of the server.

45 | *

Status code 429: This request was rate-limited.

46 | * 47 | * @return {@link CapabilitiesResponse}. 48 | */ 49 | @GET 50 | @Path("/capabilities") 51 | CompletionStage capabilities(); 52 | } 53 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/methods/async/SendToDeviceAsyncMethods.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.methods.async; 18 | 19 | import io.github.ma1uta.matrix.client.model.sendtodevice.SendToDeviceRequest; 20 | import io.github.ma1uta.matrix.client.rest.async.SendToDeviceApi; 21 | import io.github.ma1uta.matrix.common.EmptyResponse; 22 | import org.eclipse.microprofile.rest.client.RestClientBuilder; 23 | 24 | import java.util.Objects; 25 | import java.util.concurrent.CompletableFuture; 26 | 27 | /** 28 | * Send to device method. 29 | */ 30 | public class SendToDeviceAsyncMethods { 31 | 32 | private final SendToDeviceApi sendToDeviceApi; 33 | 34 | public SendToDeviceAsyncMethods(RestClientBuilder restClientBuilder) { 35 | this.sendToDeviceApi = restClientBuilder.build(SendToDeviceApi.class); 36 | } 37 | 38 | /** 39 | * This endpoint is used to send send-to-device events to a set of client devices. 40 | * 41 | * @param eventType The type of event to send. 42 | * @param request The sending data. 43 | * @return The empty response. 44 | */ 45 | public CompletableFuture sendToDevice(String eventType, SendToDeviceRequest request) { 46 | Objects.requireNonNull(eventType, "RoomId cannot be empty."); 47 | 48 | return sendToDeviceApi.send(eventType, Long.toString(System.currentTimeMillis()), request).toCompletableFuture(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/methods/async/SearchAsyncMethods.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.methods.async; 18 | 19 | import io.github.ma1uta.matrix.client.model.search.SearchRequest; 20 | import io.github.ma1uta.matrix.client.model.search.SearchResponse; 21 | import io.github.ma1uta.matrix.client.rest.async.SearchApi; 22 | import org.eclipse.microprofile.rest.client.RestClientBuilder; 23 | 24 | import java.util.Objects; 25 | import java.util.concurrent.CompletableFuture; 26 | 27 | /** 28 | * Search methods. 29 | */ 30 | public class SearchAsyncMethods { 31 | 32 | private final SearchApi searchApi; 33 | 34 | public SearchAsyncMethods(RestClientBuilder restClientBuilder) { 35 | this.searchApi = restClientBuilder.build(SearchApi.class); 36 | } 37 | 38 | /** 39 | * Performs a full text search across different categories. 40 | * 41 | * @param request The search request. 42 | * @param nextBatch The point to return events from. If given, this should be a next_batch result from a previous call 43 | * to this endpoint. 44 | * @return Results of the search. 45 | */ 46 | public CompletableFuture search(SearchRequest request, String nextBatch) { 47 | Objects.requireNonNull(request.getSearchCategories(), "Search categories cannot be empty."); 48 | 49 | return searchApi.search(nextBatch, request).toCompletableFuture(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/FederationHomeServerResolver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client; 18 | 19 | import org.slf4j.Logger; 20 | import org.slf4j.LoggerFactory; 21 | 22 | import java.util.Optional; 23 | 24 | /** 25 | * Home server resolver. 26 | */ 27 | public class FederationHomeServerResolver extends AbstractHomeServerResolver { 28 | 29 | private static final Logger LOGGER = LoggerFactory.getLogger(FederationHomeServerResolver.class); 30 | 31 | public FederationHomeServerResolver() { 32 | super(null); 33 | } 34 | 35 | public FederationHomeServerResolver(Boolean homeserverVerificationDisabled) { 36 | super(homeserverVerificationDisabled); 37 | } 38 | 39 | /** 40 | * Resolve homeserver url. 41 | * 42 | * @param domain homeserver domain. 43 | * @return homeserver url. 44 | */ 45 | @Override 46 | public Optional resolveDomain(String domain) { 47 | Optional resolvedHomeserver = tryParseIPAddresses(domain); 48 | if (!resolvedHomeserver.isPresent()) { 49 | resolvedHomeserver = tryWellKnown(domain); 50 | } 51 | if (!resolvedHomeserver.isPresent()) { 52 | resolvedHomeserver = trySrvRecord(domain); 53 | } 54 | if (!resolvedHomeserver.isPresent()) { 55 | resolvedHomeserver = tryDirectUrl(domain); 56 | } 57 | return resolvedHomeserver; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /jackson-support/src/main/java/io/github/ma1uta/matrix/support/jackson/workaround/ReceiptTsDeserialized4898.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.support.jackson.workaround; 18 | 19 | import com.fasterxml.jackson.core.JsonParser; 20 | import com.fasterxml.jackson.core.JsonProcessingException; 21 | import com.fasterxml.jackson.core.ObjectCodec; 22 | import com.fasterxml.jackson.databind.DeserializationContext; 23 | import com.fasterxml.jackson.databind.JsonDeserializer; 24 | import com.fasterxml.jackson.databind.JsonNode; 25 | import com.fasterxml.jackson.databind.ObjectMapper; 26 | import io.github.ma1uta.matrix.event.nested.ReceiptTs; 27 | 28 | import java.io.IOException; 29 | 30 | /** 31 | * Workaround for https://github.com/matrix-org/synapse/issues/4898 issue. 32 | */ 33 | public class ReceiptTsDeserialized4898 extends JsonDeserializer { 34 | 35 | private static final ObjectMapper MAPPER = new ObjectMapper(); 36 | 37 | @Override 38 | public ReceiptTs deserialize(JsonParser parser, DeserializationContext ctxt) throws IOException, JsonProcessingException { 39 | ObjectCodec codec = parser.getCodec(); 40 | JsonNode node = codec.readTree(parser); 41 | 42 | if (node.isTextual()) { 43 | return MAPPER.readValue(node.asText(), ReceiptTs.class); 44 | } else { 45 | ReceiptTs receiptTs = new ReceiptTs(); 46 | receiptTs.setTs(node.get("ts").asLong()); 47 | return receiptTs; 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/methods/async/UserDirectoryAsyncMethods.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.methods.async; 18 | 19 | import io.github.ma1uta.matrix.client.model.userdirectory.SearchRequest; 20 | import io.github.ma1uta.matrix.client.model.userdirectory.SearchResponse; 21 | import io.github.ma1uta.matrix.client.rest.async.UserDirectoryApi; 22 | import org.eclipse.microprofile.rest.client.RestClientBuilder; 23 | 24 | import java.util.Objects; 25 | import java.util.concurrent.CompletableFuture; 26 | 27 | /** 28 | * User directory methods. 29 | */ 30 | public class UserDirectoryAsyncMethods { 31 | 32 | private final UserDirectoryApi userDirectoryApi; 33 | 34 | public UserDirectoryAsyncMethods(RestClientBuilder restClientBuilder) { 35 | this.userDirectoryApi = restClientBuilder.build(UserDirectoryApi.class); 36 | } 37 | 38 | /** 39 | * This API performs a server-side search over all users registered on the server. It searches user ID and displayname 40 | * case-insensitively for users that you share a room with or that are in public rooms. 41 | * 42 | * @param request The search request. 43 | * @return The result of the search. 44 | */ 45 | public CompletableFuture search(SearchRequest request) { 46 | Objects.requireNonNull(request.getSearchTerm(), "SearchTerm cannot be empty."); 47 | 48 | return userDirectoryApi.searchUsers(request).toCompletableFuture(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/methods/blocked/EventContextMethods.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.methods.blocked; 18 | 19 | import io.github.ma1uta.matrix.client.model.eventcontext.EventContextResponse; 20 | import io.github.ma1uta.matrix.client.rest.blocked.EventContextApi; 21 | import org.eclipse.microprofile.rest.client.RestClientBuilder; 22 | 23 | import java.util.Objects; 24 | 25 | /** 26 | * Event context methods. 27 | */ 28 | public class EventContextMethods { 29 | 30 | private final EventContextApi eventContextApi; 31 | 32 | public EventContextMethods(RestClientBuilder restClientBuilder) { 33 | this.eventContextApi = restClientBuilder.build(EventContextApi.class); 34 | } 35 | 36 | /** 37 | * This API returns a number of events that happened just before and after the specified event. This allows clients to get the 38 | * context surrounding an event. 39 | * 40 | * @param roomId The room to get events from. 41 | * @param eventId The event to get context around. 42 | * @param limit The maximum number of events to return. Default: 10. 43 | * @return The events and state surrounding the requested event. 44 | */ 45 | public EventContextResponse context(String roomId, String eventId, Integer limit) { 46 | Objects.requireNonNull(roomId, "RoomId cannot be empty."); 47 | Objects.requireNonNull(eventId, "EventId cannot be empty."); 48 | 49 | return eventContextApi.context(roomId, eventId, limit); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /common-impl/src/main/java/io/github/ma1uta/matrix/impl/exception/ExceptionHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.impl.exception; 18 | 19 | import io.github.ma1uta.matrix.common.ErrorResponse; 20 | import io.github.ma1uta.matrix.common.ExceptionResponse; 21 | import org.slf4j.Logger; 22 | import org.slf4j.LoggerFactory; 23 | 24 | import javax.net.ssl.HttpsURLConnection; 25 | import javax.ws.rs.core.Response; 26 | import javax.ws.rs.ext.ExceptionMapper; 27 | 28 | /** 29 | * Common exception handler. 30 | */ 31 | public class ExceptionHandler implements ExceptionMapper { 32 | 33 | private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionHandler.class); 34 | 35 | @Override 36 | public Response toResponse(Throwable exception) { 37 | 38 | LOGGER.error("Exception:", exception); 39 | 40 | ExceptionResponse message; 41 | Integer status; 42 | if (exception instanceof MatrixException) { 43 | MatrixException matrixException = (MatrixException) exception; 44 | message = matrixException.getResponse(); 45 | status = matrixException.getStatus(); 46 | } else { 47 | message = message(exception); 48 | status = HttpsURLConnection.HTTP_INTERNAL_ERROR; 49 | } 50 | 51 | return Response.status(status).entity(message).build(); 52 | } 53 | 54 | protected ErrorResponse message(Throwable throwable) { 55 | return new ErrorResponse(MatrixException.M_INTERNAL, throwable.getMessage()); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /bot-impl/src/main/java/io/github/ma1uta/matrix/bot/command/Prefix.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.bot.command; 18 | 19 | import io.github.ma1uta.matrix.bot.BotConfig; 20 | import io.github.ma1uta.matrix.bot.BotDao; 21 | import io.github.ma1uta.matrix.bot.Context; 22 | import io.github.ma1uta.matrix.bot.PersistentService; 23 | import io.github.ma1uta.matrix.event.RoomEvent; 24 | 25 | /** 26 | * Set new prefix or show current. 27 | * 28 | * @param bot configuration. 29 | * @param bot dao. 30 | * @param bot service. 31 | * @param extra data. 32 | */ 33 | public class Prefix, S extends PersistentService, E> extends OwnerCommand { 34 | 35 | @Override 36 | public String name() { 37 | return "prefix"; 38 | } 39 | 40 | @Override 41 | public boolean ownerInvoke(Context context, String roomId, RoomEvent event, String arguments) { 42 | C config = context.getConfig(); 43 | if (arguments == null || arguments.trim().isEmpty()) { 44 | String prefix = config.getPrefix(); 45 | context.getMatrixClient().event().sendNotice(roomId, prefix == null ? "!" : prefix); 46 | } else { 47 | config.setPrefix(arguments); 48 | } 49 | return true; 50 | } 51 | 52 | @Override 53 | public String help() { 54 | return "set new prefix or show current (invoked only by owner)."; 55 | } 56 | 57 | @Override 58 | public String usage() { 59 | return "prefix []"; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /bot-impl/src/main/java/io/github/ma1uta/matrix/bot/command/NewName.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.bot.command; 18 | 19 | import io.github.ma1uta.matrix.bot.BotConfig; 20 | import io.github.ma1uta.matrix.bot.BotDao; 21 | import io.github.ma1uta.matrix.bot.Context; 22 | import io.github.ma1uta.matrix.bot.PersistentService; 23 | import io.github.ma1uta.matrix.client.MatrixClient; 24 | import io.github.ma1uta.matrix.event.RoomEvent; 25 | 26 | /** 27 | * Set new name. 28 | * 29 | * @param bot configuration. 30 | * @param bot dao. 31 | * @param bot service. 32 | * @param extra data. 33 | */ 34 | public class NewName, S extends PersistentService, E> extends OwnerCommand { 35 | @Override 36 | public String name() { 37 | return "name"; 38 | } 39 | 40 | @Override 41 | public boolean ownerInvoke(Context context, String roomId, RoomEvent event, String arguments) { 42 | MatrixClient matrixClient = context.getMatrixClient(); 43 | if (arguments == null || arguments.trim().isEmpty()) { 44 | matrixClient.event().sendNotice(roomId, "Usage: " + usage()); 45 | } else { 46 | matrixClient.profile().setDisplayName(arguments); 47 | context.getConfig().setDisplayName(arguments); 48 | } 49 | return true; 50 | } 51 | 52 | @Override 53 | public String help() { 54 | return "set new name (invoked only by owner)."; 55 | } 56 | 57 | @Override 58 | public String usage() { 59 | return "name "; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/methods/async/EventContextAsyncMethods.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.methods.async; 18 | 19 | import io.github.ma1uta.matrix.client.model.eventcontext.EventContextResponse; 20 | import io.github.ma1uta.matrix.client.rest.async.EventContextApi; 21 | import org.eclipse.microprofile.rest.client.RestClientBuilder; 22 | 23 | import java.util.Objects; 24 | import java.util.concurrent.CompletableFuture; 25 | 26 | /** 27 | * Event context methods. 28 | */ 29 | public class EventContextAsyncMethods { 30 | 31 | private final EventContextApi eventContextApi; 32 | 33 | public EventContextAsyncMethods(RestClientBuilder restClientBuilder) { 34 | this.eventContextApi = restClientBuilder.build(EventContextApi.class); 35 | } 36 | 37 | /** 38 | * This API returns a number of events that happened just before and after the specified event. This allows clients to get the 39 | * context surrounding an event. 40 | * 41 | * @param roomId The room to get events from. 42 | * @param eventId The event to get context around. 43 | * @param limit The maximum number of events to return. Default: 10. 44 | * @return The events and state surrounding the requested event. 45 | */ 46 | public CompletableFuture context(String roomId, String eventId, Integer limit) { 47 | Objects.requireNonNull(roomId, "RoomId cannot be empty."); 48 | Objects.requireNonNull(eventId, "EventId cannot be empty."); 49 | 50 | return eventContextApi.context(roomId, eventId, limit).toCompletableFuture(); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/rest/blocked/EventContextApi.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.rest.blocked; 18 | 19 | import io.github.ma1uta.matrix.client.model.eventcontext.EventContextResponse; 20 | 21 | import javax.ws.rs.GET; 22 | import javax.ws.rs.Path; 23 | import javax.ws.rs.PathParam; 24 | import javax.ws.rs.Produces; 25 | import javax.ws.rs.QueryParam; 26 | import javax.ws.rs.core.MediaType; 27 | 28 | /** 29 | * This API returns a number of events that happened just before and after the specified event. This allows clients to get the context 30 | * surrounding an event. 31 | */ 32 | @Path("/_matrix/client/r0/rooms") 33 | @Produces(MediaType.APPLICATION_JSON) 34 | public interface EventContextApi { 35 | 36 | /** 37 | * This API returns a number of events that happened just before and after the specified event. This allows clients to get the 38 | * context surrounding an event. 39 | *
40 | * Requires auth: Yes. 41 | *
42 | * Return: {@link EventContextResponse}. 43 | *

Status code 200: The events and state surrounding the requested event.

44 | * 45 | * @param roomId Required. The room to get events from. 46 | * @param eventId Required. The event to get context around. 47 | * @param limit The maximum number of events to return. Default: 10. 48 | * @return {@link EventContextResponse}. 49 | */ 50 | @GET 51 | @Path("/{roomId}/context/{eventId}") 52 | EventContextResponse context( 53 | @PathParam("roomId") String roomId, 54 | @PathParam("eventId") String eventId, 55 | @QueryParam("limit") Integer limit 56 | ); 57 | } 58 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/rest/blocked/ServerDiscoveryApi.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.rest.blocked; 18 | 19 | import io.github.ma1uta.matrix.client.model.serverdiscovery.ServerDiscoveryResponse; 20 | 21 | import javax.ws.rs.GET; 22 | import javax.ws.rs.Path; 23 | import javax.ws.rs.Produces; 24 | import javax.ws.rs.core.MediaType; 25 | 26 | /** 27 | * In order to allow users to connect to a Matrix server without needing to explicitly specify the homeserver's URL or other parameters, 28 | * clients SHOULD use an auto-discovery mechanism to determine the server's URL based on a user's Matrix ID. Auto-discovery should only 29 | * be done at login time. 30 | */ 31 | @Path("/.well-known/matrix/client") 32 | @Produces(MediaType.APPLICATION_JSON) 33 | public interface ServerDiscoveryApi { 34 | 35 | /** 36 | * Gets discovery information about the domain. The file may include additional keys, which MUST follow the Java package naming 37 | * convention, e.g. com.example.myapp.property. This ensures property names are suitably namespaced for each application and 38 | * reduces the risk of clashes. 39 | *
40 | * Note that this endpoint is not necessarily handled by the homeserver, but by another webserver, to be used for discovering 41 | * the homeserver URL. 42 | *
43 | * Return: {@link ServerDiscoveryResponse}. 44 | *

Status code 200: Server discovery information.

45 | *

Status code 404: No server discovery information available.

46 | * 47 | * @return {@link ServerDiscoveryResponse}. 48 | */ 49 | @GET 50 | @Path("/") 51 | ServerDiscoveryResponse serverDiscovery(); 52 | } 53 | -------------------------------------------------------------------------------- /bot-impl/src/main/java/io/github/ma1uta/matrix/bot/command/SetAccessPolicy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.bot.command; 18 | 19 | import io.github.ma1uta.matrix.bot.AccessPolicy; 20 | import io.github.ma1uta.matrix.bot.BotConfig; 21 | import io.github.ma1uta.matrix.bot.BotDao; 22 | import io.github.ma1uta.matrix.bot.Context; 23 | import io.github.ma1uta.matrix.bot.PersistentService; 24 | import io.github.ma1uta.matrix.event.RoomEvent; 25 | 26 | /** 27 | * Set new access policy. 28 | * 29 | * @param bot configuration. 30 | * @param bot dao. 31 | * @param bot service. 32 | * @param extra data. 33 | */ 34 | public class SetAccessPolicy, S extends PersistentService, E> extends OwnerCommand { 35 | @Override 36 | public String name() { 37 | return "policy"; 38 | } 39 | 40 | @Override 41 | public boolean ownerInvoke(Context context, String roomId, RoomEvent event, String arguments) { 42 | if (arguments != null && !arguments.isEmpty()) { 43 | try { 44 | context.getConfig().setPolicy(AccessPolicy.valueOf(arguments.toUpperCase())); 45 | return true; 46 | } catch (IllegalArgumentException ignored) { 47 | // wrong option. 48 | } 49 | } 50 | context.getMatrixClient().event().sendNotice(roomId, "usage: " + usage()); 51 | return true; 52 | } 53 | 54 | @Override 55 | public String help() { 56 | return "who can invoke commands (invoked only by owner)."; 57 | } 58 | 59 | @Override 60 | public String usage() { 61 | return "policy [all|owner]"; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /bot-impl/src/main/java/io/github/ma1uta/matrix/bot/command/DefaultCommand.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.bot.command; 18 | 19 | import io.github.ma1uta.matrix.bot.BotConfig; 20 | import io.github.ma1uta.matrix.bot.BotDao; 21 | import io.github.ma1uta.matrix.bot.Context; 22 | import io.github.ma1uta.matrix.bot.PersistentService; 23 | import io.github.ma1uta.matrix.event.RoomEvent; 24 | 25 | /** 26 | * Set or show default command. 27 | * 28 | * @param bot configuration. 29 | * @param bot dao. 30 | * @param bot service. 31 | * @param extra data. 32 | */ 33 | public class DefaultCommand, S extends PersistentService, E> extends OwnerCommand { 34 | 35 | @Override 36 | public String name() { 37 | return "default"; 38 | } 39 | 40 | @Override 41 | public boolean ownerInvoke(Context context, String roomId, RoomEvent event, String arguments) { 42 | if (arguments == null || arguments.trim().isEmpty()) { 43 | context.getConfig().setDefaultCommand(null); 44 | return true; 45 | } else if (context.getBot().getCommands().get(arguments) != null) { 46 | context.getConfig().setDefaultCommand(arguments); 47 | return true; 48 | } else { 49 | context.getMatrixClient().event().sendNotice(roomId, "Unknown command: " + arguments); 50 | return false; 51 | } 52 | } 53 | 54 | @Override 55 | public String help() { 56 | return "set or show default command (invoked only by owner)."; 57 | } 58 | 59 | @Override 60 | public String usage() { 61 | return "default []"; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/rest/async/EventContextApi.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.rest.async; 18 | 19 | import io.github.ma1uta.matrix.client.model.eventcontext.EventContextResponse; 20 | 21 | import java.util.concurrent.CompletionStage; 22 | import javax.ws.rs.GET; 23 | import javax.ws.rs.Path; 24 | import javax.ws.rs.PathParam; 25 | import javax.ws.rs.Produces; 26 | import javax.ws.rs.QueryParam; 27 | import javax.ws.rs.core.MediaType; 28 | 29 | /** 30 | * This API returns a number of events that happened just before and after the specified event. This allows clients to get the context 31 | * surrounding an event. 32 | */ 33 | @Path("/_matrix/client/r0/rooms") 34 | @Produces(MediaType.APPLICATION_JSON) 35 | public interface EventContextApi { 36 | 37 | /** 38 | * This API returns a number of events that happened just before and after the specified event. This allows clients to get the 39 | * context surrounding an event. 40 | *
41 | * Requires auth: Yes. 42 | *
43 | * Return: {@link EventContextResponse}. 44 | *

Status code 200: The events and state surrounding the requested event.

45 | * 46 | * @param roomId Required. The room to get events from. 47 | * @param eventId Required. The event to get context around. 48 | * @param limit The maximum number of events to return. Default: 10. 49 | * @return {@link EventContextResponse}. 50 | */ 51 | @GET 52 | @Path("/{roomId}/context/{eventId}") 53 | CompletionStage context( 54 | @PathParam("roomId") String roomId, 55 | @PathParam("eventId") String eventId, 56 | @QueryParam("limit") Integer limit 57 | ); 58 | } 59 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/rest/async/ServerDiscoveryApi.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.rest.async; 18 | 19 | import io.github.ma1uta.matrix.client.model.serverdiscovery.ServerDiscoveryResponse; 20 | 21 | import java.util.concurrent.CompletionStage; 22 | import javax.ws.rs.GET; 23 | import javax.ws.rs.Path; 24 | import javax.ws.rs.Produces; 25 | import javax.ws.rs.core.MediaType; 26 | 27 | /** 28 | * In order to allow users to connect to a Matrix server without needing to explicitly specify the homeserver's URL or other parameters, 29 | * clients SHOULD use an auto-discovery mechanism to determine the server's URL based on a user's Matrix ID. Auto-discovery should only 30 | * be done at login time. 31 | */ 32 | @Path("/.well-known/matrix/client") 33 | @Produces(MediaType.APPLICATION_JSON) 34 | public interface ServerDiscoveryApi { 35 | 36 | /** 37 | * Gets discovery information about the domain. The file may include additional keys, which MUST follow the Java package naming 38 | * convention, e.g. com.example.myapp.property. This ensures property names are suitably namespaced for each application and 39 | * reduces the risk of clashes. 40 | *
41 | * Note that this endpoint is not necessarily handled by the homeserver, but by another webserver, to be used for discovering 42 | * the homeserver URL. 43 | *
44 | * Return: {@link ServerDiscoveryResponse}. 45 | *

Status code 200: Server discovery information.

46 | *

Status code 404: No server discovery information available.

47 | * 48 | * @return {@link ServerDiscoveryResponse}. 49 | */ 50 | @GET 51 | @Path("/") 52 | CompletionStage serverDiscovery(); 53 | } 54 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/rest/blocked/ReportApi.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.rest.blocked; 18 | 19 | import io.github.ma1uta.matrix.client.model.report.ReportRequest; 20 | import io.github.ma1uta.matrix.common.EmptyResponse; 21 | 22 | import javax.ws.rs.Consumes; 23 | import javax.ws.rs.POST; 24 | import javax.ws.rs.Path; 25 | import javax.ws.rs.PathParam; 26 | import javax.ws.rs.Produces; 27 | import javax.ws.rs.core.MediaType; 28 | 29 | /** 30 | * Users may encounter content which they find inappropriate and should be able to report it to the server administrators or room 31 | * moderators for review. This module defines a way for users to report content. 32 | *
33 | * Content is reported based upon a negative score, where -100 is "most offensive" and 0 is "inoffensive". 34 | */ 35 | @Path("/_matrix/client/r0") 36 | @Consumes(MediaType.APPLICATION_JSON) 37 | @Produces(MediaType.APPLICATION_JSON) 38 | public interface ReportApi { 39 | 40 | /** 41 | * Reports an event as inappropriate to the server, which may then notify the appropriate people. 42 | *
43 | * Requires auth: Yes. 44 | *
45 | *

Status code 200: The event has been reported successfully.

46 | * 47 | * @param roomId Required. The room in which the event being reported is located. 48 | * @param eventId Required. The event to report. 49 | * @param reportRequest JSON body request. 50 | * @return {@link EmptyResponse}. 51 | */ 52 | @POST 53 | @Path("/rooms/{roomId}/report/{eventId}") 54 | EmptyResponse report( 55 | @PathParam("roomId") String roomId, 56 | @PathParam("eventId") String eventId, 57 | ReportRequest reportRequest 58 | ); 59 | } 60 | -------------------------------------------------------------------------------- /client-impl/src/test/java/io/github/ma1uta/matrix/client/ContentAsyncMethodsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client; 18 | 19 | import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; 20 | import static com.github.tomakehurst.wiremock.client.WireMock.okJson; 21 | import static com.github.tomakehurst.wiremock.client.WireMock.post; 22 | import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching; 23 | import static org.junit.jupiter.api.Assertions.assertEquals; 24 | 25 | import io.github.ma1uta.matrix.client.model.content.ContentUri; 26 | import org.junit.jupiter.api.Test; 27 | 28 | import java.io.InputStream; 29 | import java.util.concurrent.TimeUnit; 30 | import javax.ws.rs.core.MediaType; 31 | 32 | class ContentAsyncMethodsTest extends MockServer { 33 | 34 | @Test 35 | void upload() throws Exception { 36 | wireMockServer.stubFor(post(urlPathMatching("/_matrix/media/r0/upload")) 37 | .withHeader("Content-Type", equalTo(MediaType.TEXT_PLAIN)) 38 | .withHeader("Authorization", equalTo("Bearer " + ACCESS_TOKEN)) 39 | .withQueryParam("filename", equalTo("content.txt")) 40 | .willReturn(okJson("{\"content_uri\":\"mxc://example.com/AQwafuaFswefuhsfAFAgsw\"}") 41 | ) 42 | ); 43 | 44 | try (InputStream inputStream = getClass().getResourceAsStream("/content.txt")) { 45 | getMatrixClient().getConnectionInfo().setAccessToken(ACCESS_TOKEN); 46 | String uri = getMatrixClient().contentAsync().upload(inputStream, "content.txt", "text/plain").thenApply(ContentUri::getContentUri) 47 | .get(1000, TimeUnit.MILLISECONDS); 48 | assertEquals("mxc://example.com/AQwafuaFswefuhsfAFAgsw", uri); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/AbstractClientBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client; 18 | 19 | import java.util.Objects; 20 | 21 | /** 22 | * A Client builder. 23 | * 24 | * @param class of the matrix client. 25 | */ 26 | public abstract class AbstractClientBuilder { 27 | 28 | protected ConnectionInfo connectionInfo = new ConnectionInfo(); 29 | 30 | protected AbstractClientBuilder() { 31 | } 32 | 33 | /** 34 | * Specify a user ID. 35 | * 36 | * @param userId The user ID. 37 | * @return This builder. 38 | */ 39 | public AbstractClientBuilder userId(String userId) { 40 | this.connectionInfo.setUserId(userId); 41 | return this; 42 | } 43 | 44 | /** 45 | * Specify an access token. 46 | * 47 | * @param accessToken The access token. 48 | * @return This builder. 49 | */ 50 | public AbstractClientBuilder accessToken(String accessToken) { 51 | this.connectionInfo.setAccessToken(accessToken); 52 | return this; 53 | } 54 | 55 | /** 56 | * Specify the domain. 57 | * 58 | * @param domain Domain. 59 | * @return This builder. 60 | */ 61 | public AbstractClientBuilder domain(String domain) { 62 | this.connectionInfo.setDomain(domain); 63 | return this; 64 | } 65 | 66 | /** 67 | * Build a new client. 68 | * 69 | * @return The new client. 70 | */ 71 | public C build() { 72 | Objects.requireNonNull(this.connectionInfo.getDomain(), "Domain must be set."); 73 | return newInstance(); 74 | } 75 | 76 | /** 77 | * Create a new client instance. 78 | * 79 | * @return The new client. 80 | */ 81 | protected abstract C newInstance(); 82 | } 83 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/methods/blocked/ReceiptMethods.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.methods.blocked; 18 | 19 | import io.github.ma1uta.matrix.client.model.receipt.ReadMarkersRequest; 20 | import io.github.ma1uta.matrix.client.rest.blocked.ReceiptApi; 21 | import io.github.ma1uta.matrix.common.EmptyResponse; 22 | import org.eclipse.microprofile.rest.client.RestClientBuilder; 23 | 24 | import java.util.Objects; 25 | 26 | /** 27 | * Receipt method. 28 | */ 29 | public class ReceiptMethods { 30 | 31 | private final ReceiptApi receiptApi; 32 | 33 | public ReceiptMethods(RestClientBuilder restClientBuilder) { 34 | this.receiptApi = restClientBuilder.build(ReceiptApi.class); 35 | } 36 | 37 | /** 38 | * Send receipt to specified event. 39 | * 40 | * @param roomId The room id. 41 | * @param eventId The event id. 42 | * @return The empty response. 43 | */ 44 | public EmptyResponse sendReceipt(String roomId, String eventId) { 45 | Objects.requireNonNull(roomId, "RoomId cannot be empty."); 46 | Objects.requireNonNull(eventId, "EventId cannot be empty."); 47 | 48 | return receiptApi.receipt(roomId, io.github.ma1uta.matrix.client.api.ReceiptApi.Receipt.READ, eventId); 49 | } 50 | 51 | /** 52 | * Sets the position of the read marker for a given room, and optionally the read receipt's location. 53 | * 54 | * @param roomId The room id. 55 | * @param request The optional read markers. 56 | * @return The empty response. 57 | */ 58 | public EmptyResponse readMarkers(String roomId, ReadMarkersRequest request) { 59 | Objects.requireNonNull(roomId, "RoomId cannot be empty."); 60 | 61 | return receiptApi.readMarkers(roomId, request); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/methods/blocked/FilterMethods.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.methods.blocked; 18 | 19 | import io.github.ma1uta.matrix.client.ConnectionInfo; 20 | import io.github.ma1uta.matrix.client.model.filter.FilterData; 21 | import io.github.ma1uta.matrix.client.model.filter.FilterResponse; 22 | import io.github.ma1uta.matrix.client.rest.blocked.FilterApi; 23 | import org.eclipse.microprofile.rest.client.RestClientBuilder; 24 | 25 | import java.util.Objects; 26 | 27 | /** 28 | * Filter methods. 29 | */ 30 | public class FilterMethods { 31 | 32 | private final FilterApi filterApi; 33 | 34 | private final ConnectionInfo connectionInfo; 35 | 36 | public FilterMethods(RestClientBuilder restClientBuilder, ConnectionInfo connectionInfo) { 37 | this.filterApi = restClientBuilder.build(FilterApi.class); 38 | this.connectionInfo = connectionInfo; 39 | } 40 | 41 | /** 42 | * Upload new filter. 43 | * 44 | * @param filter An new filter. 45 | * @return The filter id. 46 | */ 47 | public FilterResponse uploadFilter(FilterData filter) { 48 | String userId = connectionInfo.getUserId(); 49 | Objects.requireNonNull(userId, "UserId cannot be empty."); 50 | 51 | return filterApi.uploadFilter(userId, filter); 52 | } 53 | 54 | /** 55 | * Get specified filter. 56 | * 57 | * @param filterId The filter id. 58 | * @return The filter data. 59 | */ 60 | public FilterData getFilter(String filterId) { 61 | String userId = connectionInfo.getUserId(); 62 | Objects.requireNonNull(userId, "UserId cannot be empty."); 63 | Objects.requireNonNull(filterId, "FilterId cannot be empty."); 64 | 65 | return filterApi.getFilter(userId, filterId); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/methods/blocked/ReportMethods.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.methods.blocked; 18 | 19 | import io.github.ma1uta.matrix.client.model.report.ReportRequest; 20 | import io.github.ma1uta.matrix.client.rest.blocked.ReportApi; 21 | import io.github.ma1uta.matrix.common.EmptyResponse; 22 | import org.eclipse.microprofile.rest.client.RestClientBuilder; 23 | 24 | import java.util.Objects; 25 | 26 | /** 27 | * Report methods. 28 | */ 29 | public class ReportMethods { 30 | 31 | private final ReportApi reportApi; 32 | 33 | public ReportMethods(RestClientBuilder restClientBuilder) { 34 | this.reportApi = restClientBuilder.build(ReportApi.class); 35 | } 36 | 37 | /** 38 | * Reports an event as inappropriate to the server, which may then notify the appropriate people. 39 | * 40 | * @param roomId The room in which the event being reported is located. 41 | * @param eventId The event to report. 42 | * @param reason The reason the content is being reported. May be blank. 43 | * @param score The score to rate this content as where -100 is most offensive and 0 is inoffensive. 44 | * @return The empty response. 45 | */ 46 | public EmptyResponse report(String roomId, String eventId, String reason, Integer score) { 47 | Objects.requireNonNull(roomId, "RoomId cannot be empty."); 48 | Objects.requireNonNull(eventId, "EventId cannot be empty."); 49 | Objects.requireNonNull(reason, "Reason cannot be empty."); 50 | Objects.requireNonNull(score, "Score cannot be empty."); 51 | 52 | ReportRequest request = new ReportRequest(); 53 | request.setReason(reason); 54 | request.setScore(score); 55 | 56 | return reportApi.report(roomId, eventId, request); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/rest/async/ReportApi.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.rest.async; 18 | 19 | import io.github.ma1uta.matrix.client.model.report.ReportRequest; 20 | import io.github.ma1uta.matrix.common.EmptyResponse; 21 | 22 | import java.util.concurrent.CompletionStage; 23 | import javax.ws.rs.Consumes; 24 | import javax.ws.rs.POST; 25 | import javax.ws.rs.Path; 26 | import javax.ws.rs.PathParam; 27 | import javax.ws.rs.Produces; 28 | import javax.ws.rs.core.MediaType; 29 | 30 | /** 31 | * Users may encounter content which they find inappropriate and should be able to report it to the server administrators or room 32 | * moderators for review. This module defines a way for users to report content. 33 | *
34 | * Content is reported based upon a negative score, where -100 is "most offensive" and 0 is "inoffensive". 35 | */ 36 | @Path("/_matrix/client/r0") 37 | @Consumes(MediaType.APPLICATION_JSON) 38 | @Produces(MediaType.APPLICATION_JSON) 39 | public interface ReportApi { 40 | 41 | /** 42 | * Reports an event as inappropriate to the server, which may then notify the appropriate people. 43 | *
44 | * Requires auth: Yes. 45 | *
46 | *

Status code 200: The event has been reported successfully.

47 | * 48 | * @param roomId Required. The room in which the event being reported is located. 49 | * @param eventId Required. The event to report. 50 | * @param reportRequest JSON body request. 51 | * @return {@link EmptyResponse}. 52 | */ 53 | @POST 54 | @Path("/rooms/{roomId}/report/{eventId}") 55 | CompletionStage report( 56 | @PathParam("roomId") String roomId, 57 | @PathParam("eventId") String eventId, 58 | ReportRequest reportRequest 59 | ); 60 | } 61 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/rest/blocked/SearchApi.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.rest.blocked; 18 | 19 | import io.github.ma1uta.matrix.client.model.search.SearchRequest; 20 | import io.github.ma1uta.matrix.client.model.search.SearchResponse; 21 | 22 | import javax.ws.rs.Consumes; 23 | import javax.ws.rs.POST; 24 | import javax.ws.rs.Path; 25 | import javax.ws.rs.Produces; 26 | import javax.ws.rs.QueryParam; 27 | import javax.ws.rs.core.MediaType; 28 | 29 | /** 30 | * The search API allows clients to perform full text search across events in all rooms that the user has been in, including those 31 | * that they have left. Only events that the user is allowed to see will be searched, e.g. it won't include events in rooms that 32 | * happened after you left. 33 | */ 34 | @Path("/_matrix/client/r0") 35 | @Consumes(MediaType.APPLICATION_JSON) 36 | @Produces(MediaType.APPLICATION_JSON) 37 | public interface SearchApi { 38 | 39 | /** 40 | * Performs a full text search across different categories. 41 | *
42 | * Rate-limited: Yes. 43 | *
44 | * Requires auth: Yes. 45 | *
46 | *

Status code 200: Results of the search.

47 | *

Status code 400: Part of the request was invalid.

48 | *

Status code 429: This request was rate-limited.

49 | * 50 | * @param nextBatch The point to return events from. If given, this should be a next_batch result from a previous call 51 | * to this endpoint. 52 | * @param searchRequest JSON body request. 53 | * @return {@link SearchResponse}. 54 | */ 55 | @POST 56 | @Path("/search") 57 | SearchResponse search( 58 | @QueryParam("next_batch") String nextBatch, 59 | SearchRequest searchRequest 60 | ); 61 | } 62 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/rest/async/SearchApi.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.rest.async; 18 | 19 | import io.github.ma1uta.matrix.client.model.search.SearchRequest; 20 | import io.github.ma1uta.matrix.client.model.search.SearchResponse; 21 | 22 | import java.util.concurrent.CompletionStage; 23 | import javax.ws.rs.Consumes; 24 | import javax.ws.rs.POST; 25 | import javax.ws.rs.Path; 26 | import javax.ws.rs.Produces; 27 | import javax.ws.rs.QueryParam; 28 | import javax.ws.rs.core.MediaType; 29 | 30 | /** 31 | * The search API allows clients to perform full text search across events in all rooms that the user has been in, including those 32 | * that they have left. Only events that the user is allowed to see will be searched, e.g. it won't include events in rooms that 33 | * happened after you left. 34 | */ 35 | @Path("/_matrix/client/r0") 36 | @Consumes(MediaType.APPLICATION_JSON) 37 | @Produces(MediaType.APPLICATION_JSON) 38 | public interface SearchApi { 39 | 40 | /** 41 | * Performs a full text search across different categories. 42 | *
43 | * Rate-limited: Yes. 44 | *
45 | * Requires auth: Yes. 46 | *
47 | *

Status code 200: Results of the search.

48 | *

Status code 400: Part of the request was invalid.

49 | *

Status code 429: This request was rate-limited.

50 | * 51 | * @param nextBatch The point to return events from. If given, this should be a next_batch result from a previous call 52 | * to this endpoint. 53 | * @param searchRequest JSON body request. 54 | * @return {@link SearchResponse}. 55 | */ 56 | @POST 57 | @Path("/search") 58 | CompletionStage search( 59 | @QueryParam("next_batch") String nextBatch, 60 | SearchRequest searchRequest 61 | ); 62 | } 63 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/rest/blocked/UserDirectoryApi.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.rest.blocked; 18 | 19 | import io.github.ma1uta.matrix.client.model.userdirectory.SearchRequest; 20 | import io.github.ma1uta.matrix.client.model.userdirectory.SearchResponse; 21 | 22 | import javax.ws.rs.Consumes; 23 | import javax.ws.rs.POST; 24 | import javax.ws.rs.Path; 25 | import javax.ws.rs.Produces; 26 | import javax.ws.rs.core.MediaType; 27 | 28 | /** 29 | * User directory. 30 | *
31 | * Provides search over all users. 32 | */ 33 | @Path("/_matrix/client/r0/user_directory") 34 | @Consumes(MediaType.APPLICATION_JSON) 35 | @Produces(MediaType.APPLICATION_JSON) 36 | public interface UserDirectoryApi { 37 | 38 | /** 39 | * Performs a search for users on the homeserver. The homeserver may determine which subset of users are searched, however 40 | * the homeserver MUST at a minimum consider the users the requesting user shares a room with and those who reside in public 41 | * rooms (known to the homeserver). The search MUST consider local users to the homeserver, and SHOULD query remote users as 42 | * part of the search. 43 | *
44 | * The search is performed case-insensitively on user IDs and display names preferably using a collation determined based upon 45 | * the Accept-Language header provided in the request, if present. 46 | *
47 | * Rate-limited: Yes. 48 | *
49 | * Requires auth: Yes. 50 | *
51 | *

Status code 200: The results of the search.

52 | *

Status code 429: This request was rate-limited.

53 | * 54 | * @param request JSON body request. 55 | * @return {@link SearchResponse}. 56 | */ 57 | @POST 58 | @Path("/search") 59 | SearchResponse searchUsers( 60 | SearchRequest request 61 | ); 62 | } 63 | -------------------------------------------------------------------------------- /bot-impl/src/main/java/io/github/ma1uta/matrix/bot/AbstractStandaloneBotPool.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.bot; 18 | 19 | import org.slf4j.Logger; 20 | import org.slf4j.LoggerFactory; 21 | 22 | import java.util.List; 23 | import java.util.concurrent.ExecutorService; 24 | import java.util.concurrent.Executors; 25 | import java.util.concurrent.TimeUnit; 26 | 27 | /** 28 | * Bot service. 29 | * 30 | * @param bot configuration. 31 | * @param bot dao. 32 | * @param bot service. 33 | * @param extra data. 34 | */ 35 | public abstract class AbstractStandaloneBotPool, S extends PersistentService, E> extends 36 | AbstractBotPool> { 37 | 38 | private static final Logger LOGGER = LoggerFactory.getLogger(AbstractStandaloneBotPool.class); 39 | 40 | private static final int TIMEOUT = 10; 41 | 42 | private final ExecutorService pool; 43 | 44 | public AbstractStandaloneBotPool(String displayName, S service, List>> commandClasses) { 45 | super(displayName, service, commandClasses); 46 | pool = Executors.newCachedThreadPool(); 47 | } 48 | 49 | public ExecutorService getPool() { 50 | return pool; 51 | } 52 | 53 | @Override 54 | protected StandaloneBot createBotInstance(C config) { 55 | return new StandaloneBot<>(true, config, getService(), getCommandClasses()); 56 | } 57 | 58 | @Override 59 | protected void submitBot(StandaloneBot bot) { 60 | getPool().submit(bot); 61 | } 62 | 63 | /** 64 | * Stop pool. 65 | * 66 | * @throws InterruptedException when cannot stop bot's thread. 67 | */ 68 | @Override 69 | public void stop() throws InterruptedException { 70 | getPool().awaitTermination(TIMEOUT, TimeUnit.SECONDS); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /jackson-support/src/main/java/io/github/ma1uta/matrix/support/jackson/DefaultObjectMapperProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.support.jackson; 18 | 19 | import com.fasterxml.jackson.annotation.JsonInclude; 20 | import com.fasterxml.jackson.databind.DeserializationFeature; 21 | import com.fasterxml.jackson.databind.ObjectMapper; 22 | import com.fasterxml.jackson.databind.SerializationFeature; 23 | import com.fasterxml.jackson.databind.module.SimpleModule; 24 | import io.github.ma1uta.matrix.event.Event; 25 | import io.github.ma1uta.matrix.event.content.RoomEncryptedContent; 26 | import io.github.ma1uta.matrix.event.content.RoomMessageContent; 27 | import io.github.ma1uta.matrix.event.nested.ReceiptTs; 28 | import io.github.ma1uta.matrix.support.jackson.workaround.ReceiptTsDeserialized4898; 29 | 30 | /** 31 | * Default implementation. 32 | */ 33 | public class DefaultObjectMapperProvider implements ObjectMapperProvider { 34 | 35 | private final ObjectMapper mapper; 36 | 37 | public DefaultObjectMapperProvider() { 38 | mapper = new ObjectMapper(); 39 | mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); 40 | mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); 41 | mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); 42 | 43 | SimpleModule eventModule = new SimpleModule("Jackson Matrix Module"); 44 | eventModule.addDeserializer(Event.class, new EventDeserializer()); 45 | eventModule.addDeserializer(RoomEncryptedContent.class, new RoomEncryptedContentDeserializer()); 46 | eventModule.addDeserializer(RoomMessageContent.class, new RoomMessageContentDeserializer()); 47 | eventModule.addDeserializer(ReceiptTs.class, new ReceiptTsDeserialized4898()); 48 | 49 | mapper.registerModule(eventModule); 50 | } 51 | 52 | @Override 53 | public ObjectMapper get() { 54 | return mapper; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/methods/async/ReportAsyncMethods.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.methods.async; 18 | 19 | import io.github.ma1uta.matrix.client.model.report.ReportRequest; 20 | import io.github.ma1uta.matrix.client.rest.async.ReportApi; 21 | import io.github.ma1uta.matrix.common.EmptyResponse; 22 | import org.eclipse.microprofile.rest.client.RestClientBuilder; 23 | 24 | import java.util.Objects; 25 | import java.util.concurrent.CompletableFuture; 26 | 27 | /** 28 | * Report methods. 29 | */ 30 | public class ReportAsyncMethods { 31 | 32 | private final ReportApi reportApi; 33 | 34 | public ReportAsyncMethods(RestClientBuilder restClientBuilder) { 35 | this.reportApi = restClientBuilder.build(ReportApi.class); 36 | } 37 | 38 | /** 39 | * Reports an event as inappropriate to the server, which may then notify the appropriate people. 40 | * 41 | * @param roomId The room in which the event being reported is located. 42 | * @param eventId The event to report. 43 | * @param reason The reason the content is being reported. May be blank. 44 | * @param score The score to rate this content as where -100 is most offensive and 0 is inoffensive. 45 | * @return The empty response. 46 | */ 47 | public CompletableFuture report(String roomId, String eventId, String reason, Integer score) { 48 | Objects.requireNonNull(roomId, "RoomId cannot be empty."); 49 | Objects.requireNonNull(eventId, "EventId cannot be empty."); 50 | Objects.requireNonNull(reason, "Reason cannot be empty."); 51 | Objects.requireNonNull(score, "Score cannot be empty."); 52 | 53 | ReportRequest request = new ReportRequest(); 54 | request.setReason(reason); 55 | request.setScore(score); 56 | 57 | return reportApi.report(roomId, eventId, request).toCompletableFuture(); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/ContentUriModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client; 18 | 19 | import java.util.Objects; 20 | 21 | /** 22 | * Matrix content URI. 23 | */ 24 | public class ContentUriModel { 25 | 26 | /** 27 | * Content URI schema. 28 | */ 29 | public static final String PREFIX = "mxc://"; 30 | 31 | private static final int PREFIX_LENGTH = PREFIX.length(); 32 | 33 | /** 34 | * Delimiter between server and media id. 35 | */ 36 | public static final String DELIMITER = "/"; 37 | 38 | private final String server; 39 | private final String mediaId; 40 | 41 | public ContentUriModel(String server, String mediaId) { 42 | Objects.requireNonNull(server, "Server parts must be specified"); 43 | Objects.requireNonNull(mediaId, "Media ID part must be specified"); 44 | this.server = server; 45 | this.mediaId = mediaId; 46 | } 47 | 48 | public String getServer() { 49 | return server; 50 | } 51 | 52 | public String getMediaId() { 53 | return mediaId; 54 | } 55 | 56 | public static ContentUriModel valueOf(String uri) { 57 | Objects.requireNonNull(uri, "URI must be specified"); 58 | if (!uri.startsWith(PREFIX)) { 59 | throw new IllegalArgumentException(String.format("%s is not mxc uri, missing schema 'mxc://'", uri)); 60 | } 61 | int delimiterIndex = uri.indexOf(DELIMITER, PREFIX_LENGTH); 62 | if (delimiterIndex == -1) { 63 | throw new IllegalArgumentException(String.format("%s is not mxc uri, unable to split to the server and media id parts", uri)); 64 | } 65 | return new ContentUriModel(uri.substring(PREFIX_LENGTH, delimiterIndex), uri.substring(delimiterIndex + 1)); 66 | } 67 | 68 | @Override 69 | public String toString() { 70 | return PREFIX + server + "/" + mediaId; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/methods/async/ReceiptAsyncMethods.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.methods.async; 18 | 19 | import io.github.ma1uta.matrix.client.model.receipt.ReadMarkersRequest; 20 | import io.github.ma1uta.matrix.client.rest.async.ReceiptApi; 21 | import io.github.ma1uta.matrix.common.EmptyResponse; 22 | import org.eclipse.microprofile.rest.client.RestClientBuilder; 23 | 24 | import java.util.Objects; 25 | import java.util.concurrent.CompletableFuture; 26 | 27 | /** 28 | * Receipt method. 29 | */ 30 | public class ReceiptAsyncMethods { 31 | 32 | private final ReceiptApi receiptApi; 33 | 34 | public ReceiptAsyncMethods(RestClientBuilder restClientBuilder) { 35 | this.receiptApi = restClientBuilder.build(ReceiptApi.class); 36 | } 37 | 38 | /** 39 | * Send receipt to specified event. 40 | * 41 | * @param roomId The room id. 42 | * @param eventId The event id. 43 | * @return The empty response. 44 | */ 45 | public CompletableFuture sendReceipt(String roomId, String eventId) { 46 | Objects.requireNonNull(roomId, "RoomId cannot be empty."); 47 | Objects.requireNonNull(eventId, "EventId cannot be empty."); 48 | 49 | return receiptApi.receipt(roomId, io.github.ma1uta.matrix.client.api.ReceiptApi.Receipt.READ, eventId).toCompletableFuture(); 50 | } 51 | 52 | /** 53 | * Sets the position of the read marker for a given room, and optionally the read receipt's location. 54 | * 55 | * @param roomId The room id. 56 | * @param request The optional read markers. 57 | * @return The empty response. 58 | */ 59 | public CompletableFuture readMarkers(String roomId, ReadMarkersRequest request) { 60 | Objects.requireNonNull(roomId, "RoomId cannot be empty."); 61 | 62 | return receiptApi.readMarkers(roomId, request).toCompletableFuture(); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/rest/async/UserDirectoryApi.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.rest.async; 18 | 19 | import io.github.ma1uta.matrix.client.model.userdirectory.SearchRequest; 20 | import io.github.ma1uta.matrix.client.model.userdirectory.SearchResponse; 21 | 22 | import java.util.concurrent.CompletionStage; 23 | import javax.ws.rs.Consumes; 24 | import javax.ws.rs.POST; 25 | import javax.ws.rs.Path; 26 | import javax.ws.rs.Produces; 27 | import javax.ws.rs.core.MediaType; 28 | 29 | /** 30 | * User directory. 31 | *
32 | * Provides search over all users. 33 | */ 34 | @Path("/_matrix/client/r0/user_directory") 35 | @Consumes(MediaType.APPLICATION_JSON) 36 | @Produces(MediaType.APPLICATION_JSON) 37 | public interface UserDirectoryApi { 38 | 39 | /** 40 | * Performs a search for users on the homeserver. The homeserver may determine which subset of users are searched, however 41 | * the homeserver MUST at a minimum consider the users the requesting user shares a room with and those who reside in public 42 | * rooms (known to the homeserver). The search MUST consider local users to the homeserver, and SHOULD query remote users as 43 | * part of the search. 44 | *
45 | * The search is performed case-insensitively on user IDs and display names preferably using a collation determined based upon 46 | * the Accept-Language header provided in the request, if present. 47 | *
48 | * Rate-limited: Yes. 49 | *
50 | * Requires auth: Yes. 51 | *
52 | *

Status code 200: The results of the search.

53 | *

Status code 429: This request was rate-limited.

54 | * 55 | * @param request JSON body request. 56 | * @return {@link SearchResponse}. 57 | */ 58 | @POST 59 | @Path("/search") 60 | CompletionStage searchUsers( 61 | SearchRequest request 62 | ); 63 | } 64 | -------------------------------------------------------------------------------- /docs/Client-sdk.md: -------------------------------------------------------------------------------- 1 | # Client sdk 2 | 3 | There are two classes: `MatrixClient` and `SyncLoop`. 4 | 5 | `MatrixClient` is a core classes with full support of the [Client-Server API](https://matrix.org/docs/spec/client_server/r0.3.0.html). 6 | 7 | Access to the methods organized via api methods (event() for Event Api, room() for Room Api, ...). 8 | You can find implemented API [here](https://github.com/ma1uta/jeon/tree/master/client-api/src/main/java/io/github/ma1uta/matrix/client/api) 9 | 10 | ### Usage 11 | 12 | ```$java 13 | StandaloneClient mxClient = new StandaloneClient("matrix.homeserver.tld"); 14 | 15 | // login 16 | mxClient.auth().login("username", "password"); 17 | 18 | // set display name via profile api 19 | mxCLient.profile().setDisplayName("my new display name"); 20 | 21 | // retrieve all joined rooms 22 | List rooms = mxClient.room().joinedRooms().join(); 23 | String roomId = rooms.get(0); 24 | // or join to the room 25 | String roomId = mxClient.room().joinByIdOrAlias("#test:matrix.org").join(); 26 | 27 | // send message to the room 28 | mxCLient.event().sendMessage(roomId, "Hello, World!"); 29 | 30 | // logout 31 | mxClient.auth().logout(); 32 | ``` 33 | 34 | There are two ways to receive events from the server: 35 | 1. invoke api 36 | ```$java 37 | CompletableFuture response = mxClient.sync().sync(filterId, nextBatch, fullState, presence, timeout); 38 | ``` 39 | Also it should organize loop to cycle `sync`-method. 40 | 2. run `SyncLoop` in the separate thread. 41 | ```$java 42 | SyncLoop syncLoop = new SyncLoop(mxClient.sync()); 43 | syncLoop.setInboundListener((SyncResponse) -> { 44 | // parse events from the server. 45 | return null; // or a new sync params. 46 | }); 47 | 48 | // Set initial parameters. 49 | SyncParams params = new SyncParams(); 50 | // set initial batch_token (optional) 51 | params.setNextBatch("s123"); 52 | // retrieve full state or not 53 | params.setFullState(true); 54 | // filter the received events (optional) 55 | params.setFilter("myFilter"); 56 | // set presence "offline" or null (optional) 57 | params.setPresence(null); 58 | // set long-polling timeout in milliseconds (recommended to set bigger than 0 to avoid spam server) 59 | params.setTimeout(10 * 1000); 60 | 61 | syncLoop.setInit(params); 62 | 63 | // run the syncLoop 64 | ExecutorService service = Executors.newFixedThreadPool(1); 65 | service.submit(syncLoop); 66 | 67 | // stop loop 68 | service.shutdown(); 69 | service.awaitTermination(10 * 1000, TimeUnit.MILLISECONDS); 70 | ``` 71 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/methods/async/FilterAsyncMethods.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.methods.async; 18 | 19 | import io.github.ma1uta.matrix.client.ConnectionInfo; 20 | import io.github.ma1uta.matrix.client.model.filter.FilterData; 21 | import io.github.ma1uta.matrix.client.model.filter.FilterResponse; 22 | import io.github.ma1uta.matrix.client.rest.async.FilterApi; 23 | import org.eclipse.microprofile.rest.client.RestClientBuilder; 24 | 25 | import java.util.Objects; 26 | import java.util.concurrent.CompletableFuture; 27 | 28 | /** 29 | * Filter methods. 30 | */ 31 | public class FilterAsyncMethods { 32 | 33 | private final FilterApi filterApi; 34 | 35 | private final ConnectionInfo connectionInfo; 36 | 37 | public FilterAsyncMethods(RestClientBuilder restClientBuilder, ConnectionInfo connectionInfo) { 38 | this.filterApi = restClientBuilder.build(FilterApi.class); 39 | this.connectionInfo = connectionInfo; 40 | } 41 | 42 | /** 43 | * Upload new filter. 44 | * 45 | * @param filter An new filter. 46 | * @return The filter id. 47 | */ 48 | public CompletableFuture uploadFilter(FilterData filter) { 49 | String userId = connectionInfo.getUserId(); 50 | Objects.requireNonNull(userId, "UserId cannot be empty."); 51 | 52 | return filterApi.uploadFilter(userId, filter).toCompletableFuture(); 53 | } 54 | 55 | /** 56 | * Get specified filter. 57 | * 58 | * @param filterId The filter id. 59 | * @return The filter data. 60 | */ 61 | public CompletableFuture getFilter(String filterId) { 62 | String userId = connectionInfo.getUserId(); 63 | Objects.requireNonNull(userId, "UserId cannot be empty."); 64 | Objects.requireNonNull(filterId, "FilterId cannot be empty."); 65 | 66 | return filterApi.getFilter(userId, filterId).toCompletableFuture(); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /bot-impl/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 19 | 4.0.0 20 | 21 | 22 | io.github.ma1uta.matrix 23 | jmsdk 24 | 0.14.0-SNAPSHOT 25 | 26 | bot-impl 27 | 28 | 29 | 30 | 31 | io.github.ma1uta.matrix 32 | jmsdk-bom 33 | 0.14.0-SNAPSHOT 34 | pom 35 | import 36 | 37 | 38 | 39 | 40 | 41 | 42 | io.github.ma1uta.matrix 43 | client-impl 44 | ${project.version} 45 | 46 | 47 | 48 | jakarta.persistence 49 | jakarta.persistence-api 50 | provided 51 | 52 | 53 | 54 | 55 | 56 | org.apache.felix 57 | maven-bundle-plugin 58 | 59 | 60 | matrix.bot.impl 61 | 62 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/methods/blocked/SyncMethods.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.methods.blocked; 18 | 19 | import io.github.ma1uta.matrix.client.model.sync.SyncResponse; 20 | import io.github.ma1uta.matrix.client.rest.blocked.SyncApi; 21 | import io.github.ma1uta.matrix.common.Page; 22 | import io.github.ma1uta.matrix.event.Event; 23 | import org.eclipse.microprofile.rest.client.RestClientBuilder; 24 | 25 | /** 26 | * Sync method. 27 | */ 28 | public class SyncMethods { 29 | 30 | private final SyncApi syncApi; 31 | 32 | public SyncMethods(RestClientBuilder restClientBuilder) { 33 | this.syncApi = restClientBuilder.build(SyncApi.class); 34 | } 35 | 36 | /** 37 | * Sync events. 38 | * 39 | * @param filter The filter name. 40 | * @param since The next batch token. 41 | * @param fullState The full state or not. 42 | * @param presence The offline presence or not. 43 | * @param timeout The timeout. 44 | * @return The sync data. 45 | */ 46 | public SyncResponse sync(String filter, String since, boolean fullState, String presence, Long timeout) { 47 | return syncApi.sync(filter, since, fullState, presence, timeout); 48 | } 49 | 50 | /** 51 | * This will listen for new events related to a particular room and return them to the caller. This will block until an event is 52 | * received, or until the timeout is reached. 53 | * 54 | * @param from The token to stream from. This token is either from a previous request to this API or from the initial sync 55 | * API. 56 | * @param timeout The maximum time in milliseconds to wait for an event. 57 | * @param roomId The room ID for which events should be returned. 58 | * @return The events received, which may be none. 59 | */ 60 | public Page events(String from, Long timeout, String roomId) { 61 | return syncApi.events(from, timeout, roomId); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /client-impl/Readme.md: -------------------------------------------------------------------------------- 1 | # Client sdk 2 | 3 | There are two classes: `MatrixClient` and `SyncLoop`. 4 | 5 | `MatrixClient` is a core classe with full support of the [Client-Server API](https://matrix.org/docs/spec/client_server/r0.4.0.html). 6 | 7 | Access to the methods organized via api methods (event() for Event Api, room() for Room Api, ...). 8 | You can find implemented API [here](https://github.com/ma1uta/jeon/tree/master/client-api/src/main/java/io/github/ma1uta/matrix/client/api) 9 | 10 | ### Usage 11 | 12 | Complete example with minimal configuration: https://github.com/ma1uta/matrix-client-example 13 | 14 | ```$java 15 | StandaloneClient mxClient = new StandaloneClient.Builder().domain("matrix.homeserver.tld").build(); 16 | 17 | // login 18 | mxClient.auth().login("username", "password"); 19 | 20 | // set display name via profile api 21 | mxClient.profile().setDisplayName("my new display name"); 22 | 23 | // retrieve all joined rooms 24 | List rooms = mxClient.room().joinedRooms().getJoinedRooms(); 25 | String roomId = rooms.get(0); 26 | // or join to the room 27 | String roomId = mxClient.room().joinByIdOrAlias("#test:matrix.org", null, null).getRoomId(); 28 | 29 | // send message to the room 30 | mxClient.event().sendMessage(roomId, "Hello, World!"); 31 | 32 | // logout 33 | mxClient.auth().logout(); 34 | ``` 35 | 36 | There are two ways to receive events from the server: 37 | 1. invoke api 38 | ```$java 39 | SyncResponse response = mxClient.sync().sync(filterId, nextBatch, fullState, presence, delay); 40 | ``` 41 | 2. run `SyncLoop` in the separate thread. 42 | ```$java 43 | SyncLoop syncLoop = new SyncLoop(mxClient.sync(), (syncResponse, syncParams) -> { 44 | // inbound listener to parse incoming events. 45 | ... 46 | 47 | syncParams.setFullState(false); 48 | 49 | // syncParams.setTerminate(true); // to stop SyncLoop. 50 | }); 51 | 52 | // Set initial parameters (Optional). 53 | SyncParams params = SyncParams.builder() 54 | .nextBatch("s123") // set initial batch_token (optional) 55 | .fullState(true) // retrieve full state or not 56 | .filter("myFilter") // filter the received events (optional) 57 | .presence(null) // set presence "offline" or null (optional) 58 | .timeout(10 * 1000) // set long-polling delay in milliseconds (recommended to set bigger than 0 to avoid spam server) 59 | .build(); 60 | 61 | syncLoop.setInit(params); 62 | 63 | // run the syncLoop 64 | ExecutorService service = Executors.newFixedThreadPool(1); 65 | service.submit(syncLoop); 66 | 67 | // stop loop 68 | service.shutdown(); 69 | 70 | ``` 71 | -------------------------------------------------------------------------------- /examples/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 21 | 4.0.0 22 | 23 | io.github.ma1uta.matrix.examples 24 | simple 25 | 1.0-SNAPSHOT 26 | 27 | 28 | 29 | io.github.ma1uta.matrix 30 | client-impl 31 | 0.14.0-SNAPSHOT 32 | 33 | 34 | 35 | org.jboss.resteasy 36 | resteasy-client-microprofile 37 | 4.6.0.Final 38 | 39 | 40 | org.jboss.resteasy 41 | resteasy-jackson2-provider 42 | 4.6.0.Final 43 | 44 | 45 | ch.qos.logback 46 | logback-classic 47 | 1.2.3 48 | 49 | 50 | javax.enterprise 51 | cdi-api 52 | 1.2 53 | 54 | 55 | 56 | 57 | 58 | 59 | org.apache.maven.plugins 60 | maven-compiler-plugin 61 | 3.8.1 62 | 63 | 11 64 | 65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/methods/blocked/TypingMethods.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.methods.blocked; 18 | 19 | import io.github.ma1uta.matrix.client.ConnectionInfo; 20 | import io.github.ma1uta.matrix.client.model.typing.TypingRequest; 21 | import io.github.ma1uta.matrix.client.rest.blocked.TypingApi; 22 | import io.github.ma1uta.matrix.common.EmptyResponse; 23 | import org.eclipse.microprofile.rest.client.RestClientBuilder; 24 | 25 | import java.util.Objects; 26 | 27 | /** 28 | * Typing methods. 29 | */ 30 | public class TypingMethods { 31 | 32 | private final TypingApi typingApi; 33 | 34 | private final ConnectionInfo connectionInfo; 35 | 36 | public TypingMethods(RestClientBuilder restClientBuilder, ConnectionInfo connectionInfo) { 37 | this.typingApi = restClientBuilder.build(TypingApi.class); 38 | this.connectionInfo = connectionInfo; 39 | } 40 | 41 | /** 42 | * This tells the server that the user is typing for the next N milliseconds where N is the value specified in the timeout key. 43 | * Alternatively, if typing is false, it tells the server that the user has stopped typing. 44 | * 45 | * @param roomId The user who has started to type. 46 | * @param typing Whether the user is typing or not. If false, the timeout key can be omitted. 47 | * @param timeout The length of time in milliseconds to mark this user as typing. 48 | * @return The empty response. 49 | */ 50 | public EmptyResponse typing(String roomId, Boolean typing, Long timeout) { 51 | String userId = connectionInfo.getUserId(); 52 | Objects.requireNonNull(roomId, "RoomId cannot be empty."); 53 | Objects.requireNonNull(userId, "UserId cannot be empty."); 54 | Objects.requireNonNull(typing, "Typing cannot be empty."); 55 | 56 | TypingRequest request = new TypingRequest(); 57 | request.setTyping(typing); 58 | request.setTimeout(timeout); 59 | 60 | return typingApi.typing(roomId, userId, request); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /jackson-support/src/main/java/io/github/ma1uta/matrix/support/jackson/RoomEncryptedContentDeserializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.support.jackson; 18 | 19 | import com.fasterxml.jackson.core.JsonParser; 20 | import com.fasterxml.jackson.core.ObjectCodec; 21 | import com.fasterxml.jackson.databind.DeserializationContext; 22 | import com.fasterxml.jackson.databind.JsonDeserializer; 23 | import com.fasterxml.jackson.databind.JsonNode; 24 | import io.github.ma1uta.matrix.event.content.RoomEncryptedContent; 25 | import io.github.ma1uta.matrix.event.encrypted.MegolmEncryptedContent; 26 | import io.github.ma1uta.matrix.event.encrypted.OlmEncryptedContent; 27 | import io.github.ma1uta.matrix.event.encrypted.RawEncryptedContent; 28 | 29 | import java.io.IOException; 30 | 31 | /** 32 | * RoomEncryptedContent deserializer. 33 | */ 34 | public class RoomEncryptedContentDeserializer extends JsonDeserializer { 35 | 36 | @Override 37 | public RoomEncryptedContent deserialize(JsonParser parser, DeserializationContext ctxt) throws IOException { 38 | ObjectCodec codec = parser.getCodec(); 39 | JsonNode node = codec.readTree(parser); 40 | 41 | JsonNode algorithmNode = node.get("algorithm"); 42 | if (algorithmNode == null || !algorithmNode.isTextual()) { 43 | return parse(node, ctxt, codec, null); 44 | } 45 | String algorithm = algorithmNode.asText(); 46 | 47 | switch (algorithm) { 48 | case MegolmEncryptedContent.ALGORITHM: 49 | return codec.treeToValue(node, MegolmEncryptedContent.class); 50 | case OlmEncryptedContent.ALGORITHM: 51 | return codec.treeToValue(node, OlmEncryptedContent.class); 52 | default: 53 | return parse(node, ctxt, codec, algorithm); 54 | } 55 | } 56 | 57 | protected RoomEncryptedContent parse(JsonNode jsonNode, DeserializationContext ctxt, ObjectCodec codec, String algorithm) { 58 | return new RawEncryptedContent(jsonNode, algorithm); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /client-impl/src/main/java/io/github/ma1uta/matrix/client/rest/blocked/OpenIdApi.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright Anatoliy Sablin tolya@sablin.xyz 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ma1uta.matrix.client.rest.blocked; 18 | 19 | import io.github.ma1uta.matrix.client.model.openid.OpenIdResponse; 20 | 21 | import javax.ws.rs.Consumes; 22 | import javax.ws.rs.POST; 23 | import javax.ws.rs.Path; 24 | import javax.ws.rs.PathParam; 25 | import javax.ws.rs.Produces; 26 | import javax.ws.rs.core.MediaType; 27 | 28 | /** 29 | * This module allows users to verify their identity with a third party service. The third party service does need to be matrix-aware 30 | * in that it will need to know to resolve matrix homeservers to exchange the user's token for identity information. 31 | */ 32 | @Path("/_matrix/client/r0") 33 | @Consumes(MediaType.APPLICATION_JSON) 34 | @Produces(MediaType.APPLICATION_JSON) 35 | public interface OpenIdApi { 36 | 37 | /** 38 | * Gets an OpenID token object that the requester may supply to another service to verify their identity in Matrix. 39 | * The generated token is only valid for exchanging for user information from the federation API for OpenID. 40 | *
41 | * The access token generated is only valid for the OpenID API. 42 | * It cannot be used to request another OpenID access token or call /sync, for example. 43 | *
44 | * Rate-limited: Yes. 45 | * Requires auth: Yes. 46 | *
47 | *

Status code 200: OpenID token information. This response is nearly compatible with the response documented 48 | * in the OpenID 1.0 Specification with the only difference being the lack of an id_token. Instead, the Matrix homeserver's name 49 | * is provided.

50 | *

Status code 429: This request was rate-limited.

51 | * 52 | * @param userId Required. The user to request and OpenID token for. Should be the user who is authenticated for the request. 53 | * @return {@link OpenIdResponse}. 54 | */ 55 | @POST 56 | @Path("/user/{userId}/openid/request_token") 57 | OpenIdResponse requestToken( 58 | @PathParam("userId") String userId 59 | ); 60 | } 61 | --------------------------------------------------------------------------------