// The left join below will return None in this case
let person_id_join = my_person_id.unwrap_or(PersonId(-1));
- let mut query = post::table
- .find(post_id)
+ let mut query = post_aggregates::table
+ .filter(post_aggregates::post_id.eq(post_id))
.inner_join(person::table)
.inner_join(community::table)
.left_join(
community_person_ban::table.on(
- post::community_id
+ post_aggregates::community_id
.eq(community_person_ban::community_id)
- .and(community_person_ban::person_id.eq(post::creator_id)),
+ .and(community_person_ban::person_id.eq(post_aggregates::creator_id)),
),
)
- .inner_join(post_aggregates::table)
+ .inner_join(post::table)
.left_join(
community_follower::table.on(
- post::community_id
+ post_aggregates::community_id
.eq(community_follower::community_id)
.and(community_follower::person_id.eq(person_id_join)),
),
)
.left_join(
post_saved::table.on(
- post::id
+ post_aggregates::post_id
.eq(post_saved::post_id)
.and(post_saved::person_id.eq(person_id_join)),
),
)
.left_join(
post_read::table.on(
- post::id
+ post_aggregates::post_id
.eq(post_read::post_id)
.and(post_read::person_id.eq(person_id_join)),
),
)
.left_join(
person_block::table.on(
- post::creator_id
+ post_aggregates::creator_id
.eq(person_block::target_id)
.and(person_block::person_id.eq(person_id_join)),
),
)
.left_join(
post_like::table.on(
- post::id
+ post_aggregates::post_id
.eq(post_like::post_id)
.and(post_like::person_id.eq(person_id_join)),
),
)
.left_join(
person_post_aggregates::table.on(
- post::id
+ post_aggregates::post_id
.eq(person_post_aggregates::post_id)
.and(person_post_aggregates::person_id.eq(person_id_join)),
),
.map(|l| l.local_user.id)
.unwrap_or(LocalUserId(-1));
- let mut query = post::table
+ let mut query = post_aggregates::table
.inner_join(person::table)
+ .inner_join(post::table)
.inner_join(community::table)
.left_join(
community_person_ban::table.on(
- post::community_id
+ post_aggregates::community_id
.eq(community_person_ban::community_id)
- .and(community_person_ban::person_id.eq(post::creator_id)),
+ .and(community_person_ban::person_id.eq(post_aggregates::creator_id)),
),
)
- .inner_join(post_aggregates::table)
.left_join(
community_follower::table.on(
- post::community_id
+ post_aggregates::community_id
.eq(community_follower::community_id)
.and(community_follower::person_id.eq(person_id_join)),
),
)
.left_join(
post_saved::table.on(
- post::id
+ post_aggregates::post_id
.eq(post_saved::post_id)
.and(post_saved::person_id.eq(person_id_join)),
),
)
.left_join(
post_read::table.on(
- post::id
+ post_aggregates::post_id
.eq(post_read::post_id)
.and(post_read::person_id.eq(person_id_join)),
),
)
.left_join(
person_block::table.on(
- post::creator_id
+ post_aggregates::creator_id
.eq(person_block::target_id)
.and(person_block::person_id.eq(person_id_join)),
),
)
.left_join(
community_block::table.on(
- post::community_id
+ post_aggregates::community_id
.eq(community_block::community_id)
.and(community_block::person_id.eq(person_id_join)),
),
)
.left_join(
post_like::table.on(
- post::id
+ post_aggregates::post_id
.eq(post_like::post_id)
.and(post_like::person_id.eq(person_id_join)),
),
)
.left_join(
person_post_aggregates::table.on(
- post::id
+ post_aggregates::post_id
.eq(person_post_aggregates::post_id)
.and(person_post_aggregates::person_id.eq(person_id_join)),
),
query = query.then_order_by(post_aggregates::featured_local.desc());
} else if let Some(community_id) = self.community_id {
query = query
- .filter(post::community_id.eq(community_id))
+ .filter(post_aggregates::community_id.eq(community_id))
.then_order_by(post_aggregates::featured_community.desc());
}
if let Some(creator_id) = self.creator_id {
- query = query.filter(post::creator_id.eq(creator_id));
+ query = query.filter(post_aggregates::creator_id.eq(creator_id));
}
if let Some(listing_type) = self.listing_type {
featured_local: false,
hot_rank: 1728,
hot_rank_active: 1728,
+ community_id: inserted_post.community_id,
+ creator_id: inserted_post.creator_id,
},
subscribed: SubscribedType::NotSubscribed,
read: false,
--- /dev/null
+-- Your SQL goes here
+ALTER TABLE post_aggregates
+ ADD COLUMN community_id integer REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE,
+ ADD COLUMN creator_id integer REFERENCES person ON UPDATE CASCADE ON DELETE CASCADE;
+
+CREATE OR REPLACE FUNCTION post_aggregates_post()
+ RETURNS trigger
+ LANGUAGE plpgsql
+AS
+$$
+BEGIN
+ IF (TG_OP = 'INSERT') THEN
+ INSERT INTO post_aggregates (post_id,
+ published,
+ newest_comment_time,
+ newest_comment_time_necro,
+ community_id,
+ creator_id)
+ VALUES (NEW.id, NEW.published, NEW.published, NEW.published, NEW.community_id, NEW.creator_id);
+ ELSIF (TG_OP = 'DELETE') THEN
+ DELETE FROM post_aggregates WHERE post_id = OLD.id;
+ END IF;
+ RETURN NULL;
+END
+$$;
+
+UPDATE post_aggregates
+SET community_id=post.community_id,
+ creator_id=post.creator_id
+FROM post
+WHERE post.id = post_aggregates.post_id;
+
+ALTER TABLE post_aggregates
+ ALTER COLUMN community_id SET NOT NULL,
+ ALTER COLUMN creator_id SET NOT NULL;
\ No newline at end of file