Skip to content

Commit

Permalink
automatically unfollow closed and inactive accounts - closes #16379
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Nov 11, 2024
1 parent 2ed018e commit 4205c2e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
7 changes: 5 additions & 2 deletions app/controllers/Relation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,16 @@ final class Relation(env: Env, apiC: => Api) extends LilaController(env):
def following(username: UserStr, page: Int) = Open:
Reasonable(page, Max(20)):
Found(meOrFetch(username)): user =>
RelatedPager(api.followingPaginatorAdapter(user.id), page).flatMap: pag =>
negotiate(
for
_ <- (page == 1).so(api.unfollowInactiveAccounts(user.id))
pag <- RelatedPager(api.followingPaginatorAdapter(user.id), page)
res <- negotiate(
if ctx.is(user) || isGrantedOpt(_.CloseAccount)
then Ok.page(views.relation.friends(user, pag))
else Found(ctx.me)(me => Redirect(routes.Relation.following(me.username))),
Ok(jsonRelatedPaginator(pag))
)
yield res

def followers(username: UserStr, page: Int) = Open:
negotiateJson:
Expand Down
31 changes: 16 additions & 15 deletions modules/relation/src/main/RelationApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ final class RelationApi(

def countFollowing(userId: UserId) = countFollowingCache.get(userId)

private val unfollowInactiveAccountsOnceEvery = scalalib.cache.OnceEvery[UserId](1.hour)

def unfollowInactiveAccounts(userId: UserId, since: Instant = nowInstant.minusYears(2)): Fu[List[UserId]] =
unfollowInactiveAccountsOnceEvery(userId).so:
for
following <- fetchFollowing(userId)
inactive <- userApi.filterClosedOrInactiveIds(since)(following)
_ <- inactive.nonEmpty.so:
countFollowingCache.update(userId, _ - inactive.size)
repo.unfollowMany(userId, inactive)
yield inactive

def reachedMaxFollowing(userId: UserId): Fu[Boolean] =
countFollowingCache.get(userId).map(config.maxFollow <= _)

Expand Down Expand Up @@ -128,23 +140,12 @@ final class RelationApi(
}
})

private val limitFollowRateLimiter = lila.memo.RateLimit[UserId](
credits = 1,
duration = 1 hour,
key = "follow.limit.cleanup"
)

private def limitFollow(u: UserId) =
countFollowing(u).flatMap: nb =>
(config.maxFollow < nb).so {
limitFollowRateLimiter(u, fuccess(Nil)):
fetchFollowing(u).flatMap(userApi.filterClosedOrInactiveIds(nowInstant.minusDays(90)))
.flatMap:
case Nil => repo.drop(u, Follow, nb - config.maxFollow.value)
case inactiveIds =>
for _ <- repo.unfollowMany(u, inactiveIds)
yield countFollowingCache.update(u, _ - inactiveIds.size)
}
(config.maxFollow < nb).so:
unfollowInactiveAccounts(u, nowInstant.minusDays(90)).flatMap:
_.isEmpty.so:
repo.drop(u, Follow, nb - config.maxFollow.value)

private def limitBlock(u: UserId) =
countBlocking(u).flatMap: nb =>
Expand Down

0 comments on commit 4205c2e

Please sign in to comment.