fix: create activities after update trigger
SET AUTOCOMMIT = 0;
START TRANSACTION;
DROP TRIGGER IF EXISTS cp_fediverse_activities_after_insert;
DELIMITER $$
CREATE TRIGGER `cp_fediverse_activities_after_insert` AFTER INSERT ON `cp_fediverse_activities` 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 `cp_notifications` (`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 `cp_notifications`
WHERE `actor_id` = NEW.actor_id
AND `target_actor_id` = NEW.target_actor_id
AND `type` = 'follow';
END IF;
END IF;
END
$$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER `cp_fediverse_activities_after_update` AFTER UPDATE ON `cp_fediverse_activities` 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 IN ('Create', 'Like', 'Announce') AND OLD.post_id IS NULL AND NEW.post_id IS NOT NULL THEN
SET @type = (CASE
WHEN NEW.type = 'Create' THEN 'reply'
WHEN NEW.type = 'Like' THEN 'like'
WHEN NEW.type = 'Announce' THEN 'share'
END);
INSERT INTO `cp_notifications` (`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);
ELSEIF NEW.type IN ('Undo_Like', 'Undo_Announce') THEN
DELETE FROM `cp_notifications`
WHERE `actor_id` = NEW.actor_id
AND `target_actor_id` = NEW.target_actor_id
AND `type` = (CASE
WHEN NEW.type = 'Undo_Like' THEN 'like'
WHEN NEW.type = 'Undo_Announce' THEN 'share'
END)
AND `post_id` = NEW.post_id;
END IF;
END IF;
END
$$
DELIMITER ;
COMMIT;
Edited by Ola Hneini