Skip to content

Commit

Permalink
Add complete thread hiding
Browse files Browse the repository at this point in the history
  • Loading branch information
ahushh committed Aug 31, 2013
1 parent ce5097f commit f151f5b
Show file tree
Hide file tree
Showing 15 changed files with 155 additions and 20 deletions.
32 changes: 32 additions & 0 deletions Handler/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Handler.Api where

import Import
import Yesod.Auth
import qualified Data.Text as T (concat)
---------------------------------------------------------------------------------------------------------
getPostsHelper :: YesodDB App [Entity Post] -> -- ^ Post selector: selectList [...] [...]
YesodDB App [Entity Post] -> -- ^ Post selector: selectList [...] [...]
Expand Down Expand Up @@ -125,3 +126,34 @@ getApiPostR board postId = do
selectRep $ do
provideRep $ bareLayout widget
provideJson postAndFiles
---------------------------------------------------------------------------------------------------------
getApiHideThread :: Text -> Int -> Handler TypedContent
getApiHideThread board threadId = do
ht <- lookupSession "hidden-threads"
case ht of
Just xs' ->
let xs = read (unpack xs') :: [(Text,[Int])]
ys = fromMaybe [] $ lookup board xs
zs = filter ((/=board).fst) xs
new = pack $ show ((board, threadId:ys):zs)
in setSession "hidden-threads" new
Nothing -> setSession "hidden-threads" $ T.concat ["[(",board,",[",pack (show threadId),"])]"]
selectRep $ do
provideRep $ bareLayout [whamlet|ok|]
provideJson $ object [("ok", "hidden")]

getApiUnhideThread :: Text -> Int -> Handler TypedContent
getApiUnhideThread board threadId = do
ht <- lookupSession "hidden-threads"
case ht of
Just xs' ->
let xs = read (unpack xs') :: [(Text,[Int])]
ys = fromMaybe [] $ lookup board xs
zs = filter ((/=board).fst) xs
ms = filter (/=threadId) ys
new = pack $ show (if null ms then zs else (board, ms):zs)
in setSession "hidden-threads" new
Nothing -> setSession "hidden-threads" "[]"
selectRep $ do
provideRep $ bareLayout [whamlet|ok|]
provideJson $ object [("ok", "showed")]
26 changes: 15 additions & 11 deletions Handler/Board.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,22 @@ getBoardNoPageR board = getBoardR board 0
postBoardNoPageR :: Text -> Handler Html
postBoardNoPageR board = postBoardR board 0
---------------------------------------------------------------------------------------------------------
selectThreadsAndPreviews :: Text ->
Int ->
Int ->
Int ->
Text ->
[Permission] ->
selectThreadsAndPreviews :: Text -> -- ^ Board name
Int -> -- ^ Page
Int -> -- ^ Threads per page
Int -> -- ^ Previews per thread
Text -> -- ^ Poster ID
[Permission] -> -- ^ Permissions
[Int] -> -- ^ Hidden threads
Handler [( (Entity Post, [Entity Attachedfile])
, [(Entity Post, [Entity Attachedfile])]
, Int
)]
selectThreadsAndPreviews board page threadsPerPage previewsPerThread posterId permissions =
let selectThreadsAll = selectList [PostBoard ==. board, PostParent ==. 0, PostDeleted ==. False]
selectThreadsAndPreviews board page threadsPerPage previewsPerThread posterId permissions hiddenThreads =
let selectThreadsAll = selectList [PostBoard ==. board, PostParent ==. 0, PostDeleted ==. False, PostLocalId /<-. hiddenThreads]
[Desc PostSticked, Desc PostBumped, LimitTo threadsPerPage, OffsetBy $ page*threadsPerPage]
selectThreadsHB = selectList ( [PostBoard ==. board, PostParent ==. 0, PostDeleted ==. False, PostHellbanned ==. False] ||.
selectThreadsHB = selectList ( [PostBoard ==. board, PostParent ==. 0, PostDeleted ==. False, PostHellbanned ==. False
,PostLocalId /<-. hiddenThreads] ||.
[PostBoard ==. board, PostParent ==. 0, PostDeleted ==. False
,PostHellbanned ==. True, PostPosterId ==. posterId]
)
Expand Down Expand Up @@ -76,7 +78,9 @@ getBoardR board page = do
let hasAccessToNewThread = checkAccessToNewThread mgroup boardVal
permissions = getPermissions mgroup
-------------------------------------------------------------------------------------------------------
numberOfThreads <- runDB $ count [PostBoard ==. board, PostParent ==. 0, PostDeleted ==. False, PostHellbanned ==. False]
hiddenThreads <- getHiddenThreads board
numberOfThreads <- runDB $ count [PostBoard ==. board, PostParent ==. 0, PostDeleted ==. False, PostHellbanned ==. False
,PostLocalId /<-. hiddenThreads]
posterId <- getPosterId
let numberFiles = boardNumberFiles boardVal
maxMessageLength = boardMaxMsgLength boardVal
Expand All @@ -87,7 +91,7 @@ getBoardR board page = do
boardLongDesc = boardLongDescription boardVal
geoIpEnabled = boardEnableGeoIp boardVal
pages = listPages threadsPerPage numberOfThreads
threadsAndPreviews <- selectThreadsAndPreviews board page threadsPerPage previewsPerThread posterId permissions
threadsAndPreviews <- selectThreadsAndPreviews board page threadsPerPage previewsPerThread posterId permissions hiddenThreads
-------------------------------------------------------------------------------------------------------
geoIps' <- forM (if geoIpEnabled then threadsAndPreviews else []) $ \((Entity tId t,_),ps,_) -> do
xs <- forM ps $ \(Entity pId p,_) -> getCountry (postIp p) >>= (\c' -> return (pId, c'))
Expand Down
1 change: 1 addition & 0 deletions Handler/Settings.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ getSettingsR = do

nameOfTheBoard <- extraSiteName <$> getExtra
msgrender <- getMessageRender
hiddenThreads <- getAllHiddenThreads
defaultLayout $ do
setTitle $ toHtml $ T.concat [nameOfTheBoard, titleDelimiter, msgrender MsgSettings]
$(widgetFile "settings")
Expand Down
14 changes: 14 additions & 0 deletions Import.hs
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,20 @@ getPosterId = do

getConfig :: forall b. (Config -> b) -> Handler b
getConfig f = f . entityVal . fromJust <$> (runDB $ selectFirst ([]::[Filter Config]) [])

getHiddenThreads :: Text -> Handler [Int]
getHiddenThreads board = do
ht <- lookupSession "hidden-threads"
case ht of
Just xs -> return $ fromMaybe [] $ lookup board (read (unpack xs) :: [(Text, [Int])])
Nothing -> setSession "hidden-threads" "[]" >> return []

getAllHiddenThreads :: Handler [(Text, [Int])]
getAllHiddenThreads = do
ht <- lookupSession "hidden-threads"
case ht of
Just xs -> return $ read $ unpack xs
Nothing -> setSession "hidden-threads" "[]" >> return []
-------------------------------------------------------------------------------------------------------------------
-- IP getter
-------------------------------------------------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Features
* Internationalization
* Post deletion by OP
* GeoIP support
* Thread hiding

Dependencies
------
Expand Down
3 changes: 2 additions & 1 deletion TODO.org
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@
*** DONE UTC -> local time, store timezone in cookies/session
CLOSED: [2013-08-10 Сб 19:45]
*** TODO Switchable youtube player
*** TODO post hiding
*** DONE thread hiding
CLOSED: [2013-08-31 Сб 22:09]
*** TODO favorite threads
* Further
** TODO best API error handling
Expand Down
2 changes: 2 additions & 0 deletions config/routes
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
/api/thread/#Text/#Int/last/#Int ApiLastPostsR GET
/api/thread/#Text/#Int/all ApiAllPostsR GET
/api/thread/#Text/#Int/deleted ApiDeletedPostsR GET
/api/hide/thread/#Text/#Int ApiHideThread GET
/api/unhide/thread/#Text/#Int ApiUnhideThread GET
/api/post/#Text/#Int ApiPostR GET
/jsonfrommsg/#Text JsonFromMsgR GET

Expand Down
8 changes: 8 additions & 0 deletions messages/en.msg
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,17 @@ ExpandPost: Show full.
ShrinkPost: Shrink message.

Sage: sage

######################################################################################
#### Thread hiding
######################################################################################
HideThread: Hide thread
HideThreadCompletely: Hide thread completely
ThreadIsHidden: Thread is hidden.
HiddenThreads: Hidden threads
Unhide: Раскрыть
ShowThread: Show thread

######################################################################################
#### Settings
######################################################################################
Expand Down
8 changes: 8 additions & 0 deletions messages/ru.msg
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,17 @@ ExpandPost: Полный текст.
ShrinkPost: Обрезать текст.

Sage: сажа

######################################################################################
#### Thread hiding
######################################################################################
HideThread: Скрыть тред
HideThreadCompletely: Скрыть тред полностью
ThreadIsHidden: Тред скрыт.
HiddenThreads: Скрытые треды
Unhide: Раскрыть
ShowThread: Показать тред

######################################################################################
#### Settings
######################################################################################
Expand Down
16 changes: 10 additions & 6 deletions templates/default-layout.julius
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ function initHiddenThreads() {
});
}

