@@ -7,6 +7,7 @@ import '../api/exception.dart';
77import '../api/model/model.dart' ;
88import '../api/model/narrow.dart' ;
99import '../api/route/messages.dart' ;
10+ import '../api/route/channels.dart' as channels_api;
1011import '../generated/l10n/zulip_localizations.dart' ;
1112import '../model/binding.dart' ;
1213import '../model/narrow.dart' ;
@@ -239,6 +240,32 @@ abstract final class ZulipAction {
239240
240241 return fetchedMessage? .content;
241242 }
243+
244+ static Future <void > subscribeToChannel (BuildContext context, {
245+ required int channelId,
246+ }) async {
247+ final store = PerAccountStoreWidget .of (context);
248+ final channel = store.streams[channelId];
249+ if (channel == null || channel is Subscription ) return ; // TODO could give feedback
250+
251+ try {
252+ await channels_api.subscribeToChannel (store.connection, subscriptions: [channel.name]);
253+ } catch (e) {
254+ if (! context.mounted) return ;
255+
256+ String ? errorMessage;
257+ switch (e) {
258+ case ZulipApiException ():
259+ errorMessage = e.message;
260+ // TODO(#741) specific messages for common errors, like network errors
261+ // (support with reusable code)
262+ default :
263+ }
264+
265+ final title = ZulipLocalizations .of (context).subscribeFailedTitle;
266+ showErrorDialog (context: context, title: title, message: errorMessage);
267+ }
268+ }
242269}
243270
244271/// Methods that act through platform APIs and show feedback in the UI.
0 commit comments