Skip to content

Commit

Permalink
Add config check for ZWSP.
Browse files Browse the repository at this point in the history
Helper methods now better reflect their actions.
  • Loading branch information
Tjzabel committed May 23, 2020
1 parent 67ff1ca commit 48bc3b7
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 19 deletions.
1 change: 1 addition & 0 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type IRCSettings struct {
Suffix string `env:"IRC_SUFFIX" envDefault:">"`
ShowJoinMessage bool `env:"IRC_SHOW_JOIN_MESSAGE" envDefault:"true"`
ShowLeaveMessage bool `env:"IRC_SHOW_LEAVE_MESSAGE" envDefault:"true"`
ShowZWSP bool `env:"IRC_SHOW_ZWSP" envDefault:"true"`
NickServPassword string `env:"IRC_NICKSERV_PASS" envDefault:""`
NickServService string `env:"IRC_NICKSERV_SERVICE" envDefault:""`
EditedPrefix string `env:"IRC_EDITED_PREFIX" envDefault:"[EDIT] "`
Expand Down
11 changes: 8 additions & 3 deletions internal/handlers/telegram/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ messageHandler handles the Message Telegram Object, which formats the
Telegram update into a simple string for IRC.
*/
func messageHandler(tg *Client, u tgbotapi.Update) {
username := ResolveUserName(u.Message.From)
username := ""
if tg.IRCSettings.ShowZWSP {
username = ZwspUsername(u.Message.From)
} else {
username = GetUsername(u.Message.From)
}
formatted := fmt.Sprintf("%s%s%s %s",
tg.Settings.Prefix,
username,
Expand Down Expand Up @@ -109,7 +114,7 @@ photoHandler handles the Message.Photo Telegram object. Only acknowledges Photo
exists, and sends notification to IRC
*/
func photoHandler(tg *Client, u tgbotapi.Update) {
username := ResolveUserName(u.Message.From)
username := GetUsername(u.Message.From)
formatted := username + " shared a photo on Telegram with caption: '" +
u.Message.Caption + "'"

Expand All @@ -121,7 +126,7 @@ documentHandler receives a document object from Telegram, and sends
a notification to IRC.
*/
func documentHandler(tg *Client, u *tgbotapi.Message) {
username := ResolveUserName(u.From)
username := GetUsername(u.From)
formatted := username + " shared a file"
if u.Document.MimeType != "" {
formatted += " (" + u.Document.MimeType + ")"
Expand Down
37 changes: 37 additions & 0 deletions internal/handlers/telegram/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,9 @@ func TestMessageRandomWithUsername(t *testing.T) {
Prefix: "<",
Suffix: ">",
},
IRCSettings: &internal.IRCSettings{
ShowZWSP: false,
},
sendToIrc: func(s string) {
assert.Equal(t, correct, s)
},
Expand Down Expand Up @@ -518,6 +521,40 @@ func TestMessageRandomWithoutUsername(t *testing.T) {
Prefix: "<",
Suffix: ">",
},
IRCSettings: &internal.IRCSettings{
ShowZWSP: false,
},
sendToIrc: func(s string) {
assert.Equal(t, correct, s)
},
}

messageHandler(clientObj, updateObj)
}

func TestMessageZwsp(t *testing.T) {
testUser := &tgbotapi.User{
ID: 1,
UserName: "test",
FirstName: "testing",
LastName: "123",
}
correct := fmt.Sprintf("<%s> Random Text", "t"+""+"est")

updateObj := tgbotapi.Update{
Message: &tgbotapi.Message{
From: testUser,
Text: "Random Text",
},
}
clientObj := &Client{
Settings: &internal.TelegramSettings{
Prefix: "<",
Suffix: ">",
},
IRCSettings: &internal.IRCSettings{
ShowZWSP: true,
},
sendToIrc: func(s string) {
assert.Equal(t, correct, s)
},
Expand Down
21 changes: 17 additions & 4 deletions internal/handlers/telegram/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
)

/*
ResolveUserName does basic cleanup if a user does not have a username on Telegram.
GetUsername returns a Telegram user's username if one is set, or first name otherwise.
*/
func ResolveUserName(u *tgbotapi.User) string {
func GetUsername(u *tgbotapi.User) string {
if u.UserName == "" {
return u.FirstName
}
Expand All @@ -18,12 +18,25 @@ func ResolveUserName(u *tgbotapi.User) string {
}

/*
GetFullUsername returns the name and username of a user. Since usernames are optional
on Telegram, we first need to check to see if they have one set.
GetFullUsername returns both the Telegram user's first name and username, if available.
*/
func GetFullUsername(u *tgbotapi.User) string {
if u.UserName == "" {
return u.FirstName
}
return u.FirstName + " (@" + u.UserName[:1] + "" + u.UserName[1:] + ")"
}

/*
ZwspUsername adds a zero-width space after the first character of a Telegram user's
username.
*/
func ZwspUsername(u *tgbotapi.User) string {
if u.UserName == "" {
return u.FirstName
}
// Add ZWSP to prevent pinging across platforms
// See https://github.com/42wim/matterbridge/issues/175
username := u.UserName[:1] + "" + u.UserName[1:]
return username
}
32 changes: 20 additions & 12 deletions internal/handlers/telegram/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,35 @@ import (
"testing"
)

func TestGetUsername(t *testing.T) {
username := &tgbotapi.User{ID: 1, FirstName: "John", UserName: "jsmith"}
correct := username.FirstName + " (@" + username.UserName[:1] +
"" + username.UserName[1:] + ")"
name := GetFullUsername(username)
func TestGetFullUsername(t *testing.T) {
user := &tgbotapi.User{ID: 1, FirstName: "John", UserName: "jsmith"}
correct := user.FirstName + " (@" + user.UserName[:1] +
"" + user.UserName[1:] + ")"
name := GetFullUsername(user)

assert.Equal(t, correct, name)
}

func TestGetNoUsername(t *testing.T) {
username := &tgbotapi.User{ID: 1, FirstName: "John"}
correct := username.FirstName
name := GetFullUsername(username)
user := &tgbotapi.User{ID: 1, FirstName: "John"}
correct := user.FirstName
name := GetFullUsername(user)

assert.Equal(t, correct, name)
}

func TestGetUsername(t *testing.T) {
user := &tgbotapi.User{ID: 1, FirstName: "John", UserName: "jsmith"}
correct := user.UserName[:1] + "" + user.UserName[1:]
name := GetUsername(user)

assert.Equal(t, correct, name)
}

func TestResolveUserName(t *testing.T) {
username := &tgbotapi.User{ID: 1, FirstName: "John", UserName: "jsmith"}
correct := username.UserName[:1] + "" + username.UserName[1:]
name := ResolveUserName(username)
func TestZwspUsername(t *testing.T) {
user := &tgbotapi.User{ID: 1, FirstName: "John", UserName: "jsmith"}
correct := "j" + "" + "smith"
name := ZwspUsername(user)

assert.Equal(t, correct, name)
}

0 comments on commit 48bc3b7

Please sign in to comment.