impl matrix continuation so i can get rid of per-msg get-set
This commit is contained in:
@@ -15,21 +15,23 @@ import (
|
||||
)
|
||||
|
||||
type Matrix struct {
|
||||
mock bool
|
||||
homeserver string
|
||||
username string
|
||||
token string
|
||||
room string
|
||||
mock bool
|
||||
homeserver string
|
||||
username string
|
||||
token string
|
||||
room string
|
||||
continuation string
|
||||
}
|
||||
|
||||
func NewMatrix() Matrix {
|
||||
conf := config.Get().Message.Matrix
|
||||
return Matrix{
|
||||
homeserver: conf.Homeserver,
|
||||
username: conf.Username,
|
||||
token: conf.Token,
|
||||
room: conf.Room,
|
||||
mock: conf.Mock,
|
||||
homeserver: conf.Homeserver,
|
||||
username: conf.Username,
|
||||
token: conf.Token,
|
||||
room: conf.Room,
|
||||
mock: conf.Mock,
|
||||
continuation: conf.Continuation,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,7 +39,11 @@ func (m Matrix) getclient() (*gomatrix.Client, error) {
|
||||
return gomatrix.NewClient(m.homeserver, m.username, m.token)
|
||||
}
|
||||
|
||||
func (m Matrix) Receive() ([]Message, error) {
|
||||
func (m Matrix) Continuation() string {
|
||||
return m.continuation
|
||||
}
|
||||
|
||||
func (m *Matrix) Receive() ([]Message, error) {
|
||||
if m.mock {
|
||||
log.Printf("matrix.Receive()")
|
||||
messages := make([]Message, 0)
|
||||
@@ -65,11 +71,14 @@ func (m Matrix) Receive() ([]Message, error) {
|
||||
return nil, err
|
||||
}
|
||||
messages := make([]Message, 0)
|
||||
result, err := c.Messages(m.room, "", "", 'b', 50)
|
||||
result, err := c.Messages(m.room, "", m.continuation, 'b', 50)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.Printf("%s => {Start:%s End:%v};; %v, (%d)", m.continuation, result.Start, result.End, err, len(result.Chunk))
|
||||
m.continuation = result.Start
|
||||
for _, event := range result.Chunk {
|
||||
//log.Printf("%+v", event)
|
||||
if _, ok := matrixIDs[event.Sender]; !ok {
|
||||
continue
|
||||
}
|
||||
@@ -82,6 +91,7 @@ func (m Matrix) Receive() ([]Message, error) {
|
||||
}
|
||||
}
|
||||
clientChange := regexp.MustCompile("@[a-z]+$")
|
||||
log.Printf("rewriting messages based on @abc")
|
||||
for i := range messages {
|
||||
if found := clientChange.FindString(messages[i].Content); found != "" {
|
||||
messages[i].Content = strings.TrimSpace(strings.ReplaceAll(messages[i].Content, found, ""))
|
||||
@@ -95,6 +105,14 @@ func (m Matrix) Receive() ([]Message, error) {
|
||||
}
|
||||
messages[i].Content = strings.TrimSpace(messages[i].Content)
|
||||
}
|
||||
log.Printf("rewriting messages based on ! CoMmAnD ...")
|
||||
for i := range messages {
|
||||
if strings.HasPrefix(messages[i].Content, "!") {
|
||||
messages[i].Content = "!" + strings.TrimSpace(messages[i].Content[1:])
|
||||
splits := strings.Split(messages[i].Content, " ")
|
||||
messages[i].Content = strings.ToLower(splits[0]) + " " + strings.Join(splits, " ")
|
||||
}
|
||||
}
|
||||
return messages, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user