opensource-socialnetwork / OssnServices

Integrate your OSSN powered social network into 3rd party application
https://www.opensource-socialnetwork.org/webservices/view/3616/welcome-to-ossn-web-services-api-integrate-ossn-into-your-application
11 stars 8 forks source link

ossn_entities table flooded by cover_time #5

Closed mirabledictu closed 3 years ago

mirabledictu commented 3 years ago

Hi, I checked my ossn_entities table and it's full of cover_time subtypes. I guess it's because of the api setting the user authenticated for specific routes? Do we have a method to clean it? Thanks

Screen Shot 2021-08-28 at 7 16 56 PM
lianglee commented 3 years ago

Hi,

Thanks for reporting there is definitely way to clean it, First I thought you created issue in OSSN core :) I was surprised because this thing shouldn't happen.

I will look into OssnServices tomorrow and let you know and will write you a simple script to delete these cover_time.

lianglee commented 3 years ago

I just checked

https://github.com/opensource-socialnetwork/OssnServices/blob/master/plugins/default/services/v1.0/photos_cover_add.php and this shouldn't be happening,

Can you tell me more about how you are utilizing the photo_cover_add endpoint? Can you confirm if this is cause of the API?

mirabledictu commented 3 years ago

I actually am not using that endpoint yet. Here's a wild guess.. since wall_list_home and other api contains the user info too.. maybe it's because of getCoverURL()?

https://github.com/opensource-socialnetwork/OssnServices/blob/master/classes/OssnServices.php#L174

Here's the getCoverUrl method

/**
 * Get cover URL
 *
 * @param object $user OssnUser object
 *
 * @return string|boolean
 */
public function getCoverURL($user = ''){
    if(!empty($user) && $user instanceof OssnUser){
        if(!isset($user->cover_time) && empty($user->cover_time)){
            $user->cover_time = time();
            $user->data->cover_time = $user->cover_time;
            $user->save();
        }s
        return ossn_site_url("cover/{$user->username}/".md5($user->cover_time).'.jpg');
    }
    return false;
}
lianglee commented 3 years ago

Ok I tested via simple php script to use API I added various covers for same user and the cover_time entity updated instead of being added again and again.

image

Here is a script

<?php
function curl_file_value($filename, $contentType, $postname){
        if(function_exists('curl_file_create')){
                return curl_file_create($filename, $contentType, $postname);
        }
        $value = "@{$filename};filename=" . $postname;
        if($contentType){
                $value .= ';type=' . $contentType;
        }
        return $value;
}

$filename = dirname(__FILE__) . '/cover.jpg';
$cfile    = curl_file_value($filename, 'image/jpeg', 'cattle-01.jpg');
$data     = array(
        'userphoto'     => $cfile,
        'guid'          => 1,
        'api_key_token' => 'dd10824ff9b961d94ee2b1bc8f3cd8aa35162933a73236652fa5d465833da75f',
);
$ch = curl_init();
curl_setopt_array($ch, array(
        CURLOPT_URL            => 'http://localhost/ossn/api/v1.0/photos_cover_add',
        CURLOPT_RETURNTRANSFER => true,
        CURLINFO_HEADER_OUT    => true, 
        CURLOPT_HEADER         => true, 
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_POST           => true,
        CURLOPT_POSTFIELDS     => $data,
));
$result      = curl_exec($ch);
$header_info = curl_getinfo($ch, CURLINFO_HEADER_OUT);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header      = substr($result, 0, $header_size);
$body        = substr($result, $header_size);
curl_close($ch);

$json = json_decode($body);
echo "<pre>";
print_r($json);

To cleanup you can use the following code (please do backups for datafolder and database before doing it)

<?php
define('OSSN_ALLOW_SYSTEM_START', true);
require_once 'system/start.php';
//clean
set_time_limit(0);
$database = new OssnDatabase();
$database->statement("SELECT * FROM `ossn_entities` WHERE `subtype` LIKE 'cover_time';");
$database->execute();
$list = $database->fetch(true);
if($list){
        //delete all entties and reserve for which users its deleted,
        $users = array();
        foreach ($list as $item){
                $users[] = $item->owner_guid;
                //delete entity subtype of cover_time
                $entity = new OssnEntities();
                $entity->deleteEntity($item->guid);
        }
        if($users){
                foreach ($users as $guid){
                        $user = ossn_user_by_guid($guid);
                        if($user){
                                $user->data->cover_time = time();
                                $user->save();
                        }
                }
        }
}
lianglee commented 3 years ago

I actually am not using that endpoint yet. Here's a wild guess.. since wall_list_home and other api contains the user info too.. maybe it's because of getCoverURL()?

https://github.com/opensource-socialnetwork/OssnServices/blob/master/classes/OssnServices.php#L174

Here's the getCoverUrl method

/**
 * Get cover URL
 *
 * @param object $user OssnUser object
 *
 * @return string|boolean
 */
public function getCoverURL($user = ''){
    if(!empty($user) && $user instanceof OssnUser){
        if(!isset($user->cover_time) && empty($user->cover_time)){
            $user->cover_time = time();
            $user->data->cover_time = $user->cover_time;
            $user->save();
        }s
        return ossn_site_url("cover/{$user->username}/".md5($user->cover_time).'.jpg');
    }
    return false;
}

Still there should not be anything more then onces in ossn_entities in database. Because save() takes care of this

mirabledictu commented 3 years ago

I confirm that calling /api/v1.0/wall_list_home generates cover_time

lianglee commented 3 years ago

Can you confirm which OSSN version are you running? because for me /api/v1.0/wall_list_home not generating more then one entry.

lianglee commented 3 years ago

I tested also on wall_list_user it gives cover_url false in API response

image

Can you try to run a fresh installation of OSSN and let me know results and please let me know which OSSN version you are currently trying?

mirabledictu commented 3 years ago

Will try in a bit. Thanks! I am using OSSN 5.2 and using this API for months now..

lianglee commented 3 years ago

Ossn v5.2 is too old to use :) Please try on v5.6 alot of bugs in v5.2

mirabledictu commented 3 years ago

I tested also on wall_list_user it gives cover_url false in API response

image

Can you try to run a fresh installation of OSSN and let me know results and please let me know which OSSN version you are currently trying?

I'm getting the cover url

mirabledictu commented 3 years ago

Ossn v5.2 is too old to use :) Please try on v5.6 alot of bugs in v5.2

Yeah. I'll try to upgrade, thanks

lianglee commented 3 years ago

I tested also on wall_list_user it gives cover_url false in API response image Can you try to run a fresh installation of OSSN and let me know results and please let me know which OSSN version you are currently trying?

I'm getting the cover url

Ossn v5.2 is too old to use :) Please try on v5.6 alot of bugs in v5.2

mirabledictu commented 3 years ago

Yeah thanks. I have custom codes in a lot of places so it's not gonna be a one time upgrade, I have to check it one by one ugh