Skip to content

Commit

Permalink
* Add http call for all subscription update list
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastien Dugene committed Jan 11, 2021
1 parent ace6cb4 commit e637266
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.0.17

* Add http call for all subscription update list

## 0.0.16

* Clean code
Expand Down
18 changes: 17 additions & 1 deletion example/lib/rest_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import 'package:rocket_chat_connector_flutter/models/filters/channel_counters_fi
import 'package:rocket_chat_connector_flutter/models/filters/channel_history_filter.dart';
import 'package:rocket_chat_connector_flutter/models/new/message_new.dart';
import 'package:rocket_chat_connector_flutter/models/response/message_new_response.dart';
import 'package:rocket_chat_connector_flutter/models/subscription.dart';
import 'package:rocket_chat_connector_flutter/models/subscription_update.dart';
import 'package:rocket_chat_connector_flutter/services/authentication_service.dart';
import 'package:rocket_chat_connector_flutter/services/channel_service.dart';
import 'package:rocket_chat_connector_flutter/services/http_service.dart'
as rocket_http_service;
import 'package:rocket_chat_connector_flutter/services/message_service.dart';
import 'package:rocket_chat_connector_flutter/services/subscription_service.dart';

final String serverUrl = "myServerUrl";
final String username = "myUserName";
Expand All @@ -23,16 +26,29 @@ final AuthenticationService authenticationService =
AuthenticationService(rocketHttpService);
final ChannelService channelService = ChannelService(rocketHttpService);
final MessageService messageService = MessageService(rocketHttpService);
final SubscriptionService subscriptionService =
SubscriptionService(rocketHttpService);

