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.

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