|
11 | 11 | use ThemeIsle\HyveLite\BaseAPI;
|
12 | 12 | use ThemeIsle\HyveLite\Cosine_Similarity;
|
13 | 13 | use ThemeIsle\HyveLite\Qdrant_API;
|
14 |
| -use ThemeIsle\HyveLite\Tokenizer; |
15 | 14 | use ThemeIsle\HyveLite\OpenAI;
|
16 | 15 |
|
17 | 16 | /**
|
@@ -463,55 +462,28 @@ public function get_data( $request ) {
|
463 | 462 | * @throws \Exception If Qdrant API fails.
|
464 | 463 | */
|
465 | 464 | public function add_data( $request ) {
|
466 |
| - $data = $request->get_param( 'data' ); |
467 |
| - $post_id = $data['ID']; |
468 |
| - $data = Tokenizer::tokenize( $data ); |
469 |
| - $chunks = array_column( $data, 'post_content' ); |
470 |
| - $moderation = OpenAI::instance()->moderate_chunks( $chunks, $post_id ); |
471 |
| - |
472 |
| - if ( is_wp_error( $moderation ) ) { |
473 |
| - return rest_ensure_response( [ 'error' => $this->get_error_message( $moderation ) ] ); |
474 |
| - } |
475 |
| - |
476 |
| - if ( true !== $moderation && 'override' !== $request->get_param( 'action' ) ) { |
477 |
| - update_post_meta( $post_id, '_hyve_moderation_failed', 1 ); |
478 |
| - update_post_meta( $post_id, '_hyve_moderation_review', $moderation ); |
| 465 | + $data = $request->get_param( 'data' ); |
| 466 | + $post_id = $data['ID']; |
| 467 | + $action = $request->get_param( 'action' ); |
| 468 | + $process = $this->table->add_post( $post_id, $action ); |
479 | 469 |
|
480 |
| - return rest_ensure_response( |
481 |
| - [ |
482 |
| - 'error' => __( 'The content failed moderation policies.', 'hyve-lite' ), |
483 |
| - 'code' => 'content_failed_moderation', |
484 |
| - 'review' => $moderation, |
485 |
| - ] |
486 |
| - ); |
487 |
| - } |
| 470 | + if ( is_wp_error( $process ) ) { |
| 471 | + if ( 'content_failed_moderation' === $process->get_error_code() ) { |
| 472 | + $data = $process->get_error_data(); |
| 473 | + $review = isset( $data['review'] ) ? $data['review'] : []; |
488 | 474 |
|
489 |
| - if ( 'update' === $request->get_param( 'action' ) ) { |
490 |
| - if ( Qdrant_API::is_active() ) { |
491 |
| - try { |
492 |
| - $delete_result = Qdrant_API::instance()->delete_point( $post_id ); |
493 |
| - |
494 |
| - if ( ! $delete_result ) { |
495 |
| - throw new \Exception( __( 'Failed to delete point in Qdrant.', 'hyve-lite' ) ); |
496 |
| - } |
497 |
| - } catch ( \Exception $e ) { |
498 |
| - return rest_ensure_response( [ 'error' => $e->getMessage() ] ); |
499 |
| - } |
| 475 | + return rest_ensure_response( |
| 476 | + [ |
| 477 | + 'error' => $process->get_error_message(), |
| 478 | + 'code' => $process->get_error_code(), |
| 479 | + 'review' => $review, |
| 480 | + ] |
| 481 | + ); |
500 | 482 | }
|
501 | 483 |
|
502 |
| - $this->table->delete_by_post_id( $post_id ); |
503 |
| - delete_post_meta( $post_id, '_hyve_needs_update' ); |
| 484 | + return rest_ensure_response( [ 'error' => $this->get_error_message( $process ) ] ); |
504 | 485 | }
|
505 | 486 |
|
506 |
| - foreach ( $data as $datum ) { |
507 |
| - $id = $this->table->insert( $datum ); |
508 |
| - $this->table->process_post( $id ); |
509 |
| - } |
510 |
| - |
511 |
| - update_post_meta( $post_id, '_hyve_added', 1 ); |
512 |
| - delete_post_meta( $post_id, '_hyve_moderation_failed' ); |
513 |
| - delete_post_meta( $post_id, '_hyve_moderation_review' ); |
514 |
| - |
515 | 487 | return rest_ensure_response( true );
|
516 | 488 | }
|
517 | 489 |
|
@@ -574,7 +546,7 @@ public function get_threads( $request ) {
|
574 | 546 | $post_data = [
|
575 | 547 | 'ID' => $post_id,
|
576 | 548 | 'title' => get_the_title( $post_id ),
|
577 |
| - 'date' => get_the_date( 'd/m/Y g:i A', $post_id ), |
| 549 | + 'date' => get_the_date( 'c', $post_id ), |
578 | 550 | 'thread' => get_post_meta( $post_id, '_hyve_thread_data', true ),
|
579 | 551 | 'thread_id' => get_post_meta( $post_id, '_hyve_thread_id', true ),
|
580 | 552 | ];
|
|
0 commit comments