Skip to content

Commit

Permalink
msg block/unblock
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Jan 26, 2020
1 parent 25e869f commit 858e72e
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 23 deletions.
1 change: 0 additions & 1 deletion app/controllers/Msg.scala
Expand Up @@ -3,7 +3,6 @@ package controllers
import play.api.libs.json._

import lila.app._
import lila.common.LightUser
import lila.common.LightUser.lightUserWrites

final class Msg(
Expand Down
4 changes: 1 addition & 3 deletions app/views/msg.scala
Expand Up @@ -25,9 +25,7 @@ object msg {
),
title = "Lichess Inbox"
) {
main(cls := "box msg-app")(
"loading"
)
main(cls := "box msg-app")
}

def jsI18n(implicit ctx: Context) = i18nJsObject(translations)
Expand Down
26 changes: 14 additions & 12 deletions modules/msg/src/main/Env.scala
Expand Up @@ -28,18 +28,20 @@ final class Env(

lazy val search = wire[MsgSearch]

Bus.subscribeFun("remoteSocketIn:msgRead") {
case TellUserIn(userId, msg) =>
msg str "d" map User.normalize foreach { api.setRead(userId, _) }
}
Bus.subscribeFun("remoteSocketIn:msgSend") {
case TellUserIn(userId, msg) =>
for {
obj <- msg obj "d"
dest <- obj str "dest" map User.normalize
text <- obj str "text"
} api.post(userId, dest, text)
}
Bus.subscribeFuns(
"remoteSocketIn:msgRead" -> {
case TellUserIn(userId, msg) =>
msg str "d" map User.normalize foreach { api.setRead(userId, _) }
},
"remoteSocketIn:msgSend" -> {
case TellUserIn(userId, msg) =>
for {
obj <- msg obj "d"
dest <- obj str "dest" map User.normalize
text <- obj str "text"
} api.post(userId, dest, text)
}
)
}

private class MsgColls(db: lila.db.Db) {
Expand Down
8 changes: 8 additions & 0 deletions modules/relation/src/main/RelationApi.scala
Expand Up @@ -183,6 +183,10 @@ final class RelationApi(
repo.block(u1, u2) >> limitBlock(u1) >> unfollow(u2, u1) >>- {
reloadOnlineFriends(u1, u2)
Bus.publish(lila.hub.actorApi.relation.Block(u1, u2), "relation")
Bus.publish(
lila.hub.actorApi.socket.SendTo(u2, lila.socket.Socket.makeMessage("blockedBy", u1)),
"socketUsers"
)
lila.mon.relation.block.increment()
}
}
Expand All @@ -209,6 +213,10 @@ final class RelationApi(
repo.unblock(u1, u2) >>- {
reloadOnlineFriends(u1, u2)
Bus.publish(lila.hub.actorApi.relation.UnBlock(u1, u2), "relation")
Bus.publish(
lila.hub.actorApi.socket.SendTo(u2, lila.socket.Socket.makeMessage("unblockedBy", u1)),
"socketUsers"
)
lila.mon.relation.unblock.increment()
}
case _ => funit
Expand Down
4 changes: 4 additions & 0 deletions ui/msg/css/_msg.scss
Expand Up @@ -4,6 +4,10 @@ $msg-top-height: 4em;
@import 'side';
@import 'convo';

body {
overflow: hidden;
}

%msg-input-focus {
outline: 0;
border-color: $c-bg-zebra2;
Expand Down
6 changes: 5 additions & 1 deletion ui/msg/src/ctrl.ts
Expand Up @@ -11,7 +11,7 @@ export default class MsgCtrl {
network.upgradeData(opts.data);
this.data = opts.data;
this.trans = window.lichess.trans(opts.i18n);
network.onMsgNew(this.addMsg);
network.websocketHandler(this);
};

openConvo = (userId: string) => {
Expand Down Expand Up @@ -65,4 +65,8 @@ export default class MsgCtrl {
const userId = this.data.convo?.thread.contact.id;
if (userId) network.unblock(userId).then(() => this.openConvo(userId));
}

changeBlockBy = (userId: string) => {
if (userId == this.data.convo?.thread.contact.id) this.openConvo(userId);
}
}
17 changes: 11 additions & 6 deletions ui/msg/src/network.ts
@@ -1,4 +1,4 @@
import { Msg } from './interfaces';
import MsgCtrl from './ctrl';

const headers = {
'Accept': 'application/vnd.lichess.v4+json'
Expand Down Expand Up @@ -39,14 +39,16 @@ export function search(q: string) {
export function block(u: string) {
return $.ajax({
url: `/rel/block/${u}`,
method: 'post'
method: 'post',
headers
});
}

export function unblock(u: string) {
return $.ajax({
url: `/rel/unblock/${u}`,
method: 'post'
method: 'post',
headers
});
}

Expand All @@ -58,11 +60,14 @@ export function setRead(dest: string) {
window.lichess.pubsub.emit('socket.send', 'msgRead', dest);
}

export function onMsgNew(f: (msg: Msg) => void) {
window.lichess.pubsub.on('socket.in.msgNew', msg => {
export function websocketHandler(ctrl: MsgCtrl) {
const listen = window.lichess.pubsub.on;
listen('socket.in.msgNew', msg => {
upgradeMsg(msg);
f(msg);
ctrl.addMsg(msg);
});
listen('socket.in.blockedBy', ctrl.changeBlockBy);
listen('socket.in.unblockedBy', ctrl.changeBlockBy);
}

// the upgrade functions convert incoming timestamps into JS dates
Expand Down

0 comments on commit 858e72e

Please sign in to comment.