Timezone support
This commit is contained in:
parent
9e785a56d2
commit
74a872a30b
|
@ -3,6 +3,7 @@ package persistence
|
|||
import (
|
||||
"github.com/pkg/errors"
|
||||
"io/ioutil"
|
||||
"time"
|
||||
|
||||
"dev.narayana.im/narayana/telegabber/yamldb"
|
||||
|
||||
|
@ -10,6 +11,16 @@ import (
|
|||
"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
|
||||
type SessionsYamlDB struct {
|
||||
yamldb.YamlDB
|
||||
|
@ -108,3 +119,13 @@ func (s *Session) Set(key string, value string) (string, error) {
|
|||
|
||||
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
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue