telephant/events.go

90 lines
2.5 KiB
Go
Raw Normal View History

2017-08-29 05:09:17 +00:00
package main
import (
"fmt"
"log"
"strings"
"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
}
func messageFromEvent(event accounts.MessageEvent) *Message {
var p = NewMessage(nil)
p.MessageID = event.Post.MessageID
p.PostURL = event.Post.URL
p.Name = event.Post.AuthorName
p.Author = event.Post.Author
p.AuthorURL = event.Post.AuthorURL
p.AuthorID = event.Post.AuthorID
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
p.Liked = event.Post.Liked
p.Shared = event.Post.Shared
if len(event.Media) > 0 {
p.Media = event.Media[0]
}
return p
}
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:
{
2019-05-01 15:15:56 +00:00
log.Println("Account login succeeded:", event.Username, event.Name, event.Avatar)
2017-08-29 05:09:17 +00:00
accountBridge.SetUsername(event.Username)
accountBridge.SetName(event.Name)
2017-08-29 05:09:17 +00:00
accountBridge.SetAvatar(event.Avatar)
2019-05-02 08:37:29 +00:00
accountBridge.SetProfileURL(event.ProfileURL)
accountBridge.SetProfileID(event.ProfileID)
accountBridge.SetPosts(event.Posts)
accountBridge.SetFollowCount(event.FollowCount)
accountBridge.SetFollowerCount(event.FollowerCount)
accountBridge.SetPostSizeLimit(event.PostSizeLimit)
2017-08-29 05:09:17 +00:00
}
case accounts.MessageEvent:
{
// spw := &spew.ConfigState{Indent: " ", DisableCapacities: true, DisablePointerAddresses: true}
// log.Println("Message received:", spw.Sdump(event))
2017-08-29 05:09:17 +00:00
p := messageFromEvent(event)
2017-08-29 05:09:17 +00:00
// markup links
/* re, err := regexp.Compile(`https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-z]{2,16}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)`)
2017-08-29 05:09:17 +00:00
if err != nil {
2017-08-29 14:05:51 +00:00
log.Fatal("URL detection regexp does not compile: ", err)
2017-08-29 05:09:17 +00:00
}
p.Body = string(re.ReplaceAllFunc([]byte(p.Body), linkify)) */
2017-08-29 05:09:17 +00:00
if event.Notification {
notifications.AddMessage(p)
} else {
messages.AddMessage(p)
}
}
}
}
}