Commit 97c5bbf5 authored by Mark Harding's avatar Mark Harding
Browse files

(feat): remove messenger plugin

parent 90246a9d
<?php
/**
* Minds Newsfeed API
*
* @version 1
* @author Mark Harding
*/
namespace Minds\Plugin\Messenger\Controllers\api\v1;
use Minds\Core;
use Minds\Core\Session;
use Minds\Core\Security;
use Minds\Entities;
use Minds\Helpers;
use Minds\Plugin\Messenger;
use Minds\Interfaces;
use Minds\Api\Factory;
use Minds\Core\Sockets;
class conversations implements Interfaces\Api
{
/**
* Returns the conversations or conversation
* @param array $pages
*
* API:: /v1/conversations
*/
public function get($pages)
{
Factory::isLoggedIn();
$response = [];
if (isset($pages[0])) {
$response = $this->getConversation($pages);
} else {
$response = $this->getList();
}
return Factory::response($response);
}
private function getConversation($pages)
{
$response = [];
$me = Core\Session::getLoggedInUser();
$conversation = (new Messenger\Entities\Conversation())
->setParticipant($me->guid)
->setParticipant($pages[0]);
$messages = (new Messenger\Core\Messages)
->setConversation($conversation);
if ($conversation) {
$response = $conversation->export();
}
$limit = isset($_GET['limit']) ? $_GET['limit'] : 6;
$offset = isset($_GET['offset']) ? $_GET['offset'] : "";
$finish = isset($_GET['start']) ? $_GET['start'] : "";
$messages = $messages->getMessages($limit, $offset, $finish);
if ($messages) {
foreach ($messages as $k => $message) {
$messages[$k]->message = $messages[$k]->getMessage(Core\Session::getLoggedInUserGuid());
}
$conversation->ts = time();
$conversation->markAsRead(Session::getLoggedInUserGuid());
$messages = array_reverse($messages);
if ($offset || $finish) {
array_shift($messages);
}
$response['messages'] = Factory::exportable($messages);
foreach ($response['messages'] as $k => $message) {
$response['messages'][$k]['ownerObj'] = $message['owner'];
$response['messages'][$k]['owner_guid'] = $message['owner']['guid'];
}
$response['load-next'] = (string) end($messages)->guid ?: $finish;
$response['load-previous'] = (string) reset($messages)->guid ?: $offset;
}
$keystore = new Messenger\Core\Keystore();
//return the public keys
$response['publickeys'] = [];
foreach ($conversation->getParticipants() as $participant_guid) {
if ($participant_guid == Session::getLoggedInUserGuid()) {
$response['publickeys'][(string) $participant_guid] = $keystore->setUser(Session::getLoggedInUser())->getPublicKey();
continue;
}
$response['publickeys'][(string) $participant_guid] = $keystore->setUser($participant_guid)->getPublicKey();
}
return $response;
}
private function getList()
{
$response = [];
$conversations = (new Messenger\Core\Conversations)->getList(12, $_GET['offset']);
if ($conversations) {
$response['conversations'] = Factory::exportable($conversations);
//mobile polyfill
foreach ($response['conversations'] as $k => $v) {
$guids = array_values(array_diff(explode(':', $v['guid']), [Core\Session::getLoggedInUserGuid()]));
var_dump($guids);
if ($guids[0]) {
$response['conversations'][$k]['guid'] = $guids[0];
}
$response['conversations'][$k]['subscribed'] = true;
$response['conversations'][$k]['subscriber'] = true;
}
end($conversations);
$response['load-next'] = (int) $_GET['offset'] + count($conversations);
$response['load-previous'] = (int) $_GET['offset'] - count($conversations);
}
return $response;
}
public function post($pages)
{
Factory::isLoggedIn();
$conversation = (new Messenger\Entities\Conversation());
$conversation->setParticipant(Core\Session::getLoggedInUserGuid())
->setParticipant($pages[0]);
$message = (new Messenger\Entities\Message())
->setConversation($conversation);
// Block check
foreach ($conversation->getParticipants() as $guid) {
if (Session::getLoggedInUserGuid() == $guid) {
continue;
}
if (!Security\ACL::_()->interact($guid, Session::getLoggedInUserGuid())) {
// At least one people blocked on this conversation
// Means that we cannot chat here
return Factory::response([
'unavailable' => true
]);
}
}
foreach ($conversation->getParticipants() as $guid) {
$key = "message:$guid";
$messages[$guid] = base64_encode(base64_decode(rawurldecode($_POST[$key]))); //odd bug sometimes with device base64..
}
$message->setMessages($messages, true);
$message->message = $messages[Session::getLoggedInUserGuid()];
$message->save();
$conversation->markAsUnread(Session::getLoggedInUserGuid());
$conversation->markAsRead(Session::getLoggedInUserGuid());
$response["message"] = $message->export();
$response["message"]['owner_guid'] = Session::getLoggedInUserGuid();
$emit = $response['message'];
unset($emit['message']);
$emit['tabId'] = isset($_POST['tabId']) ? $_POST['tabId'] : null;
try {
(new Sockets\Events())
->setRoom($conversation->buildSocketRoomName())
->emit('pushConversationMessage', (string) $conversation->getGuid(), $emit);
} catch (\Exception $e) { /* TODO: To log or not to log */
}
foreach ($conversation->getParticipants() as $participant) {
if ($participant == Session::getLoggedInUserGuid()) {
continue;
}
Core\Queue\Client::build()->setExchange("mindsqueue")
->setQueue("Push")
->send([
"user_guid"=>$participant,
"message"=>"You have a new message.",
"uri" => 'chat'
]);
}
$this->emitSocketTouch($conversation);
return Factory::response($response);
}
public function put($pages)
{
Factory::isLoggedIn();
if (!Security\ACL::_()->interact($pages[1], Session::getLoggedInUserGuid())) {
return Factory::response([
'unavailable' => true
]);
}
switch ($pages[0]) {
case 'call':
\Minds\Core\Queue\Client::build()->setExchange("mindsqueue")
->setQueue("Push")
->send(array(
"user_guid"=>$pages[1],
"message"=> \Minds\Core\Session::getLoggedInUser()->name . " is calling you.",
"uri" => 'call',
"sound" => 'ringing-1.m4a',
"json" => json_encode(array(
"from_guid"=>\Minds\Core\Session::getLoggedInUser()->guid,
"from_name"=>\Minds\Core\Session::getLoggedInUser()->name
))
));
break;
case 'no-answer':
//leave a notification
$conversation = new entities\conversation(elgg_get_logged_in_user_guid(), $pages[1]);
$message = new entities\CallMissed($conversation);
$message->save();
$conversation->update();
Core\Queue\Client::build()->setExchange("mindsqueue")
->setQueue("Push")
->send(array(
"user_guid"=>$pages[1],
"message"=> \Minds\Core\Session::getLoggedInUser()->name . " tried to call you.",
"uri" => 'chat',
));
break;
case 'ended':
$conversation = new entities\conversation(elgg_get_logged_in_user_guid(), $pages[1]);
$message = new entities\CallEnded($conversation);
$message->save();
break;
}
return Factory::response(array());
}
public function delete($pages)
{
Factory::isLoggedIn();
$response = [];
return Factory::response($response);
}
private function emitSocketTouch($conversation)
{
if ($conversation->getParticipants()) {
$users = [];
foreach ($conversation->getParticipants() as $guid) {
if ($guid == Core\Session::getLoggedInUserGuid()) {
continue;
}
$users[] = $guid;
}
if (!$users) {
return;
}
try {
(new Sockets\Events())
->setUsers($users)
->emit('touchConversation', (string) $conversation->getGuid());
} catch (\Exception $e) { /* TODO: To log or not to log */
}
}
}
}
<?php
/**
* Minds Newsfeed API
*
* @version 1
* @author Mark Harding
*/
namespace Minds\Plugin\Messenger\Controllers\api\v1;
use Minds\Core;
use Minds\Interfaces;
use Minds\Api\Factory;
use Minds\Plugin\Messenger;
class keys implements Interfaces\Api
{
/**
* Returns the private key belonging to a user
* @param array $pages
*
* API:: /v1/keys
*/
public function get($pages)
{
$response = [];
$keystore = new Messenger\Core\Keystore();
$keystore->setUser(Core\Session::getLoggedInUser());
// $_SESSION['user'] = new \Minds\Entities\User($_SESSION['user']->guid, false);
$unlock_password = get_input('password');
$new_password = get_input('new_password');
$pub = $keystore->getPublicKey();
$priv = $keystore->getPrivateKey();
//legacy password update
//if(helpers\openssl::verify($pub, $priv, $unlock_password) === FALSE){ //hint: this should fail if pswd is set
// $priv = helpers\openssl::temporaryPrivateKey($priv, $unlock_password, $unlock_password);
// \elgg_set_plugin_user_setting('privatekey', $priv, elgg_get_logged_in_user_guid(), 'gatherings');
//}
try {
$keystore->unlockPrivateKey($unlock_password, null);
$tmp = $keystore->getUnlockedPrivateKey();
} catch (\Exception $e) {
$response['status'] = 'error';
$response['message'] = "please check your password";
}
if (!$tmp || !$unlock_password) {
$response['status'] = 'error';
$response['message'] = "please check your password";
} else {
$response['key'] = $tmp;
}
return Factory::response($response);
}
public function post($pages)
{
$openssl = new Messenger\Core\Encryption\OpenSSL();
$keystore = (new Messenger\Core\Keystore($openssl))
->setUser(Core\Session::getLoggedInUser());
$response = [];
switch ($pages[0]) {
case "setup":
$response = array();
$keypair = $openssl->generateKeypair($_POST['password']);
$keystore->setPublicKey($keypair['public'])
->setPrivateKey($keypair['private'])
->save();
if (!isset($_POST['download']) || $_POST['download']) {
$keystore->unlockPrivateKey($_POST['password'], null);
$tmp = $keystore->getUnlockedPrivateKey();
$response['key'] = $tmp;
} else {
$unlockPassword = base64_encode(openssl_random_pseudo_bytes(128));
$keystore->unlockPrivateKey($_POST['password'], $unlockPassword);
$tmp = $keystore->getUnlockedPrivateKey();
$response['password'] = urlencode($unlockPassword);
}
break;
case "unlock":
default:
try {
$unlockPassword = base64_encode(openssl_random_pseudo_bytes(128));
$keystore->unlockPrivateKey($_POST['password'], $unlockPassword);
$tmp = $keystore->getUnlockedPrivateKey();
$response['password'] = urlencode($unlockPassword);
} catch (\Exception $e) {
$response['status'] = 'error';
$response['message'] = $e->getMessage();
$response['message'] = "please check your password";
}
//patch for legacy private key
/*if(helpers\openssl::verify($pub, $priv, $unlock_password) === FALSE){ //hint: this should fail if pswd is set
$priv = helpers\openssl::temporaryPrivateKey($priv, $unlock_password, $unlock_password);
\elgg_set_plugin_user_setting('privatekey', $priv, elgg_get_logged_in_user_guid(), 'gatherings');
}
if(!isset($_POST['download']) || (isset($_POST['download']) && $_POST['download'] === 'true')){
$tmp = helpers\openssl::temporaryPrivateKey(\elgg_get_plugin_user_setting('privatekey', elgg_get_logged_in_user_guid(), 'gatherings'), $unlock_password, NULL);
$response['key'] = $tmp;
} else {
$new_pswd = base64_encode(openssl_random_pseudo_bytes(128));
$tmp = helpers\openssl::temporaryPrivateKey($priv, $unlock_password, $new_pswd);
$_SESSION['tmp_privatekey'] = $tmp;
$response['password'] = $new_pswd;
}*/
}
return Factory::response($response);
}
public function put($pages)
{
return Factory::response(array());
}
public function delete($pages)
{
return Factory::response(array());
}
}
<?php
/**
* Minds Messenger Search
*
* @version 1
* @author Mark Harding
*/
namespace Minds\Plugin\Messenger\Controllers\api\v1;
use Minds\Core;
use Minds\Entities;
use Minds\Helpers;
use Minds\Plugin\Messenger;
use Minds\Interfaces;
use Minds\Api\Factory;
class search implements Interfaces\Api
{
/**
* Returns users and conversation guids
* @param array $pages
*
* API:: /v1/conversations/search
*/
public function get($pages)
{
Factory::isLoggedIn();
$response = [];
if (!isset($_GET['q']) || !$_GET['q']) {
return Factory::response([
'status' => 'error',
'message' => 'Missing query'
]);
}
$params = [
'limit' => isset($_GET['limit']) ? $_GET['limit'] : 24,
'type' => 'user'
];
$_GET['q'] = "({$_GET['q']})^5 OR (*{$_GET['q']}*)";
$guids = (new Core\Search\Documents())->query($_GET['q'], $params);
$response = [];
if ($guids) {
$users = Core\Entities::get([
'guids' => $guids
]);
$conversations = [];
foreach ($users as $user) {
$conversations[] = (new Messenger\Entities\Conversation())
->setParticipant($user->guid)
->setParticipant(Core\Session::getLoggedInUserGuid());
}
$response['conversations'] = Factory::exportable($conversations);
$response['load-next'] = (int) $_GET['offset'] + $_GET['limit'] + 1;
}
return Factory::response($response);
}
public function post($pages)
{
return Factory::response([]);
}
public function put($pages)
{
return Factory::response([]);
}
public function delete($pages)
{
return Factory::response([]);
}
}
<?php
/**
* Minds Newsfeed API
*
* @version 1
* @author Mark Harding
*/
namespace Minds\Plugin\Messenger\Controllers\api\v2;
use Minds\Core;
use Minds\Core\Session;
use Minds\Core\Security;
use Minds\Entities;
use Minds\Helpers;
use Minds\Plugin\Messenger;
use Minds\Interfaces;
use Minds\Api\Factory;
use Minds\Core\Sockets;
class conversations implements Interfaces\Api
{
/**
* Returns the conversations or conversation
* @param array $pages
*
* API:: /v1/conversations
*/
public function get($pages)
{
Factory::isLoggedIn();
$response = [];
if (isset($pages[0])) {
$response = $this->getConversation($pages);
} else {
$response = $this->getList();
}
return Factory::response($response);
}
private function getConversation($pages)
{
$response = [];
$me = Core\Session::getLoggedInUser();
$conversation = (new Messenger\Entities\Conversation())
->loadFromGuid($pages[0]);
$messages = (new Messenger\Core\Messages)
->setConversation($conversation);
if ($conversation) {
$response = $conversation->export();
$blocked = false;
$unavailable = false;
if (is_array($response['participants']) && false) {
foreach ($response['participants'] as $participant) {
if (!Security\ACL::_()->interact(Core\Session::getLoggedInUser(), $participant['guid'])) {
$blocked = true;
break;
}
}
if (!$blocked) {
foreach ($response['participants'] as $participant) {
if (!Security\ACL::_()->interact($participant['guid'], Core\Session::getLoggedInUser())) {
$unavailable = true;
break;
}
}
}
}