Forum Discussion

bhupen's avatar
bhupen
Advisor
10 years ago

Is it possible to show only current day posts?

Hi guyz,

I got latest reply topic but I want to show the only current day's highest reply post?

 

Solution in my mind:

I got the post date, I want to compare post date with current date.  But iam not sure is this good approach or not. Please suggest:

 

<#assign messages = rest("2.0","/search?q=" + "SELECT * FROM messages WHERE replies.count(*)>0 AND depth=0 order by replies.count(*) desc LIMIT 3"?url) />
<#if messages.data.items??>
  <#list messages.data.items as recent >
    <#if recent?has_content>
       <span>${recent.post_time?datetime}</span>
      <br/>
        </#if>
  </#list>
</#if>

 

Query: how can I compare this date with current date.  although we can get current day with ${.now}

Please suggest. Is it possible to make check so that we can show only current highest reply post?

  • OlivierSPaoloT  Thanks for your help.  OlivierS specially thanks for your effort. I have achived it other way. Iam posting it here might be help someone else:

     

    <h3>Trending Post</h3> <br />
    <#assign apiVersion = "2.0"/>
    <#assign today = .now>
    <#assign dateToday = today?string("yyyy-MM-dd'T'HH:mm:ss'+00:00'")  />
        <#assign x= rest(apiVersion,"/search?q=" + "SELECT id, subject,post_time,author, view_href, replies.count(*) FROM messages WHERE
        post_time >= ${dateToday} AND board.id = '10031' AND depth=0 ORDER BY replies.count(*) DESC LIMIT 5"?url) />
        
    <#if x.data.items ?? >
        <#list x.data.items as message >
                <p><a href="${message.view_href}">${message.subject}</a></p>
                <span>${message.author.login}</span>
        </#list>
    </#if>    

    Thanks for your help all.

    Cheers!

