/
WebmentionReceiverService.fs
88 lines (75 loc) · 3.64 KB
/
WebmentionReceiverService.fs
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
namespace WebmentionFs.Services
open System
open System.Net
open System.Threading.Tasks
open Microsoft.AspNetCore.Http
open WebmentionFs
open WebmentionFs.Services
// Webmention Receiver Interface
type IWebmentionReceiver<'a> =
abstract member ReceiveAsync : req:HttpRequest -> Task<ValidationResult<'a>>
abstract member ReceiveAsync : data:UrlData -> Task<ValidationResult<'a>>
// Implementaiton of Webmention Receiver
type WebmentionReceiverService (
requestValidationService : RequestValidationService,
webmentionValidationService : WebmentionValidationService) =
// Concrete implementation of webmention receiver
interface IWebmentionReceiver<Webmention> with
member x.ReceiveAsync (req:HttpRequest) =
task {
let! requestValidationResult =
x.RequestValidationService.ValidateAsync req
match requestValidationResult with
| RequestSuccess r ->
let! webmentionValidationResult =
x.WembentionValidationService.ValidateAsync r.Source r.Target
let (result:ValidationResult<Webmention>) =
match webmentionValidationResult with
| AnnotatedMention m ->
ValidationSuccess {RequestBody = r; Mentions = m}
| UnannotatedMention ->
ValidationSuccess
{
RequestBody = r
Mentions =
{
IsBookmark = false
IsLike = false
IsReply = false
IsRepost = false
}
}
| MentionError e -> ValidationError e
return result
| RequestError e -> return ValidationError e
}
member x.ReceiveAsync (data:UrlData) =
task {
let! requestValidationResult =
x.RequestValidationService.ValidateAsync data
match requestValidationResult with
| RequestSuccess r ->
let! webmentionValidationResult =
x.WembentionValidationService.ValidateAsync r.Source r.Target
let (result:ValidationResult<Webmention>) =
match webmentionValidationResult with
| AnnotatedMention m ->
ValidationSuccess {RequestBody = r; Mentions = m}
| UnannotatedMention ->
ValidationSuccess
{
RequestBody = r
Mentions =
{
IsBookmark = false
IsLike = false
IsReply = false
IsRepost = false
}
}
| MentionError e -> ValidationError e
return result
| RequestError e -> return ValidationError e
}
member x.RequestValidationService = requestValidationService
member x.WembentionValidationService = webmentionValidationService