]> git.mxchange.org Git - friendica.git/blob - src/Model/GContact.php
Merge remote-tracking branch 'upstream/develop' into contact-tabs
[friendica.git] / src / Model / GContact.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Model;
23
24 use Exception;
25 use Friendica\Database\DBA;
26 use Friendica\DI;
27 use Friendica\Util\DateTimeFormat;
28
29 /**
30  * This class handles GlobalContact related functions
31  */
32 class GContact
33 {
34         /**
35          * @param integer $uid id
36          * @param integer $cid id
37          * @return integer
38          * @throws Exception
39          */
40         public static function countCommonFriends($uid, $cid)
41         {
42                 $sourceId = Contact::getPublicIdByUserId($uid);
43
44                 $targetIds = Contact::getPublicAndUserContacID($cid, $uid);
45
46                 $condition = [
47                         'NOT `self` AND NOT `blocked` AND NOT `hidden` AND `id` != ?',
48                         $sourceId,
49                 ];
50
51                 return Contact\Relation::countCommonFollows($sourceId, $targetIds['public'] ?? 0, $condition);
52         }
53
54         /**
55          * @param integer $uid  id
56          * @param integer $zcid zcid
57          * @return integer
58          * @throws Exception
59          */
60         public static function countCommonFriendsZcid($uid, $zcid)
61         {
62                 $sourceId = Contact::getPublicIdByUserId($uid);
63
64                 $targetPublicContact = DI::dba()->fetchFirst("
65 SELECT `id`
66 FROM `contact` c 
67 JOIN `gcontact` z ON z.`nurl` = c.`nurl`
68 AND z.`id` = ?
69 AND c.`uid` = 0
70 LIMIT 1",
71                         $zcid
72                 );
73
74                 $condition = [
75                         'NOT `self` AND NOT `blocked` AND NOT `hidden` AND `id` != ?',
76                         $sourceId,
77                 ];
78
79                 return Contact\Relation::countCommonFollowers($sourceId, $targetPublicContact['id'] ?? 0, $condition);
80         }
81
82         /**
83          * Returns the cross-section between the local user contacts and one of their contact's own relationships
84          * as known by the local node.
85          *
86          * @param integer $uid     local user id
87          * @param integer $cid     user contact id to compare friends with
88          * @param integer $start   optional, default 0
89          * @param integer $limit   optional, default 9999
90          * @param boolean $shuffle optional, default false
91          * @return array
92          * @throws Exception
93          */
94         public static function commonFriends($uid, $cid, $start = 0, $limit = 9999, $shuffle = false)
95         {
96                 $sourceId = Contact::getPublicIdByUserId($uid);
97
98                 $targetIds = Contact::getPublicAndUserContacID($cid, $uid);
99
100                 $condition = [
101                         'NOT `self` AND NOT `blocked` AND NOT `hidden` AND `id` != ?',
102                         $sourceId,
103                 ];
104
105                 return Contact\Relation::listCommonFollows($sourceId, $targetIds['public'] ?? 0, $condition, $limit, $start, $shuffle);
106         }
107
108         /**
109          * Returns the cross-section between a local user and a remote visitor contact's own relationships
110          * as known by the local node.
111          *
112          * @param integer $uid     local user id
113          * @param integer $zcid    remote visitor contact zcid
114          * @param integer $start   optional, default 0
115          * @param integer $limit   optional, default 9999
116          * @param boolean $shuffle optional, default false
117          * @return array
118          * @throws Exception
119          */
120         public static function commonFriendsZcid($uid, $zcid, $start = 0, $limit = 9999, $shuffle = false)
121         {
122                 $sourceId = Contact::getPublicIdByUserId($uid);
123
124                 $targetPublicContact = DI::dba()->fetchFirst("
125 SELECT c.`id`
126 FROM `contact` c 
127 JOIN `gcontact` z ON z.`nurl` = c.`nurl`
128 AND z.`id` = ?
129 AND c.`uid` = 0
130 LIMIT 1",
131                         $zcid
132                 );
133
134                 $condition = [
135                         'NOT `self` AND NOT `blocked` AND NOT `hidden` AND `id` != ?',
136                         $sourceId,
137                 ];
138
139                 return Contact\Relation::listCommonFollows($sourceId, $targetPublicContact['id'] ?? 0, $condition, $limit, $start, $shuffle);
140         }
141
142         /**
143          * @param integer $uid user
144          * @param integer $cid cid
145          * @return integer
146          * @throws Exception
147          */
148         public static function countAllFriends($uid, $cid)
149         {
150                 $cids = Contact::getPublicAndUserContacID($cid, $uid);
151
152                 return Contact\Relation::countFollows($cids['public'] ?? 0);
153         }
154
155         /**
156          * @param integer $uid   user
157          * @param integer $cid   cid
158          * @param integer $start optional, default 0
159          * @param integer $limit optional, default 80
160          * @return array
161          * @throws Exception
162          */
163         public static function allFriends($uid, $cid, $start = 0, $limit = 80)
164         {
165                 $cids = Contact::getPublicAndUserContacID($cid, $uid);
166
167                 return Contact\Relation::listFollows($cids['public'] ?? 0, [], $limit, $start);
168         }
169 }