mirror of https://github.com/LemmyNet/lemmy
* Read community follower count from home instance (fixes #1440) * fmt * prettier * fix tests * fmt * rename fn * fmt * Run prettier * increase timeout * ci --------- Co-authored-by: Dessalines <dessalines@users.noreply.github.com> Co-authored-by: Dessalines <tyhou13@gmx.com>pull/4040/head^2
parent
332e698336
commit
56322c75f0
@ -0,0 +1,66 @@
|
||||
use crate::{
|
||||
objects::community::ApubCommunity,
|
||||
protocol::collections::group_followers::GroupFollowers,
|
||||
};
|
||||
use activitypub_federation::{
|
||||
config::Data,
|
||||
kinds::collection::CollectionType,
|
||||
protocol::verification::verify_domains_match,
|
||||
traits::Collection,
|
||||
};
|
||||
use lemmy_api_common::{context::LemmyContext, utils::generate_followers_url};
|
||||
use lemmy_db_schema::aggregates::structs::CommunityAggregates;
|
||||
use lemmy_db_views_actor::structs::CommunityFollowerView;
|
||||
use lemmy_utils::error::LemmyError;
|
||||
use url::Url;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct ApubCommunityFollower(Vec<()>);
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Collection for ApubCommunityFollower {
|
||||
type Owner = ApubCommunity;
|
||||
type DataType = LemmyContext;
|
||||
type Kind = GroupFollowers;
|
||||
type Error = LemmyError;
|
||||
|
||||
async fn read_local(
|
||||
community: &Self::Owner,
|
||||
context: &Data<Self::DataType>,
|
||||
) -> Result<Self::Kind, Self::Error> {
|
||||
let community_id = community.id;
|
||||
let community_followers =
|
||||
CommunityFollowerView::count_community_followers(&mut context.pool(), community_id).await?;
|
||||
|
||||
Ok(GroupFollowers {
|
||||
id: generate_followers_url(&community.actor_id)?.into(),
|
||||
r#type: CollectionType::Collection,
|
||||
total_items: community_followers as i32,
|
||||
items: vec![],
|
||||
})
|
||||
}
|
||||
|
||||
async fn verify(
|
||||
json: &Self::Kind,
|
||||
expected_domain: &Url,
|
||||
_data: &Data<Self::DataType>,
|
||||
) -> Result<(), Self::Error> {
|
||||
verify_domains_match(expected_domain, &json.id)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn from_json(
|
||||
json: Self::Kind,
|
||||
community: &Self::Owner,
|
||||
context: &Data<Self::DataType>,
|
||||
) -> Result<Self, Self::Error> {
|
||||
CommunityAggregates::update_federated_followers(
|
||||
&mut context.pool(),
|
||||
community.id,
|
||||
json.total_items,
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(ApubCommunityFollower(Vec::new()))
|
||||
}
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
pub(crate) mod community_featured;
|
||||
pub(crate) mod community_follower;
|
||||
pub(crate) mod community_moderators;
|
||||
pub(crate) mod community_outbox;
|
||||
|
@ -1,34 +1,12 @@
|
||||
use activitypub_federation::kinds::collection::CollectionType;
|
||||
use lemmy_api_common::{context::LemmyContext, utils::generate_followers_url};
|
||||
use lemmy_db_schema::source::community::Community;
|
||||
use lemmy_db_views_actor::structs::CommunityFollowerView;
|
||||
use lemmy_utils::error::LemmyError;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use url::Url;
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub(crate) struct GroupFollowers {
|
||||
id: Url,
|
||||
r#type: CollectionType,
|
||||
total_items: i32,
|
||||
items: Vec<()>,
|
||||
}
|
||||
|
||||
impl GroupFollowers {
|
||||
pub(crate) async fn new(
|
||||
community: Community,
|
||||
context: &LemmyContext,
|
||||
) -> Result<GroupFollowers, LemmyError> {
|
||||
let community_id = community.id;
|
||||
let community_followers =
|
||||
CommunityFollowerView::count_community_followers(&mut context.pool(), community_id).await?;
|
||||
|
||||
Ok(GroupFollowers {
|
||||
id: generate_followers_url(&community.actor_id)?.into(),
|
||||
r#type: CollectionType::Collection,
|
||||
total_items: community_followers as i32,
|
||||
items: vec![],
|
||||
})
|
||||
}
|
||||
pub(crate) id: Url,
|
||||
pub(crate) r#type: CollectionType,
|
||||
pub(crate) total_items: i32,
|
||||
pub(crate) items: Vec<()>,
|
||||
}
|
||||
|
@ -0,0 +1,24 @@
|
||||
CREATE OR REPLACE FUNCTION community_aggregates_subscriber_count ()
|
||||
RETURNS TRIGGER
|
||||
LANGUAGE plpgsql
|
||||
AS $$
|
||||
BEGIN
|
||||
IF (TG_OP = 'INSERT') THEN
|
||||
UPDATE
|
||||
community_aggregates
|
||||
SET
|
||||
subscribers = subscribers + 1
|
||||
WHERE
|
||||
community_id = NEW.community_id;
|
||||
ELSIF (TG_OP = 'DELETE') THEN
|
||||
UPDATE
|
||||
community_aggregates
|
||||
SET
|
||||
subscribers = subscribers - 1
|
||||
WHERE
|
||||
community_id = OLD.community_id;
|
||||
END IF;
|
||||
RETURN NULL;
|
||||
END
|
||||
$$;
|
||||
|
@ -0,0 +1,34 @@
|
||||
-- The subscriber count should only be updated for local communities. For remote
|
||||
-- communities it is read over federation from the origin instance.
|
||||
CREATE OR REPLACE FUNCTION community_aggregates_subscriber_count ()
|
||||
RETURNS TRIGGER
|
||||
LANGUAGE plpgsql
|
||||
AS $$
|
||||
BEGIN
|
||||
IF (TG_OP = 'INSERT') THEN
|
||||
UPDATE
|
||||
community_aggregates
|
||||
SET
|
||||
subscribers = subscribers + 1
|
||||
FROM
|
||||
community
|
||||
WHERE
|
||||
community.id = community_id
|
||||
AND community.local
|
||||
AND community_id = NEW.community_id;
|
||||
ELSIF (TG_OP = 'DELETE') THEN
|
||||
UPDATE
|
||||
community_aggregates
|
||||
SET
|
||||
subscribers = subscribers - 1
|
||||
FROM
|
||||
community
|
||||
WHERE
|
||||
community.id = community_id
|
||||
AND community.local
|
||||
AND community_id = OLD.community_id;
|
||||
END IF;
|
||||
RETURN NULL;
|
||||
END
|
||||
$$;
|
||||
|
Loading…
Reference in New Issue