Foreign_link.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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(255)
  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' => 255, '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. return null;
  55. }
  56. $flink = new Foreign_link();
  57. $flink->service = $service;
  58. $flink->user_id = $user_id;
  59. $flink->limit(1);
  60. $result = $flink->find(true);
  61. return empty($result) ? null : $flink;
  62. }
  63. static function getByForeignID($foreign_id, $service)
  64. {
  65. if (empty($foreign_id) || empty($service)) {
  66. return null;
  67. } else {
  68. $flink = new Foreign_link();
  69. $flink->service = $service;
  70. $flink->foreign_id = $foreign_id;
  71. $flink->limit(1);
  72. $result = $flink->find(true);
  73. return empty($result) ? null : $flink;
  74. }
  75. }
  76. function set_flags($noticesend, $noticerecv, $replysync, $friendsync)
  77. {
  78. if ($noticesend) {
  79. $this->noticesync |= FOREIGN_NOTICE_SEND;
  80. } else {
  81. $this->noticesync &= ~FOREIGN_NOTICE_SEND;
  82. }
  83. if ($noticerecv) {
  84. $this->noticesync |= FOREIGN_NOTICE_RECV;
  85. } else {
  86. $this->noticesync &= ~FOREIGN_NOTICE_RECV;
  87. }
  88. if ($replysync) {
  89. $this->noticesync |= FOREIGN_NOTICE_SEND_REPLY;
  90. } else {
  91. $this->noticesync &= ~FOREIGN_NOTICE_SEND_REPLY;
  92. }
  93. if ($friendsync) {
  94. $this->friendsync |= FOREIGN_FRIEND_RECV;
  95. } else {
  96. $this->friendsync &= ~FOREIGN_FRIEND_RECV;
  97. }
  98. $this->profilesync = 0;
  99. }
  100. // Convenience methods
  101. function getForeignUser()
  102. {
  103. $fuser = new Foreign_user();
  104. $fuser->service = $this->service;
  105. $fuser->id = $this->foreign_id;
  106. $fuser->limit(1);
  107. if ($fuser->find(true)) {
  108. return $fuser;
  109. }
  110. return null;
  111. }
  112. function getUser()
  113. {
  114. return User::getKV($this->user_id);
  115. }
  116. function getProfile()
  117. {
  118. return Profile::getKV('id', $this->user_id);
  119. }
  120. // Make sure we only ever delete one record at a time
  121. function safeDelete()
  122. {
  123. if (!empty($this->user_id)
  124. && !empty($this->foreign_id)
  125. && !empty($this->service))
  126. {
  127. return $this->delete();
  128. } else {
  129. common_debug(LOG_WARNING,
  130. 'Foreign_link::safeDelete() tried to delete a '
  131. . 'Foreign_link without a fully specified compound key: '
  132. . var_export($this, true));
  133. return false;
  134. }
  135. }
  136. }