Detecting a user action on a custom menu to insert cards in a bundle

大兔子大兔子 提交于 2019-12-13 07:35:01

问题


I have a bundle with just a cover that i have inserted in my timeline using mirror API. Now what i want is when a user clicks on the bundle, i get a custom menu clicking on which the backend is called to insert a set of cards again in the same bundle.

public class newsfeedbliss {
    static String bundleId = "lunchRoulette" + UUID.randomUUID();
    private static ArrayList<String> newstext = new ArrayList<String>();

    static final String PROD_BASE_URL = "https://newsfeedbliss.appspot.com";
      private static final String PROD_CALLBACK = PROD_BASE_URL + "/newsfeedcallback";
      private static final String TEST_CALLBACK = "https://newsfeedbliss.appspot.com/newsfeedcallback";

     public static void subscribe( HttpServletRequest req, String userId )
              throws IOException
          {
            Mirror mirror = MirrorUtils.getMirror( req );

            // START:subscribe

            final String callbackUrl = "https://newsfeedbliss.appspot.com/newsfeedcallback";
            Subscription tliSubscription = new Subscription()
              .setCallbackUrl( callbackUrl )
              .setVerifyToken( "a_secret_to_everybody" )
              .setUserToken( userId )
              .setCollection( "timeline" )
              .setOperation( Collections.singletonList( "UPDATE" ) );

            mirror.subscriptions().insert( tliSubscription ).execute();
            // END:subscribe

            // TODO: check if this user has subscribed, skip if already has
            SubscriptionsListResponse subscriptions = mirror.subscriptions().list().execute();
            for (Subscription sub : subscriptions.getItems()) {
              System.out.println( sub );
            }
          }



          public static TimelineItem buildarticlestimeline(
                  ServletContext ctx, String userId )
throws IOException, ServletException, ParserConfigurationException, SAXException
{
              Mirror mirror = MirrorUtils.getMirror( userId );
                Timeline timeline1 = mirror.timeline();
                TimelineItem timelineItem1 = new TimelineItem()
                .setText("Hello");
                timeline1.insert( timelineItem1 ).executeAndDownloadTo( System.out );

            return timelineItem1;
            }


    public static void insertSimpleTextTimelineItem( HttpServletRequest req )
            throws IOException, ParserConfigurationException, SAXException
            {
            Mirror mirror = MirrorUtils.getMirror( req );
            Timeline timeline = mirror.timeline();
            TimelineItem timelineItem = new TimelineItem()
            .setHtml("<article>\n  <section>\n    <p class=\"text-auto-size\">This <em class=\"yellow\">paragraph</em> auto-resizes according to the <strong class=\"blue\">HTML</strong> content length.\n    </p>\n  </section>\n</article>\n")
            .setBundleId(bundleId)
            .setIsBundleCover(true);
            setSimpleMenuItems(timelineItem,true);
            timeline.insert( timelineItem ).executeAndDownloadTo( System.out );
            System.out.println("Hello hello");

            }


    public static void setSimpleMenuItems( TimelineItem ti, boolean hasRestaurant ) {
        // Add blank menu list
        ti.setMenuItems( new LinkedList<MenuItem>() );
        ti.getMenuItems().add( new MenuItem().setAction( "READ_ALOUD" ) );
        ti.getMenuItems().add( new MenuItem().setAction( "DELETE" ) );
        List<MenuValue> menuValues = new ArrayList<MenuValue>(2);
          menuValues.add( new MenuValue()
            .setState( "DEFAULT" )
            .setDisplayName( "Alternative" )
            // .setIconUrl( "" )
          );
          menuValues.add( new MenuValue()
            .setState( "PENDING" )
            .setDisplayName( "Generating Alternative" ) );

          ti.getMenuItems().add( new MenuItem()
            .setAction( "CUSTOM" )
              .setId( "ALT" )
              .setPayload( "ALT" )
              .setValues( menuValues )
          );
        }
}

This is my servlet file

    public class NewsfeedblissServlet extends HttpServlet
{
    private static final Logger log = Logger.getLogger(NewsfeedblissServlet.class.getName());
/** Accept an HTTP GET request, and write a random lunch type. */

public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException
{

    log.info("in do get");
    try {
        newsfeedbliss.insertSimpleTextTimelineItem( req );
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    log.info("called insert text");
    resp.setContentType("text/html");
    resp.getWriter().append( "Inserted Timeline Item" );
}
}

And this is the class i have written that has the code that i want to run on callback that detects custom menu click and inserts the cards.

    public class TimelineUpdateServlet extends HttpServlet
{
protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
{
    System.out.println("Hey, Hello");
    res.getWriter().append( "Inside update servlet" );
// Generate Notification from request body
JsonFactory jsonFactory = new JacksonFactory();
Notification notification =
jsonFactory.fromInputStream( req.getInputStream(), Notification.class );
// Get this user action's type
String userActionType = null;
if( !notification.getUserActions().isEmpty() )
userActionType = notification.getUserActions().get(0).getType();
//If this is a pinned timeline item, log who and which timeline item

if( "timeline".equals( notification.getCollection() )
&& "UPDATE".equals( notification.getOperation() )
&& "CUSTOM".equals( userActionType ) )
{
    UserAction userAction = notification.getUserActions().get(0);
    if( "ALT".equals( userAction.getPayload() ) )
    {
    // Add a new timeline item, and bundle it to the previous one
    String userId = notification.getUserToken();
    String itemId = notification.getItemId();
    Mirror mirror = MirrorUtils.getMirror( userId );
    Timeline timeline = mirror.timeline();
    // Get the timeline item that owns the tapped menu
    TimelineItem current = timeline.get( itemId ).execute();
    String bundleId = current.getBundleId();
    // If not a bundle, update this item as a bundle
    if( bundleId == null ) {
    bundleId = "lunchRoulette" + UUID.randomUUID();
    current.setBundleId( bundleId );
    timeline.update( itemId, current).execute();
    }
    // Create a new random restaurant suggestion
    TimelineItem newTi=null;
    try {
        newTi = newsfeedbliss.buildarticlestimeline( getServletContext(), userId );
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    newTi.setBundleId( bundleId );
    timeline.insert( newTi ).execute();

}
}
}
}

回答1:


In your TimelineUpdateServlet code you must modify your original item to setIsBundleCover(true). The bundle cover will not have any menu items as it only acts as a parent for the items below it. Thus call

current.setIsBundleCover(true);

before you call

timeline.update( itemId, current).execute();

This should allow the new card to appear in the bundle properly.



来源:https://stackoverflow.com/questions/22755140/detecting-a-user-action-on-a-custom-menu-to-insert-cards-in-a-bundle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!