ActivityPubActor.php 28 KB

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