2020-08-04 00:47:02 -04:00
|
|
|
<?php
|
|
|
|
/**
|
2021-03-29 02:40:20 -04:00
|
|
|
* @copyright Copyright (C) 2010-2021, the Friendica project
|
2020-08-04 00:47:02 -04:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Friendica\Model\Contact;
|
|
|
|
|
2021-09-10 16:22:24 -04:00
|
|
|
use Exception;
|
2021-09-10 09:05:16 -04:00
|
|
|
use Friendica\Core\Logger;
|
2021-10-02 16:00:06 -04:00
|
|
|
use Friendica\Core\Protocol;
|
2021-09-10 09:05:16 -04:00
|
|
|
use Friendica\Core\System;
|
|
|
|
use Friendica\Database\Database;
|
2020-08-04 00:47:02 -04:00
|
|
|
use Friendica\Database\DBA;
|
2021-09-10 16:22:24 -04:00
|
|
|
use Friendica\Database\DBStructure;
|
2020-08-04 00:47:02 -04:00
|
|
|
use Friendica\Model\Contact;
|
2021-09-10 09:05:16 -04:00
|
|
|
use Friendica\Model\ItemURI;
|
2021-09-10 16:22:24 -04:00
|
|
|
use PDOException;
|
2020-08-04 00:47:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class provides information about user related contacts based on the "user-contact" table.
|
|
|
|
*/
|
|
|
|
class User
|
|
|
|
{
|
2021-09-10 09:05:16 -04:00
|
|
|
/**
|
|
|
|
* Insert a user-contact for a given contact array
|
|
|
|
*
|
|
|
|
* @param array $contact
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function insertForContactArray(array $contact)
|
|
|
|
{
|
2021-09-11 08:43:46 -04:00
|
|
|
if (empty($contact['uid'])) {
|
|
|
|
// We don't create entries for the public user - by now
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($contact['uri-id']) && empty($contact['url'])) {
|
2021-09-10 09:05:16 -04:00
|
|
|
Logger::info('Missing contact details', ['contact' => $contact, 'callstack' => System::callstack(20)]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($contact['uri-id'])) {
|
|
|
|
$contact['uri-id'] = ItemURI::getIdByURI($contact['url']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$pcontact = Contact::selectFirst(['id'], ['uri-id' => $contact['uri-id'], 'uid' => 0]);
|
2021-09-11 08:43:46 -04:00
|
|
|
if (!empty($contact['uri-id']) && DBA::isResult($pcontact)) {
|
|
|
|
$pcid = $pcontact['id'];
|
2021-09-11 09:00:12 -04:00
|
|
|
} elseif (empty($contact['url']) || !($pcid = Contact::getIdForURL($contact['url'], 0, false))) {
|
2021-09-11 03:59:46 -04:00
|
|
|
Logger::info('Public contact for user not found', ['uri-id' => $contact['uri-id'], 'uid' => $contact['uid']]);
|
2021-09-10 09:05:16 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-13 10:20:20 -04:00
|
|
|
$fields = self::preparedFields($contact);
|
2021-09-11 08:43:46 -04:00
|
|
|
$fields['cid'] = $pcid;
|
2021-09-10 16:53:10 -04:00
|
|
|
$fields['uid'] = $contact['uid'];
|
|
|
|
$fields['uri-id'] = $contact['uri-id'];
|
2021-09-10 09:05:16 -04:00
|
|
|
|
2021-09-11 08:43:46 -04:00
|
|
|
$ret = DBA::insert('user-contact', $fields, Database::INSERT_UPDATE);
|
2021-09-10 09:05:16 -04:00
|
|
|
|
2021-09-11 08:43:46 -04:00
|
|
|
Logger::info('Inserted user contact', ['uid' => $contact['uid'], 'cid' => $pcid, 'uri-id' => $contact['uri-id'], 'ret' => $ret]);
|
2021-09-10 09:05:16 -04:00
|
|
|
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
2021-09-10 16:22:24 -04:00
|
|
|
/**
|
|
|
|
* Apply changes from contact update data to user-contact table
|
|
|
|
*
|
|
|
|
* @param array $fields
|
|
|
|
* @param array $condition
|
|
|
|
* @return void
|
|
|
|
* @throws PDOException
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2021-09-10 19:59:33 -04:00
|
|
|
public static function updateByContactUpdate(array $fields, array $condition)
|
2021-09-10 16:22:24 -04:00
|
|
|
{
|
|
|
|
DBA::transaction();
|
|
|
|
|
2021-09-13 10:20:20 -04:00
|
|
|
$update_fields = self::preparedFields($fields);
|
2021-09-10 16:22:24 -04:00
|
|
|
if (!empty($update_fields)) {
|
|
|
|
$contacts = DBA::select('contact', ['uri-id', 'uid'], $condition);
|
|
|
|
while ($row = DBA::fetch($contacts)) {
|
2021-09-11 08:43:46 -04:00
|
|
|
if (empty($row['uri-id']) || empty($contact['uid'])) {
|
2021-09-10 16:22:24 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$ret = DBA::update('user-contact', $update_fields, ['uri-id' => $row['uri-id'], 'uid' => $row['uid']]);
|
|
|
|
Logger::info('Updated user contact', ['uid' => $row['uid'], 'uri-id' => $row['uri-id'], 'ret' => $ret]);
|
|
|
|
}
|
|
|
|
|
|
|
|
DBA::close($contacts);
|
|
|
|
}
|
|
|
|
DBA::commit();
|
|
|
|
}
|
|
|
|
|
2021-09-13 10:20:20 -04:00
|
|
|
/**
|
|
|
|
* Prepare field data for update/insert
|
|
|
|
*
|
|
|
|
* @param array $fields
|
|
|
|
* @return array prepared fields
|
|
|
|
*/
|
|
|
|
private static function preparedFields(array $fields): array
|
|
|
|
{
|
|
|
|
unset($fields['uid']);
|
|
|
|
unset($fields['cid']);
|
|
|
|
unset($fields['uri-id']);
|
|
|
|
|
|
|
|
if (isset($fields['readonly'])) {
|
|
|
|
$fields['ignored'] = $fields['readonly'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($fields['self'])) {
|
|
|
|
$fields['rel'] = Contact::SELF;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DBStructure::getFieldsForTable('user-contact', $fields);
|
|
|
|
}
|
|
|
|
|
2020-08-04 00:47:02 -04:00
|
|
|
/**
|
|
|
|
* Block contact id for user id
|
|
|
|
*
|
|
|
|
* @param int $cid Either public contact id or user's contact id
|
|
|
|
* @param int $uid User ID
|
|
|
|
* @param boolean $blocked Is the contact blocked or unblocked?
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public static function setBlocked($cid, $uid, $blocked)
|
|
|
|
{
|
2021-07-11 05:39:34 -04:00
|
|
|
$cdata = Contact::getPublicAndUserContactID($cid, $uid);
|
2020-08-04 00:47:02 -04:00
|
|
|
if (empty($cdata)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-02 16:00:06 -04:00
|
|
|
$contact = Contact::getById($cdata['public']);
|
|
|
|
if ($blocked) {
|
|
|
|
Protocol::block($contact);
|
|
|
|
} else {
|
|
|
|
Protocol::unblock($contact);
|
|
|
|
}
|
|
|
|
|
2020-08-04 00:47:02 -04:00
|
|
|
if ($cdata['user'] != 0) {
|
|
|
|
DBA::update('contact', ['blocked' => $blocked], ['id' => $cdata['user'], 'pending' => false]);
|
|
|
|
}
|
|
|
|
|
|
|
|
DBA::update('user-contact', ['blocked' => $blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns "block" state for contact id and user id
|
|
|
|
*
|
|
|
|
* @param int $cid Either public contact id or user's contact id
|
|
|
|
* @param int $uid User ID
|
|
|
|
*
|
|
|
|
* @return boolean is the contact id blocked for the given user?
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public static function isBlocked($cid, $uid)
|
|
|
|
{
|
2021-07-11 05:39:34 -04:00
|
|
|
$cdata = Contact::getPublicAndUserContactID($cid, $uid);
|
2020-08-04 00:47:02 -04:00
|
|
|
if (empty($cdata)) {
|
2020-11-11 02:50:22 -05:00
|
|
|
return false;
|
2020-08-04 00:47:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
$public_blocked = false;
|
|
|
|
|
|
|
|
if (!empty($cdata['public'])) {
|
|
|
|
$public_contact = DBA::selectFirst('user-contact', ['blocked'], ['cid' => $cdata['public'], 'uid' => $uid]);
|
|
|
|
if (DBA::isResult($public_contact)) {
|
|
|
|
$public_blocked = $public_contact['blocked'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$user_blocked = $public_blocked;
|
|
|
|
|
|
|
|
if (!empty($cdata['user'])) {
|
|
|
|
$user_contact = DBA::selectFirst('contact', ['blocked'], ['id' => $cdata['user'], 'pending' => false]);
|
|
|
|
if (DBA::isResult($user_contact)) {
|
|
|
|
$user_blocked = $user_contact['blocked'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($user_blocked != $public_blocked) {
|
|
|
|
DBA::update('user-contact', ['blocked' => $user_blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $user_blocked;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ignore contact id for user id
|
|
|
|
*
|
|
|
|
* @param int $cid Either public contact id or user's contact id
|
|
|
|
* @param int $uid User ID
|
|
|
|
* @param boolean $ignored Is the contact ignored or unignored?
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public static function setIgnored($cid, $uid, $ignored)
|
|
|
|
{
|
2021-07-11 05:39:34 -04:00
|
|
|
$cdata = Contact::getPublicAndUserContactID($cid, $uid);
|
2020-08-04 00:47:02 -04:00
|
|
|
if (empty($cdata)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($cdata['user'] != 0) {
|
|
|
|
DBA::update('contact', ['readonly' => $ignored], ['id' => $cdata['user'], 'pending' => false]);
|
|
|
|
}
|
|
|
|
|
|
|
|
DBA::update('user-contact', ['ignored' => $ignored], ['cid' => $cdata['public'], 'uid' => $uid], true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns "ignore" state for contact id and user id
|
|
|
|
*
|
|
|
|
* @param int $cid Either public contact id or user's contact id
|
|
|
|
* @param int $uid User ID
|
|
|
|
*
|
|
|
|
* @return boolean is the contact id ignored for the given user?
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public static function isIgnored($cid, $uid)
|
|
|
|
{
|
2021-07-11 05:39:34 -04:00
|
|
|
$cdata = Contact::getPublicAndUserContactID($cid, $uid);
|
2020-08-04 00:47:02 -04:00
|
|
|
if (empty($cdata)) {
|
2020-11-11 02:50:22 -05:00
|
|
|
return false;
|
2020-08-04 00:47:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
$public_ignored = false;
|
|
|
|
|
|
|
|
if (!empty($cdata['public'])) {
|
|
|
|
$public_contact = DBA::selectFirst('user-contact', ['ignored'], ['cid' => $cdata['public'], 'uid' => $uid]);
|
|
|
|
if (DBA::isResult($public_contact)) {
|
|
|
|
$public_ignored = $public_contact['ignored'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$user_ignored = $public_ignored;
|
|
|
|
|
|
|
|
if (!empty($cdata['user'])) {
|
|
|
|
$user_contact = DBA::selectFirst('contact', ['readonly'], ['id' => $cdata['user'], 'pending' => false]);
|
|
|
|
if (DBA::isResult($user_contact)) {
|
|
|
|
$user_ignored = $user_contact['readonly'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($user_ignored != $public_ignored) {
|
|
|
|
DBA::update('user-contact', ['ignored' => $user_ignored], ['cid' => $cdata['public'], 'uid' => $uid], true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $user_ignored;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set "collapsed" for contact id and user id
|
|
|
|
*
|
|
|
|
* @param int $cid Either public contact id or user's contact id
|
|
|
|
* @param int $uid User ID
|
|
|
|
* @param boolean $collapsed are the contact's posts collapsed or uncollapsed?
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public static function setCollapsed($cid, $uid, $collapsed)
|
|
|
|
{
|
2021-07-11 05:39:34 -04:00
|
|
|
$cdata = Contact::getPublicAndUserContactID($cid, $uid);
|
2020-08-04 00:47:02 -04:00
|
|
|
if (empty($cdata)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBA::update('user-contact', ['collapsed' => $collapsed], ['cid' => $cdata['public'], 'uid' => $uid], true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns "collapsed" state for contact id and user id
|
|
|
|
*
|
|
|
|
* @param int $cid Either public contact id or user's contact id
|
|
|
|
* @param int $uid User ID
|
|
|
|
*
|
|
|
|
* @return boolean is the contact id blocked for the given user?
|
|
|
|
* @throws HTTPException\InternalServerErrorException
|
|
|
|
* @throws \ImagickException
|
|
|
|
*/
|
|
|
|
public static function isCollapsed($cid, $uid)
|
|
|
|
{
|
2021-07-11 05:39:34 -04:00
|
|
|
$cdata = Contact::getPublicAndUserContactID($cid, $uid);
|
2020-08-04 00:47:02 -04:00
|
|
|
if (empty($cdata)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$collapsed = false;
|
|
|
|
|
|
|
|
if (!empty($cdata['public'])) {
|
|
|
|
$public_contact = DBA::selectFirst('user-contact', ['collapsed'], ['cid' => $cdata['public'], 'uid' => $uid]);
|
|
|
|
if (DBA::isResult($public_contact)) {
|
|
|
|
$collapsed = $public_contact['collapsed'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $collapsed;
|
|
|
|
}
|
|
|
|
}
|