Guest User

Untitled

a guest
May 20th, 2018
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 34.12 KB | None | 0 0
  1. /**
  2. * OpenSocial Framework
  3. * @version: 1.0
  4. * @author: Suhail Doshi (suhail.doshi@asu.edu)
  5. * @updated: 6/27/2008 (9:46 AM)
  6. * Notes: Supports version 0.7 of OpenSocial.
  7. */
  8.  
  9. var osNetwork = function() {
  10. network = {};
  11.  
  12. network.owner = null;
  13. network.viewer = null;
  14. network.raw_persons = {};
  15.  
  16. // Static URI used for makeRequest() if set, so you don't have to keep typing in:
  17. // example.com/my_app/do_something, make staticURI = 'example.com/my_app' and pass:
  18. // '/do_something' into your URL parameter for doRequest();
  19. network.staticURI = null;
  20.  
  21. // Use network.setRequestAuth('SIGNED'); to make doRequest() (wrapper of makeRequest())
  22. // send signed requests.
  23. network.authentication = gadgets.io.AuthorizationType.NONE;
  24.  
  25. network.getUser = function(person) {
  26. info = {};
  27.  
  28. info.person = person;
  29. info.current_location = null;
  30.  
  31. /*
  32. * Add more keys to the userInfo array as more available data is available.
  33. *
  34. * returns an object structure.
  35. */
  36. info.aggregate = function() {
  37. userInfo = {
  38. 'about_me' : info.about_me(),
  39. 'age' : info.age(),
  40. 'date_of_birth' : info.date_of_birth(),
  41. 'display_name' : info.display_name(),
  42. 'gender' : info.gender(),
  43. 'os_id' : info.id(),
  44. 'name' : info.name(),
  45. 'thumbnail_url' : info.thumbnail_url(),
  46. 'is_owner' : info.isOwner(),
  47. 'is_viewer' : info.isViewer(),
  48. 'profile_url' : info.getProfileURL(),
  49. 'city' : info.city(),
  50. 'region' : info.region(),
  51. 'country' : info.country(),
  52. 'postal_code' : info.postal_code()
  53. //'is_online' : info.isOnline(),
  54. //'allow_send' : info.allowedToSend()
  55. };
  56.  
  57. userInfo.has_app = (userInfo.is_owner && userInfo.is_viewer);
  58.  
  59. return userInfo;
  60. };
  61.  
  62. /*info.allowedToSend = function() {
  63. return person.getField(opensocial.Person.ALLOWSEND);
  64. }*/
  65.  
  66. /*info.isOnline = function() {
  67. if (person) {
  68. switch (person.getField(opensocial.Person.PRESENCE).toUpperString()) {
  69. case 'OFFLINE':
  70. return false;
  71. case 'ONLINE':
  72. return true;
  73. default:
  74. return null;
  75. }
  76. } else
  77. return null;
  78. }*/
  79.  
  80. info.getProfileURL = function() {
  81. if (person){
  82. return person.getField(opensocial.Person.Field.PROFILE_URL);
  83. } else {
  84. return null;
  85. }
  86. };
  87.  
  88. info.isOwner = function() {
  89. if (person) {
  90. return person.isOwner();
  91. } else {
  92. return false;
  93. }
  94. };
  95.  
  96. info.isViewer = function() {
  97. if (person) {
  98. return person.isViewer();
  99. }
  100. else {
  101. return false;
  102. }
  103. };
  104.  
  105. info.display_name = function() {
  106. if (person) {
  107. return person.getDisplayName();
  108. }
  109. else {
  110. return null;
  111. }
  112. };
  113.  
  114. info.name = function() {
  115. if (person.getField && person.getField(opensocial.Person.Field.NAME)) {
  116. var name = person.getField(opensocial.Person.Field.NAME);
  117.  
  118. // Some containers return it this way. (maybe it will change later?)
  119. if (typeof(name) != 'string') {
  120. name = name.getField(opensocial.Name.Field.UNSTRUCTURED);
  121. }
  122.  
  123. if (typeof(name) == 'string') {
  124. return name;
  125. } else {
  126. return null;
  127. }
  128. } else {
  129. return null;
  130. }
  131. };
  132.  
  133. info.id = function() {
  134. if (person && person.getId()) {
  135. return person.getId();
  136. }
  137. else {
  138. return null;
  139. }
  140. };
  141.  
  142. info.about_me = function() {
  143. if (person.getField) {
  144. return person.getField(opensocial.Person.Field.ABOUT_ME);
  145. } else {
  146. return null;
  147. }
  148. };
  149.  
  150. info.age = function() {
  151. if (person.getField) {
  152. return person.getField(opensocial.Person.Field.AGE);
  153. } else {
  154. return null;
  155. }
  156. };
  157.  
  158. info.set_current_location = function() {
  159. if (person.getField) {
  160. info.user_location = person.getField(opensocial.Person.Field.CURRENT_LOCATION);
  161. }
  162. };
  163.  
  164. info.city = function() {
  165. if (! info.user_location || info.user_location === null) {
  166. info.set_current_location(); // Grab location info
  167. }
  168.  
  169. if (info.user_location && info.user_location.getField) {
  170. return info.user_location.getField(opensocial.Address.Field.LOCALITY);
  171. }
  172. else {
  173. return null;
  174. }
  175. };
  176.  
  177. info.region = function() {
  178. if (! info.user_location && info.user_location === null) {
  179. info.set_current_location(); // Grab location info
  180. }
  181.  
  182. if (info.user_location && info.user_location.getField) {
  183. return info.user_location.getField(opensocial.Address.Field.REGION);
  184. }
  185. else {
  186. return null;
  187. }
  188. };
  189.  
  190. info.country = function() {
  191. if (! info.user_location && info.user_location === null) {
  192. info.set_current_location(); // Grab location info
  193. }
  194.  
  195. if (info.user_location && info.user_location.getField) {
  196. return info.user_location.getField(opensocial.Address.Field.COUNTRY);
  197. }
  198. else {
  199. return null;
  200. }
  201. };
  202.  
  203. info.postal_code = function() {
  204. if (! info.user_location && info.user_location === null) {
  205. info.set_current_location(); // Grab location info
  206. }
  207.  
  208. if (info.user_location && info.user_location.getField) {
  209. return info.user_location.getField(opensocial.Address.Field.POSTAL_CODE);
  210. }
  211. else {
  212. return null;
  213. }
  214. };
  215.  
  216. info.thumbnail_url = function() {
  217. if (person.getField) {
  218. return person.getField(opensocial.Person.Field.THUMBNAIL_URL);
  219. }
  220. else {
  221. return null;
  222. }
  223. };
  224.  
  225. info.date_of_birth = function() {
  226. if (person.getField) {
  227. return person.getField(opensocial.Person.Field.DATE_OF_BIRTH);
  228. }
  229. else {
  230. return null;
  231. }
  232. };
  233.  
  234. info.gender = function() {
  235. if (person.getField(opensocial.Person.Field.GENDER) && person.getField(opensocial.Person.Field.GENDER).getKey) {
  236. return person.getField(opensocial.Person.Field.GENDER).getKey(opensocial.Enum.Gender);
  237. }
  238. else {
  239. return null;
  240. }
  241. };
  242.  
  243. // For any other fields
  244. info.getField = function(key) {
  245. if (person.getField) {
  246. return person.getField(key);
  247. }
  248. else {
  249. return null;
  250. }
  251. };
  252.  
  253. return info;
  254. };
  255.  
  256. /*
  257. * @param url example.com/my_app/
  258. * @param method -> GET/POST
  259. * @param params Either mapped string object or a string of style: var1=val1&var2=val2
  260. * @param contentType Should be an alias that works with the function: getContentType();
  261. * @param refreshInterval -1 means no cache is used, null or 0 means caching always, any other value caches in seconds
  262. */
  263. network.doRequest = function(url, method, params, callback, contentType, refreshInterval) {
  264.  
  265. var encodedParams;
  266. var sep = '?';
  267.  
  268. if (params !== null) {
  269. if (typeof(params) != 'string') {
  270. encodedParams = gadgets.io.encodeValues(params); // Encode params to form correct URL
  271. } else {
  272. encodedParams = params;
  273. }
  274. }
  275. else {
  276. encodedParams = null;
  277. }
  278.  
  279. contentType = network.getContentType(contentType); // Select content type
  280.  
  281. // What kind of request is being made
  282. if (method.toUpperCase() == 'POST') {
  283. optParams = { METHOD : 'POST', CONTENT_TYPE : contentType, POST_DATA : encodedParams, AUTHORIZATION : network.authentication };
  284. } else { // GET request
  285. if (encodedParams !== null) {
  286. url += '?' + encodedParams; // Append to URL
  287. }
  288. optParams = { METHOD : 'GET', CONTENT_TYPE : contentType, AUTHORIZATION : network.authentication };
  289. }
  290.  
  291. // Refresh cache result? -1 = no cached requests.
  292. if (refreshInterval || refreshInterval == -1) {
  293. if (refreshInterval == -1) {
  294. refreshInterval = 0;
  295. }
  296.  
  297. if (url.indexOf("?") > -1) {
  298. sep = "&";
  299. }
  300. else {
  301. sep = "?";
  302. }
  303. url += sep + 'nocache=' + network.getRefreshInterval(refreshInterval);
  304. }
  305.  
  306. // Append which container is sending the request (SIGNED requests will pass it anyway)
  307. if (network.authentication !== gadgets.io.AuthorizationType.SIGNED) {
  308. url += sep + 'container_src=' + network.getContainerHost();
  309. }
  310.  
  311. // If a staticURI to make a request to has been specified, always use it.
  312. if (network.staticURI) {
  313. url = network.staticURI + url;
  314. }
  315.  
  316. // Send request
  317. gadgets.io.makeRequest(url, callback, optParams);
  318. };
  319.  
  320. network.setRequestAuth = function(type) {
  321. if (! type) {
  322. type = null;
  323. }
  324.  
  325. switch (type.toUpperCase()) {
  326. case 'SIGNED':
  327. network.authentication = gadgets.io.AuthorizationType.SIGNED;
  328. break;
  329. case 'AUTHENTICATED':
  330. network.authentication = gadgets.io.AuthorizationType.AUTHENTICATED;
  331. break;
  332. default:
  333. network.authentication = gadgets.io.AuthorizationType.NONE;
  334. }
  335. };
  336.  
  337. network.getContentType = function(type) {
  338. var contentType = null; // Initialize
  339. if (! type || type === null) {
  340. contentType = gadgets.io.ContentType.JSON;
  341. }
  342. else {
  343. switch (type.toUpperCase()) {
  344. case 'DOM':
  345. contentType = gadgets.io.ContentType.DOM;
  346. break;
  347. case 'FEED':
  348. contentType = gadgets.io.ContentType.FEED;
  349. break;
  350. case 'JSON':
  351. contentType = gadgets.io.ContentType.JSON;
  352. break;
  353. case 'TEXT':
  354. contentType = gadgets.io.ContentType.TEXT;
  355. break;
  356. default:
  357. contentType = gadgets.io.ContentType.JSON;
  358. }
  359. }
  360.  
  361. return contentType;
  362. };
  363.  
  364. // A refresh interval of 0, means current date time stamp.
  365. network.getRefreshInterval = function(refreshInterval) {
  366. var ts = new Date().getTime();
  367. if (refreshInterval && refreshInterval > 0) {
  368. return Math.floor(ts / (refreshInterval * 1000));
  369. }
  370. return ts;
  371. };
  372.  
  373. /*
  374. * returns the location where the application is currently being used.
  375. */
  376. network.getAppSurface = function() {
  377. if (gadgets.views) {
  378. return gadgets.views.getCurrentView().getName();
  379. }
  380. else {
  381. return null;
  382. }
  383. };
  384.  
  385. network.getAllRequestFields = function() {
  386. var fields = [];
  387. // Only bothered to do ones I felt the social networks were going to actually implement or
  388. // were relevant to applications in a majority of the categories as a just in case mechanism.
  389. fields.push( opensocial.Person.Field.ABOUT_ME,
  390. opensocial.Person.Field.ADDRESSES,
  391. opensocial.Person.Field.AGE,
  392. opensocial.Person.Field.CURRENT_LOCATION, // yes
  393. opensocial.Person.Field.DATE_OF_BIRTH,
  394. opensocial.Person.Field.EMAILS,
  395. opensocial.Person.Field.ETHNICITY,
  396. opensocial.Person.Field.GENDER, // yes
  397. opensocial.Person.Field.FOOD,
  398. opensocial.Person.Field.HEROES,
  399. opensocial.Person.Field.ID, // yes
  400. opensocial.Person.Field.INTERESTS,
  401. opensocial.Person.Field.LANGUAGES_SPOKEN,
  402. opensocial.Person.Field.MOVIES,
  403. opensocial.Person.Field.MUSIC,
  404. opensocial.Person.Field.NAME,
  405. opensocial.Person.Field.NICKNAME,
  406. opensocial.Person.Field.PETS,
  407. opensocial.Person.Field.PHONE_NUMBERS,
  408. opensocial.Person.Field.PROFILE_SONG,
  409. opensocial.Person.Field.PROFILE_URL, // yes
  410. opensocial.Person.Field.PROFILE_VIDEO,
  411. opensocial.Person.Field.QUOTES,
  412. opensocial.Person.Field.RELATIONSHIP_STATUS,
  413. opensocial.Person.Field.RELIGION,
  414. opensocial.Person.Field.STATUS,
  415. opensocial.Person.Field.THUMBNAIL_URL, // yes
  416. opensocial.Person.Field.TIME_ZONE,
  417. opensocial.Person.Field.URLS
  418. );
  419. return fields;
  420. };
  421.  
  422. /*
  423. * @param callback function
  424. * @param bool refresh = re-request the data.
  425. */
  426. network.getViewerData = function(callback, refresh, params) {
  427. // Already got the data, re-return
  428. if (network.viewer && ! refresh) {
  429. if (callback) {
  430. callback(network.parseUserData(network.viewer));
  431. }
  432. } else {
  433.  
  434. if (typeof(params) == 'undefined' || params === null) {
  435. params = {};
  436. params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = network.getAllRequestFields();
  437. }
  438.  
  439. var req = opensocial.newDataRequest();
  440. req.add(req.newFetchPersonRequest(opensocial.DataRequest.PersonId.VIEWER, params), "viewer");
  441. req.send(function(response) {
  442. viewerInfo = response.get("viewer");
  443. if (viewerInfo && viewerInfo.getData) {
  444. network.viewer = viewerInfo.getData(); // Grab PERSON object
  445.  
  446. if (callback) {
  447. callback(network.parseUserData(network.viewer));
  448. }
  449. } else {
  450. callback(null);
  451. }
  452. });
  453. }
  454. };
  455.  
  456. /*
  457. * @param callback function
  458. * @param bool refresh = re-request the data.
  459. */
  460. network.getOwnerData = function(callback, refresh, params) {
  461. // Already got the data, re-return
  462. if (network.owner && ! refresh) {
  463. if (callback) {
  464. callback(network.parseUserData(network.owner));
  465. }
  466. } else {
  467.  
  468. if (typeof(params) == 'undefined' || params === null) {
  469. params = {};
  470. params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = network.getAllRequestFields();
  471. }
  472.  
  473. // Make request
  474. var req = opensocial.newDataRequest();
  475. req.add(req.newFetchPersonRequest(opensocial.DataRequest.PersonId.OWNER, params), "owner");
  476. req.send(function(response) {
  477. // Get PERSON information
  478. ownerInfo = response.get("owner");
  479. if (ownerInfo && ownerInfo.getData) {
  480. network.owner = ownerInfo.getData(); // Grab PERSON object
  481.  
  482. if (callback) {
  483. callback(network.parseUserData(network.owner));
  484. } else {
  485. callback(null);
  486. }
  487. } else {
  488. callback(null);
  489. }
  490. });
  491. }
  492. };
  493.  
  494. // Get both owner and viewer data
  495. network.getViewerOwner = function(callback, refresh, params) {
  496. if (typeof(params) == 'undefined' || params === null) {
  497. params = {};
  498. params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = network.getAllRequestFields();
  499. }
  500.  
  501. var req = opensocial.newDataRequest();
  502. req.add(req.newFetchPersonRequest(opensocial.DataRequest.PersonId.VIEWER, params), "viewer");
  503. req.add(req.newFetchPersonRequest(opensocial.DataRequest.PersonId.OWNER, params), "owner");
  504. req.send(function(response) {
  505. var ownerInfo = response.get("owner");
  506. var viewerInfo = response.get("viewer");
  507.  
  508. if (ownerInfo) {
  509. network.owner = ownerInfo.getData();
  510. }
  511.  
  512. if (viewerInfo) {
  513. network.viewer = viewerInfo.getData();
  514. }
  515.  
  516. var users = {};
  517. if (network.owner) {
  518. users.owner = network.parseUserData(network.owner);
  519. }
  520.  
  521. if (network.viewer) {
  522. users.viewer = network.parseUserData(network.viewer);
  523. }
  524.  
  525. if (callback) {
  526. callback(users);
  527. }
  528. });
  529. };
  530.  
  531. /*
  532. * @param array Opensocial id's of people.
  533. * @param array Optional parameters
  534. *
  535. * Will get PEOPLE objects for a single or array of id's.
  536. */
  537. network.getPersons = function(personIds, params, callback) {
  538. if (typeof(params) == 'undefined' || params === null) {
  539. params = {};
  540. params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = network.getAllRequestFields();
  541. }
  542.  
  543. var req = opensocial.newDataRequest();
  544. req.add(req.newFetchPeopleRequest(personIds, params), "people");
  545. req.send(function(response) {
  546. var persons = response.get("people").getData();
  547. network.processPersons(persons, callback);
  548. });
  549. };
  550.  
  551. network.getPeopleSettings = function(filter, max, sortOrder, page) {
  552. var settings = {};
  553.  
  554. if (max === null) {
  555. max = 50;
  556. }
  557.  
  558. if (filter && filter.toUpperCase()) {
  559. // Set filter type
  560. switch(filter) {
  561. case 'ALL':
  562. settings[opensocial.DataRequest.PeopleRequestFields.FILTER] = opensocial.DataRequest.FilterType.ALL;
  563. break;
  564. case 'HAS_APP':
  565. settings[opensocial.DataRequest.PeopleRequestFields.FILTER] = opensocial.DataRequest.FilterType.HAS_APP;
  566. break;
  567. }
  568. }
  569.  
  570. if (sortOrder && sortOrder.toUpperCase()) {
  571. switch(sortOrder) {
  572. case 'NAME':
  573. settings[opensocial.DataRequest.PeopleRequestFields.SORT_ORDER] = opensocial.DataRequest.SortOrder.NAME;
  574. break;
  575. case 'TOP_FRIENDS':
  576. settings[opensocial.DataRequest.PeopleRequestFields.SORT_ORDER] = opensocial.DataRequest.SortOrder.TOP_FRIENDS;
  577. break;
  578. }
  579. }
  580.  
  581. if (max) {
  582. settings[opensocial.DataRequest.PeopleRequestFields.MAX] = max;
  583. }
  584. if (page) {
  585. settings[opensocial.DataRequest.PeopleRequestFields.FIRST] = (max * page);
  586. }
  587.  
  588. return settings;
  589. };
  590.  
  591. /*
  592. * @param string userType Either request the 'viewer' or 'owner' friends.
  593. * @param params opensocial.DataRequest.PeopleRequestFields -> use network.getPeopleSettings() to easily define them.
  594. */
  595. network.getUserFriends = function(userType, params, callback, iterations) {
  596. var personId = network.getUserIdSpec(userType);
  597.  
  598. if (typeof(params) == 'undefined' || params === null) {
  599. params = network.getPeopleSettings('ALL', 40, 'TOP_FRIENDS', 0);
  600. params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = network.getAllRequestFields();
  601. }
  602.  
  603. // Iterate at least once
  604. if (typeof(iterations) == 'undefined' || iterations === null) {
  605. iterations = 1;
  606. }
  607.  
  608. if (personId) {
  609. var req = opensocial.newDataRequest();
  610.  
  611. // Batch friend requests up based on # of specified iterations
  612. for (var i = 0; i < iterations; i++) {
  613.  
  614. var settings = params; // inherit properties
  615.  
  616. // Need a default max
  617. if (typeof(settings[opensocial.DataRequest.PeopleRequestFields.MAX]) == 'undefined' || settings[opensocial.DataRequest.PeopleRequestFields.MAX] === null) {
  618. settings[opensocial.DataRequest.PeopleRequestFields.MAX] = 40;
  619. }
  620.  
  621. // Default a FIRST offset
  622. if (typeof(params[opensocial.DataRequest.PeopleRequestFields.FIRST]) == 'undefined' || params[opensocial.DataRequest.PeopleRequestFields.FIRST] === null) {
  623. settings[opensocial.DataRequest.PeopleRequestFields.FIRST] = 1;
  624. }
  625.  
  626. // Determine next FIRST offset
  627. settings[opensocial.DataRequest.PeopleRequestFields.FIRST] = (i * settings[opensocial.DataRequest.PeopleRequestFields.MAX]);
  628.  
  629. // Batch the settings set with unique key
  630. req.add(req.newFetchPeopleRequest(personId, settings), "friends" + i);
  631.  
  632. settings = null; // Reset
  633. }
  634.  
  635. req.send(function(response) {
  636. // Parse info in a nice way, then send back...
  637. network.processFriends(response, iterations, callback);
  638. });
  639. }
  640. };
  641.  
  642. /*
  643. * @param data response from newFetchPeopleRequest()
  644. *
  645. * returns nicely formatted object array of friend data.
  646. */
  647. network.processFriends = function(response, iterations, callback) {
  648. for (var i = 0; i < iterations; i++) {
  649. if (response.get("friends" + i) && response.get("friends" + i)) {
  650. var friendData = response.get("friends" + i).getData();
  651.  
  652. network.processPersons(friendData, callback);
  653. }
  654. }
  655. };
  656.  
  657. network.processPersons = function(personData, callback) {
  658. if (personData && personData.each) {
  659. var persons = {};
  660. personData.each(function(person) {
  661. userData = network.parseUserData(person);
  662. persons[userData.os_id] = userData;
  663. network.raw_persons[userData.os_id] = person; // Global person data cache (non-parsed version)
  664. });
  665.  
  666. callback(persons);
  667. }
  668. };
  669.  
  670. /*
  671. * @param PERSON object
  672. * returns nicely formatted user information.
  673. */
  674. network.parseUserData = function(userData) {
  675. if (userData) {
  676. var userInfo = network.getUser(userData).aggregate();
  677. return userInfo;
  678. } else {
  679. return null;
  680. }
  681. };
  682.  
  683. /*
  684. * @param opensocial.PERSON
  685. * @param Specify whether the user is a 'viewer' or 'owner'
  686. *
  687. * returns a boolean
  688. */
  689. network.hasAppInstalled = function(person, type) {
  690. if (type) {
  691. switch (type.toUpperCase()) {
  692. case 'VIEWER':
  693. if (person) {
  694. return person.isOwner();
  695. }
  696. break;
  697. case 'OWNER':
  698. if (person) {
  699. return person.isViewer();
  700. }
  701. break;
  702. }
  703. }
  704. return false; // Last case
  705. };
  706.  
  707. // Post to the activity stream so users can see other users updates/actions.
  708. // Non-template version
  709. // priority can be 'HIGH' or 'LOW'
  710. network.doPostActivityStream = function(title, body, priority, mediaItems, callback) {
  711. switch (priority.toUpperCase()) {
  712. case 'HIGH':
  713. priority = opensocial.CreateActivityPriority.HIGH;
  714. break;
  715. case 'LOW':
  716. priority = opensocial.CreateActivityPriority.LOW;
  717. break;
  718. default:
  719. priority = opensocial.CreateActivityPriority.HIGH;
  720. }
  721.  
  722. var params = {};
  723.  
  724. params[opensocial.Activity.Field.TITLE] = title;
  725. params[opensocial.Activity.Field.BODY] = body;
  726.  
  727. var activity = opensocial.newActivity(params);
  728.  
  729. if (activity && mediaItems) {
  730. activity.setField(opensocial.Activity.Field.MEDIA_ITEMS, mediaItems);
  731. }
  732. opensocial.requestCreateActivity(activity, priority, callback);
  733. };
  734.  
  735. network.buildMediaItem = function(type, mediaURL, params) {
  736. if (type) {
  737. var mimeType = network.getMimeType(type);
  738.  
  739. if (mediaURL && mimeType) {
  740. return opensocial.newActivityMediaItem(mimeType, mediaURL, params);
  741. }
  742. }
  743. };
  744.  
  745. network.getMimeType = function(type) {
  746. switch(type.toUpperCase()) {
  747. case 'AUDIO':
  748. return opensocial.Activity.MediaItem.Type.AUDIO;
  749. break;
  750. case 'IMAGE':
  751. return opensocial.Activity.MediaItem.Type.IMAGE;
  752. break;
  753. case 'VIDEO':
  754. return opensocial.Activity.MediaItem.Type.VIDEO;
  755. break;
  756. }
  757. };
  758.  
  759. // Grabs the domain for a container the application is hosted on.
  760. network.getContainerHost = function() {
  761. if (opensocial.getEnvironment()) {
  762. return opensocial.getEnvironment().getDomain();
  763. } else {
  764. return null;
  765. }
  766. };
  767.  
  768. // Do we have the ability to play with the viewer's data?
  769. network.hasViewerPermission = function() {
  770. return opensocial.hasPermission(opensocial.Permission.VIEWER);
  771. };
  772.  
  773. network.getActivityRequest = function(userType, callback) {
  774. var personId = network.getUserIdSpec(userType);
  775.  
  776. var req = opensocial.newDataRequest();
  777. req.add(req.newFetchActivitiesRequest(personId));
  778. req.send(function(response) {
  779. if (callback) {
  780. callback(response.get());
  781. }
  782. });
  783. };
  784.  
  785. network.getUserIdSpec = function(userType) {
  786. switch (userType.toUpperCase()) {
  787. case 'VIEWER_FRIENDS':
  788. personId = opensocial.DataRequest.Group.VIEWER_FRIENDS;
  789. break;
  790. case 'OWNER_FRIENDS':
  791. personId = opensocial.DataRequest.Group.OWNER_FRIENDS;
  792. break;
  793. case 'OWNER':
  794. personId = opensocial.DataRequest.PersonId.OWNER;
  795. break;
  796. case 'VIEWER':
  797. personId = opensocial.DataRequest.PersonId.VIEWER;
  798. break;
  799. }
  800.  
  801. return personId;
  802. };
  803.  
  804. network.getPossibleSurfaces = function() {
  805. var views = gadgets.views.getSupportedViews(); // Get supported views from the container.
  806. return views;
  807. };
  808.  
  809. network.gotoSurface = function(surfaceName, params) {
  810. var surfaces = network.getPossibleSurfaces();
  811.  
  812. if (typeof(params) == 'undefined') {
  813. params = null;
  814. }
  815.  
  816. switch (surfaceName.toUpperCase()) {
  817. case 'PROFILE':
  818. if (surfaces.profile) {
  819. gadgets.views.requestNavigateTo(surfaces.profile, params);
  820. }
  821. break;
  822. case 'CANVAS':
  823. if (surfaces.canvas) {
  824. gadgets.views.requestNavigateTo(surfaces.canvas, params);
  825. }
  826. break;
  827. }
  828. };
  829.  
  830. /*
  831. * @param Map.<String, String> params
  832. * Will return a *string* view params for you to append to your URL to grab them via gadgets.views.getParams()
  833. * Example: url += '&' + network.convertViewParams({'var1' : 'value1', 'var2' : 'value2'});
  834. */
  835. network.convertViewParams = function(params) {
  836. var strParams = gadgets.json.stringify(params);
  837. return encodeURIComponent(strParams);
  838. };
  839.  
  840. network.getCurrentSurface = function() {
  841. return gadgets.views.getCurrentView();
  842. };
  843.  
  844. // Builds a message object used for many viral API implementations
  845. network.buildMessage = function(title, body, type) {
  846. // Build message object
  847. var params = {};
  848.  
  849. if (title) {
  850. params[opensocial.Message.Field.BODY] = body;
  851. }
  852. if (body) {
  853. params[opensocial.Message.Field.TITLE] = title;
  854. }
  855. if (type) {
  856. params[opensocial.Message.Field.TYPE] = network.getMessageType(type);
  857. }
  858.  
  859. message = opensocial.newMessage(body, params);
  860.  
  861. return message;
  862. };
  863.  
  864. network.getMessageType = function(type) {
  865. if (type && type !== null) {
  866. switch(type.toUpperCase()) {
  867. case 'NOTIFICATION':
  868. return opensocial.Message.Type.NOTIFICATION;
  869. case 'PRIVATE_MESSAGE':
  870. return opensocial.Message.Type.PRIVATE_MESSAGE;
  871. case 'PUBLIC_MESSAGE':
  872. return opensocial.Message.Type.PUBLIC_MESSAGE;
  873. case 'EMAIL':
  874. return opensocial.Message.Type.EMAIL;
  875. default:
  876. return null;
  877. }
  878. }
  879.  
  880. return null;
  881. };
  882.  
  883. network.shareAppOwnerFriends = function(title, body, type, callback) {
  884. network.shareApp(network.getUserIdSpec('OWNER_FRIENDS'), title, body, type, callback);
  885. };
  886.  
  887. network.shareAppViewerFriends = function(title, body, type, callback) {
  888. network.shareApp(network.getUserIdSpec('VIEWER_FRIENDS'), title, body, type, callback);
  889. };
  890.  
  891. /*
  892. * @param mixed recipients UserIdSpec or array of opensocial_id's
  893. * @param string title Title of message to send.
  894. * @param string body Body of the message.
  895. */
  896. network.shareApp = function(recipients, title, body, type, callback) {
  897. // Build message object
  898. message = network.buildMessage(title, body, type);
  899.  
  900. // Send message to recipients
  901. opensocial.requestShareApp(recipients, message, callback);
  902. };
  903.  
  904. network.sendViewerMessage = function(title, body, type, callback) {
  905. network.sendMessage(network.getUserIdSpec('VIEWER'), title, body, type, callback);
  906. };
  907.  
  908. network.sendOwnerFriendsMessage = function(title, body, type, callback) {
  909. network.sendMessage(network.getUserIdSpec('OWNER_FRIENDS'), title, body, type, callback);
  910. };
  911.  
  912. /*
  913. * @param mixed recipients UserIdSpec or array of opensocial_id's
  914. * @param string title Title of message to send.
  915. * @param string body Body of the message.
  916. * @param string type Container dependent, ex: NOTIFICATION, EMAIL
  917. */
  918. network.sendMessage = function(recipients, title, body, type, callback) {
  919. try {
  920. // Build message object
  921. message = network.buildMessage(title, body, type);
  922.  
  923. // Send message to recipients
  924. opensocial.requestSendMessage(recipients, message, callback);
  925. } catch (ex) {
  926. callback(false);
  927. }
  928. };
  929.  
  930. // This depends on the container, some do not support it--be wary about cross compatibility.
  931. network.getAppId = function() {
  932. return gadgets.util.getUrlParameters().gadgetId;
  933. };
  934.  
  935. network.debug = function(title, data) {
  936. /*console.log('DEBUG: ' + title);
  937.  
  938. if (data) {
  939. console.log(data);
  940. }
  941. console.log('===========END DEBUG===========');*/
  942. };
  943.  
  944. return network;
  945. };
  946.  
  947. var net = new osNetwork();
  948. net.shareApp('384989418', 'Testing', 'Testing body', null, function(data) {
  949. console.log(data);
  950. });
Add Comment
Please, Sign In to add comment