aboutsummaryrefslogtreecommitdiff
path: root/telegram
diff options
context:
space:
mode:
authorbodqhrohro <bodqhrohro@gmail.com>2019-12-12 01:48:35 +0300
committerbodqhrohro <bodqhrohro@gmail.com>2019-12-12 01:48:35 +0300
commit74a872a30bf023cf37d9d3d30e28c25d84c69ffe (patch)
tree1ae2497e083b21971fb027c53056301012213113 /telegram
parent9e785a56d29f617a29afb941476c73f43bcdb3cc (diff)
Timezone support
Diffstat (limited to 'telegram')
-rw-r--r--telegram/commands.go2
-rw-r--r--telegram/handlers.go4
-rw-r--r--telegram/utils.go16
-rw-r--r--telegram/utils_test.go38
4 files changed, 40 insertions, 20 deletions
diff --git a/telegram/commands.go b/telegram/commands.go
index 678570f..04c6e3a 100644
--- a/telegram/commands.go
+++ b/telegram/commands.go
@@ -53,7 +53,7 @@ var chatCommands = map[string]command{
}
var transportConfigurationOptions = map[string]configurationOption{
- //"timezone": configurationOption{"00:00", "adjust timezone for Telegram user statuses"}
+ "timezone": configurationOption{"00:00", "adjust timezone for Telegram user statuses"},
}
type command struct {
diff --git a/telegram/handlers.go b/telegram/handlers.go
index 0cc3fad..d6aba94 100644
--- a/telegram/handlers.go
+++ b/telegram/handlers.go
@@ -103,13 +103,13 @@ func (c *Client) updateHandler() {
// new user discovered
func (c *Client) updateUser(update *client.UpdateUser) {
c.cache.users[update.User.Id] = update.User
- show, status := userStatusToText(update.User.Status)
+ show, status := c.userStatusToText(update.User.Status)
c.processStatusUpdate(int64(update.User.Id), status, show)
}
// user status changed
func (c *Client) updateUserStatus(update *client.UpdateUserStatus) {
- show, status := userStatusToText(update.Status)
+ show, status := c.userStatusToText(update.Status)
c.processStatusUpdate(int64(update.UserId), status, show, gateway.SPImmed(false))
}
diff --git a/telegram/utils.go b/telegram/utils.go
index 8b8da57..eb7b905 100644
--- a/telegram/utils.go
+++ b/telegram/utils.go
@@ -98,7 +98,7 @@ func (c *Client) GetContactByID(id int64, chat *client.Chat) (*client.Chat, *cli
return chat, user, nil
}
-func userStatusToText(status client.UserStatus) (string, string) {
+func (c *Client) userStatusToText(status client.UserStatus) (string, string) {
var show, textStatus string
switch status.UserStatusType() {
@@ -121,8 +121,9 @@ func userStatusToText(status client.UserStatus) (string, string) {
} else {
show = "xa"
}
- // TODO: timezone
- textStatus = time.Unix(int64(offlineStatus.WasOnline), 0).Format("Last seen at 15:04 02/01/2006")
+ textStatus = time.Unix(int64(offlineStatus.WasOnline), 0).
+ In(c.Session.TimezoneToLocation()).
+ Format("Last seen at 15:04 02/01/2006")
}
return show, textStatus
@@ -163,7 +164,7 @@ func (c *Client) processStatusUpdate(chatID int64, status string, show string, a
if status == "" {
if user != nil {
- show, status = userStatusToText(user.Status)
+ show, status = c.userStatusToText(user.Status)
} else {
show, status = "chat", chat.Title
}
@@ -229,9 +230,12 @@ func (c *Client) formatMessage(chatID int64, messageID int64, preview bool, mess
var str strings.Builder
str.WriteString(fmt.Sprintf("%v | %s | ", message.Id, c.formatContact(int64(message.SenderUserId))))
- // TODO: timezone
if !preview {
- str.WriteString(time.Unix(int64(message.Date), 0).UTC().Format("02 Jan 2006 15:04:05 | "))
+ str.WriteString(
+ time.Unix(int64(message.Date), 0).
+ In(c.Session.TimezoneToLocation()).
+ Format("02 Jan 2006 15:04:05 | "),
+ )
}
var text string
diff --git a/telegram/utils_test.go b/telegram/utils_test.go
index 7371f7f..69124fc 100644
--- a/telegram/utils_test.go
+++ b/telegram/utils_test.go
@@ -5,6 +5,7 @@ import (
"time"
"dev.narayana.im/narayana/telegabber/config"
+ "dev.narayana.im/narayana/telegabber/persistence"
"github.com/zelenin/go-tdlib/client"
)
@@ -12,24 +13,30 @@ import (
const testTimeFormat string = "15:04 02/01/2006"
func TestOnlineStatus(t *testing.T) {
- show, status := userStatusToText(client.UserStatus(&client.UserStatusOnline{}))
+ show, status := (&Client{}).userStatusToText(client.UserStatus(&client.UserStatusOnline{}))
if show != "" || status != "Online" {
t.Errorf("Wrong online status: %v, %v", show, status)
}
}
func TestOnlineRecently(t *testing.T) {
- show, status := userStatusToText(client.UserStatus(&client.UserStatusRecently{}))
+ show, status := (&Client{}).userStatusToText(client.UserStatus(&client.UserStatusRecently{}))
if show != "dnd" || status != "Last seen recently" {
t.Errorf("Wrong recently status: %v, %v", show, status)
}
}
func TestOnlineOfflineAway(t *testing.T) {
- timestamp := time.Now().Unix() - 3599
- time := time.Unix(timestamp, 0)
- show, status := userStatusToText(client.UserStatus(&client.UserStatusOffline{WasOnline: int32(timestamp)}))
- trueStatus := "Last seen at " + time.Format(testTimeFormat)
+ location, _ := time.LoadLocation("Europe/Berlin")
+ timestamp := time.Now().In(location).Unix() - 3599
+ tm := time.Unix(timestamp, 0).In(location)
+ c := &Client{
+ Session: &persistence.Session{
+ Timezone: "+01:00",
+ },
+ }
+ show, status := c.userStatusToText(client.UserStatus(&client.UserStatusOffline{WasOnline: int32(timestamp)}))
+ trueStatus := "Last seen at " + tm.Format(testTimeFormat)
if show != "away" || status != trueStatus {
t.Errorf("Wrong away status: %v, %v, should be %v", show, status, trueStatus)
}
@@ -37,9 +44,12 @@ func TestOnlineOfflineAway(t *testing.T) {
func TestOnlineOfflineXa(t *testing.T) {
timestamp := time.Now().Unix() - 3601
- time := time.Unix(timestamp, 0)
- show, status := userStatusToText(client.UserStatus(&client.UserStatusOffline{WasOnline: int32(timestamp)}))
- trueStatus := "Last seen at " + time.Format(testTimeFormat)
+ tm := time.Unix(timestamp, 0).UTC()
+ c := &Client{
+ Session: &persistence.Session{},
+ }
+ show, status := c.userStatusToText(client.UserStatus(&client.UserStatusOffline{WasOnline: int32(timestamp)}))
+ trueStatus := "Last seen at " + tm.Format(testTimeFormat)
if show != "xa" || status != trueStatus {
t.Errorf("Wrong xa status: %v, %v, should be %v", show, status, trueStatus)
}
@@ -88,7 +98,10 @@ func TestFormatMessageOnelinePreview(t *testing.T) {
},
}
- text := (&Client{}).formatMessage(0, 0, false, &message)
+ c := &Client{
+ Session: &persistence.Session{},
+ }
+ text := c.formatMessage(0, 0, false, &message)
if text != "42 | | 10 Jan 2008 21:20:00 | tist" {
t.Errorf("Wrong oneline preview message formatting: %v", text)
}
@@ -105,7 +118,10 @@ func TestFormatMessageMultilinePreview(t *testing.T) {
},
}
- text := (&Client{}).formatMessage(0, 0, false, &message)
+ c := &Client{
+ Session: &persistence.Session{},
+ }
+ text := c.formatMessage(0, 0, false, &message)
if text != "42 | | 10 Jan 2008 21:20:00 | tist\nziz" {
t.Errorf("Wrong multiline preview message formatting: %v", text)
}