Got rid of ! in Server.hs via better lensing

master
Levi Pearson 2014-06-29 23:50:26 -06:00
parent fd141d09fb
commit f8b361dc5b
1 changed files with 12 additions and 14 deletions

View File

@ -20,11 +20,9 @@ module Pipes.IRC.Server.Server
where
import Control.Lens
import Data.Map ((!))
import qualified Data.Map as M
import Data.Maybe
import qualified Data.Set as S
import Pipes.IRC.Server.Channel
import Pipes.IRC.Server.Types
import Pipes.IRC.Server.User
@ -46,14 +44,14 @@ ircDelUser :: NickKey -- ^ nickname of the user to delete
-> IrcServer -- ^ server to delete the user from
-> IrcServer -- ^ new server with user deleted
ircDelUser nn srv =
srv & ircNicks %~ S.delete nn
& if M.notMember nn (srv ^. ircUsers) then id else
srv & ircNicks %~ sans nn
& if isn't _Just (srv ^. ircUsers . at nn) then id else
let
usr = (srv ^. ircUsers) ! nn
uchans = S.elems (usr ^. userChannels)
ichans = S.elems (usr ^. userInvites)
usr = srv ^. ircUsers . at nn
uchans = usr ^. traverse . userChannels . to S.elems
ichans = usr ^. traverse . userInvites . to S.elems
in
(ircUsers %~ M.delete nn)
(ircUsers %~ sans nn)
. (ircChannels %~ alterAtKeys (ircPartChan nn) uchans)
. (ircChannels %~ adjustAtKeys (chanDelInvite nn) ichans)
@ -105,8 +103,8 @@ ircPart uname cn srv =
srv & (ircChannels %~ (M.alter $ ircPartChan uname) cn)
& (ircUsers %~ adjustAtKeys (userDelInvite cn) iusers)
where
chan = (srv ^. ircChannels) ! cn
iusers = S.elems $ chan ^. chanInvites
chan = srv ^. ircChannels . at cn
iusers = chan ^. traverse . chanInvites . to S.elems
-- | Add the user with the given nickname to the invited list for the
-- channel, and add the channel to the user's invited list.
@ -146,9 +144,9 @@ ircChangeNick old new srv =
if M.notMember old (srv ^. ircUsers) then
srv
else let
usr = (srv ^. ircUsers) ! old
chs = S.elems (usr ^. userChannels)
usr = srv ^. ircUsers . at old
chs = usr ^. traverse . userChannels . to S.elems
in
srv & (ircNicks %~ S.delete old . S.insert new)
& (ircUsers %~ M.delete old . M.insert new usr)
srv & (ircNicks %~ sans old . S.insert new)
& (ircUsers %~ sans old . (at new .~ usr))
& (ircChannels %~ adjustAtKeys (chanChangeNick old new) chs)