-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.fs
More file actions
530 lines (463 loc) · 17.4 KB
/
Copy pathProgram.fs
File metadata and controls
530 lines (463 loc) · 17.4 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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
open System
open System.Collections.Generic
open Akka.Actor
open Akka.Configuration
open Akka.FSharp
open Suave
open Suave.Filters
open Suave.Operators
open Suave.Successful
open Suave.ServerErrors
open Suave.Writers
open Newtonsoft.Json
open Suave.Sockets
open Suave.Sockets.Control
open Suave.WebSocket
let system = ActorSystem.Create("TwitterEngine")
let setCORSHeaders =
setHeader "Access-Control-Allow-Origin" "*"
>=> setHeader "Access-Control-Allow-Headers" "content-type"
let mutable users = Map.empty
let mutable activeUsers = Map.empty
let mutable tweetOwner = Map.empty
let mutable followers = Map.empty
let mutable mentions = Map.empty
let mutable hashTags = Map.empty
let mutable websockmap = Map.empty
type NewAnswer =
{
Text: string
}
type Answer =
{
Text: string
AnswerId: int
}
type RespMsg =
{
Comment: string
Content: list<string>
status: int
error: bool
}
type Register =
{
UserName: string
Password: string
}
type Login =
{
UserName: string
Password: string
}
type Logout =
{
UserName: string
}
type Follower =
{
UserName: string
Following: string
}
type NewTweet =
{
Tweet: string
UserName: string
}
let buildByteResponseToWS (message:string) =
message
|> System.Text.Encoding.ASCII.GetBytes
|> ByteSegment
let addUser (user: Register) =
let temp = users.TryFind(user.UserName)
if temp = None then
users <- users.Add(user.UserName,user.Password)
{Comment = "Sucessfully registered! Please login!!";Content=[];status=1;error=false}
else
{Comment = "Please Try Again!! The entered user exists!";Content=[];status=1;error=true}
let loginuser (user: Login) =
printfn "Received Request to login from %s as %A" user.UserName user
let temp = users.TryFind(user.UserName)
if temp = None then
{Comment = "Please Register! The user mentioned is not registered!";Content=[];status=0;error=true}
else
if temp.Value.CompareTo(user.Password) = 0 then
let temp1 = activeUsers.TryFind(user.UserName)
if temp1 = None then
activeUsers <- activeUsers.Add(user.UserName,true)
{Comment = "You have entered the Twitter Application!!";Content=[];status=2;error=false}
else
{Comment = "User logged in";Content=[];status=2;error=true}
else
{Comment = "Password is incorrect!";Content=[];status=1;error=true}
let logoutuser (user:Logout) =
printfn "Request for logout received from %s as %A" user.UserName user
let temp = users.TryFind(user.UserName)
if temp = None then
{Comment = "Please Register! The user mentioned is not registered!";Content=[];status=0;error=true}
else
let temp1 = activeUsers.TryFind(user.UserName)
if temp1 = None then
{Comment = "USer has not logged in!";Content=[];status=1;error=true}
else
activeUsers <- activeUsers.Remove(user.UserName)
{Comment = "Logout Sucessful!!";Content=[];status=1;error=false}
let isUserLoggedIn username =
let temp = activeUsers.TryFind(username)
if temp <> None then
1
else
let temp1 = users.TryFind(username)
if temp1 = None then
-1
else
0
let checkUserExsistance username =
let temp = users.TryFind(username)
temp <> None
type LiveUserHandlerMsg =
| SendTweet of WebSocket*NewTweet
| SendMention of WebSocket* NewTweet
| SelfTweet of WebSocket * NewTweet
| Following of WebSocket * string
let liveUserHandler (mailbox:Actor<_>) =
let rec loop() = actor{
let! msg = mailbox.Receive()
match msg with
|SelfTweet(ws,tweet)-> let response = "You have tweeted '"+tweet.Tweet+"'"
let byteResponse = buildByteResponseToWS response
let s =socket{
do! ws.send Text byteResponse true
}
Async.StartAsTask s |> ignore
|SendTweet(ws,tweet)->
let response = tweet.UserName+" has tweeted '"+tweet.Tweet+"'"
let byteResponse = buildByteResponseToWS response
let s =socket{
do! ws.send Text byteResponse true
}
Async.StartAsTask s |> ignore
|SendMention(ws,tweet)->
let response = tweet.UserName+" mentioned you in tweet '"+tweet.Tweet+"'"
let byteResponse = buildByteResponseToWS response
let s =socket{
do! ws.send Text byteResponse true
}
Async.StartAsTask s |> ignore
|Following(ws,msg)->
let response = msg
let byteResponse = buildByteResponseToWS response
let s =socket{
do! ws.send Text byteResponse true
}
Async.StartAsTask s |> ignore
return! loop()
}
loop()
let liveUserHandlerRef = spawn system "luref" liveUserHandler
let tweetParser (tweet:NewTweet) =
let splits = (tweet.Tweet.Split ' ')
for i in splits do
if i.StartsWith "@" then
let temp = i.Split '@'
if checkUserExsistance temp.[1] then
let temp1 = mentions.TryFind(temp.[1])
if temp1 = None then
let mutable mp = Map.empty
let tlist = new List<string>()
tlist.Add(tweet.Tweet)
mp <- mp.Add(tweet.UserName,tlist)
mentions <- mentions.Add(temp.[1],mp)
else
let temp2 = temp1.Value.TryFind(tweet.UserName)
if temp2 = None then
let tlist = new List<string>()
tlist.Add(tweet.Tweet)
let mutable mp = temp1.Value
mp <- mp.Add(tweet.UserName,tlist)
mentions <- mentions.Add(temp.[1],mp)
else
temp2.Value.Add(tweet.Tweet)
let temp3 = websockmap.TryFind(temp.[1])
if temp3<>None then
liveUserHandlerRef <! SendMention(temp3.Value,tweet)
elif i.StartsWith "#" then
let temp1 = i.Split '#'
let temp = hashTags.TryFind(temp1.[1])
if temp = None then
let lst = List<string>()
lst.Add(tweet.Tweet)
hashTags <- hashTags.Add(temp1.[1],lst)
else
temp.Value.Add(tweet.Tweet)
let addFollower (follower: Follower) =
printfn "Request for subscribe received from %s as %A" follower.UserName follower
let status = isUserLoggedIn follower.UserName
if status = 1 then
if (checkUserExsistance follower.Following) then
let temp = followers.TryFind(follower.Following)
let temp1 = websockmap.TryFind(follower.UserName)
if temp = None then
let lst = new List<string>()
lst.Add(follower.UserName)
followers <- followers.Add(follower.Following,lst)
if temp1 <> None then
liveUserHandlerRef <! Following(temp1.Value,"You have sucessfully been added as a subscriber to "+follower.Following)
{Comment = "Subscriber list is updated!";Content=[];status=2;error=false}
else
if temp.Value.Exists( fun x -> x.CompareTo(follower.UserName) = 0 ) then
if temp1 <> None then
liveUserHandlerRef <! Following(temp1.Value,"You already been subscribed to: "+follower.Following)
{Comment = "You already been subscribed to: "+follower.Following;Content=[];status=2;error=true}
else
temp.Value.Add(follower.UserName)
if temp1 <> None then
liveUserHandlerRef <! Following(temp1.Value,"You are now following: "+follower.Following)
{Comment = "Subscriber list is updated!";Content=[];status=2;error=false}
else
{Comment = "Subscriber "+follower.Following+" doesn't exist";Content=[];status=2;error=true}
elif status = 0 then
{Comment = "Login to the application!!";Content=[];status=1;error=true}
else
{Comment = "Please Register!! Couldn't find User!";Content=[];status=0;error=true}
let addTweet (tweet: NewTweet) =
let temp = tweetOwner.TryFind(tweet.UserName)
if temp = None then
let lst = new List<string>()
lst.Add(tweet.Tweet)
tweetOwner <- tweetOwner.Add(tweet.UserName,lst)
else
temp.Value.Add(tweet.Tweet)
let addTweetToFollowers (tweet: NewTweet) =
let temp = followers.TryFind(tweet.UserName)
if temp <> None then
for i in temp.Value do
let temp1 = {Tweet=tweet.Tweet;UserName=i}
addTweet temp1
let temp2 = websockmap.TryFind(i)
printfn "%s" i
if temp2 <> None then
liveUserHandlerRef <! SendTweet(temp2.Value,tweet)
type tweetHandlerMsg =
| AddTweetMsg of NewTweet
| AddTweetToFollowersMsg of NewTweet
| TweetParserMsg of NewTweet
let tweetHandler (mailbox:Actor<_>) =
let rec loop() = actor{
let! msg = mailbox.Receive()
match msg with
| AddTweetMsg(tweet) -> addTweet(tweet)
let temp = websockmap.TryFind(tweet.UserName)
if temp <> None then
liveUserHandlerRef <! SelfTweet(temp.Value,tweet)
| AddTweetToFollowersMsg(tweet) -> addTweetToFollowers(tweet)
| TweetParserMsg(tweet) -> tweetParser(tweet)
return! loop()
}
loop()
let tweetHandlerRef = spawn system "thref" tweetHandler
let addTweetToUser (tweet: NewTweet) =
let status = isUserLoggedIn tweet.UserName
if status = 1 then
tweetHandlerRef <! AddTweetMsg(tweet)
// addTweet tweet
tweetHandlerRef <! AddTweetToFollowersMsg(tweet)
// addTweetToFollowers tweet
tweetHandlerRef <! TweetParserMsg(tweet)
{Comment = "Sent tweet sucessfully!!";Content=[];status=2;error=false}
elif status = 0 then
{Comment = "Please Login";Content=[];status=1;error=true}
else
{Comment = "Please Register!! Couldn't find User!";Content=[];status=0;error=true}
let getTweets username =
let status = isUserLoggedIn username
if status = 1 then
let temp = tweetOwner.TryFind(username)
if temp = None then
{Comment = "Tweet history empty! Please go Head and tweet!!";Content=[];status=2;error=false}
else
let len = Math.Min(10,temp.Value.Count)
let res = [for i in 1 .. len do yield(temp.Value.[i-1])]
{Comment = "User tweets task sucessfully executed!!";Content=res;status=2;error=false}
elif status = 0 then
{Comment = "Login to the application!!";Content=[];status=1;error=true}
else
{Comment = "Please Register!! Couldn't find User!";Content=[];status=0;error=true}
let getMentions username =
let status = isUserLoggedIn username
if status = 1 then
let temp = mentions.TryFind(username)
if temp = None then
{Comment = "No Mentions yet!!";Content=[];status=2;error=false}
else
let res = new List<string>()
for i in temp.Value do
for j in i.Value do
res.Add(j)
let len = Math.Min(10,res.Count)
let res1 = [for i in 1 .. len do yield(res.[i-1])]
{Comment = "Get Mentions done Succesfully";Content=res1;status=2;error=false}
elif status = 0 then
{Comment = "Please Login";Content=[];status=1;error=true}
else
{Comment = "User Doesn't Exsist!!Please Register";Content=[];status=0;error=true}
let getHashTags username hashtag =
let status = isUserLoggedIn username
if status = 1 then
printf "%s" hashtag
let temp = hashTags.TryFind(hashtag)
if temp = None then
{Comment = "No Tweets with this hashtag found";Content=[];status=2;error=false}
else
let len = Math.Min(10,temp.Value.Count)
let res = [for i in 1 .. len do yield(temp.Value.[i-1])]
{Comment = "Get Hashtags done Succesfully";Content=res;status=2;error=false}
elif status = 0 then
{Comment = "Please Login";Content=[];status=1;error=true}
else
{Comment = "Please Register!! Couldn't find User!";Content=[];status=0;error=true}
let registerNewUser (user: Register) =
printfn "Request to register from %s as %A" user.UserName user
addUser user
let respTweet (tweet: NewTweet) =
printfn "Sending tweet request received from %s as %A" tweet.UserName tweet
addTweetToUser tweet
let getString (rawForm: byte[]) =
System.Text.Encoding.UTF8.GetString(rawForm)
let fromJson<'a> json =
JsonConvert.DeserializeObject(json, typeof<'a>) :?> 'a
let gettweets username =
printfn "Received GetTweets Request from %s " username
getTweets username
|> JsonConvert.SerializeObject
|> OK
>=> setMimeType "application/json"
>=> setCORSHeaders
let getmentions username =
printfn "Received GetMentions Request from %s " username
getMentions username
|> JsonConvert.SerializeObject
|> OK
>=> setMimeType "application/json"
>=> setCORSHeaders
let gethashtags username hashtag =
printfn "Received GetHashTag Request from %s for hashtag %A" username hashtag
getHashTags username hashtag
|> JsonConvert.SerializeObject
|> OK
>=> setMimeType "application/json"
>=> setCORSHeaders
let register =
request (fun r ->
r.rawForm
|> getString
|> fromJson<Register>
|> registerNewUser
|> JsonConvert.SerializeObject
|> OK
)
>=> setMimeType "application/json"
>=> setCORSHeaders
let login =
request (fun r ->
r.rawForm
|> getString
|> fromJson<Login>
|> loginuser
|> JsonConvert.SerializeObject
|> OK
)
>=> setMimeType "application/json"
>=> setCORSHeaders
let logout =
request (fun r ->
r.rawForm
|> getString
|> fromJson<Logout>
|> logoutuser
|> JsonConvert.SerializeObject
|> OK
)
>=> setMimeType "application/json"
>=> setCORSHeaders
let newTweet =
request (fun r ->
r.rawForm
|> getString
|> fromJson<NewTweet>
|> respTweet
|> JsonConvert.SerializeObject
|> OK
)
>=> setMimeType "application/json"
>=> setCORSHeaders
let follow =
request (fun r ->
r.rawForm
|> getString
|> fromJson<Follower>
|> addFollower
|> JsonConvert.SerializeObject
|> OK
)
>=> setMimeType "application/json"
>=> setCORSHeaders
let websocketHandler (webSocket : WebSocket) (context: HttpContext) =
socket {
let mutable loop = true
while loop do
let! msg = webSocket.read()
match msg with
| (Text, data, true) ->
let str = UTF8.toString data
if str.StartsWith("UserName:") then
let uname = str.Split(':').[1]
websockmap <- websockmap.Add(uname,webSocket)
printfn "connected to %s websocket" uname
else
let response = sprintf "response to %s" str
let byteResponse = buildByteResponseToWS response
do! webSocket.send Text byteResponse true
| (Close, _, _) ->
let emptyResponse = [||] |> ByteSegment
do! webSocket.send Close emptyResponse true
loop <- false
| _ -> ()
}
let allow_cors : WebPart =
choose [
OPTIONS >=>
fun context ->
context |> (
setCORSHeaders
>=> OK "CORS approved" )
]
let app =
choose
[
path "/websocket" >=> handShake websocketHandler
allow_cors
GET >=> choose
[
path "/" >=> OK "Twitter Server Ok! Please start the twitter.html"
pathScan "/gettweets/%s" (fun username -> (gettweets username))
pathScan "/getmentions/%s" (fun username -> (getmentions username))
pathScan "/gethashtags/%s/%s" (fun (username,hashtag) -> (gethashtags username hashtag))
]
POST >=> choose
[
path "/newtweet" >=> newTweet
path "/register" >=> register
path "/login" >=> login
path "/logout" >=> logout
path "/follow" >=> follow
]
PUT >=> choose
[ ]
DELETE >=> choose
[ ]
]
[<EntryPoint>]
let main argv =
startWebServer defaultConfig app
0