This commit is contained in:
parent
fa0569b63f
commit
f3f5d30385
|
|
@ -656,7 +656,7 @@ func (b *memoryBackend) updateChannelUnreadCount(channel string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer b.save()
|
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.lock.Lock()
|
||||||
b.Channels[channel] = c
|
b.Channels[channel] = c
|
||||||
|
|
|
||||||
|
|
@ -35,8 +35,8 @@ import (
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const (
|
const (
|
||||||
UNREAD_BOOL = 1 << iota
|
UnreadBool = 0
|
||||||
UNREAD_COUNT
|
UnreadCount = 1
|
||||||
)
|
)
|
||||||
|
|
||||||
type Unread struct {
|
type Unread struct {
|
||||||
|
|
@ -50,7 +50,7 @@ type Channel struct {
|
||||||
// UID is a unique id for the channel
|
// UID is a unique id for the channel
|
||||||
UID string `json:"uid"`
|
UID string `json:"uid"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Unread Unread `json:"unread"`
|
Unread Unread `json:"unread,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Card struct {
|
type Card struct {
|
||||||
|
|
@ -149,15 +149,21 @@ type Microsub interface {
|
||||||
AddEventListener(el EventListener) error
|
AddEventListener(el EventListener) error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (unread *Unread) MarshalJSON() ([]byte, error) {
|
func (unread Unread) MarshalJSON() ([]byte, error) {
|
||||||
return nil, nil
|
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 {
|
func (unread *Unread) UnmarshalJSON(bytes []byte) error {
|
||||||
var b bool
|
var b bool
|
||||||
err := json.Unmarshal(bytes, &b)
|
err := json.Unmarshal(bytes, &b)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
unread.Type = UNREAD_BOOL
|
unread.Type = UnreadBool
|
||||||
unread.Unread = b
|
unread.Unread = b
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
@ -165,7 +171,7 @@ func (unread *Unread) UnmarshalJSON(bytes []byte) error {
|
||||||
var count int
|
var count int
|
||||||
err = json.Unmarshal(bytes, &count)
|
err = json.Unmarshal(bytes, &count)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
unread.Type = UNREAD_COUNT
|
unread.Type = UnreadCount
|
||||||
unread.UnreadCount = count
|
unread.UnreadCount = count
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
@ -175,9 +181,9 @@ func (unread *Unread) UnmarshalJSON(bytes []byte) error {
|
||||||
|
|
||||||
func (unread Unread) String() string {
|
func (unread Unread) String() string {
|
||||||
switch unread.Type {
|
switch unread.Type {
|
||||||
case UNREAD_BOOL:
|
case UnreadBool:
|
||||||
return fmt.Sprint(unread.Unread)
|
return fmt.Sprint(unread.Unread)
|
||||||
case UNREAD_COUNT:
|
case UnreadCount:
|
||||||
return fmt.Sprint(unread.UnreadCount)
|
return fmt.Sprint(unread.UnreadCount)
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
|
|
@ -185,9 +191,9 @@ func (unread Unread) String() string {
|
||||||
|
|
||||||
func (unread *Unread) HasUnread() bool {
|
func (unread *Unread) HasUnread() bool {
|
||||||
switch unread.Type {
|
switch unread.Type {
|
||||||
case UNREAD_BOOL:
|
case UnreadBool:
|
||||||
return unread.Unread
|
return unread.Unread
|
||||||
case UNREAD_COUNT:
|
case UnreadCount:
|
||||||
return unread.UnreadCount > 0
|
return unread.UnreadCount > 0
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import (
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"p83.nl/go/ekster/pkg/client"
|
"p83.nl/go/ekster/pkg/client"
|
||||||
|
"p83.nl/go/ekster/pkg/microsub"
|
||||||
)
|
)
|
||||||
|
|
||||||
func createServerClient() (*httptest.Server, *client.Client) {
|
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, "notifications", channels[0].Name)
|
||||||
assert.Equal(t, "0001", channels[0].UID)
|
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, "default", channels[1].Name)
|
||||||
assert.Equal(t, "0000", channels[1].UID)
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -32,8 +32,8 @@ func (b *NullBackend) AddEventListener(el microsub.EventListener) error {
|
||||||
// ChannelsGetList gets no channels
|
// ChannelsGetList gets no channels
|
||||||
func (b *NullBackend) ChannelsGetList() ([]microsub.Channel, error) {
|
func (b *NullBackend) ChannelsGetList() ([]microsub.Channel, error) {
|
||||||
return []microsub.Channel{
|
return []microsub.Channel{
|
||||||
{UID: "0001", Name: "notifications", Unread: microsub.Unread{Type: microsub.UNREAD_BOOL, Unread: false}},
|
{UID: "0001", Name: "notifications", Unread: microsub.Unread{Type: microsub.UnreadBool, Unread: false}},
|
||||||
{UID: "0000", Name: "default", Unread: microsub.Unread{Type: microsub.UNREAD_COUNT, UnreadCount: 0}},
|
{UID: "0000", Name: "default", Unread: microsub.Unread{Type: microsub.UnreadCount, UnreadCount: 0}},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user