diff --git a/querki/app/modules/PersonModule.scala b/querki/app/modules/PersonModule.scala index d80557364..58b3a417e 100644 --- a/querki/app/modules/PersonModule.scala +++ b/querki/app/modules/PersonModule.scala @@ -173,11 +173,7 @@ instead, you usually want to set the Chromeless Invites property on your Space.) // TODO: this is a fugly declaration, and possibly unsafe -- do we have any // assurance that modules.Modules.Email has been constructed before this? (modules.Modules.Email.MOIDs.EmailPropOID -> Optional.QNone), - DisplayTextProp(""" -This represents a Person who is using Querki or can be invited to it. You can create a Person in -your Space, and compose an email to invite them to use the Space; you can also create a new Model -to add new Properties for any Person in your Space. -"""))) + DisplayTextProp("""This represents a Member of this Space."""))) override lazy val things = Seq( securityPrincipal, @@ -437,26 +433,48 @@ object PersonModule { import modules.Modules.Person._ import modules.Modules.Person.MOIDs._ + def getPersonIdentity(person:Thing)(implicit state:SpaceState):Option[OID] = { + for ( + identityVal <- person.getPropOpt(identityLink); + identityId <- identityVal.firstOpt + ) + yield identityId + } + /** * Additional features of User, specifically for interacting with Person. */ implicit class UserPersonEnhancements(user:User) { def hasPerson(personId:OID)(implicit state:SpaceState):Boolean = { - val idOpt = for ( - person <- state.anything(personId); - identityVal <- person.getPropOpt(identityLink); - identityId <- identityVal.firstOpt - ) - yield identityId - + state.anything(personId).map(hasPerson(_)).getOrElse(false) + } + def hasPerson(person:Thing)(implicit state:SpaceState):Boolean = { + val idOpt = getPersonIdentity(person) idOpt.map(user.hasIdentity(_)).getOrElse(false) } def localPerson(implicit state:SpaceState):Option[Thing] = { state. descendants(PersonOID, false, true). - filter(person => hasPerson(person.id)). + filter(person => hasPerson(person)). headOption } } + + /** + * Additional features of Identity, specifically for interacting with Person. + */ + implicit class IdentityPersonEnhancements(identity:Identity) { + def isPerson(person:Thing)(implicit state:SpaceState):Boolean = { + val idOpt = getPersonIdentity(person) + idOpt.map(_ == identity.id).getOrElse(false) + } + + def localPerson(implicit state:SpaceState):Option[Thing] = { + state. + descendants(PersonOID, false, true). + filter(person => isPerson(person)). + headOption + } + } } \ No newline at end of file diff --git a/querki/app/querki/spaces/Space.scala b/querki/app/querki/spaces/Space.scala index 5e276fb94..489398498 100644 --- a/querki/app/querki/spaces/Space.scala +++ b/querki/app/querki/spaces/Space.scala @@ -114,6 +114,31 @@ private [spaces] class Space(persistenceFactory:SpacePersistenceFactory) extends } } + /** + * When we load the Space, make sure that the Owner has a matching Person record, so they show up + * as a Member. This will mainly affect newly-created Spaces. + * + * TBD: all these internal imports are a bad smell. This probably belongs elsewhere, but where? + */ + def checkOwnerIsMember() = { + import models.Thing._ + import modules.Modules.Person.{identityLink, person} + import modules.person.PersonModule._ + import querki.identity.SystemUser + + state.ownerIdentity.foreach { identity => + if (identity.localPerson(state).isEmpty) { + createSomething(id, SystemUser, person.id, + toProps( + setName(identity.handle), + DisplayNameProp(identity.name), + identityLink(identity.id))(), + Kind.Thing, + None) + } + } + } + def loadSpace() = { // TEMP: just as a proof of concept. This is entirely wrong in the long run: we should be using // FSM and Requester instead of blocking here: @@ -122,6 +147,7 @@ private [spaces] class Space(persistenceFactory:SpacePersistenceFactory) extends result match { case Loaded(state) => { _currentState = Some(state) + checkOwnerIsMember() } case _ => QLog.error("Got an error!") } diff --git a/querki/test/modules/person/PersonTest.scala b/querki/test/modules/person/PersonTest.scala index b912d67e1..8a7044ba2 100644 --- a/querki/test/modules/person/PersonTest.scala +++ b/querki/test/modules/person/PersonTest.scala @@ -13,6 +13,8 @@ class PersonTest extends QuerkiTests { val person = commonState.anything(personId) assert(person.isDefined) assert(commonSpace.member1.user.hasPerson(personId)) + + assert(commonSpace.member1.user.mainIdentity.isPerson(person.get)) } }