|
|
@ -8,7 +8,6 @@ import (
|
|
|
|
"path/filepath"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/hrfee/mediabrowser"
|
|
|
|
"github.com/hrfee/mediabrowser"
|
|
|
@ -21,6 +20,11 @@ type telegramStore map[string]TelegramUser
|
|
|
|
type matrixStore map[string]MatrixUser
|
|
|
|
type matrixStore map[string]MatrixUser
|
|
|
|
type emailStore map[string]EmailAddress
|
|
|
|
type emailStore map[string]EmailAddress
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type UserExpiry struct {
|
|
|
|
|
|
|
|
JellyfinID string `badgerhold:"key"`
|
|
|
|
|
|
|
|
Expiry time.Time
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type Storage struct {
|
|
|
|
type Storage struct {
|
|
|
|
timePattern string
|
|
|
|
timePattern string
|
|
|
|
|
|
|
|
|
|
|
@ -28,22 +32,21 @@ type Storage struct {
|
|
|
|
db *badgerhold.Store
|
|
|
|
db *badgerhold.Store
|
|
|
|
|
|
|
|
|
|
|
|
invite_path, emails_path, policy_path, configuration_path, displayprefs_path, ombi_path, profiles_path, customEmails_path, users_path, telegram_path, discord_path, matrix_path, announcements_path, matrix_sql_path, userPage_path string
|
|
|
|
invite_path, emails_path, policy_path, configuration_path, displayprefs_path, ombi_path, profiles_path, customEmails_path, users_path, telegram_path, discord_path, matrix_path, announcements_path, matrix_sql_path, userPage_path string
|
|
|
|
users map[string]time.Time // Map of Jellyfin User IDs to their expiry times.
|
|
|
|
deprecatedUserExpiries map[string]time.Time // Map of Jellyfin User IDs to their expiry times.
|
|
|
|
invites Invites
|
|
|
|
deprecatedInvites Invites
|
|
|
|
profiles map[string]Profile
|
|
|
|
profiles map[string]Profile
|
|
|
|
defaultProfile string
|
|
|
|
defaultProfile string
|
|
|
|
displayprefs, ombi_template map[string]interface{}
|
|
|
|
displayprefs, ombi_template map[string]interface{}
|
|
|
|
emails emailStore // Map of Jellyfin User IDs to Email addresses.
|
|
|
|
deprecatedEmails emailStore // Map of Jellyfin User IDs to Email addresses.
|
|
|
|
telegram telegramStore // Map of Jellyfin User IDs to telegram users.
|
|
|
|
deprecatedTelegram telegramStore // Map of Jellyfin User IDs to telegram users.
|
|
|
|
discord discordStore // Map of Jellyfin user IDs to discord users.
|
|
|
|
deprecatedDiscord discordStore // Map of Jellyfin user IDs to discord users.
|
|
|
|
matrix matrixStore // Map of Jellyfin user IDs to Matrix users.
|
|
|
|
deprecatedMatrix matrixStore // Map of Jellyfin user IDs to Matrix users.
|
|
|
|
customEmails customEmails
|
|
|
|
customEmails customEmails
|
|
|
|
userPage userPageContent
|
|
|
|
userPage userPageContent
|
|
|
|
policy mediabrowser.Policy
|
|
|
|
policy mediabrowser.Policy
|
|
|
|
configuration mediabrowser.Configuration
|
|
|
|
configuration mediabrowser.Configuration
|
|
|
|
lang Lang
|
|
|
|
lang Lang
|
|
|
|
announcements map[string]announcementTemplate
|
|
|
|
deprecatedAnnouncements map[string]announcementTemplate
|
|
|
|
invitesLock, usersLock, discordLock, telegramLock, matrixLock, emailsLock sync.Mutex
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (app *appContext) ConnectDB() {
|
|
|
|
func (app *appContext) ConnectDB() {
|
|
|
@ -203,36 +206,39 @@ func (st *Storage) DeleteMatrixKey(k string) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetInvites returns a copy of the store.
|
|
|
|
// GetInvites returns a copy of the store.
|
|
|
|
func (st *Storage) GetInvites() Invites {
|
|
|
|
func (st *Storage) GetInvites() []Invite {
|
|
|
|
if st.invites == nil {
|
|
|
|
result := []Invite{}
|
|
|
|
st.invites = Invites{}
|
|
|
|
err := st.db.Find(&result, &badgerhold.Query{})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
// fmt.Printf("Failed to find invites: %v\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return st.invites
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetInvitesKey returns the value stored in the store's key.
|
|
|
|
// GetInvitesKey returns the value stored in the store's key.
|
|
|
|
func (st *Storage) GetInvitesKey(k string) (Invite, bool) {
|
|
|
|
func (st *Storage) GetInvitesKey(k string) (Invite, bool) {
|
|
|
|
v, ok := st.invites[k]
|
|
|
|
result := Invite{}
|
|
|
|
return v, ok
|
|
|
|
err := st.db.Get(k, &result)
|
|
|
|
|
|
|
|
ok := true
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
// fmt.Printf("Failed to find invite: %v\n", err)
|
|
|
|
|
|
|
|
ok = false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, ok
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// SetInvitesKey stores value v in key k.
|
|
|
|
// SetInvitesKey stores value v in key k.
|
|
|
|
func (st *Storage) SetInvitesKey(k string, v Invite) {
|
|
|
|
func (st *Storage) SetInvitesKey(k string, v Invite) {
|
|
|
|
st.invitesLock.Lock()
|
|
|
|
v.Code = k
|
|
|
|
if st.invites == nil {
|
|
|
|
err := st.db.Upsert(k, v)
|
|
|
|
st.invites = Invites{}
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
// fmt.Printf("Failed to set invite: %v\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
st.invites[k] = v
|
|
|
|
|
|
|
|
st.storeInvites()
|
|
|
|
|
|
|
|
st.invitesLock.Unlock()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// DeleteInvitesKey deletes value at key k.
|
|
|
|
// DeleteInvitesKey deletes value at key k.
|
|
|
|
func (st *Storage) DeleteInvitesKey(k string) {
|
|
|
|
func (st *Storage) DeleteInvitesKey(k string) {
|
|
|
|
st.invitesLock.Lock()
|
|
|
|
st.db.Delete(k, Invite{})
|
|
|
|
delete(st.invites, k)
|
|
|
|
|
|
|
|
st.storeInvites()
|
|
|
|
|
|
|
|
st.invitesLock.Unlock()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetAnnouncements returns a copy of the store.
|
|
|
|
// GetAnnouncements returns a copy of the store.
|
|
|
@ -270,6 +276,42 @@ func (st *Storage) DeleteAnnouncementsKey(k string) {
|
|
|
|
st.db.Delete(k, announcementTemplate{})
|
|
|
|
st.db.Delete(k, announcementTemplate{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GetUserExpiries returns a copy of the store.
|
|
|
|
|
|
|
|
func (st *Storage) GetUserExpiries() []UserExpiry {
|
|
|
|
|
|
|
|
result := []UserExpiry{}
|
|
|
|
|
|
|
|
err := st.db.Find(&result, &badgerhold.Query{})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
// fmt.Printf("Failed to find expiries: %v\n", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GetUserExpiryKey returns the value stored in the store's key.
|
|
|
|
|
|
|
|
func (st *Storage) GetUserExpiryKey(k string) (UserExpiry, bool) {
|
|
|
|
|
|
|
|
result := UserExpiry{}
|
|
|
|
|
|
|
|
err := st.db.Get(k, &result)
|
|
|
|
|
|
|
|
ok := true
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
// fmt.Printf("Failed to find expiry: %v\n", err)
|
|
|
|
|
|
|
|
ok = false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, ok
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// SetUserExpiryKey stores value v in key k.
|
|
|
|
|
|
|
|
func (st *Storage) SetUserExpiryKey(k string, v UserExpiry) {
|
|
|
|
|
|
|
|
v.JellyfinID = k
|
|
|
|
|
|
|
|
err := st.db.Upsert(k, v)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
// fmt.Printf("Failed to set expiry: %v\n", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// DeleteUserExpiryKey deletes value at key k.
|
|
|
|
|
|
|
|
func (st *Storage) DeleteUserExpiryKey(k string) {
|
|
|
|
|
|
|
|
st.db.Delete(k, UserExpiry{})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type TelegramUser struct {
|
|
|
|
type TelegramUser struct {
|
|
|
|
JellyfinID string `badgerhold:"key"`
|
|
|
|
JellyfinID string `badgerhold:"key"`
|
|
|
|
ChatID int64 `badgerhold:"index"`
|
|
|
|
ChatID int64 `badgerhold:"index"`
|
|
|
@ -335,6 +377,7 @@ type Profile struct {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type Invite struct {
|
|
|
|
type Invite struct {
|
|
|
|
|
|
|
|
Code string `badgerhold:"key"`
|
|
|
|
Created time.Time `json:"created"`
|
|
|
|
Created time.Time `json:"created"`
|
|
|
|
NoLimit bool `json:"no-limit"`
|
|
|
|
NoLimit bool `json:"no-limit"`
|
|
|
|
RemainingUses int `json:"remaining-uses"`
|
|
|
|
RemainingUses int `json:"remaining-uses"`
|
|
|
@ -1036,18 +1079,16 @@ func (st *Storage) loadLangTelegram(filesystems ...fs.FS) error {
|
|
|
|
type Invites map[string]Invite
|
|
|
|
type Invites map[string]Invite
|
|
|
|
|
|
|
|
|
|
|
|
func (st *Storage) loadInvites() error {
|
|
|
|
func (st *Storage) loadInvites() error {
|
|
|
|
return loadJSON(st.invite_path, &st.invites)
|
|
|
|
return loadJSON(st.invite_path, &st.deprecatedInvites)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (st *Storage) storeInvites() error {
|
|
|
|
func (st *Storage) storeInvites() error {
|
|
|
|
return storeJSON(st.invite_path, st.invites)
|
|
|
|
return storeJSON(st.invite_path, st.deprecatedInvites)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (st *Storage) loadUsers() error {
|
|
|
|
func (st *Storage) loadUserExpiries() error {
|
|
|
|
st.usersLock.Lock()
|
|
|
|
if st.deprecatedUserExpiries == nil {
|
|
|
|
defer st.usersLock.Unlock()
|
|
|
|
st.deprecatedUserExpiries = map[string]time.Time{}
|
|
|
|
if st.users == nil {
|
|
|
|
|
|
|
|
st.users = map[string]time.Time{}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
temp := map[string]time.Time{}
|
|
|
|
temp := map[string]time.Time{}
|
|
|
|
err := loadJSON(st.users_path, &temp)
|
|
|
|
err := loadJSON(st.users_path, &temp)
|
|
|
@ -1055,47 +1096,47 @@ func (st *Storage) loadUsers() error {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for id, t1 := range temp {
|
|
|
|
for id, t1 := range temp {
|
|
|
|
if _, ok := st.users[id]; !ok {
|
|
|
|
if _, ok := st.deprecatedUserExpiries[id]; !ok {
|
|
|
|
st.users[id] = t1
|
|
|
|
st.deprecatedUserExpiries[id] = t1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (st *Storage) storeUsers() error {
|
|
|
|
func (st *Storage) storeUserExpiries() error {
|
|
|
|
return storeJSON(st.users_path, st.users)
|
|
|
|
return storeJSON(st.users_path, st.deprecatedUserExpiries)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (st *Storage) loadEmails() error {
|
|
|
|
func (st *Storage) loadEmails() error {
|
|
|
|
return loadJSON(st.emails_path, &st.emails)
|
|
|
|
return loadJSON(st.emails_path, &st.deprecatedEmails)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (st *Storage) storeEmails() error {
|
|
|
|
func (st *Storage) storeEmails() error {
|
|
|
|
return storeJSON(st.emails_path, st.emails)
|
|
|
|
return storeJSON(st.emails_path, st.deprecatedEmails)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (st *Storage) loadTelegramUsers() error {
|
|
|
|
func (st *Storage) loadTelegramUsers() error {
|
|
|
|
return loadJSON(st.telegram_path, &st.telegram)
|
|
|
|
return loadJSON(st.telegram_path, &st.deprecatedTelegram)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (st *Storage) storeTelegramUsers() error {
|
|
|
|
func (st *Storage) storeTelegramUsers() error {
|
|
|
|
return storeJSON(st.telegram_path, st.telegram)
|
|
|
|
return storeJSON(st.telegram_path, st.deprecatedTelegram)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (st *Storage) loadDiscordUsers() error {
|
|
|
|
func (st *Storage) loadDiscordUsers() error {
|
|
|
|
return loadJSON(st.discord_path, &st.discord)
|
|
|
|
return loadJSON(st.discord_path, &st.deprecatedDiscord)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (st *Storage) storeDiscordUsers() error {
|
|
|
|
func (st *Storage) storeDiscordUsers() error {
|
|
|
|
return storeJSON(st.discord_path, st.discord)
|
|
|
|
return storeJSON(st.discord_path, st.deprecatedDiscord)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (st *Storage) loadMatrixUsers() error {
|
|
|
|
func (st *Storage) loadMatrixUsers() error {
|
|
|
|
return loadJSON(st.matrix_path, &st.matrix)
|
|
|
|
return loadJSON(st.matrix_path, &st.deprecatedMatrix)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (st *Storage) storeMatrixUsers() error {
|
|
|
|
func (st *Storage) storeMatrixUsers() error {
|
|
|
|
return storeJSON(st.matrix_path, st.matrix)
|
|
|
|
return storeJSON(st.matrix_path, st.deprecatedMatrix)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (st *Storage) loadCustomEmails() error {
|
|
|
|
func (st *Storage) loadCustomEmails() error {
|
|
|
@ -1147,11 +1188,11 @@ func (st *Storage) storeOmbiTemplate() error {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (st *Storage) loadAnnouncements() error {
|
|
|
|
func (st *Storage) loadAnnouncements() error {
|
|
|
|
return loadJSON(st.announcements_path, &st.announcements)
|
|
|
|
return loadJSON(st.announcements_path, &st.deprecatedAnnouncements)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (st *Storage) storeAnnouncements() error {
|
|
|
|
func (st *Storage) storeAnnouncements() error {
|
|
|
|
return storeJSON(st.announcements_path, st.announcements)
|
|
|
|
return storeJSON(st.announcements_path, st.deprecatedAnnouncements)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (st *Storage) loadProfiles() error {
|
|
|
|
func (st *Storage) loadProfiles() error {
|
|
|
|