function hideThreadCompletely(threadId, board) {
$.getJSON("/api/hide/thread/"+board+"/"+threadId, function(data) {
$('#thread-'+threadId).hide();
});
}

function hideThread(threadId, board) {
var ths = JSON.parse(localStorage.getItem('hidden-threads'));
if (!ths) {
Expand All @@ -180,23 +186,21 @@ function hideThread(threadId, board) {
}
localStorage.setItem('hidden-threads', JSON.stringify(ths));

$('#thread-'+threadId).find('.op-post').hide();
$('#thread-'+threadId).find('.reply-post').hide();
$('#thread-'+threadId).find('.op-post, .reply-post').hide();
if ($('#thread-'+threadId).has('.hidden-thread').length) {
$('#thread-'+threadId).find('.hidden-thread').show()
} else {
$('#thread-'+threadId).append('<span class=hidden-thread> No. '+threadId+' '+#{toJSON $ msgrender MsgThreadIsHidden}+' <img class=icon-show-thread src="@{StaticR img_blank_gif}" onclick="showThread('+threadId+', \''+board+'\')" alt="'+#{toJSON $ msgrender MsgShowThread}+'" title="'+#{toJSON $ msgrender MsgShowThread}+'"></span>');
$('#thread-'+threadId).append('<span class=hidden-thread> No. '+threadId+' '+#{toJSON $ msgrender MsgThreadIsHidden}+' <img class=icon-show-thread src="@{StaticR img_blank_gif}" onclick="unhideThread('+threadId+', \''+board+'\')" alt="'+#{toJSON $ msgrender MsgShowThread}+'" title="'+#{toJSON $ msgrender MsgShowThread}+'"></span>');
}
}

function showThread(threadId,board) {
function unhideThread(threadId,board) {
var ths = JSON.parse(localStorage.getItem('hidden-threads'));
if (ths) {
delete ths[threadId+'-'+board];
localStorage.setItem('hidden-threads', JSON.stringify(ths));
}
$('#thread-'+threadId).find('.op-post').show();
$('#thread-'+threadId).find('.reply-post').show();
$('#thread-'+threadId).find('.op-post, .reply-post').show();
$('#thread-'+threadId).find('.hidden-thread').hide();
}
//-----------------------------------------------------------------
Expand Down
5 changes: 5 additions & 0 deletions templates/default-layout.lucius
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ hr {
background-position: -1037px -77px;
}

.icon-hide-thread-completely {
float: right;
background-position: -77px -29px;
}

.admin-table {
border-collapse: collapse;
font-size: 0.8em;
Expand Down
4 changes: 3 additions & 1 deletion templates/op-post.hamlet
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ $with thread <- show $ postLocalId $ entityVal eOpPostW
<span .reply-title>#{postTitle $ entityVal $ eOpPostW}
<span .poster-name>#{postName $ entityVal eOpPostW}
<span .time>#{myFormatTime tOffsetW $ postDate $ entityVal $ eOpPostW}
<img onclick="hideThread(#{thread}, '#{board}')" src=@{StaticR img_blank_gif} title=_{MsgHideThread} alt=_{MsgHideThread} .icon-hide-thread>
$if not $ isInThreadW
<img onclick="hideThread(#{thread}, '#{board}')" src=@{StaticR img_blank_gif} title=_{MsgHideThread} alt=_{MsgHideThread} .icon-hide-thread>
<img onclick="hideThreadCompletely(#{thread}, '#{board}')" src=@{StaticR img_blank_gif} title=_{MsgHideThreadCompletely} alt=_{MsgHideThreadCompletely} .icon-hide-thread-completely>
<div .thread-status>
$if postSticked $ entityVal $ eOpPostW
<img src=@{StaticR img_blank_gif} title=sticked alt=sticked .icon-thread-sticked>
Expand Down
22 changes: 21 additions & 1 deletion templates/settings.hamlet
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
<h2 style=text-align:center>_{MsgSettings}
<div style="margin-left:30px">
<form #settings-form method=post enctype=#{formEnctype} action=@{SettingsR}>
<div #settings-form-container>
<form #settings-form method=post enctype=#{formEnctype} action=@{SettingsR}>
^{formWidget}
<input type=submit value=_{MsgApply}>
<span style=display:none #settings-applied>
<div #hidden-threads-list>
$if not $ null hiddenThreads
<div #hidden-threads-title>
_{MsgHiddenThreads}:
$forall (b, ths) <- hiddenThreads
<div .hidden-threads-board #hidden-threads-board-#{b}>
<div #hidden-thread-board-title>
/#{b}/:
<table>
$forall t <- ths
<tr #hidden-thread-elem-#{b}-#{t}>
<td>
<a href="@{ThreadR b t}">
No. #{t}
<td>
[
<a onclick="unhideThreadCompletely(#{t},'#{b}')">
_{MsgUnhide}
]
13 changes: 13 additions & 0 deletions templates/settings.julius
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,16 @@ $('#settings-form').ajaxForm({
}
});

function unhideThreadCompletely(threadId, board) {
$.getJSON("/api/unhide/thread/"+board+"/"+threadId, function(data) {
$('#hidden-thread-elem-'+board+'-'+threadId).remove();
console.log($('#hidden-threads-board-'+board).find('td'));
if ($('#hidden-threads-board-'+board).find('td').length == 0) {
$('#hidden-threads-board-'+board).remove();
}
if ($('#hidden-threads-list').find('.hidden-threads-board').length == 0) {
$('#hidden-threads-list').remove();
}

});
}
20 changes: 20 additions & 0 deletions templates/settings.lucius
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#settings-form-container, #hidden-threads-list {
max-width: 40%;
display: table-cell;
padding: 0 15px;
}

.hidden-threads-board {
display: inline-table;
}

#hidden-threads-title {
text-align: center;
font-size: 1.2em;
margin: 5px;
}

#hidden-thread-board-title {
font-weight: bold;
text-align: center;
}

0 comments on commit f151f5b

Please sign in to comment.