Skip to content

Commit

Permalink
print dates in human readable form
Browse files Browse the repository at this point in the history
  • Loading branch information
ivarprudnikov committed Apr 24, 2024
1 parent 21f5880 commit 4908e34
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 19 deletions.
16 changes: 8 additions & 8 deletions internal/storage/memstore/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,37 +43,37 @@ func (s *memMessageStore) Decrypt(ciphertext string, pass string) (string, error
return plaintext, nil
}

func (s *memMessageStore) ListMessages(username string) ([]storage.Message, error) {
var msgs []storage.Message
func (s *memMessageStore) ListMessages(username string) ([]*storage.Message, error) {
var msgs []*storage.Message
s.messages.Range(func(k, v any) bool {
if msg, ok := v.(storage.Message); ok && msg.RowKey == username {
msgs = append(msgs, msg)
msgs = append(msgs, &msg)
}
return true
})
return msgs, nil
}

// TODO: allow to reset the pin for the owner
func (s *memMessageStore) AddMessage(text string, username string) (storage.Message, error) {
func (s *memMessageStore) AddMessage(text string, username string) (*storage.Message, error) {
// an easy to enter pin
pin, err := storage.MakePin()
if err != nil {
return storage.Message{}, err
return nil, err
}
ciphertext, err := s.Encrypt(text, pin)
if err != nil {
return storage.Message{}, err
return nil, err
}
msg, err := storage.NewMessage(username, ciphertext, pin)
if err != nil {
return storage.Message{}, err
return nil, err
}
// store unreadbale message, pin
s.messages.Store(msg.Entity.PartitionKey, msg)
// temporarily show the pin to the creator
msg.Pin = pin
return msg, nil
return &msg, nil
}

func (s *memMessageStore) GetMessage(id string) (*storage.Message, error) {
Expand Down
8 changes: 4 additions & 4 deletions internal/storage/memstore/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ func NewMemUserStore(salt string) storage.UserStore {
return &memUserStore{users: sync.Map{}, salt: salt}
}

func (u *memUserStore) AddUser(username string, password string) (storage.User, error) {
func (u *memUserStore) AddUser(username string, password string) (*storage.User, error) {
if _, ok := u.users.Load(username); ok {
return storage.User{}, errors.New("username is not available")
return nil, errors.New("username is not available")
}
usr, err := storage.NewUser(username, password)
if err != nil {
return storage.User{}, err
return nil, err
}
u.users.Store(usr.PartitionKey, usr)
return usr, nil
return &usr, nil
}

func (u *memUserStore) GetUser(username string) (*storage.User, error) {
Expand Down
9 changes: 7 additions & 2 deletions internal/storage/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
const MAX_PIN_ATTEMPTS = 5

type MessageStore interface {
ListMessages(username string) ([]Message, error)
AddMessage(text string, username string) (Message, error)
ListMessages(username string) ([]*Message, error)
AddMessage(text string, username string) (*Message, error)
GetMessage(id string) (*Message, error)
GetFullMessage(id string, pin string) (*Message, error)
Encrypt(text string, pass string) (string, error)
Expand All @@ -24,6 +24,11 @@ type Message struct {
AttemptsRemaining int `json:"remaining,omitempty"`
}

func (m *Message) FormattedDate() string {
t := time.Time(m.Timestamp)
return t.Format(time.RFC822)
}

func NewMessage(username string, ciphertext string, pin string) (Message, error) {
pinHash, err := HashPass(pin)
if err != nil {
Expand Down
9 changes: 7 additions & 2 deletions internal/storage/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ import (
)

type UserStore interface {
AddUser(username string, password string) (User, error)
AddUser(username string, password string) (*User, error)
GetUser(username string) (*User, error)
GetUserWithPass(username string, password string) (*User, error)
}

type User struct {
aztables.Entity
Password string `json:"password"`
Password string `json:"password"`
}

func (u *User) FormattedDate() string {
t := time.Time(u.Timestamp)
return t.Format(time.RFC822)
}

func NewUser(username string, password string) (User, error) {
Expand Down
2 changes: 1 addition & 1 deletion web/account.created.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<h1>Account created</h1>
<p>Your username: {{ .data.PartitionKey }}</p>
<p>Created at: {{ .data.Timestamp }}</p>
<p>Created at: {{ .data.FormattedDate }}</p>

<a href="/accounts/login" class="btn btn-primary">Login</a>

Expand Down
2 changes: 1 addition & 1 deletion web/message.list.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
{{range .data}}
<tr class="message-row">
<td><a href="/messages/{{ .PartitionKey }}">{{ .PartitionKey }}</a></td>
<td>{{ .Timestamp }}</td>
<td>{{ .FormattedDate }}</td>
</tr>
{{end}}

Expand Down
2 changes: 1 addition & 1 deletion web/message.show.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<h1>Secret message</h1>
<p>ID: {{ .data.PartitionKey }}</p>
<p>Created at: {{ .data.Timestamp }}</p>
<p>Created at: {{ .data.FormattedDate }}</p>

{{if .data.Pin}}
<h3>Content</h3>
Expand Down

0 comments on commit 4908e34

Please sign in to comment.