Timezone support

This commit is contained in:
bodqhrohro 2019-12-12 00:48:35 +02:00
parent 9e785a56d2
commit 74a872a30b
5 changed files with 61 additions and 20 deletions

View file

@ -3,6 +3,7 @@ package persistence
import ( import (
"github.com/pkg/errors" "github.com/pkg/errors"
"io/ioutil" "io/ioutil"
"time"
"dev.narayana.im/narayana/telegabber/yamldb" "dev.narayana.im/narayana/telegabber/yamldb"
@ -10,6 +11,16 @@ import (
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
var zeroLocation *time.Location
func init() {
var err error
zeroLocation, err = time.LoadLocation("")
if err != nil {
log.Fatal("Wrong hardcoded timezone")
}
}
// SessionsYamlDB wraps YamlDB with Session // SessionsYamlDB wraps YamlDB with Session
type SessionsYamlDB struct { type SessionsYamlDB struct {
yamldb.YamlDB yamldb.YamlDB
@ -108,3 +119,13 @@ func (s *Session) Set(key string, value string) (string, error) {
return "", errors.New("Unknown session property") return "", errors.New("Unknown session property")
} }
func (s *Session) TimezoneToLocation() *time.Location {
time, err := time.Parse("-07:00", s.Timezone)
if err == nil {
return time.Location()
}
// default
return zeroLocation
}

View file

@ -53,7 +53,7 @@ var chatCommands = map[string]command{
} }
var transportConfigurationOptions = map[string]configurationOption{ 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 { type command struct {

View file

@ -103,13 +103,13 @@ func (c *Client) updateHandler() {
// new user discovered // new user discovered
func (c *Client) updateUser(update *client.UpdateUser) { func (c *Client) updateUser(update *client.UpdateUser) {
c.cache.users[update.User.Id] = update.User 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) c.processStatusUpdate(int64(update.User.Id), status, show)
} }
// user status changed // user status changed
func (c *Client) updateUserStatus(update *client.UpdateUserStatus) { 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)) c.processStatusUpdate(int64(update.UserId), status, show, gateway.SPImmed(false))
} }

View file

@ -98,7 +98,7 @@ func (c *Client) GetContactByID(id int64, chat *client.Chat) (*client.Chat, *cli
return chat, user, nil return chat, user, nil
} }
func userStatusToText(status client.UserStatus) (string, string) { func (c *Client) userStatusToText(status client.UserStatus) (string, string) {
var show, textStatus string var show, textStatus string
switch status.UserStatusType() { switch status.UserStatusType() {
@ -121,8 +121,9 @@ func userStatusToText(status client.UserStatus) (string, string) {
} else { } else {
show = "xa" show = "xa"
} }
// TODO: timezone textStatus = time.Unix(int64(offlineStatus.WasOnline), 0).
textStatus = time.Unix(int64(offlineStatus.WasOnline), 0).Format("Last seen at 15:04 02/01/2006") In(c.Session.TimezoneToLocation()).
Format("Last seen at 15:04 02/01/2006")
} }
return show, textStatus return show, textStatus
@ -163,7 +164,7 @@ func (c *Client) processStatusUpdate(chatID int64, status string, show string, a
if status == "" { if status == "" {
if user != nil { if user != nil {
show, status = userStatusToText(user.Status) show, status = c.userStatusToText(user.Status)
} else { } else {
show, status = "chat", chat.Title show, status = "chat", chat.Title
} }
@ -229,9 +230,12 @@ func (c *Client) formatMessage(chatID int64, messageID int64, preview bool, mess
var str strings.Builder var str strings.Builder
str.WriteString(fmt.Sprintf("%v | %s | ", message.Id, c.formatContact(int64(message.SenderUserId)))) str.WriteString(fmt.Sprintf("%v | %s | ", message.Id, c.formatContact(int64(message.SenderUserId))))
// TODO: timezone
if !preview { 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 var text string

View file

@ -5,6 +5,7 @@ import (
"time" "time"
"dev.narayana.im/narayana/telegabber/config" "dev.narayana.im/narayana/telegabber/config"
"dev.narayana.im/narayana/telegabber/persistence"
"github.com/zelenin/go-tdlib/client" "github.com/zelenin/go-tdlib/client"
) )
@ -12,24 +13,30 @@ import (
const testTimeFormat string = "15:04 02/01/2006" const testTimeFormat string = "15:04 02/01/2006"
func TestOnlineStatus(t *testing.T) { func TestOnlineStatus(t *testing.T) {
show, status := userStatusToText(client.UserStatus(&client.UserStatusOnline{})) show, status := (&Client{}).userStatusToText(client.UserStatus(&client.UserStatusOnline{}))
if show != "" || status != "Online" { if show != "" || status != "Online" {
t.Errorf("Wrong online status: %v, %v", show, status) t.Errorf("Wrong online status: %v, %v", show, status)
} }
} }
func TestOnlineRecently(t *testing.T) { 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" { if show != "dnd" || status != "Last seen recently" {
t.Errorf("Wrong recently status: %v, %v", show, status) t.Errorf("Wrong recently status: %v, %v", show, status)
} }
} }
func TestOnlineOfflineAway(t *testing.T) { func TestOnlineOfflineAway(t *testing.T) {
timestamp := time.Now().Unix() - 3599 location, _ := time.LoadLocation("Europe/Berlin")
time := time.Unix(timestamp, 0) timestamp := time.Now().In(location).Unix() - 3599
show, status := userStatusToText(client.UserStatus(&client.UserStatusOffline{WasOnline: int32(timestamp)})) tm := time.Unix(timestamp, 0).In(location)
trueStatus := "Last seen at " + time.Format(testTimeFormat) 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 { if show != "away" || status != trueStatus {
t.Errorf("Wrong away status: %v, %v, should be %v", show, 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) { func TestOnlineOfflineXa(t *testing.T) {
timestamp := time.Now().Unix() - 3601 timestamp := time.Now().Unix() - 3601
time := time.Unix(timestamp, 0) tm := time.Unix(timestamp, 0).UTC()
show, status := userStatusToText(client.UserStatus(&client.UserStatusOffline{WasOnline: int32(timestamp)})) c := &Client{
trueStatus := "Last seen at " + time.Format(testTimeFormat) 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 { if show != "xa" || status != trueStatus {
t.Errorf("Wrong xa status: %v, %v, should be %v", show, 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" { if text != "42 | | 10 Jan 2008 21:20:00 | tist" {
t.Errorf("Wrong oneline preview message formatting: %v", text) 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" { if text != "42 | | 10 Jan 2008 21:20:00 | tist\nziz" {
t.Errorf("Wrong multiline preview message formatting: %v", text) t.Errorf("Wrong multiline preview message formatting: %v", text)
} }