Add api_friendships_destroy() like mod/unfollow.php

This commit is contained in:
root 2018-09-13 23:23:53 +02:00
parent aa2ab3d0e7
commit 3f85fee7e3

View file

@ -3629,6 +3629,102 @@ function api_direct_messages_destroy($type)
/// @TODO move to top of file or somewhere better
api_register_func('api/direct_messages/destroy', 'api_direct_messages_destroy', true, API_METHOD_DELETE);
function api_friendships_destroy($type)
{
$a = get_app();
logger("OrigUser: ".$a->user['uid'], LOGGER_DEBUG);
logger("ContactUser: ".$_REQUEST['user_id'], LOGGER_DEBUG);
if (api_user() === false) {
throw new ForbiddenException();
}
$uid = local_user();
$contact_id = (x($_REQUEST, 'user_id') ? $_REQUEST['user_id'] : null);
if ($contact_id == null) {
logger("No POST user_id", LOGGER_DEBUG);
throw new BadRequestException("no user_id specified");
}
$contact = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d AND `self` = 0", intval($contact_id), 0);
if(!DBA::isResult($contact)) {
logger("No contact by _id", LOGGER_DEBUG);
throw new BadRequestException("no contact found to given ID");
}
$url = $contact[0]["url"];
logger("Contact Url: ".$contact[0]["url"], LOGGER_DEBUG);
$condition = ["`uid` = ? AND (`rel` = ? OR `rel` = ?) AND (`nurl` = ? OR `alias` = ? OR `alias` = ?)",
$uid, Contact::SHARING, Contact::FRIEND, normalise_link($url),
normalise_link($url), $url];
$contact = DBA::selectFirst('contact', [], $condition);
if (!DBA::isResult($contact)) {
logger("No contact founded", LOGGER_DEBUG);
throw new BadRequestException("Not following Contact");
}
if (!in_array($contact['network'], Protocol::NATIVE_SUPPORT)) {
logger("Not supported", LOGGER_DEBUG);
throw new BadRequestException("Not supported");
}
$dissolve = ($contact['rel'] == Contact::SHARING);
$owner = User::getOwnerDataById($uid);
if ($owner) {
Contact::terminateFriendship($owner, $contact, $dissolve);
}
// Sharing-only contacts get deleted as there no relationship any more
if ($dissolve) {
Contact::remove($contact['id']);
$return_path = 'contacts';
} else {
DBA::update('contact', ['rel' => Contact::FOLLOWER], ['id' => $contact['id']]);
}
///////////////////
/*
$contact = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d AND `self` = 0", intval($contact_id), 0);
if(!DBA::isResult($contact)) {
throw new BadRequestException("no contact found to given ID");
}
logger("Contact Url: ".$contact[0]["url"], LOGGER_DEBUG);
$contact_uid = Contact::getIdForUrl($contact[0]["url"], $a->user["uid"]);
if ($contact_uid == 0) {
logger("No UserURL founded", LOGGER_DEBUG);
throw new BadRequestException("No contact id found");
}
logger("User found: ".$contact_uid, LOGGER_DEBUG);
$contact_user = q("SELECT * FROM `user` WHERE uid = $contact_uid");
if(!DBA::isResult($contact_user)) {
logger("No Contact to ContactId founded", LOGGER_DEBUG);
throw new BadRequestException("No Profile found");
}
logger("Contact founded!", LOGGER_DEBUG);
logger("Founded User: ".$contact_user[0][nick]." + ".$contact[0]["id"]);
Contact::terminateFriendship($a->user, $contact_user[0]);
Contact::remove($contact_user[0]['uid']);
*/
$answer = ['result' => 'ok', 'contact' => 'contact deleted'];
return api_format_data("friendships-destroy", $type, ['result' => $answer]);
}
api_register_func('api/friendships/destroy', 'api_friendships_destroy', true, API_METHOD_POST);
/**
*
* @param string $type Return type (atom, rss, xml, json)