This repository has been archived by the owner on Feb 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
book.go
63 lines (53 loc) · 1.81 KB
/
book.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
package model
import (
"fmt"
"net/url"
"time"
"github.com/go-fed/activity/streams"
"github.com/go-fed/activity/streams/vocab"
"github.com/jinzhu/gorm"
)
// A Book is something that can be read. Currently this only supports things which are in the Library of Congress API, but eventually it'd be great to support fanfiction and other online-only sources.
type Book struct {
ID string `gorm:"primary_key" json:"id"`
Title string `gorm:"not null;index" json:"title"`
Published int `json:"published,omitempty"`
ISBN string `json:"isbn,omitempty"`
Authors []Author `gorm:"-"`
db *gorm.DB
}
// NewBook returns a new instance of a book
func NewBook(id string, title string, published int, isbn string) *Book {
return &Book{
ID: id,
Title: title,
Published: published,
ISBN: isbn,
}
}
// ToType returns a representation of a book as an ActivityPub object.
func (b *Book) ToType() vocab.Type {
book := streams.NewActivityStreamsDocument()
u, err := url.Parse(fmt.Sprintf("https://openlibrary.org/works/%s/", b.ID))
if err == nil {
id := streams.NewJSONLDIdProperty()
id.SetIRI(u)
book.SetJSONLDId(id)
}
name := streams.NewActivityStreamsNameProperty()
name.AppendXMLSchemaString(b.Title)
book.SetActivityStreamsName(name)
// this isn't ideal since it will default to 1/1 at 12:00:00 am of the year...?
if b.Published > 0 {
published := streams.NewActivityStreamsPublishedProperty()
date := time.Date(b.Published, time.January, 1, 0, 0, 0, 0, time.UTC)
published.Set(date)
book.SetActivityStreamsPublished(published)
}
authors := streams.NewActivityStreamsAttributedToProperty()
for _, a := range b.Authors {
authors.AppendActivityStreamsPerson(a.ToType().(vocab.ActivityStreamsPerson))
}
book.SetActivityStreamsAttributedTo(authors)
return book
}