2022-10-21 21:26:41 +02:00
|
|
|
<?php
|
2022-10-24 14:16:30 +02:00
|
|
|
/**
|
|
|
|
* https://github.com/duniter-gchange/gchange-pod/blob/master/src/site/markdown/REST_API.md
|
|
|
|
*/
|
2022-10-21 21:26:41 +02:00
|
|
|
|
|
|
|
require_once('DAO.class.php');
|
2022-10-22 18:35:53 +02:00
|
|
|
require_once('GchangeUser.class.php');
|
2022-10-24 19:14:58 +02:00
|
|
|
require_once('GchangeRecord.class.php');
|
2022-10-26 12:12:20 +02:00
|
|
|
require_once('GchangeRating.class.php');
|
2022-10-21 21:26:41 +02:00
|
|
|
|
|
|
|
class Gchange {
|
|
|
|
|
|
|
|
private $dao;
|
|
|
|
|
|
|
|
private $cacheDir = __DIR__ . '/../cache/';
|
|
|
|
|
|
|
|
private $isActivatedCache = true;
|
|
|
|
|
|
|
|
private $cacheLongevity = array(
|
|
|
|
'placesNearby' => 604800, // 3 jours
|
2022-10-24 19:14:58 +02:00
|
|
|
'usersNearby' => 43200, // 12 heures
|
2022-10-21 21:26:41 +02:00
|
|
|
'users' => 43200, // 12 heures
|
|
|
|
'records' => 900 // 15 min
|
|
|
|
);
|
|
|
|
|
|
|
|
public function __construct () {
|
|
|
|
|
|
|
|
$this->dao = DAO::getInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRecordsByIssuer ($issuer) {
|
|
|
|
|
|
|
|
|
|
|
|
$recordsCacheDir = 'records/user/';
|
|
|
|
$recordsCacheFile = $issuer . '.json';
|
|
|
|
|
|
|
|
$json = $this->getJsonFromCache($recordsCacheDir, $recordsCacheFile, $this->cacheLongevity['records']);
|
|
|
|
|
|
|
|
if (empty($json)) {
|
|
|
|
|
|
|
|
$n = 20;
|
|
|
|
|
|
|
|
$queryParams = [
|
|
|
|
'size' => $n,
|
|
|
|
'query' => [
|
2022-10-24 19:14:58 +02:00
|
|
|
'bool' => [
|
|
|
|
'filter' => [
|
2022-10-21 21:26:41 +02:00
|
|
|
'term' => [
|
|
|
|
'issuer' => $issuer
|
|
|
|
]
|
2022-10-24 19:14:58 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
, 'range' => [
|
|
|
|
'stock' => [
|
|
|
|
'gte' => 1
|
|
|
|
]
|
|
|
|
]
|
|
|
|
*/
|
|
|
|
]
|
|
|
|
/*
|
|
|
|
, 'filter' => [
|
|
|
|
]
|
|
|
|
*/
|
|
|
|
|
|
|
|
, 'must_not' => [
|
2022-10-21 21:26:41 +02:00
|
|
|
[ "term" => ["stock" => 0] ]
|
|
|
|
]
|
2022-10-24 19:14:58 +02:00
|
|
|
|
2022-10-21 21:26:41 +02:00
|
|
|
]
|
|
|
|
],
|
|
|
|
'sort' => [
|
|
|
|
['time' => 'desc']
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$json = $this->dao->fetchJson('/market/record/_search', 'gchange', $queryParams);
|
|
|
|
|
|
|
|
$this->cacheJson($recordsCacheDir, $recordsCacheFile, $json);
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = json_decode($json);
|
|
|
|
|
2022-10-24 19:14:58 +02:00
|
|
|
|
|
|
|
$records = [];
|
|
|
|
|
|
|
|
foreach ($result->hits->hits as $hit) {
|
|
|
|
|
|
|
|
$records[] = new GchangeRecord($hit);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $records;
|
2022-10-21 21:26:41 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getJsonFromCache ($dir, $file, $cacheLongevity) {
|
|
|
|
|
|
|
|
if (!$this->isActivatedCache) {
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$json = null;
|
|
|
|
$jsonFullPath = $this->cacheDir . $dir . $file;
|
|
|
|
|
|
|
|
if (file_exists($jsonFullPath) and ((time() - filemtime($jsonFullPath)) < $cacheLongevity)) {
|
|
|
|
|
|
|
|
$json = file_get_contents($jsonFullPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $json;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private function cacheJson ($dir, $file, $json) {
|
|
|
|
|
|
|
|
if (!$this->isActivatedCache) {
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
$jsonCacheDir = $this->cacheDir . $dir;
|
|
|
|
$jsonFullPath = $jsonCacheDir . $file;
|
|
|
|
|
|
|
|
if (!file_exists($jsonCacheDir)) {
|
|
|
|
|
|
|
|
mkdir($jsonCacheDir, 0777, true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
file_put_contents($jsonFullPath, $json);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUser ($pubkey) {
|
|
|
|
|
|
|
|
$usersCacheDir = 'users/';
|
|
|
|
$usersCacheFile = $pubkey . '.json';
|
|
|
|
|
|
|
|
$json = $this->getJsonFromCache($usersCacheDir, $usersCacheFile, $this->cacheLongevity['users']);
|
|
|
|
|
|
|
|
if (empty($json)) {
|
|
|
|
|
|
|
|
$json = $this->dao->fetchJson(('/user/profile/'. $pubkey), 'gchange');
|
|
|
|
|
|
|
|
$this->cacheJson($usersCacheDir, $usersCacheFile, $json);
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = json_decode($json);
|
|
|
|
|
2022-10-22 18:35:53 +02:00
|
|
|
return new GchangeUser($result);
|
2022-10-21 21:26:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getPlacesNearUser ($user, $radius) {
|
|
|
|
|
2022-10-24 19:14:58 +02:00
|
|
|
return $this->getNearbyPlaces($user->getLat(), $user->getLon(), $radius);
|
|
|
|
|
|
|
|
/*
|
2022-10-21 21:26:41 +02:00
|
|
|
$placesCacheDir = 'places-nearby/user/';
|
2022-10-22 18:35:53 +02:00
|
|
|
$placesCacheFile = $user->getUserGchangeId() . '.json';
|
2022-10-21 21:26:41 +02:00
|
|
|
|
|
|
|
$json = $this->getJsonFromCache($placesCacheDir, $placesCacheFile, $this->cacheLongevity['placesNearby']);
|
|
|
|
|
|
|
|
if (empty($json)) {
|
|
|
|
|
2022-10-22 18:35:53 +02:00
|
|
|
$json = $this->getNearbyPlacesJson($user->getLat(), $user->getLon(), $radius);
|
2022-10-21 21:26:41 +02:00
|
|
|
|
|
|
|
$this->cacheJson($placesCacheDir, $placesCacheFile, $json);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = json_decode($json);
|
|
|
|
|
|
|
|
return $result->hits->hits;
|
2022-10-24 19:14:58 +02:00
|
|
|
|
|
|
|
*/
|
2022-10-21 21:26:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getNearbyPlaces ($lat, $lon, $maxDistance, $minDistance = NULL) {
|
2022-10-22 18:35:53 +02:00
|
|
|
|
2022-10-24 19:14:58 +02:00
|
|
|
$placesCacheDir = 'places-nearby/geopoint/';
|
|
|
|
$placesCacheFile = $lat . ',' . $lon . '.json';
|
|
|
|
|
|
|
|
$json = $this->getJsonFromCache($placesCacheDir, $placesCacheFile, $this->cacheLongevity['placesNearby']);
|
|
|
|
|
|
|
|
if (empty($json)) {
|
|
|
|
|
|
|
|
$json = $this->getNearbyPlacesJson($lat, $lon, $maxDistance, $minDistance);
|
|
|
|
|
|
|
|
$this->cacheJson($placesCacheDir, $placesCacheFile, $json);
|
|
|
|
|
|
|
|
}
|
2022-10-21 21:26:41 +02:00
|
|
|
|
|
|
|
$result = json_decode($json);
|
|
|
|
|
|
|
|
return $result->hits->hits;
|
|
|
|
}
|
|
|
|
|
2022-10-24 19:14:58 +02:00
|
|
|
public function getNearbyUsers ($lat, $lon, $maxDistance, $minDistance = NULL) {
|
|
|
|
|
|
|
|
$placesCacheDir = 'users-nearby/geopoint/';
|
|
|
|
$placesCacheFile = $lat . ',' . $lon . '.json';
|
2022-10-21 21:26:41 +02:00
|
|
|
|
2022-10-24 19:14:58 +02:00
|
|
|
$json = $this->getJsonFromCache($placesCacheDir, $placesCacheFile, $this->cacheLongevity['usersNearby']);
|
|
|
|
|
|
|
|
if (empty($json)) {
|
|
|
|
|
|
|
|
$json = $this->getNearbyPlacesJson($lat, $lon, $maxDistance, $minDistance);
|
|
|
|
|
|
|
|
$this->cacheJson($placesCacheDir, $placesCacheFile, $json);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = json_decode($json);
|
|
|
|
|
|
|
|
return $result->hits->hits;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getImmaterialRecords () {
|
|
|
|
|
|
|
|
$n = 20;
|
|
|
|
|
|
|
|
$queryParams = [
|
|
|
|
'size' => $n,
|
|
|
|
'query' => [
|
|
|
|
'nested' => [
|
|
|
|
'path' => 'category',
|
|
|
|
'query' => [
|
|
|
|
'bool' => [
|
|
|
|
'should' => [
|
|
|
|
[ 'term' => [ 'category.parent' => 'cat31' ] ],
|
|
|
|
[ 'term' => [ 'category.id' => 'cat31' ] ],
|
|
|
|
[ 'term' => [ 'category.parent' => 'cat74' ] ],
|
|
|
|
[ 'term' => [ 'category.id' => 'cat74' ] ]
|
|
|
|
],
|
|
|
|
'must_not' => [
|
|
|
|
[ "term" => ["category.id" => "cat65"] ]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$json = $this->dao->fetchJson('/market/record/_search?pretty', 'gchange', $queryParams);
|
|
|
|
$result = json_decode($json);
|
|
|
|
|
|
|
|
|
|
|
|
$records = [];
|
|
|
|
|
|
|
|
foreach ($result->hits->hits as $hit) {
|
|
|
|
|
|
|
|
$records[] = new GchangeRecord($hit);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $records;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUsersInDaPlace ($placeId) {
|
|
|
|
|
|
|
|
$n = 20;
|
2022-10-21 21:26:41 +02:00
|
|
|
|
2022-10-24 19:14:58 +02:00
|
|
|
$queryParams = [
|
|
|
|
'size' => $n,
|
|
|
|
'query' => [
|
|
|
|
'nested' => [
|
|
|
|
'path' => 'socials',
|
|
|
|
'query' => [
|
|
|
|
'bool' => [
|
|
|
|
'filter' => [
|
|
|
|
'term' => [
|
|
|
|
'socials.url' => 'https://www.gchange.fr/#/app/page/view/'. $placeId .'/'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$json = $this->dao->fetchJson('/user/profile/_search', 'gchange', $queryParams);
|
|
|
|
|
|
|
|
$result = json_decode($json);
|
|
|
|
|
|
|
|
|
|
|
|
$users = [];
|
|
|
|
|
|
|
|
foreach ($result->hits->hits as $hit) {
|
|
|
|
|
|
|
|
$users[] = new GchangeUser($hit);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $users;
|
|
|
|
}
|
|
|
|
|
2022-10-26 12:12:20 +02:00
|
|
|
public function getRatingsSentBy ($issuer) {
|
|
|
|
|
|
|
|
$n = 20;
|
|
|
|
|
|
|
|
$queryParams = [
|
|
|
|
'size' => $n,
|
|
|
|
'query' => [
|
|
|
|
'bool' => [
|
|
|
|
'must' => [
|
|
|
|
'term' => ['kind' => 'STAR']
|
|
|
|
],
|
|
|
|
'filter' => [
|
|
|
|
'term' => ['issuer' => $issuer]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$json = $this->dao->fetchJson('/like/record/_search', 'gchange', $queryParams);
|
|
|
|
|
|
|
|
$result = json_decode($json);
|
|
|
|
|
|
|
|
$ratings = [];
|
|
|
|
|
|
|
|
foreach ($result->hits->hits as $hit) {
|
|
|
|
|
|
|
|
$ratings[] = new GchangeRating($hit);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ratings;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRatingsReceivedBy ($receiver) {
|
|
|
|
|
|
|
|
$n = 20;
|
|
|
|
|
|
|
|
$queryParams = [
|
|
|
|
'size' => $n,
|
|
|
|
'query' => [
|
|
|
|
'bool' => [
|
|
|
|
'must' => [
|
|
|
|
'term' => ['kind' => 'STAR']
|
|
|
|
],
|
|
|
|
'filter' => [
|
|
|
|
'term' => ['id' => $receiver]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$json = $this->dao->fetchJson('/like/record/_search', 'gchange', $queryParams);
|
|
|
|
|
|
|
|
$result = json_decode($json);
|
|
|
|
|
|
|
|
$ratings = [];
|
|
|
|
|
|
|
|
foreach ($result->hits->hits as $hit) {
|
|
|
|
|
|
|
|
$ratings[] = new GchangeRating($hit);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ratings;
|
|
|
|
}
|
|
|
|
|
2022-10-24 19:14:58 +02:00
|
|
|
public function getNearbyPlacesJson ($lat, $lon, $maxDistance, $minDistance = NULL) {
|
|
|
|
|
2022-10-21 21:26:41 +02:00
|
|
|
$n = 20;
|
|
|
|
|
|
|
|
$queryParams = [
|
|
|
|
'size' => $n,
|
|
|
|
'query' => [
|
|
|
|
'bool' => [
|
2022-10-24 19:14:58 +02:00
|
|
|
'filter' => [
|
2022-10-21 21:26:41 +02:00
|
|
|
[
|
|
|
|
'geo_distance' => [
|
|
|
|
|
|
|
|
"distance" => $maxDistance . 'km',
|
|
|
|
"geoPoint"=> [
|
|
|
|
"lat" => $lat,
|
|
|
|
"lon" => $lon
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$json = $this->dao->fetchJson('/page/record/_search', 'gchange', $queryParams);
|
|
|
|
|
|
|
|
return $json;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getNearbyRecords ($lat, $lon, $maxDistance, $minDistance = NULL) {
|
|
|
|
|
|
|
|
$n = 20;
|
|
|
|
|
|
|
|
$queryParams = [
|
|
|
|
'size' => $n,
|
|
|
|
'query' => [
|
|
|
|
'bool' => [
|
|
|
|
'must' => [
|
|
|
|
[
|
|
|
|
'geo_distance' => [
|
|
|
|
|
|
|
|
"distance" => $maxDistance . 'km',
|
|
|
|
"geoPoint"=> [
|
|
|
|
"lat" => $lat,
|
|
|
|
"lon" => $lon
|
|
|
|
]
|
|
|
|
]
|
|
|
|
], [
|
|
|
|
'range' => [
|
|
|
|
'stock' => [
|
|
|
|
'gte' => 1
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$json = $this->dao->fetchJson('/market/record/_search?pretty', 'gchange', $queryParams);
|
|
|
|
$result = json_decode($json);
|
|
|
|
|
|
|
|
|
2022-10-24 19:14:58 +02:00
|
|
|
$records = [];
|
|
|
|
|
|
|
|
foreach ($result->hits->hits as $hit) {
|
|
|
|
|
|
|
|
$records[] = new GchangeRecord($hit);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $records;
|
2022-10-21 21:26:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getShippable () {
|
|
|
|
|
|
|
|
$n = 20;
|
|
|
|
|
|
|
|
$queryParams = [
|
|
|
|
'size' => $n,
|
|
|
|
'query' => [
|
|
|
|
'match' => [
|
|
|
|
'description' => 'envoi possible'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$json = $this->dao->fetchJson('/market/record/_search?pretty', 'gchange', $queryParams);
|
|
|
|
$result = json_decode($json);
|
|
|
|
|
2022-10-24 19:14:58 +02:00
|
|
|
|
|
|
|
$records = [];
|
|
|
|
|
|
|
|
foreach ($result->hits->hits as $hit) {
|
|
|
|
|
|
|
|
$records[] = new GchangeRecord($hit);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $records;
|
2022-10-21 21:26:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getHousingOffers () {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getShippableOffers () {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|