Skip to content

Commit

Permalink
Ublog: fix lichess rank computation and username in timeline
Browse files Browse the repository at this point in the history
Previously the username of the person who published the post was used in the propagation to all lichess followers, instead of `lichess`.

Similarly, the rank of the post was using the user blog tier, not lichess
  • Loading branch information
kraktus committed Aug 2, 2024
1 parent a7f54ee commit 6c5b867
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
12 changes: 10 additions & 2 deletions app/controllers/Ublog.scala
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,19 @@ final class Ublog(env: Env) extends LilaController(env):

def update(id: UblogPostId) = AuthBody { ctx ?=> me ?=>
NotForKids:
Found(env.ublog.api.findEditableByMe(id)): prev =>
Found(
env.ublog.api
.findEditableByMe(id)
.flatMapz: prev =>
env.user.repo
.byId(prev.created.by)
.map: userOpt =>
userOpt.map(user => (user, prev))
): (author, prev) =>
bindForm(env.ublog.form.edit(prev))(
err => BadRequest.page(views.ublog.form.edit(prev, err)),
data =>
env.ublog.api.update(data, prev).flatMap { post =>
env.ublog.api.update(author, data, prev).flatMap { post =>
logModAction(post, "edit").inject(Redirect(urlOfPost(post)).flashSuccess)
}
)
Expand Down
18 changes: 10 additions & 8 deletions modules/ublog/src/main/UblogApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,28 @@ final class UblogApi(

def getByPrismicId(id: String): Fu[Option[UblogPost]] = colls.post.one[UblogPost]($doc("prismicId" -> id))

def update(data: UblogForm.UblogPostData, prev: UblogPost)(using me: Me): Fu[UblogPost] =
getUserBlog(me.value, insertMissing = true).flatMap { blog =>
def update(author: User, data: UblogForm.UblogPostData, prev: UblogPost)(using me: Me): Fu[UblogPost] =
getUserBlog(author, insertMissing = true).flatMap { blog =>
val post = data.update(me.value, prev)
(colls.post.update.one($id(prev.id), $set(bsonWriteObjTry[UblogPost](post).get)) >> {
(post.live && prev.lived.isEmpty).so(onFirstPublish(me.value, blog, post))
(post.live && prev.lived.isEmpty).so(onFirstPublish(author, blog, post))
}).inject(post)
}

private def onFirstPublish(user: User, blog: UblogBlog, post: UblogPost): Funit =
private def onFirstPublish(author: User, blog: UblogBlog, post: UblogPost): Funit =
rank
.computeRank(blog, post)
.computeRank(blog.pp("blog"), post)
.pp("rank")
.so: rank =>
colls.post.updateField($id(post.id), "rank", rank).void
.andDo:
lila.common.Bus.publish(UblogPost.Create(post), "ublogPost")
if blog.visible then
lila.common.Bus.pub:
tl.Propagate(tl.UblogPost(user.id, post.id, post.slug, post.title)).toFollowersOf(post.created.by)
shutupApi.publicText(user.id, post.allText, PublicSource.Ublog(post.id))
if blog.modTier.isEmpty then sendPostToZulipMaybe(user, post)
tl.Propagate(tl.UblogPost(author.id, post.id, post.slug, post.title))
.toFollowersOf(post.created.by)
shutupApi.publicText(author.id, post.allText, PublicSource.Ublog(post.id))
if blog.modTier.isEmpty then sendPostToZulipMaybe(author, post)

def getUserBlog(user: User, insertMissing: Boolean = false): Fu[UblogBlog] =
getBlog(UblogBlog.Id.User(user.id)).getOrElse(
Expand Down

0 comments on commit 6c5b867

Please sign in to comment.