Browse Source

fix data obtain from the memory cache

main
ghost 3 years ago
parent
commit
e9474b1214
  1. 107
      src/application/controller/api/user/avatar.php
  2. 125
      src/application/controller/api/user/profile.php

107
src/application/controller/api/user/avatar.php

@ -28,73 +28,76 @@ if (isset($_SESSION['userName'])) { @@ -28,73 +28,76 @@ if (isset($_SESSION['userName'])) {
'avatar' => $mcAvatar
];
/*
* Step 2: try to obtain avatar from DHT
*
* */
} else if ($dhtAvatarRevisions = $_twister->getDHTAvatarRevisions($userName)) {
// Add DHT version if not exists
foreach ((array) $dhtAvatarRevisions as $dhtAvatarRevision) {
if (!$_modelAvatar->versionExists($userId,
$dhtAvatarRevision['height'],
$dhtAvatarRevision['seq'])) {
} else {
$_modelAvatar->add($userId,
$dhtAvatarRevision['height'],
$dhtAvatarRevision['seq'],
$dhtAvatarRevision['time'],
$dhtAvatarRevision['data']);
/*
* Step 2: try to obtain avatar from DHT
*
* */
if ($dhtAvatarRevisions = $_twister->getDHTAvatarRevisions($userName)) {
// Add DHT version if not exists
foreach ((array) $dhtAvatarRevisions as $dhtAvatarRevision) {
if (!$_modelAvatar->versionExists($userId,
$dhtAvatarRevision['height'],
$dhtAvatarRevision['seq'])) {
$_modelAvatar->add($userId,
$dhtAvatarRevision['height'],
$dhtAvatarRevision['seq'],
$dhtAvatarRevision['time'],
$dhtAvatarRevision['data']);
}
}
}
}
/*
* Step 3: Select latest version available from DB revisions
*
* */
$dbAvatarRevision = $_modelAvatar->get($userId);
/*
* Step 3: Select latest version available from DB revisions
*
* */
$dbAvatarRevision = $_modelAvatar->get($userId);
if ($dbAvatarRevision && Valid::base64image($dbAvatarRevision['data'])) {
if ($dbAvatarRevision && Valid::base64image($dbAvatarRevision['data'])) {
// Response
$response = [
'success' => true,
'message' => _('Avatar successfully received from DHT/DB'),
'avatar' => $dbAvatarRevision['data'] // format
];
// Response
$response = [
'success' => true,
'message' => _('Avatar successfully received from DHT/DB'),
'avatar' => $dbAvatarRevision['data'] // format
];
// Save request into the cache pool
$_memcache->set('api.user.avatar.' . $userName, $dbAvatarRevision['data'], MEMCACHE_COMPRESS, MEMCACHE_DHT_AVATAR_TIMEOUT);
// Save request into the cache pool
$_memcache->set('api.user.avatar.' . $userName, $dbAvatarRevision['data'], MEMCACHE_COMPRESS, MEMCACHE_DHT_AVATAR_TIMEOUT);
// Cache, DHT, DB not contain any the avatar details about user requested,
// Generate and return identity icon
} else {
// Cache, DHT, DB not contain any the avatar details about user requested,
// Generate and return identity icon
} else {
// Generate identity icon
$fileName = md5($userName);
$filePath = PROJECT_DIR . '/cache/image/' . $fileName . '.jpeg';
// Generate identity icon
$fileName = md5($userName);
$filePath = PROJECT_DIR . '/cache/image/' . $fileName . '.jpeg';
// Identity icons supports file cache
if (!file_exists($filePath)) {
// Identity icons supports file cache
if (!file_exists($filePath)) {
$icon = new Icon();
$image = $icon->generateImageResource($fileName, 42, 42, false);
$icon = new Icon();
$image = $icon->generateImageResource($fileName, 42, 42, false);
file_put_contents($filePath, $image);
}
file_put_contents($filePath, $image);
}
$identityIcon = 'data:image/jpeg;base64,' . base64_encode(file_get_contents($filePath));
$identityIcon = 'data:image/jpeg;base64,' . base64_encode(file_get_contents($filePath));
$response = [
'success' => true,
'message' => _('Could not receive any avatar details, generated identity icon'),
'avatar' => $identityIcon
];
$response = [
'success' => true,
'message' => _('Could not receive any avatar details, generated identity icon'),
'avatar' => $identityIcon
];
// Save identity icon into the cache pool
$_memcache->set('api.user.avatar.' . $userName, $identityIcon, MEMCACHE_COMPRESS, MEMCACHE_DHT_AVATAR_TIMEOUT);
// Save identity icon into the cache pool
$_memcache->set('api.user.avatar.' . $userName, $identityIcon, MEMCACHE_COMPRESS, MEMCACHE_DHT_AVATAR_TIMEOUT);
}
}
// User not found in the local database registry

125
src/application/controller/api/user/profile.php

@ -28,70 +28,73 @@ if (isset($_SESSION['userName'])) { @@ -28,70 +28,73 @@ if (isset($_SESSION['userName'])) {
'profile' => $mcProfile
];
/*
* Step 2: try to obtain profile from DHT
*
* */
} else if ($dhtProfileRevisions = $_twister->getDHTProfileRevisions($userName)) {
// Add DHT version if not exists
foreach ((array) $dhtProfileRevisions as $dhtProfileRevision) {
// Save revision into the database if not exists
if (!$_modelProfile->versionExists($userId,
$dhtProfileRevision['height'],
$dhtProfileRevision['seq'])) {
$_modelProfile->add($userId,
$dhtProfileRevision['height'],
$dhtProfileRevision['seq'],
$dhtProfileRevision['time'],
$dhtProfileRevision['fullName'],
$dhtProfileRevision['bio'],
$dhtProfileRevision['location'],
$dhtProfileRevision['url'],
$dhtProfileRevision['bitMessage'],
$dhtProfileRevision['tox']);
} else {
/*
* Step 2: try to obtain profile from DHT
*
* */
if ($dhtProfileRevisions = $_twister->getDHTProfileRevisions($userName)) {
// Add DHT version if not exists
foreach ((array) $dhtProfileRevisions as $dhtProfileRevision) {
// Save revision into the database if not exists
if (!$_modelProfile->versionExists($userId,
$dhtProfileRevision['height'],
$dhtProfileRevision['seq'])) {
$_modelProfile->add($userId,
$dhtProfileRevision['height'],
$dhtProfileRevision['seq'],
$dhtProfileRevision['time'],
$dhtProfileRevision['fullName'],
$dhtProfileRevision['bio'],
$dhtProfileRevision['location'],
$dhtProfileRevision['url'],
$dhtProfileRevision['bitMessage'],
$dhtProfileRevision['tox']);
}
}
}
}
/*
* Step 3: Select latest version available from DB revisions
*
* */
if ($dbProfileRevision = $_modelProfile->get($userId)) {
// Format output
$profile = [
'userName' => $userName,
'fullName' => Format::text($dbProfileRevision['fullName']),
'location' => Format::text($dbProfileRevision['location']),
'url' => Format::text($dbProfileRevision['url']),
'bitMessage' => Format::text($dbProfileRevision['bitMessage']),
'tox' => Format::text($dbProfileRevision['tox']),
'bio' => Format::text($dbProfileRevision['bio']),
];
// Save request into the cache pool
$_memcache->set('api.user.profile.' . $userName, $profile, MEMCACHE_COMPRESS, MEMCACHE_DHT_PROFILE_TIMEOUT);
// Response
$response = [
'success' => true,
'message' => _('Profile successfully received from DHT/DB'),
'profile' => $profile
];
// Cache, DHT, DB not contain any the details about user requested
} else {
$response = [
'success' => false,
'message' => _('Could not receive any profile details'),
'profile' => []
];
/*
* Step 3: Select latest version available from DB revisions
*
* */
if ($dbProfileRevision = $_modelProfile->get($userId)) {
// Format output
$profile = [
'userName' => $userName,
'fullName' => Format::text($dbProfileRevision['fullName']),
'location' => Format::text($dbProfileRevision['location']),
'url' => Format::text($dbProfileRevision['url']),
'bitMessage' => Format::text($dbProfileRevision['bitMessage']),
'tox' => Format::text($dbProfileRevision['tox']),
'bio' => Format::text($dbProfileRevision['bio']),
];
// Save request into the cache pool
$_memcache->set('api.user.profile.' . $userName, $profile, MEMCACHE_COMPRESS, MEMCACHE_DHT_PROFILE_TIMEOUT);
// Response
$response = [
'success' => true,
'message' => _('Profile successfully received from DHT/DB'),
'profile' => $profile
];
// Cache, DHT, DB not contain any the details about user requested
} else {
$response = [
'success' => false,
'message' => _('Could not receive any profile details'),
'profile' => []
];
}
}
// User not found in the local database registry

Loading…
Cancel
Save