diohscrc.sample: fix GMIR alias
[diohsc.git] / History.hs
blobd234797ba0433014c465b197fb21548b5d262d94
1 -- This file is part of Diohsc
2 -- Copyright (C) 2020-23 Martin Bays <mbays@sdf.org>
3 --
4 -- This program is free software: you can redistribute it and/or modify
5 -- it under the terms of version 3 of the GNU General Public License as
6 -- published by the Free Software Foundation, or any later version.
7 --
8 -- You should have received a copy of the GNU General Public License
9 -- along with this program. If not, see http://www.gnu.org/licenses/.
11 {-# LANGUAGE OverloadedStrings #-}
13 module History where
14 import Data.List (find)
15 import Time.Types (ElapsedP)
17 import qualified Codec.MIME.Type as MIME
18 import qualified Data.Text.Encoding.Error as T
19 import qualified Data.Text.Lazy.Encoding as T
21 import GeminiProtocol
22 import Request
23 import TextGemini
24 import URI
26 data HistoryChild = HistoryChild
27 { childItem :: HistoryItem
28 , childLink :: Maybe Int
31 data HistoryOrigin = HistoryOrigin
32 { originItem :: HistoryItem
33 , originLink :: Maybe Int
36 data HistoryItem = HistoryItem
37 { historyRequest :: Request
38 , historyRequestTime :: ElapsedP
39 , historyMimedData :: MimedData
40 , historyGeminatedMimedData :: MimedData -- ^generated with lazy IO
41 , historyParent :: Maybe HistoryItem
42 , historyChild :: Maybe HistoryChild
45 historyUri :: HistoryItem -> URI
46 historyUri = requestUri . historyRequest
48 historyAncestors :: HistoryItem -> [HistoryItem]
49 historyAncestors i = case historyParent i of
50 Nothing -> []
51 Just i' -> i' : historyAncestors i'
53 historyDescendants :: HistoryItem -> [HistoryItem]
54 historyDescendants i = case historyChild i of
55 Nothing -> []
56 Just (HistoryChild i' _) -> i' : historyDescendants i'
58 pathItemByUri :: HistoryItem -> URI -> Maybe HistoryItem
59 pathItemByUri i uri = find ((uri ==) . historyUri) $
60 historyAncestors i ++ [i] ++ historyDescendants i
62 historyLinks :: HistoryItem -> [Link]
63 historyLinks item = case historyGeminatedMimedData item of
64 MimedData (MIME.Type (MIME.Text "gemini") _) body ->
65 extractLinks . parseGemini $ T.decodeUtf8With T.lenientDecode body
66 _ -> []