Activitypub_profile.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * ActivityPub implementation for GNU social
  18. *
  19. * @package GNUsocial
  20. * @author Diogo Cordeiro <diogo@fc.up.pt>
  21. * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org
  22. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  23. * @link http://www.gnu.org/software/social/
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * ActivityPub Profile
  28. *
  29. * @category Plugin
  30. * @package GNUsocial
  31. * @author Diogo Cordeiro <diogo@fc.up.pt>
  32. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  33. */
  34. class Activitypub_profile extends Managed_DataObject
  35. {
  36. public $__table = 'activitypub_profile';
  37. public $uri; // text() not_null
  38. public $profile_id; // int(4) primary_key not_null
  39. public $inboxuri; // text() not_null
  40. public $sharedInboxuri; // text()
  41. public $nickname; // varchar(64) multiple_key not_null
  42. public $fullname; // text()
  43. public $profileurl; // text()
  44. public $homepage; // text()
  45. public $bio; // text() multiple_key
  46. public $location; // text()
  47. public $created; // datetime() not_null default_CURRENT_TIMESTAMP
  48. public $modified; // datetime() not_null default_CURRENT_TIMESTAMP
  49. /**
  50. * Return table definition for Schema setup and DB_DataObject usage.
  51. *
  52. * @author Diogo Cordeiro <diogo@fc.up.pt>
  53. * @return array array of column definitions
  54. */
  55. public static function schemaDef()
  56. {
  57. return [
  58. 'fields' => [
  59. 'uri' => ['type' => 'text', 'not null' => true],
  60. 'profile_id' => ['type' => 'int', 'not null' => true],
  61. 'inboxuri' => ['type' => 'text', 'not null' => true],
  62. 'sharedInboxuri' => ['type' => 'text'],
  63. 'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
  64. 'modified' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
  65. ],
  66. 'primary key' => ['profile_id'],
  67. 'foreign keys' => [
  68. 'activitypub_profile_profile_id_fkey' => ['profile', ['profile_id' => 'id']],
  69. ],
  70. ];
  71. }
  72. /**
  73. * Generates a pretty profile from a Profile object
  74. *
  75. * @param Profile $profile
  76. * @return array array to be used in a response
  77. * @throws InvalidUrlException
  78. * @throws ServerException
  79. * @author Diogo Cordeiro <diogo@fc.up.pt>
  80. */
  81. public static function profile_to_array($profile)
  82. {
  83. $uri = ActivityPubPlugin::actor_uri($profile);
  84. $id = $profile->getID();
  85. $rsa = new Activitypub_rsa();
  86. $public_key = $rsa->ensure_public_key($profile);
  87. unset($rsa);
  88. $res = [
  89. '@context' => [
  90. 'https://www.w3.org/ns/activitystreams',
  91. 'https://w3id.org/security/v1',
  92. [
  93. 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers'
  94. ]
  95. ],
  96. 'id' => $uri,
  97. 'type' => 'Person',
  98. 'following' => common_local_url('apActorFollowing', ['id' => $id]),
  99. 'followers' => common_local_url('apActorFollowers', ['id' => $id]),
  100. 'liked' => common_local_url('apActorLiked', ['id' => $id]),
  101. 'inbox' => common_local_url('apInbox', ['id' => $id]),
  102. 'outbox' => common_local_url('apActorOutbox', ['id' => $id]),
  103. 'preferredUsername' => $profile->getNickname(),
  104. 'name' => $profile->getBestName(),
  105. 'summary' => ($desc = $profile->getDescription()) == null ? "" : $desc,
  106. 'url' => $profile->getUrl(),
  107. 'manuallyApprovesFollowers' => false,
  108. 'publicKey' => [
  109. 'id' => $uri."#public-key",
  110. 'owner' => $uri,
  111. 'publicKeyPem' => $public_key
  112. ],
  113. 'tag' => [],
  114. 'attachment' => [],
  115. 'icon' => [
  116. 'type' => 'Image',
  117. 'mediaType' => 'image/png',
  118. 'height' => AVATAR_PROFILE_SIZE,
  119. 'width' => AVATAR_PROFILE_SIZE,
  120. 'url' => $profile->avatarUrl(AVATAR_PROFILE_SIZE)
  121. ]
  122. ];
  123. if ($profile->isLocal()) {
  124. $res['endpoints']['sharedInbox'] = common_local_url('apInbox');
  125. } else {
  126. $aprofile = new Activitypub_profile();
  127. $aprofile = $aprofile->from_profile($profile);
  128. $res['endpoints']['sharedInbox'] = $aprofile->sharedInboxuri;
  129. }
  130. return $res;
  131. }
  132. /**
  133. * Insert the current object variables into the database
  134. *
  135. * @author Diogo Cordeiro <diogo@fc.up.pt>
  136. * @access public
  137. * @throws ServerException
  138. */
  139. public function do_insert()
  140. {
  141. $profile = new Profile();
  142. $profile->created = $this->created = $this->modified = common_sql_now();
  143. $fields = [
  144. 'uri' => 'profileurl',
  145. 'nickname' => 'nickname',
  146. 'fullname' => 'fullname',
  147. 'bio' => 'bio'
  148. ];
  149. foreach ($fields as $af => $pf) {
  150. $profile->$pf = $this->$af;
  151. }
  152. $this->profile_id = $profile->insert();
  153. if ($this->profile_id === false) {
  154. $profile->query('ROLLBACK');
  155. throw new ServerException('Profile insertion failed.');
  156. }
  157. $ok = $this->insert();
  158. if ($ok === false) {
  159. $profile->query('ROLLBACK');
  160. $this->query('ROLLBACK');
  161. throw new ServerException('Cannot save ActivityPub profile.');
  162. }
  163. }
  164. /**
  165. * Fetch the locally stored profile for this Activitypub_profile
  166. *
  167. * @return Profile
  168. * @throws NoProfileException if it was not found
  169. * @author Diogo Cordeiro <diogo@fc.up.pt>
  170. */
  171. public function local_profile()
  172. {
  173. $profile = Profile::getKV('id', $this->profile_id);
  174. if (!$profile instanceof Profile) {
  175. throw new NoProfileException($this->profile_id);
  176. }
  177. return $profile;
  178. }
  179. /**
  180. * Generates an Activitypub_profile from a Profile
  181. *
  182. * @author Diogo Cordeiro <diogo@fc.up.pt>
  183. * @param Profile $profile
  184. * @return Activitypub_profile
  185. * @throws Exception if no Activitypub_profile exists for given Profile
  186. */
  187. public static function from_profile(Profile $profile)
  188. {
  189. $profile_id = $profile->getID();
  190. $aprofile = self::getKV('profile_id', $profile_id);
  191. if (!$aprofile instanceof Activitypub_profile) {
  192. // No Activitypub_profile for this profile_id,
  193. if (!$profile->isLocal()) {
  194. // create one!
  195. $aprofile = self::create_from_local_profile($profile);
  196. } else {
  197. throw new Exception('No Activitypub_profile for Profile ID: '.$profile_id. ', this is a local user.');
  198. }
  199. }
  200. $fields = [
  201. 'uri' => 'profileurl',
  202. 'nickname' => 'nickname',
  203. 'fullname' => 'fullname',
  204. 'bio' => 'bio'
  205. ];
  206. foreach ($fields as $af => $pf) {
  207. $aprofile->$af = $profile->$pf;
  208. }
  209. return $aprofile;
  210. }
  211. public static function from_profile_collection(array $profiles): array {
  212. $ap_profiles = [];
  213. foreach ($profiles as $profile) {
  214. try {
  215. $ap_profiles[] = self::from_profile($profile);
  216. } catch (Exception $e) {
  217. // Don't mind local profiles
  218. }
  219. }
  220. return $ap_profiles;
  221. }
  222. /**
  223. * Given an existent local profile creates an ActivityPub profile.
  224. * One must be careful not to give a user profile to this function
  225. * as only remote users have ActivityPub_profiles on local instance
  226. *
  227. * @param Profile $profile
  228. * @return Activitypub_profile
  229. * @throws HTTP_Request2_Exception
  230. * @author Diogo Cordeiro <diogo@fc.up.pt>
  231. */
  232. private static function create_from_local_profile(Profile $profile)
  233. {
  234. $aprofile = new Activitypub_profile();
  235. $url = $profile->getUri();
  236. $inboxes = Activitypub_explorer::get_actor_inboxes_uri($url);
  237. if ($inboxes == null) {
  238. throw new Exception('This is not an ActivityPub user thus AProfile is politely refusing to proceed.');
  239. }
  240. $aprofile->created = $aprofile->modified = common_sql_now();
  241. $aprofile = new Activitypub_profile;
  242. $aprofile->profile_id = $profile->getID();
  243. $aprofile->uri = $url;
  244. $aprofile->nickname = $profile->getNickname();
  245. $aprofile->fullname = $profile->getFullname();
  246. $aprofile->bio = substr($profile->getDescription(), 0, 1000);
  247. $aprofile->inboxuri = $inboxes["inbox"];
  248. $aprofile->sharedInboxuri = $inboxes["sharedInbox"];
  249. $aprofile->insert();
  250. return $aprofile;
  251. }
  252. /**
  253. * Returns sharedInbox if possible, inbox otherwise
  254. *
  255. * @author Diogo Cordeiro <diogo@fc.up.pt>
  256. * @return string Inbox URL
  257. */
  258. public function get_inbox()
  259. {
  260. if (is_null($this->sharedInboxuri)) {
  261. return $this->inboxuri;
  262. }
  263. return $this->sharedInboxuri;
  264. }
  265. /**
  266. * Getter for uri property
  267. *
  268. * @author Diogo Cordeiro <diogo@fc.up.pt>
  269. * @return string URI
  270. */
  271. public function getUri()
  272. {
  273. return $this->uri;
  274. }
  275. /**
  276. * Getter for url property
  277. *
  278. * @author Diogo Cordeiro <diogo@fc.up.pt>
  279. * @return string URL
  280. */
  281. public function getUrl()
  282. {
  283. return $this->getUri();
  284. }
  285. /**
  286. * Getter for id property
  287. *
  288. * @author Diogo Cordeiro <diogo@fc.up.pt>
  289. * @return int
  290. */
  291. public function getID()
  292. {
  293. return $this->profile_id;
  294. }
  295. /**
  296. * Ensures a valid Activitypub_profile when provided with a valid URI.
  297. *
  298. * @param string $url
  299. * @param bool $grab_online whether to try online grabbing, defaults to true
  300. * @return Activitypub_profile
  301. * @throws Exception if it isn't possible to return an Activitypub_profile
  302. * @author Diogo Cordeiro <diogo@fc.up.pt>
  303. */
  304. public static function fromUri($url, $grab_online = true)
  305. {
  306. try {
  307. return self::from_profile(Activitypub_explorer::get_profile_from_url($url, $grab_online));
  308. } catch (Exception $e) {
  309. throw new Exception('No valid ActivityPub profile found for given URI.');
  310. }
  311. }
  312. /**
  313. * Look up, and if necessary create, an Activitypub_profile for the remote
  314. * entity with the given WebFinger address.
  315. * This should never return null -- you will either get an object or
  316. * an exception will be thrown.
  317. *
  318. * @author GNU social
  319. * @author Diogo Cordeiro <diogo@fc.up.pt>
  320. * @param string $addr WebFinger address
  321. * @return Activitypub_profile
  322. * @throws Exception on error conditions
  323. */
  324. public static function ensure_webfinger($addr)
  325. {
  326. // Normalize $addr, i.e. add 'acct:' if missing
  327. $addr = Discovery::normalize($addr);
  328. // Try the cache
  329. $uri = self::cacheGet(sprintf('activitypub_profile:webfinger:%s', $addr));
  330. if ($uri !== false) {
  331. if (is_null($uri)) {
  332. // Negative cache entry
  333. // TRANS: Exception.
  334. throw new Exception(_m('Not a valid WebFinger address (via cache).'));
  335. }
  336. try {
  337. return self::fromUri($uri);
  338. } catch (Exception $e) {
  339. common_log(LOG_ERR, sprintf(__METHOD__ . ': WebFinger address cache inconsistent with database, did not find Activitypub_profile uri==%s', $uri));
  340. self::cacheSet(sprintf('activitypub_profile:webfinger:%s', $addr), false);
  341. }
  342. }
  343. // Now, try some discovery
  344. $disco = new Discovery();
  345. try {
  346. $xrd = $disco->lookup($addr);
  347. } catch (Exception $e) {
  348. // Save negative cache entry so we don't waste time looking it up again.
  349. // @todo FIXME: Distinguish temporary failures?
  350. self::cacheSet(sprintf('activitypub_profile:webfinger:%s', $addr), null);
  351. // TRANS: Exception.
  352. throw new Exception(_m('Not a valid WebFinger address.'));
  353. }
  354. $hints = array_merge(
  355. ['webfinger' => $addr],
  356. DiscoveryHints::fromXRD($xrd)
  357. );
  358. // If there's an Hcard, let's grab its info
  359. if (array_key_exists('hcard', $hints)) {
  360. if (!array_key_exists('profileurl', $hints) ||
  361. $hints['hcard'] != $hints['profileurl']) {
  362. $hcardHints = DiscoveryHints::fromHcardUrl($hints['hcard']);
  363. $hints = array_merge($hcardHints, $hints);
  364. }
  365. }
  366. // If we got a profile page, try that!
  367. $profileUrl = null;
  368. if (array_key_exists('profileurl', $hints)) {
  369. $profileUrl = $hints['profileurl'];
  370. try {
  371. common_log(LOG_INFO, "Discovery on acct:$addr with profile URL $profileUrl");
  372. $aprofile = self::fromUri($hints['profileurl']);
  373. self::cacheSet(sprintf('activitypub_profile:webfinger:%s', $addr), $aprofile->getUri());
  374. return $aprofile;
  375. } catch (Exception $e) {
  376. common_log(LOG_WARNING, "Failed creating profile from profile URL '$profileUrl': " . $e->getMessage());
  377. // keep looking
  378. //
  379. // @todo FIXME: This means an error discovering from profile page
  380. // may give us a corrupt entry using the webfinger URI, which
  381. // will obscure the correct page-keyed profile later on.
  382. }
  383. }
  384. // XXX: try hcard
  385. // XXX: try FOAF
  386. // TRANS: Exception. %s is a WebFinger address.
  387. throw new Exception(sprintf(_m('Could not find a valid profile for "%s".'), $addr));
  388. }
  389. /**
  390. * Update remote user profile in local instance
  391. * Depends on do_update
  392. *
  393. * @param Activitypub_profile $aprofile
  394. * @param array $res remote response
  395. * @return Profile remote Profile object
  396. * @throws Exception
  397. * @author Diogo Cordeiro <diogo@fc.up.pt>
  398. */
  399. public static function update_profile($aprofile, $res)
  400. {
  401. // ActivityPub Profile
  402. $aprofile->uri = $res['id'];
  403. $aprofile->nickname = $res['preferredUsername'];
  404. $aprofile->fullname = isset($res['name']) ? $res['name'] : null;
  405. $aprofile->bio = isset($res['summary']) ? substr(strip_tags($res['summary']), 0, 1000) : null;
  406. $aprofile->inboxuri = $res['inbox'];
  407. $aprofile->sharedInboxuri = isset($res['endpoints']['sharedInbox']) ? $res['endpoints']['sharedInbox'] : $res['inbox'];
  408. $profile = $aprofile->local_profile();
  409. $profile->modified = $aprofile->modified = common_sql_now();
  410. $fields = [
  411. 'uri' => 'profileurl',
  412. 'nickname' => 'nickname',
  413. 'fullname' => 'fullname',
  414. 'bio' => 'bio'
  415. ];
  416. foreach ($fields as $af => $pf) {
  417. $profile->$pf = $aprofile->$af;
  418. }
  419. // Profile
  420. $profile->update();
  421. $aprofile->update();
  422. // Public Key
  423. Activitypub_rsa::update_public_key($profile, $res['publicKey']['publicKeyPem']);
  424. // Avatar
  425. if (isset($res['icon']['url'])) {
  426. try {
  427. Activitypub_explorer::update_avatar($profile, $res['icon']['url']);
  428. } catch (Exception $e) {
  429. // Let the exception go, it isn't a serious issue
  430. common_debug('An error ocurred while grabbing remote avatar'.$e->getMessage());
  431. }
  432. }
  433. return $profile;
  434. }
  435. /**
  436. * Getter for the number of subscribers of a
  437. * given local profile
  438. *
  439. * @param Profile $profile profile object
  440. * @return int number of subscribers
  441. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  442. */
  443. public static function subscriberCount(Profile $profile): int {
  444. $cnt = self::cacheGet(sprintf('activitypub_profile:subscriberCount:%d', $profile->id));
  445. if ($cnt !== false && is_int($cnt)) {
  446. return $cnt;
  447. }
  448. $sub = new Subscription();
  449. $sub->subscribed = $profile->id;
  450. $sub->whereAdd('subscriber != subscribed');
  451. $sub->whereAdd('subscriber IN (SELECT id FROM user UNION SELECT profile_id FROM activitypub_profile)');
  452. $cnt = $sub->count('distinct subscriber');
  453. self::cacheSet(sprintf('activitypub_profile:subscriberCount:%d', $profile->id), $cnt);
  454. return $cnt;
  455. }
  456. /**
  457. * Getter for the number of subscriptions of a
  458. * given local profile
  459. *
  460. * @param Profile $profile profile object
  461. * @return int number of subscriptions
  462. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  463. */
  464. public static function subscriptionCount(Profile $profile): int {
  465. $cnt = self::cacheGet(sprintf('activitypub_profile:subscriptionCount:%d', $profile->id));
  466. if ($cnt !== false && is_int($cnt)) {
  467. return $cnt;
  468. }
  469. $sub = new Subscription();
  470. $sub->subscriber = $profile->id;
  471. $sub->whereAdd('subscriber != subscribed');
  472. $sub->whereAdd('subscribed IN (SELECT id FROM user UNION SELECT profile_id FROM activitypub_profile)');
  473. $cnt = $sub->count('distinct subscribed');
  474. self::cacheSet(sprintf('activitypub_profile:subscriptionCount:%d', $profile->id), $cnt);
  475. return $cnt;
  476. }
  477. public static function updateSubscriberCount(Profile $profile, $adder) {
  478. $cnt = self::cacheGet(sprintf('activitypub_profile:subscriberCount:%d', $profile->id));
  479. if ($cnt !== false && is_int($cnt)) {
  480. self::cacheSet(sprintf('activitypub_profile:subscriberCount:%d', $profile->id), $cnt+$adder);
  481. }
  482. }
  483. public static function updateSubscriptionCount(Profile $profile, $adder) {
  484. $cnt = self::cacheGet(sprintf('activitypub_profile:subscriptionCount:%d', $profile->id));
  485. if ($cnt !== false && is_int($cnt)) {
  486. self::cacheSet(sprintf('activitypub_profile:subscriptionCount:%d', $profile->id), $cnt+$adder);
  487. }
  488. }
  489. /**
  490. * Getter for the subscriber profiles of a
  491. * given local profile
  492. *
  493. * @param Profile $profile profile object
  494. * @param int $offset index of the starting row to fetch from
  495. * @param int $limit maximum number of rows allowed for fetching
  496. * @return array subscriber profile objects
  497. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  498. */
  499. public static function getSubscribers(Profile $profile, $offset = 0, $limit = null): array {
  500. $cache = false;
  501. if ($offset + $limit <= Subscription::CACHE_WINDOW) {
  502. $subs = self::cacheGet(sprintf('activitypub_profile:subscriberCollection:%d', $profile->id));
  503. if ($subs !== false && is_array($subs)) {
  504. return array_slice($subs, $offset, $limit);
  505. }
  506. $cache = true;
  507. }
  508. $subs = Subscription::getSubscriberIDs($profile->id, $offset, $limit);
  509. try {
  510. $profiles = [];
  511. $users = User::multiGet('id', $subs);
  512. foreach ($users->fetchAll() as $user) {
  513. $profiles[$user->id] = $user->getProfile();
  514. }
  515. $ap_profiles = Activitypub_profile::multiGet('profile_id', $subs);
  516. foreach ($ap_profiles->fetchAll() as $ap) {
  517. $profiles[$ap->getID()] = $ap->local_profile();
  518. }
  519. } catch (NoResultException $e) {
  520. return $e->obj;
  521. }
  522. if ($cache) {
  523. self::cacheSet(sprintf('activitypub_profile:subscriberCollection:%d', $profile->id), $profiles);
  524. }
  525. return $profiles;
  526. }
  527. /**
  528. * Getter for the subscribed profiles of a
  529. * given local profile
  530. *
  531. * @param Profile $profile profile object
  532. * @param int $offset index of the starting row to fetch from
  533. * @param int $limit maximum number of rows allowed for fetching
  534. * @return array subscribed profile objects
  535. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  536. */
  537. public static function getSubscribed(Profile $profile, $offset = 0, $limit = null): array {
  538. $cache = false;
  539. if ($offset + $limit <= Subscription::CACHE_WINDOW) {
  540. $subs = self::cacheGet(sprintf('activitypub_profile:subscribedCollection:%d', $profile->id));
  541. if (is_array($subs)) {
  542. return array_slice($subs, $offset, $limit);
  543. }
  544. $cache = true;
  545. }
  546. $subs = Subscription::getSubscribedIDs($profile->id, $offset, $limit);
  547. try {
  548. $profiles = [];
  549. $users = User::multiGet('id', $subs);
  550. foreach ($users->fetchAll() as $user) {
  551. $profiles[$user->id] = $user->getProfile();
  552. }
  553. $ap_profiles = Activitypub_profile::multiGet('profile_id', $subs);
  554. foreach ($ap_profiles->fetchAll() as $ap) {
  555. $profiles[$ap->getID()] = $ap->local_profile();
  556. }
  557. } catch (NoResultException $e) {
  558. return $e->obj;
  559. }
  560. if ($cache) {
  561. self::cacheSet(sprintf('activitypub_profile:subscribedCollection:%d', $profile->id), $profiles);
  562. }
  563. return $profiles;
  564. }
  565. /**
  566. * Update cached values that are relevant to
  567. * the users involved in a subscription
  568. *
  569. * @param Profile $actor subscriber profile object
  570. * @param Profile $other subscribed profile object
  571. * @return void
  572. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  573. */
  574. public static function subscribeCacheUpdate(Profile $actor, Profile $other) {
  575. self::blow('activitypub_profile:subscribedCollection:%d', $actor->getID());
  576. self::blow('activitypub_profile:subscriberCollection:%d', $other->id);
  577. self::updateSubscriptionCount($actor, +1);
  578. self::updateSubscriberCount($other, +1);
  579. }
  580. /**
  581. * Update cached values that are relevant to
  582. * the users involved in an unsubscription
  583. *
  584. * @param Profile $actor subscriber profile object
  585. * @param Profile $other subscribed profile object
  586. * @return void
  587. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  588. */
  589. public static function unsubscribeCacheUpdate(Profile $actor, Profile $other) {
  590. self::blow('activitypub_profile:subscribedCollection:%d', $actor->getID());
  591. self::blow('activitypub_profile:subscriberCollection:%d', $other->id);
  592. self::updateSubscriptionCount($actor, -1);
  593. self::updateSubscriberCount($other, -1);
  594. }
  595. }