Compare commits

...

2 Commits

Author SHA1 Message Date
c49afabaa8
add removeFeed to remove feed from channels
All checks were successful
continuous-integration/drone/push Build is passing
2021-10-21 21:21:38 +02:00
fff2a92b72
breaking change: make "postgres-stream" the default channel type 2021-10-21 20:03:03 +02:00
3 changed files with 128 additions and 16 deletions

View File

@ -374,13 +374,8 @@ func (h *mainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} else {
page.CurrentSetting = channelSetting{}
}
// FIXME: similar code is found in timeline.go
if page.CurrentSetting.ChannelType == "" {
if v.UID == "notifications" {
page.CurrentSetting.ChannelType = "stream"
} else {
page.CurrentSetting.ChannelType = "sorted-set"
}
page.CurrentSetting.ChannelType = "postgres-stream"
}
page.ExcludedTypeNames = map[string]string{
"repost": "Reposts",

View File

@ -290,6 +290,21 @@ func (b *memoryBackend) ChannelsDelete(uid string) error {
return nil
}
func (b *memoryBackend) removeFeed(feedID string) error {
b.lock.Lock()
for uid := range b.Channels {
feeds := b.Feeds[uid]
for i, feed := range feeds {
if feed.URL == feedID {
feeds = append(feeds[:i], feeds[i+1:]...)
}
}
b.Feeds[uid] = feeds
}
b.lock.Unlock()
return nil
}
func (b *memoryBackend) getFeeds() map[string][]string {
feeds := make(map[string][]string)
b.lock.RLock()
@ -832,17 +847,11 @@ func Fetch2(fetchURL string) (*http.Response, error) {
}
func (b *memoryBackend) getTimeline(channel string) timeline.Backend {
timelineType := "sorted-set"
if channel == "notifications" {
timelineType = "stream"
} else {
if setting, ok := b.Settings[channel]; ok {
if setting.ChannelType != "" {
timelineType = setting.ChannelType
}
}
// Set a default timeline type if not set
timelineType := "postgres-stream"
if setting, ok := b.Settings[channel]; ok && setting.ChannelType != "" {
timelineType = setting.ChannelType
}
tl := timeline.Create(channel, timelineType, b.pool, b.database)
if tl == nil {
log.Printf("no timeline found with name %q and type %q", channel, timelineType)

View File

@ -7,6 +7,7 @@ import (
"time"
"github.com/gomodule/redigo/redis"
"github.com/stretchr/testify/assert"
"p83.nl/go/ekster/pkg/microsub"
"p83.nl/go/ekster/pkg/sse"
)
@ -112,3 +113,110 @@ func Test_memoryBackend_ChannelsCreate(t *testing.T) {
})
}
}
func Test_memoryBackend_removeFeed(t *testing.T) {
type fields struct {
Channels map[string]microsub.Channel
Feeds map[string][]microsub.Feed
}
type args struct {
feedID string
}
tests := []struct {
name string
fields fields
args args
lens map[string]int
wantErr bool
}{
{
name: "remove from channel 1",
fields: fields{
Channels: map[string]microsub.Channel{
"123": {UID: "channel1", Name: "Channel 1"},
"124": {UID: "channel2", Name: "Channel 2"},
},
Feeds: map[string][]microsub.Feed{
"123": {{Type: "feed", URL: "feed1", Name: "Feed1"}},
"124": {{Type: "feed", URL: "feed2", Name: "Feed2"}},
},
},
args: args{feedID: "feed1"},
lens: map[string]int{"123": 0, "124": 1},
wantErr: false,
},
{
name: "remove from channel 2",
fields: fields{
Channels: map[string]microsub.Channel{
"123": {UID: "channel1", Name: "Channel 1"},
"124": {UID: "channel2", Name: "Channel 2"},
},
Feeds: map[string][]microsub.Feed{
"123": {{Type: "feed", URL: "feed1", Name: "Feed1"}},
"124": {{Type: "feed", URL: "feed2", Name: "Feed2"}},
},
},
args: args{feedID: "feed2"},
lens: map[string]int{"123": 1, "124": 0},
wantErr: false,
},
{
name: "remove unknown",
fields: fields{
Channels: map[string]microsub.Channel{
"123": {UID: "channel1", Name: "Channel 1"},
"124": {UID: "channel2", Name: "Channel 2"},
},
Feeds: map[string][]microsub.Feed{
"123": {{Type: "feed", URL: "feed1", Name: "Feed1"}},
"124": {{Type: "feed", URL: "feed2", Name: "Feed2"}},
},
},
args: args{feedID: "feed3"},
lens: map[string]int{"123": 1, "124": 1},
wantErr: false,
},
{
name: "remove from 0 channels",
fields: fields{
Channels: map[string]microsub.Channel{},
Feeds: map[string][]microsub.Feed{},
},
args: args{feedID: "feed3"},
lens: map[string]int{},
wantErr: false,
},
{
name: "remove from multiple channels",
fields: fields{
Channels: map[string]microsub.Channel{
"123": {UID: "channel1", Name: "Channel 1"},
"124": {UID: "channel2", Name: "Channel 2"},
},
Feeds: map[string][]microsub.Feed{
"123": {{Type: "feed", URL: "feed1", Name: "Feed1"}},
"124": {{Type: "feed", URL: "feed1", Name: "Feed1"}},
},
},
args: args{feedID: "feed1"},
lens: map[string]int{"123": 0, "124": 0},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := &memoryBackend{
Channels: tt.fields.Channels,
Feeds: tt.fields.Feeds,
}
if err := b.removeFeed(tt.args.feedID); (err != nil) != tt.wantErr {
t.Errorf("removeFeed() error = %v, wantErr %v", err, tt.wantErr)
}
assert.Len(t, b.Channels, len(tt.lens))
for k, v := range tt.lens {
assert.Len(t, b.Feeds[k], v)
}
})
}
}