2013-04-02 09:45:45 -06:00
|
|
|
package lila.team
|
|
|
|
|
2013-04-04 08:28:52 -06:00
|
|
|
import actorApi._
|
2019-11-26 14:44:08 -07:00
|
|
|
import lila.common.Bus
|
2016-04-01 11:50:57 -06:00
|
|
|
import lila.db.dsl._
|
2017-08-06 01:40:37 -06:00
|
|
|
import lila.hub.actorApi.team.{ CreateTeam, JoinTeam }
|
2019-12-13 07:30:20 -07:00
|
|
|
import lila.hub.actorApi.timeline.{ Propagate, TeamCreate, TeamJoin }
|
2019-12-08 09:58:50 -07:00
|
|
|
import lila.hub.LightTeam
|
2018-07-05 14:39:57 -06:00
|
|
|
import lila.mod.ModlogApi
|
2019-12-08 10:35:26 -07:00
|
|
|
import lila.user.{ User, UserRepo }
|
2015-12-06 09:09:16 -07:00
|
|
|
import org.joda.time.Period
|
2018-07-21 03:05:05 -06:00
|
|
|
import reactivemongo.api.{ Cursor, ReadPreference }
|
2013-04-02 09:45:45 -06:00
|
|
|
|
2013-04-03 11:12:38 -06:00
|
|
|
final class TeamApi(
|
2019-12-02 21:31:35 -07:00
|
|
|
teamRepo: TeamRepo,
|
|
|
|
memberRepo: MemberRepo,
|
|
|
|
requestRepo: RequestRepo,
|
|
|
|
userRepo: UserRepo,
|
2013-04-03 11:12:38 -06:00
|
|
|
cached: Cached,
|
|
|
|
notifier: Notifier,
|
2019-12-02 21:31:35 -07:00
|
|
|
timeline: lila.hub.actors.Timeline,
|
|
|
|
indexer: lila.hub.actors.TeamSearch,
|
2018-07-05 14:39:57 -06:00
|
|
|
modLog: ModlogApi
|
2017-02-14 08:34:07 -07:00
|
|
|
) {
|
2013-04-02 09:45:45 -06:00
|
|
|
|
2016-04-01 22:57:54 -06:00
|
|
|
import BSONHandlers._
|
|
|
|
|
2014-11-15 06:53:01 -07:00
|
|
|
val creationPeriod = Period weeks 1
|
2013-04-02 09:45:45 -06:00
|
|
|
|
2019-12-02 21:31:35 -07:00
|
|
|
def team(id: Team.ID) = teamRepo.coll.byId[Team](id)
|
2013-05-06 14:49:12 -06:00
|
|
|
|
2019-12-02 21:31:35 -07:00
|
|
|
def light(id: Team.ID) = teamRepo.coll.byId[LightTeam](id, $doc("name" -> true))
|
2019-10-04 14:46:43 -06:00
|
|
|
|
2019-12-02 21:31:35 -07:00
|
|
|
def request(id: Team.ID) = requestRepo.coll.byId[Request](id)
|
2013-05-06 14:49:12 -06:00
|
|
|
|
2013-04-04 08:28:52 -06:00
|
|
|
def create(setup: TeamSetup, me: User): Option[Fu[Team]] = me.canTeam option {
|
|
|
|
val s = setup.trim
|
|
|
|
val team = Team.make(
|
|
|
|
name = s.name,
|
|
|
|
location = s.location,
|
|
|
|
description = s.description,
|
|
|
|
open = s.isOpen,
|
2017-02-14 08:34:07 -07:00
|
|
|
createdBy = me
|
|
|
|
)
|
2019-12-03 09:52:11 -07:00
|
|
|
teamRepo.coll.insert.one(team) >>
|
2019-12-02 21:31:35 -07:00
|
|
|
memberRepo.add(team.id, me.id) >>- {
|
2019-12-13 07:30:20 -07:00
|
|
|
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
|
2013-04-04 08:28:52 -06:00
|
|
|
}
|
|
|
|
|
2014-02-17 02:12:19 -07:00
|
|
|
def update(team: Team, edit: TeamEdit, me: User): Funit = edit.trim |> { e =>
|
2013-04-04 08:28:52 -06:00
|
|
|
team.copy(
|
|
|
|
location = e.location,
|
|
|
|
description = e.description,
|
2017-02-14 08:34:07 -07:00
|
|
|
open = e.isOpen
|
|
|
|
) |> { team =>
|
2019-12-03 09:52:11 -07:00
|
|
|
teamRepo.coll.update.one($id(team.id), team).void >>
|
2018-07-05 14:39:57 -06:00
|
|
|
!team.isCreator(me.id) ?? {
|
|
|
|
modLog.teamEdit(me.id, team.createdBy, team.name)
|
|
|
|
} >>-
|
|
|
|
(indexer ! InsertTeam(team))
|
2017-01-25 03:41:08 -07:00
|
|
|
}
|
2013-04-04 08:28:52 -06:00
|
|
|
}
|
|
|
|
|
2017-01-25 03:41:08 -07:00
|
|
|
def mine(me: User): Fu[List[Team]] =
|
2019-12-13 07:30:20 -07:00
|
|
|
cached teamIds me.id flatMap { ids =>
|
|
|
|
teamRepo.coll.byIds[Team](ids.toArray)
|
|
|
|
}
|
2013-04-04 08:28:52 -06:00
|
|
|
|
2017-02-05 04:11:03 -07:00
|
|
|
def hasTeams(me: User): Fu[Boolean] = cached.teamIds(me.id).map(_.value.nonEmpty)
|
2015-09-09 09:32:49 -06:00
|
|
|
|
2013-04-04 08:28:52 -06:00
|
|
|
def hasCreatedRecently(me: User): Fu[Boolean] =
|
2019-12-02 21:31:35 -07:00
|
|
|
teamRepo.userHasCreatedSince(me.id, creationPeriod)
|
2013-04-04 08:28:52 -06:00
|
|
|
|
2019-12-13 07:30:20 -07:00
|
|
|
def requestsWithUsers(team: Team): Fu[List[RequestWithUser]] =
|
|
|
|
for {
|
|
|
|
requests <- requestRepo findByTeam team.id
|
|
|
|
users <- userRepo usersFromSecondary requests.map(_.user)
|
|
|
|
} yield requests zip users map {
|
|
|
|
case (request, user) => RequestWithUser(request, user)
|
|
|
|
}
|
2013-04-04 08:28:52 -06:00
|
|
|
|
2019-12-13 07:30:20 -07:00
|
|
|
def requestsWithUsers(user: User): Fu[List[RequestWithUser]] =
|
|
|
|
for {
|
|
|
|
teamIds <- teamRepo teamIdsByCreator user.id
|
|
|
|
requests <- requestRepo findByTeams teamIds
|
|
|
|
users <- userRepo usersFromSecondary requests.map(_.user)
|
|
|
|
} yield requests zip users map {
|
|
|
|
case (request, user) => RequestWithUser(request, user)
|
|
|
|
}
|
2013-04-04 08:28:52 -06:00
|
|
|
|
2019-07-16 12:03:10 -06:00
|
|
|
def join(teamId: Team.ID, me: User): Fu[Option[Requesting]] =
|
2019-12-02 21:31:35 -07:00
|
|
|
teamRepo.coll.byId[Team](teamId) flatMap {
|
2019-07-16 12:03:10 -06:00
|
|
|
_ ?? { team =>
|
|
|
|
if (team.open) doJoin(team, me) inject Joined(team).some
|
|
|
|
else fuccess(Motivate(team).some)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def joinApi(teamId: Team.ID, me: User, oAuthAppOwner: User.ID): Fu[Option[Requesting]] =
|
2019-12-02 21:31:35 -07:00
|
|
|
teamRepo.coll.byId[Team](teamId) flatMap {
|
2019-07-16 12:03:10 -06:00
|
|
|
_ ?? { team =>
|
|
|
|
if (team.open || team.createdBy == oAuthAppOwner) doJoin(team, me) inject Joined(team).some
|
|
|
|
else fuccess(Motivate(team).some)
|
|
|
|
}
|
|
|
|
}
|
2013-04-04 08:28:52 -06:00
|
|
|
|
2019-12-13 07:30:20 -07:00
|
|
|
def requestable(teamId: Team.ID, user: User): Fu[Option[Team]] =
|
|
|
|
for {
|
|
|
|
teamOption <- teamRepo.coll.byId[Team](teamId)
|
|
|
|
able <- teamOption.??(requestable(_, user))
|
|
|
|
} yield teamOption filter (_ => able)
|
2013-04-04 08:28:52 -06:00
|
|
|
|
2019-12-13 07:30:20 -07:00
|
|
|
def requestable(team: Team, user: User): Fu[Boolean] =
|
|
|
|
for {
|
|
|
|
belongs <- belongsTo(team.id, user.id)
|
|
|
|
requested <- requestRepo.exists(team.id, user.id)
|
|
|
|
} yield !belongs && !requested
|
2013-04-04 08:28:52 -06:00
|
|
|
|
|
|
|
def createRequest(team: Team, setup: RequestSetup, user: User): Funit =
|
2013-05-11 15:45:39 -06:00
|
|
|
requestable(team, user) flatMap {
|
|
|
|
_ ?? {
|
|
|
|
val request = Request.make(team = team.id, user = user.id, message = setup.message)
|
2019-12-03 09:52:11 -07:00
|
|
|
requestRepo.coll.insert.one(request).void >>- (cached.nbRequests invalidate team.createdBy)
|
2013-05-11 15:45:39 -06:00
|
|
|
}
|
2013-04-04 08:28:52 -06:00
|
|
|
}
|
|
|
|
|
2019-12-13 07:30:20 -07:00
|
|
|
def processRequest(team: Team, request: Request, accept: Boolean): Funit =
|
|
|
|
for {
|
|
|
|
_ <- requestRepo.coll.delete.one(request)
|
|
|
|
_ = cached.nbRequests invalidate team.createdBy
|
|
|
|
userOption <- userRepo byId request.user
|
|
|
|
_ <- userOption
|
|
|
|
.filter(_ => accept)
|
|
|
|
.??(user => doJoin(team, user) >>- notifier.acceptRequest(team, request))
|
|
|
|
} yield ()
|
2013-04-04 08:28:52 -06:00
|
|
|
|
2017-10-30 09:31:27 -06:00
|
|
|
def deleteRequestsByUserId(userId: lila.user.User.ID) =
|
2019-12-02 21:31:35 -07:00
|
|
|
requestRepo.getByUserId(userId) flatMap {
|
2017-10-30 09:31:27 -06:00
|
|
|
_.map { request =>
|
2019-12-02 21:31:35 -07:00
|
|
|
requestRepo.remove(request.id) >>
|
|
|
|
teamRepo.creatorOf(request.team).map { _ ?? cached.nbRequests.invalidate }
|
2017-10-30 09:31:27 -06:00
|
|
|
}.sequenceFu
|
|
|
|
}
|
|
|
|
|
2019-07-16 12:03:10 -06:00
|
|
|
def doJoin(team: Team, user: User): Funit = !belongsTo(team.id, user.id) flatMap {
|
2017-01-25 03:41:08 -07:00
|
|
|
_ ?? {
|
2019-12-02 21:31:35 -07:00
|
|
|
memberRepo.add(team.id, user.id) >>
|
|
|
|
teamRepo.incMembers(team.id, +1) >>- {
|
2019-12-13 07:30:20 -07:00
|
|
|
cached invalidateTeamIds user.id
|
|
|
|
timeline ! Propagate(TeamJoin(user.id, team.id)).toFollowersOf(user.id)
|
|
|
|
Bus.publish(JoinTeam(id = team.id, userId = user.id), "team")
|
|
|
|
}
|
2017-02-01 05:56:47 -07:00
|
|
|
} recover lila.db.recoverDuplicateKey(_ => ())
|
2017-01-25 03:41:08 -07:00
|
|
|
}
|
2013-04-04 08:28:52 -06:00
|
|
|
|
2019-07-16 14:45:14 -06:00
|
|
|
def quit(teamId: Team.ID, me: User): Fu[Option[Team]] =
|
2019-12-02 21:31:35 -07:00
|
|
|
teamRepo.coll.byId[Team](teamId) flatMap {
|
2019-07-16 14:45:14 -06:00
|
|
|
_ ?? { team =>
|
|
|
|
doQuit(team, me.id) inject team.some
|
|
|
|
}
|
|
|
|
}
|
2013-04-04 08:28:52 -06:00
|
|
|
|
2019-07-08 11:59:39 -06:00
|
|
|
def doQuit(team: Team, userId: User.ID): Funit = belongsTo(team.id, userId) flatMap {
|
2017-01-25 03:41:08 -07:00
|
|
|
_ ?? {
|
2019-12-02 21:31:35 -07:00
|
|
|
memberRepo.remove(team.id, userId) >>
|
|
|
|
teamRepo.incMembers(team.id, -1) >>-
|
2017-01-25 03:41:08 -07:00
|
|
|
(cached invalidateTeamIds userId)
|
|
|
|
}
|
2014-04-18 03:51:19 -06:00
|
|
|
}
|
2013-04-04 08:28:52 -06:00
|
|
|
|
2019-12-02 21:31:35 -07:00
|
|
|
def quitAll(userId: User.ID): Funit = memberRepo.removeByUser(userId)
|
2013-04-04 08:28:52 -06:00
|
|
|
|
2019-07-08 11:59:39 -06:00
|
|
|
def kick(team: Team, userId: User.ID, me: User): Funit =
|
2018-07-05 14:39:57 -06:00
|
|
|
doQuit(team, userId) >>
|
|
|
|
!team.isCreator(me.id) ?? {
|
|
|
|
modLog.teamKick(me.id, userId, team.name)
|
|
|
|
}
|
2013-04-04 08:28:52 -06:00
|
|
|
|
2019-07-08 11:59:39 -06:00
|
|
|
def changeOwner(team: Team, userId: User.ID, me: User): Funit =
|
2019-12-02 21:31:35 -07:00
|
|
|
memberRepo.exists(team.id, userId) flatMap { e =>
|
2018-07-06 08:55:11 -06:00
|
|
|
e ?? {
|
2019-12-02 21:31:35 -07:00
|
|
|
teamRepo.changeOwner(team.id, userId) >>
|
2018-07-06 08:55:11 -06:00
|
|
|
modLog.teamMadeOwner(me.id, userId, team.name) >>-
|
|
|
|
notifier.madeOwner(team, userId)
|
|
|
|
}
|
|
|
|
}
|
2018-07-03 15:00:32 -06:00
|
|
|
|
2013-04-04 08:28:52 -06:00
|
|
|
def enable(team: Team): Funit =
|
2019-12-02 21:31:35 -07:00
|
|
|
teamRepo.enable(team).void >>- (indexer ! InsertTeam(team))
|
2013-04-04 08:28:52 -06:00
|
|
|
|
|
|
|
def disable(team: Team): Funit =
|
2019-12-02 21:31:35 -07:00
|
|
|
teamRepo.disable(team).void >>- (indexer ! RemoveTeam(team.id))
|
2013-04-04 08:28:52 -06:00
|
|
|
|
2013-05-28 07:53:32 -06:00
|
|
|
// delete for ever, with members but not forums
|
2013-04-04 08:28:52 -06:00
|
|
|
def delete(team: Team): Funit =
|
2019-12-03 09:52:11 -07:00
|
|
|
teamRepo.coll.delete.one($id(team.id)) >>
|
2019-12-02 21:31:35 -07:00
|
|
|
memberRepo.removeByteam(team.id) >>-
|
2013-04-04 08:28:52 -06:00
|
|
|
(indexer ! RemoveTeam(team.id))
|
|
|
|
|
2019-07-08 11:59:39 -06:00
|
|
|
def syncBelongsTo(teamId: Team.ID, userId: User.ID): Boolean =
|
2017-01-25 03:41:08 -07:00
|
|
|
cached.syncTeamIds(userId) contains teamId
|
|
|
|
|
2019-07-08 11:59:39 -06:00
|
|
|
def belongsTo(teamId: Team.ID, userId: User.ID): Fu[Boolean] =
|
2017-01-25 03:41:08 -07:00
|
|
|
cached.teamIds(userId) map (_ contains teamId)
|
2013-04-09 12:58:34 -06:00
|
|
|
|
2019-07-08 11:59:39 -06:00
|
|
|
def owns(teamId: Team.ID, userId: User.ID): Fu[Boolean] =
|
2019-12-02 21:31:35 -07:00
|
|
|
teamRepo ownerOf teamId map (_ has userId)
|
2013-05-28 07:53:32 -06:00
|
|
|
|
2019-10-02 04:20:44 -06:00
|
|
|
def filterExistingIds(ids: Set[String]): Fu[Set[Team.ID]] =
|
2019-12-02 21:31:35 -07:00
|
|
|
teamRepo.coll.distinctEasy[Team.ID, Set]("_id", $doc("_id" $in ids))
|
2019-10-02 04:20:44 -06:00
|
|
|
|
2019-10-02 10:50:09 -06:00
|
|
|
def autocomplete(term: String, max: Int): Fu[List[Team]] =
|
2019-12-13 07:30:20 -07:00
|
|
|
teamRepo.coll.ext
|
|
|
|
.find(
|
|
|
|
$doc(
|
|
|
|
"name".$startsWith(java.util.regex.Pattern.quote(term), "i"),
|
|
|
|
"enabled" -> true
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.sort($sort desc "nbMembers")
|
|
|
|
.list[Team](max, ReadPreference.secondaryPreferred)
|
2019-10-02 10:50:09 -06:00
|
|
|
|
2019-07-08 11:59:39 -06:00
|
|
|
def nbRequests(teamId: Team.ID) = cached.nbRequests get teamId
|
2015-04-12 00:18:47 -06:00
|
|
|
|
|
|
|
def recomputeNbMembers =
|
2019-12-13 07:30:20 -07:00
|
|
|
teamRepo.coll.ext.find($empty).cursor[Team](ReadPreference.secondaryPreferred).foldWhileM({}) {
|
|
|
|
(_, team) =>
|
|
|
|
for {
|
|
|
|
nb <- memberRepo.countByTeam(team.id)
|
|
|
|
_ <- teamRepo.coll.updateField($id(team.id), "nbMembers", nb)
|
|
|
|
} yield Cursor.Cont({})
|
2016-07-21 06:41:34 -06:00
|
|
|
}
|
2013-04-03 11:12:38 -06:00
|
|
|
}
|