@@ -16,7 +16,7 @@ module Data.PostElement (
1616 MkElementRow,
1717 PostElementRow(..),
1818 elementsToRows
19−) where
19+) where
2020
2121import Data.Int (Int64)
2222import qualified Generics.SOP as SOP
@@ -25,6 +25,7 @@ import Data.Text (Text)
2525import Data.Aeson
2626import Control.Applicative ((<|>))
2727import Servant.Docs (ToSample, toSamples, samples)
28+import Data.Sanitizer
2829
2930-- MARK: Documentation
3031
@@ -43,7 +44,14 @@ data PostElement a = Markdown Text
4344 | Quote PostQuote
4445 | Attachment Text
4546
46−instance ToJSON (PostElement t) where
47+instance {-# Overlapping #-} Sanitizable (PostElement t) where
48+ sanitize f (Markdown m) = Markdown (sanitize f m)
49+ sanitize f (Latex l) = Latex (sanitize f l)
50+ sanitize f (Image i) = Image (sanitize f i)
51+ sanitize f (Quote q) = Quote (sanitize f q)
52+ sanitize f (Attachment a) = Attachment (sanitize f a)
53+
54+instance ToJSON (PostElement t) where
4755 toJSON (Markdown m) = object ["markdown" .= m]
4856 toJSON (Latex l) = object ["latex" .= l]
4957 toJSON (Image i) = object ["image" .= i]
@@ -51,7 +59,7 @@ instance ToJSON (PostElement t) where
5159 toJSON (Attachment a) = object ["attachment" .= a]
5260
5361instance FromJSON (PostElement t) where
54− parseJSON = withObject "post element" $ \e ->
62+ parseJSON = withObject "post element" $ \e ->
5563 Markdown <$> e .: "markdown" <|>
5664 Latex <$> e .: "latex" <|>
5765 Image <$> e .: "image" <|>
@@ -75,7 +83,7 @@ rowsToElement :: MkElementRow (Maybe (PostElement p))
7583rowsToElement (Just m) Nothing Nothing Nothing Nothing = Just $ Markdown m
7684rowsToElement Nothing (Just l) Nothing Nothing Nothing = Just $ Latex l
7785rowsToElement Nothing Nothing (Just i) Nothing Nothing = Just $ Image i
78−-- rowsToElement Nothing Nothing Nothing (Just q) Nothing =
86+-- rowsToElement Nothing Nothing Nothing (Just q) Nothing =
7987-- fmap Quote $ M.lookup q pqrs >>= rowsToQuote erqs pqrs
8088rowsToElement Nothing Nothing Nothing Nothing (Just a) = Just $ Attachment a
8189rowsToElement _ _ _ _ _ = Nothing
@@ -98,7 +106,7 @@ elementsToRows :: Int64 -> [PostElement a] -> [PostElementRow a]
98106elementsToRows pId els = map (\(el, f) -> elemenToRow f el) . zip els . map (PostElementRow pId) $ [0..]
99107
100108
101−-- -- MARK: Quote
109+-- -- MARK: Quote
102110
103111data PostQuote = PostQuote {
104112 quoteId :: Int64,
@@ -106,13 +114,16 @@ data PostQuote = PostQuote {
106114 quotePostId :: Int64
107115}
108116
117+instance Sanitizable PostQuote where
118+ sanitize f (PostQuote qId qB qPId) = PostQuote qId (sanitize f qB) qPId
119+
109120instance ToJSON PostQuote where
110121 toJSON (PostQuote qId pb pId) = object [
111− "id" .= qId,
122+ "id" .= qId,
112123 "postId" .= pId,
113124 "quoteBody" .= pb ]
114125
115−
126+
116127-- -- MARK: PostQuoteRow
117128
118129-- data PostQuoteRow = PostQuoteRow {
@@ -122,7 +133,7 @@ instance ToJSON PostQuote where
122133
123134-- instance SOP.Generic PostQuoteRow
124135-- instance SOP.HasDatatypeInfo PostQuoteRow
125−
136+
126137-- rowsToQuote :: M.Map Int64 [ElementRow PostQuote] -> M.Map Int64 PostQuoteRow -> PostQuoteRow -> Maybe PostQuote
127138-- rowsToQuote erqs pqrs (PostQuoteRow qId pId) = do
128139-- elementRows <- M.lookup qId erqs