mirror of https://github.com/LemmyNet/lemmy
Merge pull request #1180 from LemmyNet/no_conflict_triggers
No send blocked and no conflict triggerspull/1183/head
commit
8d0580461b
@ -1,49 +0,0 @@
|
||||
use crate::{activity_queue::send_activity, community::do_announce, insert_activity};
|
||||
use activitystreams::{
|
||||
base::{Extends, ExtendsExt},
|
||||
object::AsObject,
|
||||
};
|
||||
use lemmy_db::{community::Community, user::User_};
|
||||
use lemmy_utils::{settings::Settings, LemmyError};
|
||||
use lemmy_websocket::LemmyContext;
|
||||
use serde::{export::fmt::Debug, Serialize};
|
||||
use url::{ParseError, Url};
|
||||
use uuid::Uuid;
|
||||
|
||||
pub async fn send_activity_to_community<T, Kind>(
|
||||
creator: &User_,
|
||||
community: &Community,
|
||||
to: Vec<Url>,
|
||||
activity: T,
|
||||
context: &LemmyContext,
|
||||
) -> Result<(), LemmyError>
|
||||
where
|
||||
T: AsObject<Kind> + Extends<Kind> + Serialize + Debug + Send + Clone + 'static,
|
||||
Kind: Serialize,
|
||||
<T as Extends<Kind>>::Error: From<serde_json::Error> + Send + Sync + 'static,
|
||||
{
|
||||
// TODO: looks like call this sometimes with activity, and sometimes with any_base
|
||||
insert_activity(creator.id, activity.clone(), true, context.pool()).await?;
|
||||
|
||||
// if this is a local community, we need to do an announce from the community instead
|
||||
if community.local {
|
||||
do_announce(activity.into_any_base()?, &community, creator, context).await?;
|
||||
} else {
|
||||
send_activity(context.activity_queue(), activity, creator, to)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(in crate) fn generate_activity_id<T>(kind: T) -> Result<Url, ParseError>
|
||||
where
|
||||
T: ToString,
|
||||
{
|
||||
let id = format!(
|
||||
"{}/activities/{}/{}",
|
||||
Settings::get().get_protocol_and_hostname(),
|
||||
kind.to_string().to_lowercase(),
|
||||
Uuid::new_v4()
|
||||
);
|
||||
Url::parse(&id)
|
||||
}
|
@ -0,0 +1,128 @@
|
||||
create or replace function refresh_community()
|
||||
returns trigger language plpgsql
|
||||
as $$
|
||||
begin
|
||||
IF (TG_OP = 'DELETE') THEN
|
||||
delete from community_aggregates_fast where id = OLD.id;
|
||||
ELSIF (TG_OP = 'UPDATE') THEN
|
||||
delete from community_aggregates_fast where id = OLD.id;
|
||||
insert into community_aggregates_fast select * from community_aggregates_view where id = NEW.id;
|
||||
|
||||
-- Update user view due to owner changes
|
||||
delete from user_fast where id = NEW.creator_id;
|
||||
insert into user_fast select * from user_view where id = NEW.creator_id;
|
||||
|
||||
-- Update post view due to community changes
|
||||
delete from post_aggregates_fast where community_id = NEW.id;
|
||||
insert into post_aggregates_fast select * from post_aggregates_view where community_id = NEW.id;
|
||||
|
||||
-- TODO make sure this shows up in the users page ?
|
||||
ELSIF (TG_OP = 'INSERT') THEN
|
||||
insert into community_aggregates_fast select * from community_aggregates_view where id = NEW.id;
|
||||
END IF;
|
||||
|
||||
return null;
|
||||
end $$;
|
||||
|
||||
create or replace function refresh_user()
|
||||
returns trigger language plpgsql
|
||||
as $$
|
||||
begin
|
||||
IF (TG_OP = 'DELETE') THEN
|
||||
delete from user_fast where id = OLD.id;
|
||||
ELSIF (TG_OP = 'UPDATE') THEN
|
||||
delete from user_fast where id = OLD.id;
|
||||
insert into user_fast select * from user_view where id = NEW.id;
|
||||
|
||||
-- Refresh post_fast, cause of user info changes
|
||||
delete from post_aggregates_fast where creator_id = NEW.id;
|
||||
insert into post_aggregates_fast select * from post_aggregates_view where creator_id = NEW.id;
|
||||
|
||||
delete from comment_aggregates_fast where creator_id = NEW.id;
|
||||
insert into comment_aggregates_fast select * from comment_aggregates_view where creator_id = NEW.id;
|
||||
|
||||
ELSIF (TG_OP = 'INSERT') THEN
|
||||
insert into user_fast select * from user_view where id = NEW.id;
|
||||
END IF;
|
||||
|
||||
return null;
|
||||
end $$;
|
||||
|
||||
create or replace function refresh_post()
|
||||
returns trigger language plpgsql
|
||||
as $$
|
||||
begin
|
||||
IF (TG_OP = 'DELETE') THEN
|
||||
delete from post_aggregates_fast where id = OLD.id;
|
||||
|
||||
-- Update community number of posts
|
||||
update community_aggregates_fast set number_of_posts = number_of_posts - 1 where id = OLD.community_id;
|
||||
ELSIF (TG_OP = 'UPDATE') THEN
|
||||
delete from post_aggregates_fast where id = OLD.id;
|
||||
insert into post_aggregates_fast select * from post_aggregates_view where id = NEW.id;
|
||||
ELSIF (TG_OP = 'INSERT') THEN
|
||||
insert into post_aggregates_fast select * from post_aggregates_view where id = NEW.id;
|
||||
|
||||
-- Update that users number of posts, post score
|
||||
delete from user_fast where id = NEW.creator_id;
|
||||
insert into user_fast select * from user_view where id = NEW.creator_id;
|
||||
|
||||
-- Update community number of posts
|
||||
update community_aggregates_fast set number_of_posts = number_of_posts + 1 where id = NEW.community_id;
|
||||
|
||||
-- Update the hot rank on the post table
|
||||
-- TODO this might not correctly update it, using a 1 week interval
|
||||
update post_aggregates_fast as paf
|
||||
set hot_rank = pav.hot_rank
|
||||
from post_aggregates_view as pav
|
||||
where paf.id = pav.id and (pav.published > ('now'::timestamp - '1 week'::interval));
|
||||
END IF;
|
||||
|
||||
return null;
|
||||
end $$;
|
||||
|
||||
create or replace function refresh_comment()
|
||||
returns trigger language plpgsql
|
||||
as $$
|
||||
begin
|
||||
IF (TG_OP = 'DELETE') THEN
|
||||
delete from comment_aggregates_fast where id = OLD.id;
|
||||
|
||||
-- Update community number of comments
|
||||
update community_aggregates_fast as caf
|
||||
set number_of_comments = number_of_comments - 1
|
||||
from post as p
|
||||
where caf.id = p.community_id and p.id = OLD.post_id;
|
||||
|
||||
ELSIF (TG_OP = 'UPDATE') THEN
|
||||
delete from comment_aggregates_fast where id = OLD.id;
|
||||
insert into comment_aggregates_fast select * from comment_aggregates_view where id = NEW.id;
|
||||
ELSIF (TG_OP = 'INSERT') THEN
|
||||
insert into comment_aggregates_fast select * from comment_aggregates_view where id = NEW.id;
|
||||
|
||||
-- Update user view due to comment count
|
||||
update user_fast
|
||||
set number_of_comments = number_of_comments + 1
|
||||
where id = NEW.creator_id;
|
||||
|
||||
-- Update post view due to comment count, new comment activity time, but only on new posts
|
||||
-- TODO this could be done more efficiently
|
||||
delete from post_aggregates_fast where id = NEW.post_id;
|
||||
insert into post_aggregates_fast select * from post_aggregates_view where id = NEW.post_id;
|
||||
|
||||
-- Force the hot rank as zero on week-older posts
|
||||
update post_aggregates_fast as paf
|
||||
set hot_rank = 0
|
||||
where paf.id = NEW.post_id and (paf.published < ('now'::timestamp - '1 week'::interval));
|
||||
|
||||
-- Update community number of comments
|
||||
update community_aggregates_fast as caf
|
||||
set number_of_comments = number_of_comments + 1
|
||||
from post as p
|
||||
where caf.id = p.community_id and p.id = NEW.post_id;
|
||||
|
||||
END IF;
|
||||
|
||||
return null;
|
||||
end $$;
|
||||
|
@ -0,0 +1,132 @@
|
||||
-- This adds on conflict do nothing triggers to all the insert_intos
|
||||
-- Github issue: https://github.com/LemmyNet/lemmy/issues/1179
|
||||
|
||||
create or replace function refresh_community()
|
||||
returns trigger language plpgsql
|
||||
as $$
|
||||
begin
|
||||
IF (TG_OP = 'DELETE') THEN
|
||||
delete from community_aggregates_fast where id = OLD.id;
|
||||
ELSIF (TG_OP = 'UPDATE') THEN
|
||||
delete from community_aggregates_fast where id = OLD.id;
|
||||
insert into community_aggregates_fast select * from community_aggregates_view where id = NEW.id on conflict (id) do nothing;
|
||||
|
||||
-- Update user view due to owner changes
|
||||
delete from user_fast where id = NEW.creator_id;
|
||||
insert into user_fast select * from user_view where id = NEW.creator_id on conflict (id) do nothing;
|
||||
|
||||
-- Update post view due to community changes
|
||||
delete from post_aggregates_fast where community_id = NEW.id;
|
||||
insert into post_aggregates_fast select * from post_aggregates_view where community_id = NEW.id on conflict (id) do nothing;
|
||||
|
||||
-- TODO make sure this shows up in the users page ?
|
||||
ELSIF (TG_OP = 'INSERT') THEN
|
||||
insert into community_aggregates_fast select * from community_aggregates_view where id = NEW.id;
|
||||
END IF;
|
||||
|
||||
return null;
|
||||
end $$;
|
||||
|
||||
create or replace function refresh_user()
|
||||
returns trigger language plpgsql
|
||||
as $$
|
||||
begin
|
||||
IF (TG_OP = 'DELETE') THEN
|
||||
delete from user_fast where id = OLD.id;
|
||||
ELSIF (TG_OP = 'UPDATE') THEN
|
||||
delete from user_fast where id = OLD.id;
|
||||
insert into user_fast select * from user_view where id = NEW.id on conflict(id) do nothing;
|
||||
|
||||
-- Refresh post_fast, cause of user info changes
|
||||
delete from post_aggregates_fast where creator_id = NEW.id;
|
||||
insert into post_aggregates_fast select * from post_aggregates_view where creator_id = NEW.id on conflict (id) do nothing;
|
||||
|
||||
delete from comment_aggregates_fast where creator_id = NEW.id;
|
||||
insert into comment_aggregates_fast select * from comment_aggregates_view where creator_id = NEW.id on conflict (id) do nothing;
|
||||
|
||||
ELSIF (TG_OP = 'INSERT') THEN
|
||||
insert into user_fast select * from user_view where id = NEW.id;
|
||||
END IF;
|
||||
|
||||
return null;
|
||||
end $$;
|
||||
|
||||
create or replace function refresh_post()
|
||||
returns trigger language plpgsql
|
||||
as $$
|
||||
begin
|
||||
IF (TG_OP = 'DELETE') THEN
|
||||
delete from post_aggregates_fast where id = OLD.id;
|
||||
|
||||
-- Update community number of posts
|
||||
update community_aggregates_fast set number_of_posts = number_of_posts - 1 where id = OLD.community_id;
|
||||
ELSIF (TG_OP = 'UPDATE') THEN
|
||||
delete from post_aggregates_fast where id = OLD.id;
|
||||
insert into post_aggregates_fast select * from post_aggregates_view where id = NEW.id on conflict (id) do nothing;
|
||||
|
||||
ELSIF (TG_OP = 'INSERT') THEN
|
||||
insert into post_aggregates_fast select * from post_aggregates_view where id = NEW.id;
|
||||
|
||||
-- Update that users number of posts, post score
|
||||
delete from user_fast where id = NEW.creator_id;
|
||||
insert into user_fast select * from user_view where id = NEW.creator_id on conflict (id) do nothing;
|
||||
|
||||
-- Update community number of posts
|
||||
update community_aggregates_fast set number_of_posts = number_of_posts + 1 where id = NEW.community_id;
|
||||
|
||||
-- Update the hot rank on the post table
|
||||
-- TODO this might not correctly update it, using a 1 week interval
|
||||
update post_aggregates_fast as paf
|
||||
set hot_rank = pav.hot_rank
|
||||
from post_aggregates_view as pav
|
||||
where paf.id = pav.id and (pav.published > ('now'::timestamp - '1 week'::interval));
|
||||
END IF;
|
||||
|
||||
return null;
|
||||
end $$;
|
||||
|
||||
|
||||
create or replace function refresh_comment()
|
||||
returns trigger language plpgsql
|
||||
as $$
|
||||
begin
|
||||
IF (TG_OP = 'DELETE') THEN
|
||||
delete from comment_aggregates_fast where id = OLD.id;
|
||||
|
||||
-- Update community number of comments
|
||||
update community_aggregates_fast as caf
|
||||
set number_of_comments = number_of_comments - 1
|
||||
from post as p
|
||||
where caf.id = p.community_id and p.id = OLD.post_id;
|
||||
|
||||
ELSIF (TG_OP = 'UPDATE') THEN
|
||||
delete from comment_aggregates_fast where id = OLD.id;
|
||||
insert into comment_aggregates_fast select * from comment_aggregates_view where id = NEW.id on conflict (id) do nothing;
|
||||
ELSIF (TG_OP = 'INSERT') THEN
|
||||
insert into comment_aggregates_fast select * from comment_aggregates_view where id = NEW.id;
|
||||
|
||||
-- Update user view due to comment count
|
||||
update user_fast
|
||||
set number_of_comments = number_of_comments + 1
|
||||
where id = NEW.creator_id;
|
||||
|
||||
-- Update post view due to comment count, new comment activity time, but only on new posts
|
||||
-- TODO this could be done more efficiently
|
||||
delete from post_aggregates_fast where id = NEW.post_id;
|
||||
insert into post_aggregates_fast select * from post_aggregates_view where id = NEW.post_id on conflict (id) do nothing;
|
||||
|
||||
-- Force the hot rank as zero on week-older posts
|
||||
update post_aggregates_fast as paf
|
||||
set hot_rank = 0
|
||||
where paf.id = NEW.post_id and (paf.published < ('now'::timestamp - '1 week'::interval));
|
||||
|
||||
-- Update community number of comments
|
||||
update community_aggregates_fast as caf
|
||||
set number_of_comments = number_of_comments + 1
|
||||
from post as p
|
||||
where caf.id = p.community_id and p.id = NEW.post_id;
|
||||
|
||||
END IF;
|
||||
|
||||
return null;
|
||||
end $$;
|
Loading…
Reference in New Issue