@@ -363,6 +363,57 @@ public static function insertUser(User $user, $date, Chat $chat = null)
363
363
}
364
364
}
365
365
366
+ /**
367
+ * Insert chat
368
+ *
369
+ * @todo Needs to return something if successful
370
+ *
371
+ * @param Entities\Chat $chat
372
+ * @param string $date
373
+ * @param int $migrate_to_chat_id
374
+ */
375
+ public static function insertChat (Chat $ chat , $ date , $ migrate_to_chat_id = null )
376
+ {
377
+ if (!self ::isDbConnected ()) {
378
+ return false ;
379
+ }
380
+
381
+ $ chat_id = $ chat ->getId ();
382
+ $ chat_title = $ chat ->getTitle ();
383
+ $ type = $ chat ->getType ();
384
+
385
+ try {
386
+ //chat table
387
+ $ sth2 = self ::$ pdo ->prepare ('INSERT INTO ` ' . TB_CHAT . '`
388
+ (
389
+ `id`, `type`, `title`, `created_at` ,`updated_at`, `old_id`
390
+ )
391
+ VALUES (
392
+ :id, :type, :title, :date, :date, :oldid
393
+ )
394
+ ON DUPLICATE KEY UPDATE `type`=:type, `title`=:title, `updated_at`=:date
395
+ ' );
396
+
397
+ if ($ migrate_to_chat_id ) {
398
+ $ type = 'supergroup ' ;
399
+
400
+ $ sth2 ->bindParam (':id ' , $ migrate_to_chat_id , \PDO ::PARAM_INT );
401
+ $ sth2 ->bindParam (':oldid ' , $ chat_id , \PDO ::PARAM_INT );
402
+ } else {
403
+ $ sth2 ->bindParam (':id ' , $ chat_id , \PDO ::PARAM_INT );
404
+ $ sth2 ->bindParam (':oldid ' , $ migrate_to_chat_id , \PDO ::PARAM_INT );
405
+ }
406
+
407
+ $ sth2 ->bindParam (':type ' , $ type , \PDO ::PARAM_INT );
408
+ $ sth2 ->bindParam (':title ' , $ chat_title , \PDO ::PARAM_STR , 255 );
409
+ $ sth2 ->bindParam (':date ' , $ date , \PDO ::PARAM_STR );
410
+
411
+ $ status = $ sth2 ->execute ();
412
+ } catch (PDOException $ e ) {
413
+ throw new TelegramException ($ e ->getMessage ());
414
+ }
415
+ }
416
+
366
417
/**
367
418
* Insert request into database
368
419
*
@@ -561,64 +612,35 @@ public static function insertMessageRequest(Message &$message)
561
612
562
613
$ date = self ::getTimestamp ($ message ->getDate ());
563
614
$ forward_from = $ message ->getForwardFrom ();
564
- if ($ forward_from ) {
565
- $ forward_date = self ::getTimestamp ($ message ->getForwardDate ());
566
- }
567
-
615
+ $ forward_from_chat = $ message ->getForwardFromChat ();
568
616
$ photo = $ message ->getPhoto ();
569
617
$ entities = $ message ->getEntities ();
570
618
$ new_chat_member = $ message ->getNewChatMember ();
571
-
572
619
$ new_chat_photo = $ message ->getNewChatPhoto ();
573
620
$ left_chat_member = $ message ->getLeftChatMember ();
574
-
575
621
$ migrate_from_chat_id = $ message ->getMigrateFromChatId ();
576
622
$ migrate_to_chat_id = $ message ->getMigrateToChatId ();
577
623
578
- try {
579
- //chat table
580
- $ sth2 = self ::$ pdo ->prepare ('INSERT INTO ` ' . TB_CHAT . '`
581
- (
582
- `id`, `type`, `title`, `created_at` ,`updated_at`, `old_id`
583
- )
584
- VALUES (
585
- :id, :type, :title, :date, :date, :oldid
586
- )
587
- ON DUPLICATE KEY UPDATE `type`=:type, `title`=:title, `updated_at`=:date
588
- ' );
589
-
590
- $ chat_title = $ chat ->getTitle ();
591
- $ type = $ chat ->getType ();
592
-
593
- if ($ migrate_to_chat_id ) {
594
- $ type = 'supergroup ' ;
595
-
596
- $ sth2 ->bindParam (':id ' , $ migrate_to_chat_id , \PDO ::PARAM_INT );
597
- $ sth2 ->bindParam (':oldid ' , $ chat_id , \PDO ::PARAM_INT );
598
- } else {
599
- $ sth2 ->bindParam (':id ' , $ chat_id , \PDO ::PARAM_INT );
600
- $ sth2 ->bindParam (':oldid ' , $ migrate_to_chat_id , \PDO ::PARAM_INT );
601
- }
602
-
603
- $ sth2 ->bindParam (':type ' , $ type , \PDO ::PARAM_INT );
604
- $ sth2 ->bindParam (':title ' , $ chat_title , \PDO ::PARAM_STR , 255 );
605
- $ sth2 ->bindParam (':date ' , $ date , \PDO ::PARAM_STR );
606
-
607
- $ status = $ sth2 ->execute ();
608
- } catch (PDOException $ e ) {
609
- throw new TelegramException ($ e ->getMessage ());
610
- }
624
+ self ::insertChat ($ chat , $ date , $ migrate_to_chat_id );
611
625
612
626
//Insert user and the relation with the chat
613
627
self ::insertUser ($ from , $ date , $ chat );
614
628
629
+ //Forwarded object
630
+ if ($ forward_from || $ forward_from_chat ) {
631
+ $ forward_date = self ::getTimestamp ($ message ->getForwardDate ());
632
+ }
615
633
//Insert the forwarded message user in users table
616
- $ forward_from = null ;
617
634
if (is_object ($ forward_from )) {
618
635
self ::insertUser ($ forward_from , $ forward_date );
619
636
$ forward_from = $ forward_from ->getId ();
620
637
}
621
-
638
+ if (is_object ($ forward_from_chat )) {
639
+ self ::insertChat ($ forward_from_chat , $ forward_date );
640
+ $ forward_from_chat = $ forward_from_chat ->getId ();
641
+ }
642
+
643
+ //New and left chat member
622
644
if ($ new_chat_member ) {
623
645
//Insert the new chat user
624
646
self ::insertUser ($ new_chat_member , $ date , $ chat );
@@ -633,7 +655,7 @@ public static function insertMessageRequest(Message &$message)
633
655
//message Table
634
656
$ sth = self ::$ pdo ->prepare ('INSERT IGNORE INTO ` ' . TB_MESSAGE . '`
635
657
(
636
- `id`, `user_id`, `date`, `chat_id`, `forward_from`,
658
+ `id`, `user_id`, `date`, `chat_id`, `forward_from`, `forward_from_chat`,
637
659
`forward_date`, `reply_to_chat`, `reply_to_message`, `text`, `entities`, `audio`, `document`,
638
660
`photo`, `sticker`, `video`, `voice`, `caption`, `contact`,
639
661
`location`, `venue`, `new_chat_member`, `left_chat_member`,
@@ -642,7 +664,7 @@ public static function insertMessageRequest(Message &$message)
642
664
`migrate_from_chat_id`, `migrate_to_chat_id`, `pinned_message`
643
665
)
644
666
VALUES (
645
- :message_id, :user_id, :date, :chat_id, :forward_from,
667
+ :message_id, :user_id, :date, :chat_id, :forward_from, :forward_from_chat,
646
668
:forward_date, :reply_to_chat, :reply_to_message, :text, :entities, :audio, :document,
647
669
:photo, :sticker, :video, :voice, :caption, :contact,
648
670
:location, :venue, :new_chat_member, :left_chat_member,
@@ -687,6 +709,7 @@ public static function insertMessageRequest(Message &$message)
687
709
$ sth ->bindParam (':user_id ' , $ from_id , \PDO ::PARAM_INT );
688
710
$ sth ->bindParam (':date ' , $ date , \PDO ::PARAM_STR );
689
711
$ sth ->bindParam (':forward_from ' , $ forward_from , \PDO ::PARAM_INT );
712
+ $ sth ->bindParam (':forward_from_chat ' , $ forward_from_chat , \PDO ::PARAM_INT );
690
713
$ sth ->bindParam (':forward_date ' , $ forward_date , \PDO ::PARAM_STR );
691
714
$ reply_chat_id = null ;
692
715
if ($ reply_to_message_id ) {
0 commit comments