Cleanup and tests for unread counts
All checks were successful
the build was successful

This commit is contained in:
Peter Stuifzand 2019-01-03 22:06:16 +01:00
parent fa0569b63f
commit f3f5d30385
Signed by: peter
GPG Key ID: 374322D56E5209E8
4 changed files with 23 additions and 16 deletions

View File

@ -656,7 +656,7 @@ func (b *memoryBackend) updateChannelUnreadCount(channel string) error {
return err
}
defer b.save()
c.Unread = microsub.Unread{Type: microsub.UNREAD_COUNT, UnreadCount: unread}
c.Unread = microsub.Unread{Type: microsub.UnreadCount, UnreadCount: unread}
b.lock.Lock()
b.Channels[channel] = c

View File

@ -35,8 +35,8 @@ import (
*/
const (
UNREAD_BOOL = 1 << iota
UNREAD_COUNT
UnreadBool = 0
UnreadCount = 1
)
type Unread struct {
@ -50,7 +50,7 @@ type Channel struct {
// UID is a unique id for the channel
UID string `json:"uid"`
Name string `json:"name"`
Unread Unread `json:"unread"`
Unread Unread `json:"unread,omitempty"`
}
type Card struct {
@ -149,15 +149,21 @@ type Microsub interface {
AddEventListener(el EventListener) error
}
func (unread *Unread) MarshalJSON() ([]byte, error) {
return nil, nil
func (unread Unread) MarshalJSON() ([]byte, error) {
switch unread.Type {
case UnreadBool:
return json.Marshal(unread.Unread)
case UnreadCount:
return json.Marshal(unread.UnreadCount)
}
return json.Marshal(nil)
}
func (unread *Unread) UnmarshalJSON(bytes []byte) error {
var b bool
err := json.Unmarshal(bytes, &b)
if err == nil {
unread.Type = UNREAD_BOOL
unread.Type = UnreadBool
unread.Unread = b
return nil
}
@ -165,7 +171,7 @@ func (unread *Unread) UnmarshalJSON(bytes []byte) error {
var count int
err = json.Unmarshal(bytes, &count)
if err == nil {
unread.Type = UNREAD_COUNT
unread.Type = UnreadCount
unread.UnreadCount = count
return nil
}
@ -175,9 +181,9 @@ func (unread *Unread) UnmarshalJSON(bytes []byte) error {
func (unread Unread) String() string {
switch unread.Type {
case UNREAD_BOOL:
case UnreadBool:
return fmt.Sprint(unread.Unread)
case UNREAD_COUNT:
case UnreadCount:
return fmt.Sprint(unread.UnreadCount)
}
return ""
@ -185,9 +191,9 @@ func (unread Unread) String() string {
func (unread *Unread) HasUnread() bool {
switch unread.Type {
case UNREAD_BOOL:
case UnreadBool:
return unread.Unread
case UNREAD_COUNT:
case UnreadCount:
return unread.UnreadCount > 0
}
return false

View File

@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/assert"
"p83.nl/go/ekster/pkg/client"
"p83.nl/go/ekster/pkg/microsub"
)
func createServerClient() (*httptest.Server, *client.Client) {
@ -39,11 +40,11 @@ func TestServer_ChannelsGetList(t *testing.T) {
assert.Equal(t, "notifications", channels[0].Name)
assert.Equal(t, "0001", channels[0].UID)
assert.Equal(t, 0, channels[0].Unread)
assert.Equal(t, microsub.Unread{Type: microsub.UnreadBool}, channels[0].Unread)
assert.Equal(t, "default", channels[1].Name)
assert.Equal(t, "0000", channels[1].UID)
assert.Equal(t, 0, channels[1].Unread)
assert.Equal(t, microsub.Unread{Type: microsub.UnreadBool}, channels[0].Unread)
}
}

View File

@ -32,8 +32,8 @@ func (b *NullBackend) AddEventListener(el microsub.EventListener) error {
// ChannelsGetList gets no channels
func (b *NullBackend) ChannelsGetList() ([]microsub.Channel, error) {
return []microsub.Channel{
{UID: "0001", Name: "notifications", Unread: microsub.Unread{Type: microsub.UNREAD_BOOL, Unread: false}},
{UID: "0000", Name: "default", Unread: microsub.Unread{Type: microsub.UNREAD_COUNT, UnreadCount: 0}},
{UID: "0001", Name: "notifications", Unread: microsub.Unread{Type: microsub.UnreadBool, Unread: false}},
{UID: "0000", Name: "default", Unread: microsub.Unread{Type: microsub.UnreadCount, UnreadCount: 0}},
}, nil
}