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
|
||||
container *sqlstore.Container
|
||||
mu sync.Mutex
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
|
|
@ -74,17 +75,11 @@ func (c *WhatsmeowChannel) Start(ctx context.Context) error {
|
|||
return fmt.Errorf("failed to connect whatsmeow: %w", err)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
c.cancel = cancel
|
||||
c.ctx, c.cancel = context.WithCancel(ctx)
|
||||
|
||||
c.setRunning(true)
|
||||
logger.InfoC("whatsmeow", "WhatsApp (whatsmeow) channel connected")
|
||||
|
||||
// Keep context alive for cleanup
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -175,6 +170,11 @@ func (c *WhatsmeowChannel) handleIncomingMessage(msg *events.Message) {
|
|||
content = msg.Message.GetExtendedTextMessage().GetText()
|
||||
}
|
||||
|
||||
// Check allowlist before downloading media
|
||||
if !c.IsAllowed(senderID) {
|
||||
return
|
||||
}
|
||||
|
||||
// Handle media
|
||||
var mediaPaths []string
|
||||
|
||||
|
|
@ -248,7 +248,7 @@ func (c *WhatsmeowChannel) downloadMedia(msg whatsmeow.DownloadableMessage, ext
|
|||
return "", fmt.Errorf("client not connected")
|
||||
}
|
||||
|
||||
data, err := client.Download(context.Background(), msg)
|
||||
data, err := client.Download(c.ctx, msg)
|
||||
if err != nil {
|
||||
logger.ErrorCF("whatsmeow", "Failed to download media", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
|
|
@ -257,14 +257,14 @@ func (c *WhatsmeowChannel) downloadMedia(msg whatsmeow.DownloadableMessage, ext
|
|||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
filename := fmt.Sprintf("wa_%d%s", time.Now().UnixNano(), ext)
|
||||
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
|
||||
}
|
||||
|
||||
|
|
@ -276,9 +276,16 @@ func stringPtr(s string) *string {
|
|||
}
|
||||
|
||||
func expandHomePath(path string) string {
|
||||
if path == "~" {
|
||||
if home, err := os.UserHomeDir(); err == nil {
|
||||
return home
|
||||
}
|
||||
return path
|
||||
}
|
||||
if strings.HasPrefix(path, "~/") {
|
||||
home, _ := os.UserHomeDir()
|
||||
return filepath.Join(home, path[2:])
|
||||
if home, err := os.UserHomeDir(); err == nil {
|
||||
return filepath.Join(home, path[2:])
|
||||
}
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
|
|
|||
|
|
@ -110,10 +110,13 @@ func LinkWhatsmeow(dbPath string, mode string) error {
|
|||
func WhatsmeowStatus(dbPath string) error {
|
||||
dbPath = expandHomePath(dbPath)
|
||||
|
||||
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
|
||||
fmt.Println("No WhatsApp database found.")
|
||||
fmt.Println("Run 'picoclaw whatsapp link' to link a device.")
|
||||
return nil
|
||||
if _, err := os.Stat(dbPath); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
fmt.Println("No WhatsApp database found.")
|
||||
fmt.Println("Run 'picoclaw whatsapp link' to link a device.")
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("failed to stat db: %w", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue