feat_: Allow to set custom nodes & log level

This commit is contained in:
Andrea Maria Piana 2023-11-27 12:38:45 +00:00
parent f48f9cccc6
commit 3ab1afaae8
9 changed files with 106 additions and 26 deletions

View File

@ -214,11 +214,16 @@ func insertWakuV2Config(tx *sql.Tx, c *params.NodeConfig) error {
return err
}
return setWakuV2CustomNodes(tx, c.WakuV2Config.CustomNodes)
}
func setWakuV2CustomNodes(tx *sql.Tx, customNodes map[string]string) error {
if _, err := tx.Exec(`DELETE FROM wakuv2_custom_nodes WHERE synthetic_id = 'id'`); err != nil {
return err
}
for name, multiaddress := range c.WakuV2Config.CustomNodes {
for name, multiaddress := range customNodes {
// NOTE: synthetic id is redundant, name is effectively the primary key
_, err := tx.Exec(`INSERT OR REPLACE INTO wakuv2_custom_nodes (name, multiaddress, synthetic_id) VALUES (?, ?, 'id')`, name, multiaddress)
if err != nil {
return err
@ -791,3 +796,25 @@ func SetLightClient(db *sql.DB, enabled bool) error {
_, err := db.Exec(`UPDATE wakuv2_config SET light_client = ?`, enabled)
return err
}
func SetLogLevel(db *sql.DB, logLevel string) error {
_, err := db.Exec(`UPDATE log_config SET log_level = ?`, logLevel)
return err
}
func SetWakuV2CustomNodes(db *sql.DB, customNodes map[string]string) error {
tx, err := db.BeginTx(context.Background(), &sql.TxOptions{})
if err != nil {
return err
}
defer func() {
if err == nil {
err = tx.Commit()
return
}
// don't shadow original error
_ = tx.Rollback()
}()
return setWakuV2CustomNodes(tx, customNodes)
}

View File

@ -634,7 +634,7 @@ func (m *Messenger) clearHistory(id string) (*MessengerResponse, error) {
return response, nil
}
func (m *Messenger) SyncChat(request *requests.SyncChat) error {
func (m *Messenger) FetchMessages(request *requests.FetchMessages) error {
if err := request.Validate(); err != nil {
return err
@ -647,7 +647,7 @@ func (m *Messenger) SyncChat(request *requests.SyncChat) error {
return ErrChatNotFound
}
_, err := m.SyncChatOneMonth(chat.ID)
_, err := m.fetchMessages(chat.ID, oneMonthInSeconds)
if err != nil {
return err
}

View File

@ -946,9 +946,9 @@ func (m *Messenger) ConnectionChanged(state connection.State) {
m.connectionState = state
}
func (m *Messenger) SyncChatOneMonth(chatID string) (uint32, error) {
func (m *Messenger) fetchMessages(chatID string, duration uint32) (uint32, error) {
to := uint32(m.getTimesource().GetCurrentTime() / 1000)
from := to - oneMonthInSeconds
from := to - duration
_, err := m.performMailserverRequest(func() (*MessengerResponse, error) {
pubsubTopic, topics, err := m.topicsForChat(chatID)
if err != nil {

View File

@ -8,3 +8,15 @@ import (
func (m *Messenger) SetLightClient(request *requests.SetLightClient) error {
return nodecfg.SetLightClient(m.database, request.Enabled)
}
func (m *Messenger) SetLogLevel(request *requests.SetLogLevel) error {
if err := request.Validate(); err != nil {
return err
}
return nodecfg.SetLogLevel(m.database, request.LogLevel)
}
func (m *Messenger) SetCustomNodes(request *requests.SetCustomNodes) error {
return nodecfg.SetWakuV2CustomNodes(m.database, request.CustomNodes)
}

View File

@ -0,0 +1,19 @@
package requests
import (
"errors"
)
var ErrFetchMessagesInvalidID = errors.New("fetch-messages: invalid id")
type FetchMessages struct {
ID string `json:"id"`
}
func (c *FetchMessages) Validate() error {
if len(c.ID) == 0 {
return ErrFetchMessagesInvalidID
}
return nil
}

View File

@ -0,0 +1,5 @@
package requests
type SetCustomNodes struct {
CustomNodes map[string]string `json:"customNodes"`
}

View File

@ -0,0 +1,28 @@
package requests
import (
"errors"
)
const (
ErrorLogLevel = "ERROR"
WarnLogLevel = "WARN"
InfoLogLevel = "INFO"
DebugLogLevel = "DEBUG"
TraceLogLevel = "TRACE"
)
var ErrSetLogLevelInvalidLogLevel = errors.New("set-log-level: invalid log level")
type SetLogLevel struct {
LogLevel string `json:"logLevel"`
}
func (c *SetLogLevel) Validate() error {
switch c.LogLevel {
case ErrorLogLevel, WarnLogLevel, InfoLogLevel, DebugLogLevel, TraceLogLevel:
return nil
}
return ErrSetLogLevelInvalidLogLevel
}

View File

@ -1,19 +0,0 @@
package requests
import (
"errors"
)
var ErrSyncChatInvalidID = errors.New("sync-chat: invalid id")
type SyncChat struct {
ID string `json:"id"`
}
func (c *SyncChat) Validate() error {
if len(c.ID) == 0 {
return ErrSyncChatInvalidID
}
return nil
}

View File

@ -1681,14 +1681,22 @@ func (api *PublicAPI) PromoteSelfToControlMode(communityID string) error {
return err
}
func (api *PublicAPI) SyncChat(request *requests.SyncChat) error {
return api.service.messenger.SyncChat(request)
func (api *PublicAPI) FetchMessages(request *requests.FetchMessages) error {
return api.service.messenger.FetchMessages(request)
}
func (api *PublicAPI) SetLightClient(request *requests.SetLightClient) error {
return api.service.messenger.SetLightClient(request)
}
func (api *PublicAPI) SetLogLevel(request *requests.SetLogLevel) error {
return api.service.messenger.SetLogLevel(request)
}
func (api *PublicAPI) SetCustomNodes(request *requests.SetCustomNodes) error {
return api.service.messenger.SetCustomNodes(request)
}
// -----
// HELPER
// -----