-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from dingdayu/master
WIP: add user/message.go
- Loading branch information
Showing
11 changed files
with
233 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,60 @@ | ||
package wechaty_puppet | ||
|
||
import ( | ||
lru "github.com/hashicorp/golang-lru" | ||
|
||
"github.com/wechaty/go-wechaty/src/wechaty-puppet/schemas" | ||
) | ||
|
||
type Puppet interface { | ||
type PuppetInterface interface { | ||
MessageImage(messageId string, imageType schemas.ImageType) FileBox | ||
} | ||
|
||
type Puppet struct { | ||
PuppetInterface | ||
|
||
CacheMessagePayload *lru.Cache | ||
} | ||
|
||
// MessageList | ||
func (p *Puppet) MessageList() (ks []string) { | ||
keys := p.CacheMessagePayload.Keys() | ||
for _, v := range keys { | ||
if k, ok := v.(string); ok { | ||
ks = append(ks, k) | ||
} | ||
} | ||
return | ||
} | ||
|
||
func (p *Puppet) MessageSearch(query schemas.MessageUserQueryFilter) []string { | ||
allMessageIdList := p.MessageList() | ||
if len(allMessageIdList) <= 0 { | ||
return allMessageIdList | ||
} | ||
|
||
// load | ||
var messagePayloadList []schemas.MessagePayload | ||
for _, v := range allMessageIdList { | ||
messagePayloadList = append(messagePayloadList, p.MessagePayload(v)) | ||
} | ||
// Filter todo:: messageQueryFilterFactory | ||
var messageIdList []string | ||
for _, message := range messagePayloadList { | ||
if message.FromId == query.FromId || message.RoomId == query.RoomId || message.ToId == query.ToId { | ||
messageIdList = append(messageIdList, message.Id) | ||
} | ||
} | ||
|
||
return messageIdList | ||
} | ||
|
||
// messageQueryFilterFactory 实现正则和直接匹配 | ||
func (p *Puppet) messageQueryFilterFactory(query string) schemas.MessagePayloadFilterFunction { | ||
return nil | ||
} | ||
|
||
// todo:: no finish | ||
func (p *Puppet) MessagePayload(messageId string) schemas.MessagePayload { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package wechaty | ||
|
||
import ( | ||
"github.com/wechaty/go-wechaty/src/wechaty/user" | ||
) | ||
|
||
type Sayable interface { | ||
Say(text string, replyTo ...user.Contact) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package user | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/wechaty/go-wechaty/src/wechaty" | ||
"github.com/wechaty/go-wechaty/src/wechaty-puppet/schemas" | ||
) | ||
|
||
type MessageUserQueryFilter struct { | ||
From Contact | ||
Text string // todo:: RegExp | ||
Room Room | ||
Type schemas.MessageType | ||
To Contact | ||
} | ||
|
||
type Message struct { | ||
wechaty.Accessory | ||
|
||
Type schemas.MessageType | ||
|
||
InvalidDict map[string]bool | ||
|
||
Id string | ||
Payload schemas.MessagePayload | ||
} | ||
|
||
// Find | ||
// userQuery: MessageUserQueryFilter | string | ||
func (m *Message) Find(query interface{}) Message { | ||
var userQuery MessageUserQueryFilter | ||
if q, ok := query.(string); ok { | ||
userQuery.Text = q | ||
} else if q, ok := query.(MessageUserQueryFilter); ok { | ||
userQuery = q | ||
} | ||
|
||
messageList := m.FindAll(userQuery) | ||
if len(messageList) > 0 { | ||
return messageList[0] | ||
} | ||
return Message{} | ||
} | ||
|
||
// FindAll | ||
func (m *Message) FindAll(userQuery MessageUserQueryFilter) (messages []Message) { | ||
puppetQuery := schemas.MessageUserQueryFilter{ | ||
FromId: userQuery.From.Id, | ||
RoomId: userQuery.Room.Id, | ||
Text: userQuery.Text, | ||
ToId: userQuery.To.Id, | ||
Type: userQuery.Type, | ||
} | ||
|
||
ids := m.GetPuppet().MessageSearch(puppetQuery) | ||
|
||
// check invalid message | ||
for _, v := range ids { | ||
message := m.Load(v) | ||
if message.Ready() { | ||
messages = append(messages, message) | ||
} | ||
} | ||
return | ||
} | ||
|
||
// Ready todo:: no finish | ||
func (m *Message) Ready() bool { | ||
if m.IsReady() { | ||
return true | ||
} | ||
|
||
m.Payload = m.GetPuppet().MessagePayload(m.Id) | ||
|
||
if len(m.Payload.Id) == 0 { | ||
// todo:: should not panic, because not recover | ||
panic("no payload") | ||
} | ||
|
||
m.GetWechaty().Room.Load(m.Payload.RoomId).Ready(false) | ||
m.GetWechaty().Contact.Load(m.Payload.RoomId).Ready(false) | ||
m.GetWechaty().Contact.Load(m.Payload.RoomId).Ready(false) | ||
|
||
return false | ||
} | ||
|
||
func (m *Message) IsReady() bool { | ||
return m.Payload != nil | ||
} | ||
|
||
// Load load message | ||
func (m *Message) Load(id string) Message { | ||
return Message{Id: id} | ||
} | ||
|
||
// Create load message alias | ||
func (m *Message) Create(id string) Message { | ||
return m.Load(id) | ||
} | ||
|
||
// ToString message to print string | ||
// todo:: no finish | ||
func (m *Message) ToString() string { | ||
return fmt.Sprintf("%s", m.Payload) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package user | ||
|
||
type Room struct { | ||
Id string | ||
} | ||
|
||
func (r *Room) Load(id string) Room { | ||
return Room{} | ||
} | ||
|
||
func (r *Room) Ready(forceSync bool) bool { | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters