Foreign_link.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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; // blob
  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' => 'blob', '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_service_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_link_foreign_id_service_idx' => array('foreign_id', 'service'),
  62. 'foreign_link_service_idx' => array('service'),
  63. ),
  64. );
  65. }
  66. public static function getByUserID($user_id, $service)
  67. {
  68. if (empty($user_id) || empty($service)) {
  69. throw new ServerException('Empty user_id or service for Foreign_link::getByUserID');
  70. }
  71. $flink = new Foreign_link();
  72. $flink->service = $service;
  73. $flink->user_id = $user_id;
  74. $flink->limit(1);
  75. if (!$flink->find(true)) {
  76. throw new NoResultException($flink);
  77. }
  78. return $flink;
  79. }
  80. public static function getByForeignID($foreign_id, $service)
  81. {
  82. if (empty($foreign_id) || empty($service)) {
  83. throw new ServerException('Empty foreign_id or service for Foreign_link::getByForeignID');
  84. }
  85. $flink = new Foreign_link();
  86. $flink->service = $service;
  87. $flink->foreign_id = $foreign_id;
  88. $flink->limit(1);
  89. if (!$flink->find(true)) {
  90. throw new NoResultException($flink);
  91. }
  92. return $flink;
  93. }
  94. public function set_flags($noticesend, $noticerecv, $replysync, $repeatsync, $friendsync)
  95. {
  96. if ($noticesend) {
  97. $this->noticesync |= FOREIGN_NOTICE_SEND;
  98. } else {
  99. $this->noticesync &= ~FOREIGN_NOTICE_SEND;
  100. }
  101. if ($noticerecv) {
  102. $this->noticesync |= FOREIGN_NOTICE_RECV;
  103. } else {
  104. $this->noticesync &= ~FOREIGN_NOTICE_RECV;
  105. }
  106. if ($replysync) {
  107. $this->noticesync |= FOREIGN_NOTICE_SEND_REPLY;
  108. } else {
  109. $this->noticesync &= ~FOREIGN_NOTICE_SEND_REPLY;
  110. }
  111. if ($repeatsync) {
  112. $this->noticesync |= FOREIGN_NOTICE_SEND_REPEAT;
  113. } else {
  114. $this->noticesync &= ~FOREIGN_NOTICE_SEND_REPEAT;
  115. }
  116. if ($friendsync) {
  117. $this->friendsync |= FOREIGN_FRIEND_RECV;
  118. } else {
  119. $this->friendsync &= ~FOREIGN_FRIEND_RECV;
  120. }
  121. $this->profilesync = 0;
  122. }
  123. // Convenience methods
  124. public function getForeignUser()
  125. {
  126. $fuser = new Foreign_user();
  127. $fuser->service = $this->service;
  128. $fuser->id = $this->foreign_id;
  129. $fuser->limit(1);
  130. if (!$fuser->find(true)) {
  131. throw new NoResultException($fuser);
  132. }
  133. return $fuser;
  134. }
  135. public function getUser()
  136. {
  137. return Profile::getByID($this->user_id)->getUser();
  138. }
  139. public function getProfile()
  140. {
  141. return Profile::getByID($this->user_id);
  142. }
  143. // Make sure we only ever delete one record at a time
  144. public function safeDelete()
  145. {
  146. if (!empty($this->user_id)
  147. && !empty($this->foreign_id)
  148. && !empty($this->service)) {
  149. return $this->delete();
  150. } else {
  151. common_debug(
  152. LOG_WARNING,
  153. 'Foreign_link::safeDelete() tried to delete a '
  154. . 'Foreign_link without a fully specified compound key: '
  155. . var_export($this, true)
  156. );
  157. return false;
  158. }
  159. }
  160. }