/src/com/foursquare/examples/push/util/Common.java

http://github.com/zackzachariah/Foursquare-Welcome-Screen · Java · 109 lines · 85 code · 19 blank · 5 comment · 9 complexity · 6be598ea29b839567ce8c413d503d8af MD5 · raw file

  1. package com.foursquare.examples.push.util;
  2. import java.util.Date;
  3. import java.util.List;
  4. import javax.jdo.JDOHelper;
  5. import javax.jdo.PersistenceManager;
  6. import javax.jdo.PersistenceManagerFactory;
  7. import com.foursquare.examples.push.models.LinkedUser;
  8. import com.google.appengine.api.channel.ChannelMessage;
  9. import com.google.appengine.api.channel.ChannelService;
  10. import com.google.appengine.api.channel.ChannelServiceFactory;
  11. import com.google.appengine.api.users.User;
  12. import com.google.appengine.api.users.UserServiceFactory;
  13. import fi.foyt.foursquare.api.FoursquareApi;
  14. public class Common {
  15. private final static String CLIENT_ID = "YOUR CLIENT ID";
  16. private final static String CLIENT_SECRET = "YOUR CLIENT SECRET";
  17. private final static String CALLBACK = "YOUR CLIENT CALLBACK";
  18. public final static String PUSH_SECRET = "YOUR CLIENT PUSH SECRET";
  19. public final static String TARGET_VENUE = "THE FOURSQUARE ID OF THE VENUE YOU MANAGE";
  20. public final static String TARGET_VENUE_NAME = "THE NAME OF THE VENUE YOU MANAGE";
  21. public static FoursquareApi getApi() { return getApi(null); }
  22. public static FoursquareApi getApi(String token) {
  23. FoursquareApi foursquareApi = new FoursquareApi(CLIENT_ID, CLIENT_SECRET, CALLBACK);
  24. if (token != null) foursquareApi.setoAuthToken(token);
  25. return foursquareApi;
  26. }
  27. public static FoursquareApi getCurrentApi(PersistenceManager pm) {
  28. User googler = getGoogleUser();
  29. if (googler != null) {
  30. LinkedUser luser = LinkedUser.loadOrCreate(pm, googler.getUserId());
  31. if (luser.foursquareAuth() != null) {
  32. return getApi(luser.foursquareAuth());
  33. }
  34. }
  35. return null;
  36. }
  37. // Convert the core aspects of a checkin to json
  38. public static String checkinToJson(String name, String photo, boolean isMayor) {
  39. StringBuilder sb = new StringBuilder();
  40. sb.append("{\"name\":\"");
  41. sb.append(name);
  42. sb.append("\",\"photo\":\"");
  43. sb.append(photo);
  44. sb.append("\",\"isMayor\":");
  45. sb.append(isMayor);
  46. sb.append("}");
  47. return sb.toString();
  48. }
  49. /* Methods for getting at aspects of persistence */
  50. private static final PersistenceManagerFactory pmfInstance =
  51. JDOHelper.getPersistenceManagerFactory("transactions-optional");
  52. private static PersistenceManagerFactory getPMF() {
  53. return pmfInstance;
  54. }
  55. public static PersistenceManager getPM() {
  56. return getPMF().getPersistenceManager();
  57. }
  58. /* Methods for user management tidbits */
  59. public static User getGoogleUser() {
  60. return UserServiceFactory.getUserService().getCurrentUser();
  61. }
  62. public static String getGoogleLoginUrl() {
  63. return UserServiceFactory.getUserService().createLoginURL("/#retryAuth");
  64. }
  65. public static String getFoursquareLoginUrl() {
  66. return "https://foursquare.com/oauth2/authenticate?client_id=" + CLIENT_ID +
  67. "&response_type=code&redirect_uri=" + CALLBACK;
  68. }
  69. /* Methods for handling Channel Client ID creation and understanding */
  70. public static String createChannelToken(String vid) {
  71. ChannelService cService = ChannelServiceFactory.getChannelService();
  72. return cService.createChannel(getClientId(vid));
  73. }
  74. private static String getClientId(String vid) {
  75. return vid + "-" + (new Date()).getTime();
  76. }
  77. public static String parseClientId(String clientId) {
  78. int ind = clientId.indexOf('-');
  79. if (ind > 0) {
  80. return clientId.substring(0, ind);
  81. } else return null;
  82. }
  83. // Actually handle sending out messages to a given list of clients
  84. public static void sendUpdate(List<String> clients, String message) {
  85. ChannelService cService = ChannelServiceFactory.getChannelService();
  86. for (String client : clients) {
  87. cService.sendMessage(new ChannelMessage(client, message));
  88. }
  89. }
  90. }