2017-08-29 05:09:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
|
|
|
|
"github.com/muesli/chirp/accounts"
|
|
|
|
)
|
|
|
|
|
|
|
|
func linkify(in []byte) []byte {
|
2017-08-29 13:22:03 +00:00
|
|
|
return []byte(fmt.Sprintf("<a style=\"text-decoration: none; color: orange;\" href=\"%s\">%s</a>", in, in))
|
2017-08-29 05:09:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleEvents handles incoming events and puts them into the right models
|
|
|
|
func handleEvents(eventsIn chan interface{}, messages *MessageModel, notifications *MessageModel) {
|
|
|
|
for {
|
|
|
|
ev, ok := <-eventsIn
|
|
|
|
if !ok {
|
|
|
|
log.Println()
|
|
|
|
log.Println("Stopped event handler!")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
switch event := ev.(type) {
|
|
|
|
case accounts.LoginEvent:
|
|
|
|
{
|
|
|
|
log.Println("Twitter login succeeded:", event.Username, event.Name, event.Avatar)
|
|
|
|
accountBridge.SetUsername(event.Username)
|
|
|
|
accountBridge.SetAvatar(event.Avatar)
|
|
|
|
}
|
|
|
|
case accounts.MessageEvent:
|
|
|
|
{
|
|
|
|
spw := &spew.ConfigState{Indent: " ", DisableCapacities: true, DisablePointerAddresses: true}
|
|
|
|
log.Println("Message received:", spw.Sdump(event))
|
|
|
|
|
|
|
|
var p = NewMessage(nil)
|
|
|
|
p.MessageID = event.Post.MessageID
|
|
|
|
p.Name = event.Post.AuthorName
|
|
|
|
p.Author = event.Post.Author
|
|
|
|
p.Avatar = event.Post.Avatar
|
|
|
|
p.Body = strings.TrimSpace(event.Post.Body)
|
|
|
|
p.CreatedAt = event.Post.CreatedAt
|
|
|
|
p.Reply = event.Reply
|
|
|
|
p.ReplyToID = event.Post.ReplyToID
|
|
|
|
p.ReplyToAuthor = event.Post.ReplyToAuthor
|
|
|
|
p.Mention = event.Mention
|
|
|
|
p.Like = event.Like
|
|
|
|
p.Forward = event.Forward
|
|
|
|
p.Actor = event.Post.Actor
|
|
|
|
p.ActorName = event.Post.ActorName
|
|
|
|
|
|
|
|
// markup links
|
|
|
|
re, err := regexp.Compile(`https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-z]{2,16}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)`)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
p.Body = string(re.ReplaceAllFunc([]byte(p.Body), linkify))
|
|
|
|
|
|
|
|
if event.Notification {
|
|
|
|
notifications.AddMessage(p)
|
|
|
|
} else {
|
|
|
|
messages.AddMessage(p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|