7 Commits
Author SHA1 Message Date
Matt Baer 8dbd86535d Don't return pointer from NewImageAttachment() 2021-03-24 15:47:23 -04:00
Matt Baer f534cc9f9b Fix TagMention type 2021-03-24 15:46:13 -04:00
Matt Baer a77cafb3ae Add NewImageAttachment func 2021-03-24 15:45:58 -04:00
Matt Baer 48cbbf652a Support attachments on activitystreams.Object 2021-03-24 15:33:34 -04:00
Matt BaerandGitHub e05f572eb2 Merge pull request #11 from colin-axner/colin/memo-reset
Add (forceful) Reset function to memo
2021-03-06 16:51:49 -05:00
Colin Axner 7ddb288ae3 add godoc 2020-12-06 12:06:51 +01:00
Colin Axner cee889bd58 Add Reset func to memo
Add a new function Reset to memo to forcibly reset the cache
2020-11-23 15:36:27 +01:00
5 changed files with 85 additions and 1 deletions
+1
View File
@@ -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
+33
View File
@@ -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,
}
}
+40
View File
@@ -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)
}
})
}
}
+1 -1
View File
@@ -10,5 +10,5 @@ type TagType string
const ( const (
TagHashtag TagType = "Hashtag" TagHashtag TagType = "Hashtag"
TagMention = "Mention" TagMention TagType = "Mention"
) )
+10
View File
@@ -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()
}