Advertisement
Nycticorax

Untitled

Nov 18th, 2020
1,184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. addOrSubscribe :: T.Text -> SubsVar -> FeedsStore -> Chan OutboundMessage -> ChatId -> IO ()
  2. {-
  3.     Get locally or
  4.     Get redis or
  5.     Request
  6. -}
  7. addOrSubscribe url subsvar store outQ cid = do
  8.     print "addOrSubscribe: attempting to take store now"
  9.     tryTakeMVar store >>= \case
  10.         {- Store not initialized yet -}
  11.         Nothing -> print "addOrSubscribe: Store not initialized yet"
  12.         Just hmap -> do
  13.             print "addOrSubscribe: took store"
  14.             case HMS.lookup url hmap of
  15.                 Nothing -> connectAndDo_ $ do
  16.                     liftIO . print $ "addOrSubscribe: Feed not found in local store, asking Redis"
  17.                     getFeed url >>= \case
  18.                         {- Not found in local store, asking Redis -}
  19.                         Left _ -> liftIO $ do
  20.                             liftIO . print $ "addOrSubscribe: Feed not found in Redis, fetching"
  21.                             fetchIfFeed cid "no seed" url >>= \(_, eitherfeed) -> case eitherfeed of
  22.                                 {- Not found in Redis, not found after trying to fetch -}
  23.                                 Left failed -> do
  24.                                     print "addOrSubscribe: Fetching failed"
  25.                                     if null failed then reply cid outQ $ "No feed at this address: " `T.append` url
  26.                                     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)
  27.                                     putMVar store hmap
  28.                                 {- Didn't find feed in store or Redis, found after fetching -}
  29.                                 Right f -> do
  30.                                     print "addOrSubscribe: Fetching successful"
  31.                                     let replied = reply cid outQ $ "Fetched this new feed:\n" `T.append` (T.pack . show . f_header $ f) -- replies to user
  32.                                         savedUserState = do
  33.                                             print "addOrSubscribe: Trying to save user state"
  34.                                             addSubToSubs subsvar cid url -- saves to user state
  35.                                         savedFeedsStore = do
  36.                                             print "addOrSubscribe: Trying to add feed to local store"
  37.                                             addToStore store hmap f -- saves to feedsstore
  38.                                         savedRedis = do
  39.                                             print "addOrSubscribe: Trying to save feed to db and then subscribing user"
  40.                                             connectAndDo_ $ do
  41.                                                 written <- writeFeed f -- saves to Redis
  42.                                                 res <- subscribeToFeed url cid outQ -- add user to pub/sub channel corresponding to feed url
  43.                                                 liftIO . print $ "writeFeed from addorSubscribe" `T.append` (T.pack . show $ written)
  44.                                                 liftIO . print $ "subscribeToFeed from addorSubscribe" `T.append` (T.pack . show $ res)
  45.                                     forkActions [replied, savedUserState, savedFeedsStore, savedRedis]
  46.                         {- Found feed in local store, checking user state -}
  47.                         Right feed -> liftIO $ readMVar subsvar >>= \subs -> case HMS.lookup cid subs of
  48.                             {- Feed got from Redis, user not in user state -}
  49.                             Nothing -> liftIO $ do
  50.                                 reply cid outQ "Just created a new user for you! \n"
  51.                                 reply cid outQ $ "Found in database:\n" `T.append` (T.pack . show . f_header $ feed)
  52.                                 let newSub = Subscriber { subfoldtd = HMS.insert url (0, [""]) HMS.empty, subfolders = [], subid = cid }
  53.                                     savedUserState = do
  54.                                         print "addOrSubscribe: Trying to save user state"
  55.                                         addSubToSubs subsvar cid url -- saves to user state
  56.                                     savedFeedsStore = do
  57.                                         print "addOrSubscribe: Trying to add feed to local store"
  58.                                         addToStore store hmap feed -- saves to feedsstore
  59.                                     savedUserRedis = do
  60.                                         print "addOrSubscribe: Trying to save user to redis"
  61.                                         connectAndDo_ $ do
  62.                                             res <- writeUser newSub
  63.                                             liftIO . print $ "writeUser from addorSubscribe" `T.append` (T.pack . show $ res)
  64.                                 forkActions [savedUserState, savedFeedsStore, savedUserRedis]
  65.                             {- Feed got from Redis, user in user state -}
  66.                             Just _ -> liftIO $ do
  67.                                 reply cid outQ $ "Found in database, and you're already subscribed\n:" `T.append` (T.pack . show . f_header $ feed)
  68.                                 ensureSubscribed subsvar subs cid url
  69.                                 putMVar store hmap        
  70.                 {- Found in local store, replying -}
  71.                 Just f -> do
  72.                     reply cid outQ $ "Found in cache:\n" `T.append` (T.pack . show . f_header $ f)
  73.                     putMVar store hmap
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement