ensure there's no existing chat for a new team ID

pull/7377/head
Thibault Duplessis 2020-09-25 17:31:21 +02:00
parent 89b0f2af51
commit 842e848e2e
3 changed files with 36 additions and 22 deletions

View File

@ -27,6 +27,8 @@ final class ChatApi(
import Chat.{ chatIdBSONHandler, userChatBSONHandler }
def exists(id: String) = coll.exists($id(id))
object userChat {
// only use for public, multi-user chats - tournaments, simuls

View File

@ -67,6 +67,7 @@ object Team {
}
def make(
id: String,
name: String,
location: Option[String],
description: String,
@ -74,7 +75,7 @@ object Team {
createdBy: User
): Team =
new Team(
_id = nameToId(name),
_id = id,
name = name,
location = location,
description = description,
@ -90,6 +91,8 @@ object Team {
def nameToId(name: String) =
(lila.common.String slugify name) pipe { slug =>
// if most chars are not latin, go for random slug
if (slug.lengthIs > (name.lengthIs / 2)) slug else lila.common.ThreadLocalRandom nextString 8
if (slug.lengthIs > (name.lengthIs / 2)) slug else randomId()
}
private[team] def randomId() = lila.common.ThreadLocalRandom nextString 8
}

View File

@ -1,12 +1,13 @@
package lila.team
import actorApi._
import org.joda.time.Period
import scala.util.chaining._
import play.api.libs.json.{ JsSuccess, Json }
import reactivemongo.api.{ Cursor, ReadPreference }
import scala.util.chaining._
import scala.util.Try
import actorApi._
import lila.chat.ChatApi
import lila.common.Bus
import lila.db.dsl._
import lila.hub.actorApi.team.{ CreateTeam, JoinTeam, KickFromTeam }
@ -25,7 +26,8 @@ final class TeamApi(
notifier: Notifier,
timeline: lila.hub.actors.Timeline,
indexer: lila.hub.actors.TeamSearch,
modLog: ModlogApi
modLog: ModlogApi,
chatApi: ChatApi
)(implicit ec: scala.concurrent.ExecutionContext) {
import BSONHandlers._
@ -39,23 +41,30 @@ final class TeamApi(
def request(id: Team.ID) = requestRepo.coll.byId[Request](id)
def create(setup: TeamSetup, me: User): Fu[Team] = {
val s = setup.trim
val team = Team.make(
name = s.name,
location = s.location,
description = s.description,
open = s.isOpen,
createdBy = me
)
teamRepo.coll.insert.one(team) >>
memberRepo.add(team.id, me.id) >>- {
cached invalidateTeamIds me.id
indexer ! InsertTeam(team)
timeline ! Propagate(
TeamCreate(me.id, team.id)
).toFollowersOf(me.id)
Bus.publish(CreateTeam(id = team.id, name = team.name, userId = me.id), "team")
} inject team
val s = setup.trim
val bestId = Team.nameToId(s.name)
chatApi.exists(bestId) map {
case true => Team.randomId()
case false => bestId
} flatMap { id =>
val team = Team.make(
id = id,
name = s.name,
location = s.location,
description = s.description,
open = s.isOpen,
createdBy = me
)
teamRepo.coll.insert.one(team) >>
memberRepo.add(team.id, me.id) >>- {
cached invalidateTeamIds me.id
indexer ! InsertTeam(team)
timeline ! Propagate(
TeamCreate(me.id, team.id)
).toFollowersOf(me.id)
Bus.publish(CreateTeam(id = team.id, name = team.name, userId = me.id), "team")
} inject team
}
}
def update(team: Team, edit: TeamEdit, me: User): Funit =