Improve handling of authors and checkins

This commit is contained in:
Peter Stuifzand 2018-04-08 20:23:00 +02:00
parent 04841aa652
commit 9c7d11cb68
4 changed files with 96 additions and 65 deletions

View File

@ -156,7 +156,6 @@ func (b *memoryBackend) feedHeader(fetchURL, contentType string, body io.Reader)
log.Printf("Error while parsing rss/atom feed: %s\n", err)
return feed, err
}
log.Printf("%#v\n", xfeed)
feed.Type = "feed"
feed.Name = xfeed.Title
@ -258,7 +257,7 @@ func (b *memoryBackend) feedItems(fetchURL, contentType string, body io.Reader)
return items, err
}
author := microsub.Author{}
author := microsub.Card{}
author.Type = "card"
author.Name = feed.Author.Name
author.URL = feed.Author.URL
@ -428,8 +427,6 @@ func Fetch2(fetchURL string) (*http.Response, error) {
cur := b.Bytes()
copy(cachedCopy, cur)
log.Println(string(cachedCopy))
cache[u.String()] = cacheItem{item: cachedCopy, created: time.Now()}
cachedResp, err := http.ReadResponse(bufio.NewReader(bytes.NewReader(cachedCopy)), req)
@ -465,7 +462,7 @@ func Fetch(fetchURL string) []microsub.Item {
jw.SetIndent("", " ")
jw.Encode(data)
author := microsub.Author{}
author := microsub.Card{}
for _, item := range data.Items {
if item.Type[0] == "h-feed" {

View File

@ -48,6 +48,10 @@ type Debug interface {
Debug()
}
func init() {
log.SetFlags(log.Lshortfile | log.Ldate | log.Ltime)
}
func (b *memoryBackend) Debug() {
fmt.Println(b.Channels)
}
@ -165,17 +169,29 @@ func (b *memoryBackend) ChannelsDelete(uid string) {
}
}
func mapToAuthor(result map[string]interface{}) microsub.Author {
item := microsub.Author{}
func mapToAuthor(result map[string]string) microsub.Card {
item := microsub.Card{}
item.Type = "card"
if name, e := result["name"]; e {
item.Name = name.(string)
item.Name = name
}
if u, e := result["url"]; e {
item.URL = u.(string)
item.URL = u
}
if photo, e := result["photo"]; e {
item.Photo = photo.(string)
item.Photo = photo
}
if value, e := result["longitude"]; e {
item.Longitude = value
}
if value, e := result["latitude"]; e {
item.Latitude = value
}
if value, e := result["country-name"]; e {
item.CountryName = value
}
if value, e := result["locality"]; e {
item.Locality = value
}
return item
}
@ -198,7 +214,11 @@ func mapToItem(result map[string]interface{}) microsub.Item {
}
if author, e := result["author"]; e {
item.Author = mapToAuthor(author.(map[string]interface{}))
item.Author = mapToAuthor(author.(map[string]string))
}
if checkin, e := result["checkin"]; e {
item.Checkin = mapToAuthor(checkin.(map[string]string))
}
if content, e := result["content"]; e {
@ -214,50 +234,49 @@ func mapToItem(result map[string]interface{}) microsub.Item {
// TODO: Check how to improve this
// if value, e := result["like-of"]; e {
// for _, v := range value.([]interface{}) {
// item.LikeOf = append(item.LikeOf, v.(string))
// }
// }
if value, e := result["like-of"]; e {
for _, v := range value.([]interface{}) {
item.LikeOf = append(item.LikeOf, v.(string))
}
}
// if value, e := result["repost-of"]; e {
// for _, v := range value.([]interface{}) {
// item.RepostOf = append(item.RepostOf, v.(string))
// }
// }
if value, e := result["repost-of"]; e {
for _, v := range value.([]interface{}) {
item.RepostOf = append(item.RepostOf, v.(string))
}
}
// if value, e := result["bookmark-of"]; e {
// for _, v := range value.([]interface{}) {
// item.BookmarkOf = append(item.BookmarkOf, v.(string))
// }
// }
if value, e := result["bookmark-of"]; e {
for _, v := range value.([]interface{}) {
item.BookmarkOf = append(item.BookmarkOf, v.(string))
}
}
// if value, e := result["in-reply-to"]; e {
// for _, v := range value.([]interface{}) {
// if replyTo, ok := v.(string); ok {
// item.InReplyTo = append(item.InReplyTo, replyTo)
// } else if cite, ok := v.(map[string]interface{}); ok {
// item.InReplyTo = append(item.InReplyTo, cite["url"].(string))
// }
// }
// }
if value, e := result["in-reply-to"]; e {
for _, v := range value.([]interface{}) {
if replyTo, ok := v.(string); ok {
item.InReplyTo = append(item.InReplyTo, replyTo)
} else if cite, ok := v.(map[string]interface{}); ok {
item.InReplyTo = append(item.InReplyTo, cite["url"].(string))
}
}
}
// if value, e := result["photo"]; e {
// for _, v := range value.([]interface{}) {
// item.Photo = append(item.Photo, v.(string))
// }
// }
if value, e := result["photo"]; e {
for _, v := range value.([]interface{}) {
item.Photo = append(item.Photo, v.(string))
}
}
// if value, e := result["category"]; e {
// if cats, ok := value.([]string); ok {
// for _, v := range cats {
// item.Category = append(item.Category, v)
// }
// } else {
// item.Category = append(item.Category, value.(string))
// }
// }
if value, e := result["category"]; e {
if cats, ok := value.([]string); ok {
for _, v := range cats {
item.Category = append(item.Category, v)
}
} else {
item.Category = append(item.Category, value.(string))
}
}
if published, e := result["published"]; e {
item.Published = published.(string)

View File

@ -42,6 +42,15 @@ func simplify(itemType string, item map[string][]interface{}) map[string]interfa
feedItem[k] = v
} else if k == "featured" {
feedItem[k] = v
} else if k == "checkin" || k == "author" {
if value, ok := v[0].(*microformats.Microformat); ok {
card := make(map[string]string)
card["type"] = "card"
for ik, vk := range value.Properties {
card[ik] = vk[0].(string)
}
feedItem[k] = card
}
} else if value, ok := v[0].(*microformats.Microformat); ok {
mType := value.Type[0][2:]
m := simplify(mType, value.Properties)

View File

@ -37,12 +37,17 @@ type Channel struct {
Unread bool `json:"unread"`
}
type Author struct {
Filled bool `json:"-,omitempty"`
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
Photo string `json:"photo,omitempty"`
type Card struct {
Filled bool `json:"-,omitempty"`
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
Photo string `json:"photo,omitempty"`
Locality string `json:"locality,omitempty"`
Region string `json:"region,omitempty"`
CountryName string `json:"country-name,omitempty"`
Longitude string `json:"longitude,omitempty"`
Latitude string `json:"latitude,omitempty"`
}
type Content struct {
@ -57,18 +62,19 @@ type Item struct {
Published string `json:"published"`
Updated string `json:"updated"`
URL string `json:"url"`
UID string `json:"uid"`
Author Author `json:"author"`
Category []string `json:"category"`
Photo []string `json:"photo"`
LikeOf []string `json:"like-of"`
BookmarkOf []string `json:"bookmark-of"`
RepostOf []string `json:"repost-of"`
InReplyTo []string `json:"in-reply-to"`
UID string `json:"uid,omitempty"`
Author Card `json:"author,omitempty"`
Category []string `json:"category,omitempty"`
Photo []string `json:"photo,omitempty"`
LikeOf []string `json:"like-of,omitempty"`
BookmarkOf []string `json:"bookmark-of,omitempty"`
RepostOf []string `json:"repost-of,omitempty"`
InReplyTo []string `json:"in-reply-to,omitempty"`
Summary []string `json:"summary,omitempty"`
Content Content `json:"content,omitempty"`
Latitude string `json:"latitude,omitempty"`
Longitude string `json:"longitude,omitempty"`
Checkin Card `json:"checkin,omitempty"`
Id string `json:"_id"`
Read bool `json:"_is_read"`
}
@ -91,7 +97,7 @@ type Feed struct {
Name string `json:"name,omitempty"`
Photo string `json:"photo,omitempty"`
Description string `json:"description,omitempty"`
Author Author `json:"author,omitempty"`
Author Card `json:"author,omitempty"`
}
// Microsub is the main protocol that should be implemented by a backend