zeg1jeux/lib/Messenger.class.php

106 lines
1.9 KiB
PHP

<?php
require_once('Conversation.class.php');
require_once('Message.class.php');
class Messenger {
private $gchange;
private $users = [];
private $conversations = [];
private $latestMessage;
public function __construct (&$gchange) {
$this->gchange = $gchange;
}
public function addMessages ($msgIn, $msgOut) {
foreach ($msgIn as $m) {
if (!isset($this->conversations[$m->pubkey])) {
$this->conversations[$m->pubkey] = new Conversation();
}
$msg = new Message($m, 'in');
$this->conversations[$msg->getPenpalPubkey()]->addMessage($msg);
$this->feedUserBase($msg->getPenpalPubkey());
}
foreach ($msgOut as $m) {
if (!isset($this->conversations[$m->pubkey])) {
$this->conversations[$m->pubkey] = new Conversation();
}
$msg = new Message($m, 'out');
$this->conversations[$m->pubkey]->addMessage($msg);
$this->feedUserBase($msg->getPenpalPubkey());
}
foreach ($this->conversations as $conv) {
$conv->sortMessages();
$this->checkIfIsLatestMessage($conv->getLastMessage());
}
}
public function checkIfIsLatestMessage ($msg) {
if (!isset($this->latestMessage)) {
$this->latestMessage = $msg;
} else {
if ($msg->getDate() > $this->getLatestMessage()->getDate()) {
$this->latestMessage = $msg;
}
}
}
public function getLatestMessage () {
return $this->latestMessage;
}
public function feedUserBase ($penpalPubkey) {
if (!isset($users[$penpalPubkey])) {
$this->users[$penpalPubkey] = $this->gchange->getUser($penpalPubkey);
}
}
public function getConversation ($penpalGchangeId) {
return $this->conversations[$penpalGchangeId];
}
public function getConversations () {
return $this->conversations;
}
public function getUser ($pubkey) {
return $this->users[$pubkey];
}
public function getLastConversation () {
return array_reverse($this->conversations)[0];
}
}