Move timeline backend to own package
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Peter Stuifzand 2019-03-23 20:42:13 +01:00
parent 5b5b344f82
commit a23b31cefe
Signed by: peter
GPG Key ID: 374322D56E5209E8
7 changed files with 75 additions and 76 deletions

View File

@ -60,19 +60,6 @@ type Debug interface {
Debug()
}
type redisItem struct {
ID string
Published string
Read bool
Data []byte
}
func (ri *redisItem) Item() microsub.Item {
var item microsub.Item
_ = json.Unmarshal(ri.Data, &item)
return item
}
type fetch2 struct{}
func (f *fetch2) Fetch(url string) (*http.Response, error) {
@ -743,7 +730,7 @@ func (b *memoryBackend) createChannel(name string) microsub.Channel {
channel := microsub.Channel{
UID: uid,
Name: name,
Unread: microsub.Unread{microsub.UnreadCount, false, 0},
Unread: microsub.Unread{Type: microsub.UnreadCount},
}
return channel
}

View File

@ -1,24 +1,10 @@
package main
import (
"p83.nl/go/ekster/pkg/microsub"
"p83.nl/go/ekster/pkg/timeline"
)
// TimelineBackend specifies the interface for Timeline. It supports everything that is needed
// for Ekster to implement the channel protocol for Microsub
type TimelineBackend interface {
Items(before, after string) (microsub.Timeline, error)
Count() (int, error)
AddItem(item microsub.Item) error
MarkRead(uids []string) error
// Not used at the moment
// MarkUnread(uids []string) error
}
func (b *memoryBackend) getTimeline(channel string) TimelineBackend {
// TODO: fetch timeline type from channel
func (b *memoryBackend) getTimeline(channel string) timeline.Backend {
timelineType := "sorted-set"
if channel == "notifications" {
timelineType = "stream"
@ -30,29 +16,5 @@ func (b *memoryBackend) getTimeline(channel string) TimelineBackend {
}
}
if timelineType == "sorted-set" {
timeline := &redisSortedSetTimeline{channel: channel, pool: b.pool}
err := timeline.Init()
if err != nil {
return nil
}
return timeline
}
if timelineType == "stream" {
timeline := &redisStreamTimeline{channel: channel, pool: b.pool}
err := timeline.Init()
if err != nil {
return nil
}
return timeline
}
if timelineType == "null" {
timeline := &nullTimeline{channel: channel}
err := timeline.Init()
if err != nil {
return nil
}
return timeline
}
return nil
return timeline.Create(channel, timelineType, b.pool)
}

View File

@ -1,20 +1,3 @@
/*
Microsub server
Copyright (C) 2018 Peter Stuifzand
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package server
import (
@ -82,7 +65,7 @@ func (b *NullBackend) UnfollowURL(uid string, url string) error {
// Search search for a query and return an example list of feeds
func (b *NullBackend) Search(query string) ([]microsub.Feed, error) {
return []microsub.Feed{
{"feed", "https://example.com/", "Example", "test.jpg", "test", microsub.Card{}},
{Type: "feed", URL: "https://example.com/", Name: "Example", Photo: "test.jpg", Description: "test"},
}, nil
}

View File

@ -1,4 +1,4 @@
package main
package timeline
import "p83.nl/go/ekster/pkg/microsub"

View File

@ -1,4 +1,4 @@
package main
package timeline
import (
"encoding/json"

View File

@ -1,4 +1,4 @@
package main
package timeline
import (
"encoding/json"

67
pkg/timeline/timeline.go Normal file
View File

@ -0,0 +1,67 @@
package timeline
import (
"encoding/json"
"github.com/gomodule/redigo/redis"
"p83.nl/go/ekster/pkg/microsub"
)
// Backend specifies the interface for Timeline. It supports everything that is needed
// for Ekster to implement the channel protocol for Microsub
type Backend interface {
Items(before, after string) (microsub.Timeline, error)
Count() (int, error)
AddItem(item microsub.Item) error
MarkRead(uids []string) error
// Not used at the moment
// MarkUnread(uids []string) error
}
// Create creates a channel of the specfied type. Return nil when the type
// is not known.
func Create(channel, timelineType string, pool *redis.Pool) Backend {
if timelineType == "sorted-set" {
timeline := &redisSortedSetTimeline{channel: channel, pool: pool}
err := timeline.Init()
if err != nil {
return nil
}
return timeline
}
if timelineType == "stream" {
timeline := &redisStreamTimeline{channel: channel, pool: pool}
err := timeline.Init()
if err != nil {
return nil
}
return timeline
}
if timelineType == "null" {
timeline := &nullTimeline{channel: channel}
err := timeline.Init()
if err != nil {
return nil
}
return timeline
}
return nil
}
type redisItem struct {
ID string
Published string
Read bool
Data []byte
}
func (ri *redisItem) Item() microsub.Item {
var item microsub.Item
_ = json.Unmarshal(ri.Data, &item)
return item
}