Foreign_link.php 6.5 KB

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