Skip to content

Commit

Permalink
Facilitate multiple Safe notifications across devices
Browse files Browse the repository at this point in the history
  • Loading branch information
iamacook committed Jul 24, 2024
1 parent bbf8872 commit 906e7b6
Show file tree
Hide file tree
Showing 6 changed files with 1,079 additions and 1,172 deletions.
98 changes: 47 additions & 51 deletions migrations/00005_notifications/index.sql
Original file line number Diff line number Diff line change
@@ -1,13 +1,40 @@
DROP TABLE IF EXISTS notification_types,
notification_subscriptions,
notification_subscription_notification_types,
notification_channels,
notification_channel_configurations CASCADE;
DROP TABLE IF EXISTS notification_devices, notification_channels, notification_types, notification_subscriptions, notification_subscription_notification_types CASCADE;

---------------------------------------------------
-- Notification devices: 'ANDROID', 'IOS', 'WEB' --
---------------------------------------------------
CREATE TABLE notification_devices (
id SERIAL PRIMARY KEY,
account_id INT NOT NULL,
device_type VARCHAR(255) CHECK (device_type IN ('ANDROID', 'IOS', 'WEB')) NOT NULL,
device_uuid UUID NOT NULL UNIQUE,
cloud_messaging_token VARCHAR(255) NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
FOREIGN KEY (account_id) REFERENCES accounts(id) ON DELETE CASCADE
);

-- Update updated_at when device is updated to track validity of token
CREATE OR REPLACE TRIGGER update_notification_devices_updated_at
BEFORE UPDATE ON notification_devices
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();

CREATE TABLE notification_channels (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL UNIQUE
);

----------------------------------------------------
-- Notification channels, e.g. PUSH_NOTIFICATIONS --
----------------------------------------------------
INSERT INTO notification_channels (name) VALUES
('PUSH_NOTIFICATIONS');

--------------------------------------------
-- Notification types, e.g. INCOMING_TOKEN --
--------------------------------------------
CREATE TABLE notification_types(
CREATE TABLE notification_types (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL UNIQUE
);
Expand All @@ -21,18 +48,22 @@ INSERT INTO notification_types (name) VALUES
('MESSAGE_CONFIRMATION_REQUEST'), -- MESSAGE_CREATED
('MODULE_TRANSACTION');

----------------------------------------------------------------------
-- Chain-specific Safe notification preferences for a given account --
----------------------------------------------------------------------
CREATE TABLE notification_subscriptions(
-----------------------------------------------------------
-- Safe subscriptions for a given account-device-channel --
-----------------------------------------------------------
CREATE TABLE notification_subscriptions (
id SERIAL PRIMARY KEY,
account_id INT NOT NULL,
device_id INT NOT NULL,
chain_id VARCHAR(255) NOT NULL,
safe_address VARCHAR(42) NOT NULL,
notification_channel_id INT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
FOREIGN KEY (account_id) REFERENCES accounts(id) ON DELETE CASCADE,
UNIQUE(account_id, chain_id, safe_address)
FOREIGN KEY (device_id) REFERENCES notification_devices(id) ON DELETE CASCADE,
FOREIGN KEY (notification_channel_id) REFERENCES notification_channels(id) ON DELETE CASCADE,
UNIQUE(account_id, chain_id, safe_address, device_id, notification_channel_id)
);

-- Update updated_at when a notification subscription is updated
Expand All @@ -42,46 +73,11 @@ CREATE OR REPLACE TRIGGER update_notification_subscriptions_updated_at
EXECUTE FUNCTION update_updated_at_column();

-- Join table for subscriptions/notification types
CREATE TABLE notification_subscription_notification_types(
id SERIAL PRIMARY KEY,
subscription_id INT NOT NULL,
notification_type_id INT NOT NULL,
FOREIGN KEY (subscription_id) REFERENCES notification_subscriptions(id) ON DELETE CASCADE,
FOREIGN KEY (notification_type_id) REFERENCES notification_types(id) ON DELETE CASCADE,
UNIQUE (subscription_id, notification_type_id)
);

---------------------------------------------------
-- Notification channels, e.g. PUSH_NOTIFICATIONS --
---------------------------------------------------
CREATE TABLE notification_channels(
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL UNIQUE
);

-- Add PUSH_NOTIFICATIONS as a notification channel
INSERT INTO notification_channels (name) VALUES
('PUSH_NOTIFICATIONS');

----------------------------------------------------------------
-- Configuration for a given notification subscription/channel --
----------------------------------------------------------------
CREATE TABLE notification_channel_configurations(
CREATE TABLE notification_subscription_notification_types (
id SERIAL PRIMARY KEY,
notification_subscription_id INT NOT NULL,
notification_channel_id INT NOT NULL,
cloud_messaging_token VARCHAR(255) NOT NULL,
device_type VARCHAR(255) CHECK (device_type IN ('ANDROID', 'IOS', 'WEB')) NOT NULL,
device_uuid UUID NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
notification_type_id INT NOT NULL,
FOREIGN KEY (notification_subscription_id) REFERENCES notification_subscriptions(id) ON DELETE CASCADE,
FOREIGN KEY (notification_channel_id) REFERENCES notification_channels(id) ON DELETE CASCADE,
UNIQUE (notification_subscription_id, notification_channel_id, device_uuid)
);

-- Update updated_at when a notification channel is updated
CREATE OR REPLACE TRIGGER update_notification_channel_configurations_updated_at
BEFORE UPDATE ON notification_channel_configurations
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
FOREIGN KEY (notification_type_id) REFERENCES notification_types(id) ON DELETE CASCADE,
UNIQUE(notification_subscription_id, notification_type_id)
);
Loading

0 comments on commit 906e7b6

Please sign in to comment.