7 Replies

  • bhupen - you'll need to create a function for this which will compare the dates for you. An overview is below:

     

    <#assign end_date = .now?long?replace(",","")?substring(0,10)> <!-- Converting current time from millisecs to epoch time-->
    
    <#assign start_date = (end_date?number - 86483)?replace(",","")?substring(0,10)> <!-- 86483 epoch for 1 day-->


    <#assign recent_threads = restadmin("/boards/id/${board_link.id}/search/messages?q=date%3A%5B${start_date}%20TO%20${end_date}%5D&restapi.response_style=view").messages/>

     

     You can then build on this.

  • bhupen's avatar
    bhupen
    Advisor
    10 years ago

    VarunGrazitti  I gave a try but it didnt work out for me. Its showing error to me:

    error:

    The following has evaluated to null or missing:
    ==> board_link

     

    Following is the chunk of code i have used:

     

    <#assign messages = rest("2.0","/search?q=" + "SELECT * FROM messages WHERE replies.count(*)>0 AND depth=0 order by replies.count(*) desc LIMIT 3"?url) />
    <#assign end_date = .now?long?replace(",","")?substring(0,10)> <!-- Converting current time from millisecs to epoch time-->
    <#assign start_date = (end_date?number - 86483)?replace(",","")?substring(0,10)> <!-- 86483 epoch for 1 day-->
    <#assign recent_threads = restadmin("/boards/id/${board_link.id}/search/messages?q=date%3A%5B${start_date}%20TO%20${end_date}%5D&has_kudos=${has_kudos}&restapi.response_style=view&page_size=10").messages/>
    <#setting locale="en_US">
    <#if messages.data.items??>
      <#list messages.data.items as recent >
        <#if recent?has_content>
          <a href="${recent.view_href}">${recent_threads.subject}</a>
          <span>${recent_threads.author.login}</span>
          <span>${recent_threads.post_time?datetime}</span>
          <br/>
        <#else>
          No post  found.
        </#if>
      </#list>
    </#if>

     

    also Iam not clear about this:
    %3A%5B${start_date}%20TO%20${end_date}%

     

    will it give me on current day post?

     

    Thanks

     

  • bhupen - it was our piece of code, the board_link is defined in ours, you need to pass the node you're in. Moreover, you seem to have mixed V1 andV2 again, you can but what you were trying to do was you picked data items from V2 but dates were in V1. Below is a code for V1, works fine. You need to pass the node and dates

     

    This will help for dates: http://www.epochconverter.com/

     

    <#assign end_date = .now?long?replace(",","")?substring(0,10)> <!-- Converting current time from millisecs to epoch time-->
    <#assign start_date = (end_date?number - 86483)?replace(",","")?substring(0,10)> <!-- 86483 epoch for 1 day-->
    <#assign recent_threads = restadmin("/boards/id/${YOUR_NODE_ID}/search/messages?q=date%3A%5B1391688278%20TO%201423224278%5D&restapi.response_style=view").messages/>
    <#list recent_threads.message as recent >
    
    <a href="#">${recent.subject}</a><br/>
    </#list>

     

    NOTE: I HAVE HARD CODED DATES IN THE QUERY, please pass it as per your needs using 

     

    /boards/id/${Your_node}/search/messages?q=date%3A%5B${start_date}%20TO%20${end_date}%5D&restapi.response_style=view&page_size=3

  • bhupen's avatar
    bhupen
    Advisor
    10 years ago

    VarunGrazitti   Thanks for your help.

     

    You had assigned start date and end date but I want it could be dynamic so that It will show only today's date post. also you made rest call but i want to get it done by using  API V2 which I have done in my query below. I stuck only in date format because Im not able to get only today's post. Is there any FTL or lithium database  solution where we can get only today's post on keeping mind my query:

     

    /*Working code only to show today's post */

    <#assign apiVersion = "2.0"/>
    <#assign x= rest(apiVersion,"/search?q=" + "SELECT id, subject,post_time,author, view_href, replies.count(*) FROM messages WHERE board.id = '10031' AND depth=0 ORDER BY replies.count(*) DESC LIMIT 5"?url) />
    <#list x.data.items as message >
    <p><a href="${message.view_href}">${message.subject}</a></p>
    <span>${message.author.login}</span>
    </#list>

     

    I also tried following idea to achive this but din't work:

    <#assign messages = rest("2.0","/search?q=" + "SELECT * FROM messages WHERE replies.count(*)>0 AND depth=0 AND DATE_FORMAT(date_time,'%b %d,%Y') = CURDATE() order by replies.count(*) desc LIMIT 3"?url) />

     

    I tried the date format but Iam not sure which database is using at the backend. I tried to achive it by using curdate() which is mysql function. But it din't work for me.

     

    Any idea is appreciatable.

     

    Thanks

     

  • OlivierS's avatar
    OlivierS
    Lithium Alumni (Retired)
    10 years ago

    bhupen 

     

    Ok, this was a tricky one ... This is probably not the only way to achieve it. And I would hardly advise to test, test and test it before rolling it out to production!

     

    The idea here is to loop through the messages by bunch of 10 using the API v2, LiQL and the OFFSET clause.

    I'm assuming the friendly date is set in a way we have 'x m ago' for post posted a minute ago and 'x h ago' for post posted x hour ago.

    If a message date friendly hasn't been posted in the last x minute or hour ago, we exit the loop.

     

    Note: if you don't have date friendly set on your system, it will display all the messages. So might be worth not setting the offsetmax value too high!

     

    <#assign offsetmax=100/>
    <#assign stoploop=false/>
    <#list 0..offsetmax as i>
    <#assign offset=10*i />
    ${offset}
    <#assign apiVersion = "2.0"/>
    <#assign query = "SELECT id, body, subject, view_href, post_time, post_time_friendly, replies.count(*), kudos.sum(weight), author, board FROM messages WHERE depth=0 ORDER BY conversation.last_post_time DESC LIMIT 10 OFFSET ${offset}" />
    <#assign topics = rest(apiVersion, "/search?q=" + query?url).data.items![] />
    <ul>
    <#list topics as message>
    <#if message.post_time_friendly??>
    <#if (message.post_time_friendly?contains("m ago")) || (message.post_time_friendly?contains("h ago"))>
    <a href="${message.view_href}">${message.subject}</a>&nbsp;[${message.post_time_friendly}]
    <#else>
    <#assign stoploop=true> 
    </#if>
    <#else>
    <a href="${message.view_href}">${message.subject}</a>&nbsp;[${message.post_time?datetime?string}]
    </#if>
    </#list>
    <ul>
    
    <#if stoploop==true> <#break> </#if>
    </#list>

     

     

  • bhupen's avatar
    bhupen
    Advisor
    10 years ago

    OlivierSPaoloT  Thanks for your help.  OlivierS specially thanks for your effort. I have achived it other way. Iam posting it here might be help someone else:

     

    <h3>Trending Post</h3> <br />
    <#assign apiVersion = "2.0"/>
    <#assign today = .now>
    <#assign dateToday = today?string("yyyy-MM-dd'T'HH:mm:ss'+00:00'")  />
        <#assign x= rest(apiVersion,"/search?q=" + "SELECT id, subject,post_time,author, view_href, replies.count(*) FROM messages WHERE
        post_time >= ${dateToday} AND board.id = '10031' AND depth=0 ORDER BY replies.count(*) DESC LIMIT 5"?url) />
        
    <#if x.data.items ?? >
        <#list x.data.items as message >
                <p><a href="${message.view_href}">${message.subject}</a></p>
                <span>${message.author.login}</span>
        </#list>
    </#if>    

    Thanks for your help all.

    Cheers!