Write on the margins of the internet. Powered by the AT Protocol. margin.at
extension web atproto comments
at ui-refactor 82 lines 2.0 kB view raw
1package xrpc 2 3import ( 4 "encoding/json" 5 "time" 6) 7 8const ( 9 CollectionSembleCard = "network.cosmik.card" 10 CollectionSembleCollection = "network.cosmik.collection" 11 CollectionSembleCollectionLink = "network.cosmik.collectionLink" 12) 13 14type SembleCard struct { 15 Type string `json:"type"` 16 Content json.RawMessage `json:"content"` 17 URL string `json:"url,omitempty"` 18 ParentCard *StrongRef `json:"parentCard,omitempty"` 19 CreatedAt string `json:"createdAt"` 20} 21 22type SembleURLContent struct { 23 URL string `json:"url"` 24 Metadata *SembleURLMetadata `json:"metadata,omitempty"` 25} 26 27type SembleNoteContent struct { 28 Text string `json:"text"` 29} 30 31type SembleURLMetadata struct { 32 Title string `json:"title,omitempty"` 33 Description string `json:"description,omitempty"` 34 Author string `json:"author,omitempty"` 35 SiteName string `json:"siteName,omitempty"` 36} 37 38type SembleCollection struct { 39 Name string `json:"name"` 40 Description string `json:"description,omitempty"` 41 AccessType string `json:"accessType"` 42 CreatedAt string `json:"createdAt"` 43} 44 45type SembleCollectionLink struct { 46 Collection StrongRef `json:"collection"` 47 Card StrongRef `json:"card"` 48 AddedBy string `json:"addedBy"` 49 AddedAt string `json:"addedAt"` 50 CreatedAt string `json:"createdAt"` 51} 52 53type StrongRef struct { 54 URI string `json:"uri"` 55 CID string `json:"cid"` 56} 57 58func (c *SembleCard) ParseContent() (interface{}, error) { 59 switch c.Type { 60 case "URL": 61 var content SembleURLContent 62 if err := json.Unmarshal(c.Content, &content); err != nil { 63 return nil, err 64 } 65 return &content, nil 66 case "NOTE": 67 var content SembleNoteContent 68 if err := json.Unmarshal(c.Content, &content); err != nil { 69 return nil, err 70 } 71 return &content, nil 72 } 73 return nil, nil 74} 75 76func (c *SembleCard) GetCreatedAtTime() time.Time { 77 t, err := time.Parse(time.RFC3339, c.CreatedAt) 78 if err != nil { 79 return time.Now() 80 } 81 return t 82}