Skip to content

Commit

Permalink
Types.Users: trim sigils from inputs to findUserByNickname and findUs…
Browse files Browse the repository at this point in the history
…erByName (fixes #384)
  • Loading branch information
jtdaugherty committed Apr 6, 2018
1 parent 42e9da8 commit 2bfee4c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
10 changes: 0 additions & 10 deletions src/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ module Types
, refreshChannelZipper
, getChannelIdsInOrder

, trimUserSigil
, trimChannelSigil

, LinkChoice(LinkChoice)
Expand Down Expand Up @@ -185,7 +184,6 @@ module Types
, displaynameForUserId
, raiseInternalEvent

, userSigil
, normalChannelSigil

, HighlightSet(..)
Expand Down Expand Up @@ -408,9 +406,6 @@ makeLenses ''LinkChoice
normalChannelSigil :: Text
normalChannelSigil = "~"

userSigil :: Text
userSigil = "@"

-- ** Channel-matching types

data ChannelSelectMatch =
Expand Down Expand Up @@ -956,11 +951,6 @@ trimChannelSigil n
| normalChannelSigil `T.isPrefixOf` n = T.tail n
| otherwise = n

trimUserSigil :: Text -> Text
trimUserSigil n
| userSigil `T.isPrefixOf` n = T.tail n
| otherwise = n

addNewUser :: UserInfo -> MH ()
addNewUser u = do
csUsers %= addUser u
Expand Down
24 changes: 18 additions & 6 deletions src/Types/Users.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ module Types.Users
-- * Creating UserInfo objects
, userInfoFromUser
-- * Miscellaneous
, userSigil
, trimUserSigil
, statusFromText
, findUserById
, findUserByName
Expand Down Expand Up @@ -152,21 +154,31 @@ expireTypingUsers expiryTimestamp =
findUserById :: UserId -> Users -> Maybe UserInfo
findUserById uId = HM.lookup uId . _ofUsers

-- | Get the User information given the user's name. This is an exact
-- match on the username field, not necessarly the presented name.
-- | Get the User information given the user's name. This is an exact
-- match on the username field, not necessarly the presented name. It
-- will automatically trim a user sigil from the input.
findUserByName :: Users -> Text -> Maybe (UserId, UserInfo)
findUserByName allusers name =
case filter ((== name) . _uiName . snd) $ HM.toList $ _ofUsers allusers of
case filter ((== trimUserSigil name) . _uiName . snd) $ HM.toList $ _ofUsers allusers of
(usr : []) -> Just usr
_ -> Nothing

-- | Get the User information given the user's name. This is an exact
-- match on the nickname field, not necessarily the presented name.
-- | Get the User information given the user's name. This is an exact
-- match on the nickname field, not necessarily the presented name. It
-- will automatically trim a user sigil from the input.
findUserByNickname :: [UserInfo] -> Text -> Maybe UserInfo
findUserByNickname uList nick =
find (nickCheck nick) uList
where
nickCheck n = maybe False (== n) . _uiNickName
nickCheck n = maybe False (== (trimUserSigil n)) . _uiNickName

userSigil :: Text
userSigil = "@"

trimUserSigil :: Text -> Text
trimUserSigil n
| userSigil `T.isPrefixOf` n = T.tail n
| otherwise = n

-- | Extract a specific user from the collection and perform an
-- endomorphism operation on it, then put it back into the collection.
Expand Down

0 comments on commit 2bfee4c

Please sign in to comment.