Skip to content

Commit

Permalink
Updates Lists API
Browse files Browse the repository at this point in the history
  • Loading branch information
solomon-b committed Nov 22, 2022
1 parent b9c7842 commit 3014283
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 20 deletions.
1 change: 1 addition & 0 deletions cofree-bot.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,4 @@ library
, process
, random
, vector
, xdg-basedir
5 changes: 1 addition & 4 deletions src/CofreeBot/Bot.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ import CofreeBot.Utils
import CofreeBot.Utils.ListT
import Control.Arrow qualified as Arrow
import Control.Exception
( catch,
throwIO,
)
import Control.Lens
( ifolded,
view,
Expand All @@ -78,8 +75,8 @@ import Data.These
import Network.Matrix.Client
import Network.Matrix.Client.Lens
import System.Directory (createDirectoryIfMissing)
import System.Directory.Internal.Prelude (isDoesNotExistError)
import System.IO
import System.IO.Error (isDoesNotExistError)
import System.Random

--------------------------------------------------------------------------------
Expand Down
68 changes: 52 additions & 16 deletions src/CofreeBot/Bot/Behaviors/Lists.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ module CofreeBot.Bot.Behaviors.Lists
)
where

-- TODO: I want a bot combinator ala Sessionize that allows you take
-- any bot and save its state to disk on updates.

--------------------------------------------------------------------------------

import CofreeBot.Bot
import CofreeBot.Utils (indistinct)
import Control.Applicative
import Control.Monad (void)
import Data.Attoparsec.ByteString.Char8
( isSpace,
)
Expand All @@ -25,7 +29,7 @@ import Data.Text qualified as T

data ListItemAction = Insert T.Text | Modify Int T.Text | Remove Int

data ListAction = CreateList T.Text | ModifyList T.Text ListItemAction | RemoveList T.Text | List T.Text
data ListAction = CreateList T.Text | ModifyList T.Text ListItemAction | DeleteList T.Text | ShowList T.Text

listItemBot :: Monad m => Bot m (IntMap T.Text) ListItemAction T.Text
listItemBot = Bot $ \s -> \case
Expand All @@ -46,25 +50,57 @@ listsBot' = Bot $ \s -> \case
let t = fromMaybe IntMap.empty $ Map.lookup name s
t' <- fmap snd $ runBot listItemBot t action
pure ("List Updated", Map.insert name t' s)
RemoveList name -> pure ("List deleted", Map.delete name s)
List name -> pure (T.pack $ show $ Map.lookup name s, s)
DeleteList name -> pure ("List deleted", Map.delete name s)
ShowList name -> pure (T.pack $ show $ Map.lookup name s, s)

listsBot :: Monad m => Bot m (Map T.Text (IntMap T.Text)) T.Text T.Text
listsBot = dimap (parseOnly parseListAction) indistinct $ emptyBot \/ listsBot'

parseListAction :: Parser ListAction
parseListAction =
parseCreateList <|> parseModifyList <|> parseRemoveList <|> parseListList
parseCreateList
<|> parseDeleteList
<|> parseAddListItem
<|> parseRemoveListItem
<|> parseUpdateListItem
<|> parseShowList
where
parseCreateList = "create" *> skipSpace *> fmap CreateList (takeTill isSpace)
parseModifyList =
"add"
*> skipSpace
*> fmap ModifyList (takeTill isSpace)
<*> fmap Insert takeText
parseRemoveList =
"remove"
*> skipSpace
*> fmap ModifyList (takeTill isSpace)
<*> fmap Remove decimal
parseListList = "show" *> skipSpace *> fmap List takeText
parseName = ("📝" <|> "list") *> skipSpace *> takeTill isSpace
parseCreateList = do
name <- parseName
skipSpace
void $ "" <|> "create"
skipSpace
pure (CreateList name)
parseDeleteList = do
name <- parseName
skipSpace
void $ "" <|> "delete"
skipSpace
pure (DeleteList name)
parseShowList = do
name <- parseName
pure (ShowList name)
parseAddListItem = do
name <- parseName
skipSpace
void ("📝" <|> "add")
skipSpace
item <- takeText
pure (ModifyList name (Insert item))
parseRemoveListItem = do
name <- parseName
skipSpace
void ("✔️" <|> "remove")
skipSpace
key <- decimal
pure (ModifyList name (Remove key))
parseUpdateListItem = do
name <- parseName
skipSpace
void "update"
skipSpace
key <- decimal
skipSpace
item <- takeText
pure (ModifyList name (Modify key item))

0 comments on commit 3014283

Please sign in to comment.