Activitypub_profile.php 24 KB

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