core (xmpp): set message timestamp on message reception:

message timestamp was set during the parsing, but this could lead to misordering as async
calls are done before message is parsed (so a variable delay could happen before the
timestamp was set). This patch workaround this by setting timestamp as soon as message is
received (in onMessage).
This commit is contained in:
Goffi 2019-07-02 19:36:15 +02:00
commit c18b828940

View file

@ -17,6 +17,10 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
import time
import calendar
import uuid
from functools import partial
from sat.core.i18n import _
from sat.core.constants import Const as C
@ -37,10 +41,6 @@ from sat.memory import encryption
from sat.memory import persistent
from sat.tools import xml_tools
from zope.interface import implements
import time
import calendar
import uuid
import sys
log = getLogger(__name__)
@ -1013,14 +1013,23 @@ class SatMessageProtocol(xmppim.MessageProtocol):
subject[e.getAttribute((C.NS_XML, "lang"), "")] = unicode(e)
# delay and timestamp
try:
received_timestamp = message_elt._received_timestamp
except AttributeError:
# message_elt._received_timestamp should have been set in onMessage
# but if parseMessage is called directly, it can be missing
log.debug(u"missing received timestamp for {message_elt}".format(
message_elt=message_elt))
received_timestamp = time.time()
try:
delay_elt = message_elt.elements(delay.NS_DELAY, "delay").next()
except StopIteration:
data["timestamp"] = time.time()
data["timestamp"] = received_timestamp
else:
parsed_delay = delay.Delay.fromElement(delay_elt)
data["timestamp"] = calendar.timegm(parsed_delay.stamp.utctimetuple())
data["received_timestamp"] = unicode(time.time())
data["received_timestamp"] = received_timestamp
if parsed_delay.sender:
data["delay_sender"] = parsed_delay.sender.full()
@ -1047,6 +1056,7 @@ class SatMessageProtocol(xmppim.MessageProtocol):
def onMessage(self, message_elt):
# TODO: handle threads
message_elt._received_timestamp = time.time()
client = self.parent
if not "from" in message_elt.attributes:
message_elt["from"] = client.jid.host
@ -1079,7 +1089,7 @@ class SatMessageProtocol(xmppim.MessageProtocol):
def bridgeSignal(self, __, data):
try:
data["extra"]["received_timestamp"] = data["received_timestamp"]
data["extra"]["received_timestamp"] = unicode(data["received_timestamp"])
data["extra"]["delay_sender"] = data["delay_sender"]
except KeyError:
pass