Skip to content

Commit f0fcae3

Browse files
committed
Use either instead of manually implemented enums (fixes #5513)
1 parent 7711562 commit f0fcae3

32 files changed

+153
-544
lines changed

Diff for: Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: crates/apub/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ itertools = { workspace = true }
3737
uuid = { workspace = true }
3838
async-trait = "0.1.86"
3939
anyhow = { workspace = true }
40-
reqwest = { workspace = true }
4140
moka.workspace = true
4241
serde_with.workspace = true
4342
html2md = "0.2.15"
4443
html2text = "0.14.0"
4544
stringreader = "0.1.1"
4645
enum_delegate = "0.2.0"
4746
semver = "1.0.25"
47+
either = "1.15.0"
4848

4949
[dev-dependencies]
5050
serial_test = { workspace = true }

Diff for: crates/apub/src/activities/community/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
activities::send_lemmy_activity,
33
activity_lists::AnnouncableActivities,
4-
fetcher::post_or_comment::PostOrComment,
4+
fetcher::PostOrComment,
55
objects::{community::ApubCommunity, instance::ApubSite, person::ApubPerson},
66
protocol::activities::community::announce::AnnounceActivity,
77
};
@@ -96,8 +96,8 @@ async fn report_inboxes(
9696

9797
// also send report to user's home instance if possible
9898
let object_creator_id = match object_id.dereference_local(context).await? {
99-
PostOrComment::Post(p) => p.creator_id,
100-
PostOrComment::Comment(c) => c.creator_id,
99+
PostOrComment::Left(p) => p.creator_id,
100+
PostOrComment::Right(c) => c.creator_id,
101101
};
102102
let object_creator = Person::read(&mut context.pool(), object_creator_id).await?;
103103
let object_creator_site: Option<ApubSite> =

Diff for: crates/apub/src/activities/community/report.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl ActivityHandler for Report {
9595
let actor = self.actor.dereference(context).await?;
9696
let reason = self.reason()?;
9797
match self.object.dereference(context).await? {
98-
PostOrComment::Post(post) => {
98+
PostOrComment::Left(post) => {
9999
check_post_deleted_or_removed(&post)?;
100100

101101
let report_form = PostReportForm {
@@ -109,7 +109,7 @@ impl ActivityHandler for Report {
109109
};
110110
PostReport::report(&mut context.pool(), &report_form).await?;
111111
}
112-
PostOrComment::Comment(comment) => {
112+
PostOrComment::Right(comment) => {
113113
check_comment_deleted_or_removed(&comment)?;
114114

115115
let report_form = CommentReportForm {

Diff for: crates/apub/src/activities/community/resolve_report.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ impl ActivityHandler for ResolveReport {
8787
let reporter = self.object.actor.dereference(context).await?;
8888
let actor = self.actor.dereference(context).await?;
8989
match self.object.object.dereference(context).await? {
90-
PostOrComment::Post(post) => {
90+
PostOrComment::Left(post) => {
9191
PostReport::resolve_apub(&mut context.pool(), post.id, reporter.id, actor.id).await?;
9292
}
93-
PostOrComment::Comment(comment) => {
93+
PostOrComment::Right(comment) => {
9494
CommentReport::resolve_apub(&mut context.pool(), comment.id, reporter.id, actor.id).await?;
9595
}
9696
};

Diff for: crates/apub/src/activities/following/accept.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
use super::send_activity_from_user_or_community;
12
use crate::{
2-
activities::{generate_activity_id, send_lemmy_activity},
3+
activities::generate_activity_id,
34
insert_received_activity,
45
protocol::activities::following::{accept::AcceptFollow, follow::Follow},
56
};
@@ -32,7 +33,7 @@ impl AcceptFollow {
3233
)?,
3334
};
3435
let inbox = ActivitySendTargets::to_inbox(person.shared_inbox_or_inbox());
35-
send_lemmy_activity(context, accept, &user_or_community, inbox, true).await
36+
send_activity_from_user_or_community(context, accept, user_or_community, inbox).await
3637
}
3738
}
3839

Diff for: crates/apub/src/activities/following/follow.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
verify_person,
66
verify_person_in_community,
77
},
8-
fetcher::user_or_community::UserOrCommunity,
8+
fetcher::UserOrCommunity,
99
insert_received_activity,
1010
objects::{community::ApubCommunity, person::ApubPerson},
1111
protocol::activities::following::{accept::AcceptFollow, follow::Follow},
@@ -79,7 +79,7 @@ impl ActivityHandler for Follow {
7979
async fn verify(&self, context: &Data<LemmyContext>) -> LemmyResult<()> {
8080
verify_person(&self.actor, context).await?;
8181
let object = self.object.dereference(context).await?;
82-
if let UserOrCommunity::Community(c) = object {
82+
if let UserOrCommunity::Right(c) = object {
8383
verify_person_in_community(&self.actor, &c, context).await?;
8484
}
8585
if let Some(to) = &self.to {
@@ -94,12 +94,12 @@ impl ActivityHandler for Follow {
9494
let actor = self.actor.dereference(context).await?;
9595
let object = self.object.dereference(context).await?;
9696
match object {
97-
UserOrCommunity::User(u) => {
97+
UserOrCommunity::Left(u) => {
9898
let form = PersonFollowerForm::new(u.id, actor.id, false);
9999
PersonActions::follow(&mut context.pool(), &form).await?;
100100
AcceptFollow::send(self, context).await?;
101101
}
102-
UserOrCommunity::Community(c) => {
102+
UserOrCommunity::Right(c) => {
103103
if c.visibility == CommunityVisibility::Private {
104104
let instance = Instance::read(&mut context.pool(), actor.instance_id).await?;
105105
if [Some("kbin"), Some("mbin")].contains(&instance.software.as_deref()) {

Diff for: crates/apub/src/activities/following/mod.rs

+26-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use super::generate_activity_id;
1+
use super::{generate_activity_id, send_lemmy_activity};
22
use crate::{
3+
fetcher::UserOrCommunity,
34
objects::{community::ApubCommunity, person::ApubPerson},
45
protocol::activities::following::{
56
accept::AcceptFollow,
@@ -8,14 +9,15 @@ use crate::{
89
undo_follow::UndoFollow,
910
},
1011
};
11-
use activitypub_federation::{config::Data, kinds::activity::FollowType};
12+
use activitypub_federation::{config::Data, kinds::activity::FollowType, traits::ActivityHandler};
1213
use lemmy_api_common::context::LemmyContext;
1314
use lemmy_db_schema::{
1415
newtypes::{CommunityId, PersonId},
15-
source::{community::Community, person::Person},
16+
source::{activity::ActivitySendTargets, community::Community, person::Person},
1617
traits::Crud,
1718
};
18-
use lemmy_utils::error::LemmyResult;
19+
use lemmy_utils::error::{LemmyError, LemmyResult};
20+
use serde::Serialize;
1921

2022
pub(crate) mod accept;
2123
pub(crate) mod follow;
@@ -62,3 +64,23 @@ pub async fn send_accept_or_reject_follow(
6264
RejectFollow::send(follow, context).await
6365
}
6466
}
67+
68+
/// Wrapper type which is needed because we cant implement ActorT for Either.
69+
async fn send_activity_from_user_or_community<Activity>(
70+
context: &Data<LemmyContext>,
71+
activity: Activity,
72+
user_or_community: UserOrCommunity,
73+
send_targets: ActivitySendTargets,
74+
) -> LemmyResult<()>
75+
where
76+
Activity: ActivityHandler + Serialize + Send + Sync + Clone + ActivityHandler<Error = LemmyError>,
77+
{
78+
match user_or_community {
79+
UserOrCommunity::Left(user) => {
80+
send_lemmy_activity(context, activity, &user, send_targets, true).await
81+
}
82+
UserOrCommunity::Right(community) => {
83+
send_lemmy_activity(context, activity, &community, send_targets, true).await
84+
}
85+
}
86+
}

Diff for: crates/apub/src/activities/following/reject.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
use super::send_activity_from_user_or_community;
12
use crate::{
2-
activities::{generate_activity_id, send_lemmy_activity},
3+
activities::generate_activity_id,
34
insert_received_activity,
45
protocol::activities::following::{follow::Follow, reject::RejectFollow},
56
};
@@ -32,7 +33,7 @@ impl RejectFollow {
3233
)?,
3334
};
3435
let inbox = ActivitySendTargets::to_inbox(person.shared_inbox_or_inbox());
35-
send_lemmy_activity(context, reject, &user_or_community, inbox, true).await
36+
send_activity_from_user_or_community(context, reject, user_or_community, inbox).await
3637
}
3738
}
3839

Diff for: crates/apub/src/activities/following/undo_follow.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
activities::{generate_activity_id, send_lemmy_activity, verify_person},
3-
fetcher::user_or_community::UserOrCommunity,
3+
fetcher::UserOrCommunity,
44
insert_received_activity,
55
objects::{community::ApubCommunity, person::ApubPerson},
66
protocol::activities::following::{follow::Follow, undo_follow::UndoFollow},
@@ -74,10 +74,10 @@ impl ActivityHandler for UndoFollow {
7474
let object = self.object.object.dereference(context).await?;
7575

7676
match object {
77-
UserOrCommunity::User(u) => {
77+
UserOrCommunity::Left(u) => {
7878
PersonActions::unfollow(&mut context.pool(), person.id, u.id).await?;
7979
}
80-
UserOrCommunity::Community(c) => {
80+
UserOrCommunity::Right(c) => {
8181
CommunityActions::unfollow(&mut context.pool(), person.id, c.id).await?;
8282
}
8383
}

Diff for: crates/apub/src/activities/mod.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,11 @@ pub(crate) fn verify_is_public(to: &[Url], cc: &[Url]) -> LemmyResult<()> {
113113

114114
/// Returns an error if object visibility doesnt match community visibility
115115
/// (ie content in private community must also be private).
116-
pub(crate) fn verify_visibility(to: &[Url], cc: &[Url], community: &Community) -> LemmyResult<()> {
116+
pub(crate) fn verify_visibility(
117+
to: &[Url],
118+
cc: &[Url],
119+
community: &ApubCommunity,
120+
) -> LemmyResult<()> {
117121
use CommunityVisibility::*;
118122
let object_is_public = [to, cc].iter().any(|set| set.contains(&public()));
119123
match community.visibility {
@@ -186,9 +190,8 @@ async fn send_lemmy_activity<Activity, ActorT>(
186190
sensitive: bool,
187191
) -> LemmyResult<()>
188192
where
189-
Activity: ActivityHandler + Serialize + Send + Sync + Clone,
193+
Activity: ActivityHandler + Serialize + Send + Sync + Clone + ActivityHandler<Error = LemmyError>,
190194
ActorT: Actor + GetActorType,
191-
Activity: ActivityHandler<Error = LemmyError>,
192195
{
193196
info!("Saving outgoing activity to queue {}", activity.id());
194197

Diff for: crates/apub/src/activities/voting/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
activities::community::send_activity_in_community,
33
activity_lists::AnnouncableActivities,
4-
fetcher::post_or_comment::PostOrComment,
4+
fetcher::PostOrComment,
55
objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson, post::ApubPost},
66
protocol::activities::voting::{
77
undo_vote::UndoVote,

Diff for: crates/apub/src/activities/voting/undo_vote.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ impl ActivityHandler for UndoVote {
6666
let actor = self.actor.dereference(context).await?;
6767
let object = self.object.object.dereference(context).await?;
6868
match object {
69-
PostOrComment::Post(p) => undo_vote_post(actor, &p, context).await,
70-
PostOrComment::Comment(c) => undo_vote_comment(actor, &c, context).await,
69+
PostOrComment::Left(p) => undo_vote_post(actor, &p, context).await,
70+
PostOrComment::Right(c) => undo_vote_comment(actor, &c, context).await,
7171
}
7272
}
7373
}

Diff for: crates/apub/src/activities/voting/vote.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ impl ActivityHandler for Vote {
7272
.unwrap_or_default();
7373

7474
let (downvote_setting, upvote_setting) = match object {
75-
PostOrComment::Post(_) => (local_site.post_downvotes, local_site.post_upvotes),
76-
PostOrComment::Comment(_) => (local_site.comment_downvotes, local_site.comment_upvotes),
75+
PostOrComment::Left(_) => (local_site.post_downvotes, local_site.post_upvotes),
76+
PostOrComment::Right(_) => (local_site.comment_downvotes, local_site.comment_upvotes),
7777
};
7878

7979
// Don't allow dislikes for either disabled, or local only votes
@@ -83,14 +83,14 @@ impl ActivityHandler for Vote {
8383
if downvote_fail || upvote_fail {
8484
// If this is a rejection, undo the vote
8585
match object {
86-
PostOrComment::Post(p) => undo_vote_post(actor, &p, context).await,
87-
PostOrComment::Comment(c) => undo_vote_comment(actor, &c, context).await,
86+
PostOrComment::Left(p) => undo_vote_post(actor, &p, context).await,
87+
PostOrComment::Right(c) => undo_vote_comment(actor, &c, context).await,
8888
}
8989
} else {
9090
// Otherwise apply the vote normally
9191
match object {
92-
PostOrComment::Post(p) => vote_post(&self.kind, actor, &p, context).await,
93-
PostOrComment::Comment(c) => vote_comment(&self.kind, actor, &c, context).await,
92+
PostOrComment::Left(p) => vote_post(&self.kind, actor, &p, context).await,
93+
PostOrComment::Right(c) => vote_comment(&self.kind, actor, &c, context).await,
9494
}
9595
}
9696
}

Diff for: crates/apub/src/api/resolve_object.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::fetcher::{
2-
post_or_comment::PostOrComment,
32
search::{search_query_to_object_id, search_query_to_object_id_local, SearchableObjects},
4-
user_or_community::UserOrCommunity,
3+
PostOrComment,
4+
UserOrCommunity,
55
};
66
use activitypub_federation::config::Data;
77
use actix_web::web::{Json, Query};
@@ -56,17 +56,17 @@ async fn convert_response(
5656
let is_admin = local_user.clone().map(|l| l.admin).unwrap_or_default();
5757

5858
match object {
59-
SearchableObjects::PostOrComment(pc) => match *pc {
60-
PostOrComment::Post(p) => {
59+
SearchableObjects::Left(pc) => match pc {
60+
PostOrComment::Left(p) => {
6161
res.post = Some(PostView::read(pool, p.id, local_user.as_ref(), is_admin).await?)
6262
}
63-
PostOrComment::Comment(c) => {
63+
PostOrComment::Right(c) => {
6464
res.comment = Some(CommentView::read(pool, c.id, local_user.as_ref()).await?)
6565
}
6666
},
67-
SearchableObjects::PersonOrCommunity(pc) => match *pc {
68-
UserOrCommunity::User(u) => res.person = Some(PersonView::read(pool, u.id, is_admin).await?),
69-
UserOrCommunity::Community(c) => {
67+
SearchableObjects::Right(pc) => match pc {
68+
UserOrCommunity::Left(u) => res.person = Some(PersonView::read(pool, u.id, is_admin).await?),
69+
UserOrCommunity::Right(c) => {
7070
res.community = Some(CommunityView::read(pool, c.id, local_user.as_ref(), is_admin).await?)
7171
}
7272
},

Diff for: crates/apub/src/fetcher/markdown_links.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use super::{search::SearchableObjects, user_or_community::UserOrCommunity};
2-
use crate::fetcher::post_or_comment::PostOrComment;
1+
use super::{search::SearchableObjects, UserOrCommunity};
2+
use crate::fetcher::PostOrComment;
33
use activitypub_federation::{config::Data, fetch::object_id::ObjectId};
44
use lemmy_api_common::context::LemmyContext;
55
use lemmy_db_schema::{newtypes::InstanceId, source::instance::Instance};
@@ -57,17 +57,17 @@ pub(crate) async fn to_local_url(url: &str, context: &Data<LemmyContext>) -> Opt
5757
}
5858
let dereferenced = object_id.dereference_local(context).await.ok()?;
5959
match dereferenced {
60-
SearchableObjects::PostOrComment(pc) => match *pc {
61-
PostOrComment::Post(post) => post.local_url(context.settings()),
62-
PostOrComment::Comment(comment) => comment.local_url(context.settings()),
60+
SearchableObjects::Left(pc) => match pc {
61+
PostOrComment::Left(post) => post.local_url(context.settings()),
62+
PostOrComment::Right(comment) => comment.local_url(context.settings()),
6363
}
6464
.ok()
6565
.map(Into::into),
66-
SearchableObjects::PersonOrCommunity(pc) => match *pc {
67-
UserOrCommunity::User(user) => {
66+
SearchableObjects::Right(pc) => match pc {
67+
UserOrCommunity::Left(user) => {
6868
format_actor_url(&user.name, "u", user.instance_id, context).await
6969
}
70-
UserOrCommunity::Community(community) => {
70+
UserOrCommunity::Right(community) => {
7171
format_actor_url(&community.name, "c", community.instance_id, context).await
7272
}
7373
}

0 commit comments

Comments
 (0)