Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix follows on Mastodon API #403

Merged
merged 2 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading