Activitypub_profile.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  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()
  48. public $modified; // timestamp() 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', 'description' => 'date this record was created'],
  64. 'modified' => ['type' => 'timestamp', 'not null' => true, '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 $profile): array
  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(): void
  141. {
  142. // Does any other protocol have this remote entity we're about to add ?
  143. Event::handle('StartTFNLookup', [$this->uri, get_class($this), &$profile_id]);
  144. if (!is_null($profile_id)) {
  145. // Yes! Avoid creating a new profile
  146. $this->profile_id = $profile_id;
  147. $this->created = $this->modified = common_sql_now();
  148. if ($this->insert() === false) {
  149. $this->query('ROLLBACK');
  150. throw new ServerException('Cannot save ActivityPub profile.');
  151. }
  152. // Update existing profile with received data
  153. $profile = Profile::getKV('id', $profile_id);
  154. self::update_local_profile($profile, $this);
  155. // Ask TFN to handle profile duplication
  156. Event::handle('EndTFNLookup', [get_class($this), $profile_id]);
  157. } else {
  158. // No, create both a new profile and remote profile
  159. $profile = new Profile();
  160. $profile->created = $this->created = $this->modified = common_sql_now();
  161. self::update_local_profile($profile, $this);
  162. $this->profile_id = $profile->insert();
  163. if ($this->profile_id === false) {
  164. $profile->query('ROLLBACK');
  165. throw new ServerException('Profile insertion failed.');
  166. }
  167. $ok = $this->insert();
  168. if ($ok === false) {
  169. $profile->query('ROLLBACK');
  170. $this->query('ROLLBACK');
  171. throw new ServerException('Cannot save ActivityPub profile.');
  172. }
  173. }
  174. }
  175. /**
  176. * Fetch the locally stored profile for this Activitypub_profile
  177. *
  178. * @return Profile
  179. * @throws NoProfileException if it was not found
  180. * @author Diogo Cordeiro <diogo@fc.up.pt>
  181. */
  182. public function local_profile(): Profile
  183. {
  184. $profile = Profile::getKV('id', $this->profile_id);
  185. if (!$profile instanceof Profile) {
  186. throw new NoProfileException($this->profile_id);
  187. }
  188. return $profile;
  189. }
  190. /**
  191. * Generates an Activitypub_profile from a Profile
  192. *
  193. * @param Profile $profile
  194. * @return Activitypub_profile
  195. * @throws Exception if no Activitypub_profile exists for given Profile
  196. * @author Diogo Cordeiro <diogo@fc.up.pt>
  197. */
  198. public static function from_profile(Profile $profile): Activitypub_profile
  199. {
  200. $profile_id = $profile->getID();
  201. $aprofile = self::getKV('profile_id', $profile_id);
  202. if (!$aprofile instanceof Activitypub_profile) {
  203. // No Activitypub_profile for this profile_id,
  204. if (!$profile->isLocal()) {
  205. // create one!
  206. $aprofile = self::create_from_local_profile($profile);
  207. } else {
  208. throw new Exception('No Activitypub_profile for Profile ID: ' . $profile_id . ', this is a local user.');
  209. }
  210. }
  211. // extend the ap_profile with some information we
  212. // don't store in the database
  213. $fields = [
  214. 'nickname' => 'nickname',
  215. 'fullname' => 'fullname',
  216. 'bio' => 'bio'
  217. ];
  218. foreach ($fields as $af => $pf) {
  219. $aprofile->$af = $profile->$pf;
  220. }
  221. return $aprofile;
  222. }
  223. /**
  224. * Travels an array of Profile and returns an array of Activitypub_profile
  225. *
  226. * @param array of Profile $profiles
  227. * @return array of Activitypub_profile
  228. */
  229. public static function from_profile_collection(array $profiles): array
  230. {
  231. $ap_profiles = [];
  232. foreach ($profiles as $profile) {
  233. try {
  234. $ap_profiles[] = self::from_profile($profile);
  235. } catch (Exception $e) {
  236. // Don't mind local profiles
  237. }
  238. }
  239. return $ap_profiles;
  240. }
  241. /**
  242. * Given an existent local profile creates an ActivityPub profile.
  243. * One must be careful not to give a user profile to this function
  244. * as only remote users have ActivityPub_profiles on local instance
  245. *
  246. * @param Profile $profile
  247. * @return Activitypub_profile
  248. * @throws HTTP_Request2_Exception
  249. * @throws Exception
  250. * @throws Exception
  251. * @author Diogo Cordeiro <diogo@fc.up.pt>
  252. */
  253. private static function create_from_local_profile(Profile $profile): Activitypub_profile
  254. {
  255. $aprofile = new Activitypub_profile();
  256. $url = $profile->getUri();
  257. $inboxes = Activitypub_explorer::get_actor_inboxes_uri($url);
  258. if ($inboxes === false) {
  259. throw new Exception('This is not an ActivityPub user thus AProfile is politely refusing to proceed.');
  260. }
  261. $aprofile->created = $aprofile->modified = common_sql_now();
  262. $aprofile = new Activitypub_profile;
  263. $aprofile->profile_id = $profile->getID();
  264. $aprofile->uri = $url;
  265. $aprofile->nickname = $profile->getNickname();
  266. $aprofile->fullname = $profile->getFullname();
  267. $aprofile->bio = substr($profile->getDescription(), 0, 1000);
  268. $aprofile->inboxuri = $inboxes["inbox"];
  269. $aprofile->sharedInboxuri = $inboxes["sharedInbox"];
  270. $aprofile->insert();
  271. return $aprofile;
  272. }
  273. /**
  274. * Returns sharedInbox if possible, inbox otherwise
  275. *
  276. * @return string Inbox URL
  277. * @author Diogo Cordeiro <diogo@fc.up.pt>
  278. */
  279. public function get_inbox(): string
  280. {
  281. if (is_null($this->sharedInboxuri)) {
  282. return $this->inboxuri;
  283. }
  284. return $this->sharedInboxuri;
  285. }
  286. /**
  287. * Getter for uri property
  288. *
  289. * @return string URI
  290. * @author Diogo Cordeiro <diogo@fc.up.pt>
  291. */
  292. public function getUri(): string
  293. {
  294. return $this->uri;
  295. }
  296. /**
  297. * Getter for url property
  298. *
  299. * @return string URL
  300. * @author Diogo Cordeiro <diogo@fc.up.pt>
  301. */
  302. public function getUrl(): string
  303. {
  304. return $this->getUri();
  305. }
  306. /**
  307. * Getter for id property
  308. *
  309. * @return int
  310. * @author Diogo Cordeiro <diogo@fc.up.pt>
  311. */
  312. public function getID(): int
  313. {
  314. return $this->profile_id;
  315. }
  316. /**
  317. * Ensures a valid Activitypub_profile when provided with a valid URI.
  318. *
  319. * @param string $url
  320. * @param bool $grab_online whether to try online grabbing, defaults to true
  321. * @return Activitypub_profile
  322. * @throws Exception if it isn't possible to return an Activitypub_profile
  323. * @author Diogo Cordeiro <diogo@fc.up.pt>
  324. */
  325. public static function fromUri(string $url, bool $grab_online = true): Activitypub_profile
  326. {
  327. try {
  328. return self::from_profile(Activitypub_explorer::get_profile_from_url($url, $grab_online));
  329. } catch (Exception $e) {
  330. throw new Exception('No valid ActivityPub profile found for given URI.');
  331. }
  332. }
  333. /**
  334. * Look up, and if necessary create, an Activitypub_profile for the remote
  335. * entity with the given WebFinger address.
  336. * This should never return null -- you will either get an object or
  337. * an exception will be thrown.
  338. *
  339. * @param string $addr WebFinger address
  340. * @return Activitypub_profile
  341. * @throws Exception on error conditions
  342. * @author Diogo Cordeiro <diogo@fc.up.pt>
  343. * @author GNU social
  344. */
  345. public static function ensure_webfinger(string $addr): Activitypub_profile
  346. {
  347. // Normalize $addr, i.e. add 'acct:' if missing
  348. $addr = Discovery::normalize($addr);
  349. // Try the cache
  350. $uri = self::cacheGet(sprintf('activitypub_profile:webfinger:%s', $addr));
  351. if ($uri !== false) {
  352. if (is_null($uri)) {
  353. // Negative cache entry
  354. // TRANS: Exception.
  355. throw new Exception(_m('Not a valid WebFinger address (via cache).'));
  356. }
  357. try {
  358. return self::fromUri($uri);
  359. } catch (Exception $e) {
  360. common_log(LOG_ERR, sprintf(__METHOD__ . ': WebFinger address cache inconsistent with database, did not find Activitypub_profile uri==%s', $uri));
  361. self::cacheSet(sprintf('activitypub_profile:webfinger:%s', $addr), false);
  362. }
  363. }
  364. // Now, try some discovery
  365. $disco = new Discovery();
  366. try {
  367. $xrd = $disco->lookup($addr);
  368. } catch (Exception $e) {
  369. // Save negative cache entry so we don't waste time looking it up again.
  370. // @todo FIXME: Distinguish temporary failures?
  371. self::cacheSet(sprintf('activitypub_profile:webfinger:%s', $addr), null);
  372. // TRANS: Exception.
  373. throw new Exception(_m('Not a valid WebFinger address.'));
  374. }
  375. $hints = array_merge(
  376. ['webfinger' => $addr],
  377. DiscoveryHints::fromXRD($xrd)
  378. );
  379. // If there's an Hcard, let's grab its info
  380. if (array_key_exists('hcard', $hints)) {
  381. if (!array_key_exists('profileurl', $hints) ||
  382. $hints['hcard'] != $hints['profileurl']) {
  383. $hcardHints = DiscoveryHints::fromHcardUrl($hints['hcard']);
  384. $hints = array_merge($hcardHints, $hints);
  385. }
  386. }
  387. // If we got a profile page, try that!
  388. $profileUrl = null;
  389. if (array_key_exists('profileurl', $hints)) {
  390. $profileUrl = $hints['profileurl'];
  391. try {
  392. common_log(LOG_INFO, "Discovery on acct:$addr with profile URL $profileUrl");
  393. $aprofile = self::fromUri($hints['profileurl']);
  394. self::cacheSet(sprintf('activitypub_profile:webfinger:%s', $addr), $aprofile->getUri());
  395. return $aprofile;
  396. } catch (Exception $e) {
  397. common_log(LOG_WARNING, "Failed creating profile from profile URL '$profileUrl': " . $e->getMessage());
  398. // keep looking
  399. //
  400. // @todo FIXME: This means an error discovering from profile page
  401. // may give us a corrupt entry using the webfinger URI, which
  402. // will obscure the correct page-keyed profile later on.
  403. }
  404. }
  405. // XXX: try hcard
  406. // XXX: try FOAF
  407. // TRANS: Exception. %s is a WebFinger address.
  408. throw new Exception(sprintf(_m('Could not find a valid profile for "%s".'), $addr));
  409. }
  410. /**
  411. * Update local profile with info from some AP profile
  412. *
  413. * @param Profile $profile
  414. * @param Activitypub_profile $aprofile
  415. * @return void
  416. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  417. * @author Diogo Cordeiro <diogo@fc.up.pt>
  418. */
  419. public static function update_local_profile(Profile $profile, Activitypub_profile $aprofile): void
  420. {
  421. $fields = [
  422. 'profileurl' => 'profileurl',
  423. 'nickname' => 'nickname',
  424. 'fullname' => 'fullname',
  425. 'bio' => 'bio'
  426. ];
  427. $orig = clone($profile);
  428. foreach ($fields as $af => $pf) {
  429. $profile->$pf = $aprofile->$af;
  430. }
  431. if ($profile->id) {
  432. common_debug('Updating local Profile:' . $profile->id . ' from remote ActivityPub profile');
  433. $profile->modified = common_sql_now();
  434. $profile->update($orig);
  435. }
  436. }
  437. /**
  438. * Update remote user profile in local instance
  439. *
  440. * @param Activitypub_profile $aprofile
  441. * @param array|false $res remote response, if array it updates, if false it deletes
  442. * @return Profile remote Profile object
  443. * @throws NoProfileException
  444. * @author Diogo Cordeiro <diogo@fc.up.pt>
  445. */
  446. public static function update_profile(Activitypub_profile $aprofile, $res): Profile
  447. {
  448. if ($res === false) {
  449. $profile = $aprofile->local_profile();
  450. $id = $profile->getID();
  451. $profile->delete();
  452. throw new NoProfileException($id, "410 Gone");
  453. }
  454. if (!is_array($res)) {
  455. throw new InvalidArgumentException('TypeError: Argument 2 passed to Activitypub_profile::update_profile() must be of the type array or bool(false).');
  456. }
  457. // ActivityPub Profile
  458. $aprofile->uri = $res['id'];
  459. $aprofile->nickname = $res['preferredUsername'];
  460. $aprofile->fullname = $res['name'] ?? null;
  461. $aprofile->bio = isset($res['summary']) ? substr(strip_tags($res['summary']), 0, 1000) : null;
  462. $aprofile->inboxuri = $res['inbox'];
  463. $aprofile->sharedInboxuri = $res['endpoints']['sharedInbox'] ?? $res['inbox'];
  464. $aprofile->profileurl = $res['url'] ?? $aprofile->uri;
  465. $aprofile->modified = common_sql_now();
  466. $profile = $aprofile->local_profile();
  467. // Profile
  468. self::update_local_profile($profile, $aprofile);
  469. $aprofile->update();
  470. // Public Key
  471. Activitypub_rsa::update_public_key($profile, $res['publicKey']['publicKeyPem']);
  472. // Avatar
  473. if (isset($res['icon']['url'])) {
  474. try {
  475. Activitypub_explorer::update_avatar($profile, $res['icon']['url']);
  476. } catch (Exception $e) {
  477. // Let the exception go, it isn't a serious issue
  478. common_debug('An error ocurred while grabbing remote avatar' . $e->getMessage());
  479. }
  480. }
  481. return $profile;
  482. }
  483. /**
  484. * Update remote user profile URI in local instance
  485. *
  486. * @param string $uri
  487. * @return void
  488. * @throws Exception (if the update fails)
  489. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  490. */
  491. public function updateUri(string $uri): void
  492. {
  493. $orig = clone($this);
  494. $this->uri = $uri;
  495. $this->updateWithKeys($orig);
  496. }
  497. /**
  498. * Getter for the number of subscribers of a
  499. * given local profile
  500. *
  501. * @param Profile $profile profile object
  502. * @return int number of subscribers
  503. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  504. */
  505. public static function subscriberCount(Profile $profile): int
  506. {
  507. $cnt = self::cacheGet(sprintf('activitypub_profile:subscriberCount:%d', $profile->id));
  508. if ($cnt !== false && is_int($cnt)) {
  509. return $cnt;
  510. }
  511. $user_table = common_database_tablename('user');
  512. $sub = new Subscription();
  513. $sub->subscribed = $profile->id;
  514. $sub->_join .= "\n" . <<<END
  515. INNER JOIN (
  516. SELECT id AS subscriber FROM {$user_table}
  517. UNION ALL
  518. SELECT profile_id FROM activitypub_profile
  519. ) AS t1 USING (subscriber)
  520. END;
  521. $sub->whereAdd('subscriber <> subscribed');
  522. $cnt = $sub->count('DISTINCT subscriber');
  523. self::cacheSet(sprintf('activitypub_profile:subscriberCount:%d', $profile->id), $cnt);
  524. return $cnt;
  525. }
  526. /**
  527. * Getter for the number of subscriptions of a
  528. * given local profile
  529. *
  530. * @param Profile $profile profile object
  531. * @return int number of subscriptions
  532. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  533. */
  534. public static function subscriptionCount(Profile $profile): int
  535. {
  536. $cnt = self::cacheGet(sprintf('activitypub_profile:subscriptionCount:%d', $profile->id));
  537. if ($cnt !== false && is_int($cnt)) {
  538. return $cnt;
  539. }
  540. $user_table = common_database_tablename('user');
  541. $sub = new Subscription();
  542. $sub->subscriber = $profile->id;
  543. $sub->_join .= "\n" . <<<END
  544. INNER JOIN (
  545. SELECT id AS subscribed FROM {$user_table}
  546. UNION ALL
  547. SELECT profile_id FROM activitypub_profile
  548. ) AS t1 USING (subscribed)
  549. END;
  550. $sub->whereAdd('subscriber <> subscribed');
  551. $cnt = $sub->count('DISTINCT subscribed');
  552. self::cacheSet(sprintf('activitypub_profile:subscriptionCount:%d', $profile->id), $cnt);
  553. return $cnt;
  554. }
  555. /**
  556. * Increment or decrement subscriber count
  557. *
  558. * @param Profile $profile
  559. * @param $adder
  560. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  561. */
  562. public static function updateSubscriberCount(Profile $profile, $adder): void
  563. {
  564. $cnt = self::cacheGet(sprintf('activitypub_profile:subscriberCount:%d', $profile->id));
  565. if ($cnt !== false && is_int($cnt)) {
  566. self::cacheSet(sprintf('activitypub_profile:subscriberCount:%d', $profile->id), $cnt + $adder);
  567. }
  568. }
  569. /**
  570. * Increment or decrement subscription count
  571. *
  572. * @param Profile $profile
  573. * @param $adder
  574. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  575. */
  576. public static function updateSubscriptionCount(Profile $profile, $adder): void
  577. {
  578. $cnt = self::cacheGet(sprintf('activitypub_profile:subscriptionCount:%d', $profile->id));
  579. if ($cnt !== false && is_int($cnt)) {
  580. self::cacheSet(sprintf('activitypub_profile:subscriptionCount:%d', $profile->id), $cnt + $adder);
  581. }
  582. }
  583. /**
  584. * Getter for the subscriber profiles of a
  585. * given local profile
  586. *
  587. * @param Profile $profile profile object
  588. * @param int $offset [optional] index of the starting row to fetch from
  589. * @param int|null $limit [optional] maximum number of rows allowed for fetching. If it is omitted,
  590. * then the sequence will have everything
  591. * from offset up until the end.
  592. * @return array subscriber profile objects
  593. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  594. */
  595. public static function getSubscribers(Profile $profile, int $offset = 0, ?int $limit = null): array
  596. {
  597. $cache = false;
  598. if ($offset + $limit <= Subscription::CACHE_WINDOW) {
  599. $subs = self::cacheGet(sprintf('activitypub_profile:subscriberCollection:%d', $profile->id));
  600. if ($subs !== false && is_array($subs)) {
  601. return array_slice($subs, $offset, $limit);
  602. }
  603. $cache = true;
  604. }
  605. $subs = Subscription::getSubscriberIDs($profile->id, $offset, $limit);
  606. $profiles = [];
  607. $users = User::multiGet('id', $subs);
  608. foreach ($users->fetchAll() as $user) {
  609. $profiles[$user->id] = $user->getProfile();
  610. }
  611. $ap_profiles = Activitypub_profile::multiGet('profile_id', $subs);
  612. foreach ($ap_profiles->fetchAll() as $ap) {
  613. $profiles[$ap->getID()] = $ap->local_profile();
  614. }
  615. if ($cache) {
  616. self::cacheSet(sprintf('activitypub_profile:subscriberCollection:%d', $profile->id), $profiles);
  617. }
  618. return $profiles;
  619. }
  620. /**
  621. * Getter for the subscribed profiles of a
  622. * given local profile
  623. *
  624. * @param Profile $profile profile object
  625. * @param int $offset index of the starting row to fetch from
  626. * @param int|null $limit maximum number of rows allowed for fetching
  627. * @return array subscribed profile objects
  628. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  629. */
  630. public static function getSubscribed(Profile $profile, int $offset = 0, ?int $limit = null): array
  631. {
  632. $cache = false;
  633. if ($offset + $limit <= Subscription::CACHE_WINDOW) {
  634. $subs = self::cacheGet(sprintf('activitypub_profile:subscribedCollection:%d', $profile->id));
  635. if (is_array($subs)) {
  636. return array_slice($subs, $offset, $limit);
  637. }
  638. $cache = true;
  639. }
  640. $subs = Subscription::getSubscribedIDs($profile->id, $offset, $limit);
  641. $profiles = [];
  642. $users = User::multiGet('id', $subs);
  643. foreach ($users->fetchAll() as $user) {
  644. $profiles[$user->id] = $user->getProfile();
  645. }
  646. $ap_profiles = Activitypub_profile::multiGet('profile_id', $subs);
  647. foreach ($ap_profiles->fetchAll() as $ap) {
  648. $profiles[$ap->getID()] = $ap->local_profile();
  649. }
  650. if ($cache) {
  651. self::cacheSet(sprintf('activitypub_profile:subscribedCollection:%d', $profile->id), $profiles);
  652. }
  653. return $profiles;
  654. }
  655. /**
  656. * Update cached values that are relevant to
  657. * the users involved in a subscription
  658. *
  659. * @param Profile $actor subscriber profile object
  660. * @param Profile $other subscribed profile object
  661. * @return void
  662. * @throws Exception
  663. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  664. */
  665. public static function subscribeCacheUpdate(Profile $actor, Profile $other): void
  666. {
  667. self::blow('activitypub_profile:subscribedCollection:%d', $actor->getID());
  668. self::blow('activitypub_profile:subscriberCollection:%d', $other->id);
  669. self::updateSubscriptionCount($actor, +1);
  670. self::updateSubscriberCount($other, +1);
  671. }
  672. /**
  673. * Update cached values that are relevant to
  674. * the users involved in an unsubscription
  675. *
  676. * @param Profile $actor subscriber profile object
  677. * @param Profile $other subscribed profile object
  678. * @return void
  679. * @throws Exception
  680. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  681. */
  682. public static function unsubscribeCacheUpdate(Profile $actor, Profile $other): void
  683. {
  684. self::blow('activitypub_profile:subscribedCollection:%d', $actor->getID());
  685. self::blow('activitypub_profile:subscriberCollection:%d', $other->id);
  686. self::updateSubscriptionCount($actor, -1);
  687. self::updateSubscriberCount($other, -1);
  688. }
  689. }