#13 Add visibility information to channels #21

Merged
MasseR merged 2 commits from sandbox/MasseR/13-channel-visibility into master 2018-08-15 22:38:36 +03:00
3 changed files with 16 additions and 5 deletions
Showing only changes of commit 00a7e3d524 - Show all commits

View File

@ -0,0 +1 @@
alter table channels add column visibility text NOT NULL default 'Private';

View File

@ -22,7 +22,7 @@ userChannels username = fromRels <$> query q
where where
q = do q = do
userId :*: _ :*: username' :*: _ <- select (gen users) userId :*: _ :*: username' :*: _ <- select (gen users)
channel@(_ :*: _ :*: owner) <- select (gen channels) channel@(_ :*: _ :*: owner :*: _) <- select (gen channels)
restrict (owner .== userId) restrict (owner .== userId)
restrict (username' .== literal username) restrict (username' .== literal username)
return channel return channel
@ -31,9 +31,9 @@ insertChannel :: (MonadMask m, MonadIO m) => Username -> Text -> SeldaT m ()
insertChannel username channel = do insertChannel username channel = do
mUserId <- listToMaybe <$> getUser mUserId <- listToMaybe <$> getUser
void $ forM mUserId $ \userId -> void $ forM mUserId $ \userId ->
insertUnless (gen channels) (doesNotExist userId) [ def :*: channel :*: userId ] insertUnless (gen channels) (doesNotExist userId) [ def :*: channel :*: userId :*: def ]
where where
doesNotExist userId (_ :*: channel' :*: userId') = channel' .== literal channel .&& userId' .== literal userId doesNotExist userId (_ :*: channel' :*: userId' :*: _) = channel' .== literal channel .&& userId' .== literal userId
getUser = query $ do getUser = query $ do
userId :*: _ :*: user :*: _ <- select (gen users) userId :*: _ :*: user :*: _ <- select (gen users)
restrict (user .== literal username) restrict (user .== literal username)
@ -62,7 +62,7 @@ attachChannel username bookId channel = do
return channelId' return channelId'
channelQ = do channelQ = do
userId :*: _ :*: username' :*: _ <- select (gen users) userId :*: _ :*: username' :*: _ <- select (gen users)
ch@(_ :*: channel' :*: owner) <- select (gen channels) ch@(_ :*: channel' :*: owner :*: _) <- select (gen channels)
restrict (username' .== literal username) restrict (username' .== literal username)
restrict (owner .== userId) restrict (owner .== userId)
restrict (channel' .== literal channel) restrict (channel' .== literal channel)

View File

@ -124,9 +124,19 @@ data Tag = Tag { identifier :: TagID
, owner :: UserID } , owner :: UserID }
deriving (Show, Generic) deriving (Show, Generic)
data Visibility = Public | Private | Followers
deriving (Show, Read, Generic)
instance SqlType Visibility where
mkLit = LCustom . LText . pack . show
fromSql (SqlString x) = fromMaybe (error "fromSql: Not a valid visibility token") . readMay . unpack $ x
fromSql _ = error "fromSql: Not a valid visibility token"
defaultValue = mkLit Private
data Channel = Channel { identifier :: ChannelID data Channel = Channel { identifier :: ChannelID
, channel :: Text , channel :: Text
, owner :: UserID } , owner :: UserID
, visibility :: Visibility }
deriving (Show, Generic) deriving (Show, Generic)
tags :: GenTable Tag tags :: GenTable Tag