Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8dbd86535d | ||
|
|
f534cc9f9b | ||
|
|
a77cafb3ae | ||
|
|
48cbbf652a | ||
|
|
e05f572eb2 | ||
|
|
7ddb288ae3 | ||
|
|
cee889bd58 |
@@ -117,6 +117,7 @@ type Object struct {
|
|||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
ContentMap map[string]string `json:"contentMap,omitempty"`
|
ContentMap map[string]string `json:"contentMap,omitempty"`
|
||||||
Tag []Tag `json:"tag"`
|
Tag []Tag `json:"tag"`
|
||||||
|
Attachment []Attachment `json:"attachment,omitempty"`
|
||||||
|
|
||||||
// Extensions
|
// Extensions
|
||||||
// NOTE: add extensions here
|
// NOTE: add extensions here
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package activitystreams
|
||||||
|
|
||||||
|
import (
|
||||||
|
"mime"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Attachment struct {
|
||||||
|
Type AttachmentType `json:"type"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
MediaType string `json:"mediaType"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type AttachmentType string
|
||||||
|
|
||||||
|
const AttachImage AttachmentType = "Image"
|
||||||
|
|
||||||
|
// NewImageAttachment creates a new Attachment from the given URL, setting the
|
||||||
|
// correct type and automatically detecting the MediaType based on the file
|
||||||
|
// extension.
|
||||||
|
func NewImageAttachment(url string) Attachment {
|
||||||
|
var imgType string
|
||||||
|
extIdx := strings.LastIndexByte(url, '.')
|
||||||
|
if extIdx > -1 {
|
||||||
|
imgType = mime.TypeByExtension(url[extIdx:])
|
||||||
|
}
|
||||||
|
return Attachment{
|
||||||
|
Type: AttachImage,
|
||||||
|
URL: url,
|
||||||
|
MediaType: imgType,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package activitystreams
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNewImageAttachment(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
url string
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want *Attachment
|
||||||
|
}{
|
||||||
|
{name: "good svg", args: args{"https://writefreely.org/img/writefreely.svg"}, want: &Attachment{
|
||||||
|
Type: "Image",
|
||||||
|
URL: "https://writefreely.org/img/writefreely.svg",
|
||||||
|
MediaType: "image/svg+xml",
|
||||||
|
}},
|
||||||
|
{name: "good png", args: args{"https://i.snap.as/12345678.png"}, want: &Attachment{
|
||||||
|
Type: "Image",
|
||||||
|
URL: "https://i.snap.as/12345678.png",
|
||||||
|
MediaType: "image/png",
|
||||||
|
}},
|
||||||
|
{name: "no extension", args: args{"https://i.snap.as/12345678"}, want: &Attachment{
|
||||||
|
Type: "Image",
|
||||||
|
URL: "https://i.snap.as/12345678",
|
||||||
|
MediaType: "",
|
||||||
|
}},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := NewImageAttachment(tt.args.url); !reflect.DeepEqual(got, tt.want) {
|
||||||
|
t.Errorf("NewImageAttachment() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,5 +10,5 @@ type TagType string
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
TagHashtag TagType = "Hashtag"
|
TagHashtag TagType = "Hashtag"
|
||||||
TagMention = "Mention"
|
TagMention TagType = "Mention"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -67,3 +67,13 @@ func (memo *Memo) Get() (value interface{}, err error) {
|
|||||||
}
|
}
|
||||||
return memo.cache.res.value, memo.cache.res.err
|
return memo.cache.res.value, memo.cache.res.err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reset forcibly resets the cache to nil without checking if the cache
|
||||||
|
// duration has elapsed.
|
||||||
|
func (memo *Memo) Reset() {
|
||||||
|
defer memo.mu.Unlock()
|
||||||
|
memo.mu.Lock()
|
||||||
|
|
||||||
|
memo.cache = nil
|
||||||
|
memo.lastCache = time.Now()
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user