Skip to content

Commit

Permalink
v2 alpha.1
Browse files Browse the repository at this point in the history
  • Loading branch information
mohemohe committed Feb 26, 2022
1 parent dd956f3 commit e87f7d1
Show file tree
Hide file tree
Showing 13 changed files with 417 additions and 295 deletions.
48 changes: 48 additions & 0 deletions adapter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package lxlib

import (
"github.com/lxbot/lxlib/v2/common"
"github.com/lxbot/lxlib/v2/lxtypes"
)

type Adapter struct {
common *common.LxCommon
eventCh *chan *lxtypes.Event
messageCh *chan *lxtypes.Message
}

func NewAdapter() (*Adapter, *chan *lxtypes.Message) {
messageCh := make(chan *lxtypes.Message)
eventCh := make(chan *lxtypes.Event)

common := common.NewLxCommon()
adapter := &Adapter{
common: common,
eventCh: &eventCh,
messageCh: &messageCh,
}

go common.Listen(&eventCh)
go adapter.listen()
adapter.Raw(lxtypes.NewEvent(lxtypes.ReadyEvent, nil))

return adapter, &messageCh
}

func (this *Adapter) listen() {
for {
eventPtr := <-*this.eventCh
switch eventPtr.Event {
case lxtypes.OutgoingMessageEvent:
*this.messageCh <- eventPtr.Payload.(*lxtypes.Message)
}
}
}

func (this *Adapter) Raw(event *lxtypes.Event) {
go this.common.Send(event)
}

func (this *Adapter) Send(message *lxtypes.Message) {
go this.common.Send(lxtypes.NewEvent(lxtypes.IncomingMessageEvent, message))
}
60 changes: 60 additions & 0 deletions common/listener.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package common

import (
"encoding/json"
"fmt"
"io"
"log"
"os"

"github.com/lxbot/lxlib/v2/lxtypes"
)

type LxCommon struct {
logger *log.Logger
}

func NewLxCommon() *LxCommon {
return &LxCommon{
logger: log.New(os.Stdout, "", 0),
}
}

func (this *LxCommon) Listen(event *chan *lxtypes.Event) {
d := json.NewDecoder(os.Stdin)
var data map[string]interface{}

for {
err := d.Decode(&data)
if err != nil {
if err == io.EOF {
break
} else {
fmt.Fprintln(os.Stderr, err)
continue
}
}
message := lxtypes.NewEvent(lxtypes.IncomingMessageEvent, data)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
*event <- message
}
}

func (this *LxCommon) Send(message *lxtypes.Event) {
m, err := ToMap(message)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
this.logger.Println(m)
}

func (this *LxCommon) Close() {
message := lxtypes.NewEvent(lxtypes.CloseEvent, nil)
m, err := ToMap(message)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
this.logger.Println(m)
}
19 changes: 19 additions & 0 deletions common/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package common

import (
"encoding/json"

"github.com/lxbot/lxlib/v2/lxtypes"
)

func ToMap(i interface{}) (lxtypes.M, error) {
t, err := json.Marshal(i)
if err != nil {
return nil, err
}
var r lxtypes.M
if err := json.Unmarshal(t, &r); err != nil {
return nil, err
}
return r, nil
}
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module github.com/lxbot/lxlib
module github.com/lxbot/lxlib/v2

go 1.13

require github.com/go-test/deep v1.0.5
require (
github.com/go-test/deep v1.0.5
github.com/rs/xid v1.3.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/go-test/deep v1.0.5 h1:AKODKU3pDH1RzZzm6YZu77YWtEAq6uh1rLIAQlay2qc=
github.com/go-test/deep v1.0.5/go.mod h1:QV8Hv/iy04NyLBxAdO9njL0iVPN1S4d/A3NVv1V36o8=
github.com/rs/xid v1.3.0 h1:6NjYksEUlhurdVehpc7S7dk6DAmcKv8V9gG0FsVN2U4=
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
37 changes: 37 additions & 0 deletions lxtypes/event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package lxtypes

import "github.com/rs/xid"

type (
Event struct {
ID string `json:"id"`
Event EventType `json:"event"`
Payload interface{} `json:"payload"`
}
EventType string
)

const (
ReadyEvent EventType = "ready"
CloseEvent = "close"
IncomingMessageEvent = "outgoing_message"
OutgoingMessageEvent = "incoming_message"
GetStorageEvent = "get_storage"
SetStorageEvent = "set_storage"
)

func NewEvent(event EventType, payload interface{}) *Event {
return &Event{
ID: xid.New().String(),
Event: event,
Payload: payload,
}
}

func (this *Event) CopyID(copyFrom *Event) {
this.ID = copyFrom.ID
}

func (this *Event) PayloadAsMap() M {
return this.Payload.(M)
}
8 changes: 8 additions & 0 deletions lxtypes/kv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package lxtypes

type (
KV struct {
Key string `json:"key"`
Value interface{} `json:"value"`
}
)
5 changes: 5 additions & 0 deletions lxtypes/map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package lxtypes

type (
M = map[string]interface{}
)
104 changes: 104 additions & 0 deletions lxtypes/message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package lxtypes

import (
"encoding/json"

"github.com/rs/xid"
)

type (
Message struct {
User User `json:"user"`
Room Room `json:"room"`
Contents []Content `json:"messages"`
Mode string `json:"mode"`
Raw interface{} `json:"raw"`
}
User struct {
ID string `json:"id"`
Name string `json:"name"`
}
Room struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
}
Content struct {
ID string `json:"id"`
Text string `json:"text"`
Attachments []Attachment `json:"attachments"`
}
Attachment struct {
Url string `json:"url"`
Description string `json:"description"`
}
)

func NewLXMessage(msg M) (*Message, error) {
t, err := json.Marshal(msg)
if err != nil {
return nil, err
}
m := new(Message)
if err := json.Unmarshal(t, m); err != nil {
return nil, err
}
return &Message{
User: m.User,
Room: m.Room,
Contents: m.Contents,
Mode: "",
Raw: m.Raw,
}, nil
}

func (this *Message) Copy() (*Message, error) {
m, err := this.ToMap()
if err != nil {
return nil, err
}
return NewLXMessage(m)
}

func (this *Message) ResetContents() *Message {
this.Contents = make([]Content, 0)
return this
}

func (this *Message) AddContent(text string, attachments ...Attachment) *Message {
var a []Attachment
if attachments == nil {
a = make([]Attachment, 0)
} else {
a = attachments
}

this.Contents = append(this.Contents, Content{
ID: xid.New().String(),
Text: text,
Attachments: a,
})
return this
}

func (this *Message) Send() *Message {
this.Mode = "send"
return this
}

func (this *Message) Reply() *Message {
this.Mode = "reply"
return this
}

func (this *Message) ToMap() (M, error) {
t, err := json.Marshal(this)
if err != nil {
return nil, err
}
var r M
if err := json.Unmarshal(t, &r); err != nil {
return nil, err
}
return r, nil
}
Loading

0 comments on commit e87f7d1

Please sign in to comment.