fix actions issues

This commit is contained in:
Karmanyaah Malhotra 2021-12-30 21:21:35 -06:00
parent 10e0b23a51
commit b3ce5e3794
2 changed files with 13 additions and 14 deletions

View file

@ -16,11 +16,11 @@ import (
"unifiedpush.org/go/np2p_dbus/utils" "unifiedpush.org/go/np2p_dbus/utils"
) )
type Store struct { type store struct {
*storage.Storage *storage.Storage
} }
func (s Store) GetAllPubTokens() string { func (s store) GetAllPubTokens() string {
var conns []storage.Connection var conns []storage.Connection
result := s.DB().Find(&conns) result := s.DB().Find(&conns)
if result.Error != nil { if result.Error != nil {
@ -34,26 +34,26 @@ func (s Store) GetAllPubTokens() string {
return strings.Join(pubtokens, ",") return strings.Join(pubtokens, ",")
} }
type KVStore struct { type kvStore struct {
Key string `gorm:"primaryKey"` Key string `gorm:"primaryKey"`
Value string Value string
} }
func (kv *KVStore) Get(db *gorm.DB) error { func (kv *kvStore) get(db *gorm.DB) error {
return db.First(kv).Error return db.First(kv).Error
} }
func (kv KVStore) Set(db *gorm.DB) error { func (kv kvStore) set(db *gorm.DB) error {
return db.Clauses(clause.OnConflict{UpdateAll: true}).Create(&kv).Error return db.Clauses(clause.OnConflict{UpdateAll: true}).Create(&kv).Error
} }
func (s Store) SetLastMessage(id int64) error { func (s store) SetLastMessage(id int64) error {
return KVStore{"device-id", fmt.Sprintf("%d", id)}.Set(s.DB()) return kvStore{"device-id", fmt.Sprintf("%d", id)}.set(s.DB())
} }
func (s Store) GetLastMessage() int64 { func (s store) GetLastMessage() int64 {
answer := KVStore{Key: "device-id"} answer := kvStore{Key: "device-id"}
if err := answer.Get(s.DB()); err != nil { if err := answer.get(s.DB()); err != nil {
//log or fatal?? //log or fatal??
return 100 return 100
} }
@ -64,13 +64,13 @@ func (s Store) GetLastMessage() int64 {
// creates a new distributor object with an initialized storage and dbus // creates a new distributor object with an initialized storage and dbus
func newDistributor(conf *client.Config) (d *distributor) { func newDistributor(conf *client.Config) (d *distributor) {
st, err := storage.InitStorage(utils.StoragePath("ntfy.db")) st, err := storage.InitStorage(utils.StoragePath("ntfy.db"))
st.DB().AutoMigrate(KVStore{}) //todo move to proper function st.DB().AutoMigrate(kvStore{}) //todo move to proper function
if err != nil { if err != nil {
log.Fatalln("failed to connect database") log.Fatalln("failed to connect database")
} }
dbus := distributor_tools.NewDBus("org.unifiedpush.Distributor.ntfy") dbus := distributor_tools.NewDBus("org.unifiedpush.Distributor.ntfy")
d = &distributor{dbus, Store{st}, conf, make(chan struct{})} d = &distributor{dbus, store{st}, conf, make(chan struct{})}
err = dbus.StartHandling(d) err = dbus.StartHandling(d)
fmt.Println("DBUS HANDLING") fmt.Println("DBUS HANDLING")
if err != nil { if err != nil {
@ -82,7 +82,7 @@ func newDistributor(conf *client.Config) (d *distributor) {
type distributor struct { type distributor struct {
dbus *distributor_tools.DBus dbus *distributor_tools.DBus
st Store st store
conf *client.Config conf *client.Config
resub chan struct{} resub chan struct{}
} }

View file

@ -201,7 +201,6 @@ func doSubscribe(c *cli.Context, cl *client.Client, conf *client.Config, d *dist
func printMessageOrRunCommand(c *cli.Context, m *client.Message, d *distributor, command string) { func printMessageOrRunCommand(c *cli.Context, m *client.Message, d *distributor, command string) {
if command == "unifiedpush" && d != nil { if command == "unifiedpush" && d != nil {
// this shouldn't ever be run if d is nil since there won't be a "unifiedpush" subscription
if conn := d.st.GetConnectionbyPublic(m.Topic); conn != nil { if conn := d.st.GetConnectionbyPublic(m.Topic); conn != nil {
fmt.Println("NEWMSG") fmt.Println("NEWMSG")
_ = d.dbus.NewConnector(conn.AppID).Message(conn.AppToken, m.Message, "") _ = d.dbus.NewConnector(conn.AppID).Message(conn.AppToken, m.Message, "")