@@ -1112,6 +1112,156 @@ describe("SlidingSync", () => {
11121112 } ) ;
11131113 } ) ;
11141114
1115+ describe ( "custom room subscriptions" , ( ) => {
1116+ beforeAll ( setupClient ) ;
1117+ afterAll ( teardownClient ) ;
1118+
1119+ const roomA = "!a" ;
1120+ const roomB = "!b" ;
1121+ const roomC = "!c" ;
1122+ const roomD = "!d" ;
1123+
1124+ const defaultSub = {
1125+ timeline_limit : 1 ,
1126+ required_state : [ [ "m.room.create" , "" ] ] ,
1127+ } ;
1128+
1129+ const customSubName1 = "sub1" ;
1130+ const customSub1 = {
1131+ timeline_limit : 2 ,
1132+ required_state : [ [ "*" , "*" ] ] ,
1133+ } ;
1134+
1135+ const customSubName2 = "sub2" ;
1136+ const customSub2 = {
1137+ timeline_limit : 3 ,
1138+ required_state : [ [ "*" , "*" ] ] ,
1139+ } ;
1140+
1141+ it ( "should be possible to use custom subscriptions on startup" , async ( ) => {
1142+ const slidingSync = new SlidingSync ( proxyBaseUrl , [ ] , defaultSub , client ! , 1 ) ;
1143+ // the intention is for clients to set this up at startup
1144+ slidingSync . addCustomSubscription ( customSubName1 , customSub1 ) ;
1145+ slidingSync . addCustomSubscription ( customSubName2 , customSub2 ) ;
1146+ // then call these depending on the kind of room / context
1147+ slidingSync . useCustomSubscription ( roomA , customSubName1 ) ;
1148+ slidingSync . useCustomSubscription ( roomB , customSubName1 ) ;
1149+ slidingSync . useCustomSubscription ( roomC , customSubName2 ) ;
1150+ slidingSync . modifyRoomSubscriptions ( new Set < string > ( [ roomA , roomB , roomC , roomD ] ) ) ;
1151+
1152+ httpBackend ! . when ( "POST" , syncUrl ) . check ( function ( req ) {
1153+ const body = req . data ;
1154+ logger . log ( "custom subs" , body ) ;
1155+ expect ( body . room_subscriptions ) . toBeTruthy ( ) ;
1156+ expect ( body . room_subscriptions [ roomA ] ) . toEqual ( customSub1 ) ;
1157+ expect ( body . room_subscriptions [ roomB ] ) . toEqual ( customSub1 ) ;
1158+ expect ( body . room_subscriptions [ roomC ] ) . toEqual ( customSub2 ) ;
1159+ expect ( body . room_subscriptions [ roomD ] ) . toEqual ( defaultSub ) ;
1160+ } ) . respond ( 200 , {
1161+ pos : "b" ,
1162+ lists : [ ] ,
1163+ extensions : { } ,
1164+ rooms : { } ,
1165+ } ) ;
1166+ slidingSync . start ( ) ;
1167+ await httpBackend ! . flushAllExpected ( ) ;
1168+ slidingSync . stop ( ) ;
1169+ } ) ;
1170+
1171+ it ( "should be possible to use custom subscriptions mid-connection" , async ( ) => {
1172+ const slidingSync = new SlidingSync ( proxyBaseUrl , [ ] , defaultSub , client ! , 1 ) ;
1173+ // the intention is for clients to set this up at startup
1174+ slidingSync . addCustomSubscription ( customSubName1 , customSub1 ) ;
1175+ slidingSync . addCustomSubscription ( customSubName2 , customSub2 ) ;
1176+ // initially no subs
1177+ httpBackend ! . when ( "POST" , syncUrl ) . check ( function ( req ) {
1178+ const body = req . data ;
1179+ logger . log ( "custom subs" , body ) ;
1180+ expect ( body . room_subscriptions ) . toBeFalsy ( ) ;
1181+ } ) . respond ( 200 , {
1182+ pos : "b" ,
1183+ lists : [ ] ,
1184+ extensions : { } ,
1185+ rooms : { } ,
1186+ } ) ;
1187+ slidingSync . start ( ) ;
1188+ await httpBackend ! . flushAllExpected ( ) ;
1189+
1190+ // now the user clicks on a room which uses the default sub
1191+ httpBackend ! . when ( "POST" , syncUrl ) . check ( function ( req ) {
1192+ const body = req . data ;
1193+ logger . log ( "custom subs" , body ) ;
1194+ expect ( body . room_subscriptions ) . toBeTruthy ( ) ;
1195+ expect ( body . room_subscriptions [ roomA ] ) . toEqual ( defaultSub ) ;
1196+ } ) . respond ( 200 , {
1197+ pos : "b" ,
1198+ lists : [ ] ,
1199+ extensions : { } ,
1200+ rooms : { } ,
1201+ } ) ;
1202+ slidingSync . modifyRoomSubscriptions ( new Set < string > ( [ roomA ] ) ) ;
1203+ await httpBackend ! . flushAllExpected ( ) ;
1204+
1205+ // now the user clicks on a room which uses a custom sub
1206+ httpBackend ! . when ( "POST" , syncUrl ) . check ( function ( req ) {
1207+ const body = req . data ;
1208+ logger . log ( "custom subs" , body ) ;
1209+ expect ( body . room_subscriptions ) . toBeTruthy ( ) ;
1210+ expect ( body . room_subscriptions [ roomB ] ) . toEqual ( customSub1 ) ;
1211+ expect ( body . unsubscribe_rooms ) . toEqual ( [ roomA ] ) ;
1212+ } ) . respond ( 200 , {
1213+ pos : "b" ,
1214+ lists : [ ] ,
1215+ extensions : { } ,
1216+ rooms : { } ,
1217+ } ) ;
1218+ slidingSync . useCustomSubscription ( roomB , customSubName1 ) ;
1219+ slidingSync . modifyRoomSubscriptions ( new Set < string > ( [ roomB ] ) ) ;
1220+ await httpBackend ! . flushAllExpected ( ) ;
1221+
1222+ // now the user uses a different sub for the same room: we don't unsub but just resend
1223+ httpBackend ! . when ( "POST" , syncUrl ) . check ( function ( req ) {
1224+ const body = req . data ;
1225+ logger . log ( "custom subs" , body ) ;
1226+ expect ( body . room_subscriptions ) . toBeTruthy ( ) ;
1227+ expect ( body . room_subscriptions [ roomB ] ) . toEqual ( customSub2 ) ;
1228+ expect ( body . unsubscribe_rooms ) . toBeFalsy ( ) ;
1229+ } ) . respond ( 200 , {
1230+ pos : "b" ,
1231+ lists : [ ] ,
1232+ extensions : { } ,
1233+ rooms : { } ,
1234+ } ) ;
1235+ slidingSync . useCustomSubscription ( roomB , customSubName2 ) ;
1236+ slidingSync . modifyRoomSubscriptions ( new Set < string > ( [ roomB ] ) ) ;
1237+ await httpBackend ! . flushAllExpected ( ) ;
1238+
1239+ slidingSync . stop ( ) ;
1240+ } ) ;
1241+
1242+ it ( "uses the default subscription for unknown subscription names" , async ( ) => {
1243+ const slidingSync = new SlidingSync ( proxyBaseUrl , [ ] , defaultSub , client ! , 1 ) ;
1244+ slidingSync . addCustomSubscription ( customSubName1 , customSub1 ) ;
1245+ slidingSync . useCustomSubscription ( roomA , "unknown name" ) ;
1246+ slidingSync . modifyRoomSubscriptions ( new Set < string > ( [ roomA ] ) ) ;
1247+
1248+ httpBackend ! . when ( "POST" , syncUrl ) . check ( function ( req ) {
1249+ const body = req . data ;
1250+ logger . log ( "custom subs" , body ) ;
1251+ expect ( body . room_subscriptions ) . toBeTruthy ( ) ;
1252+ expect ( body . room_subscriptions [ roomA ] ) . toEqual ( defaultSub ) ;
1253+ } ) . respond ( 200 , {
1254+ pos : "b" ,
1255+ lists : [ ] ,
1256+ extensions : { } ,
1257+ rooms : { } ,
1258+ } ) ;
1259+ slidingSync . start ( ) ;
1260+ await httpBackend ! . flushAllExpected ( ) ;
1261+ slidingSync . stop ( ) ;
1262+ } ) ;
1263+ } ) ;
1264+
11151265 describe ( "extensions" , ( ) => {
11161266 beforeAll ( setupClient ) ;
11171267 afterAll ( teardownClient ) ;
0 commit comments