Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ on:
branches:
- "main"
jobs:
check-format:
runs-on: ubuntu-latest
container: eclipse-temurin:21
steps:
- name: Checkout repository
uses: actions/checkout@v6
- name: Check format with spotless
run: |
./gradlew spotlessCheck
build-test:
runs-on: ubuntu-latest
container: eclipse-temurin:21
Expand Down
69 changes: 43 additions & 26 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,51 +1,68 @@
plugins {
id 'java'
id 'java-library'
id 'maven-publish'
id 'java'
id 'java-library'
id 'maven-publish'
id 'com.diffplug.spotless' version "8.1.0"
}

group = 'org.synyx'
version = project.hasProperty('version') ? project.version : 'dev'

repositories {
mavenCentral()
mavenCentral()
}

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(21))
}
withSourcesJar()
toolchain {
languageVersion.set(JavaLanguageVersion.of(21))
}
withSourcesJar()
}

dependencies {
implementation 'com.fasterxml.jackson.core:jackson-core:[2.17.0,)'
implementation 'com.fasterxml.jackson.core:jackson-databind:[2.17.0,)'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8:[2.17.0,)'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:[2.17.0,)'
implementation 'com.fasterxml.jackson.core:jackson-core:[2.17.0,)'
implementation 'com.fasterxml.jackson.core:jackson-databind:[2.17.0,)'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8:[2.17.0,)'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:[2.17.0,)'

implementation 'org.slf4j:slf4j-api:[2.0.0,)'
implementation 'org.slf4j:slf4j-api:[2.0.0,)'

testImplementation platform('org.junit:junit-bom:6.0.1')
testImplementation 'org.junit.jupiter:junit-jupiter'
testImplementation platform('org.junit:junit-bom:6.0.1')
testImplementation 'org.junit.jupiter:junit-jupiter'
}

test {
useJUnitPlatform()
useJUnitPlatform()
}

