mirror of
https://code.castopod.org/adaures/castopod.git
synced 2024-09-30 05:31:22 +02:00
fix(notifications): add trigger after activities update + update insert trigger
This commit is contained in:
parent
9222975856
commit
e5d16e8711
@ -0,0 +1,50 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class AddActivitiesTriggerAfterInsert Creates activities trigger in database
|
||||||
|
*
|
||||||
|
* @copyright 2020 Ad Aures
|
||||||
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
||||||
|
* @link https://castopod.org/
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Database\Migrations;
|
||||||
|
|
||||||
|
use CodeIgniter\Database\Migration;
|
||||||
|
|
||||||
|
class AddActivitiesTriggerAfterInsert extends Migration
|
||||||
|
{
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
$activitiesTable = $this->db->prefixTable(config('Fediverse')->tablesPrefix . 'activities');
|
||||||
|
$notificationsTable = $this->db->prefixTable('notifications');
|
||||||
|
$createQuery = <<<CODE_SAMPLE
|
||||||
|
CREATE TRIGGER `{$activitiesTable}_after_insert`
|
||||||
|
AFTER INSERT ON `{$activitiesTable}`
|
||||||
|
FOR EACH ROW
|
||||||
|
BEGIN
|
||||||
|
-- only create notification if new incoming activity with NULL status is created
|
||||||
|
IF NEW.target_actor_id AND NEW.target_actor_id != NEW.actor_id AND NEW.status IS NULL THEN
|
||||||
|
IF NEW.type = 'Follow' THEN
|
||||||
|
INSERT INTO `{$notificationsTable}` (`actor_id`, `target_actor_id`, `activity_id`, `type`, `created_at`, `updated_at`)
|
||||||
|
VALUES (NEW.actor_id, NEW.target_actor_id, NEW.id, 'follow', NEW.created_at, NEW.created_at);
|
||||||
|
ELSEIF NEW.type = 'Undo_Follow' THEN
|
||||||
|
DELETE FROM `{$notificationsTable}`
|
||||||
|
WHERE `actor_id` = NEW.actor_id
|
||||||
|
AND `target_actor_id` = NEW.target_actor_id
|
||||||
|
AND `type` = 'follow';
|
||||||
|
END IF;
|
||||||
|
END IF;
|
||||||
|
END
|
||||||
|
CODE_SAMPLE;
|
||||||
|
$this->db->query($createQuery);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
$activitiesTable = $this->db->prefixTable(config('Fediverse')->tablesPrefix . 'activities');
|
||||||
|
$this->db->query("DROP TRIGGER IF EXISTS `{$activitiesTable}_after_insert`");
|
||||||
|
}
|
||||||
|
}
|
@ -3,7 +3,7 @@
|
|||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class AddActivitiesTrigger Creates activities trigger in database
|
* Class AddActivitiesTriggerAfterUpdate Creates activities trigger in database
|
||||||
*
|
*
|
||||||
* @copyright 2020 Ad Aures
|
* @copyright 2020 Ad Aures
|
||||||
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
||||||
@ -14,39 +14,36 @@ namespace App\Database\Migrations;
|
|||||||
|
|
||||||
use CodeIgniter\Database\Migration;
|
use CodeIgniter\Database\Migration;
|
||||||
|
|
||||||
class AddNotificationsTrigger extends Migration
|
class AddActivitiesTriggerAfterUpdate extends Migration
|
||||||
{
|
{
|
||||||
public function up(): void
|
public function up(): void
|
||||||
{
|
{
|
||||||
$activitiesTable = $this->db->prefixTable(config('Fediverse')->tablesPrefix . 'activities');
|
$activitiesTable = $this->db->prefixTable(config('Fediverse')->tablesPrefix . 'activities');
|
||||||
$notificationsTable = $this->db->prefixTable('notifications');
|
$notificationsTable = $this->db->prefixTable('notifications');
|
||||||
$createQuery = <<<CODE_SAMPLE
|
$createQuery = <<<CODE_SAMPLE
|
||||||
CREATE TRIGGER `{$activitiesTable}_after_insert`
|
CREATE TRIGGER `{$activitiesTable}_after_update`
|
||||||
AFTER INSERT ON `{$activitiesTable}`
|
AFTER UPDATE ON `{$activitiesTable}`
|
||||||
FOR EACH ROW
|
FOR EACH ROW
|
||||||
BEGIN
|
BEGIN
|
||||||
-- only create notification if new incoming activity with NULL status is created
|
-- only create notification if new incoming activity with NULL status is created
|
||||||
IF NEW.target_actor_id AND NEW.target_actor_id != NEW.actor_id AND NEW.status IS NULL THEN
|
IF NEW.target_actor_id AND NEW.target_actor_id != NEW.actor_id AND NEW.status IS NULL THEN
|
||||||
IF NEW.type IN ( 'Create', 'Like', 'Announce', 'Follow' ) THEN
|
IF NEW.type IN ('Create', 'Like', 'Announce') AND OLD.post_id IS NULL AND NEW.post_id IS NOT NULL THEN
|
||||||
SET @type = (CASE
|
SET @type = (CASE
|
||||||
WHEN NEW.type = 'Create' THEN 'reply'
|
WHEN NEW.type = 'Create' THEN 'reply'
|
||||||
WHEN NEW.type = 'Like' THEN 'like'
|
WHEN NEW.type = 'Like' THEN 'like'
|
||||||
WHEN NEW.type = 'Announce' THEN 'share'
|
WHEN NEW.type = 'Announce' THEN 'share'
|
||||||
WHEN NEW.type = 'Follow' THEN 'follow'
|
|
||||||
END);
|
END);
|
||||||
INSERT INTO `{$notificationsTable}` (`actor_id`, `target_actor_id`, `post_id`, `activity_id`, `type`, `created_at`, `updated_at`)
|
INSERT INTO `{$notificationsTable}` (`actor_id`, `target_actor_id`,`post_id`, `activity_id`, `type`, `created_at`, `updated_at`)
|
||||||
VALUES (NEW.actor_id, NEW.target_actor_id, NEW.post_id, NEW.id, @type, NEW.created_at, NEW.created_at);
|
VALUES (NEW.actor_id, NEW.target_actor_id,NEW.post_id, NEW.id, @type, NEW.created_at, NEW.created_at);
|
||||||
ELSE
|
ELSEIF NEW.type IN ('Undo_Like', 'Undo_Announce') THEN
|
||||||
DELETE FROM `{$notificationsTable}`
|
DELETE FROM `{$notificationsTable}`
|
||||||
WHERE `actor_id` = NEW.actor_id
|
WHERE `actor_id` = NEW.actor_id
|
||||||
AND `target_actor_id` = NEW.target_actor_id
|
AND `target_actor_id` = NEW.target_actor_id
|
||||||
AND ((`type` = (CASE WHEN NEW.type = 'Undo_Follow' THEN 'follow' END) AND `post_id` IS NULL)
|
AND `type` = (CASE
|
||||||
OR (`type` = (CASE
|
|
||||||
WHEN NEW.type = 'Delete' THEN 'reply'
|
|
||||||
WHEN NEW.type = 'Undo_Like' THEN 'like'
|
WHEN NEW.type = 'Undo_Like' THEN 'like'
|
||||||
WHEN NEW.type = 'Undo_Announce' THEN 'share'
|
WHEN NEW.type = 'Undo_Announce' THEN 'share'
|
||||||
END)
|
END)
|
||||||
AND `post_id` = NEW.post_id));
|
AND `post_id` = NEW.post_id;
|
||||||
END IF;
|
END IF;
|
||||||
END IF;
|
END IF;
|
||||||
END
|
END
|
||||||
@ -57,6 +54,6 @@ class AddNotificationsTrigger extends Migration
|
|||||||
public function down(): void
|
public function down(): void
|
||||||
{
|
{
|
||||||
$activitiesTable = $this->db->prefixTable(config('Fediverse')->tablesPrefix . 'activities');
|
$activitiesTable = $this->db->prefixTable(config('Fediverse')->tablesPrefix . 'activities');
|
||||||
$this->db->query("DROP TRIGGER IF EXISTS `{$activitiesTable}_after_insert`");
|
$this->db->query("DROP TRIGGER IF EXISTS `{$activitiesTable}_after_update`");
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user