inbox2
Thibault Duplessis 2020-01-24 14:10:07 -06:00
parent b7c78e562a
commit f24bf798e6
9 changed files with 181 additions and 1 deletions

View File

@ -0,0 +1,41 @@
db.msg_msg.remove({});
db.msg_thread.remove({});
print("Create db.m_thread_sorted");
if (true) {
db.m_thread_sorted.drop();
db.m_thread.find({visibleByUserIds:{$size:2}}).forEach(t => {
t.visibleByUserIds.sort();
db.m_thread_sorted.insert(t);
});
}
print("Create db.msg_thread");
db.m_thread_sorted.aggregate([
{$group:{_id:'$visibleByUserIds',threads:{$push:'$$ROOT'}}}
]).forEach(o => {
let first = o.threads[0];
let posts = [];
o.posts.forEach(ps => {
ps.forEach(p => posts.push);
});
posts.sort((a,b) => new Date(b.createdAt) - new Date(a.createdAt));
let msgs = posts.map(p => ({
_id: p.id,
text: p.text,
date: p.createdAt,
read: p.isRead
}));
let thread = {
_id: first._id,
users: o._id.sort(),
lastMsg: lastMsg
}
});

View File

@ -0,0 +1,2 @@
db.msg_thread.ensureIndex({users:1,'lastMsg.date':-1});
db.msg_msg.ensureIndex({thread:1,date:-1})

View File

@ -45,7 +45,7 @@ libraryDependencies ++= Seq(
lazy val modules = Seq(
common, db, rating, user, security, hub, socket,
message, notifyModule, i18n, game, bookmark, search,
message, msg, notifyModule, i18n, game, bookmark, search,
gameSearch, timeline, forum, forumSearch, team, teamSearch,
analyse, mod, round, pool, lobby, setup,
importer, tournament, simul, relation, report, pref,
@ -353,6 +353,11 @@ lazy val message = module("message",
reactivemongo.bundle
)
lazy val msg = module("msg",
Seq(common, db, user, hub, relation, security, shutup, notifyModule),
reactivemongo.bundle
)
lazy val forum = module("forum",
Seq(common, db, user, security, hub, mod, notifyModule),
reactivemongo.bundle

View File

@ -0,0 +1,16 @@
package lila.msg
import lila.db.dsl._
import reactivemongo.api.bson._
private[msg] object BsonHandlers {
import Msg.Last
implicit val msgContentBSONHandler = Macros.handler[Last]
implicit val threadIdBSONHandler = stringAnyValHandler[MsgThread.Id](_.value, MsgThread.Id.apply)
implicit val threadBSONHandler = Macros.handler[MsgThread]
implicit val msgIdBSONHandler = stringAnyValHandler[Msg.Id](_.value, Msg.Id.apply)
implicit val msgBSONHandler = Macros.handler[Msg]
}

View File

@ -0,0 +1,21 @@
package lila.msg
import com.softwaremill.macwire._
import lila.common.config._
@Module
final class Env(
db: lila.db.Db,
userRepo: lila.user.UserRepo
)(implicit ec: scala.concurrent.ExecutionContext) {
private val colls = wire[MsgColls]
lazy val api: MsgApi = wire[MsgApi]
}
private class MsgColls(db: lila.db.Db) {
val thread = db(CollName("msg_thread"))
val msg = db(CollName("msg_msg"))
}

View File

@ -0,0 +1,36 @@
package lila.msg
import org.joda.time.DateTime
import lila.user.User
case class Msg(
_id: Msg.Id,
thread: MsgThread.Id,
text: String,
user: User.ID,
date: DateTime
)
object Msg {
case class Id(value: String) extends AnyVal
case class Last(
text: String,
user: User.ID,
date: DateTime,
read: Boolean
)
def make(
thread: MsgThread.Id,
last: Last
): Msg = Msg(
_id = Id(ornicar.scalalib.Random nextString 8),
thread = thread,
text = last.text,
user = last.user,
date = last.date
)
}

View File

@ -0,0 +1,27 @@
package lila.msg
import org.joda.time.DateTime
import scala.concurrent.duration._
import reactivemongo.api._
import lila.db.dsl._
import lila.user.{ User, UserRepo }
final class MsgApi(
colls: MsgColls,
userRepo: UserRepo
)(implicit ec: scala.concurrent.ExecutionContext) {
import BsonHandlers._
def inbox(me: User): Fu[List[MsgThread]] =
colls.thread.ext
.find(
$doc(
"users" -> me.id,
"blockers" $ne me.id
)
)
.sort($sort desc "lastMsg.date")
.list[MsgThread](100)
}

View File

@ -0,0 +1,26 @@
package lila.msg
import org.joda.time.DateTime
import lila.user.User
case class MsgThread(
_id: MsgThread.Id, // random
users: List[User.ID], // unique
lastMsg: Msg.Last
)
object MsgThread {
case class Id(value: String) extends AnyVal
def make(
msg: Msg.Last,
orig: User.ID,
dest: User.ID
): MsgThread = MsgThread(
_id = Id(ornicar.scalalib.Random nextString 8),
users = List(orig, dest).sorted,
lastMsg = msg
)
}

View File

@ -0,0 +1,6 @@
package lila
package object msg extends PackageObject {
private[msg] val logger = lila.log("msg")
}