~jackwines/discourse-tui

ref: 9a30b6e7282224a8bfba319347d36dd68aeddcd7 discourse-tui/src/Types.hs -rw-r--r-- 3.8 KiB
9a30b6e7 — Jack Wines selected posts have a better styed marker 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
module Types where

import Data.Aeson
import Cursor.Simple.List.NonEmpty (NonEmptyCursor)
import qualified Data.IntMap.Strict as M
import Brick.Widgets.List
import GHC.Generics
import Control.Lens
import Control.Lens.TH

instance FromJSON Topic where
    parseJSON = withObject "Topic" $ \v -> do 
        topicId' <- v .: "id"
        title'  <- v .: "title"
        likeCount' <- v.: "like_count"
        postsCount' <- v.: "posts_count"
        posters' <- v.: "posters"
        pinned' <- v.: "pinned"
        categoryId' <- v.: "category_id"
        return $ Topic topicId' categoryId' title' likeCount' postsCount' posters' pinned'

instance FromJSON User where
    parseJSON (Object v) = User
            <$> v .: "id"
            <*> v .: "username"
            <*> v .: "name"

instance FromJSON TopicResponse where
    parseJSON = withObject "TopicResponse" $ \v -> do
           users' <- v .: "users"
           topicList' <- v .: "topic_list"
           topics' <- topicList' .: "topics"
           return $ TopicResponse users' topics'

instance FromJSON Poster where
    parseJSON (Object v) = Poster
           <$> v .: "user_id"
           <*> v .: "description"

instance FromJSON Category where
    parseJSON (Object v) = Category
        <$> v .: "id"
        <*> v .: "name"

instance FromJSON CategoryResponse where
    parseJSON = withObject "CategoryResponse" $ \v -> do
        categoryList <- v .: "category_list"
        categories' <- categoryList .: "categories"
        return $ CategoryResponse categories'

instance FromJSON PostResponse where
    parseJSON = withObject "PastResponse" $ \v -> do
        postStream <- v .: "post_stream"
        posts' <- postStream .: "posts"
        return $ PostResponse posts'

instance FromJSON Post where
    parseJSON = withObject "Post" $ \v -> do
        id' <- v .: "id"
        username' <- v .: "username"
        cooked' <- v .: "cooked"
        actions <- v .: "actions_summary"
        return $ Post id' username' cooked' (if null actions then 0 else _count . head $ actions)

instance FromJSON Action where
    parseJSON (Object v) = Action
        <$> v .: "id"
        <*> v .: "count"

newtype CategoryResponse = CategoryResponse
    {
    _categories :: [Category]
    } deriving (Show)

data Action = Action
    {
    _actionId :: Int,
    _count :: Int
    } deriving (Show)

data TopicResponse = TopicResponse
    {
    _users :: [User],
    _topicList :: [Topic]
    } deriving (Show)

topicHeight :: Int
topicHeight = 4

data Topic = Topic
    {
    _topicId :: Int,
    _categoryID :: Int,
    _title :: String,
    _likeCount :: Int,
    _postsCount :: Int,
    _posters :: [Poster],
    _pinned :: Bool
    } deriving (Show)


data User = User
    {
    _userId :: Int,
    _userName :: String,
    _realName :: String
    } deriving (Show)


data Category = Category
    {
    _categoryId :: Int,
    _categoryName :: String
    } deriving (Show)

data Poster = Poster
    {
    _posterId :: Int,
    _description :: String
    } deriving (Show)

data PostResponse = PostResponse
    {
    _postList :: [Post]                         
    } deriving (Show)


data Post = Post
    {
    _postId :: Int,
    _opUserName :: String,
    _contents :: String,
    _likes :: Int
    } deriving (Show)

data TuiState = TuiState
    {
    _topics :: List String Topic,
    _posts :: Maybe (List String Post), -- Nothing if not in post view
    _userMap :: M.IntMap User,
    _categoryMap :: M.IntMap Category,
    _baseURL :: String,
    _singlePostView :: Bool -- if we're looking at the full contents of one post
    } deriving (Show)

type ResourceName = String

makeLenses ''CategoryResponse
makeLenses ''Post
makeLenses ''PostResponse
makeLenses ''Category
makeLenses ''Poster
makeLenses ''Topic
makeLenses ''TopicResponse
makeLenses ''Action
makeLenses ''User
makeLenses ''TuiState