mirror of
https://github.com/twisterarmy/cloud-server.git
synced 2025-09-08 11:52:31 +00:00
implement profile avatars receiving from DHT
This commit is contained in:
parent
ab15aa0d35
commit
3e3853125d
Binary file not shown.
72
src/application/controller/api/user/avatar.php
Normal file
72
src/application/controller/api/user/avatar.php
Normal file
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
$response = [
|
||||
'success' => false,
|
||||
'message' => _('Internal server error'),
|
||||
'avatar' => false
|
||||
];
|
||||
|
||||
if (isset($_SESSION['userName'])) {
|
||||
|
||||
$userName = isset($_POST['userName']) ? Filter::userName($_POST['userName']) : $_SESSION['userName'];
|
||||
|
||||
if ($avatarVersions = $_twister->getDHT($userName, 'avatar', 's')) {
|
||||
|
||||
// Check avatar exists
|
||||
if ($userId = $_modelUser->getUserId($userName)) {
|
||||
|
||||
// Add DHT version if not exists
|
||||
foreach ($avatarVersions as $avatarVersion) {
|
||||
|
||||
if (!$_modelAvatar->versionExists($userId,
|
||||
$avatarVersion['p']['height'],
|
||||
$avatarVersion['p']['seq'])) {
|
||||
|
||||
$_modelAvatar->add( $userId,
|
||||
$avatarVersion['p']['height'],
|
||||
$avatarVersion['p']['seq'],
|
||||
$avatarVersion['p']['time'],
|
||||
$avatarVersion['p']['v']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get latest version available
|
||||
if ($avatarInfo = $_modelAvatar->get($userId)) {
|
||||
|
||||
$response = [
|
||||
'success' => true,
|
||||
'message' => _('Avatar successfully received'),
|
||||
'avatar' => $avatarInfo['data']
|
||||
];
|
||||
|
||||
} else {
|
||||
|
||||
$response = [
|
||||
'success' => false,
|
||||
'message' => _('Avatar data not available'),
|
||||
'avatar' => false
|
||||
];
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
$response = [
|
||||
'success' => false,
|
||||
'message' => _('Could not receive avatar details'),
|
||||
'avatar' => false
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
$response = [
|
||||
'success' => false,
|
||||
'message' => _('Session expired. Please, reload the page.'),
|
||||
'avatar' => false
|
||||
];
|
||||
}
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
echo json_encode($response);
|
72
src/application/model/avatar.php
Normal file
72
src/application/model/avatar.php
Normal file
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
class ModelAvatar extends Model {
|
||||
|
||||
public function get(int $userId) {
|
||||
|
||||
try {
|
||||
|
||||
$query = $this->_db->prepare("SELECT * FROM `avatar`
|
||||
WHERE `userId` = ?
|
||||
|
||||
ORDER BY `seq` DESC
|
||||
LIMIT 1");
|
||||
|
||||
$query->execute([$userId]);
|
||||
|
||||
return $query->fetch();
|
||||
|
||||
} catch (PDOException $e) {
|
||||
|
||||
trigger_error($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function versionExists(int $userId, int $blockId, int $seq) {
|
||||
|
||||
try {
|
||||
|
||||
$query = $this->_db->prepare("SELECT COUNT(*) AS `total` FROM `avatar`
|
||||
WHERE `userId` = ? AND `blockId` = ? AND `seq` = ?");
|
||||
|
||||
$query->execute([$userId, $blockId, $seq]);
|
||||
|
||||
return $query->fetch()['total'];
|
||||
|
||||
} catch (PDOException $e) {
|
||||
|
||||
trigger_error($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function add(int $userId,
|
||||
int $blockId,
|
||||
int $seq,
|
||||
int $time,
|
||||
string $data) {
|
||||
|
||||
try {
|
||||
|
||||
$query = $this->_db->prepare("INSERT INTO `avatar` SET `userId` = ?,
|
||||
`blockId` = ?,
|
||||
`seq` = ?,
|
||||
`time` = ?,
|
||||
`data` = ?");
|
||||
|
||||
$query->execute([$userId,
|
||||
$blockId,
|
||||
$seq,
|
||||
$time,
|
||||
$data]);
|
||||
|
||||
return $this->_db->lastInsertId();
|
||||
|
||||
} catch (PDOException $e) {
|
||||
|
||||
trigger_error($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
@ -7,6 +7,7 @@ require(PROJECT_DIR . '/application/model/model.php');
|
||||
require(PROJECT_DIR . '/application/model/block.php');
|
||||
require(PROJECT_DIR . '/application/model/user.php');
|
||||
require(PROJECT_DIR . '/application/model/profile.php');
|
||||
require(PROJECT_DIR . '/application/model/avatar.php');
|
||||
|
||||
require(PROJECT_DIR . '/system/curl.php');
|
||||
require(PROJECT_DIR . '/system/twister.php');
|
||||
@ -44,6 +45,14 @@ $_modelProfile = new ModelProfile(
|
||||
DB_PASSWORD
|
||||
);
|
||||
|
||||
$_modelAvatar = new ModelAvatar(
|
||||
DB_DATABASE,
|
||||
DB_HOST,
|
||||
DB_PORT,
|
||||
DB_USER,
|
||||
DB_PASSWORD
|
||||
);
|
||||
|
||||
$_modelBlock = new ModelBlock(
|
||||
DB_DATABASE,
|
||||
DB_HOST,
|
||||
@ -104,6 +113,9 @@ if (isset($_GET['_route_'])) {
|
||||
case 'api/user/profile':
|
||||
require(PROJECT_DIR . '/application/controller/api/user/profile.php');
|
||||
break;
|
||||
case 'api/user/avatar':
|
||||
require(PROJECT_DIR . '/application/controller/api/user/avatar.php');
|
||||
break;
|
||||
|
||||
// Multi-attribute pages
|
||||
default:
|
||||
|
@ -23,6 +23,8 @@
|
||||
.moduleFeed .item .avatar img {
|
||||
border-radius: 50%;
|
||||
border: 2px #fff solid;
|
||||
width: 46px;
|
||||
height: 46px;
|
||||
}
|
||||
|
||||
.moduleFeed .item .message {
|
||||
|
@ -15,6 +15,8 @@
|
||||
.modulePost .avatar img {
|
||||
border-radius: 50%;
|
||||
border: 2px #fff solid;
|
||||
width: 46px;
|
||||
height: 46px;
|
||||
}
|
||||
|
||||
.modulePost .message {
|
||||
|
@ -2,6 +2,7 @@ $(document).ready(function() {
|
||||
|
||||
// Init modules
|
||||
ModuleMenu.init('/');
|
||||
ModulePost.init('#modulePost');
|
||||
ModuleFeed.load('#moduleFeed', true);
|
||||
//ModuleUsers.load('#moduleUsers', true);
|
||||
|
||||
|
@ -28,7 +28,8 @@ var ModuleFeed = {
|
||||
}
|
||||
$(feed).append(
|
||||
$('<div/>', {
|
||||
'class': 'item'
|
||||
'class': 'item',
|
||||
'data-username': userName
|
||||
}).append(
|
||||
$('<div/>', {
|
||||
'class': 'avatar'
|
||||
@ -70,6 +71,32 @@ var ModuleFeed = {
|
||||
}
|
||||
}
|
||||
},
|
||||
loadAvatar: function(feed, userName) {
|
||||
$.ajax({
|
||||
url: 'api/user/avatar',
|
||||
type: 'POST',
|
||||
data: {
|
||||
userName: userName
|
||||
},
|
||||
success: function (response) {
|
||||
|
||||
if (response.success) {
|
||||
|
||||
if (response.avatar) {
|
||||
$(feed).find('div[data-username="' + userName + '"] .avatar img').attr('src', response.avatar);
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
console.log(response.message);
|
||||
|
||||
}
|
||||
},
|
||||
error: function(jqXHR, textStatus, errorThrown) {
|
||||
console.log(textStatus, errorThrown);
|
||||
}
|
||||
});
|
||||
},
|
||||
load: function(feed, reFresh) {
|
||||
$.ajax({
|
||||
url: 'api/post/get',
|
||||
@ -86,6 +113,7 @@ var ModuleFeed = {
|
||||
|
||||
$(response.posts).each(function() {
|
||||
ModuleFeed.template.feed.item.append(feed, this.userName, this.time, this.message, this.reTwist);
|
||||
ModuleFeed.loadAvatar(feed, this.userName);
|
||||
});
|
||||
|
||||
} else {
|
||||
|
@ -98,6 +98,32 @@ var ModuleFollowing = {
|
||||
}
|
||||
});
|
||||
},
|
||||
loadAvatar: function(list, userName) {
|
||||
$.ajax({
|
||||
url: 'api/user/avatar',
|
||||
type: 'POST',
|
||||
data: {
|
||||
userName: userName
|
||||
},
|
||||
success: function (response) {
|
||||
|
||||
if (response.success) {
|
||||
|
||||
if (response.avatar) {
|
||||
$(list).find('div[data-username="' + userName + '"] .avatar img').attr('src', response.avatar);
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
console.log(response.message);
|
||||
|
||||
}
|
||||
},
|
||||
error: function(jqXHR, textStatus, errorThrown) {
|
||||
console.log(textStatus, errorThrown);
|
||||
}
|
||||
});
|
||||
},
|
||||
load: function(list, reFresh) {
|
||||
$.ajax({
|
||||
url: 'api/follow/get',
|
||||
@ -112,6 +138,7 @@ var ModuleFollowing = {
|
||||
|
||||
$(response.users).each(function() {
|
||||
ModuleFollowing.template.list.item.append(list, this.userName);
|
||||
ModuleFollowing.loadAvatar(list, this.userName);
|
||||
ModuleFollowing.loadProfile(list, this.userName);
|
||||
});
|
||||
|
||||
|
@ -1,7 +1,33 @@
|
||||
var ModulePost = {
|
||||
init: function(element) {
|
||||
ModulePost.loadAvatar(element);
|
||||
},
|
||||
loadAvatar: function(element) {
|
||||
$.ajax({
|
||||
url: 'api/user/avatar',
|
||||
type: 'POST',
|
||||
success: function (response) {
|
||||
|
||||
if (response.success) {
|
||||
|
||||
if (response.avatar) {
|
||||
$(element).find('img').attr('src', response.avatar);
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
console.log(response.message);
|
||||
|
||||
}
|
||||
},
|
||||
error: function(jqXHR, textStatus, errorThrown) {
|
||||
console.log(textStatus, errorThrown);
|
||||
}
|
||||
});
|
||||
},
|
||||
add: function() {
|
||||
|
||||
var input = $('#modulePost textarea');
|
||||
var input = $('#modulePost > .message > textarea');
|
||||
|
||||
$.ajax({
|
||||
url: 'api/post/add',
|
||||
|
Loading…
x
Reference in New Issue
Block a user