我有一个只有封面的捆绑,我已经使用镜像API插入了我的时间轴。现在我想要的是当用户点击捆绑包时,我得到一个自定义菜单,点击后端被调用以在同一捆绑中再次插入一组卡片。
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 )
);
}
}
这是我的servlet文件
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" );
}
}
这是我编写的类,我想在回调上运行代码,检测自定义菜单点击并插入卡片。
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();
}
}
}
}
答案 0 :(得分:2)
在TimelineUpdateServlet代码中,您必须将原始项目修改为setIsBundleCover(true)。捆绑封面不包含任何菜单项,因为它仅作为其下方项目的父项。这样叫
current.setIsBundleCover(true);
之前打电话
timeline.update( itemId, current).execute();
这应该允许新卡正确地出现在捆绑包中。