-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicropub.go
87 lines (83 loc) · 2.42 KB
/
micropub.go
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
package main
import (
"log"
"net/http"
"time"
"github.com/boltdb/bolt"
)
func (b *Blog) MicroPubEndpoint(rw http.ResponseWriter, req *http.Request) {
token := b.ia.GetReqAccessToken(req)
if token == nil {
rw.WriteHeader(http.StatusUnauthorized)
return
}
hasScope := false
for _, scope := range token.Scope {
if scope == "post" {
hasScope = true
}
}
if !hasScope {
rw.WriteHeader(http.StatusUnauthorized)
return
}
switch req.FormValue("h") {
case "entry":
name := req.FormValue("name")
//summary := req.FormValue("summary")
content := req.FormValue("content")
//published := req.FormValue("published")
//updated := req.FormValue("updated")
//category := req.FormValue("category")
//slug := req.FormValue("slug")
//location := req.FormValue("location")
//in_reply_to := req.FormValue("in-reply-to")
//repost_of := req.FormValue("repost-of")
//syndication := req.FormValue("syndication")
//mp_syndicate_to := req.FormValue("mp-syndicate-to")
if name != "" {
var entry Article
entry.Title = name
entry.Content = content
entry.Published = time.Now()
b.db.Update(func(tx *bolt.Tx) error {
posts := tx.Bucket([]byte("posts"))
posts.Put(TimeToID(entry.Published), MarshalPost(entry))
return nil
})
links := ScanLinks(content)
source, _ := b.router.Get("Post").URL("id", entry.Slug())
for _, link := range links {
log.Printf("Sending notification to %s", link.String())
b.wm.SendNotification(link, source)
}
rw.Header().Set("Location", b.AbsRoute("Post", "id", entry.Slug()))
rw.WriteHeader(http.StatusCreated)
b.gp.Publish(source, []byte(""))
index, _ := b.router.Get("Home").URL()
b.gp.Publish(index, []byte(""))
return
} else if content != "" {
var entry Note
entry.Message = content
entry.Published = time.Now()
b.db.Update(func(tx *bolt.Tx) error {
posts := tx.Bucket([]byte("posts"))
posts.Put(TimeToID(entry.Published), MarshalPost(entry))
return nil
})
links := ScanLinks(content)
source, _ := b.router.Get("Post").URL("id", entry.Slug())
for _, link := range links {
log.Printf("Sending notification to %s", link.String())
b.wm.SendNotification(link, source)
}
rw.Header().Set("Location", b.AbsRoute("Post", "id", entry.Slug()))
rw.WriteHeader(http.StatusCreated)
b.gp.Publish(source, []byte(""))
index, _ := b.router.Get("Home").URL()
b.gp.Publish(index, []byte(""))
return
}
}
}