publishing {
publications {
javaMatrixBotLib(MavenPublication) {
from components.java
}
publications {
javaMatrixBotLib(MavenPublication) {
from components.java
}
}

repositories {
maven {
name = 'synyxPublicReleases'
url = uri('https://nexus.synyx.de/repository/public-releases/')
credentials(PasswordCredentials)
}
repositories {
maven {
name = 'synyxPublicReleases'
url = uri('https://nexus.synyx.de/repository/public-releases/')
credentials(PasswordCredentials)
}
}
}

spotless {
groovyGradle {
greclipse()
trimTrailingWhitespace()
leadingTabsToSpaces(2)
endWithNewline()
}
java {
googleJavaFormat('1.33.0').reflowLongStrings()
importOrder()
removeUnusedImports()
forbidWildcardImports()
formatAnnotations()
}
}
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1 @@
rootProject.name = 'java-matrix-bot-lib'
rootProject.name = 'java-matrix-bot-lib'
52 changes: 28 additions & 24 deletions src/main/java/org/synyx/matrix/bot/MatrixClient.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package org.synyx.matrix.bot;

import java.util.Optional;
import org.synyx.matrix.bot.domain.MatrixEventId;
import org.synyx.matrix.bot.domain.MatrixRoomId;
import org.synyx.matrix.bot.internal.MatrixClientImpl;

import java.util.Optional;

/**
* An interface for a client connecting to a matrix server.
* Serves as the main method of communicating with the server.
* An interface for a client connecting to a matrix server. Serves as the main method of
* communicating with the server.
*/
public interface MatrixClient {

/**
* Creates a new matrix client to connect to the specified server.
*
* @param url The url for connecting to the intended matrix server. Must start with http:// or https://
* @param url The url for connecting to the intended matrix server. Must start with http:// or
* https://
* @param username The username for logging into the matrix server.
* @param password The password for logging into the matrix server.
* @return A {@link MatrixClient} implementation that connects to the specified matrix server.
Expand All @@ -26,69 +26,73 @@ static MatrixClient create(String url, String username, String password) {
}

/**
* Sets a consumer object that gets called on events happening on the matrix server.
* Only one consumer can be set at any time.
* Calling this method again replaces any previous event callback.
* Sets a consumer object that gets called on events happening on the matrix server. Only one
* consumer can be set at any time. Calling this method again replaces any previous event
* callback.
*
* @param eventConsumer The consumer to call on events.
*/
void setEventCallback(MatrixEventConsumer eventConsumer);

/**
* Optionally provides an interface to provide the current state of the matrix client.
* If not provided, any startup will act like the first startup and will ignore any previously sent messages.
* Providing a persisted state will make the client be able to determine which events happened while offline.
* Optionally provides an interface to provide the current state of the matrix client. If not
* provided, any startup will act like the first startup and will ignore any previously sent
* messages. Providing a persisted state will make the client be able to determine which events
* happened while offline.
*
* @param persistedState An interface for persisting the matrix client state
*/
void setPersistedStateProvider(MatrixPersistedStateProvider persistedState);


/**
* The main matrix client event loop that continuously syncs all events happening on the matrix server to the client.
* This is a blocking call, so make sure to call it from a different thread if needed.
* The main matrix client event loop that continuously syncs all events happening on the matrix
* server to the client. This is a blocking call, so make sure to call it from a different thread
* if needed.
*
* @throws InterruptedException The sync has been interrupted
*/
void syncContinuous() throws InterruptedException;

/**
* Requests the matrix client to stop syncing and terminate.
* May be called from a different thread.
* Requests the matrix client to stop syncing and terminate. May be called from a different
* thread.
*/
void requestStopOfSync();


/**
* Returns whether the matrix client is currently connected to the server or not.
*
* @return {@code true} if the client is currently connected to the server, {@code false} otherwise.
* @return {@code true} if the client is currently connected to the server, {@code false}
* otherwise.
*/
boolean isConnected();

/**
* Returns the current state of the matrix client.
*
* @return A {@link MatrixState} object if currently connected to a server, {@link Optional#empty()} otherwise.
* @return A {@link MatrixState} object if currently connected to a server, {@link
* Optional#empty()} otherwise.
*/
Optional<MatrixState> getState();

/**
* Attempts to send a message to the specified room.
*
* @param roomId The id of the room to send the message to.
* @param roomId The id of the room to send the message to.
* @param messageBody The body of the message to send.
* @return A {@link MatrixEventId} containing the id of the event that was sent or {@link Optional#empty()} if sending the message did not succeed.
* @return A {@link MatrixEventId} containing the id of the event that was sent or {@link
* Optional#empty()} if sending the message did not succeed.
*/
Optional<MatrixEventId> sendMessage(MatrixRoomId roomId, String messageBody);

/**
* Attempts to add a reaction to an event (a message of the time).
*
* @param roomId The id of the room to send the message in.
* @param eventId The id of the event to react to.
* @param roomId The id of the room to send the message in.
* @param eventId The id of the event to react to.
* @param reaction The reaction to send.
* @return A {@link MatrixEventId} containing the id of the event that was sent or {@link Optional#empty()} if sending the reaction did not succeed.
* @return A {@link MatrixEventId} containing the id of the event that was sent or {@link
* Optional#empty()} if sending the reaction did not succeed.
*/
Optional<MatrixEventId> addReaction(MatrixRoomId roomId, MatrixEventId eventId, String reaction);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.synyx.matrix.bot;

/**
* An exception that was not recoverable from by the matrix client itself occurred while communicating with the matrix server.
* An exception that was not recoverable from by the matrix client itself occurred while
* communicating with the matrix server.
*/
public class MatrixCommunicationException extends RuntimeException {

Expand Down
54 changes: 22 additions & 32 deletions src/main/java/org/synyx/matrix/bot/MatrixEventConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
import org.synyx.matrix.bot.internal.MatrixClientImpl;

/**
* An interface providing callbacks for things happening on the matrix server that were received by the client.
* All methods have a default implementation that does nothing, so implementing classes only need to override whatever
* they want to listen to.
* <p>
* Any reactions to events happening shall be performed using the appropriate {@link MatrixClientImpl} instance.
* An interface providing callbacks for things happening on the matrix server that were received by
* the client. All methods have a default implementation that does nothing, so implementing classes
* only need to override whatever they want to listen to.
*
* <p>Any reactions to events happening shall be performed using the appropriate {@link
* MatrixClientImpl} instance.
*/
public interface MatrixEventConsumer {

Expand All @@ -21,60 +22,49 @@ public interface MatrixEventConsumer {
*
* @param state The state after the initial synchronisation.
*/
default void onConnected(MatrixState state) {

}
default void onConnected(MatrixState state) {}

/**
* A message event was received in a room that the client is part of.
*
* @param state The current client state.
* @param room The room the message was received in.
* @param state The current client state.
* @param room The room the message was received in.
* @param message The message that was received.
*/
default void onMessage(MatrixState state, MatrixRoom room, MatrixMessage message) {

}
default void onMessage(MatrixState state, MatrixRoom room, MatrixMessage message) {}

/**
* An invitation to a room was received.
*
* @param state The current client state.
* @param state The current client state.
* @param invite The invite that was received.
*/
default void onInviteToRoom(MatrixState state, MatrixRoomInvite invite) {

}
default void onInviteToRoom(MatrixState state, MatrixRoomInvite invite) {}

/**
* A user joined a room that the client is part of.
*
* @param state The current client state.
* @param room The room that the user joined in.
* @param state The current client state.
* @param room The room that the user joined in.
* @param userId The id of the user that joined the room.
*/
default void onUserJoinRoom(MatrixState state, MatrixRoom room, MatrixUserId userId) {

}
default void onUserJoinRoom(MatrixState state, MatrixRoom room, MatrixUserId userId) {}

/**
* A user left a room that the client is part of.
*
* @param state The current client state.
* @param room The room that the user left from.
* @param state The current client state.
* @param room The room that the user left from.
* @param userId The id of the user that left the room.
*/
default void onUserLeaveRoom(MatrixState state, MatrixRoom room, MatrixUserId userId) {

}
default void onUserLeaveRoom(MatrixState state, MatrixRoom room, MatrixUserId userId) {}

/**
* The client left a room it was part of. May have been caused by external factors like kicks or bans.
* The client left a room it was part of. May have been caused by external factors like kicks or
* bans.
*
* @param state The current client state.
* @param state The current client state.
* @param roomId The id of the room that the client left from.
*/
default void onSelfLeaveRoom(MatrixState state, MatrixRoomId roomId) {

}
default void onSelfLeaveRoom(MatrixState state, MatrixRoomId roomId) {}
}
Loading