Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- addOrSubscribe :: T.Text -> SubsVar -> FeedsStore -> Chan OutboundMessage -> ChatId -> IO ()
- {-
- Get locally or
- Get redis or
- Request
- -}
- addOrSubscribe url subsvar store outQ cid = do
- print "addOrSubscribe: attempting to take store now"
- tryTakeMVar store >>= \case
- {- Store not initialized yet -}
- Nothing -> print "addOrSubscribe: Store not initialized yet"
- Just hmap -> do
- print "addOrSubscribe: took store"
- case HMS.lookup url hmap of
- Nothing -> connectAndDo_ $ do
- liftIO . print $ "addOrSubscribe: Feed not found in local store, asking Redis"
- getFeed url >>= \case
- {- Not found in local store, asking Redis -}
- Left _ -> liftIO $ do
- liftIO . print $ "addOrSubscribe: Feed not found in Redis, fetching"
- fetchIfFeed cid "no seed" url >>= \(_, eitherfeed) -> case eitherfeed of
- {- Not found in Redis, not found after trying to fetch -}
- Left failed -> do
- print "addOrSubscribe: Fetching failed"
- if null failed then reply cid outQ $ "No feed at this address: " `T.append` url
- else reply cid outQ $ "Unable to parse the publication dates of the following items, preventing the creation of their feed " `T.append` (T.pack . show $ failed)
- putMVar store hmap
- {- Didn't find feed in store or Redis, found after fetching -}
- Right f -> do
- print "addOrSubscribe: Fetching successful"
- let replied = reply cid outQ $ "Fetched this new feed:\n" `T.append` (T.pack . show . f_header $ f) -- replies to user
- savedUserState = do
- print "addOrSubscribe: Trying to save user state"
- addSubToSubs subsvar cid url -- saves to user state
- savedFeedsStore = do
- print "addOrSubscribe: Trying to add feed to local store"
- addToStore store hmap f -- saves to feedsstore
- savedRedis = do
- print "addOrSubscribe: Trying to save feed to db and then subscribing user"
- connectAndDo_ $ do
- written <- writeFeed f -- saves to Redis
- res <- subscribeToFeed url cid outQ -- add user to pub/sub channel corresponding to feed url
- liftIO . print $ "writeFeed from addorSubscribe" `T.append` (T.pack . show $ written)
- liftIO . print $ "subscribeToFeed from addorSubscribe" `T.append` (T.pack . show $ res)
- forkActions [replied, savedUserState, savedFeedsStore, savedRedis]
- {- Found feed in local store, checking user state -}
- Right feed -> liftIO $ readMVar subsvar >>= \subs -> case HMS.lookup cid subs of
- {- Feed got from Redis, user not in user state -}
- Nothing -> liftIO $ do
- reply cid outQ "Just created a new user for you! \n"
- reply cid outQ $ "Found in database:\n" `T.append` (T.pack . show . f_header $ feed)
- let newSub = Subscriber { subfoldtd = HMS.insert url (0, [""]) HMS.empty, subfolders = [], subid = cid }
- savedUserState = do
- print "addOrSubscribe: Trying to save user state"
- addSubToSubs subsvar cid url -- saves to user state
- savedFeedsStore = do
- print "addOrSubscribe: Trying to add feed to local store"
- addToStore store hmap feed -- saves to feedsstore
- savedUserRedis = do
- print "addOrSubscribe: Trying to save user to redis"
- connectAndDo_ $ do
- res <- writeUser newSub
- liftIO . print $ "writeUser from addorSubscribe" `T.append` (T.pack . show $ res)
- forkActions [savedUserState, savedFeedsStore, savedUserRedis]
- {- Feed got from Redis, user in user state -}
- Just _ -> liftIO $ do
- reply cid outQ $ "Found in database, and you're already subscribed\n:" `T.append` (T.pack . show . f_header $ feed)
- ensureSubscribed subsvar subs cid url
- putMVar store hmap
- {- Found in local store, replying -}
- Just f -> do
- reply cid outQ $ "Found in cache:\n" `T.append` (T.pack . show . f_header $ f)
- putMVar store hmap
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement