Skip to content

Commit 1c014a9

Browse files
committed
Use the new API for a bunch of classes
1 parent 280cdd1 commit 1c014a9

File tree

13 files changed

+63
-85
lines changed

13 files changed

+63
-85
lines changed

duga-core/src/main/java/net/zomis/duga/chat/ChatBot.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
package net.zomis.duga.chat;
22

33
import net.zomis.duga.chat.events.DugaEvent;
4-
import net.zomis.duga.chat.events.DugaStartedEvent;
54

65
import java.util.List;
76
import java.util.concurrent.Future;
87
import java.util.function.Consumer;
98

109
public interface ChatBot {
1110

12-
Future<List<ChatMessageResponse>> postChat(WebhookParameters params, List<String> messages);
13-
14-
void postSingle(WebhookParameters params, String message);
11+
Future<List<ChatMessageResponse>> postChat(List<ChatMessage> messages);
1512

1613
Future<ChatMessageResponse> postAsync(ChatMessage message);
1714

duga-core/src/main/java/net/zomis/duga/chat/StackExchangeChatBot.java

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,16 @@ private void login() {
6464
LOGGER.info("Found fkey: " + chatFKey);
6565
}
6666

67-
public Future<List<ChatMessageResponse>> postMessages(WebhookParameters params, final List<String> messages) {
68-
if (params == null) {
69-
throw new NullPointerException("Params cannot be null, unable to post " + messages);
70-
}
71-
// params.useDefaultRoom(configuration.getRoomId());
67+
@Override
68+
public Future<List<ChatMessageResponse>> postChat(List<ChatMessage> messages) {
7269
Objects.requireNonNull(messages, "messages");
70+
if (messages.isEmpty()) {
71+
return null;
72+
}
7373
List<ChatMessage> shortenedMessages = new ArrayList<>();
74-
for (String message : messages) {
74+
WebhookParameters params = WebhookParameters.toRoom(messages.get(0).getRoom());
75+
for (ChatMessage mess : messages) {
76+
String message = mess.getMessage();
7577
if (message.length() > MAX_MESSAGE_LENGTH) {
7678
final List<String> messageTokens = ChatMessageHelper.splitToTokens(message);
7779
for (final String reassembled : ChatMessageHelper.reassembleTokens(messageTokens, MAX_MESSAGE_LENGTH,
@@ -83,12 +85,6 @@ public Future<List<ChatMessageResponse>> postMessages(WebhookParameters params,
8385
shortenedMessages.add(new ChatMessage(params, message));
8486
}
8587
}
86-
if (!params.getPost()) {
87-
for (ChatMessage message : shortenedMessages) {
88-
LOGGER.info("Ignoring message for " + message.getRoom() + ": " + message.getMessage());
89-
}
90-
return null;
91-
}
9288

9389
System.out.println("Adding messages to queue: " + shortenedMessages);
9490
messagesQueue.add(shortenedMessages);
@@ -127,16 +123,6 @@ private ChatMessageResponse attemptPostMessageToChat(final ChatMessage message)
127123
return response;
128124
}
129125

130-
@Override
131-
public Future<List<ChatMessageResponse>> postChat(WebhookParameters params, List<String> messages) {
132-
return postMessages(params, messages);
133-
}
134-
135-
@Override
136-
public void postSingle(WebhookParameters params, String message) {
137-
postChat(params, Collections.singletonList(message));
138-
}
139-
140126
@Override
141127
public Future<ChatMessageResponse> postAsync(ChatMessage message) {
142128
this.messagesQueue.add(Collections.singletonList(message));

duga-core/src/main/java/net/zomis/duga/chat/TestBot.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,22 @@
55
import java.util.*;
66
import java.util.concurrent.Future;
77
import java.util.function.Consumer;
8+
import java.util.stream.Collectors;
89

910
public class TestBot implements ChatBot {
1011

1112
Map<WebhookParameters, List<String>> messages = new HashMap<>();
1213

13-
WebhookParameters debug = WebhookParameters.toRoom("debug");
14-
15-
@Deprecated
16-
public void postDebug(String message) {
17-
this.postChat(debug, Collections.singletonList(message));
18-
}
19-
2014
@Override
21-
public Future<List<ChatMessageResponse>> postChat(WebhookParameters params, List<String> messages) {
15+
public Future<List<ChatMessageResponse>> postChat(List<ChatMessage> messages) {
16+
WebhookParameters params = WebhookParameters.toRoom(messages.get(0).getRoom());
2217
this.messages.putIfAbsent(params, new ArrayList<>());
23-
this.messages.get(params).addAll(messages);
18+
this.messages.get(params).addAll(messages.stream()
19+
.map(ChatMessage::getMessage)
20+
.collect(Collectors.toList()));
2421
return null;
2522
}
2623

27-
@Override
28-
public void postSingle(WebhookParameters params, String message) {
29-
this.postChat(params, Collections.singletonList(message));
30-
}
31-
3224
@Override
3325
public Future<ChatMessageResponse> postAsync(ChatMessage message) {
3426
return null;
Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package net.zomis.duga.chat;
22

3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
37
public class WebhookParameters {
48

59
private String roomId;
610

7-
/**
8-
* Used for webhooks that want to be in the statistics but not post to a room
9-
*/
10-
private Boolean post;
11-
1211
public String getRoomId() {
1312
return roomId;
1413
}
@@ -17,17 +16,8 @@ public void setRoomId(String roomId) {
1716
this.roomId = roomId;
1817
}
1918

20-
public boolean getPost() {
21-
return post == null ? true : post;
22-
}
23-
24-
public void setPost(Boolean post) {
25-
this.post = post;
26-
}
27-
2819
public static WebhookParameters toRoom(String roomId) {
2920
WebhookParameters params = new WebhookParameters();
30-
params.setPost(true);
3121
params.setRoomId(roomId);
3222
return params;
3323
}
@@ -40,7 +30,6 @@ public boolean equals(Object o) {
4030

4131
WebhookParameters that = (WebhookParameters) o;
4232

43-
if (post != that.post) return false;
4433
if (!roomId.equals(that.roomId)) return false;
4534

4635
return true;
@@ -49,7 +38,6 @@ public boolean equals(Object o) {
4938
@Override
5039
public int hashCode() {
5140
int result = roomId.hashCode();
52-
result = 31 * result + (post != null ? post.hashCode() : 0);
5341
return result;
5442
}
5543

@@ -61,4 +49,13 @@ public String toString() {
6149
public ChatMessage message(String input) {
6250
return new ChatMessage(this, input);
6351
}
52+
53+
public List<ChatMessage> messages(String... messages) {
54+
return Arrays.stream(messages).map(this::message).collect(Collectors.toList());
55+
}
56+
57+
public List<ChatMessage> messages(List<String> messages) {
58+
return messages.stream().map(this::message).collect(Collectors.toList());
59+
}
60+
6461
}

duga-core/src/main/java/net/zomis/duga/chat/listen/ChatMessageIncoming.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ public class ChatMessageIncoming {
3939
WebhookParameters params;
4040

4141
public void reply(String message) {
42-
bot.postSingle(params, ":" + messageId + " " + message);
42+
bot.postAsync(params.message(":" + messageId + " " + message));
4343
}
4444

4545
public void post(String message) {
46-
bot.postSingle(params, message);
46+
bot.postAsync(params.message(message));
4747
}
4848

4949
public void ping(String message) {
50-
bot.postSingle(params, "@$user_name $message");
50+
bot.postAsync(params.message("@$user_name $message"));
5151
}
5252

5353
@Override

duga-core/src/main/java/net/zomis/duga/chat/listen/ListenTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ synchronized void latestMessages() {
8686
handler.accept(message);
8787
}
8888
if (previousId <= 0) {
89-
bot.postSingle(params, "Monking! (Duga is now listening for commands)");
89+
bot.postAsync(params.message("Monking! (Duga is now listening for commands)"));
9090
}
9191
/* Root node: {"ms":4,"time":41194973,"sync":1433551091,"events":
9292
[{"room_id":16134,"event_type":1,"time_stamp":1433547911,"user_id":125580,"user_name":"Duga","message_id":22039309,"content":"Loki Astari vs. Simon Andr&#233; Forsberg: 4383 diff. Year: -1368. Quarter: -69. Month: -5. Week: +60. Day: -25."}

grails-app/controllers/net/zomis/duga/GithubHookController.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ class GithubHookController {
2828
strings.forEach({ println it })
2929
String[] rooms = room.split(',')
3030
for (String postRoom : rooms) {
31-
bot.postChat(WebhookParameters.toRoom(postRoom), strings)
31+
WebhookParameters hookParams = WebhookParameters.toRoom(postRoom)
32+
bot.postChat(hookParams.messages(strings))
3233
}
3334
render 'OK'
3435
}

grails-app/controllers/net/zomis/duga/TravisHookController.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class TravisHookController {
9797
default:
9898
break;
9999
}
100-
chatBot.postChat(params, messages);
100+
chatBot.postChat(params.messages(messages));
101101
render 'OK'
102102
}
103103

grails-app/services/net/zomis/duga/DugaBotService.groovy

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,17 @@ class DugaBotService implements ChatBot, InitializingBean {
2121

2222
private StackExchangeChatBot bot
2323

24+
@Deprecated
2425
void postSingle(WebhookParameters params, String message) {
25-
this.postChat(params, [message])
26+
this.postAsync(params.message(message))
27+
}
28+
29+
@Override
30+
Future<List<ChatMessageResponse>> postChat(List<ChatMessage> messages) {
31+
messages.each {
32+
println "postChat $it to $it.room"
33+
}
34+
return bot.postChat(messages)
2635
}
2736

2837
@Override
@@ -55,14 +64,6 @@ class DugaBotService implements ChatBot, InitializingBean {
5564
bot.registerListener(eventClass, handler)
5665
}
5766

58-
@Override
59-
Future<List<ChatMessageResponse>> postChat(WebhookParameters params, List<String> messages) {
60-
messages.each {
61-
println "postChat $params: $it"
62-
}
63-
return bot.postMessages(params, messages)
64-
}
65-
6667
@Override
6768
void afterPropertiesSet() throws Exception {
6869
def config = new BotConfiguration()

src/main/groovy/net/zomis/duga/tasks/CommentsScanTask.groovy

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@ public class CommentsScanTask implements Runnable {
4545
def comments = stackAPI.fetchComments("stackoverflow", fromDate);
4646
int currentQuota = comments.quota_remaining
4747
if (currentQuota > remainingQuota && fromDate != 0) {
48-
chatBot.postSingle(debug, Instant.now().toString() + " Quota has been reset. Was " + remainingQuota + " is now " + currentQuota);
48+
chatBot.postAsync(debug.message(Instant.now().toString() + " Quota has been reset. Was " +
49+
remainingQuota + " is now " + currentQuota));
4950
}
5051
remainingQuota = currentQuota;
5152
List items = comments.items;
5253
if (items) {
5354
if (items.size() >= 100) {
54-
chatBot.postSingle(debug, Instant.now().toString() + " Warning: Retrieved 100 comments. Might have missed some.");
55+
chatBot.postAsync(debug.message(Instant.now().toString() +
56+
" Warning: Retrieved 100 comments. Might have missed some."));
5557
}
5658

5759
long previousLastComment = lastComment;
@@ -63,33 +65,36 @@ public class CommentsScanTask implements Runnable {
6365
lastComment = Math.max(comment.comment_id as long, lastComment);
6466
fromDate = Math.max(comment.creation_date as long, fromDate);
6567
if (isInterestingComment(comment)) {
66-
chatBot.postSingle(params, comment.link as String);
68+
chatBot.postAsync(params.message(comment.link as String));
6769
}
6870
float programmersCertainty = CommentClassification.calcInterestingLevelProgrammers(comment);
6971

7072
if (programmersCertainty >= CommentClassification.REAL) {
71-
chatBot.postSingle(programmers, comment.link as String);
73+
chatBot.postAsync(programmers.message(comment.link as String));
7274
}
7375
if (programmersCertainty >= CommentClassification.DEBUG) {
74-
chatBot.postSingle(debug, "Certainty level " + programmersCertainty);
75-
chatBot.postSingle(debug, comment.link as String);
76+
chatBot.postChat(Arrays.asList(
77+
debug.message("Certainty level " + programmersCertainty),
78+
debug.message(comment.link as String)
79+
));
7680
}
7781

7882
float softwareCertainty = CommentClassification.calcInterestingLevelSoftwareRecs(comment);
7983

8084
if (softwareCertainty >= CommentClassification.REAL) {
81-
chatBot.postSingle(softwareRecs, comment.link as String);
85+
chatBot.postAsync(softwareRecs.message(comment.link as String));
8286
}
8387
}
8488
items.clear();
8589
}
8690
if (comments.backoff) {
8791
nextFetch = Instant.now().plusSeconds((int) comments.backoff + 10);
88-
chatBot.postSingle(debug, Instant.now().toString() + " Next fetch: " + nextFetch + " because of backoff " + String.valueOf(comments.backoff));
92+
chatBot.postAsync(debug.message(Instant.now().toString() +
93+
" Next fetch: " + nextFetch + " because of backoff " + String.valueOf(comments.backoff)));
8994
}
9095
} catch (Exception e) {
9196
logger.error("Error retrieving comments", e);
92-
chatBot.postSingle(debug, Instant.now().toString() + " Exception in comment task " + e);
97+
chatBot.postAsync(debug.message(Instant.now().toString() + " Exception in comment task " + e));
9398
}
9499
}
95100

0 commit comments

Comments
 (0)