-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathevents.go
128 lines (115 loc) · 3.43 KB
/
events.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"fmt"
"log"
"strings"
"github.com/muesli/telephant/accounts"
"github.com/tachiniererin/bananasplit"
)
func addEmojiFont(body string) string {
runeRange := map[string][]bananasplit.RuneRange{"emoji": bananasplit.EmojiRange}
bodyParts := bananasplit.SplitByRanges(body, runeRange)
var mon strings.Builder
for _, part := range bodyParts {
if part.Type == "emoji" {
mon.WriteString(fmt.Sprintf(`<font face="%s">%s</font>`, config.EmojiFont, part.Text))
} else {
mon.WriteString(part.Text)
}
}
return mon.String()
}
// handleEvents handles incoming events and puts them into the right models
func handleEvents(eventsIn chan interface{}, messages *MessageModel) {
for {
ev, ok := <-eventsIn
if !ok {
log.Println()
log.Println("Stopped event handler!")
break
}
switch event := ev.(type) {
case accounts.LoginEvent:
{
log.Println("Account login succeeded:", event.Username, event.Name, event.Avatar)
accountBridge.SetUsername(event.Username)
accountBridge.SetName(addEmojiFont(event.Name))
accountBridge.SetAvatar(event.Avatar)
accountBridge.SetProfileURL(event.ProfileURL)
accountBridge.SetProfileID(event.ProfileID)
accountBridge.SetPosts(event.Posts)
accountBridge.SetFollowCount(event.FollowCount)
accountBridge.SetFollowerCount(event.FollowerCount)
accountBridge.SetPostSizeLimit(event.PostSizeLimit)
}
case accounts.ErrorEvent:
{
log.Println("Error:", event.Message)
accountBridge.SetError(event.Message)
}
case accounts.Media:
{
log.Printf("Added attachment: %+v\n", event)
var p = NewAttachment(nil)
p.ID = event.ID
p.Preview = event.Preview
attachmentModel.AddAttachment(p)
}
case accounts.DeleteEvent:
{
deleteMessage(event.ID)
}
case accounts.MessageEvent:
{
// spw := &spew.ConfigState{Indent: " ", DisableCapacities: true, DisablePointerAddresses: true}
// log.Println("Message received:", spw.Sdump(event))
p := messageFromEvent(event)
p.Name = addEmojiFont(p.Name)
p.ActorName = addEmojiFont(p.ActorName)
p.Body = addEmojiFont(p.Body)
// 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 {
log.Fatal("URL detection regexp does not compile: ", err)
}
p.Body = string(re.ReplaceAllFunc([]byte(p.Body), linkify))
*/
if event.Notification {
notificationModel.AddMessage(p)
if event.Notify {
title := "Telephant"
body := p.Body
if p.Mention {
body = fmt.Sprintf("%s mentioned you", p.Name)
}
if p.Followed {
body = fmt.Sprintf("%s followed you", p.ActorName)
}
if p.Like {
body = fmt.Sprintf("%s liked your post", p.ActorName)
}
if p.Forward {
body = fmt.Sprintf("%s shared your post", p.ActorName)
}
/*
if body == "" {
spw := &spew.ConfigState{Indent: " ", DisableCapacities: true, DisablePointerAddresses: true}
log.Println("Unknown notification received:", spw.Sdump(event))
}
*/
if body != "" {
notify(title, body)
}
}
} else {
messages.AddMessage(p)
}
}
}
}
}
// linkify is a helper function to wrap an HTML anchor around detected links.
func linkify(in []byte) []byte {
return []byte(fmt.Sprintf("<a style=\"text-decoration: none;\" href=\"%s\">%s</a>", in, in))
}