Skip to content

Commit e9a2221

Browse files
committed
If user A views the mini profile of user B, disable the message button if user B's preferences prevent chatting.
1 parent 5b9687b commit e9a2221

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

app/controllers/User.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,16 @@ final class User(
179179
ctx.userId.so(relationApi.fetchBlocks(user.id, _)),
180180
ctx.userId.traverse(env.game.crosstableApi(user.id, _)),
181181
ctx.isAuth.so(env.pref.api.followable(user.id)),
182-
ctx.useMe(env.clas.api.clas.realName(user.id))
183-
).flatMapN: (blocked, crosstable, followable, realName) =>
182+
ctx.useMe(env.clas.api.clas.realName(user.id)),
183+
ctx.me.so(me => env.msg.api.canMessage(user.id)(using me))
184+
).flatMapN: (blocked, crosstable, followable, realName, canMessage) =>
184185
negotiate(
185186
html = for
186187
pov <- ctx.isnt(user).so(env.round.currentlyPlaying.exec(user.user.id))
187188
ping = env.socket.isOnline.exec(user.id).so(env.socket.getLagRating(user.id))
188189
snip <- Ok.snip:
189-
views.user.mini(user, pov, blocked, followable, relation, ping, crosstable, realName)
190+
views.user
191+
.mini(user, pov, blocked, followable, relation, ping, crosstable, realName, canMessage)
190192
yield snip.headerCacheSeconds(5),
191193
json =
192194
import lila.game.JsonView.given

app/views/user/ui.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ def mini(
1919
relation: Option[lila.relation.Relation],
2020
ping: Option[Int],
2121
ct: Option[lila.game.Crosstable],
22-
realName: Option[String]
22+
realName: Option[String],
23+
canMessage: Boolean
2324
)(using ctx: Context) =
2425
val rel = views.relation.mini(u.id, blocked, followable, relation)
2526
def crosstable(myId: UserId) = ct
@@ -38,7 +39,7 @@ def mini(
3839
val flag = u.profileOrDefault.flagInfo
3940
val perfs = u.perfs.best8Perfs
4041
val nameFrag = realName.map(frag(_))
41-
show.ui.mini(u, playing, blocked, ping, rel, crosstable, flag, nameFrag, perfs, userMarks)
42+
show.ui.mini(u, playing, blocked, ping, rel, crosstable, flag, nameFrag, perfs, userMarks, canMessage)
4243

4344
val perfStat = lila.perfStat.PerfStatUi(helpers)(views.user.bits.communityMenu("ratings"))
4445
def perfStatPage(data: PerfStatData, ratingChart: Option[SafeJsonStr])(using Context) =

modules/msg/src/main/MsgApi.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ final class MsgApi(
9696
_ <- setReadBy(threadId, me, userId)
9797
msgs <- threadMsgsFor(threadId, me, before)
9898
relations <- relationApi.fetchRelations(me, userId)
99-
postable <- security.may.post(me, userId, isNew = msgs.headOption.isEmpty)
99+
postable <- security.may.post(me, userId, msgs.headOption.isEmpty)
100100
details <- Granter(_.PublicMod).optionFu(fetchContactDetailsForMods(userId))
101101
yield MsgConvo(contact, msgs, relations, postable, details).some
102102
}
@@ -396,3 +396,12 @@ final class MsgApi(
396396
// filter conversation where only team messages where sent
397397
msgs <- doc.getAsOpt[NonEmptyList[Msg]]("msgs")
398398
yield (tid, msgs)).toList
399+
400+
def canMessage(dest: UserId)(using me: Me): Fu[Boolean] =
401+
if dest.is(me) then fuFalse
402+
else
403+
for
404+
hasMsgs <- threadMsgsFor(MsgThread.id(me, dest), me, none).map(_.nonEmpty)
405+
canPost <- security.may.post(me, dest, !hasMsgs)
406+
yield
407+
canPost

modules/user/src/main/ui/UserShow.scala

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ final class UserShow(helpers: Helpers, bits: UserBits):
2020
flag: Option[Flag],
2121
realName: Option[Frag],
2222
best8Perfs: List[PerfKey],
23-
userMarks: => Frag
23+
userMarks: => Frag,
24+
canMessage: Boolean
2425
)(using ctx: Context) =
2526
frag(
2627
div(cls := "upt__info")(
@@ -58,12 +59,22 @@ final class UserShow(helpers: Helpers, bits: UserBits):
5859
),
5960
(!blocked).option(
6061
frag(
61-
a(
62-
dataIcon := Icon.BubbleSpeech,
63-
cls := "btn-rack__btn",
64-
title := trans.site.chat.txt(),
65-
href := routes.Msg.convo(u.username)
66-
),
62+
if canMessage then
63+
a(
64+
dataIcon := Icon.BubbleSpeech,
65+
cls := "btn-rack__btn",
66+
title := trans.site.chat.txt(),
67+
href := routes.Msg.convo(u.username)
68+
)
69+
else
70+
span(
71+
dataIcon := Icon.BubbleSpeech,
72+
cls := "btn-rack__btn",
73+
title := s"${u.username} doesn't accept new messages.",
74+
attr("disabled") := "disabled",
75+
aria.disabled := "true"
76+
)
77+
,
6778
a(
6879
dataIcon := Icon.Swords,
6980
cls := "btn-rack__btn",

0 commit comments

Comments
 (0)