-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automatic mark mail as read #56
Merged
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
281a25a
Use latest HEAD of hs-notmuch
romanofski e723258
Retrieve the messageId
romanofski f4b3556
Retrieve the messageId
romanofski f231cc9
Indicate if mail is unread during rendering
romanofski 7b3a8e8
toggle mails as read
romanofski 6c766f7
Properly style selected, unread mail in list
romanofski 53a72d4
Acceptance test automatic set to read feature
romanofski 806e51f
tests: Allow easier debugging
romanofski 7601c51
ci: run all tests in debug mode
romanofski 76039d3
tests: Fixes intermittently breaking test
romanofski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,96 @@ | ||
{-# LANGUAGE FlexibleContexts #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
-- | module for integrating notmuch within purebred | ||
module Storage.Notmuch where | ||
|
||
import Types (NotmuchMail(..)) | ||
import Notmuch | ||
import Notmuch.Search | ||
import Notmuch | ||
import Notmuch.Search | ||
|
||
import Data.Maybe (fromMaybe) | ||
import qualified Data.Vector as Vec | ||
import System.Process (readProcess) | ||
import Control.Monad (void) | ||
import Control.Monad.IO.Class (liftIO) | ||
import Control.Monad.Except (runExceptT) | ||
import Data.Traversable (traverse) | ||
import Data.List (union) | ||
import Data.Maybe (fromMaybe) | ||
import Data.Bifunctor (first) | ||
import qualified Data.Vector as Vec | ||
import System.Process (readProcess) | ||
import Control.Exception (bracket) | ||
import qualified Data.Text as T | ||
import Data.Text.Encoding (decodeUtf8) | ||
import Types (NotmuchSettings, nmDatabase, nmNewTag) | ||
import Data.Text.Encoding (decodeUtf8, encodeUtf8) | ||
import Types (NotmuchSettings, nmDatabase, mailId, mailTags) | ||
import Control.Lens.Getter (view) | ||
import Control.Lens.Setter (over) | ||
|
||
|
||
-- | creates a vector of parsed mails from a not much search | ||
-- Note, that at this point in time only free form searches are supported. Also, | ||
-- we filter out the tag which we use to mark mails as new mails | ||
getMessages :: T.Text -> NotmuchSettings FilePath -> IO (Vec.Vector NotmuchMail) | ||
getMessages s settings = do | ||
db' <- databaseOpen (view nmDatabase settings) | ||
case db' of | ||
Left status -> do | ||
error $ show status | ||
Right db -> do | ||
q <- query db (FreeForm $ T.unpack s) | ||
msgs <- messages q | ||
mails <- mapM (messageToMail $ view nmNewTag settings) msgs | ||
return $ Vec.fromList mails | ||
getMessages :: T.Text | ||
-> NotmuchSettings FilePath | ||
-> IO (Either String (Vec.Vector NotmuchMail)) | ||
getMessages s settings = | ||
first show <$> | ||
bracket (runExceptT (databaseOpenReadOnly (view nmDatabase settings)) | ||
>>= either (error . show) pure) | ||
(void . runExceptT . databaseDestroy) | ||
(\db -> runExceptT $ do | ||
msgs <- query db (FreeForm $ T.unpack s) >>= messages | ||
mails <- liftIO $ mapM messageToMail msgs | ||
return $ Vec.fromList mails) | ||
|
||
setNotmuchMailTags | ||
:: FilePath | ||
-> NotmuchMail | ||
-> IO (Either String NotmuchMail) | ||
setNotmuchMailTags dbpath m = | ||
case mailTagsToNotmuchTags m of | ||
Nothing -> pure $ Left "Tags are corrupt" | ||
Just nmtags -> | ||
bracket (runExceptT (databaseOpen dbpath) >>= either (error . show) pure) | ||
(void . runExceptT . databaseDestroy) | ||
(\db -> tagsToMessage nmtags m db) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, |
||
|
||
tagsToMessage | ||
:: [Tag] -> NotmuchMail -> Database RW -> IO (Either String NotmuchMail) | ||
tagsToMessage xs m db = | ||
runExceptT | ||
(findMessage db | ||
(view mailId m)) >>= | ||
\case | ||
Left e -> pure $ Left (show e) | ||
Right Nothing -> pure $ Left "boop" | ||
Right (Just msg) -> do | ||
_ <- runExceptT (messageSetTags xs msg) | ||
pure $ Right m | ||
|
||
addTag :: NotmuchMail -> T.Text -> NotmuchMail | ||
addTag m t = over mailTags (`union` [t]) m | ||
|
||
removeTag :: NotmuchMail -> T.Text -> NotmuchMail | ||
removeTag m t = over mailTags (filter (/= t)) m | ||
|
||
mailTagsToNotmuchTags :: NotmuchMail -> Maybe [Tag] | ||
mailTagsToNotmuchTags m = | ||
let xs = view mailTags m | ||
in traverse id (mkTag . encodeUtf8 <$> xs) | ||
|
||
messageToMail | ||
:: HasTags Message | ||
=> T.Text | ||
-> Message | ||
:: HasTags (Message n a) | ||
=> Message n a | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
-> IO NotmuchMail | ||
messageToMail ignoredTag m = do | ||
messageToMail m = do | ||
tgs <- tags m | ||
let tgs' = decodeUtf8 . getTag <$> tgs | ||
NotmuchMail <$> | ||
(decodeUtf8 . fromMaybe "" <$> messageHeader "Subject" m) <*> | ||
(decodeUtf8 . fromMaybe "" <$> messageHeader "From" m) <*> | ||
messageFilename m <*> | ||
messageDate m <*> | ||
(pure $ tagsToText tgs ignoredTag) <*> | ||
(pure $ isNewMail tgs ignoredTag) | ||
|
||
tagsToText :: [Tag] -> T.Text -> [T.Text] | ||
tagsToText t ignored = filter (/= ignored) $ decodeUtf8 <$> t | ||
pure tgs' <*> | ||
messageId m | ||
|
||
getDatabasePath :: IO (FilePath) | ||
getDatabasePath = getFromNotmuchConfig "database.path" | ||
|
@@ -59,5 +102,5 @@ getFromNotmuchConfig key = do | |
stdout <- readProcess cmd args [] | ||
pure $ filter (/= '\n') stdout | ||
|
||
isNewMail :: [Tag] -> T.Text -> Bool | ||
isNewMail t newTag = newTag `elem` (decodeUtf8 <$> t) | ||
mailIsNew :: T.Text -> NotmuchMail -> Bool | ||
mailIsNew ignoredTag m = ignoredTag `elem` (view mailTags m) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still not right IMHO... it would currently crash the application if I wouldn't be able the open the database properly. Arguably that's pretty bad in itself, but perhaps the application shouldn't crash. Same for the function which opens the database RW for tagging. Any suggestions or leave it for now and keep a backlog item.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leave for now.