Skip to content

Commit

Permalink
Add unique constraint to notification (#3657)
Browse files Browse the repository at this point in the history
* Add unique constraint to notification

* Fix lint

* Fix comment issues

* Fix challenge disbursement test

* fix milestone migration

* Add flush so tx exists before disbursements
  • Loading branch information
jowlee authored Aug 9, 2022
1 parent 4d03415 commit 7436c17
Show file tree
Hide file tree
Showing 24 changed files with 221 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ begin
if reward_manager_tx is not null then
-- create a notification for the challenge disbursement
insert into notification
(slot, user_ids, timestamp, type, specifier, data)
(slot, user_ids, timestamp, type, group_id, specifier, data)
values
(
new.slot,
ARRAY [new.user_id],
reward_manager_tx.created_at,
'challenge_reward',
'challenge_reward:' || new.user_id || ':' || new.specifier,
'challenge_reward',
'challenge_reward:' || new.user_id || ':challenge:' || new.challenge_id || ':specifier:' || new.specifier,
new.user_id,
json_build_object('specifier', new.specifier, 'challenge_id', new.challenge_id, 'amount', new.amount)
)
on conflict do nothing;
Expand Down
6 changes: 4 additions & 2 deletions discovery-provider/alembic/trigger_sql/handle_follow.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ begin
(new.followee_user_id, 'FOLLOWER_COUNT', milestone, new.blocknumber, new.slot, new.created_at)
on conflict do nothing;
insert into notification
(user_ids, type, specifier, blocknumber, timestamp, data)
(user_ids, type, group_id, specifier, blocknumber, timestamp, data)
values
(
ARRAY [new.followee_user_id],
'milestone_follower_count',
'milestone:FOLLOWER_COUNT:id:' || new.followee_user_id || ':threshold:' || milestone,
new.followee_user_id,
new.blocknumber,
new.created_at,
json_build_object('type', 'FOLLOWER_COUNT', 'user_id', new.followee_user_id, 'threshold', milestone)
Expand All @@ -53,13 +54,14 @@ begin
-- create a notification for the followee
if new.is_delete is false then
insert into notification
(blocknumber, user_ids, timestamp, type, specifier, data)
(blocknumber, user_ids, timestamp, type, specifier, group_id, data)
values
(
new.blocknumber,
ARRAY [new.followee_user_id],
new.created_at,
'follow',
new.follower_user_id,
'follow:' || new.followee_user_id,
json_build_object('followee_user_id', new.followee_user_id, 'follower_user_id', new.follower_user_id)
)
Expand Down
27 changes: 15 additions & 12 deletions discovery-provider/alembic/trigger_sql/handle_play.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,21 @@ begin
(new.play_item_id, 'LISTEN_COUNT', milestone, new.slot, new.created_at)
on conflict do nothing;
select tracks.owner_id into owner_user_id from tracks where is_current and track_id = new.play_item_id;
insert into notification
(user_ids, specifier, type, slot, timestamp, data)
values
(
array[owner_user_id],
'milestone:LISTEN_COUNT:id:' || new.play_item_id || ':threshold:' || milestone,
'milestone',
new.slot,
new.created_at,
json_build_object('type', 'LISTEN_COUNT', 'track_id', new.play_item_id, 'threshold', milestone)
)
on conflict do nothing;
if owner_user_id is not null then
insert into notification
(user_ids, specifier, group_id, type, slot, timestamp, data)
values
(
array[owner_user_id],
owner_user_id,
'milestone:LISTEN_COUNT:id:' || new.play_item_id || ':threshold:' || milestone,
'milestone',
new.slot,
new.created_at,
json_build_object('type', 'LISTEN_COUNT', 'track_id', new.play_item_id, 'threshold', milestone)
)
on conflict do nothing;
end if;
end if;
return null;
end;
Expand Down
41 changes: 22 additions & 19 deletions discovery-provider/alembic/trigger_sql/handle_playlist.sql
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,29 @@ begin
end if;

begin
for track_item IN select jsonb_array_elements from jsonb_array_elements(new.playlist_contents -> 'track_ids')
loop
if (track_item->>'time')::double precision::int >= extract(epoch from new.updated_at)::int then
select owner_id into track_owner_id from tracks where is_current and track_id=(track_item->>'track')::int;
if track_owner_id != new.playlist_owner_id then
insert into notification
(blocknumber, user_ids, timestamp, type, specifier, data)
values
(
new.blocknumber,
ARRAY [track_owner_id],
new.updated_at,
'track_added_to_playlist',
'track_added_to_playlist:playlist_id:' || new.playlist_id || ':track_id:' || (track_item->>'track')::int || ':blocknumber:' || new.blocknumber,
json_build_object('track_id', (track_item->>'track')::int, 'playlist_id', new.playlist_id)
)
on conflict do nothing;
if new.is_delete IS FALSE and new.is_private IS FALSE then
for track_item IN select jsonb_array_elements from jsonb_array_elements(new.playlist_contents -> 'track_ids')
loop
if (track_item->>'time')::double precision::int >= extract(epoch from new.updated_at)::int then
select owner_id into track_owner_id from tracks where is_current and track_id=(track_item->>'track')::int;
if track_owner_id != new.playlist_owner_id then
insert into notification
(blocknumber, user_ids, timestamp, type, specifier, group_id, data)
values
(
new.blocknumber,
ARRAY [track_owner_id],
new.updated_at,
'track_added_to_playlist',
track_owner_id,
'track_added_to_playlist:playlist_id:' || new.playlist_id || ':track_id:' || (track_item->>'track')::int || ':blocknumber:' || new.blocknumber,
json_build_object('track_id', (track_item->>'track')::int, 'playlist_id', new.playlist_id)
)
on conflict do nothing;
end if;
end if;
end if;
end loop;
end loop;
end if;
exception
when others then null;
end;
Expand Down
5 changes: 3 additions & 2 deletions discovery-provider/alembic/trigger_sql/handle_reaction.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ begin

if sender_user_id is not null then
insert into notification
(slot, user_ids, timestamp, type, specifier, data)
(slot, user_ids, timestamp, type, specifier, group_id, data)
values
(
new.slot,
ARRAY [sender_user_id],
new.timestamp,
'reaction',
'reaction:' || 'reaction_to:' || new.reacted_to || ':slot:' || new.slot,
sender_user_id,
'reaction:' || 'reaction_to:' || new.reacted_to || ':reaction_type:' || new.reaction_type || ':reaction_value:' || new.reaction_value || ':timestamp:' || new.timestamp,
json_build_object('sender_wallet', new.sender_wallet, 'reaction_type', new.reaction_type, 'reacted_to', new.reacted_to, 'reaction_value', new.reaction_value)
)
on conflict do nothing;
Expand Down
9 changes: 6 additions & 3 deletions discovery-provider/alembic/trigger_sql/handle_repost.sql
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ begin
(new.repost_item_id, milestone_name, milestone, new.blocknumber, new.slot, new.created_at)
on conflict do nothing;
insert into notification
(user_ids, type, specifier, blocknumber, timestamp, data)
(user_ids, type, specifier, group_id, blocknumber, timestamp, data)
values
(
ARRAY [owner_user_id],
'milestone',
owner_user_id,
'milestone:' || milestone_name || ':id:' || new.repost_item_id || ':threshold:' || milestone,
new.blocknumber,
new.created_at,
Expand All @@ -90,13 +91,14 @@ begin
-- create a notification for the reposted content's owner
if new.is_delete is false then
insert into notification
(blocknumber, user_ids, timestamp, type, specifier, data)
(blocknumber, user_ids, timestamp, type, specifier, group_id, data)
values
(
new.blocknumber,
ARRAY [owner_user_id],
new.created_at,
'repost',
new.user_id,
'repost:' || new.repost_item_id || ':type:'|| new.repost_type,
json_build_object('repost_item_id', new.repost_item_id, 'user_id', new.user_id, 'type', new.repost_type)
)
Expand All @@ -111,13 +113,14 @@ begin
where is_current and track_id = (track_remix_of->'tracks'->0->>'parent_track_id')::int;
if is_remix_cosign then
insert into notification
(blocknumber, user_ids, timestamp, type, specifier, data)
(blocknumber, user_ids, timestamp, type, specifier, group_id, data)
values
(
new.blocknumber,
ARRAY [owner_user_id],
new.created_at,
'cosign',
new.user_id,
'cosign:parent_track' || (track_remix_of->'tracks'->0->>'parent_track_id')::int || ':original_track:'|| new.repost_item_id,
json_build_object('parent_track_id', (track_remix_of->'tracks'->0->>'parent_track_id')::int, 'track_id', new.repost_item_id, 'track_owner_id', owner_user_id)
)
Expand Down
9 changes: 6 additions & 3 deletions discovery-provider/alembic/trigger_sql/handle_save.sql
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,12 @@ begin
(new.save_item_id, milestone_name, milestone, new.blocknumber, new.slot, new.created_at)
on conflict do nothing;
insert into notification
(user_ids, type, specifier, blocknumber, timestamp, data)
(user_ids, type, specifier, group_id, blocknumber, timestamp, data)
values
(
ARRAY [owner_user_id],
'milestone',
owner_user_id,
'milestone:' || milestone_name || ':id:' || new.save_item_id || ':threshold:' || milestone,
new.blocknumber,
new.created_at,
Expand All @@ -94,13 +95,14 @@ begin
-- create a notification for the saved content's owner
if new.is_delete is false then
insert into notification
(blocknumber, user_ids, timestamp, type, specifier, data)
(blocknumber, user_ids, timestamp, type, specifier, group_id, data)
values
(
new.blocknumber,
ARRAY [owner_user_id],
new.created_at,
'save',
new.user_id,
'save:' || new.save_item_id || ':type:'|| new.save_type,
json_build_object('save_item_id', new.save_item_id, 'user_id', new.user_id, 'type', new.save_type)
)
Expand All @@ -115,13 +117,14 @@ begin
where is_current and track_id = (track_remix_of->'tracks'->0->>'parent_track_id')::int;
if is_remix_cosign then
insert into notification
(blocknumber, user_ids, timestamp, type, specifier, data)
(blocknumber, user_ids, timestamp, type, specifier, group_id, data)
values
(
new.blocknumber,
ARRAY [owner_user_id],
new.created_at,
'cosign',
new.user_id,
'cosign:parent_track' || (track_remix_of->'tracks'->0->>'parent_track_id')::int || ':original_track:'|| new.save_item_id,
json_build_object('parent_track_id', (track_remix_of->'tracks'->0->>'parent_track_id')::int, 'track_id', new.save_item_id, 'track_owner_id', owner_user_id)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@ begin
if user_bank_tx is not null then
-- create a notification for the sender and receiver
insert into notification
(slot, user_ids, timestamp, type, specifier, data)
(slot, user_ids, timestamp, type, specifier, group_id, data)
values
(
new.slot,
ARRAY [new.sender_user_id],
user_bank_tx.created_at,
'supporter_rank_up',
'supporter_rank_up:' || new.rank || new.slot,
new.sender_user_id,
'supporter_rank_up:' || new.rank || ':slot:' || new.slot,
json_build_object('sender_user_id', new.sender_user_id, 'receiver_user_id', new.receiver_user_id, 'rank', new.rank)
),
(
new.slot,
ARRAY [new.receiver_user_id],
user_bank_tx.created_at,
'supporting_rank_up',
'supporting_rank_up:' || new.rank || new.slot,
new.receiver_user_id,
'supporting_rank_up:' || new.rank || ':slot:' || new.slot,
json_build_object('sender_user_id', new.sender_user_id, 'receiver_user_id', new.receiver_user_id, 'rank', new.rank)
)
on conflict do nothing;
Expand Down
7 changes: 4 additions & 3 deletions discovery-provider/alembic/trigger_sql/handle_track.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,19 @@ begin

-- If remix, create notification
begin
if new.remix_of is not null then
if new.remix_of is not null AND new.is_unlisted = FALSE AND new.is_delete = FALSE AND new.stem_of IS NULL then
select owner_id into parent_track_owner_id from tracks where is_current and track_id = (new.remix_of->'tracks'->0->>'parent_track_id')::int limit 1;
if parent_track_owner_id is not null then
insert into notification
(blocknumber, user_ids, timestamp, type, specifier, data)
(blocknumber, user_ids, timestamp, type, specifier, group_id, data)
values
(
new.blocknumber,
ARRAY [parent_track_owner_id],
new.updated_at,
'remix',
'remix:' || new.track_id || ':parent_track:' || (new.remix_of->'tracks'->0->>'parent_track_id')::int || ':blocknumber:' || new.blocknumber,
new.owner_id,
'remix:track:' || new.track_id || ':parent_track:' || (new.remix_of->'tracks'->0->>'parent_track_id')::int || ':blocknumber:' || new.blocknumber,
json_build_object('track_id', new.track_id, 'parent_track_id', (new.remix_of->'tracks'->0->>'parent_track_id')::int)
)
on conflict do nothing;
Expand Down
8 changes: 5 additions & 3 deletions discovery-provider/alembic/trigger_sql/handle_user_tip.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@ begin

-- create a notification for the sender and receiver
insert into notification
(slot, user_ids, timestamp, type, specifier, data)
(slot, user_ids, timestamp, type, specifier, group_id, data)
values
(
new.slot,
ARRAY [new.receiver_user_id],
new.created_at,
'tip_receive',
'tip_receive:' || new.receiver_user_id || ':' || new.slot,
new.receiver_user_id,
'tip_receive:user_id:' || new.receiver_user_id || ':slot:' || new.slot,
json_build_object('sender_user_id', new.sender_user_id, 'receiver_user_id', new.receiver_user_id, 'amount', new.amount)
),
(
new.slot,
ARRAY [new.sender_user_id],
new.created_at,
'tip_send',
'tip_send:' || new.sender_user_id || ':' || new.slot,
new.sender_user_id,
'tip_send:user_id:' || new.sender_user_id || ':slot:' || new.slot,
json_build_object('sender_user_id', new.sender_user_id, 'receiver_user_id', new.receiver_user_id, 'amount', new.amount)
)
on conflict do nothing;
Expand Down
Loading

0 comments on commit 7436c17

Please sign in to comment.