|
7 | 7 | import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
|
8 | 8 | import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
9 | 9 | import lombok.extern.slf4j.Slf4j;
|
| 10 | +import org.synyx.matrix.bot.domain.MatrixEventId; |
10 | 11 | import org.synyx.matrix.bot.domain.MatrixRoomId;
|
11 | 12 | import org.synyx.matrix.bot.domain.MatrixUserId;
|
12 | 13 | import org.synyx.matrix.bot.internal.MatrixAuthentication;
|
13 | 14 | import org.synyx.matrix.bot.internal.MatrixEventNotifier;
|
14 | 15 | import org.synyx.matrix.bot.internal.MatrixStateSynchronizer;
|
15 | 16 | import org.synyx.matrix.bot.internal.api.MatrixApi;
|
16 | 17 | import org.synyx.matrix.bot.internal.api.dto.MessageDto;
|
| 18 | +import org.synyx.matrix.bot.internal.api.dto.ReactionDto; |
| 19 | +import org.synyx.matrix.bot.internal.api.dto.ReactionRelatesToDto; |
17 | 20 |
|
18 | 21 | import java.util.Optional;
|
19 | 22 |
|
20 | 23 | @Slf4j
|
21 | 24 | public class MatrixClient {
|
22 | 25 |
|
23 |
| - private final MatrixAuthentication authentication; |
24 |
| - private final ObjectMapper objectMapper; |
25 |
| - private final MatrixApi api; |
26 |
| - private MatrixState state; |
27 |
| - private MatrixStateSynchronizer stateSynchronizer; |
28 |
| - private MatrixPersistedState persistedState; |
29 |
| - private MatrixEventNotifier eventNotifier; |
30 |
| - private boolean interruptionRequested; |
31 |
| - |
32 |
| - public MatrixClient(String hostname, String username, String password) { |
33 |
| - |
34 |
| - this.authentication = new MatrixAuthentication(username, password); |
35 |
| - this.objectMapper = JsonMapper.builder() |
36 |
| - .addModule(new Jdk8Module()) |
37 |
| - .addModule(new JavaTimeModule()) |
38 |
| - .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) |
39 |
| - .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) |
40 |
| - .build(); |
41 |
| - this.api = new MatrixApi(hostname, authentication, objectMapper); |
42 |
| - this.state = null; |
43 |
| - this.eventNotifier = null; |
44 |
| - this.interruptionRequested = false; |
| 26 | + private final MatrixAuthentication authentication; |
| 27 | + private final ObjectMapper objectMapper; |
| 28 | + private final MatrixApi api; |
| 29 | + private MatrixState state; |
| 30 | + private MatrixStateSynchronizer stateSynchronizer; |
| 31 | + private MatrixPersistedState persistedState; |
| 32 | + private MatrixEventNotifier eventNotifier; |
| 33 | + private boolean interruptionRequested; |
| 34 | + |
| 35 | + public MatrixClient(String hostname, String username, String password) { |
| 36 | + |
| 37 | + this.authentication = new MatrixAuthentication(username, password); |
| 38 | + this.objectMapper = JsonMapper.builder() |
| 39 | + .addModule(new Jdk8Module()) |
| 40 | + .addModule(new JavaTimeModule()) |
| 41 | + .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) |
| 42 | + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) |
| 43 | + .build(); |
| 44 | + this.api = new MatrixApi(hostname, authentication, objectMapper); |
| 45 | + this.state = null; |
| 46 | + this.eventNotifier = null; |
| 47 | + this.interruptionRequested = false; |
| 48 | + } |
| 49 | + |
| 50 | + public void setEventCallback(MatrixEventConsumer eventConsumer) { |
| 51 | + |
| 52 | + this.eventNotifier = MatrixEventNotifier.from(objectMapper, eventConsumer).orElse(null); |
| 53 | + } |
| 54 | + |
| 55 | + public void setPersistedState(MatrixPersistedState persistedState) { |
| 56 | + |
| 57 | + this.persistedState = persistedState; |
| 58 | + } |
| 59 | + |
| 60 | + public void requestStopOfSync() { |
| 61 | + |
| 62 | + interruptionRequested = true; |
| 63 | + api.terminateOpenConnections(); |
| 64 | + } |
| 65 | + |
| 66 | + public void syncContinuous() { |
| 67 | + |
| 68 | + if (!authentication.isAuthenticated()) { |
| 69 | + if (api.login()) { |
| 70 | + log.info("Successfully logged in to matrix server as {}", |
| 71 | + authentication.getUserId() |
| 72 | + .map(MatrixUserId::toString) |
| 73 | + .orElse("UNKNOWN") |
| 74 | + ); |
| 75 | + } else { |
| 76 | + return; |
| 77 | + } |
45 | 78 | }
|
46 | 79 |
|
47 |
| - public void setEventCallback(MatrixEventConsumer eventConsumer) { |
| 80 | + state = new MatrixState(authentication.getUserId().orElseThrow(IllegalStateException::new)); |
| 81 | + stateSynchronizer = new MatrixStateSynchronizer(state, objectMapper); |
48 | 82 |
|
49 |
| - this.eventNotifier = MatrixEventNotifier.from(objectMapper, eventConsumer).orElse(null); |
50 |
| - } |
51 |
| - |
52 |
| - public void setPersistedState(MatrixPersistedState persistedState) { |
| 83 | + var maybeSyncResponse = api.syncFull(); |
| 84 | + String lastBatch; |
| 85 | + if (maybeSyncResponse.isPresent()) { |
| 86 | + final var syncResponse = maybeSyncResponse.get(); |
| 87 | + lastBatch = syncResponse.nextBatch(); |
53 | 88 |
|
54 |
| - this.persistedState = persistedState; |
| 89 | + stateSynchronizer.synchronizeState(syncResponse); |
| 90 | + } else { |
| 91 | + log.error("Failed to perform initial sync"); |
| 92 | + return; |
55 | 93 | }
|
56 | 94 |
|
57 |
| - public void requestStopOfSync() { |
58 |
| - |
59 |
| - interruptionRequested = true; |
60 |
| - api.terminateOpenConnections(); |
| 95 | + if (eventNotifier != null) { |
| 96 | + eventNotifier.getConsumer().onConnected(state); |
61 | 97 | }
|
62 | 98 |
|
63 |
| - public void syncContinuous() { |
64 |
| - |
65 |
| - if (!authentication.isAuthenticated()) { |
66 |
| - if (api.login()) { |
67 |
| - log.info("Successfully logged in to matrix server as {}", |
68 |
| - authentication.getUserId() |
69 |
| - .map(MatrixUserId::toString) |
70 |
| - .orElse("UNKNOWN") |
71 |
| - ); |
72 |
| - } else { |
73 |
| - return; |
74 |
| - } |
75 |
| - } |
76 |
| - |
77 |
| - state = new MatrixState(authentication.getUserId().orElseThrow(IllegalStateException::new)); |
78 |
| - stateSynchronizer = new MatrixStateSynchronizer(state, objectMapper); |
| 99 | + if (persistedState != null) { |
| 100 | + final var maybePersistedLastBatch = persistedState.getLastBatch(); |
| 101 | + if (maybePersistedLastBatch.isPresent()) { |
| 102 | + lastBatch = maybePersistedLastBatch.get(); |
| 103 | + } else { |
| 104 | + persistedState.setLastBatch(lastBatch); |
| 105 | + } |
| 106 | + } |
79 | 107 |
|
80 |
| - var maybeSyncResponse = api.syncFull(); |
81 |
| - String lastBatch; |
82 |
| - if (maybeSyncResponse.isPresent()) { |
83 |
| - final var syncResponse = maybeSyncResponse.get(); |
84 |
| - lastBatch = syncResponse.nextBatch(); |
| 108 | + while (!interruptionRequested) { |
| 109 | + maybeSyncResponse = api.sync(lastBatch); |
| 110 | + if (maybeSyncResponse.isPresent()) { |
| 111 | + final var syncResponse = maybeSyncResponse.get(); |
| 112 | + lastBatch = syncResponse.nextBatch(); |
85 | 113 |
|
86 |
| - stateSynchronizer.synchronizeState(syncResponse); |
87 |
| - } else { |
88 |
| - log.error("Failed to perform initial sync"); |
89 |
| - return; |
90 |
| - } |
| 114 | + stateSynchronizer.synchronizeState(syncResponse); |
91 | 115 |
|
92 | 116 | if (eventNotifier != null) {
|
93 |
| - eventNotifier.getConsumer().onConnected(state); |
| 117 | + eventNotifier.notifyFromSynchronizationResponse(state, syncResponse); |
94 | 118 | }
|
95 | 119 |
|
96 | 120 | if (persistedState != null) {
|
97 |
| - final var maybePersistedLastBatch = persistedState.getLastBatch(); |
98 |
| - if (maybePersistedLastBatch.isPresent()) { |
99 |
| - lastBatch = maybePersistedLastBatch.get(); |
100 |
| - } else { |
101 |
| - persistedState.setLastBatch(lastBatch); |
102 |
| - } |
| 121 | + persistedState.setLastBatch(lastBatch); |
103 | 122 | }
|
| 123 | + } |
| 124 | + } |
104 | 125 |
|
105 |
| - while (!interruptionRequested) { |
106 |
| - maybeSyncResponse = api.sync(lastBatch); |
107 |
| - if (maybeSyncResponse.isPresent()) { |
108 |
| - final var syncResponse = maybeSyncResponse.get(); |
109 |
| - lastBatch = syncResponse.nextBatch(); |
110 |
| - |
111 |
| - stateSynchronizer.synchronizeState(syncResponse); |
112 |
| - |
113 |
| - if (eventNotifier != null) { |
114 |
| - eventNotifier.notifyFromSynchronizationResponse(state, syncResponse); |
115 |
| - } |
| 126 | + interruptionRequested = false; |
| 127 | + } |
116 | 128 |
|
117 |
| - if (persistedState != null) { |
118 |
| - persistedState.setLastBatch(lastBatch); |
119 |
| - } |
120 |
| - } |
121 |
| - } |
| 129 | + public boolean isConnected() { |
122 | 130 |
|
123 |
| - interruptionRequested = false; |
124 |
| - } |
| 131 | + return state != null; |
| 132 | + } |
125 | 133 |
|
126 |
| - public boolean isConnected() { |
| 134 | + public Optional<MatrixState> getState() { |
127 | 135 |
|
128 |
| - return state != null; |
129 |
| - } |
| 136 | + return Optional.ofNullable(state); |
| 137 | + } |
130 | 138 |
|
131 |
| - public Optional<MatrixState> getState() { |
| 139 | + public boolean sendMessage(MatrixRoomId roomId, String messageBody) { |
132 | 140 |
|
133 |
| - return Optional.ofNullable(state); |
134 |
| - } |
| 141 | + return api.sendEvent(roomId.getFormatted(), "m.room.message", new MessageDto(messageBody, "m.text")); |
| 142 | + } |
135 | 143 |
|
136 |
| - public boolean sendMessage(MatrixRoomId roomId, String messageBody) { |
| 144 | + public boolean addReaction(MatrixRoomId roomId, MatrixEventId eventId, String reaction) { |
137 | 145 |
|
138 |
| - return api.sendEvent(roomId.getFormatted(), "m.room.message", new MessageDto(messageBody, "m.text")); |
139 |
| - } |
| 146 | + final var reactionDto = new ReactionDto(new ReactionRelatesToDto(eventId.getFormatted(), reaction)); |
| 147 | + return api.sendEvent(roomId.getFormatted(), "m.reaction", reactionDto); |
| 148 | + } |
140 | 149 |
|
141 |
| - public boolean joinRoom(MatrixRoomId roomId) { |
| 150 | + public boolean joinRoom(MatrixRoomId roomId) { |
142 | 151 |
|
143 |
| - return api.joinRoom(roomId.getFormatted(), "hello there"); |
144 |
| - } |
| 152 | + return api.joinRoom(roomId.getFormatted(), "hello there"); |
| 153 | + } |
145 | 154 |
|
146 |
| - public boolean leaveRoom(MatrixRoomId roomId) { |
| 155 | + public boolean leaveRoom(MatrixRoomId roomId) { |
147 | 156 |
|
148 |
| - return api.leaveRoom(roomId.getFormatted(), "bai"); |
149 |
| - } |
| 157 | + return api.leaveRoom(roomId.getFormatted(), "bai"); |
| 158 | + } |
150 | 159 | }
|
0 commit comments