3 * @copyright Copyright (C) 2010-2024, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Content;
24 use Friendica\Content\Text\HTML;
25 use Friendica\Core\Protocol;
26 use Friendica\Core\Renderer;
27 use Friendica\Database\DBA;
29 use Friendica\Model\Contact;
32 * This class handles methods related to the group functionality
37 * Function to list all groups a user is connected with
39 * @param int $uid of the profile owner
40 * @param boolean $lastitem Sort by lastitem
41 * @param boolean $showhidden Show groups which are not hidden
42 * @param boolean $showprivate Show private groups
46 * 'name' => group name
47 * 'id' => number of the key from the array
48 * 'micro' => contact photo in format micro
49 * 'thumb' => contact photo in format thumb
52 public static function getList($uid, $lastitem, $showhidden = true, $showprivate = false)
55 $params = ['order' => ['last-item' => true]];
57 $params = ['order' => ['name']];
61 'contact-type' => Contact::TYPE_COMMUNITY,
62 'network' => [Protocol::DFRN, Protocol::ACTIVITYPUB],
69 $condition = DBA::mergeConditions($condition, ["`platform` != ?", 'peertube']);
72 $condition = DBA::mergeConditions($condition, ['manually-approve' => false]);
76 $condition = DBA::mergeConditions($condition, ['hidden' => false]);
81 $fields = ['id', 'url', 'alias', 'name', 'micro', 'thumb', 'avatar', 'network', 'uid'];
82 $contacts = DBA::select('account-user-view', $fields, $condition, $params);
87 while ($contact = DBA::fetch($contacts)) {
89 'url' => $contact['url'],
90 'alias' => $contact['alias'],
91 'name' => $contact['name'],
92 'id' => $contact['id'],
93 'micro' => $contact['micro'],
94 'thumb' => $contact['thumb'],
97 DBA::close($contacts);
106 * Sidebar widget to show subscribed Friendica groups. If activated
107 * in the settings, it appears in the network page sidebar
109 * @param int $uid The ID of the User
111 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
112 * @throws \ImagickException
114 public static function widget(int $uid)
118 //sort by last updated item
121 $contacts = self::getList($uid, $lastitem, true, true);
122 $total = count($contacts);
125 if (DBA::isResult($contacts)) {
130 foreach ($contacts as $contact) {
132 'url' => 'contact/' . $contact['id'] . '/conversations',
133 'external_url' => Contact::magicLinkByContact($contact),
134 'name' => $contact['name'],
135 'cid' => $contact['id'],
136 'micro' => DI::baseUrl()->remove(Contact::getMicro($contact)),
142 $tpl = Renderer::getMarkupTemplate('widget/group_list.tpl');
144 $o .= Renderer::replaceMacros(
147 '$title' => DI::l10n()->t('Groups'),
148 '$groups' => $entries,
149 '$link_desc' => DI::l10n()->t('External link to group'),
150 '$new_group_page' => 'register/',
152 '$visible_groups' => $visibleGroups,
153 '$showless' => DI::l10n()->t('show less'),
154 '$showmore' => DI::l10n()->t('show more'),
155 '$create_new_group' => DI::l10n()->t('Create new group')]
163 * Format group list as contact block
165 * This function is used to show the group list in
166 * the advanced profile.
168 * @param int $uid The ID of the User
170 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
171 * @throws \ImagickException
173 public static function profileAdvanced($uid)
175 $profile = intval(Feature::isEnabled($uid, 'forumlist_profile'));
182 // placeholder in case somebody wants configurability
185 //don't sort by last updated item
188 $contacts = self::getList($uid, $lastitem, false, false);
191 foreach ($contacts as $contact) {
192 $o .= HTML::micropro($contact, true, 'grouplist-profile-advanced');
194 if ($total_shown == $show_total) {
203 * count unread group items
205 * Count unread items of connected groups and private groups
209 * 'name' => contact/group name
210 * 'count' => counted unseen group items
213 public static function countUnseenItems()
215 $stmtContacts = DBA::p(
216 "SELECT `contact`.`id`, `contact`.`name`, COUNT(*) AS `count` FROM `post-user-view`
217 INNER JOIN `contact` ON `post-user-view`.`contact-id` = `contact`.`id`
218 WHERE `post-user-view`.`uid` = ? AND `post-user-view`.`visible` AND NOT `post-user-view`.`deleted` AND `post-user-view`.`unseen`
219 AND `contact`.`network` IN (?, ?) AND `contact`.`contact-type` = ?
220 AND NOT `contact`.`blocked` AND NOT `contact`.`hidden`
221 AND NOT `contact`.`pending` AND NOT `contact`.`archive`
222 AND `contact`.`uid` = ?
223 GROUP BY `contact`.`id`",
224 DI::userSession()->getLocalUserId(), Protocol::DFRN, Protocol::ACTIVITYPUB, Contact::TYPE_COMMUNITY, DI::userSession()->getLocalUserId()
227 return DBA::toArray($stmtContacts);