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.

865 lines
26KB

  1. <?php
  2. /**
  3. * @file include/NotificationsManager.php
  4. * @brief Methods for read and write notifications from/to database
  5. * or for formatting notifications
  6. */
  7. require_once 'include/html2plain.php';
  8. require_once 'include/probe.php';
  9. require_once 'include/datetime.php';
  10. require_once 'include/bbcode.php';
  11. require_once 'include/Contact.php';
  12. /**
  13. * @brief Methods for read and write notifications from/to database
  14. * or for formatting notifications
  15. */
  16. class NotificationsManager {
  17. private $a;
  18. public function __construct() {
  19. $this->a = get_app();
  20. }
  21. /**
  22. * @brief set some extra note properties
  23. *
  24. * @param array $notes array of note arrays from db
  25. * @return array Copy of input array with added properties
  26. *
  27. * Set some extra properties to note array from db:
  28. * - timestamp as int in default TZ
  29. * - date_rel : relative date string
  30. * - msg_html: message as html string
  31. * - msg_plain: message as plain text string
  32. */
  33. private function _set_extra($notes) {
  34. $rets = array();
  35. foreach($notes as $n) {
  36. $local_time = datetime_convert('UTC',date_default_timezone_get(),$n['date']);
  37. $n['timestamp'] = strtotime($local_time);
  38. $n['date_rel'] = relative_date($n['date']);
  39. $n['msg_html'] = bbcode($n['msg'], false, false, false, false);
  40. $n['msg_plain'] = explode("\n",trim(html2plain($n['msg_html'], 0)))[0];
  41. $rets[] = $n;
  42. }
  43. return $rets;
  44. }
  45. /**
  46. * @brief Get all notifications for local_user()
  47. *
  48. * @param array $filter optional Array "column name"=>value: filter query by columns values
  49. * @param string $order optional Space separated list of column to sort by. prepend name with "+" to sort ASC, "-" to sort DESC. Default to "-date"
  50. * @param string $limit optional Query limits
  51. *
  52. * @return array of results or false on errors
  53. */
  54. public function getAll($filter = array(), $order="-date", $limit="") {
  55. $filter_str = array();
  56. $filter_sql = "";
  57. foreach($filter as $column => $value) {
  58. $filter_str[] = sprintf("`%s` = '%s'", $column, dbesc($value));
  59. }
  60. if (count($filter_str)>0) {
  61. $filter_sql = "AND ".implode(" AND ", $filter_str);
  62. }
  63. $aOrder = explode(" ", $order);
  64. $asOrder = array();
  65. foreach($aOrder as $o) {
  66. $dir = "asc";
  67. if ($o[0]==="-") {
  68. $dir = "desc";
  69. $o = substr($o,1);
  70. }
  71. if ($o[0]==="+") {
  72. $dir = "asc";
  73. $o = substr($o,1);
  74. }
  75. $asOrder[] = "$o $dir";
  76. }
  77. $order_sql = implode(", ", $asOrder);
  78. if($limit!="")
  79. $limit = " LIMIT ".$limit;
  80. $r = q("SELECT * FROM `notify` WHERE `uid` = %d $filter_sql ORDER BY $order_sql $limit",
  81. intval(local_user())
  82. );
  83. if (dbm::is_result($r))
  84. return $this->_set_extra($r);
  85. return false;
  86. }
  87. /**
  88. * @brief Get one note for local_user() by $id value
  89. *
  90. * @param int $id
  91. * @return array note values or null if not found
  92. */
  93. public function getByID($id) {
  94. $r = q("SELECT * FROM `notify` WHERE `id` = %d AND `uid` = %d LIMIT 1",
  95. intval($id),
  96. intval(local_user())
  97. );
  98. if (dbm::is_result($r)) {
  99. return $this->_set_extra($r)[0];
  100. }
  101. return null;
  102. }
  103. /**
  104. * @brief set seen state of $note of local_user()
  105. *
  106. * @param array $note
  107. * @param bool $seen optional true or false, default true
  108. * @return bool true on success, false on errors
  109. */
  110. public function setSeen($note, $seen = true) {
  111. return q("UPDATE `notify` SET `seen` = %d WHERE ( `link` = '%s' OR ( `parent` != 0 AND `parent` = %d AND `otype` = '%s' )) AND `uid` = %d",
  112. intval($seen),
  113. dbesc($note['link']),
  114. intval($note['parent']),
  115. dbesc($note['otype']),
  116. intval(local_user())
  117. );
  118. }
  119. /**
  120. * @brief set seen state of all notifications of local_user()
  121. *
  122. * @param bool $seen optional true or false. default true
  123. * @return bool true on success, false on error
  124. */
  125. public function setAllSeen($seen = true) {
  126. return q("UPDATE `notify` SET `seen` = %d WHERE `uid` = %d",
  127. intval($seen),
  128. intval(local_user())
  129. );
  130. }
  131. /**
  132. * @brief List of pages for the Notifications TabBar
  133. *
  134. * @return array with with notifications TabBar data
  135. */
  136. public function getTabs() {
  137. $tabs = array(
  138. array(
  139. 'label' => t('System'),
  140. 'url'=>'notifications/system',
  141. 'sel'=> (($this->a->argv[1] == 'system') ? 'active' : ''),
  142. 'id' => 'system-tab',
  143. 'accesskey' => 'y',
  144. ),
  145. array(
  146. 'label' => t('Network'),
  147. 'url'=>'notifications/network',
  148. 'sel'=> (($this->a->argv[1] == 'network') ? 'active' : ''),
  149. 'id' => 'network-tab',
  150. 'accesskey' => 'w',
  151. ),
  152. array(
  153. 'label' => t('Personal'),
  154. 'url'=>'notifications/personal',
  155. 'sel'=> (($this->a->argv[1] == 'personal') ? 'active' : ''),
  156. 'id' => 'personal-tab',
  157. 'accesskey' => 'r',
  158. ),
  159. array(
  160. 'label' => t('Home'),
  161. 'url' => 'notifications/home',
  162. 'sel'=> (($this->a->argv[1] == 'home') ? 'active' : ''),
  163. 'id' => 'home-tab',
  164. 'accesskey' => 'h',
  165. ),
  166. array(
  167. 'label' => t('Introductions'),
  168. 'url' => 'notifications/intros',
  169. 'sel'=> (($this->a->argv[1] == 'intros') ? 'active' : ''),
  170. 'id' => 'intro-tab',
  171. 'accesskey' => 'i',
  172. ),
  173. );
  174. return $tabs;
  175. }
  176. /**
  177. * @brief Format the notification query in an usable array
  178. *
  179. * @param array $notifs The array from the db query
  180. * @param string $ident The notifications identifier (e.g. network)
  181. * @return array
  182. * string 'label' => The type of the notification
  183. * string 'link' => URL to the source
  184. * string 'image' => The avatar image
  185. * string 'url' => The profile url of the contact
  186. * string 'text' => The notification text
  187. * string 'when' => The date of the notification
  188. * string 'ago' => T relative date of the notification
  189. * bool 'seen' => Is the notification marked as "seen"
  190. */
  191. private function formatNotifs($notifs, $ident = "") {
  192. $notif = array();
  193. $arr = array();
  194. if (dbm::is_result($notifs)) {
  195. foreach ($notifs as $it) {
  196. // Because we use different db tables for the notification query
  197. // we have sometimes $it['unseen'] and sometimes $it['seen].
  198. // So we will have to transform $it['unseen']
  199. if (array_key_exists('unseen', $it)) {
  200. $it['seen'] = ($it['unseen'] > 0 ? false : true);
  201. }
  202. // Depending on the identifier of the notification we need to use different defaults
  203. switch ($ident) {
  204. case 'system':
  205. $default_item_label = 'notify';
  206. $default_item_link = $this->a->get_baseurl(true).'/notify/view/'. $it['id'];
  207. $default_item_image = proxy_url($it['photo'], false, PROXY_SIZE_MICRO);
  208. $default_item_url = $it['url'];
  209. $default_item_text = strip_tags(bbcode($it['msg']));
  210. $default_item_when = datetime_convert('UTC', date_default_timezone_get(), $it['date'], 'r');
  211. $default_item_ago = relative_date($it['date']);
  212. break;
  213. case 'home':
  214. $default_item_label = 'comment';
  215. $default_item_link = $this->a->get_baseurl(true).'/display/'.$it['pguid'];
  216. $default_item_image = proxy_url($it['author-avatar'], false, PROXY_SIZE_MICRO);
  217. $default_item_url = $it['author-link'];
  218. $default_item_text = sprintf(t("%s commented on %s's post"), $it['author-name'], $it['pname']);
  219. $default_item_when = datetime_convert('UTC', date_default_timezone_get(), $it['created'], 'r');
  220. $default_item_ago = relative_date($it['created']);
  221. break;
  222. default:
  223. $default_item_label = (($it['id'] == $it['parent']) ? 'post' : 'comment');
  224. $default_item_link = $this->a->get_baseurl(true).'/display/'.$it['pguid'];
  225. $default_item_image = proxy_url($it['author-avatar'], false, PROXY_SIZE_MICRO);
  226. $default_item_url = $it['author-link'];
  227. $default_item_text = (($it['id'] == $it['parent'])
  228. ? sprintf(t("%s created a new post"), $it['author-name'])
  229. : sprintf(t("%s commented on %s's post"), $it['author-name'], $it['pname']));
  230. $default_item_when = datetime_convert('UTC', date_default_timezone_get(), $it['created'], 'r');
  231. $default_item_ago = relative_date($it['created']);
  232. }
  233. // Transform the different types of notification in an usable array
  234. switch ($it['verb']){
  235. case ACTIVITY_LIKE:
  236. $notif = array(
  237. 'label' => 'like',
  238. 'link' => $this->a->get_baseurl(true).'/display/'.$it['pguid'],
  239. 'image' => proxy_url($it['author-avatar'], false, PROXY_SIZE_MICRO),
  240. 'url' => $it['author-link'],
  241. 'text' => sprintf(t("%s liked %s's post"), $it['author-name'], $it['pname']),
  242. 'when' => $default_item_when,
  243. 'ago' => $default_item_ago,
  244. 'seen' => $it['seen']
  245. );
  246. break;
  247. case ACTIVITY_DISLIKE:
  248. $notif = array(
  249. 'label' => 'dislike',
  250. 'link' => $this->a->get_baseurl(true).'/display/'.$it['pguid'],
  251. 'image' => proxy_url($it['author-avatar'], false, PROXY_SIZE_MICRO),
  252. 'url' => $it['author-link'],
  253. 'text' => sprintf(t("%s disliked %s's post"), $it['author-name'], $it['pname']),
  254. 'when' => $default_item_when,
  255. 'ago' => $default_item_ago,
  256. 'seen' => $it['seen']
  257. );
  258. break;
  259. case ACTIVITY_ATTEND:
  260. $notif = array(
  261. 'label' => 'attend',
  262. 'link' => $this->a->get_baseurl(true).'/display/'.$it['pguid'],
  263. 'image' => proxy_url($it['author-avatar'], false, PROXY_SIZE_MICRO),
  264. 'url' => $it['author-link'],
  265. 'text' => sprintf(t("%s is attending %s's event"), $it['author-name'], $it['pname']),
  266. 'when' => $default_item_when,
  267. 'ago' => $default_item_ago,
  268. 'seen' => $it['seen']
  269. );
  270. break;
  271. case ACTIVITY_ATTENDNO:
  272. $notif = array(
  273. 'label' => 'attendno',
  274. 'link' => $this->a->get_baseurl(true).'/display/'.$it['pguid'],
  275. 'image' => proxy_url($it['author-avatar'], false, PROXY_SIZE_MICRO),
  276. 'url' => $it['author-link'],
  277. 'text' => sprintf( t("%s is not attending %s's event"), $it['author-name'], $it['pname']),
  278. 'when' => $default_item_when,
  279. 'ago' => $default_item_ago,
  280. 'seen' => $it['seen']
  281. );
  282. break;
  283. case ACTIVITY_ATTENDMAYBE:
  284. $notif = array(
  285. 'label' => 'attendmaybe',
  286. 'link' => $this->a->get_baseurl(true).'/display/'.$it['pguid'],
  287. 'image' => proxy_url($it['author-avatar'], false, PROXY_SIZE_MICRO),
  288. 'url' => $it['author-link'],
  289. 'text' => sprintf(t("%s may attend %s's event"), $it['author-name'], $it['pname']),
  290. 'when' => $default_item_when,
  291. 'ago' => $default_item_ago,
  292. 'seen' => $it['seen']
  293. );
  294. break;
  295. case ACTIVITY_FRIEND:
  296. $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
  297. $obj = parse_xml_string($xmlhead.$it['object']);
  298. $it['fname'] = $obj->title;
  299. $notif = array(
  300. 'label' => 'friend',
  301. 'link' => $this->a->get_baseurl(true).'/display/'.$it['pguid'],
  302. 'image' => proxy_url($it['author-avatar'], false, PROXY_SIZE_MICRO),
  303. 'url' => $it['author-link'],
  304. 'text' => sprintf(t("%s is now friends with %s"), $it['author-name'], $it['fname']),
  305. 'when' => $default_item_when,
  306. 'ago' => $default_item_ago,
  307. 'seen' => $it['seen']
  308. );
  309. break;
  310. default:
  311. $notif = array(
  312. 'label' => $default_item_label,
  313. 'link' => $default_item_link,
  314. 'image' => $default_item_image,
  315. 'url' => $default_item_url,
  316. 'text' => $default_item_text,
  317. 'when' => $default_item_when,
  318. 'ago' => $default_item_ago,
  319. 'seen' => $it['seen']
  320. );
  321. }
  322. $arr[] = $notif;
  323. }
  324. }
  325. return $arr;
  326. }
  327. /**
  328. * @brief Total number of network notifications
  329. * @param int|string $seen
  330. * If 0 only include notifications into the query
  331. * which aren't marked as "seen"
  332. * @return int Number of network notifications
  333. */
  334. private function networkTotal($seen = 0) {
  335. $sql_seen = "";
  336. if($seen === 0)
  337. $sql_seen = " AND `item`.`unseen` = 1 ";
  338. $r = q("SELECT COUNT(*) AS `total`
  339. FROM `item` INNER JOIN `item` AS `pitem` ON `pitem`.`id`=`item`.`parent`
  340. WHERE `item`.`visible` = 1 AND `pitem`.`parent` != 0 AND
  341. `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 0
  342. $sql_seen",
  343. intval(local_user())
  344. );
  345. if (dbm::is_result($r))
  346. return $r[0]['total'];
  347. return 0;
  348. }
  349. /**
  350. * @brief Get network notifications
  351. *
  352. * @param int|string $seen
  353. * If 0 only include notifications into the query
  354. * which aren't marked as "seen"
  355. * @param int $start Start the query at this point
  356. * @param int $limit Maximum number of query results
  357. *
  358. * @return array with
  359. * string 'ident' => Notification identifier
  360. * int 'total' => Total number of available network notifications
  361. * array 'notifications' => Network notifications
  362. */
  363. public function networkNotifs($seen = 0, $start = 0, $limit = 80) {
  364. $ident = 'network';
  365. $total = $this->networkTotal($seen);
  366. $notifs = array();
  367. $sql_seen = "";
  368. if($seen === 0)
  369. $sql_seen = " AND `item`.`unseen` = 1 ";
  370. $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`, `item`.`unseen`,
  371. `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object` AS `object`,
  372. `pitem`.`author-name` AS `pname`, `pitem`.`author-link` AS `plink`, `pitem`.`guid` AS `pguid`
  373. FROM `item` INNER JOIN `item` AS `pitem` ON `pitem`.`id`=`item`.`parent`
  374. WHERE `item`.`visible` = 1 AND `pitem`.`parent` != 0 AND
  375. `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 0
  376. $sql_seen
  377. ORDER BY `item`.`created` DESC LIMIT %d, %d ",
  378. intval(local_user()),
  379. intval($start),
  380. intval($limit)
  381. );
  382. if (dbm::is_result($r))
  383. $notifs = $this->formatNotifs($r, $ident);
  384. $arr = array (
  385. 'notifications' => $notifs,
  386. 'ident' => $ident,
  387. 'total' => $total,
  388. );
  389. return $arr;
  390. }
  391. /**
  392. * @brief Total number of system notifications
  393. * @param int|string $seen
  394. * If 0 only include notifications into the query
  395. * which aren't marked as "seen"
  396. * @return int Number of system notifications
  397. */
  398. private function systemTotal($seen = 0) {
  399. $sql_seen = "";
  400. if($seen === 0)
  401. $sql_seen = " AND `seen` = 0 ";
  402. $r = q("SELECT COUNT(*) AS `total` FROM `notify` WHERE `uid` = %d $sql_seen",
  403. intval(local_user())
  404. );
  405. if (dbm::is_result($r))
  406. return $r[0]['total'];
  407. return 0;
  408. }
  409. /**
  410. * @brief Get system notifications
  411. *
  412. * @param int|string $seen
  413. * If 0 only include notifications into the query
  414. * which aren't marked as "seen"
  415. * @param int $start Start the query at this point
  416. * @param int $limit Maximum number of query results
  417. *
  418. * @return array with
  419. * string 'ident' => Notification identifier
  420. * int 'total' => Total number of available system notifications
  421. * array 'notifications' => System notifications
  422. */
  423. public function systemNotifs($seen = 0, $start = 0, $limit = 80) {
  424. $ident = 'system';
  425. $total = $this->systemTotal($seen);
  426. $notifs = array();
  427. $sql_seen = "";
  428. if($seen === 0)
  429. $sql_seen = " AND `seen` = 0 ";
  430. $r = q("SELECT `id`, `url`, `photo`, `msg`, `date`, `seen` FROM `notify`
  431. WHERE `uid` = %d $sql_seen ORDER BY `date` DESC LIMIT %d, %d ",
  432. intval(local_user()),
  433. intval($start),
  434. intval($limit)
  435. );
  436. if (dbm::is_result($r))
  437. $notifs = $this->formatNotifs($r, $ident);
  438. $arr = array (
  439. 'notifications' => $notifs,
  440. 'ident' => $ident,
  441. 'total' => $total,
  442. );
  443. return $arr;
  444. }
  445. /**
  446. * @brief Addional SQL query string for the personal notifications
  447. *
  448. * @return string The additional sql query
  449. */
  450. private function _personal_sql_extra() {
  451. $myurl = $this->a->get_baseurl(true) . '/profile/'. $this->a->user['nickname'];
  452. $myurl = substr($myurl,strpos($myurl,'://')+3);
  453. $myurl = str_replace(array('www.','.'),array('','\\.'),$myurl);
  454. $diasp_url = str_replace('/profile/','/u/',$myurl);
  455. $sql_extra = sprintf(" AND ( `item`.`author-link` regexp '%s' OR `item`.`tag` regexp '%s' OR `item`.`tag` regexp '%s' ) ",
  456. dbesc($myurl . '$'),
  457. dbesc($myurl . '\\]'),
  458. dbesc($diasp_url . '\\]')
  459. );
  460. return $sql_extra;
  461. }
  462. /**
  463. * @brief Total number of personal notifications
  464. * @param int|string $seen
  465. * If 0 only include notifications into the query
  466. * which aren't marked as "seen"
  467. * @return int Number of personal notifications
  468. */
  469. private function personalTotal($seen = 0) {
  470. $sql_seen = "";
  471. $sql_extra = $this->_personal_sql_extra();
  472. if($seen === 0)
  473. $sql_seen = " AND `item`.`unseen` = 1 ";
  474. $r = q("SELECT COUNT(*) AS `total`
  475. FROM `item` INNER JOIN `item` AS `pitem` ON `pitem`.`id`=`item`.`parent`
  476. WHERE `item`.`visible` = 1
  477. $sql_extra
  478. $sql_seen
  479. AND `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 0 " ,
  480. intval(local_user())
  481. );
  482. if (dbm::is_result($r))
  483. return $r[0]['total'];
  484. return 0;
  485. }
  486. /**
  487. * @brief Get personal notifications
  488. *
  489. * @param int|string $seen
  490. * If 0 only include notifications into the query
  491. * which aren't marked as "seen"
  492. * @param int $start Start the query at this point
  493. * @param int $limit Maximum number of query results
  494. *
  495. * @return array with
  496. * string 'ident' => Notification identifier
  497. * int 'total' => Total number of available personal notifications
  498. * array 'notifications' => Personal notifications
  499. */
  500. public function personalNotifs($seen = 0, $start = 0, $limit = 80) {
  501. $ident = 'personal';
  502. $total = $this->personalTotal($seen);
  503. $sql_extra = $this->_personal_sql_extra();
  504. $notifs = array();
  505. $sql_seen = "";
  506. if($seen === 0)
  507. $sql_seen = " AND `item`.`unseen` = 1 ";
  508. $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`, `item`.`unseen`,
  509. `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object` AS `object`,
  510. `pitem`.`author-name` AS `pname`, `pitem`.`author-link` AS `plink`, `pitem`.`guid` AS `pguid`
  511. FROM `item` INNER JOIN `item` AS `pitem` ON `pitem`.`id`=`item`.`parent`
  512. WHERE `item`.`visible` = 1
  513. $sql_extra
  514. $sql_seen
  515. AND `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 0
  516. ORDER BY `item`.`created` DESC LIMIT %d, %d " ,
  517. intval(local_user()),
  518. intval($start),
  519. intval($limit)
  520. );
  521. if (dbm::is_result($r))
  522. $notifs = $this->formatNotifs($r, $ident);
  523. $arr = array (
  524. 'notifications' => $notifs,
  525. 'ident' => $ident,
  526. 'total' => $total,
  527. );
  528. return $arr;
  529. }
  530. /**
  531. * @brief Total number of home notifications
  532. * @param int|string $seen
  533. * If 0 only include notifications into the query
  534. * which aren't marked as "seen"
  535. * @return int Number of home notifications
  536. */
  537. private function homeTotal($seen = 0) {
  538. $sql_seen = "";
  539. if($seen === 0)
  540. $sql_seen = " AND `item`.`unseen` = 1 ";
  541. $r = q("SELECT COUNT(*) AS `total` FROM `item`
  542. WHERE `item`.`visible` = 1 AND
  543. `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 1
  544. $sql_seen",
  545. intval(local_user())
  546. );
  547. if (dbm::is_result($r))
  548. return $r[0]['total'];
  549. return 0;
  550. }
  551. /**
  552. * @brief Get home notifications
  553. *
  554. * @param int|string $seen
  555. * If 0 only include notifications into the query
  556. * which aren't marked as "seen"
  557. * @param int $start Start the query at this point
  558. * @param int $limit Maximum number of query results
  559. *
  560. * @return array with
  561. * string 'ident' => Notification identifier
  562. * int 'total' => Total number of available home notifications
  563. * array 'notifications' => Home notifications
  564. */
  565. public function homeNotifs($seen = 0, $start = 0, $limit = 80) {
  566. $ident = 'home';
  567. $total = $this->homeTotal($seen);
  568. $notifs = array();
  569. $sql_seen = "";
  570. if($seen === 0)
  571. $sql_seen = " AND `item`.`unseen` = 1 ";
  572. $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`, `item`.`unseen`,
  573. `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object` AS `object`,
  574. `pitem`.`author-name` AS `pname`, `pitem`.`author-link` AS `plink`, `pitem`.`guid` AS `pguid`
  575. FROM `item` INNER JOIN `item` AS `pitem` ON `pitem`.`id`=`item`.`parent`
  576. WHERE `item`.`visible` = 1 AND
  577. `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 1
  578. $sql_seen
  579. ORDER BY `item`.`created` DESC LIMIT %d, %d ",
  580. intval(local_user()),
  581. intval($start),
  582. intval($limit)
  583. );
  584. if (dbm::is_result($r))
  585. $notifs = $this->formatNotifs($r, $ident);
  586. $arr = array (
  587. 'notifications' => $notifs,
  588. 'ident' => $ident,
  589. 'total' => $total,
  590. );
  591. return $arr;
  592. }
  593. /**
  594. * @brief Total number of introductions
  595. * @param bool $all
  596. * If false only include introductions into the query
  597. * which aren't marked as ignored
  598. * @return int Number of introductions
  599. */
  600. private function introTotal($all = false) {
  601. $sql_extra = "";
  602. if(!$all)
  603. $sql_extra = " AND `ignore` = 0 ";
  604. $r = q("SELECT COUNT(*) AS `total` FROM `intro`
  605. WHERE `intro`.`uid` = %d $sql_extra AND `intro`.`blocked` = 0 ",
  606. intval($_SESSION['uid'])
  607. );
  608. if (dbm::is_result($r))
  609. return $r[0]['total'];
  610. return 0;
  611. }
  612. /**
  613. * @brief Get introductions
  614. *
  615. * @param bool $all
  616. * If false only include introductions into the query
  617. * which aren't marked as ignored
  618. * @param int $start Start the query at this point
  619. * @param int $limit Maximum number of query results
  620. *
  621. * @return array with
  622. * string 'ident' => Notification identifier
  623. * int 'total' => Total number of available introductions
  624. * array 'notifications' => Introductions
  625. */
  626. public function introNotifs($all = false, $start = 0, $limit = 80) {
  627. $ident = 'introductions';
  628. $total = $this->introTotal($seen);
  629. $notifs = array();
  630. $sql_extra = "";
  631. if(!$all)
  632. $sql_extra = " AND `ignore` = 0 ";
  633. /// @todo Fetch contact details by "get_contact_details_by_url" instead of queries to contact, fcontact and gcontact
  634. $r = q("SELECT `intro`.`id` AS `intro_id`, `intro`.*, `contact`.*,
  635. `fcontact`.`name` AS `fname`, `fcontact`.`url` AS `furl`,
  636. `fcontact`.`photo` AS `fphoto`, `fcontact`.`request` AS `frequest`,
  637. `gcontact`.`location` AS `glocation`, `gcontact`.`about` AS `gabout`,
  638. `gcontact`.`keywords` AS `gkeywords`, `gcontact`.`gender` AS `ggender`,
  639. `gcontact`.`network` AS `gnetwork`, `gcontact`.`addr` AS `gaddr`
  640. FROM `intro`
  641. LEFT JOIN `contact` ON `contact`.`id` = `intro`.`contact-id`
  642. LEFT JOIN `gcontact` ON `gcontact`.`nurl` = `contact`.`nurl`
  643. LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
  644. WHERE `intro`.`uid` = %d $sql_extra AND `intro`.`blocked` = 0
  645. LIMIT %d, %d",
  646. intval($_SESSION['uid']),
  647. intval($start),
  648. intval($limit)
  649. );
  650. if (dbm::is_result($r))
  651. $notifs = $this->formatIntros($r);
  652. $arr = array (
  653. 'ident' => $ident,
  654. 'total' => $total,
  655. 'notifications' => $notifs,
  656. );
  657. return $arr;
  658. }
  659. /**
  660. * @brief Format the notification query in an usable array
  661. *
  662. * @param array $intros The array from the db query
  663. * @return array with the introductions
  664. */
  665. private function formatIntros($intros) {
  666. $knowyou = '';
  667. foreach($intros as $it) {
  668. // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
  669. // We have to distinguish between these two because they use different data.
  670. // Contact suggestions
  671. if($it['fid']) {
  672. $return_addr = bin2hex($this->a->user['nickname'] . '@' . $this->a->get_hostname() . (($this->a->path) ? '/' . $this->a->path : ''));
  673. $intro = array(
  674. 'label' => 'friend_suggestion',
  675. 'notify_type' => t('Friend Suggestion'),
  676. 'intro_id' => $it['intro_id'],
  677. 'madeby' => $it['name'],
  678. 'contact_id' => $it['contact-id'],
  679. 'photo' => ((x($it,'fphoto')) ? proxy_url($it['fphoto'], false, PROXY_SIZE_SMALL) : "images/person-175.jpg"),
  680. 'name' => $it['fname'],
  681. 'url' => zrl($it['furl']),
  682. 'hidden' => $it['hidden'] == 1,
  683. 'post_newfriend' => (intval(get_pconfig(local_user(),'system','post_newfriend')) ? '1' : 0),
  684. 'knowyou' => $knowyou,
  685. 'note' => $it['note'],
  686. 'request' => $it['frequest'] . '?addr=' . $return_addr,
  687. );
  688. // Normal connection requests
  689. } else {
  690. $it = $this->getMissingIntroData($it);
  691. // Don't show these data until you are connected. Diaspora is doing the same.
  692. if($it['gnetwork'] === NETWORK_DIASPORA) {
  693. $it['glocation'] = "";
  694. $it['gabout'] = "";
  695. $it['ggender'] = "";
  696. }
  697. $intro = array(
  698. 'label' => (($it['network'] !== NETWORK_OSTATUS) ? 'friend_request' : 'follower'),
  699. 'notify_type' => (($it['network'] !== NETWORK_OSTATUS) ? t('Friend/Connect Request') : t('New Follower')),
  700. 'dfrn_id' => $it['issued-id'],
  701. 'uid' => $_SESSION['uid'],
  702. 'intro_id' => $it['intro_id'],
  703. 'contact_id' => $it['contact-id'],
  704. 'photo' => ((x($it,'photo')) ? proxy_url($it['photo'], false, PROXY_SIZE_SMALL) : "images/person-175.jpg"),
  705. 'name' => $it['name'],
  706. 'location' => bbcode($it['glocation'], false, false),
  707. 'about' => bbcode($it['gabout'], false, false),
  708. 'keywords' => $it['gkeywords'],
  709. 'gender' => $it['ggender'],
  710. 'hidden' => $it['hidden'] == 1,
  711. 'post_newfriend' => (intval(get_pconfig(local_user(),'system','post_newfriend')) ? '1' : 0),
  712. 'url' => $it['url'],
  713. 'zrl' => zrl($it['url']),
  714. 'addr' => $it['gaddr'],
  715. 'network' => $it['gnetwork'],
  716. 'knowyou' => $it['knowyou'],
  717. 'note' => $it['note'],
  718. );
  719. }
  720. $arr[] = $intro;
  721. }
  722. return $arr;
  723. }
  724. /**
  725. * @brief Check for missing contact data and try to fetch the data from
  726. * from other sources
  727. *
  728. * @param array $arr The input array with the intro data
  729. *
  730. * @return array The array with the intro data
  731. */
  732. private function getMissingIntroData($arr) {
  733. // If the network and the addr isn't available from the gcontact
  734. // table entry, take the one of the contact table entry
  735. if ($arr['gnetwork'] == "") {
  736. $arr['gnetwork'] = $arr['network'];
  737. }
  738. if ($arr['gaddr'] == "") {
  739. $arr['gaddr'] = $arr['addr'];
  740. }
  741. // If the network and addr is still not available
  742. // get the missing data data from other sources
  743. if ($arr['gnetwork'] == "" || $arr['gaddr'] == "") {
  744. $ret = get_contact_details_by_url($arr['url']);
  745. if ($arr['gnetwork'] == "" && $ret['network'] != "") {
  746. $arr['gnetwork'] = $ret['network'];
  747. }
  748. if ($arr['gaddr'] == "" && $ret['addr'] != "") {
  749. $arr['gaddr'] = $ret['addr'];
  750. }
  751. }
  752. return $arr;
  753. }
  754. }