refactor(whatsapp): apply copilot suggestions
This commit is contained in:
parent
dc8f515011
commit
60c12072d0
2 changed files with 26 additions and 16 deletions
|
|
@ -28,6 +28,7 @@ type WhatsmeowChannel struct {
|
||||||
config config.WhatsmeowConfig
|
config config.WhatsmeowConfig
|
||||||
container *sqlstore.Container
|
container *sqlstore.Container
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
|
ctx context.Context
|
||||||
cancel context.CancelFunc
|
cancel context.CancelFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -74,17 +75,11 @@ func (c *WhatsmeowChannel) Start(ctx context.Context) error {
|
||||||
return fmt.Errorf("failed to connect whatsmeow: %w", err)
|
return fmt.Errorf("failed to connect whatsmeow: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(ctx)
|
c.ctx, c.cancel = context.WithCancel(ctx)
|
||||||
c.cancel = cancel
|
|
||||||
|
|
||||||
c.setRunning(true)
|
c.setRunning(true)
|
||||||
logger.InfoC("whatsmeow", "WhatsApp (whatsmeow) channel connected")
|
logger.InfoC("whatsmeow", "WhatsApp (whatsmeow) channel connected")
|
||||||
|
|
||||||
// Keep context alive for cleanup
|
|
||||||
go func() {
|
|
||||||
<-ctx.Done()
|
|
||||||
}()
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -175,6 +170,11 @@ func (c *WhatsmeowChannel) handleIncomingMessage(msg *events.Message) {
|
||||||
content = msg.Message.GetExtendedTextMessage().GetText()
|
content = msg.Message.GetExtendedTextMessage().GetText()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check allowlist before downloading media
|
||||||
|
if !c.IsAllowed(senderID) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Handle media
|
// Handle media
|
||||||
var mediaPaths []string
|
var mediaPaths []string
|
||||||
|
|
||||||
|
|
@ -248,7 +248,7 @@ func (c *WhatsmeowChannel) downloadMedia(msg whatsmeow.DownloadableMessage, ext
|
||||||
return "", fmt.Errorf("client not connected")
|
return "", fmt.Errorf("client not connected")
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := client.Download(context.Background(), msg)
|
data, err := client.Download(c.ctx, msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.ErrorCF("whatsmeow", "Failed to download media", map[string]interface{}{
|
logger.ErrorCF("whatsmeow", "Failed to download media", map[string]interface{}{
|
||||||
"error": err.Error(),
|
"error": err.Error(),
|
||||||
|
|
@ -257,14 +257,14 @@ func (c *WhatsmeowChannel) downloadMedia(msg whatsmeow.DownloadableMessage, ext
|
||||||
}
|
}
|
||||||
|
|
||||||
dir := filepath.Join(os.TempDir(), "picoclaw_media")
|
dir := filepath.Join(os.TempDir(), "picoclaw_media")
|
||||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
if err := os.MkdirAll(dir, 0700); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
filename := fmt.Sprintf("wa_%d%s", time.Now().UnixNano(), ext)
|
filename := fmt.Sprintf("wa_%d%s", time.Now().UnixNano(), ext)
|
||||||
path := filepath.Join(dir, filename)
|
path := filepath.Join(dir, filename)
|
||||||
|
|
||||||
if err := os.WriteFile(path, data, 0644); err != nil {
|
if err := os.WriteFile(path, data, 0600); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -276,9 +276,16 @@ func stringPtr(s string) *string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func expandHomePath(path string) string {
|
func expandHomePath(path string) string {
|
||||||
if strings.HasPrefix(path, "~/") {
|
if path == "~" {
|
||||||
home, _ := os.UserHomeDir()
|
if home, err := os.UserHomeDir(); err == nil {
|
||||||
return filepath.Join(home, path[2:])
|
return home
|
||||||
|
}
|
||||||
|
return path
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(path, "~/") {
|
||||||
|
if home, err := os.UserHomeDir(); err == nil {
|
||||||
|
return filepath.Join(home, path[2:])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return path
|
return path
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -110,11 +110,14 @@ func LinkWhatsmeow(dbPath string, mode string) error {
|
||||||
func WhatsmeowStatus(dbPath string) error {
|
func WhatsmeowStatus(dbPath string) error {
|
||||||
dbPath = expandHomePath(dbPath)
|
dbPath = expandHomePath(dbPath)
|
||||||
|
|
||||||
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
|
if _, err := os.Stat(dbPath); err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
fmt.Println("No WhatsApp database found.")
|
fmt.Println("No WhatsApp database found.")
|
||||||
fmt.Println("Run 'picoclaw whatsapp link' to link a device.")
|
fmt.Println("Run 'picoclaw whatsapp link' to link a device.")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
return fmt.Errorf("failed to stat db: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
container, err := sqlstore.New(ctx, "sqlite", fmt.Sprintf("file:%s?_pragma=foreign_keys(1)&_pragma=journal_mode(WAL)", dbPath), waLog.Noop)
|
container, err := sqlstore.New(ctx, "sqlite", fmt.Sprintf("file:%s?_pragma=foreign_keys(1)&_pragma=journal_mode(WAL)", dbPath), waLog.Noop)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue