Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

258 lines
6.6 KiB

  1. <?php
  2. require_once("include/diaspora.php");
  3. /**
  4. * @brief add/remove activity to an item
  5. *
  6. * Toggle activities as like,dislike,attend of an item
  7. *
  8. * @param string $item_id
  9. * @param string $verb
  10. * Activity verb. One of
  11. * like, unlike, dislike, undislike, attendyes, unattendyes,
  12. * attendno, unattendno, attendmaybe, unattendmaybe
  13. * @hook 'post_local_end'
  14. * array $arr
  15. * 'post_id' => ID of posted item
  16. */
  17. function do_like($item_id, $verb) {
  18. $a = get_app();
  19. if(! local_user() && ! remote_user()) {
  20. return false;
  21. }
  22. switch($verb) {
  23. case 'like':
  24. case 'unlike':
  25. $activity = ACTIVITY_LIKE;
  26. break;
  27. case 'dislike':
  28. case 'undislike':
  29. $activity = ACTIVITY_DISLIKE;
  30. break;
  31. case 'attendyes':
  32. case 'unattendyes':
  33. $activity = ACTIVITY_ATTEND;
  34. break;
  35. case 'attendno':
  36. case 'unattendno':
  37. $activity = ACTIVITY_ATTENDNO;
  38. break;
  39. case 'attendmaybe':
  40. case 'unattendmaybe':
  41. $activity = ACTIVITY_ATTENDMAYBE;
  42. break;
  43. default:
  44. return false;
  45. break;
  46. }
  47. logger('like: verb ' . $verb . ' item ' . $item_id);
  48. $r = q("SELECT * FROM `item` WHERE `id` = '%s' OR `uri` = '%s' LIMIT 1",
  49. dbesc($item_id),
  50. dbesc($item_id)
  51. );
  52. if(! $item_id || (! dbm::is_result($r))) {
  53. logger('like: no item ' . $item_id);
  54. return false;
  55. }
  56. $item = $r[0];
  57. $owner_uid = $item['uid'];
  58. if (! can_write_wall($a,$owner_uid)) {
  59. return false;
  60. }
  61. $remote_owner = null;
  62. if(! $item['wall']) {
  63. // The top level post may have been written by somebody on another system
  64. $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
  65. intval($item['contact-id']),
  66. intval($item['uid'])
  67. );
  68. if (! dbm::is_result($r)) {
  69. return false;
  70. }
  71. if (! $r[0]['self']) {
  72. $remote_owner = $r[0];
  73. }
  74. }
  75. // this represents the post owner on this system.
  76. $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
  77. WHERE `contact`.`self` = 1 AND `contact`.`uid` = %d LIMIT 1",
  78. intval($owner_uid)
  79. );
  80. if (dbm::is_result($r)) {
  81. $owner = $r[0];
  82. }
  83. if (! $owner) {
  84. logger('like: no owner');
  85. return false;
  86. }
  87. if (! $remote_owner) {
  88. $remote_owner = $owner;
  89. }
  90. // This represents the person posting
  91. if ((local_user()) && (local_user() == $owner_uid)) {
  92. $contact = $owner;
  93. } else {
  94. $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
  95. intval($_SESSION['visitor_id']),
  96. intval($owner_uid)
  97. );
  98. if (dbm::is_result($r))
  99. $contact = $r[0];
  100. }
  101. if (! $contact) {
  102. return false;
  103. }
  104. $verbs = " '".dbesc($activity)."' ";
  105. // event participation are essentially radio toggles. If you make a subsequent choice,
  106. // we need to eradicate your first choice.
  107. if ($activity === ACTIVITY_ATTEND || $activity === ACTIVITY_ATTENDNO || $activity === ACTIVITY_ATTENDMAYBE) {
  108. $verbs = " '" . dbesc(ACTIVITY_ATTEND) . "','" . dbesc(ACTIVITY_ATTENDNO) . "','" . dbesc(ACTIVITY_ATTENDMAYBE) . "' ";
  109. }
  110. $r = q("SELECT `id`, `guid` FROM `item` WHERE `verb` IN ( $verbs ) AND `deleted` = 0
  111. AND `contact-id` = %d AND `uid` = %d
  112. AND (`parent` = '%s' OR `parent-uri` = '%s' OR `thr-parent` = '%s') LIMIT 1",
  113. intval($contact['id']), intval($owner_uid),
  114. dbesc($item_id), dbesc($item_id), dbesc($item['uri'])
  115. );
  116. if (dbm::is_result($r)) {
  117. $like_item = $r[0];
  118. // Already voted, undo it
  119. $r = q("UPDATE `item` SET `deleted` = 1, `unseen` = 1, `changed` = '%s' WHERE `id` = %d",
  120. dbesc(datetime_convert()),
  121. intval($like_item['id'])
  122. );
  123. // Clean up the Diaspora signatures for this like
  124. // Go ahead and do it even if Diaspora support is disabled. We still want to clean up
  125. // if it had been enabled in the past
  126. $r = q("DELETE FROM `sign` WHERE `iid` = %d",
  127. intval($like_item['id'])
  128. );
  129. $like_item_id = $like_item['id'];
  130. proc_run(PRIORITY_HIGH, "include/notifier.php", "like", $like_item_id);
  131. return true;
  132. }
  133. $uri = item_new_uri($a->get_hostname(),$owner_uid);
  134. $post_type = (($item['resource-id']) ? t('photo') : t('status'));
  135. if ($item['object-type'] === ACTIVITY_OBJ_EVENT) {
  136. $post_type = t('event');
  137. }
  138. $objtype = (($item['resource-id']) ? ACTIVITY_OBJ_IMAGE : ACTIVITY_OBJ_NOTE );
  139. $link = xmlify('<link rel="alternate" type="text/html" href="' . App::get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . '" />' . "\n") ;
  140. $body = $item['body'];
  141. $obj = <<< EOT
  142. <object>
  143. <type>$objtype</type>
  144. <local>1</local>
  145. <id>{$item['uri']}</id>
  146. <link>$link</link>
  147. <title></title>
  148. <content>$body</content>
  149. </object>
  150. EOT;
  151. if ($verb === 'like') {
  152. $bodyverb = t('%1$s likes %2$s\'s %3$s');
  153. }
  154. if ($verb === 'dislike') {
  155. $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
  156. }
  157. if ($verb === 'attendyes') {
  158. $bodyverb = t('%1$s is attending %2$s\'s %3$s');
  159. }
  160. if ($verb === 'attendno') {
  161. $bodyverb = t('%1$s is not attending %2$s\'s %3$s');
  162. }
  163. if ($verb === 'attendmaybe') {
  164. $bodyverb = t('%1$s may attend %2$s\'s %3$s');
  165. }
  166. if (! isset($bodyverb)) {
  167. return false;
  168. }
  169. $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
  170. $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
  171. $plink = '[url=' . App::get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . ']' . $post_type . '[/url]';
  172. /// @TODO Or rewrite this to multi-line initialization of the array?
  173. $arr = array();
  174. $arr['guid'] = get_guid(32);
  175. $arr['uri'] = $uri;
  176. $arr['uid'] = $owner_uid;
  177. $arr['contact-id'] = $contact['id'];
  178. $arr['type'] = 'activity';
  179. $arr['wall'] = $item['wall'];
  180. $arr['origin'] = 1;
  181. $arr['gravity'] = GRAVITY_LIKE;
  182. $arr['parent'] = $item['id'];
  183. $arr['parent-uri'] = $item['uri'];
  184. $arr['thr-parent'] = $item['uri'];
  185. $arr['owner-name'] = $remote_owner['name'];
  186. $arr['owner-link'] = $remote_owner['url'];
  187. $arr['owner-avatar'] = $remote_owner['thumb'];
  188. $arr['author-name'] = $contact['name'];
  189. $arr['author-link'] = $contact['url'];
  190. $arr['author-avatar'] = $contact['thumb'];
  191. $arr['body'] = sprintf( $bodyverb, $ulink, $alink, $plink );
  192. $arr['verb'] = $activity;
  193. $arr['object-type'] = $objtype;
  194. $arr['object'] = $obj;
  195. $arr['allow_cid'] = $item['allow_cid'];
  196. $arr['allow_gid'] = $item['allow_gid'];
  197. $arr['deny_cid'] = $item['deny_cid'];
  198. $arr['deny_gid'] = $item['deny_gid'];
  199. $arr['visible'] = 1;
  200. $arr['unseen'] = 1;
  201. $arr['last-child'] = 0;
  202. $post_id = item_store($arr);
  203. if (! $item['visible']) {
  204. $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d AND `uid` = %d",
  205. intval($item['id']),
  206. intval($owner_uid)
  207. );
  208. }
  209. // Save the author information for the like in case we need to relay to Diaspora
  210. diaspora::store_like_signature($contact, $post_id);
  211. $arr['id'] = $post_id;
  212. call_hooks('post_local_end', $arr);
  213. proc_run(PRIORITY_HIGH, "include/notifier.php", "like", $post_id);
  214. return true;
  215. }