]> git.mxchange.org Git - friendica.git/blob - src/Model/GContact.php
Fill "last-item" with an empty date when bo date had been provided
[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\DI;
26
27 /**
28  * This class handles GlobalContact related functions
29  */
30 class GContact
31 {
32         /**
33          * @param integer $uid id
34          * @param integer $cid id
35          * @return integer
36          * @throws Exception
37          */
38         public static function countCommonFriends($uid, $cid)
39         {
40                 $sourceId = Contact::getPublicIdByUserId($uid);
41
42                 $targetIds = Contact::getPublicAndUserContacID($cid, $uid);
43
44                 $condition = [
45                         'NOT `self` AND NOT `blocked` AND NOT `hidden` AND `id` != ?',
46                         $sourceId,
47                 ];
48
49                 return Contact\Relation::countCommonFollows($sourceId, $targetIds['public'] ?? 0, [], $condition);
50         }
51
52         /**
53          * @param integer $uid  id
54          * @param integer $zcid zcid
55          * @return integer
56          * @throws Exception
57          */
58         public static function countCommonFriendsZcid($uid, $zcid)
59         {
60                 $sourceId = Contact::getPublicIdByUserId($uid);
61
62                 $targetPublicContact = DI::dba()->fetchFirst("
63 SELECT `id`
64 FROM `contact` c 
65 JOIN `gcontact` z ON z.`nurl` = c.`nurl`
66 AND z.`id` = ?
67 AND c.`uid` = 0
68 LIMIT 1",
69                         $zcid
70                 );
71
72                 $condition = [
73                         'NOT `self` AND NOT `blocked` AND NOT `hidden` AND `id` != ?',
74                         $sourceId,
75                 ];
76
77                 return Contact\Relation::countCommonFollowers($sourceId, $targetPublicContact['id'] ?? 0, [], $condition);
78         }
79
80         /**
81          * Returns the cross-section between the local user contacts and one of their contact's own relationships
82          * as known by the local node.
83          *
84          * @param integer $uid     local user id
85          * @param integer $cid     user contact id to compare friends with
86          * @param integer $start   optional, default 0
87          * @param integer $limit   optional, default 9999
88          * @param boolean $shuffle optional, default false
89          * @return array
90          * @throws Exception
91          */
92         public static function commonFriends($uid, $cid, $start = 0, $limit = 9999, $shuffle = false)
93         {
94                 $sourceId = Contact::getPublicIdByUserId($uid);
95
96                 $targetIds = Contact::getPublicAndUserContacID($cid, $uid);
97
98                 $condition = [
99                         'NOT `self` AND NOT `blocked` AND NOT `hidden` AND `id` != ?',
100                         $sourceId,
101                 ];
102
103                 return Contact\Relation::listCommonFollows($sourceId, $targetIds['public'] ?? 0, [], $condition, $limit, $start, $shuffle);
104         }
105
106         /**
107          * Returns the cross-section between a local user and a remote visitor contact's own relationships
108          * as known by the local node.
109          *
110          * @param integer $uid     local user id
111          * @param integer $zcid    remote visitor contact zcid
112          * @param integer $start   optional, default 0
113          * @param integer $limit   optional, default 9999
114          * @param boolean $shuffle optional, default false
115          * @return array
116          * @throws Exception
117          */
118         public static function commonFriendsZcid($uid, $zcid, $start = 0, $limit = 9999, $shuffle = false)
119         {
120                 $sourceId = Contact::getPublicIdByUserId($uid);
121
122                 $targetPublicContact = DI::dba()->fetchFirst("
123 SELECT c.`id`
124 FROM `contact` c 
125 JOIN `gcontact` z ON z.`nurl` = c.`nurl`
126 AND z.`id` = ?
127 AND c.`uid` = 0
128 LIMIT 1",
129                         $zcid
130                 );
131
132                 $condition = [
133                         'NOT `self` AND NOT `blocked` AND NOT `hidden` AND `id` != ?',
134                         $sourceId,
135                 ];
136
137                 return Contact\Relation::listCommonFollows($sourceId, $targetPublicContact['id'] ?? 0, [], $condition, $limit, $start, $shuffle);
138         }
139 }