Fix unfollow on multiple version of article/constitution
This commit is contained in:
@@ -1,12 +1,39 @@
|
|||||||
create or replace function unfollow(reference regclass, _target_id uuid, _created_by_id uuid) returns void
|
create or replace function unfollow(reference regclass, _target_id uuid, _created_by_id uuid) returns void
|
||||||
language plpgsql as
|
language plpgsql as
|
||||||
$$
|
$$
|
||||||
|
declare
|
||||||
|
_targets_ids uuid[];
|
||||||
begin
|
begin
|
||||||
delete
|
if reference = 'article'::regclass then
|
||||||
from follow f
|
select array_agg(a2.id) into _targets_ids
|
||||||
where f.created_by_id = _created_by_id
|
from article a1
|
||||||
and f.target_id = _target_id
|
join article a2 using (version_id)
|
||||||
and f.target_reference = reference;
|
where a1.id = _target_id;
|
||||||
|
|
||||||
|
delete
|
||||||
|
from follow f
|
||||||
|
where f.created_by_id = _created_by_id
|
||||||
|
and f.target_id = any(_targets_ids)
|
||||||
|
and f.target_reference = reference;
|
||||||
|
elseif reference = 'constitution'::regclass then
|
||||||
|
select array_agg(c2.id) into _targets_ids
|
||||||
|
from constitution c1
|
||||||
|
join constitution c2 using (version_id)
|
||||||
|
where c1.id = _target_id;
|
||||||
|
delete
|
||||||
|
from follow f
|
||||||
|
where f.created_by_id = _created_by_id
|
||||||
|
and f.target_id = any(_targets_ids)
|
||||||
|
and f.target_reference = reference;
|
||||||
|
elseif reference = 'citizen'::regclass then
|
||||||
|
delete
|
||||||
|
from follow f
|
||||||
|
where f.created_by_id = _created_by_id
|
||||||
|
and f.target_id = _target_id
|
||||||
|
and f.target_reference = reference;
|
||||||
|
else
|
||||||
|
raise exception '% no implemented for follow', reference::text;
|
||||||
|
end if;
|
||||||
end;
|
end;
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ declare
|
|||||||
|
|
||||||
created_article json := '{"version_id":"933b6a1b-50c9-42b6-989f-c02a57814ef9", "title": "Love the world", "anonymous": false, "content": "bla bal bla", "tags": ["love", "test"], "draft":false}';
|
created_article json := '{"version_id":"933b6a1b-50c9-42b6-989f-c02a57814ef9", "title": "Love the world", "anonymous": false, "content": "bla bal bla", "tags": ["love", "test"], "draft":false}';
|
||||||
first_article_id uuid;
|
first_article_id uuid;
|
||||||
|
first_article_updated_id uuid;
|
||||||
begin
|
begin
|
||||||
-- insert user for context
|
-- insert user for context
|
||||||
select insert_user(created_user) into created_user;
|
select insert_user(created_user) into created_user;
|
||||||
@@ -61,6 +62,17 @@ begin
|
|||||||
perform follow('article'::regclass, first_article_id, _citizen_id);
|
perform follow('article'::regclass, first_article_id, _citizen_id);
|
||||||
assert (select following = true from find_follow(first_article_id, _citizen_id)), 'find_follow must return the following';
|
assert (select following = true from find_follow(first_article_id, _citizen_id)), 'find_follow must return the following';
|
||||||
assert (select following = false from find_follow(first_article_id, _citizen_id2)), 'find_follow must not return the following if not followinf';
|
assert (select following = false from find_follow(first_article_id, _citizen_id2)), 'find_follow must not return the following if not followinf';
|
||||||
|
assert (select count(*) = 1 from follow), 'must have 1 following';
|
||||||
|
|
||||||
|
-- add new version for article, then unfollow the new one
|
||||||
|
select upsert_article(created_article) into created_article;
|
||||||
|
first_article_updated_id = (created_article->>'id')::uuid;
|
||||||
|
assert first_article_id != first_article_updated_id;
|
||||||
|
|
||||||
|
perform unfollow('article'::regclass, first_article_id, _citizen_id);
|
||||||
|
-- perform unfollow('article'::regclass, first_article_updated_id, _citizen_id);
|
||||||
|
assert (select count(*) = 0 from follow), 'follow must be deleted after unfollow, event if article is on other version';
|
||||||
|
|
||||||
|
|
||||||
-- delete follow and context
|
-- delete follow and context
|
||||||
delete from follow;
|
delete from follow;
|
||||||
|
|||||||
Reference in New Issue
Block a user