mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	submessages: Add update_message() function.
We want to update message.submessages for new events, even though our couple of widgets (poll/tictactoe) that can process "update" events currently just apply events as "deltas" to their current data. This does fix a subtle issue where you may get incoming events for a message that the client knows about but which it hasn't yet activated as a widget. Up until now, we've rarely seen the bug that's fixed here, since it's usually the case that as soon as we receive a message, we widgetize it right away.
This commit is contained in:
		@@ -2,6 +2,7 @@ zrequire('submessage');
 | 
			
		||||
 | 
			
		||||
set_global('channel', {});
 | 
			
		||||
set_global('widgetize', {});
 | 
			
		||||
set_global('message_store', {});
 | 
			
		||||
 | 
			
		||||
run_test('get_message_events', () => {
 | 
			
		||||
    var msg = {};
 | 
			
		||||
@@ -64,6 +65,7 @@ run_test('handle_event', () => {
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    const event = {
 | 
			
		||||
        id: 11,
 | 
			
		||||
        msg_type: 'widget',
 | 
			
		||||
        sender_id: 99,
 | 
			
		||||
        message_id: message.id,
 | 
			
		||||
@@ -75,6 +77,11 @@ run_test('handle_event', () => {
 | 
			
		||||
        args = opts;
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    message_store.get = (msg_id) => {
 | 
			
		||||
        assert.equal(msg_id, message.id);
 | 
			
		||||
        return message;
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    submessage.handle_event(event);
 | 
			
		||||
 | 
			
		||||
    assert.deepEqual(args, {
 | 
			
		||||
@@ -82,4 +89,6 @@ run_test('handle_event', () => {
 | 
			
		||||
        message_id: 42,
 | 
			
		||||
        data: 'some_data',
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    assert.deepEqual(message.submessages[0], event);
 | 
			
		||||
});
 | 
			
		||||
 
 | 
			
		||||
@@ -81,7 +81,39 @@ exports.do_process_submessages = function (in_opts) {
 | 
			
		||||
    });
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
exports.update_message = function (submsg) {
 | 
			
		||||
    var message = message_store.get(submsg.message_id);
 | 
			
		||||
 | 
			
		||||
    if (message === undefined) {
 | 
			
		||||
        // This is generally not a problem--the server
 | 
			
		||||
        // can send us events without us having received
 | 
			
		||||
        // the original message, since the server doesn't
 | 
			
		||||
        // track that.
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    var existing = _.find(message.submessages, function (sm) {
 | 
			
		||||
        return sm.id === submsg.id;
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    if (existing !== undefined) {
 | 
			
		||||
        blueslip.warn("Got submessage multiple times: " + submsg.id);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (message.submessages === undefined) {
 | 
			
		||||
        message.submessages = [];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    message.submessages.push(submsg);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
exports.handle_event = function (submsg) {
 | 
			
		||||
    // Update message.submessages in case we haven't actually
 | 
			
		||||
    // activated the widget yet, so that when the message does
 | 
			
		||||
    // come in view, the data will be complete.
 | 
			
		||||
    exports.update_message(submsg);
 | 
			
		||||
 | 
			
		||||
    // Right now, our only use of submessages is widgets.
 | 
			
		||||
    var msg_type = submsg.msg_type;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user