Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ZWSP between Telegram usernames #322

Merged
merged 3 commits into from
May 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This needs to go into the env.example and our config glossary too.

NickServPassword string `env:"IRC_NICKSERV_PASS" envDefault:""`
NickServService string `env:"IRC_NICKSERV_SERVICE" envDefault:""`
EditedPrefix string `env:"IRC_EDITED_PREFIX" envDefault:"[EDIT] "`
Expand Down
19 changes: 13 additions & 6 deletions internal/handlers/telegram/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,15 @@ 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 := ""
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,
u.Message.From.String(),
username,
tg.Settings.Suffix,
// Trim unexpected trailing whitespace
strings.Trim(u.Message.Text, " "))
Expand All @@ -69,7 +75,7 @@ joinHandler handles when users join the Telegram group
func joinHandler(tg *Client, users *[]tgbotapi.User) {
if tg.IRCSettings.ShowJoinMessage {
for _, user := range *users {
username := GetUsername(&user)
username := GetFullUsername(&user)
formatted := username + " has joined the Telegram Group!"
tg.sendToIrc(formatted)
}
Expand All @@ -81,7 +87,7 @@ partHandler handles when users leave the Telegram group
*/
func partHandler(tg *Client, user *tgbotapi.User) {
if tg.IRCSettings.ShowLeaveMessage {
username := GetUsername(user)
username := GetFullUsername(user)
formatted := username + " has left the Telegram Group!"

tg.sendToIrc(formatted)
Expand All @@ -108,8 +114,8 @@ photoHandler handles the Message.Photo Telegram object. Only acknowledges Photo
exists, and sends notification to IRC
*/
func photoHandler(tg *Client, u tgbotapi.Update) {
user := u.Message.From
formatted := user.String() + " shared a photo on Telegram with caption: '" +
username := GetUsername(u.Message.From)
formatted := username + " shared a photo on Telegram with caption: '" +
u.Message.Caption + "'"

tg.sendToIrc(formatted)
Expand All @@ -120,7 +126,8 @@ documentHandler receives a document object from Telegram, and sends
a notification to IRC.
*/
func documentHandler(tg *Client, u *tgbotapi.Message) {
formatted := u.From.String() + " shared a file"
username := GetUsername(u.From)
formatted := username + " shared a file"
if u.Document.MimeType != "" {
formatted += " (" + u.Document.MimeType + ")"
}
Expand Down
39 changes: 38 additions & 1 deletion 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 All @@ -505,7 +508,7 @@ func TestMessageRandomWithoutUsername(t *testing.T) {
FirstName: "testing",
LastName: "123",
}
correct := fmt.Sprintf("<%s> Random Text", testUser.String())
correct := fmt.Sprintf("<%s> Random Text", testUser.FirstName)

updateObj := tgbotapi.Update{
Message: &tgbotapi.Message{
Expand All @@ -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
30 changes: 27 additions & 3 deletions internal/handlers/telegram/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,36 @@ import (
)

/*
GetUsername 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.
GetUsername returns a Telegram user's username if one is set, or first name otherwise.
*/
func GetUsername(u *tgbotapi.User) string {
if u.UserName == "" {
return u.FirstName
}
return u.FirstName + " (@" + u.UserName + ")"
// Add ZWSP to prevent pinging across platforms
// See https://github.com/42wim/matterbridge/issues/175
return u.UserName[:1] + "" + u.UserName[1:]
}

/*
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:] + ")"
jwflory marked this conversation as resolved.
Show resolved Hide resolved
}
jwflory marked this conversation as resolved.
Show resolved Hide resolved

/*
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
return u.UserName[:1] + "" + u.UserName[1:]
}
31 changes: 24 additions & 7 deletions internal/handlers/telegram/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,35 @@ import (
"testing"
)

func TestGetUsername(t *testing.T) {
username := &tgbotapi.User{ID: 1, FirstName: "John", UserName: "jsmith"}
correct := username.FirstName + " (@" + username.UserName + ")"
name := GetUsername(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 := GetUsername(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 TestZwspUsername(t *testing.T) {
user := &tgbotapi.User{ID: 1, FirstName: "John", UserName: "jsmith"}
correct := "j" + "" + "smith"
name := ZwspUsername(user)

assert.Equal(t, correct, name)
}