2016-05-19 21:06:27 +09:00
|
|
|
{- This file is part of Vervis.
|
|
|
|
-
|
2019-03-16 01:36:02 +09:00
|
|
|
- Written in 2016, 2019 by fr33domlover <fr33domlover@riseup.net>.
|
2016-05-19 21:06:27 +09:00
|
|
|
-
|
|
|
|
- ♡ Copying is an act of love. Please copy, reuse and share.
|
|
|
|
-
|
|
|
|
- The author(s) have dedicated all copyright and related and neighboring
|
|
|
|
- rights to this software to the public domain worldwide. This software is
|
|
|
|
- distributed without any warranty.
|
|
|
|
-
|
|
|
|
- You should have received a copy of the CC0 Public Domain Dedication along
|
|
|
|
- with this software. If not, see
|
|
|
|
- <http://creativecommons.org/publicdomain/zero/1.0/>.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Vervis.Discussion
|
2019-03-20 17:07:37 +09:00
|
|
|
( MessageTreeNodeAuthor (..)
|
|
|
|
, MessageTreeNode (..)
|
|
|
|
, getDiscussionTree
|
2016-05-19 21:06:27 +09:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Prelude
|
|
|
|
|
2016-05-20 00:49:39 +09:00
|
|
|
import Control.Arrow (second)
|
2016-05-19 21:06:27 +09:00
|
|
|
import Data.Graph.Inductive.Graph (mkGraph, lab')
|
|
|
|
import Data.Graph.Inductive.PatriciaTree (Gr)
|
|
|
|
import Data.Graph.Inductive.Query.DFS (dffWith)
|
|
|
|
import Data.Maybe (isNothing, mapMaybe)
|
2016-05-20 00:49:39 +09:00
|
|
|
import Data.Tree (Forest)
|
|
|
|
import Database.Esqueleto hiding (isNothing)
|
2016-05-19 21:06:27 +09:00
|
|
|
import Yesod.Persist.Core (runDB)
|
|
|
|
|
|
|
|
import qualified Data.HashMap.Lazy as M (fromList, lookup)
|
|
|
|
|
2019-03-20 17:07:37 +09:00
|
|
|
import Network.FedURI
|
|
|
|
|
2016-05-19 21:06:27 +09:00
|
|
|
import Data.Tree.Local (sortForestOn)
|
|
|
|
import Vervis.Foundation
|
|
|
|
import Vervis.Model
|
|
|
|
|
2019-03-20 17:07:37 +09:00
|
|
|
data MessageTreeNodeAuthor
|
|
|
|
= MessageTreeNodeLocal LocalMessageId Sharer
|
|
|
|
| MessageTreeNodeRemote FedURI
|
|
|
|
|
|
|
|
data MessageTreeNode = MessageTreeNode
|
|
|
|
{ mtnMessageId :: MessageId
|
|
|
|
, mtnMessage :: Message
|
|
|
|
, mtnAuthor :: MessageTreeNodeAuthor
|
|
|
|
}
|
|
|
|
|
|
|
|
getMessages :: AppDB DiscussionId -> Handler [MessageTreeNode]
|
|
|
|
getMessages getdid = runDB $ do
|
2016-05-20 07:40:54 +09:00
|
|
|
did <- getdid
|
2019-03-20 17:07:37 +09:00
|
|
|
l <- select $ from $ \ (lm `InnerJoin` m `InnerJoin` p `InnerJoin` s) -> do
|
|
|
|
on $ p ^. PersonIdent ==. s ^. SharerId
|
|
|
|
on $ lm ^. LocalMessageAuthor ==. p ^. PersonId
|
|
|
|
on $ lm ^. LocalMessageRest ==. m ^. MessageId
|
|
|
|
where_ $ m ^. MessageRoot ==. val did
|
|
|
|
return (m, lm ^. LocalMessageId, s)
|
|
|
|
r <- select $ from $ \ (rm `InnerJoin` m `InnerJoin` rs `InnerJoin` i) -> do
|
2019-04-12 09:56:27 +09:00
|
|
|
on $ rs ^. RemoteActorInstance ==. i ^. InstanceId
|
|
|
|
on $ rm ^. RemoteMessageAuthor ==. rs ^. RemoteActorId
|
2019-03-20 17:07:37 +09:00
|
|
|
on $ rm ^. RemoteMessageRest ==. m ^. MessageId
|
|
|
|
where_ $ m ^. MessageRoot ==. val did
|
2019-04-12 09:56:27 +09:00
|
|
|
return (m, i ^. InstanceHost, rs ^. RemoteActorIdent)
|
2019-03-20 17:07:37 +09:00
|
|
|
return $ map mklocal l ++ map mkremote r
|
|
|
|
where
|
|
|
|
mklocal (Entity mid m, Value lmid, Entity _ s) =
|
|
|
|
MessageTreeNode mid m $ MessageTreeNodeLocal lmid s
|
|
|
|
mkremote (Entity mid m, Value h, Value lu) =
|
|
|
|
MessageTreeNode mid m $ MessageTreeNodeRemote $ l2f h lu
|
2016-05-19 21:06:27 +09:00
|
|
|
|
2019-03-20 17:07:37 +09:00
|
|
|
discussionTree :: [MessageTreeNode] -> Forest MessageTreeNode
|
2016-05-20 00:49:39 +09:00
|
|
|
discussionTree mss =
|
2019-03-16 01:36:02 +09:00
|
|
|
let nodes = zip [1..] mss
|
2019-03-20 17:07:37 +09:00
|
|
|
mkEntry n mtn = (mtnMessageId mtn, n)
|
2019-03-16 01:36:02 +09:00
|
|
|
nodeMap = M.fromList $ map (uncurry mkEntry) nodes
|
2019-03-20 17:07:37 +09:00
|
|
|
mkEdge n mtn =
|
|
|
|
case messageParent $ mtnMessage mtn of
|
2016-05-19 21:06:27 +09:00
|
|
|
Nothing -> Nothing
|
|
|
|
Just mid ->
|
|
|
|
case M.lookup mid nodeMap of
|
|
|
|
Nothing -> error "message parent not in discussion"
|
|
|
|
Just p -> Just (p, n, ())
|
|
|
|
edges = mapMaybe (uncurry mkEdge) nodes
|
2019-03-20 17:07:37 +09:00
|
|
|
graph = mkGraph nodes edges :: Gr MessageTreeNode ()
|
|
|
|
roots =
|
|
|
|
[n | (n, mtn) <- nodes, isNothing $ messageParent $ mtnMessage mtn]
|
2016-05-19 21:06:27 +09:00
|
|
|
in dffWith lab' roots graph
|
|
|
|
|
2019-03-20 17:07:37 +09:00
|
|
|
sortByTime :: Forest MessageTreeNode -> Forest MessageTreeNode
|
|
|
|
sortByTime = sortForestOn $ messageCreated . mtnMessage
|
2016-05-19 21:06:27 +09:00
|
|
|
|
|
|
|
-- | Get the tree of messages in a given discussion, with siblings sorted from
|
|
|
|
-- old to new.
|
2019-03-20 17:07:37 +09:00
|
|
|
getDiscussionTree :: AppDB DiscussionId -> Handler (Forest MessageTreeNode)
|
2016-05-20 07:40:54 +09:00
|
|
|
getDiscussionTree getdid = sortByTime . discussionTree <$> getMessages getdid
|