2 Module : Gargantext.Core.Text.List.Social
4 Copyright : (c) CNRS, 2018-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
11 {-# LANGUAGE ScopedTypeVariables #-}
13 module Gargantext.Core.Text.List.Social
16 import Control.Monad (mzero)
19 import Data.HashMap.Strict (HashMap)
21 import Data.Monoid (mconcat)
22 import qualified Data.Scientific as Scientific
24 import qualified Data.Text as T
25 import qualified Data.Vector as V
26 import Gargantext.API.Ngrams.Tools
27 import Gargantext.API.Ngrams.Types
28 import Gargantext.Core.NodeStory
29 import Gargantext.Core.Text.List.Social.Find
30 import Gargantext.Core.Text.List.Social.History
31 import Gargantext.Core.Text.List.Social.Patch
32 import Gargantext.Core.Text.List.Social.Prelude
33 import Gargantext.Core.Types.Individu
34 import Gargantext.Database.Admin.Types.Node
35 import Gargantext.Database.Prelude
36 import Gargantext.Database.Query.Table.Node.Error
37 import Gargantext.Database.Query.Tree
38 import Gargantext.Database.Schema.Ngrams
39 import Gargantext.Prelude
40 import qualified Prelude as Prelude
42 ------------------------------------------------------------------------
43 ------------------------------------------------------------------------
46 -- | FlowSocialListPriority
47 -- Sociological assumption: either private or others (public) first
48 -- This parameter depends on the user choice
50 data FlowSocialListWith = FlowSocialListWithPriority { fslw_priority :: FlowSocialListPriority }
51 | FlowSocialListWithLists { fslw_lists :: [ListId] }
52 deriving (Show, Generic)
53 instance FromJSON FlowSocialListWith where
54 parseJSON (Object v) = do
55 typ :: T.Text <- v .: "type"
56 value <- v .:? "value" .!= []
58 "MyListsFirst" -> pure $ FlowSocialListWithPriority { fslw_priority = MySelfFirst }
59 "OtherListsFirst" -> pure $ FlowSocialListWithPriority { fslw_priority = OthersFirst }
60 "SelectedLists" -> pure $ FlowSocialListWithLists { fslw_lists = value }
61 _ -> pure $ FlowSocialListWithPriority { fslw_priority = MySelfFirst }
63 instance ToJSON FlowSocialListWith where
64 toJSON (FlowSocialListWithPriority { fslw_priority = MySelfFirst }) =
65 object [ ("type", String "MyListsFirst") ]
66 toJSON (FlowSocialListWithPriority { fslw_priority = OthersFirst }) =
67 object [ ("type", String "ListsFirst") ]
68 toJSON (FlowSocialListWithLists { fslw_lists = ids }) =
69 object [ ("type", String "SelectedLists")
70 , ("value", Array $ V.fromList $ (map (\(NodeId id) -> Number $ Scientific.scientific (Prelude.toInteger id) 1) ids)) ]
71 instance ToSchema FlowSocialListWith where
72 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
74 data FlowSocialListPriority = MySelfFirst | OthersFirst
75 deriving (Show, Generic)
76 instance ToSchema FlowSocialListPriority where
77 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
79 flowSocialListPriority :: FlowSocialListPriority -> [NodeMode]
80 flowSocialListPriority MySelfFirst = [Private{-, Shared, Public -}]
81 flowSocialListPriority OthersFirst = reverse $ flowSocialListPriority MySelfFirst
84 -- | We keep the parents for all ngrams but terms
85 keepAllParents :: NgramsType -> KeepAllParents
86 keepAllParents NgramsTerms = KeepAllParents False
87 keepAllParents _ = KeepAllParents True
90 ------------------------------------------------------------------------
91 flowSocialList :: ( HasNodeStory env err m
96 => Maybe FlowSocialListWith
99 -> FlowCont NgramsTerm FlowListScores
100 -> m (FlowCont NgramsTerm FlowListScores)
101 flowSocialList Nothing u = flowSocialList' MySelfFirst u
102 flowSocialList (Just (FlowSocialListWithPriority p)) u = flowSocialList' p u
103 flowSocialList (Just (FlowSocialListWithLists ls)) _ = getHistoryScores ls History_User
105 flowSocialList' :: ( HasNodeStory env err m
110 => FlowSocialListPriority
111 -> User -> NgramsType
112 -> FlowCont NgramsTerm FlowListScores
113 -> m (FlowCont NgramsTerm FlowListScores)
114 flowSocialList' flowPriority user nt flc =
115 mconcat <$> mapM (flowSocialListByMode' user nt flc)
116 (flowSocialListPriority flowPriority)
119 flowSocialListByMode' :: ( HasNodeStory env err m
124 => User -> NgramsType
125 -> FlowCont NgramsTerm FlowListScores
127 -> m (FlowCont NgramsTerm FlowListScores)
128 flowSocialListByMode' user' nt' flc' mode =
129 findListsId user' mode
130 >>= flowSocialListByModeWith nt' flc'
133 flowSocialListByModeWith :: ( HasNodeStory env err m
139 -> FlowCont NgramsTerm FlowListScores
141 -> m (FlowCont NgramsTerm FlowListScores)
142 flowSocialListByModeWith nt'' flc'' listes =
143 getHistoryScores listes History_User nt'' flc''
146 -----------------------------------------------------------------
147 getHistoryScores :: ( HasNodeStory env err m
155 -> FlowCont NgramsTerm FlowListScores
156 -> m (FlowCont NgramsTerm FlowListScores)
157 getHistoryScores lists hist nt fl =
158 addScorePatches nt lists fl <$> getHistory hist nt lists
160 getHistory :: ( HasNodeStory env err m
168 -> m (Map ListId (Map NgramsType [HashMap NgramsTerm NgramsPatch]))
169 getHistory hist nt listes =
170 history hist [nt] listes <$> getRepo' listes