fix: remove duplicate code in todoistWebhookHandler
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 6s
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 6s
This commit is contained in:
@@ -5347,82 +5347,6 @@ func (a *App) todoistWebhookHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
log.Printf("Todoist webhook: todoist_user_id=%d -> user_id=%d", todoistUserID, userID)
|
log.Printf("Todoist webhook: todoist_user_id=%d -> user_id=%d", todoistUserID, userID)
|
||||||
|
|
||||||
// Читаем тело запроса для логирования
|
|
||||||
bodyBytes, err := io.ReadAll(r.Body)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Error reading request body: %v", err)
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
|
||||||
"ok": false,
|
|
||||||
"error": "Error reading request body",
|
|
||||||
"message": "Failed to read request",
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Логируем сырое тело запроса
|
|
||||||
log.Printf("Request body (raw): %s", string(bodyBytes))
|
|
||||||
log.Printf("Request body length: %d bytes", len(bodyBytes))
|
|
||||||
|
|
||||||
// Опциональная проверка секрета webhook (если задан в переменных окружения)
|
|
||||||
todoistWebhookSecret := getEnv("TODOIST_WEBHOOK_SECRET", "")
|
|
||||||
log.Printf("Webhook secret check: configured=%v", todoistWebhookSecret != "")
|
|
||||||
if todoistWebhookSecret != "" {
|
|
||||||
providedSecret := r.Header.Get("X-Todoist-Webhook-Secret")
|
|
||||||
log.Printf("Provided secret in header: %v (length: %d)", providedSecret != "", len(providedSecret))
|
|
||||||
if providedSecret != todoistWebhookSecret {
|
|
||||||
log.Printf("Invalid Todoist webhook secret provided (expected length: %d, provided length: %d)", len(todoistWebhookSecret), len(providedSecret))
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
|
||||||
"ok": false,
|
|
||||||
"error": "Unauthorized",
|
|
||||||
"message": "Invalid webhook secret",
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
log.Printf("Webhook secret validated successfully")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Парсим webhook от Todoist из уже прочитанных байтов
|
|
||||||
var webhook TodoistWebhook
|
|
||||||
if err := json.Unmarshal(bodyBytes, &webhook); err != nil {
|
|
||||||
log.Printf("Error decoding Todoist webhook: %v", err)
|
|
||||||
log.Printf("Failed to parse body as JSON: %s", string(bodyBytes))
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
|
||||||
"ok": false,
|
|
||||||
"error": "Invalid request body",
|
|
||||||
"message": "Failed to parse JSON",
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Логируем структуру webhook после парсинга
|
|
||||||
log.Printf("Parsed webhook structure:")
|
|
||||||
log.Printf(" EventName: %s", webhook.EventName)
|
|
||||||
log.Printf(" EventData keys: %v", getMapKeys(webhook.EventData))
|
|
||||||
if eventDataJSON, err := json.MarshalIndent(webhook.EventData, " ", " "); err == nil {
|
|
||||||
log.Printf(" EventData content:\n%s", string(eventDataJSON))
|
|
||||||
} else {
|
|
||||||
log.Printf(" EventData (marshal error): %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Проверяем, что это событие закрытия задачи
|
|
||||||
if webhook.EventName != "item:completed" {
|
|
||||||
log.Printf("Received Todoist event '%s', ignoring (only processing 'item:completed')", webhook.EventName)
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
|
||||||
"ok": true,
|
|
||||||
"message": "Event ignored",
|
|
||||||
"event": webhook.EventName,
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Извлекаем content (title) и description из event_data
|
// Извлекаем content (title) и description из event_data
|
||||||
log.Printf("Extracting content and description from event_data...")
|
log.Printf("Extracting content and description from event_data...")
|
||||||
var title, description string
|
var title, description string
|
||||||
|
|||||||
Reference in New Issue
Block a user