1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the MilioooMessageBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Michiel boeckaert <[email protected]> |
7
|
|
|
* This source file is subject to the MIT license that is bundled |
8
|
|
|
* with this source code in the file LICENSE. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Miliooo\MessagingBundle\Twig\Extension; |
12
|
|
|
|
13
|
|
|
use Miliooo\Messaging\User\ParticipantProviderInterface; |
14
|
|
|
use Miliooo\Messaging\Model\MessageInterface; |
15
|
|
|
use Miliooo\Messaging\Model\MessageMetaInterface; |
16
|
|
|
use Miliooo\Messaging\Model\ThreadInterface; |
17
|
|
|
use Miliooo\Messaging\Notifications\UnreadMessagesProviderInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Twig extension class |
21
|
|
|
* |
22
|
|
|
* There is a problem with using the securityToken directly here since it does not get populated in time. |
23
|
|
|
* http://stackoverflow.com/questions/18770467/ |
24
|
|
|
* |
25
|
|
|
* @author Michiel Boeckaert <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class MessagingExtension extends \Twig_Extension |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* A participant provider instance. |
31
|
|
|
* |
32
|
|
|
* @var ParticipantProviderInterface |
33
|
|
|
*/ |
34
|
|
|
protected $participantProvider; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* An unread messages provider instance. |
38
|
|
|
* |
39
|
|
|
* @var UnreadMessagesProviderInterface |
40
|
|
|
*/ |
41
|
|
|
protected $unreadMessagesProvider; |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
public function __construct( |
45
|
|
|
ParticipantProviderInterface $participantProvider, |
46
|
|
|
UnreadMessagesProviderInterface $unreadMessagesProvider |
47
|
|
|
) { |
48
|
|
|
$this->participantProvider = $participantProvider; |
49
|
|
|
$this->unreadMessagesProvider = $unreadMessagesProvider; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Returns a list of functions to add to the existing list. |
54
|
|
|
* |
55
|
|
|
* @return array An array of functions |
56
|
|
|
*/ |
57
|
|
|
public function getFunctions() |
58
|
|
|
{ |
59
|
|
|
return [ |
60
|
|
|
new \Twig_SimpleFunction('miliooo_messaging_is_new_read', array($this, 'isMessageNewRead')), |
61
|
|
|
new \Twig_SimpleFunction('miliooo_messaging_thread_unread_count', array($this, 'getThreadUnreadCount')), |
62
|
|
|
new \Twig_SimpleFunction('miliooo_messaging_unread_messages_count', array($this, 'getUnreadMessagesCount')), |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Checks if the message needs a new read label. |
68
|
|
|
* |
69
|
|
|
* @param MessageInterface $message |
70
|
|
|
* |
71
|
|
|
* @return boolean true if it's a new read message for the logged in user |
72
|
|
|
* false if no message meta found for the logged in user |
73
|
|
|
* or not a new read message for the logged in user |
74
|
|
|
*/ |
75
|
|
|
public function isMessageNewRead(MessageInterface $message) |
76
|
|
|
{ |
77
|
|
|
$currentUser = $this->participantProvider->getAuthenticatedParticipant(); |
78
|
|
|
|
79
|
|
|
$messageMeta = $message->getMessageMetaForParticipant($currentUser); |
80
|
|
|
|
81
|
|
|
if ($messageMeta) { |
82
|
|
|
return ( |
83
|
|
|
// if this is not null this means we changed the read status, |
84
|
|
|
// Since we changed it in read it was unread before |
85
|
|
|
$messageMeta->getPreviousReadStatus() !== null |
86
|
|
|
&& |
87
|
|
|
//the current read status is already read, this is because we marked this message as read |
88
|
|
|
// just before sending it to the output. |
89
|
|
|
$messageMeta->getReadStatus() === MessageMetaInterface::READ_STATUS_READ |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
//this can happen if you let non participants (eg admin) see a thread |
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Checks how many unread messages a thread has for the logged in participant |
99
|
|
|
* |
100
|
|
|
* @param ThreadInterface $thread The thread we check |
101
|
|
|
* |
102
|
|
|
* @return integer The number of unread messages for this thread for the participant |
103
|
|
|
*/ |
104
|
|
|
public function getThreadUnreadCount(ThreadInterface $thread) |
105
|
|
|
{ |
106
|
|
|
$currentUser = $this->participantProvider->getAuthenticatedParticipant(); |
107
|
|
|
$threadMeta = $thread->getThreadMetaForParticipant($currentUser); |
108
|
|
|
|
109
|
|
|
//the current user is not part of this thread conversation let's just return zero |
110
|
|
|
if (!$threadMeta) { |
111
|
|
|
return 0; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $threadMeta->getUnreadMessageCount(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Gets the total unread messages count for the logged in user. |
119
|
|
|
* |
120
|
|
|
* @return integer The unread messages count for the logged in user |
121
|
|
|
*/ |
122
|
|
|
public function getUnreadMessagesCount() |
123
|
|
|
{ |
124
|
|
|
$currentUser = $this->participantProvider->getAuthenticatedParticipant(); |
125
|
|
|
|
126
|
|
|
return $this->unreadMessagesProvider->getUnreadMessageCountForParticipant($currentUser); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Returns the name of the extension. |
131
|
|
|
* |
132
|
|
|
* @return string The extension name |
133
|
|
|
*/ |
134
|
|
|
public function getName() |
135
|
|
|
{ |
136
|
|
|
return 'miliooo_messaging'; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|