Future main(List<String> args) async {
Authentication authentication =
await authenticationService.login(username, password);

// get all subscription with new messages
Subscription subscription =
await subscriptionService.getSubscriptions(authentication);
List<SubscriptionUpdate> updates =
subscription.update.where((e) => e.alert).toList();

for (SubscriptionUpdate subscriptionUpdate in updates) {
print(
"${subscriptionUpdate.t == "d" ? "Room" : "Channel"} rid ${subscriptionUpdate.rid} named ${subscriptionUpdate.name} have new messages");
}

// get channel message counter
ChannelCountersFilter filter = ChannelCountersFilter(channel);
ChannelCounters counters =
await channelService.counters(filter, authentication);
print("Channel have ${counters.unreads} unread messages");
print("Channel specified have ${counters.unreads} unread messages");

// get channel message list
ChannelHistoryFilter channelHistoryFilter =
Expand Down
39 changes: 39 additions & 0 deletions lib/models/subscription.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import 'dart:convert';

import 'package:rocket_chat_connector_flutter/models/subscription_update.dart';

class Subscription {
List<SubscriptionUpdate> update;
Object remove;
bool success;

Subscription({
this.update,
this.remove,
this.success,
});

Subscription.fromMap(Map<String, dynamic> json) {
if (json != null) {
if (json['update'] != null) {
List<dynamic> jsonList = json['update'].runtimeType == String //
? jsonDecode(json['update'])
: json['update'];
update = jsonList
.where((json) => json != null)
.map((value) => SubscriptionUpdate.fromMap(value))
.toList();
} else {
update = null;
}

remove = json['remove'] != null ? json['remove'] : null;
success = json['success'];
}
}

@override
String toString() {
return 'Subscription{update: $update, remove: $remove, success: $success}';
}
}
76 changes: 76 additions & 0 deletions lib/models/subscription_update.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import 'dart:convert';

import 'package:rocket_chat_connector_flutter/models/user.dart';

class SubscriptionUpdate {
String id;
bool open;
bool alert;
int unread;
int userMentions;
int groupMentions;
DateTime ts;
String rid;
String name;
String fName;
String t;
User u;
DateTime ls;
DateTime updatedAt;
List<String> roles;

SubscriptionUpdate({
this.id,
this.open,
this.alert,
this.unread,
this.userMentions,
this.groupMentions,
this.ts,
this.rid,
this.name,
this.fName,
this.t,
this.u,
this.ls,
this.updatedAt,
this.roles,
});

SubscriptionUpdate.fromMap(Map<String, dynamic> json) {
if (json != null) {
id = json['_id'];
open = json['open'];
alert = json['alert'];
unread = json['unread'];
userMentions = json['userMentions'];
groupMentions = json['groupMentions'];
ts = json['ts'] != null ? DateTime.parse(json['ts']) : null;
rid = json['rid'];
name = json['name'];
fName = json['fName'];
t = json['t'];
u = json['u'] != null ? User.fromMap(json['u']) : null;
ls = json['ls'] != null ? DateTime.parse(json['ls']) : null;
updatedAt = json['_updatedAt'] != null
? DateTime.parse(json['_updatedAt'])
: null;
if (json['roles'] != null) {
List<dynamic> jsonList = json['roles'].runtimeType == String //
? jsonDecode(json['roles'])
: json['roles'];
roles = jsonList
.where((json) => json != null)
.map((value) => value.toString())
.toList();
} else {
roles = null;
}
}
}

@override
String toString() {
return 'SubscriptionUpdate{id: $id, open: $open, alert: $alert, unread: $unread, userMentions: $userMentions, groupMentions: $groupMentions, ts: $ts, rid: $rid, name: $name, fName: $fName, t: $t, u: $u, ls: $ls, updatedAt: $updatedAt, roles: $roles}';
}
}
29 changes: 29 additions & 0 deletions lib/services/subscription_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'dart:convert';

import 'package:http/http.dart' as http;
import 'package:rocket_chat_connector_flutter/exceptions/exception.dart';
import 'package:rocket_chat_connector_flutter/models/authentication.dart';
import 'package:rocket_chat_connector_flutter/models/subscription.dart';
import 'package:rocket_chat_connector_flutter/services/http_service.dart';

class SubscriptionService {
HttpService _httpService;

SubscriptionService(this._httpService);

Future<Subscription> getSubscriptions(Authentication authentication) async {
http.Response response = await _httpService.get(
'/api/v1/subscriptions.get',
authentication,
);

if (response?.statusCode == 200) {
if (response?.body?.isNotEmpty == true) {
return Subscription.fromMap(jsonDecode(response.body));
} else {
return Subscription();
}
}
throw RocketChatException(response?.body);
}
}
12 changes: 8 additions & 4 deletions lib/web_socket/notification_result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class NotificationResult {
if (json != null) {
id = json['_id'] != null ? json['_id'] : json['id'];
token = json['token'];
tokenExpires = json['tokenExpires'] != null && json['tokenExpires']['\$date'] != null
tokenExpires = json['tokenExpires'] != null &&
json['tokenExpires']['\$date'] != null
? DateTime.fromMillisecondsSinceEpoch(json['tokenExpires']['\$date'])
: DateTime.now();
type = json['type'];
Expand All @@ -41,11 +42,14 @@ class NotificationResult {
? DateTime.fromMillisecondsSinceEpoch(json['ts']['\$date'])
: DateTime.now();
user = json['u'] != null ? NotificationUser.fromMap(json['u']) : null;
updatedAt = json['_updatedAt'] != null && json['_updatedAt']['\$date'] != null
updatedAt = json['_updatedAt'] != null &&
json['_updatedAt']['\$date'] != null
? DateTime.fromMillisecondsSinceEpoch(json['_updatedAt']['\$date'])
: DateTime.now();
mentions = json['mentions'] != null ? List<String>.from(json['mentions']) : null;
channels = json['channels'] != null ? List<String>.from(json['channels']) : null;
mentions =
json['mentions'] != null ? List<String>.from(json['mentions']) : null;
channels =
json['channels'] != null ? List<String>.from(json['channels']) : null;
}
}

Expand Down
11 changes: 2 additions & 9 deletions lib/web_socket/notification_type.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
enum NotificationType {
CONNECTED,
RESULT,
UPDATED,
CHANGED,
PING,
UNKNOWN
}
enum NotificationType { CONNECTED, RESULT, UPDATED, CHANGED, PING, UNKNOWN }

NotificationType notificationTypeFromString(String notificationType) {
if (notificationType == 'connected') {
Expand All @@ -21,4 +14,4 @@ NotificationType notificationTypeFromString(String notificationType) {
} else {
return NotificationType.UNKNOWN;
}
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: rocket_chat_connector_flutter
description: Flutter Rocket.chat connector. This library is developed to allow the use of a Rocket.chat server as a messaging system.
homepage: https://github.com/sdugene/rocket-chat-connector-flutter
version: 0.0.16
version: 0.0.17

environment:
sdk: ">=2.1.0 <3.0.0"
Expand Down

0 comments on commit e637266

Please sign in to comment.