Skip to content

Commit

Permalink
Fix follows on Mastodon API (#403)
Browse files Browse the repository at this point in the history
* Fix follows on Mastodon API

* only fetch the preferences if the account getting followed is local
  • Loading branch information
aumetra authored Oct 29, 2023
1 parent f34ec9d commit 2fd9880
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 28 deletions.
61 changes: 35 additions & 26 deletions crates/kitsune-core/src/service/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ impl AccountService {
///
/// Tuple of two account models. First model is the account the followee account, the second model is the followed account
pub async fn follow(&self, follow: Follow, notify: bool) -> Result<(Account, Account)> {
let (account, preferences, follower) = self
let (account, follower) = self
.db_pool
.with_connection(|db_conn| {
async move {
Expand All @@ -208,17 +208,12 @@ impl AccountService {
.select(Account::as_select())
.get_result(db_conn);

let preferences = accounts_preferences::table
.find(follow.account_id)
.select(Preferences::as_select())
.get_result(db_conn);

let follower_fut = accounts::table
.find(follow.follower_id)
.select(Account::as_select())
.get_result(db_conn);

try_join!(account_fut, preferences, follower_fut)
try_join!(account_fut, follower_fut)
}
.scoped()
})
Expand Down Expand Up @@ -251,29 +246,43 @@ impl AccountService {
})
.await?;

if account.local
&& ((preferences.notify_on_follow && !account.locked)
|| (preferences.notify_on_follow_request && account.locked))
{
let notification = if account.locked {
NewNotification::builder()
.receiving_account_id(account.id)
.follow_request(follower.id)
} else {
NewNotification::builder()
.receiving_account_id(account.id)
.follow(follower.id)
};
self.db_pool
.with_connection(|mut db_conn| {
diesel::insert_into(notifications::table)
.values(notification)
.on_conflict_do_nothing()
.execute(&mut db_conn)
if account.local {
let preferences = self
.db_pool
.with_connection(|db_conn| {
accounts_preferences::table
.find(follow.account_id)
.select(Preferences::as_select())
.get_result(db_conn)
.scoped()
})
.await?;

if (preferences.notify_on_follow && !account.locked)
|| (preferences.notify_on_follow_request && account.locked)
{
let notification = if account.locked {
NewNotification::builder()
.receiving_account_id(account.id)
.follow_request(follower.id)
} else {
NewNotification::builder()
.receiving_account_id(account.id)
.follow(follower.id)
};

self.db_pool
.with_connection(|mut db_conn| {
diesel::insert_into(notifications::table)
.values(notification)
.on_conflict_do_nothing()
.execute(&mut db_conn)
.scoped()
})
.await?;
}
}

if !account.local {
self.job_service
.enqueue(Enqueue::builder().job(DeliverFollow { follow_id }).build())
Expand Down
6 changes: 4 additions & 2 deletions kitsune/src/http/handler/mastodon/api/v1/accounts/follow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub async fn post(
State(mastodon_mapper): State<MastodonMapper>,
AuthExtractor(user_data): MastodonAuthExtractor,
Path(id): Path<Uuid>,
FormOrJson(follow_body): FormOrJson<FollowBody>,
follow_body: Option<FormOrJson<FollowBody>>,
) -> Result<Json<Relationship>> {
if user_data.account.id == id {
return Err(ApiError::BadRequest.into());
Expand All @@ -50,7 +50,9 @@ pub async fn post(
.account_id(id)
.follower_id(user_data.account.id)
.build();
let follow_accounts = account_service.follow(follow, follow_body.notify).await?;
let follow_accounts = account_service
.follow(follow, follow_body.map_or(false, |body| body.0.notify))
.await?;

Ok(Json(
mastodon_mapper
Expand Down

0 comments on commit 2fd9880

Please sign in to comment.