Foreign_link.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. * Table Definition for foreign_link
  18. */
  19. defined('GNUSOCIAL') || die();
  20. class Foreign_link extends Managed_DataObject
  21. {
  22. ###START_AUTOCODE
  23. /* the code below is auto generated do not remove the above tag */
  24. public $__table = 'foreign_link'; // table name
  25. public $user_id; // int(4) primary_key not_null
  26. public $foreign_id; // bigint(8) primary_key not_null unsigned
  27. public $service; // int(4) primary_key not_null
  28. public $credentials; // varchar(191) not 255 because utf8mb4 takes more space
  29. public $noticesync; // tinyint(1) not_null default_1
  30. public $friendsync; // tinyint(1) not_null default_2
  31. public $profilesync; // tinyint(1) not_null default_1
  32. public $last_noticesync; // datetime()
  33. public $last_friendsync; // datetime()
  34. public $created; // datetime()
  35. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  36. /* the code above is auto generated do not remove the tag below */
  37. ###END_AUTOCODE
  38. public static function schemaDef()
  39. {
  40. return array(
  41. 'fields' => array(
  42. 'user_id' => array('type' => 'int', 'not null' => true, 'description' => 'link to user on this system, if exists'),
  43. 'foreign_id' => array('type' => 'int', 'size' => 'big', 'not null' => true, 'description' => 'link to user on foreign service, if exists'),
  44. 'service' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to service'),
  45. 'credentials' => array('type' => 'varchar', 'length' => 191, 'description' => 'authc credentials, typically a password'),
  46. 'noticesync' => array('type' => 'int', 'size' => 'tiny', 'not null' => true, 'default' => 1, 'description' => 'notice synchronization, bit 1 = sync outgoing, bit 2 = sync incoming, bit 3 = filter local replies'),
  47. 'friendsync' => array('type' => 'int', 'size' => 'tiny', 'not null' => true, 'default' => 2, 'description' => 'friend synchronization, bit 1 = sync outgoing, bit 2 = sync incoming'),
  48. 'profilesync' => array('type' => 'int', 'size' => 'tiny', 'not null' => true, 'default' => 1, 'description' => 'profile synchronization, bit 1 = sync outgoing, bit 2 = sync incoming'),
  49. 'last_noticesync' => array('type' => 'datetime', 'description' => 'last time notices were imported'),
  50. 'last_friendsync' => array('type' => 'datetime', 'description' => 'last time friends were imported'),
  51. 'created' => array('type' => 'datetime', 'description' => 'date this record was created'),
  52. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  53. ),
  54. 'primary key' => array('user_id', 'foreign_id', 'service'),
  55. 'foreign keys' => array(
  56. 'foreign_link_user_id_fkey' => array('user', array('user_id' => 'id')),
  57. 'foreign_link_foreign_id_fkey' => array('foreign_user', array('foreign_id' => 'id', 'service' => 'service')),
  58. 'foreign_link_service_fkey' => array('foreign_service', array('service' => 'id')),
  59. ),
  60. 'indexes' => array(
  61. 'foreign_user_user_id_idx' => array('user_id'),
  62. ),
  63. );
  64. }
  65. public static function getByUserID($user_id, $service)
  66. {
  67. if (empty($user_id) || empty($service)) {
  68. throw new ServerException('Empty user_id or service for Foreign_link::getByUserID');
  69. }
  70. $flink = new Foreign_link();
  71. $flink->service = $service;
  72. $flink->user_id = $user_id;
  73. $flink->limit(1);
  74. if (!$flink->find(true)) {
  75. throw new NoResultException($flink);
  76. }
  77. return $flink;
  78. }
  79. public static function getByForeignID($foreign_id, $service)
  80. {
  81. if (empty($foreign_id) || empty($service)) {
  82. throw new ServerException('Empty foreign_id or service for Foreign_link::getByForeignID');
  83. }
  84. $flink = new Foreign_link();
  85. $flink->service = $service;
  86. $flink->foreign_id = $foreign_id;
  87. $flink->limit(1);
  88. if (!$flink->find(true)) {
  89. throw new NoResultException($flink);
  90. }
  91. return $flink;
  92. }
  93. public function set_flags($noticesend, $noticerecv, $replysync, $repeatsync, $friendsync)
  94. {
  95. if ($noticesend) {
  96. $this->noticesync |= FOREIGN_NOTICE_SEND;
  97. } else {
  98. $this->noticesync &= ~FOREIGN_NOTICE_SEND;
  99. }
  100. if ($noticerecv) {
  101. $this->noticesync |= FOREIGN_NOTICE_RECV;
  102. } else {
  103. $this->noticesync &= ~FOREIGN_NOTICE_RECV;
  104. }
  105. if ($replysync) {
  106. $this->noticesync |= FOREIGN_NOTICE_SEND_REPLY;
  107. } else {
  108. $this->noticesync &= ~FOREIGN_NOTICE_SEND_REPLY;
  109. }
  110. if ($repeatsync) {
  111. $this->noticesync |= FOREIGN_NOTICE_SEND_REPEAT;
  112. } else {
  113. $this->noticesync &= ~FOREIGN_NOTICE_SEND_REPEAT;
  114. }
  115. if ($friendsync) {
  116. $this->friendsync |= FOREIGN_FRIEND_RECV;
  117. } else {
  118. $this->friendsync &= ~FOREIGN_FRIEND_RECV;
  119. }
  120. $this->profilesync = 0;
  121. }
  122. // Convenience methods
  123. public function getForeignUser()
  124. {
  125. $fuser = new Foreign_user();
  126. $fuser->service = $this->service;
  127. $fuser->id = $this->foreign_id;
  128. $fuser->limit(1);
  129. if (!$fuser->find(true)) {
  130. throw new NoResultException($fuser);
  131. }
  132. return $fuser;
  133. }
  134. public function getUser()
  135. {
  136. return Profile::getByID($this->user_id)->getUser();
  137. }
  138. public function getProfile()
  139. {
  140. return Profile::getByID($this->user_id);
  141. }
  142. // Make sure we only ever delete one record at a time
  143. public function safeDelete()
  144. {
  145. if (!empty($this->user_id)
  146. && !empty($this->foreign_id)
  147. && !empty($this->service)) {
  148. return $this->delete();
  149. } else {
  150. common_debug(
  151. LOG_WARNING,
  152. 'Foreign_link::safeDelete() tried to delete a '
  153. . 'Foreign_link without a fully specified compound key: '
  154. . var_export($this, true)
  155. );
  156. return false;
  157. }
  158. }
  159. }