Skip to content

Commit f857777

Browse files
committed
Change the way message replies are handled
SO Chat now lets you specify the parent ID of the message you are replying to in the HTTP POST parameters. This is what happens when you click "reply". Previously, the ID had to be specified at the beginning of the chat message. Monospace-formatted replies render properly when using the parameter.
1 parent 60ffad8 commit f857777

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+321
-285
lines changed

src/main/java/oakbot/bot/Bot.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,11 @@ private void sendMessage(IRoom room, PostMessage message) throws IOException {
271271
logger.atInfo().log(() -> "Sending message [room=" + room.getRoomId() + "]: " + filteredMessage);
272272

273273
synchronized (postedMessages) {
274-
var messageIds = room.sendMessage(filteredMessage, message.splitStrategy());
274+
var messageIds = room.sendMessage(filteredMessage, message.parentId(), message.splitStrategy());
275275
var condensedMessage = message.condensedMessage();
276276
var ephemeral = message.ephemeral();
277277

278-
var postedMessage = new PostedMessage(Instant.now(), filteredMessage, condensedMessage, ephemeral, room.getRoomId(), messageIds);
278+
var postedMessage = new PostedMessage(Instant.now(), filteredMessage, condensedMessage, ephemeral, room.getRoomId(), message.parentId(), messageIds);
279279
postedMessages.put(messageIds.get(0), postedMessage);
280280
}
281281
}
@@ -486,6 +486,7 @@ private static class PostedMessage {
486486
private final String condensedContent;
487487
private final boolean ephemeral;
488488
private final int roomId;
489+
private final long parentId;
489490
private final List<Long> messageIds;
490491

491492
/**
@@ -498,16 +499,18 @@ private static class PostedMessage {
498499
* @param ephemeral true to delete the message after the amount of time
499500
* specified in the "hideOneboxesAfter" setting, false not to
500501
* @param roomId the ID of the room the message was posted in
502+
* @param parentId the ID of the message that this was a reply to
501503
* @param messageIds the ID of each message that was actually posted to
502504
* the room (the chat client may split up the original message due to
503505
* length limitations)
504506
*/
505-
public PostedMessage(Instant timePosted, String originalContent, String condensedContent, boolean ephemeral, int roomId, List<Long> messageIds) {
507+
public PostedMessage(Instant timePosted, String originalContent, String condensedContent, boolean ephemeral, int roomId, long parentId, List<Long> messageIds) {
506508
this.timePosted = timePosted;
507509
this.originalContent = originalContent;
508510
this.condensedContent = condensedContent;
509511
this.ephemeral = ephemeral;
510512
this.roomId = roomId;
513+
this.parentId = parentId;
511514
this.messageIds = messageIds;
512515
}
513516

@@ -575,6 +578,14 @@ public boolean isCondensableOrEphemeral() {
575578
public boolean isEphemeral() {
576579
return ephemeral;
577580
}
581+
582+
/**
583+
* Gets the ID of the message that this was a reply to.
584+
* @return the parent ID or 0 if it's not a reply
585+
*/
586+
public long getParentId() {
587+
return parentId;
588+
}
578589
}
579590

580591
private abstract class Chore implements Comparable<Chore> {
@@ -945,7 +956,7 @@ public void complete() {
945956

946957
var messageIds = postedMessage.getMessageIds();
947958
var quotedContent = quote(condensedContent);
948-
room.editMessage(messageIds.get(0), quotedContent);
959+
room.editMessage(messageIds.get(0), postedMessage.getParentId(), quotedContent);
949960

950961
/*
951962
* If the original content was split up into
@@ -963,6 +974,7 @@ public void complete() {
963974
}
964975
}
965976

977+
@SuppressWarnings("deprecation")
966978
private String quote(String content) {
967979
var cb = new ChatBuilder();
968980

src/main/java/oakbot/bot/ChatActions.java

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.List;
66

77
import com.github.mangstadt.sochat4j.ChatMessage;
8+
import com.github.mangstadt.sochat4j.SplitStrategy;
89

910
import oakbot.util.ChatBuilder;
1011

@@ -53,11 +54,11 @@ public static ChatActions post(CharSequence message) {
5354
* @param parent the message to reply to
5455
* @return the created object
5556
*/
56-
public static ChatActions reply(String message, ChatMessage parent) {
57+
public static ChatActions reply(CharSequence message, long parentId) {
5758
//@formatter:off
58-
return post(new ChatBuilder()
59-
.reply(parent)
60-
.append(message)
59+
return ChatActions.create(
60+
new PostMessage(message)
61+
.parentId(parentId)
6162
);
6263
//@formatter:on
6364
}
@@ -69,10 +70,39 @@ public static ChatActions reply(String message, ChatMessage parent) {
6970
* @param parent the message to reply to
7071
* @return the created object
7172
*/
72-
public static ChatActions reply(String message, ChatCommand parent) {
73+
public static ChatActions reply(CharSequence message, ChatMessage parent) {
74+
return reply(message, parent.getMessageId());
75+
}
76+
77+
/**
78+
* Convenience method for creating a list with a single
79+
* {@link PostMessage} action that is a reply to a chat message.
80+
* @param message the message to post
81+
* @param parent the message to reply to
82+
* @return the created object
83+
*/
84+
public static ChatActions reply(CharSequence message, ChatCommand parent) {
7385
return reply(message, parent.getMessage());
7486
}
7587

88+
/**
89+
* Convenience method for creating a list with a single
90+
* {@link PostMessage} action that is a reply to a chat message.
91+
* @param message the message to post
92+
* @param parent the message to reply to
93+
* @param splitStrategy the split strategy
94+
* @return the created object
95+
*/
96+
public static ChatActions reply(CharSequence message, ChatCommand parent, SplitStrategy splitStrategy) {
97+
//@formatter:off
98+
return ChatActions.create(
99+
new PostMessage(message)
100+
.parentId(parent.getMessage().getMessageId())
101+
.splitStrategy(splitStrategy)
102+
);
103+
//@formatter:on
104+
}
105+
76106
/**
77107
* Convenience method for creating a list with a single {@link PostMessage}
78108
* action that is a reply to a chat message and that contains an exception
@@ -84,10 +114,9 @@ public static ChatActions reply(String message, ChatCommand parent) {
84114
*/
85115
public static ChatActions error(CharSequence message, Exception exception, ChatCommand parent) {
86116
//@formatter:off
87-
return post(new ChatBuilder()
88-
.reply(parent)
117+
return reply(new ChatBuilder()
89118
.append(message)
90-
.code(exception.getMessage())
119+
.code(exception.getMessage()), parent
91120
);
92121
//@formatter:on
93122
}

src/main/java/oakbot/bot/PostMessage.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
public class PostMessage implements ChatAction {
1313
private String message;
1414
private String condensedMessage;
15+
private long parentId;
1516
private SplitStrategy splitStrategy = SplitStrategy.NONE;
1617
private boolean bypassFilters;
1718
private boolean ephemeral;
@@ -76,6 +77,24 @@ public PostMessage condensedMessage(CharSequence condensedMessage) {
7677
return this;
7778
}
7879

80+
/**
81+
* Gets the ID of the message that is being replied to.
82+
* @return the parent ID or 0 if not set
83+
*/
84+
public long parentId() {
85+
return parentId;
86+
}
87+
88+
/**
89+
* Sets the ID of the message that is being replied to.
90+
* @param parentId the parent ID
91+
* @return this
92+
*/
93+
public PostMessage parentId(long parentId) {
94+
this.parentId = parentId;
95+
return this;
96+
}
97+
7998
/**
8099
* Determines how the message should be split up if it exceeds length
81100
* limitations. By default, the message will be truncated.
@@ -181,20 +200,20 @@ public Duration delay() {
181200

182201
@Override
183202
public int hashCode() {
184-
return Objects.hash(broadcast, bypassFilters, condensedMessage, delay, ephemeral, message, splitStrategy);
203+
return Objects.hash(broadcast, bypassFilters, condensedMessage, delay, ephemeral, message, parentId, splitStrategy);
185204
}
186205

187206
@Override
188207
public boolean equals(Object obj) {
189208
if (this == obj) return true;
190209
if (obj == null) return false;
191210
if (getClass() != obj.getClass()) return false;
192-
var other = (PostMessage) obj;
193-
return broadcast == other.broadcast && bypassFilters == other.bypassFilters && Objects.equals(condensedMessage, other.condensedMessage) && Objects.equals(delay, other.delay) && ephemeral == other.ephemeral && Objects.equals(message, other.message) && splitStrategy == other.splitStrategy;
211+
PostMessage other = (PostMessage) obj;
212+
return broadcast == other.broadcast && bypassFilters == other.bypassFilters && Objects.equals(condensedMessage, other.condensedMessage) && Objects.equals(delay, other.delay) && ephemeral == other.ephemeral && Objects.equals(message, other.message) && parentId == other.parentId && splitStrategy == other.splitStrategy;
194213
}
195214

196215
@Override
197216
public String toString() {
198-
return "PostMessage [message=" + message + ", condensedMessage=" + condensedMessage + ", splitStrategy=" + splitStrategy + ", bypassFilters=" + bypassFilters + ", ephemeral=" + ephemeral + ", broadcast=" + broadcast + ", delay=" + delay + "]";
217+
return "PostMessage [message=" + message + ", condensedMessage=" + condensedMessage + ", parentId=" + parentId + ", splitStrategy=" + splitStrategy + ", bypassFilters=" + bypassFilters + ", ephemeral=" + ephemeral + ", broadcast=" + broadcast + ", delay=" + delay + "]";
199218
}
200219
}

src/main/java/oakbot/chat/mock/FileChatRoom.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,7 @@ public List<ChatMessage> getMessages(int count) {
160160
}
161161

162162
@Override
163-
public long sendMessage(String message) {
164-
return sendMessage(message, null).get(0);
165-
}
166-
167-
@Override
168-
public List<Long> sendMessage(String message, SplitStrategy splitStragey) {
163+
public List<Long> sendMessage(String message, long parentId, SplitStrategy splitStragey) {
169164
var id = postMessage(botUserId, botUsername, message);
170165
return List.of(id);
171166
}
@@ -218,7 +213,7 @@ public void deleteMessage(long messageId) throws RoomNotFoundException, RoomPerm
218213
}
219214

220215
@Override
221-
public void editMessage(long messageId, String updatedMessage) throws RoomNotFoundException, RoomPermissionException, IOException {
216+
public void editMessage(long messageId, long parentId, String updatedMessage) throws RoomNotFoundException, RoomPermissionException, IOException {
222217
System.out.println(roomId + " > " + botUsername + " > (edited message " + messageId + "): " + updatedMessage);
223218
}
224219

src/main/java/oakbot/command/AfkCommand.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package oakbot.command;
22

33
import static oakbot.bot.ChatActions.doNothing;
4-
import static oakbot.bot.ChatActions.post;
54
import static oakbot.bot.ChatActions.reply;
65

76
import java.time.Duration;
@@ -82,7 +81,6 @@ public ChatActions onMessage(ChatMessage message, IBot bot) {
8281
var usersNotWarnedAbout = filterUsersNotWarnedAbout(mentionedAfkUsers, message.getUserId());
8382
if (!usersNotWarnedAbout.isEmpty()) {
8483
var cb = new ChatBuilder();
85-
cb.reply(message);
8684
var first = true;
8785
usersNotWarnedAbout.sort(Comparator.comparing(AfkUser::getUsername));
8886
for (var afkUser : usersNotWarnedAbout) {
@@ -100,7 +98,7 @@ public ChatActions onMessage(ChatMessage message, IBot bot) {
10098

10199
first = false;
102100
}
103-
return post(cb);
101+
return reply(cb, message);
104102
}
105103

106104
if (returned) {

src/main/java/oakbot/command/DeleteCommand.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import static oakbot.bot.ChatActions.create;
44
import static oakbot.bot.ChatActions.doNothing;
5-
import static oakbot.bot.ChatActions.post;
65
import static oakbot.bot.ChatActions.reply;
76

87
import java.util.List;
@@ -111,10 +110,9 @@ public ChatActions onMessage(ChatMessage message, IBot bot) {
111110
private ChatActions deleteAction(long messageToDelete, long messageToReplyToOnError) {
112111
//@formatter:off
113112
return create(new DeleteMessage(messageToDelete).onError(e ->
114-
post(new ChatBuilder()
115-
.reply(messageToReplyToOnError)
113+
reply(new ChatBuilder()
116114
.append("Unable to delete message: ")
117-
.code(e.getMessage())
115+
.code(e.getMessage()), messageToReplyToOnError
118116
)
119117
));
120118
//@formatter:on

src/main/java/oakbot/command/FatCatCommand.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ private ChatActions addCat(ChatCommand chatCommand, IBot bot, String cat) {
128128
if (cat == null) {
129129
//@formatter:off
130130
return post(new ChatBuilder()
131-
.reply(chatCommand)
132131
.append("Specify the URL of the cat you want to add: ")
133132
.code()
134133
.append(bot.getTrigger())
@@ -163,14 +162,13 @@ private ChatActions deleteCat(ChatCommand chatCommand, IBot bot, String cat) {
163162

164163
if (cat == null) {
165164
//@formatter:off
166-
return post(new ChatBuilder()
167-
.reply(chatCommand)
165+
return reply(new ChatBuilder()
168166
.append("Specify the URL of the cat you want to delete: ")
169167
.code()
170168
.append(bot.getTrigger())
171169
.append(name())
172170
.append(" delete URL")
173-
.code()
171+
.code(), chatCommand
174172
);
175173
//@formatter:on
176174
}

src/main/java/oakbot/command/HelpCommand.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,15 +264,10 @@ private ChatActions showHelpText(ChatCommand message, String trigger) {
264264
}
265265

266266
var cb = new ChatBuilder();
267-
cb.reply(message);
268267
for (var helpText : helpTexts) {
269268
cb.append(helpText).nl();
270269
}
271270

272-
//@formatter:off
273-
return ChatActions.create(
274-
new PostMessage(cb.toString().trim()).splitStrategy(SplitStrategy.NEWLINE)
275-
);
276-
//@formatter:on
271+
return reply(cb.toString().trim(), message, SplitStrategy.NEWLINE);
277272
}
278273
}

src/main/java/oakbot/command/RemindCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public ChatActions onMessage(ChatCommand chatCommand, IBot bot) {
5252

5353
//@formatter:off
5454
return ChatActions.create(
55-
new PostMessage(new ChatBuilder().reply(chatCommand).append("Created.")),
55+
new PostMessage(new ChatBuilder().append("Created.")).parentId(chatCommand.getMessage().getMessageId()),
5656
new PostMessage(new ChatBuilder().mention(mention).append(" ").append(reminder)).delay(duration)
5757
);
5858
//@formatter:on

src/main/java/oakbot/command/RollCommand.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package oakbot.command;
22

3-
import static oakbot.bot.ChatActions.post;
43
import static oakbot.bot.ChatActions.reply;
54

65
import java.util.Arrays;
@@ -52,9 +51,8 @@ public ChatActions onMessage(ChatCommand chatCommand, IBot bot) {
5251

5352
if (parameters.times <= 0) {
5453
//@formatter:off
55-
return post(new ChatBuilder()
56-
.reply(chatCommand)
57-
.italic("rolls nothing")
54+
return reply(new ChatBuilder()
55+
.italic("rolls nothing"), chatCommand
5856
);
5957
//@formatter:on
6058
}
@@ -70,7 +68,6 @@ public ChatActions onMessage(ChatCommand chatCommand, IBot bot) {
7068
//@formatter:on
7169

7270
var cb = new ChatBuilder();
73-
cb.reply(chatCommand);
7471

7572
//@formatter:off
7673
cb.append(Arrays.stream(results)
@@ -85,7 +82,7 @@ public ChatActions onMessage(ChatCommand chatCommand, IBot bot) {
8582
cb.nl().append("Average = ").append(Double.toString(average));
8683
}
8784

88-
return post(cb);
85+
return reply(cb, chatCommand);
8986
}
9087

9188
private Parameters parseParameters(ChatCommand chatCommand) {

0 commit comments

Comments
 (0)