forked from pruet/DNWS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwitterPlugin.cs
More file actions
398 lines (385 loc) · 14.7 KB
/
Copy pathTwitterPlugin.cs
File metadata and controls
398 lines (385 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using System;
using System.ComponentModel.DataAnnotations.Schema;
namespace DNWS
{
class Following
{
public int FollowingId { get; set; }
public string Name { get; set; }
}
class User
{
public int UserId { get; set; }
public string Name { get; set; }
public string Password { get; set; }
public List<Following> Following { get; set; } // Bug in SQLite implemention in EF7, no FK!
}
class Tweet
{
public int TweetId { get; set; }
public string Message { get; set; }
public string User { get; set; } // Bug in SQLite implemention in EF7, no FK!
public DateTime DateCreated { get; set; }
}
// Ref: https://docs.microsoft.com/en-us/ef/core/get-started/netcore/new-db-sqlite
class TweetContext : DbContext
{
public DbSet<Tweet> Tweets { get; set; }
public DbSet<User> Users { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source=tweet.db");
}
}
class Twitter
{
User user;
public Twitter(string name)
{
if (name == null || name == "")
{
throw new Exception("User name is required");
}
user = GetUser(name);
}
public string GetUsername()
{
return user.Name;
}
public static void RemoveUser(){
if (user == null)
{
throw new Exception("User is not set");
}
using(var newContext = new TweetContext){
list<User> userlist = newContext.Users.Where(b => b.Name.Equals(user)).ToList();
if(userlist.Count <= 0){
throw new Exception("User not found");
}
newContext.Users.Remove(userlist[0]);
newContext.SaveChanges();
}
}
public void RemoveFollowing(string followingName)
{
if (user == null)
{
throw new Exception("User is not set");
}
if (followingName == null)
{
throw new Exception("Following not found");
}
using (var context = new TweetContext())
{
Following following = new Following();
following.Name = followingName;
user.Following.Remove(following);
context.Users.Update(user);
context.SaveChanges();
}
}
public void AddFollowing(string followingName)
{
if (user == null)
{
throw new Exception("User is not set");
}
if (followingName == null)
{
throw new Exception("Following not found");
}
using (var context = new TweetContext())
{
if (user.Following == null)
{
user.Following = new List<Following>();
}
List<Following> followings = user.Following.Where(b => b.Name == followingName).ToList();
if (followings.Count > 0) return;
Following following = new Following();
following.Name = followingName;
user.Following.Add(following);
context.Users.Update(user);
context.SaveChanges();
}
}
public List<Tweet> GetTimeline(User aUser)
{
if (aUser == null)
{
throw new Exception("User is not set");
}
List<Tweet> timeline;
using (var context = new TweetContext())
{
timeline = context.Tweets.Where(b => b.User.Equals(aUser.Name)).OrderBy(b => b.DateCreated).ToList();
}
return timeline;
}
public List<Tweet> GetUserTimeline()
{
if (user == null)
{
throw new Exception("User is not set");
}
return GetTimeline(user);
}
public List<Tweet> GetFollowingTimeline()
{
if (user == null)
{
throw new Exception("User is not set");
}
List<Tweet> timeline = new List<Tweet>();
using (var context = new TweetContext())
{
List<Following> followings = user.Following;
if (followings == null || followings.Count == 0)
{
return null;
}
foreach (Following following in followings)
{
User followingUser = GetUser(following.Name);
timeline.AddRange(GetTimeline(followingUser));
}
}
timeline = timeline.OrderBy(b => b.DateCreated).ToList();
return timeline;
}
public List<Following> GetFollowing(){
if(user == null){
throw new Exception("User is not set");
}
using (var newContext = new TweetContext()){
List<Following> following = user.Following;
if( following == null || following.Count == 0){
return null;
}
return following;
}
}//taught by 600611001
public void PostTweet(string message)
{
if (user == null)
{
throw new Exception("User is not set");
}
Tweet tweet = new Tweet();
tweet.User = user.Name;
tweet.Message = message;
tweet.DateCreated = DateTime.Now;
using (var context = new TweetContext())
{
context.Tweets.Add(tweet);
context.SaveChanges();
}
}
public static void AddUser(string name, string password)
{
User user = new User();
user.Name = name;
user.Password = password;
using (var context = new TweetContext())
{
List<User> userlist = context.Users.Where(b => b.Name.Equals(name)).ToList();
if (userlist.Count > 0)
{
throw new Exception("User already exists");
}
context.Users.Add(user);
context.SaveChanges();
}
}
public static bool IsValidUser(string name, string password)
{
using (var context = new TweetContext())
{
List<User> userlist = context.Users.Where(b => b.Name.Equals(name) && b.Password.Equals(password)).ToList();
if (userlist.Count == 1)
{
return true;
}
}
return false;
}
private User GetUser(string name)
{
using (var context = new TweetContext())
{
try
{
List<User> users = context.Users.Where(b => b.Name.Equals(name)).Include(b => b.Following).ToList();
return users[0];
}
catch (Exception)
{
return null;
}
}
}
}
public class TwitterPlugin : IPlugin
{
public HTTPResponse PostProcessing(HTTPResponse response)
{
throw new NotImplementedException();
}
public void PreProcessing(HTTPRequest request)
{
throw new NotImplementedException();
}
private StringBuilder GenTimeline(Twitter twitter, StringBuilder sb)
{
sb.Append("Say something<br />");
sb.Append("<form method=\"post\">");
sb.Append("<input type=\"text\" name=\"message\"></input>");
sb.Append("<input type=\"submit\" name=\"action\" value=\"tweet\" /> <br />");
sb.Append("</form>");
sb.Append("Follow someone<br />");
sb.Append("<form method=\"post\">");
sb.Append("<input type=\"text\" name=\"following\"></input>");
sb.Append("<input type=\"submit\" name=\"action\" value=\"following\" /> <br />");
sb.Append("</form>");
sb.Append(String.Format("<h3><b>{0}</b>'s timeline</h3><br />", twitter.GetUsername()));
List<Tweet> tweets = twitter.GetUserTimeline();
foreach (Tweet tweet in tweets)
{
sb.Append("[" + tweet.DateCreated + "]" + tweet.User + ":" + tweet.Message + "<br />");
}
sb.Append("<br /><br />");
sb.Append("<h3>Following timeline</h3><br />");
tweets = twitter.GetFollowingTimeline();
if (tweets == null)
{
sb.Append("Your following list is empty, follow someone!");
}
else
{
foreach (Tweet tweet in tweets)
{
sb.Append("[" + tweet.DateCreated + "] " + tweet.User + ":" + tweet.Message + "<br />");
}
}
return sb;
}
protected StringBuilder GenLoginPage(StringBuilder sb)
{
sb.Append("<h2>Login</h2>");
sb.Append("<form method=\"get\">");
sb.Append("Username: <input type=\"text\" name=\"user\" value=\"\" /> <br />");
sb.Append("Password: <input type=\"password\" name=\"password\" value=\"\" /> <br />");
sb.Append("<input type=\"submit\" name=\"action\" value=\"login\" /> <br />");
sb.Append("</form>");
sb.Append("<br /><br /><br />");
sb.Append("<h2>New user</h2>");
sb.Append("<form method=\"get\">");
sb.Append("Username: <input type=\"text\" name=\"user\" value=\"\" /> <br />");
sb.Append("Password: <input type=\"password\" name=\"password\" value=\"\" /> <br />");
sb.Append("<input type=\"submit\" name=\"action\" value=\"newuser\" /> <br />");
sb.Append("</form>");
return sb;
}
public virtual HTTPResponse GetResponse(HTTPRequest request)//taught by 600611001
{
HTTPResponse response = new HTTPResponse(200);
StringBuilder sb = new StringBuilder();
string user = request.getRequestByKey("user");
string password = request.getRequestByKey("password");
string action = request.getRequestByKey("action");
string following = request.getRequestByKey("following");
string message = request.getRequestByKey("message");
if (user == null) // no user? show login screen
{
sb.Append("<h1>Twitter</h1>");
sb = GenLoginPage(sb);
}
else
{
if (action == null) // No action? go to homepage
{
try
{
Twitter twitter = new Twitter(user);
sb.Append(String.Format("<h1>{0}'s Twitter</h1>", user));
sb = GenTimeline(twitter, sb);
}
catch (Exception ex)
{
sb.Append(String.Format("Error [{0}], please go back to <a href=\"/twitter\">login page</a> to try again", ex.Message));
}
}
else
{
if (action.Equals("newuser"))
{
if (user != null && password != null && user != "" && password != "")
{
try
{
Twitter.AddUser(user, password);
sb.Append("User added successfully, please go back to <a href=\"/twitter\">login page</a> to login");
}
catch (Exception ex)
{
sb.Append(String.Format("Error adding user with error [{0}], please go back to <a href=\"/twitter\">login page</a> to try again", ex.Message));
}
}
}
else if (action.Equals("login"))
{
if (user != null && password != null && user != "" && password != "")
{
if (Twitter.IsValidUser(user, password))
{
sb.Append(String.Format("Welcome {0}, please go back to <a href=\"/twitter?user={0}\">tweet page</a> to begin", user));
}
else
{
sb.Append("Error login, please go back to <a href=\"/twitter\">login page</a> to try again");
}
}
}
else
{
Twitter twitter = new Twitter(user);
sb.Append(String.Format("<h1>{0}'s Twitter</h1>", user));
if (action.Equals("following"))
{
try
{
twitter.AddFollowing(following);
sb = GenTimeline(twitter, sb);
}
catch (Exception ex)
{
sb.Append(String.Format("Error [{0}], please go back to <a href=\"/twitter\">login page</a> to try again", ex.Message));
}
}
else if (action.Equals("tweet"))
{
try
{
twitter.PostTweet(message);
sb = GenTimeline(twitter, sb);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
sb.Append(String.Format("Error [{0}], please go back to <a href=\"/twitter\">login page</a> to try again", ex.Message));
}
}
}
}
}
response.body = Encoding.UTF8.GetBytes(sb.ToString());
return response;
}
}
}