Skip to content

Commit e475b86

Browse files
committed
Allow open and close admin replies, and admin assigment
Send open or close admin replies, optionally with a body and allow conversations to be assigned to pther admings with some validation to check combinations of fields. Also while we're here, validate that a user reply has a message type of comment.
1 parent b6583fc commit e475b86

File tree

5 files changed

+125
-2
lines changed

5 files changed

+125
-2
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,12 @@ ConversationCollection openForAdmin = Conversation.list(params);
401401
Admin admin = new Admin().setId("1");
402402
AdminReply adminReply = new AdminReply(admin);
403403
adminReply.setBody("These apples are healthsome");
404+
Conversation.reply("66", adminReply);
405+
406+
// admin close
407+
Admin admin = new Admin().setId("1");
408+
AdminReply adminReply = new AdminReply(admin);
409+
adminReply.setMessageType("close");
404410
Conversation.reply("66", adminReply);
405411

406412
// user reply

intercom-java/src/main/java/io/intercom/api/AdminReply.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,34 @@ public String getBody() {
4141
public String getAdminID() {
4242
return reply.getFrom().getId();
4343
}
44+
45+
@JsonProperty("assignee_id")
46+
public String getAssigneeID() {
47+
return reply.getAssigneeID();
48+
}
4449
}
4550

51+
@JsonProperty("assignee_id")
52+
private String assigneeID;
53+
4654
public AdminReply(Admin admin) {
4755
this.from = admin;
4856
}
4957

58+
public Reply<Admin> setMessageType(String messageType) {
59+
return setMessageReplyType(messageType);
60+
}
61+
62+
public String getAssigneeID() {
63+
return assigneeID;
64+
}
65+
66+
public Reply<Admin> setAssigneeID(String assigneeID) {
67+
this.assigneeID = assigneeID;
68+
this.setMessageType("assign");
69+
return this;
70+
}
71+
5072
@Override
5173
public String toString() {
5274
return "AdminReply{} " + super.toString();

intercom-java/src/main/java/io/intercom/api/Conversation.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class Conversation extends TypedData {
1919

2020
private static final HashMap<String, String> SENTINEL = Maps.newHashMap();
2121
private static final List<String> DISPLAY_AS_FORMATS = Lists.newArrayList("plaintext", "html");
22+
private static final List<String> MESSAGE_TYPES = Lists.newArrayList("note", "comment", "assign", "open", "close");
2223

2324
public static Conversation find(String id) throws InvalidException, AuthorizationException {
2425
final HttpClient resource = new HttpClient(UriBuilder.newBuilder().path("conversations").path(id).build());
@@ -36,6 +37,8 @@ public static ConversationCollection list(Map<String, String> params) throws Inv
3637
}
3738

3839
public static Conversation reply(String id, UserReply reply) {
40+
validateUserReplyRequest(reply);
41+
3942
final URI uri = UriBuilder.newBuilder()
4043
.path("conversations")
4144
.path(id)
@@ -46,6 +49,8 @@ public static Conversation reply(String id, UserReply reply) {
4649
}
4750

4851
public static Conversation reply(String id, AdminReply reply) {
52+
validateAdminReplyRequest(reply);
53+
4954
final URI uri = UriBuilder.newBuilder()
5055
.path("conversations")
5156
.path(id)
@@ -89,6 +94,41 @@ public static AdminMessage create(AdminMessage message) throws InvalidException
8994
return response;
9095
}
9196

97+
static void validateAdminReplyRequest(AdminReply reply) {
98+
99+
validateMessageType(reply);
100+
101+
if (reply.getAssigneeID() != null
102+
&& !"assign".equals(reply.getMessageType())) {
103+
throw new InvalidException("an assignee id can be set only for a message type of assign");
104+
}
105+
106+
if (("note".equals(reply.getMessageType()) || "comment".equals(reply.getMessageType()))
107+
&& (isNullOrBlank(reply.getBody()))
108+
) {
109+
throw new InvalidException("a comment or note reply must have a body");
110+
}
111+
}
112+
113+
static boolean isNullOrBlank(String s) {
114+
return s == null || s.trim().length() == 0;
115+
116+
}
117+
118+
static void validateUserReplyRequest(UserReply reply) {
119+
if (! "comment".equals(reply.getMessageType())) {
120+
throw new InvalidException("a user reply must have a message type of comment");
121+
}
122+
}
123+
124+
static void validateMessageType(Reply reply) {
125+
if(! MESSAGE_TYPES.contains(reply.getMessageType())) {
126+
throw new InvalidException(
127+
"A reply message type must be one of "
128+
+ Joiner.on(", ").join(MESSAGE_TYPES));
129+
}
130+
}
131+
92132
static void validateListRequest(Map<String, String> params) {
93133
if (!params.containsKey("type")) {
94134
throw new InvalidException("a user or admin type must be supplied for a conversation query");

intercom-java/src/main/java/io/intercom/api/Reply.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
class Reply<T extends Replier> extends TypedData {
66

77
@JsonProperty("message_type")
8-
private final String messageType = "comment";
8+
private String messageType = "comment";
99

1010
@JsonProperty("body")
1111
private String body;
@@ -44,6 +44,11 @@ String getMessageType() {
4444
return messageType;
4545
}
4646

47+
Reply<T> setMessageReplyType(String messageType) {
48+
this.messageType = messageType;
49+
return this;
50+
}
51+
4752
@Override
4853
public boolean equals(Object o) {
4954
if (this == o) return true;

intercom-java/src/test/java/io/intercom/api/ConversationTest.java

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,61 @@
55

66
import java.util.Map;
77

8+
import static org.junit.Assert.*;
89
import static org.junit.Assert.assertTrue;
9-
import static org.junit.Assert.fail;
1010

1111
public class ConversationTest {
1212

13+
@Test
14+
public void testIsNullOrBlank() {
15+
assertTrue(Conversation.isNullOrBlank(null));
16+
assertTrue(Conversation.isNullOrBlank(""));
17+
assertTrue(Conversation.isNullOrBlank(" "));
18+
assertTrue(Conversation.isNullOrBlank("\n"));
19+
assertTrue(Conversation.isNullOrBlank("\r"));
20+
assertFalse(Conversation.isNullOrBlank("reply"));
21+
}
22+
23+
@Test
24+
public void testAdminReply() {
25+
26+
AdminReply adminReply = new AdminReply(null);
27+
adminReply.setAssigneeID("1");
28+
assertEquals("assign", adminReply.getMessageType());
29+
30+
try {
31+
Conversation.validateAdminReplyRequest(adminReply);
32+
} catch (InvalidException e) {
33+
fail();
34+
}
35+
36+
adminReply = new AdminReply(null);
37+
adminReply.setMessageType("comment");
38+
try {
39+
Conversation.validateAdminReplyRequest(adminReply);
40+
fail();
41+
} catch (InvalidException e) {
42+
assertTrue(e.getMessage()
43+
.contains("a comment or note reply must have a body"));
44+
}
45+
46+
adminReply.setBody(" ");
47+
try {
48+
Conversation.validateAdminReplyRequest(adminReply);
49+
fail();
50+
} catch (InvalidException e) {
51+
assertTrue(e.getMessage()
52+
.contains("a comment or note reply must have a body"));
53+
}
54+
55+
adminReply.setBody("Once, in flight school, I was laconic");
56+
try {
57+
Conversation.validateAdminReplyRequest(adminReply);
58+
} catch (InvalidException e) {
59+
fail();
60+
}
61+
}
62+
1363
@Test
1464
public void testDisplayAs() {
1565

0 commit comments

Comments
 (0)