Activitypub_profile.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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 = $profile->getUri();
  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): Activitypub_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. // extend the ap_profile with some information we
  202. // don't store in the database
  203. $fields = [
  204. 'nickname' => 'nickname',
  205. 'fullname' => 'fullname',
  206. 'bio' => 'bio'
  207. ];
  208. foreach ($fields as $af => $pf) {
  209. $aprofile->$af = $profile->$pf;
  210. }
  211. return $aprofile;
  212. }
  213. public static function from_profile_collection(array $profiles): array
  214. {
  215. $ap_profiles = [];
  216. foreach ($profiles as $profile) {
  217. try {
  218. $ap_profiles[] = self::from_profile($profile);
  219. } catch (Exception $e) {
  220. // Don't mind local profiles
  221. }
  222. }
  223. return $ap_profiles;
  224. }
  225. /**
  226. * Given an existent local profile creates an ActivityPub profile.
  227. * One must be careful not to give a user profile to this function
  228. * as only remote users have ActivityPub_profiles on local instance
  229. *
  230. * @param Profile $profile
  231. * @return Activitypub_profile
  232. * @throws HTTP_Request2_Exception
  233. * @throws Exception
  234. * @throws Exception
  235. * @author Diogo Cordeiro <diogo@fc.up.pt>
  236. */
  237. private static function create_from_local_profile(Profile $profile)
  238. {
  239. $aprofile = new Activitypub_profile();
  240. $url = $profile->getUri();
  241. $inboxes = Activitypub_explorer::get_actor_inboxes_uri($url);
  242. if ($inboxes == null) {
  243. throw new Exception('This is not an ActivityPub user thus AProfile is politely refusing to proceed.');
  244. }
  245. $aprofile->created = $aprofile->modified = common_sql_now();
  246. $aprofile = new Activitypub_profile;
  247. $aprofile->profile_id = $profile->getID();
  248. $aprofile->uri = $url;
  249. $aprofile->nickname = $profile->getNickname();
  250. $aprofile->fullname = $profile->getFullname();
  251. $aprofile->bio = substr($profile->getDescription(), 0, 1000);
  252. $aprofile->inboxuri = $inboxes["inbox"];
  253. $aprofile->sharedInboxuri = $inboxes["sharedInbox"];
  254. $aprofile->insert();
  255. return $aprofile;
  256. }
  257. /**
  258. * Returns sharedInbox if possible, inbox otherwise
  259. *
  260. * @return string Inbox URL
  261. * @author Diogo Cordeiro <diogo@fc.up.pt>
  262. */
  263. public function get_inbox()
  264. {
  265. if (is_null($this->sharedInboxuri)) {
  266. return $this->inboxuri;
  267. }
  268. return $this->sharedInboxuri;
  269. }
  270. /**
  271. * Getter for uri property
  272. *
  273. * @return string URI
  274. * @author Diogo Cordeiro <diogo@fc.up.pt>
  275. */
  276. public function getUri()
  277. {
  278. return $this->uri;
  279. }
  280. /**
  281. * Getter for url property
  282. *
  283. * @return string URL
  284. * @author Diogo Cordeiro <diogo@fc.up.pt>
  285. */
  286. public function getUrl()
  287. {
  288. return $this->getUri();
  289. }
  290. /**
  291. * Getter for id property
  292. *
  293. * @return int
  294. * @author Diogo Cordeiro <diogo@fc.up.pt>
  295. */
  296. public function getID()
  297. {
  298. return $this->profile_id;
  299. }
  300. /**
  301. * Ensures a valid Activitypub_profile when provided with a valid URI.
  302. *
  303. * @param string $url
  304. * @param bool $grab_online whether to try online grabbing, defaults to true
  305. * @return Activitypub_profile
  306. * @throws Exception if it isn't possible to return an Activitypub_profile
  307. * @author Diogo Cordeiro <diogo@fc.up.pt>
  308. */
  309. public static function fromUri($url, $grab_online = true)
  310. {
  311. try {
  312. return self::from_profile(Activitypub_explorer::get_profile_from_url($url, $grab_online));
  313. } catch (Exception $e) {
  314. throw new Exception('No valid ActivityPub profile found for given URI.');
  315. }
  316. }
  317. /**
  318. * Look up, and if necessary create, an Activitypub_profile for the remote
  319. * entity with the given WebFinger address.
  320. * This should never return null -- you will either get an object or
  321. * an exception will be thrown.
  322. *
  323. * @param string $addr WebFinger address
  324. * @return Activitypub_profile
  325. * @throws Exception on error conditions
  326. * @author Diogo Cordeiro <diogo@fc.up.pt>
  327. * @author GNU social
  328. */
  329. public static function ensure_webfinger($addr)
  330. {
  331. // Normalize $addr, i.e. add 'acct:' if missing
  332. $addr = Discovery::normalize($addr);
  333. // Try the cache
  334. $uri = self::cacheGet(sprintf('activitypub_profile:webfinger:%s', $addr));
  335. if ($uri !== false) {
  336. if (is_null($uri)) {
  337. // Negative cache entry
  338. // TRANS: Exception.
  339. throw new Exception(_m('Not a valid WebFinger address (via cache).'));
  340. }
  341. try {
  342. return self::fromUri($uri);
  343. } catch (Exception $e) {
  344. common_log(LOG_ERR, sprintf(__METHOD__ . ': WebFinger address cache inconsistent with database, did not find Activitypub_profile uri==%s', $uri));
  345. self::cacheSet(sprintf('activitypub_profile:webfinger:%s', $addr), false);
  346. }
  347. }
  348. // Now, try some discovery
  349. $disco = new Discovery();
  350. try {
  351. $xrd = $disco->lookup($addr);
  352. } catch (Exception $e) {
  353. // Save negative cache entry so we don't waste time looking it up again.
  354. // @todo FIXME: Distinguish temporary failures?
  355. self::cacheSet(sprintf('activitypub_profile:webfinger:%s', $addr), null);
  356. // TRANS: Exception.
  357. throw new Exception(_m('Not a valid WebFinger address.'));
  358. }
  359. $hints = array_merge(
  360. ['webfinger' => $addr],
  361. DiscoveryHints::fromXRD($xrd)
  362. );
  363. // If there's an Hcard, let's grab its info
  364. if (array_key_exists('hcard', $hints)) {
  365. if (!array_key_exists('profileurl', $hints) ||
  366. $hints['hcard'] != $hints['profileurl']) {
  367. $hcardHints = DiscoveryHints::fromHcardUrl($hints['hcard']);
  368. $hints = array_merge($hcardHints, $hints);
  369. }
  370. }
  371. // If we got a profile page, try that!
  372. $profileUrl = null;
  373. if (array_key_exists('profileurl', $hints)) {
  374. $profileUrl = $hints['profileurl'];
  375. try {
  376. common_log(LOG_INFO, "Discovery on acct:$addr with profile URL $profileUrl");
  377. $aprofile = self::fromUri($hints['profileurl']);
  378. self::cacheSet(sprintf('activitypub_profile:webfinger:%s', $addr), $aprofile->getUri());
  379. return $aprofile;
  380. } catch (Exception $e) {
  381. common_log(LOG_WARNING, "Failed creating profile from profile URL '$profileUrl': " . $e->getMessage());
  382. // keep looking
  383. //
  384. // @todo FIXME: This means an error discovering from profile page
  385. // may give us a corrupt entry using the webfinger URI, which
  386. // will obscure the correct page-keyed profile later on.
  387. }
  388. }
  389. // XXX: try hcard
  390. // XXX: try FOAF
  391. // TRANS: Exception. %s is a WebFinger address.
  392. throw new Exception(sprintf(_m('Could not find a valid profile for "%s".'), $addr));
  393. }
  394. /**
  395. * Update remote user profile in local instance
  396. * Depends on do_update
  397. *
  398. * @param Activitypub_profile $aprofile
  399. * @param array $res remote response
  400. * @return Profile remote Profile object
  401. * @throws NoProfileException
  402. * @author Diogo Cordeiro <diogo@fc.up.pt>
  403. */
  404. public static function update_profile($aprofile, $res)
  405. {
  406. // ActivityPub Profile
  407. $aprofile->uri = $res['id'];
  408. $aprofile->nickname = $res['preferredUsername'];
  409. $aprofile->fullname = isset($res['name']) ? $res['name'] : null;
  410. $aprofile->bio = isset($res['summary']) ? substr(strip_tags($res['summary']), 0, 1000) : null;
  411. $aprofile->inboxuri = $res['inbox'];
  412. $aprofile->sharedInboxuri = isset($res['endpoints']['sharedInbox']) ? $res['endpoints']['sharedInbox'] : $res['inbox'];
  413. $profile = $aprofile->local_profile();
  414. $profile->modified = $aprofile->modified = common_sql_now();
  415. $fields = [
  416. 'uri' => 'profileurl',
  417. 'nickname' => 'nickname',
  418. 'fullname' => 'fullname',
  419. 'bio' => 'bio'
  420. ];
  421. foreach ($fields as $af => $pf) {
  422. $profile->$pf = $aprofile->$af;
  423. }
  424. // Profile
  425. $profile->update();
  426. $aprofile->update();
  427. // Public Key
  428. Activitypub_rsa::update_public_key($profile, $res['publicKey']['publicKeyPem']);
  429. // Avatar
  430. if (isset($res['icon']['url'])) {
  431. try {
  432. Activitypub_explorer::update_avatar($profile, $res['icon']['url']);
  433. } catch (Exception $e) {
  434. // Let the exception go, it isn't a serious issue
  435. common_debug('An error ocurred while grabbing remote avatar' . $e->getMessage());
  436. }
  437. }
  438. return $profile;
  439. }
  440. /**
  441. * Getter for the number of subscribers of a
  442. * given local profile
  443. *
  444. * @param Profile $profile profile object
  445. * @return int number of subscribers
  446. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  447. */
  448. public static function subscriberCount(Profile $profile): int
  449. {
  450. $cnt = self::cacheGet(sprintf('activitypub_profile:subscriberCount:%d', $profile->id));
  451. if ($cnt !== false && is_int($cnt)) {
  452. return $cnt;
  453. }
  454. $sub = new Subscription();
  455. $sub->subscribed = $profile->id;
  456. $sub->whereAdd('subscriber != subscribed');
  457. $sub->whereAdd('subscriber IN (SELECT id FROM user UNION SELECT profile_id FROM activitypub_profile)');
  458. $cnt = $sub->count('distinct subscriber');
  459. self::cacheSet(sprintf('activitypub_profile:subscriberCount:%d', $profile->id), $cnt);
  460. return $cnt;
  461. }
  462. /**
  463. * Getter for the number of subscriptions of a
  464. * given local profile
  465. *
  466. * @param Profile $profile profile object
  467. * @return int number of subscriptions
  468. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  469. */
  470. public static function subscriptionCount(Profile $profile): int
  471. {
  472. $cnt = self::cacheGet(sprintf('activitypub_profile:subscriptionCount:%d', $profile->id));
  473. if ($cnt !== false && is_int($cnt)) {
  474. return $cnt;
  475. }
  476. $sub = new Subscription();
  477. $sub->subscriber = $profile->id;
  478. $sub->whereAdd('subscriber != subscribed');
  479. $sub->whereAdd('subscribed IN (SELECT id FROM user UNION SELECT profile_id FROM activitypub_profile)');
  480. $cnt = $sub->count('distinct subscribed');
  481. self::cacheSet(sprintf('activitypub_profile:subscriptionCount:%d', $profile->id), $cnt);
  482. return $cnt;
  483. }
  484. public static function updateSubscriberCount(Profile $profile, $adder)
  485. {
  486. $cnt = self::cacheGet(sprintf('activitypub_profile:subscriberCount:%d', $profile->id));
  487. if ($cnt !== false && is_int($cnt)) {
  488. self::cacheSet(sprintf('activitypub_profile:subscriberCount:%d', $profile->id), $cnt + $adder);
  489. }
  490. }
  491. public static function updateSubscriptionCount(Profile $profile, $adder)
  492. {
  493. $cnt = self::cacheGet(sprintf('activitypub_profile:subscriptionCount:%d', $profile->id));
  494. if ($cnt !== false && is_int($cnt)) {
  495. self::cacheSet(sprintf('activitypub_profile:subscriptionCount:%d', $profile->id), $cnt + $adder);
  496. }
  497. }
  498. /**
  499. * Getter for the subscriber profiles of a
  500. * given local profile
  501. *
  502. * @param Profile $profile profile object
  503. * @param int $offset index of the starting row to fetch from
  504. * @param int $limit maximum number of rows allowed for fetching
  505. * @return array subscriber profile objects
  506. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  507. */
  508. public static function getSubscribers(Profile $profile, $offset = 0, $limit = null): array
  509. {
  510. $cache = false;
  511. if ($offset + $limit <= Subscription::CACHE_WINDOW) {
  512. $subs = self::cacheGet(sprintf('activitypub_profile:subscriberCollection:%d', $profile->id));
  513. if ($subs !== false && is_array($subs)) {
  514. return array_slice($subs, $offset, $limit);
  515. }
  516. $cache = true;
  517. }
  518. $subs = Subscription::getSubscriberIDs($profile->id, $offset, $limit);
  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. if ($cache) {
  529. self::cacheSet(sprintf('activitypub_profile:subscriberCollection:%d', $profile->id), $profiles);
  530. }
  531. return $profiles;
  532. }
  533. /**
  534. * Getter for the subscribed profiles of a
  535. * given local profile
  536. *
  537. * @param Profile $profile profile object
  538. * @param int $offset index of the starting row to fetch from
  539. * @param int $limit maximum number of rows allowed for fetching
  540. * @return array subscribed profile objects
  541. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  542. */
  543. public static function getSubscribed(Profile $profile, $offset = 0, $limit = null): array
  544. {
  545. $cache = false;
  546. if ($offset + $limit <= Subscription::CACHE_WINDOW) {
  547. $subs = self::cacheGet(sprintf('activitypub_profile:subscribedCollection:%d', $profile->id));
  548. if (is_array($subs)) {
  549. return array_slice($subs, $offset, $limit);
  550. }
  551. $cache = true;
  552. }
  553. $subs = Subscription::getSubscribedIDs($profile->id, $offset, $limit);
  554. try {
  555. $profiles = [];
  556. $users = User::multiGet('id', $subs);
  557. foreach ($users->fetchAll() as $user) {
  558. $profiles[$user->id] = $user->getProfile();
  559. }
  560. $ap_profiles = Activitypub_profile::multiGet('profile_id', $subs);
  561. foreach ($ap_profiles->fetchAll() as $ap) {
  562. $profiles[$ap->getID()] = $ap->local_profile();
  563. }
  564. } catch (NoResultException $e) {
  565. return $e->obj;
  566. }
  567. if ($cache) {
  568. self::cacheSet(sprintf('activitypub_profile:subscribedCollection:%d', $profile->id), $profiles);
  569. }
  570. return $profiles;
  571. }
  572. /**
  573. * Update cached values that are relevant to
  574. * the users involved in a subscription
  575. *
  576. * @param Profile $actor subscriber profile object
  577. * @param Profile $other subscribed profile object
  578. * @return void
  579. * @throws Exception
  580. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  581. */
  582. public static function subscribeCacheUpdate(Profile $actor, Profile $other)
  583. {
  584. self::blow('activitypub_profile:subscribedCollection:%d', $actor->getID());
  585. self::blow('activitypub_profile:subscriberCollection:%d', $other->id);
  586. self::updateSubscriptionCount($actor, +1);
  587. self::updateSubscriberCount($other, +1);
  588. }
  589. /**
  590. * Update cached values that are relevant to
  591. * the users involved in an unsubscription
  592. *
  593. * @param Profile $actor subscriber profile object
  594. * @param Profile $other subscribed profile object
  595. * @return void
  596. * @throws Exception
  597. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  598. */
  599. public static function unsubscribeCacheUpdate(Profile $actor, Profile $other)
  600. {
  601. self::blow('activitypub_profile:subscribedCollection:%d', $actor->getID());
  602. self::blow('activitypub_profile:subscriberCollection:%d', $other->id);
  603. self::updateSubscriptionCount($actor, -1);
  604. self::updateSubscriberCount($other, -1);
  605. }
  606. }