@@ -106,7 +106,7 @@ func TestBackfillingHistory(t *testing.T) {
106106			backfillRes  :=  backfillBatchHistoricalMessages (
107107				t ,
108108				as ,
109- 				virtualUserID ,
109+ 				[] string { virtualUserID } ,
110110				roomID ,
111111				eventIdBefore ,
112112				timeAfterEventBefore .Add (timeBetweenMessages * 3 ),
@@ -123,7 +123,7 @@ func TestBackfillingHistory(t *testing.T) {
123123			backfillRes2  :=  backfillBatchHistoricalMessages (
124124				t ,
125125				as ,
126- 				virtualUserID ,
126+ 				[] string { virtualUserID } ,
127127				roomID ,
128128				eventIdBefore ,
129129				timeAfterEventBefore ,
@@ -184,6 +184,48 @@ func TestBackfillingHistory(t *testing.T) {
184184			}
185185		})
186186
187+ 		t .Run ("Backfilled historical events from multiple users in the same chunk" , func (t  * testing.T ) {
188+ 			t .Parallel ()
189+ 
190+ 			roomID  :=  as .CreateRoom (t , struct {}{})
191+ 			alice .JoinRoom (t , roomID , nil )
192+ 
193+ 			// Create the "live" event we are going to insert our backfilled events next to 
194+ 			eventIDsBefore  :=  createMessagesInRoom (t , alice , roomID , 1 )
195+ 			eventIdBefore  :=  eventIDsBefore [0 ]
196+ 			timeAfterEventBefore  :=  time .Now ()
197+ 
198+ 			// Insert a backfilled event 
199+ 			virtualUserID2  :=  "@ricky:hs1" 
200+ 			virtualUserID3  :=  "@carol:hs1" 
201+ 			backfillRes  :=  backfillBatchHistoricalMessages (
202+ 				t ,
203+ 				as ,
204+ 				[]string {virtualUserID , virtualUserID2 , virtualUserID3 },
205+ 				roomID ,
206+ 				eventIdBefore ,
207+ 				timeAfterEventBefore ,
208+ 				"" ,
209+ 				3 ,
210+ 				// Status 
211+ 				200 ,
212+ 			)
213+ 			historicalEventIDs  :=  getEventsFromBatchSendResponse (t , backfillRes )
214+ 
215+ 			messagesRes  :=  alice .MustDoFunc (t , "GET" , []string {"_matrix" , "client" , "r0" , "rooms" , roomID , "messages" }, client .WithContentType ("application/json" ), client .WithQueries (url.Values {
216+ 				"dir" :   []string {"b" },
217+ 				"limit" : []string {"100" },
218+ 			}))
219+ 
220+ 			must .MatchResponse (t , messagesRes , match.HTTPResponse {
221+ 				JSON : []match.JSON {
222+ 					match .JSONCheckOffAllowUnwanted ("chunk" , []interface {}{historicalEventIDs [0 ], historicalEventIDs [1 ], historicalEventIDs [2 ]}, func (r  gjson.Result ) interface {} {
223+ 						return  r .Get ("event_id" ).Str 
224+ 					}, nil ),
225+ 				},
226+ 			})
227+ 		})
228+ 
187229		t .Run ("Backfilled historical events with m.historical do not come down in an incremental sync" , func (t  * testing.T ) {
188230			t .Parallel ()
189231
@@ -202,7 +244,7 @@ func TestBackfillingHistory(t *testing.T) {
202244			backfillRes  :=  backfillBatchHistoricalMessages (
203245				t ,
204246				as ,
205- 				virtualUserID ,
247+ 				[] string { virtualUserID } ,
206248				roomID ,
207249				eventIdBefore ,
208250				timeAfterEventBefore ,
@@ -238,7 +280,7 @@ func TestBackfillingHistory(t *testing.T) {
238280			backfillBatchHistoricalMessages (
239281				t ,
240282				as ,
241- 				virtualUserID ,
283+ 				[] string { virtualUserID } ,
242284				roomID ,
243285				"$some-non-existant-event-id" ,
244286				time .Now (),
@@ -265,7 +307,7 @@ func TestBackfillingHistory(t *testing.T) {
265307			backfillBatchHistoricalMessages (
266308				t ,
267309				alice ,
268- 				virtualUserID ,
310+ 				[] string { virtualUserID } ,
269311				roomID ,
270312				eventIdBefore ,
271313				timeAfterEventBefore ,
@@ -302,7 +344,7 @@ func TestBackfillingHistory(t *testing.T) {
302344			backfillRes  :=  backfillBatchHistoricalMessages (
303345				t ,
304346				as ,
305- 				virtualUserID ,
347+ 				[] string { virtualUserID } ,
306348				roomID ,
307349				eventIdBefore ,
308350				timeAfterEventBefore ,
@@ -360,7 +402,7 @@ func TestBackfillingHistory(t *testing.T) {
360402			backfillRes  :=  backfillBatchHistoricalMessages (
361403				t ,
362404				as ,
363- 				virtualUserID ,
405+ 				[] string { virtualUserID } ,
364406				roomID ,
365407				eventIdBefore ,
366408				timeAfterEventBefore ,
@@ -416,7 +458,7 @@ func TestBackfillingHistory(t *testing.T) {
416458			backfillRes  :=  backfillBatchHistoricalMessages (
417459				t ,
418460				as ,
419- 				virtualUserID ,
461+ 				[] string { virtualUserID } ,
420462				roomID ,
421463				eventIdBefore ,
422464				timeAfterEventBefore ,
@@ -521,7 +563,7 @@ var chunkCount int64 = 0
521563func  backfillBatchHistoricalMessages (
522564	t  * testing.T ,
523565	c  * client.CSAPI ,
524- 	virtualUserID   string ,
566+ 	virtualUserIDs  [] string ,
525567	roomID  string ,
526568	insertAfterEventId  string ,
527569	insertTime  time.Time ,
@@ -536,6 +578,8 @@ func backfillBatchHistoricalMessages(
536578
537579	evs  :=  make ([]map [string ]interface {}, count )
538580	for  i  :=  0 ; i  <  len (evs ); i ++  {
581+ 		virtualUserID  :=  virtualUserIDs [i % len (virtualUserIDs )]
582+ 
539583		newEvent  :=  map [string ]interface {}{
540584			"type" :             "m.room.message" ,
541585			"sender" :           virtualUserID ,
@@ -550,19 +594,23 @@ func backfillBatchHistoricalMessages(
550594		evs [i ] =  newEvent 
551595	}
552596
553- 	joinEvent  :=  map [string ]interface {}{
554- 		"type" :             "m.room.member" ,
555- 		"sender" :           virtualUserID ,
556- 		"origin_server_ts" : insertOriginServerTs ,
557- 		"content" : map [string ]interface {}{
558- 			"membership" : "join" ,
559- 		},
560- 		"state_key" : virtualUserID ,
597+ 	state_evs  :=  make ([]map [string ]interface {}, len (virtualUserIDs ))
598+ 	for  i , virtualUserID  :=  range  virtualUserIDs  {
599+ 		joinEvent  :=  map [string ]interface {}{
600+ 			"type" :             "m.room.member" ,
601+ 			"sender" :           virtualUserID ,
602+ 			"origin_server_ts" : insertOriginServerTs ,
603+ 			"content" : map [string ]interface {}{
604+ 				"membership" : "join" ,
605+ 			},
606+ 			"state_key" : virtualUserID ,
607+ 		}
608+ 
609+ 		state_evs [i ] =  joinEvent 
561610	}
562611
563612	query  :=  make (url.Values , 2 )
564613	query .Add ("prev_event" , insertAfterEventId )
565- 	query .Add ("user_id" , virtualUserID )
566614	// If provided, connect the chunk to the last insertion point 
567615	if  chunkID  !=  ""  {
568616		query .Add ("chunk_id" , chunkID )
@@ -574,7 +622,7 @@ func backfillBatchHistoricalMessages(
574622		[]string {"_matrix" , "client" , "unstable" , "org.matrix.msc2716" , "rooms" , roomID , "batch_send" },
575623		client .WithJSONBody (t , map [string ]interface {}{
576624			"events" :                evs ,
577- 			"state_events_at_start" : [] map [ string ] interface {}{ joinEvent } ,
625+ 			"state_events_at_start" : state_evs ,
578626		}),
579627		client .WithContentType ("application/json" ),
580628		client .WithQueries (query ),
0 commit comments