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