mirror of
https://github.com/gabehf/Koito.git
synced 2026-03-12 08:50:40 -07:00
feat: add now playing endpoint and ui (#93)
* wip * feat: add now playing
This commit is contained in:
parent
0b7ecb0b96
commit
a4689bed27
8 changed files with 265 additions and 9 deletions
|
|
@ -8,12 +8,14 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gabehf/koito/internal/db"
|
||||
"github.com/gabehf/koito/internal/logger"
|
||||
"github.com/gabehf/koito/internal/mbz"
|
||||
"github.com/gabehf/koito/internal/memkv"
|
||||
"github.com/gabehf/koito/internal/models"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
|
@ -56,8 +58,9 @@ type SubmitListenOpts struct {
|
|||
ReleaseGroupMbzID uuid.UUID
|
||||
Time time.Time
|
||||
|
||||
UserID int32
|
||||
Client string
|
||||
UserID int32
|
||||
Client string
|
||||
IsNowPlaying bool
|
||||
}
|
||||
|
||||
const (
|
||||
|
|
@ -165,6 +168,14 @@ func SubmitListen(ctx context.Context, store db.DB, opts SubmitListenOpts) error
|
|||
}
|
||||
}
|
||||
|
||||
if opts.IsNowPlaying {
|
||||
if track.Duration == 0 {
|
||||
memkv.Store.Set(strconv.Itoa(int(opts.UserID)), track.ID)
|
||||
} else {
|
||||
memkv.Store.Set(strconv.Itoa(int(opts.UserID)), track.ID, time.Duration(track.Duration)*time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
if opts.SkipSaveListen {
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
110
internal/memkv/memkv.go
Normal file
110
internal/memkv/memkv.go
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
package memkv
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type item struct {
|
||||
value interface{}
|
||||
expiresAt time.Time
|
||||
}
|
||||
|
||||
type InMemoryStore struct {
|
||||
data map[string]item
|
||||
defaultExpiration time.Duration
|
||||
mu sync.RWMutex
|
||||
stopJanitor chan struct{}
|
||||
}
|
||||
|
||||
var Store *InMemoryStore
|
||||
|
||||
func init() {
|
||||
Store = NewStore(10 * time.Minute)
|
||||
}
|
||||
|
||||
func NewStore(defaultExpiration time.Duration) *InMemoryStore {
|
||||
s := &InMemoryStore{
|
||||
data: make(map[string]item),
|
||||
defaultExpiration: defaultExpiration,
|
||||
stopJanitor: make(chan struct{}),
|
||||
}
|
||||
|
||||
go s.janitor(1 * time.Minute)
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *InMemoryStore) Set(key string, value interface{}, expiration ...time.Duration) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
exp := s.defaultExpiration
|
||||
if len(expiration) > 0 {
|
||||
exp = expiration[0]
|
||||
}
|
||||
|
||||
var expiresAt time.Time
|
||||
if exp > 0 {
|
||||
expiresAt = time.Now().Add(exp)
|
||||
}
|
||||
|
||||
s.data[key] = item{
|
||||
value: value,
|
||||
expiresAt: expiresAt,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *InMemoryStore) Get(key string) (interface{}, bool) {
|
||||
s.mu.RLock()
|
||||
it, found := s.data[key]
|
||||
s.mu.RUnlock()
|
||||
|
||||
if !found {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
if !it.expiresAt.IsZero() && time.Now().After(it.expiresAt) {
|
||||
s.Delete(key)
|
||||
return nil, false
|
||||
}
|
||||
|
||||
return it.value, true
|
||||
}
|
||||
|
||||
func (s *InMemoryStore) Delete(key string) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
delete(s.data, key)
|
||||
}
|
||||
|
||||
func (s *InMemoryStore) janitor(interval time.Duration) {
|
||||
ticker := time.NewTicker(interval)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
s.cleanup()
|
||||
case <-s.stopJanitor:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *InMemoryStore) cleanup() {
|
||||
now := time.Now()
|
||||
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
for k, it := range s.data {
|
||||
if !it.expiresAt.IsZero() && now.After(it.expiresAt) {
|
||||
delete(s.data, k)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *InMemoryStore) Close() {
|
||||
close(s.stopJanitor)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue