Skip to content

Commit 99561e0

Browse files
committed
add user/user_link.go
1 parent b5f2920 commit 99561e0

File tree

6 files changed

+102
-1
lines changed

6 files changed

+102
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@
1414
# Dependency directories (remove the comment below to include it)
1515
# vendor/
1616
.idea/
17+
go.sum

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/wechaty/go-wechaty
22

3+
require github.com/otiai10/opengraph v1.1.1
4+
35
go 1.14

src/wechaty/user/contact.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package user
2+
3+
import "github.com/wechaty/go-wechaty/src/wechaty"
4+
5+
type Contact struct {
6+
wechaty.Accessory
7+
}

src/wechaty/user/image.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type Images struct {
1414
// NewImages create image struct
1515
func NewImages(id string, accessory wechaty.Accessory) *Images {
1616
if accessory.Puppet == nil {
17-
panic("Image class can not be instanciated without a puppet!")
17+
panic("Image class can not be instantiated without a puppet!")
1818
}
1919
return &Images{accessory, id}
2020
}

src/wechaty/user/tag.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package user
2+
3+
import (
4+
"github.com/wechaty/go-wechaty/src/wechaty"
5+
)
6+
7+
type Tag struct {
8+
wechaty.Accessory
9+
TagId string
10+
}
11+
12+
func NewTag(id string, accessory wechaty.Accessory) *Tag {
13+
if accessory.Puppet == nil {
14+
panic("Tag class can not be instantiated without a puppet!")
15+
}
16+
return &Tag{accessory, id}
17+
}

src/wechaty/user/user_link.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package user
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"github.com/otiai10/opengraph"
7+
"github.com/wechaty/go-wechaty/src/wechaty-puppet/schemas"
8+
)
9+
10+
var (
11+
ErrImageUrlOrDescNotFound = errors.New("imgUrl.or.desc.not.found")
12+
)
13+
14+
type UrlLink struct {
15+
payload *schemas.UrlLinkPayload
16+
}
17+
18+
func NewUrlLink(payload *schemas.UrlLinkPayload) *UrlLink {
19+
return &UrlLink{payload: payload}
20+
}
21+
22+
func (ul *UrlLink) String() string {
23+
return fmt.Sprintf("UrlLink<%s>", ul.Url())
24+
}
25+
26+
func (ul *UrlLink) Url() string {
27+
if ul.payload == nil {
28+
return ""
29+
}
30+
return ul.payload.Url
31+
}
32+
33+
func (ul *UrlLink) Title() string {
34+
if ul.payload == nil {
35+
return ""
36+
}
37+
return ul.payload.Title
38+
}
39+
40+
func (ul *UrlLink) ThumbnailUrl() string {
41+
if ul.payload == nil {
42+
return ""
43+
}
44+
return ul.payload.ThumbnailUrl
45+
}
46+
47+
func (ul *UrlLink) Description() string {
48+
if ul.payload == nil {
49+
return ""
50+
}
51+
return ul.payload.Description
52+
}
53+
54+
func CreateUrlLink(url string) (*UrlLink, error) {
55+
var og, err = opengraph.Fetch(url)
56+
if err != nil {
57+
return nil, err
58+
}
59+
var payload = &schemas.UrlLinkPayload{
60+
Url: url,
61+
Title: og.Title,
62+
Description: og.Description,
63+
}
64+
65+
if len(og.Image) != 0 {
66+
payload.ThumbnailUrl = og.Image[0].URL
67+
}
68+
69+
if payload.ThumbnailUrl == "" || payload.Description == "" {
70+
return nil, ErrImageUrlOrDescNotFound
71+
}
72+
73+
return NewUrlLink(payload), nil
74+
}

0 commit comments

Comments
 (0)