Support unread bool/int field
All checks were successful
the build was successful

This commit is contained in:
Peter Stuifzand 2019-01-03 21:34:36 +01:00
parent 5afdd56bb1
commit fa0569b63f
Signed by: peter
GPG Key ID: 374322D56E5209E8
4 changed files with 66 additions and 6 deletions

View File

@ -244,7 +244,7 @@ func performCommands(sub microsub.Microsub, commands []string) {
}
for _, ch := range channels {
fmt.Printf("%-20s %s\n", ch.UID, ch.Name)
fmt.Printf("%-20s %-30s %s\n", ch.UID, ch.Name, ch.Unread)
}
}

View File

@ -206,7 +206,7 @@ func (b *memoryBackend) ChannelsGetList() ([]microsub.Channel, error) {
}
}
util.StablePartition(channels, 0, len(channels), func(i int) bool {
return channels[i].Unread > 0
return channels[i].Unread.HasUnread()
})
return channels, nil
@ -656,7 +656,7 @@ func (b *memoryBackend) updateChannelUnreadCount(channel string) error {
return err
}
defer b.save()
c.Unread = unread
c.Unread = microsub.Unread{Type: microsub.UNREAD_COUNT, UnreadCount: unread}
b.lock.Lock()
b.Channels[channel] = c

View File

@ -19,6 +19,11 @@
// Package microsub describes the protocol methods of the Microsub protocol
package microsub
import (
"encoding/json"
"fmt"
)
/*
channels
search
@ -29,12 +34,23 @@ package microsub
block / unblock
*/
const (
UNREAD_BOOL = 1 << iota
UNREAD_COUNT
)
type Unread struct {
Type int
Unread bool
UnreadCount int
}
// Channel contains information about a channel.
type Channel struct {
// UID is a unique id for the channel
UID string `json:"uid"`
Name string `json:"name"`
Unread int `json:"unread"`
Unread Unread `json:"unread"`
}
type Card struct {
@ -132,3 +148,47 @@ type Microsub interface {
AddEventListener(el EventListener) error
}
func (unread *Unread) MarshalJSON() ([]byte, error) {
return nil, nil
}
func (unread *Unread) UnmarshalJSON(bytes []byte) error {
var b bool
err := json.Unmarshal(bytes, &b)
if err == nil {
unread.Type = UNREAD_BOOL
unread.Unread = b
return nil
}
var count int
err = json.Unmarshal(bytes, &count)
if err == nil {
unread.Type = UNREAD_COUNT
unread.UnreadCount = count
return nil
}
return fmt.Errorf("can't unmarshal as bool or int")
}
func (unread Unread) String() string {
switch unread.Type {
case UNREAD_BOOL:
return fmt.Sprint(unread.Unread)
case UNREAD_COUNT:
return fmt.Sprint(unread.UnreadCount)
}
return ""
}
func (unread *Unread) HasUnread() bool {
switch unread.Type {
case UNREAD_BOOL:
return unread.Unread
case UNREAD_COUNT:
return unread.UnreadCount > 0
}
return false
}

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: 0},
{UID: "0000", Name: "default", Unread: 0},
{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}},
}, nil
}