Skip to content

Commit 1c4d92d

Browse files
authored
Merge pull request #28 from CSC207-2022F-UofT/conversation-history
Conversation History Interactor and Entities - Updated with Package Structure
2 parents 3784742 + 947192a commit 1c4d92d

File tree

10 files changed

+397
-0
lines changed

10 files changed

+397
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package entities.message;
2+
3+
import java.time.LocalDateTime;
4+
5+
/**
6+
* Message (abstract class) containing the message content and associated metadata
7+
* The type of the message content is dependent on which child(ren) of Message the message is
8+
* The metadata is universal for all message types and includes the ID of the sender, timestamp, ID of message
9+
*/
10+
public abstract class Message {
11+
/**
12+
* ID of message sender (UUID)
13+
*/
14+
private final String senderID;
15+
/**
16+
* Time message was sent
17+
*/
18+
private final LocalDateTime timestamp;
19+
/**
20+
* ID of message (UUID)
21+
*/
22+
private final String msgID;
23+
24+
/**
25+
* Construct a new message
26+
* @param senderID ID of sender
27+
* @param timestamp time message was sent
28+
* @param msgID ID of message
29+
*/
30+
public Message(String senderID, LocalDateTime timestamp, String msgID) {
31+
this.senderID = senderID;
32+
this.timestamp = timestamp;
33+
this.msgID = msgID;
34+
}
35+
36+
/**
37+
* Gets ID of sender
38+
* @return ID of sender
39+
*/
40+
public String getSenderID() {
41+
return senderID;
42+
}
43+
44+
/**
45+
* Gets message timestamp
46+
* @return message timestamp
47+
*/
48+
public LocalDateTime getTimestamp() {
49+
return timestamp;
50+
}
51+
52+
/**
53+
* Gets ID of message
54+
* @return ID of message
55+
*/
56+
public String getMsgID() {
57+
return msgID;
58+
}
59+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package entities.message;
2+
3+
import java.time.LocalDateTime;
4+
import java.util.UUID;
5+
6+
public class MsgFactory {
7+
private final String msgType;
8+
9+
public MsgFactory(String msgType) {
10+
this.msgType = msgType;
11+
}
12+
13+
public Message createMsg(String senderID, Object msgContent) {
14+
if (msgType.equalsIgnoreCase("text")) {
15+
return new TextMessage(senderID, (String) msgContent, LocalDateTime.now(), UUID.randomUUID().toString());
16+
// ID of message's sender, message text/content, timestamp of message, ID of message
17+
} else {
18+
System.out.println("A " + msgType.toLowerCase() + " is an undefined message type for this program.");
19+
// TODO: implement exception
20+
return null;
21+
}
22+
}
23+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package entities.message;
2+
3+
import java.time.LocalDateTime;
4+
5+
/**
6+
* A message with text as its content (child of Message)
7+
*/
8+
public class TextMessage extends Message {
9+
/**
10+
* Text content of message
11+
*/
12+
private String msgContent;
13+
14+
/**
15+
* Construct a text message
16+
* @param senderID ID of sender
17+
* @param msgContent text content
18+
* @param timestamp message timestamp
19+
* @param msgID ID of message
20+
*/
21+
public TextMessage(String senderID, String msgContent, LocalDateTime timestamp, String msgID) {
22+
super(senderID, timestamp, msgID);
23+
this.msgContent = msgContent;
24+
}
25+
26+
/**
27+
* Gets text content of a message
28+
* @return message content
29+
*/
30+
public String getMsgContent() {
31+
return msgContent;
32+
}
33+
34+
/**
35+
* Sets text content of a message
36+
* @param msgContent updated message content
37+
*/
38+
public void setMsgContent(String msgContent) {
39+
this.msgContent = msgContent;
40+
}
41+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package use_cases.conversation_history_use_case;
2+
3+
public class ConvHistDsRequestModel {
4+
private String userID;
5+
private String chatID;
6+
7+
public ConvHistDsRequestModel(String userID, String chatID) {
8+
this.userID = userID;
9+
this.chatID = chatID;
10+
}
11+
12+
public String getUserID() {
13+
return userID;
14+
}
15+
16+
public void setUserID(String userID) {
17+
this.userID = userID;
18+
}
19+
20+
public String getChatID() {
21+
return chatID;
22+
}
23+
24+
public void setChatID(String chatID) {
25+
this.chatID = chatID;
26+
}
27+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package use_cases.conversation_history_use_case;
2+
3+
import entities.message.Message;
4+
import entities.message.MsgFactory;
5+
import data_access.UserDatabase;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
/**
11+
* Interactor responsible for displaying this history upon opening a chat
12+
*/
13+
//public class ConvHistInteractor implements ConversationHistoryInputBoundary{
14+
public class ConvHistInteractor{
15+
/**
16+
* File and in-memory storage of users and their chats (incl. conversation history)
17+
*/
18+
final UserDatabase userDatabase;
19+
/**
20+
* Factory for creating a new Message
21+
*/
22+
final MsgFactory msgFactory;
23+
// /**
24+
// * Presenter with necessary information to display a chat's conversation history
25+
// */
26+
// final ConvHistPresenter convHistPresenter;
27+
28+
/**
29+
* Construct ConvHistInteractor given storage, message factory, and presenter
30+
* @param userDatabase storage
31+
* @param msgFactory message factory
32+
* //@param convHistPresenter presenter
33+
*/
34+
// public ConvHistInteractor(UserDatabase userDatabase, MsgFactory msgFactory, ConvHistPresenter convHistPresenter) {
35+
public ConvHistInteractor(UserDatabase userDatabase, MsgFactory msgFactory) {
36+
this.userDatabase = userDatabase;
37+
this.msgFactory = msgFactory; // msgType of MsgFactory specified in Main
38+
// this.convHistPresenter = convHistPresenter;
39+
}
40+
41+
/**
42+
* Displays conversation history upon opening a chat
43+
* @param requestModel input data
44+
* @return a response model for presenter
45+
*/
46+
// @Override
47+
public ConvHistResponseModel create(ConvHistRequestModel requestModel) {
48+
String userID = requestModel.getUserID();
49+
String chatID = requestModel.getChatID();
50+
51+
ConvHistDsRequestModel dsRequestModel = new ConvHistDsRequestModel(userID, chatID);
52+
53+
// Access database (code for database will become functional after PR for issue 15 is merged)
54+
// List<Message> conversationHistory = userDatabase.getConversationHistory(dsRequestModel);
55+
56+
// Presenter show success view (code to be written); below is temporary
57+
List<Message> conversationHistory = new ArrayList<>();
58+
return new ConvHistResponseModel(conversationHistory);
59+
}
60+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package use_cases.conversation_history_use_case;
2+
3+
public class ConvHistRequestModel {
4+
private String userID;
5+
private String chatID;
6+
7+
public ConvHistRequestModel(String userID, String chatID) {
8+
this.userID = userID;
9+
this.chatID = chatID;
10+
}
11+
12+
public String getUserID() {
13+
return userID;
14+
}
15+
16+
public void setUserID(String userID) {
17+
this.userID = userID;
18+
}
19+
20+
public String getChatID() {
21+
return chatID;
22+
}
23+
24+
public void setChatID(String chatID) {
25+
this.chatID = chatID;
26+
}
27+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package use_cases.conversation_history_use_case;
2+
3+
import entities.message.Message;
4+
5+
import java.util.List;
6+
7+
public class ConvHistResponseModel {
8+
private List<Message> conversationHistory;
9+
public ConvHistResponseModel(List<Message> conversationHistory) {
10+
this.conversationHistory = conversationHistory;
11+
}
12+
13+
public List<Message> getConversationHistory() {
14+
return conversationHistory;
15+
}
16+
17+
public void setConversationHistory(List<Message> conversationHistory) {
18+
this.conversationHistory = conversationHistory;
19+
}
20+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package use_cases.conversation_history_use_case;
2+
3+
import entities.message.Message;
4+
5+
public class MsgSenderDsRequestModel {
6+
private String userID;
7+
private String chatID;
8+
private Message message;
9+
10+
public MsgSenderDsRequestModel(String userID, String chatID, Message message) {
11+
this.userID = userID;
12+
this.chatID = chatID;
13+
this.message = message;
14+
}
15+
16+
public String getUserID() {
17+
return userID;
18+
}
19+
20+
public void setUserID(String userID) {
21+
this.userID = userID;
22+
}
23+
24+
public String getChatID() {
25+
return chatID;
26+
}
27+
28+
public void setChatID(String chatID) {
29+
this.chatID = chatID;
30+
}
31+
32+
public Message getMessage() {
33+
return message;
34+
}
35+
36+
public void setMessage(Message message) {
37+
this.message = message;
38+
}
39+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package use_cases.conversation_history_use_case;
2+
3+
import entities.message.Message;
4+
import entities.message.MsgFactory;
5+
import data_access.UserDatabase;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
/**
11+
* Interactor responsible for adding messages to a chat's conversation history
12+
*/
13+
//public class ConvHistInteractor implements ConversationHistoryInputBoundary{
14+
public class MsgSenderInteractor {
15+
/**
16+
* File and in-memory storage of users and their chats (incl. conversation history)
17+
*/
18+
final UserDatabase userDatabase;
19+
/**
20+
* Factory for creating a new Message
21+
*/
22+
final MsgFactory msgFactory;
23+
// /**
24+
// * Presenter with necessary information to display a chat's conversation history
25+
// */
26+
// final ConvHistPresenter convHistPresenter;
27+
28+
/**
29+
* Construct ConvHistInteractor given storage, message factory, and presenter
30+
* @param userDatabase storage
31+
* @param msgFactory message factory
32+
* //@param convHistPresenter presenter
33+
*/
34+
// public ConvHistInteractor(UserDatabase userDatabase, MsgFactory msgFactory, ConvHistPresenter convHistPresenter) {
35+
public MsgSenderInteractor(UserDatabase userDatabase, MsgFactory msgFactory) {
36+
this.userDatabase = userDatabase;
37+
this.msgFactory = msgFactory; // msgType of MsgFactory specified in Main
38+
// this.convHistPresenter = convHistPresenter;
39+
}
40+
41+
/**
42+
* Creates and adds message to a chat's conversation history
43+
* @param requestModel input data
44+
* @return a response model for presenter
45+
*/
46+
// @Override
47+
public ConvHistResponseModel create(MsgSenderRequestModel requestModel) {
48+
// Create new message
49+
Message message = msgFactory.createMsg(requestModel.getSenderID(), requestModel.getMsgContent());
50+
51+
// Add message to specified chat in user list
52+
String userID = requestModel.getSenderID(); // need a user in the chat to get the chat
53+
String chatID = requestModel.getChatID();
54+
55+
MsgSenderDsRequestModel dsRequestModel = new MsgSenderDsRequestModel(userID, chatID, message);
56+
57+
// Access database (code for database will become functional after PR for issue 15 is merged)
58+
// List<Message> conversationHistory = userDatabase.saveMessage(dsRequestModel);
59+
60+
// Presenter show success view (code to be written); below is temporary
61+
List<Message> conversationHistory = new ArrayList<>();
62+
return new ConvHistResponseModel(conversationHistory);
63+
}
64+
}

0 commit comments

Comments
 (0)