Browse Source

implement profile avatars receiving from DHT

main
ghost 3 years ago
parent
commit
3e3853125d
  1. BIN
      database/cloud-server.mwb
  2. 72
      src/application/controller/api/user/avatar.php
  3. 72
      src/application/model/avatar.php
  4. 12
      src/bootstrap.php
  5. 2
      src/public/css/template/default/module/feed.css
  6. 2
      src/public/css/template/default/module/post.css
  7. 1
      src/public/js/home.js
  8. 30
      src/public/js/module/feed.js
  9. 27
      src/public/js/module/following.js
  10. 28
      src/public/js/module/post.js

BIN
database/cloud-server.mwb

Binary file not shown.

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

@ -0,0 +1,72 @@ @@ -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

@ -0,0 +1,72 @@ @@ -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;
}
}
}

12
src/bootstrap.php

@ -7,6 +7,7 @@ require(PROJECT_DIR . '/application/model/model.php'); @@ -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( @@ -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_'])) { @@ -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:

2
src/public/css/template/default/module/feed.css

@ -23,6 +23,8 @@ @@ -23,6 +23,8 @@
.moduleFeed .item .avatar img {
border-radius: 50%;
border: 2px #fff solid;
width: 46px;
height: 46px;
}
.moduleFeed .item .message {

2
src/public/css/template/default/module/post.css

@ -15,6 +15,8 @@ @@ -15,6 +15,8 @@
.modulePost .avatar img {
border-radius: 50%;
border: 2px #fff solid;
width: 46px;
height: 46px;
}
.modulePost .message {

1
src/public/js/home.js

@ -2,6 +2,7 @@ $(document).ready(function() { @@ -2,6 +2,7 @@ $(document).ready(function() {
// Init modules
ModuleMenu.init('/');
ModulePost.init('#modulePost');
ModuleFeed.load('#moduleFeed', true);
//ModuleUsers.load('#moduleUsers', true);

30
src/public/js/module/feed.js

@ -28,7 +28,8 @@ var ModuleFeed = { @@ -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 = { @@ -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 = { @@ -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 {

27
src/public/js/module/following.js

@ -98,6 +98,32 @@ var ModuleFollowing = { @@ -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 = { @@ -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);
});

28
src/public/js/module/post.js

@ -1,7 +1,33 @@ @@ -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…
Cancel
Save