Browse Source

Send messages to timeline and individual

Lauren Green 6 years ago
parent
commit
c65dff8ba9

+ 7
- 3
Client/src/main/java/Message.java View File

@@ -7,9 +7,9 @@ public class Message {
7 7
     String message;
8 8
 
9 9
 
10
-    public Message(Id myId, Id theirId, String message) {
11
-        this.fromId = myId.getGithub();
12
-        this.toId = theirId.getGithub();
10
+    public Message(String myId, String theirId, String message) {
11
+        this.fromId = myId;
12
+        this.toId = theirId;
13 13
         this.message = message;
14 14
 
15 15
     }
@@ -63,4 +63,8 @@ public class Message {
63 63
                 ", message='" + message + '\'' +
64 64
                 '}';
65 65
       }
66
+
67
+    public String toJSON() {
68
+        return "{\"fromid\":\"" + fromId + "\", \"toid\":\"" + toId + "\", \"message\":\"" + message + "\"}";
69
+    }
66 70
 }

+ 41
- 2
Client/src/main/java/SimpleShell.java View File

@@ -77,11 +77,12 @@ public class SimpleShell {
77 77
                 }
78 78
 
79 79
                 // messages
80
-                if (list.contains("messages")) {
80
+                if (list.contains("messages") && list.size() == 1) {
81 81
                     System.out.println(webber.get_messages());
82 82
                     continue;
83 83
                 }
84 84
 
85
+                //Post new user
85 86
                 if (list.contains("ids") && list.size() > 1 && !webber.githubIdExists(list.get(2))) {
86 87
                     Id id = new Id(list.get(1), list.get(2));
87 88
                     webber.postNewUser(id);
@@ -93,6 +94,7 @@ public class SimpleShell {
93 94
                     continue;
94 95
                 }
95 96
 
97
+                //Update user name
96 98
                 if (list.contains("ids") && list.size() > 1 && webber.githubIdExists(list.get(2))) {
97 99
                     Id id = new Id(list.get(1), list.get(2));
98 100
                     webber.putNewName(id);
@@ -103,7 +105,44 @@ public class SimpleShell {
103 105
                     }
104 106
                     continue;
105 107
                 }
106
-                // you need to add a bunch more.
108
+
109
+                //get my messages
110
+                if (list.contains("messages") && list.size() == 2) {
111
+                    System.out.println(webber.getMyMessages(list.get(1)));
112
+                    continue;
113
+                }
114
+
115
+                //send a message to timeline
116
+                if (list.contains("send") && !list.contains("to")) {
117
+                    String phrase = "";
118
+                    for (int i = 2; i < list.size(); i++) {
119
+                        phrase += list.get(i);
120
+                    }
121
+                    Message message = new Message(list.get(1), "", phrase);
122
+                    webber.sendMessage(message);
123
+                    if(webber.messageExists(phrase)) {
124
+                        System.out.println("Message posted to timeline.");
125
+                    } else {
126
+                        System.out.println("Error posting message.");
127
+                    }
128
+                    continue;
129
+                }
130
+
131
+                //send a message to someone
132
+                if (list.contains("send") && list.contains("to")) {
133
+                    String phrase = "";
134
+                    for (int i = 2; i < list.size() - 2; i++) {
135
+                        phrase += list.get(i);
136
+                    }
137
+                    Message message = new Message(list.get(1), list.get(list.size() - 1), phrase);
138
+                    webber.sendMessage(message);
139
+                    if(webber.messageExists(phrase)) {
140
+                        System.out.println("Message posted to: " + list.get(list.size() - 1));
141
+                    } else {
142
+                        System.out.println("Error posting message to: " + list.get(list.size() - 1));
143
+                    }
144
+                    continue;
145
+                }
107 146
 
108 147
                 //!! command returns the last command in history
109 148
                 if (list.get(list.size() - 1).equals("!!")) {

+ 28
- 0
Client/src/main/java/YouAreEll.java View File

@@ -76,4 +76,32 @@ public class YouAreEll {
76 76
     }
77 77
 
78 78
 
79
+    public String getMyMessages(String githubId) {
80
+        try {
81
+            HttpResponse<JsonNode> jsonResponse = Unirest.get(underARock + "/ids/" + githubId + "/messages")
82
+                    .header("accept", "application/json")
83
+                    .asJson();
84
+
85
+            return SimpleShell.prettyPrint(jsonResponse.getBody());
86
+
87
+        } catch (UnirestException e) {
88
+            return "Error";
89
+        }
90
+    }
91
+
92
+    public boolean messageExists(String message) {
93
+        return get_messages().contains(message);
94
+    }
95
+
96
+    public void sendMessage(Message message) {
97
+        try {
98
+            Unirest.post(underARock + "/ids/" + message.getFromId() + "/messages")
99
+                    .body(message.toJSON())
100
+                    .asJson();
101
+
102
+        } catch (UnirestException e) {
103
+            System.out.println("Error");
104
+        }
105
+
106
+    }
79 107
 }