Activitypub_profile.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. * GNU social - a federating social network
  4. *
  5. * ActivityPubPlugin implementation for GNU Social
  6. *
  7. * LICENCE: This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @category Plugin
  21. * @package GNUsocial
  22. * @author Diogo Cordeiro <diogo@fc.up.pt>
  23. * @author Daniel Supernault <danielsupernault@gmail.com>
  24. * @copyright 2018 Free Software Foundation http://fsf.org
  25. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  26. * @link https://www.gnu.org/software/social/
  27. */
  28. if (!defined ('GNUSOCIAL')) {
  29. exit (1);
  30. }
  31. /**
  32. * ActivityPub Profile
  33. *
  34. * @category Plugin
  35. * @package GNUsocial
  36. * @author Diogo Cordeiro <diogo@fc.up.pt>
  37. * @author Daniel Supernault <danielsupernault@gmail.com>
  38. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  39. * @link http://www.gnu.org/software/social/
  40. */
  41. class Activitypub_profile extends Profile
  42. {
  43. public $__table = 'Activitypub_profile';
  44. protected $_profile = null;
  45. /**
  46. * Return table definition for Schema setup and DB_DataObject usage.
  47. *
  48. * @return array array of column definitions
  49. */
  50. static function schemaDef ()
  51. {
  52. return array (
  53. 'fields' => array (
  54. 'uri' => array ('type' => 'varchar', 'length' => 191, 'not null' => true),
  55. 'profile_id' => array ('type' => 'integer'),
  56. 'inboxuri' => array ('type' => 'varchar', 'length' => 191),
  57. 'sharedInboxuri' => array ('type' => 'varchar', 'length' => 191),
  58. 'created' => array ('type' => 'datetime', 'not null' => true),
  59. 'modified' => array ('type' => 'datetime', 'not null' => true),
  60. ),
  61. 'primary key' => array ('uri'),
  62. 'unique keys' => array (
  63. 'Activitypub_profile_profile_id_key' => array ('profile_id'),
  64. 'Activitypub_profile_inboxuri_key' => array ('inboxuri'),
  65. ),
  66. 'foreign keys' => array (
  67. 'Activitypub_profile_profile_id_fkey' => array ('profile', array ('profile_id' => 'id')),
  68. ),
  69. );
  70. }
  71. /**
  72. * Generates a pretty profile from a Profile object
  73. *
  74. * @param Profile $profile
  75. * @return pretty array to be used in a response
  76. */
  77. public static function profile_to_array ($profile)
  78. {
  79. $url = $profile->getURL ();
  80. $res = [
  81. '@context' => [
  82. "https://www.w3.org/ns/activitystreams",
  83. [
  84. "@language" => "en"
  85. ]
  86. ],
  87. 'id' => $profile->getID (),
  88. 'type' => 'Person',
  89. 'nickname' => $profile->getNickname (),
  90. 'is_local' => $profile->isLocal (),
  91. 'inbox' => "{$url}/inbox.json",
  92. 'sharedInbox' => common_root_url ()."inbox.json",
  93. 'outbox' => "{$url}/outbox.json",
  94. 'display_name' => $profile->getFullname (),
  95. 'followers' => "{$url}/followers.json",
  96. 'followers_count' => $profile->subscriberCount (),
  97. 'following' => "{$url}/following.json",
  98. 'following_count' => $profile->subscriptionCount (),
  99. 'liked' => "{$url}/liked.json",
  100. 'liked_count' => Fave::countByProfile ($profile),
  101. 'summary' => ($desc = $profile->getDescription ()) == null ? "" : $desc,
  102. 'url' => $profile->getURL (),
  103. 'avatar' => [
  104. 'type' => 'Image',
  105. 'width' => 96,
  106. 'height' => 96,
  107. 'url' => $profile->avatarUrl (AVATAR_PROFILE_SIZE)
  108. ]
  109. ];
  110. return $res;
  111. }
  112. /**
  113. * Insert the current objects variables into the database
  114. *
  115. * @access public
  116. * @throws ServerException
  117. */
  118. public function doInsert ()
  119. {
  120. $profile = new Profile ();
  121. $profile->created = $this->created = $this->modified = common_sql_now ();
  122. $fields = array (
  123. 'uri' => 'profileurl',
  124. 'nickname' => 'nickname',
  125. 'fullname' => 'fullname',
  126. 'bio' => 'bio'
  127. );
  128. foreach ($fields as $af => $pf) {
  129. $profile->$pf = $this->$af;
  130. }
  131. $this->profile_id = $profile->insert ();
  132. if ($this->profile_id === false) {
  133. $profile->query ('ROLLBACK');
  134. throw new ServerException ('Profile insertion failed.');
  135. }
  136. $ok = $this->insert ();
  137. if ($ok === false) {
  138. $profile->query ('ROLLBACK');
  139. throw new ServerException ('Cannot save ActivityPub profile.');
  140. }
  141. }
  142. /**
  143. * Fetch the locally stored profile for this Activitypub_profile
  144. * @return Profile
  145. * @throws NoProfileException if it was not found
  146. */
  147. public function localProfile ()
  148. {
  149. $profile = Profile::getKV ('id', $this->profile_id);
  150. if (!$profile instanceof Profile) {
  151. throw new NoProfileException ($this->profile_id);
  152. }
  153. return $profile;
  154. }
  155. /**
  156. * Generates an Activitypub_profile from a Profile
  157. *
  158. * @param Profile $profile
  159. * @return Activitypub_profile
  160. * @throws Exception if no Activitypub_profile exists for given Profile
  161. */
  162. static function fromProfile (Profile $profile)
  163. {
  164. $profile_id = $profile->getID ();
  165. $aprofile = Activitypub_profile::getKV ('profile_id', $profile_id);
  166. if (!$aprofile instanceof Activitypub_profile) {
  167. throw new Exception('No Activitypub_profile for Profile ID: '.$profile_id);
  168. }
  169. foreach ($profile as $key => $value) {
  170. $aprofile->$key = $value;
  171. }
  172. return $aprofile;
  173. }
  174. /**
  175. * Returns sharedInbox if possible, inbox otherwise
  176. *
  177. * @return string Inbox URL
  178. */
  179. public function getInbox ()
  180. {
  181. if (is_null ($this->sharedInboxuri)) {
  182. return $this->inboxuri;
  183. }
  184. return $this->sharedInboxuri;
  185. }
  186. }