Skip to content

Commit

Permalink
Fix wording and add room service methods counters, history
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastien Dugene committed May 21, 2020
1 parent 88b43a5 commit ddf40fc
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 35 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.7

* Fix wording and add room service methods counters, history

## 0.0.6

* Fix NPE on filter
Expand Down
12 changes: 6 additions & 6 deletions lib/models/filters/channel_history_filter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ChannelHistoryFilter extends ChannelFilter {
DateTime latest;
DateTime oldest;
bool inclusive;
int offsetint;
int offset;
int count;
bool unreads;

Expand All @@ -14,7 +14,7 @@ class ChannelHistoryFilter extends ChannelFilter {
this.latest,
this.oldest,
this.inclusive,
this.offsetint,
this.offset,
this.count,
this.unreads,
}) : super(channel);
Expand All @@ -24,14 +24,14 @@ class ChannelHistoryFilter extends ChannelFilter {
'latest': latest != null ? latest.toIso8601String() : null,
'oldest': oldest != null ? oldest.toIso8601String() : null,
'inclusive': inclusive,
'offsetint': offsetint,
'offset': offset,
'count': count,
'unreads': unreads
};

@override
String toString() {
return 'ChannelHistoryFilter{latest: $latest, oldest: $oldest, inclusive: $inclusive, offsetint: $offsetint, count: $count, unreads: $unreads}';
return 'ChannelHistoryFilter{latest: $latest, oldest: $oldest, inclusive: $inclusive, offset: $offset, count: $count, unreads: $unreads}';
}

@override
Expand All @@ -43,7 +43,7 @@ class ChannelHistoryFilter extends ChannelFilter {
latest == other.latest &&
oldest == other.oldest &&
inclusive == other.inclusive &&
offsetint == other.offsetint &&
offset == other.offset &&
count == other.count &&
unreads == other.unreads;

Expand All @@ -53,7 +53,7 @@ class ChannelHistoryFilter extends ChannelFilter {
latest.hashCode ^
oldest.hashCode ^
inclusive.hashCode ^
offsetint.hashCode ^
offset.hashCode ^
count.hashCode ^
unreads.hashCode;
}
31 changes: 31 additions & 0 deletions lib/models/filters/room_counters_filter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:rocket_chat_connector_flutter/models/filters/room_filter.dart';
import 'package:rocket_chat_connector_flutter/models/room.dart';

import '../user.dart';

class RoomCountersFilter extends RoomFilter {
User user;

RoomCountersFilter(Room room, {this.user}) : super(room);

Map<String, dynamic> toMap() => {
'roomId': room.id,
'userId': user != null ? user.id : null,
};

@override
String toString() {
return 'RoomCountersFilter{user: $user}';
}

@override
bool operator ==(Object other) =>
identical(this, other) ||
super == other &&
other is RoomCountersFilter &&
runtimeType == other.runtimeType &&
user == other.user;

@override
int get hashCode => super.hashCode ^ user.hashCode;
}
27 changes: 27 additions & 0 deletions lib/models/filters/room_filter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'package:rocket_chat_connector_flutter/models/filters/filter.dart';
import 'package:rocket_chat_connector_flutter/models/room.dart';

class RoomFilter extends Filter {
Room room;

RoomFilter(this.room);

Map<String, dynamic> toMap() => {
'roomId': room.id,
};

@override
String toString() {
return 'RoomFilter{room: $room}';
}

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is RoomFilter &&
runtimeType == other.runtimeType &&
room == other.room;

@override
int get hashCode => room.hashCode;
}
59 changes: 59 additions & 0 deletions lib/models/filters/room_history_filter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import 'package:rocket_chat_connector_flutter/models/filters/room_filter.dart';
import 'package:rocket_chat_connector_flutter/models/room.dart';

class RoomHistoryFilter extends RoomFilter {
DateTime latest;
DateTime oldest;
bool inclusive;
int offset;
int count;
bool unreads;

RoomHistoryFilter(
Room room, {
this.latest,
this.oldest,
this.inclusive,
this.offset,
this.count,
this.unreads,
}) : super(room);

Map<String, dynamic> toMap() => {
'roomId': room.id,
'latest': latest != null ? latest.toIso8601String() : null,
'oldest': oldest != null ? oldest.toIso8601String() : null,
'inclusive': inclusive,
'offset': offset,
'count': count,
'unreads': unreads
};

@override
String toString() {
return 'RoomHistoryFilter{latest: $latest, oldest: $oldest, inclusive: $inclusive, offset: $offset, count: $count, unreads: $unreads}';
}

@override
bool operator ==(Object other) =>
identical(this, other) ||
super == other &&
other is RoomHistoryFilter &&
runtimeType == other.runtimeType &&
latest == other.latest &&
oldest == other.oldest &&
inclusive == other.inclusive &&
offset == other.offset &&
count == other.count &&
unreads == other.unreads;

@override
int get hashCode =>
super.hashCode ^
latest.hashCode ^
oldest.hashCode ^
inclusive.hashCode ^
offset.hashCode ^
count.hashCode ^
unreads.hashCode;
}
40 changes: 29 additions & 11 deletions lib/services/room_service.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import 'dart:convert';

import 'package:http/http.dart' as http;
import 'package:rocket_chat_connector_flutter/models/filters/room_counters_filter.dart';
import 'package:rocket_chat_connector_flutter/models/filters/room_filter.dart';
import 'package:rocket_chat_connector_flutter/models/filters/room_history_filter.dart';
import 'package:rocket_chat_connector_flutter/models/new/room_new.dart';
import 'package:rocket_chat_connector_flutter/models/response/response.dart';
import 'package:rocket_chat_connector_flutter/models/response/room_new_response.dart';
import 'package:rocket_chat_connector_flutter/models/room.dart';
import 'package:rocket_chat_connector_flutter/models/room_counters.dart';
import 'package:rocket_chat_connector_flutter/models/room_messages.dart';
import 'package:rocket_chat_connector_flutter/models/user.dart';
import 'package:rocket_chat_connector_flutter/services/http_service.dart';

class RoomService {
Expand All @@ -25,24 +28,39 @@ class RoomService {
}

Future<RoomMessages> messages(Room room) async {
http.Response response =
await _httpService.get('/api/v1/im.messages?roomId=${room.rid}');
http.Response response = await _httpService.getWithFilter(
'/api/v1/im.messages', RoomFilter(room));

if (response?.statusCode == 200 && response.body?.isNotEmpty == true) {
return RoomMessages.fromMap(jsonDecode(response.body));
}
return null;
}

Future<RoomCounters> counters(Room room, [User user]) async {
http.Response response;
if (user != null) {
response = await _httpService
.get('/api/v1/im.counters?roomId=${room.id}&userId=${user.id}');
} else {
response =
await _httpService.get('/api/v1/im.counters?roomId=${room.id}');
Future<bool> markAsRead(Room room) async {
Map<String, String> body = {"rid": room.id};

http.Response response =
await _httpService.post('/api/v1/subscriptions.read', jsonEncode(body));
if (response?.statusCode == 200 && response.body?.isNotEmpty == true) {
return Response.fromMap(jsonDecode(response.body)).success == true;
}
return false;
}

Future<RoomMessages> history(RoomHistoryFilter filter) async {
http.Response response =
await _httpService.getWithFilter('/api/v1/im.history', filter);

if (response?.statusCode == 200 && response.body?.isNotEmpty == true) {
return RoomMessages.fromMap(jsonDecode(response.body));
}
return null;
}

Future<RoomCounters> counters(RoomCountersFilter filter) async {
http.Response response =
await _httpService.getWithFilter('/api/v1/im.counters', filter);

if (response?.statusCode == 200 && response.body?.isNotEmpty == true) {
return RoomCounters.fromMap(jsonDecode(response.body));
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,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.
author: Sebastien Dugene <sebastien.dugene@gmail.com>
homepage: https://github.com/sdugene/rocket-chat-connector-flutter
version: 0.0.6
version: 0.0.7

environment:
sdk: ">=2.1.0 <3.0.0"
Expand Down
80 changes: 63 additions & 17 deletions test/services/room_service_test.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import 'dart:convert';

import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart';
import 'package:http/http.dart' as http;
import 'package:mockito/mockito.dart';
import 'package:rocket_chat_connector_flutter/models/filters/room_counters_filter.dart';
import 'package:rocket_chat_connector_flutter/models/filters/room_filter.dart';
import 'package:rocket_chat_connector_flutter/models/filters/room_history_filter.dart';
import 'package:rocket_chat_connector_flutter/models/new/room_new.dart';
import 'package:rocket_chat_connector_flutter/models/response/response.dart';
import 'package:rocket_chat_connector_flutter/models/response/room_new_response.dart';
import 'package:rocket_chat_connector_flutter/models/room.dart';
import 'package:rocket_chat_connector_flutter/models/room_counters.dart';
Expand Down Expand Up @@ -33,8 +37,8 @@ void main() {
});

test('create room', () async {
Response response =
Response(jsonEncode(RoomNewResponseData.getMapById(1)), 200);
http.Response response =
http.Response(jsonEncode(RoomNewResponseData.getMapById(1)), 200);
when(httpServiceMock.post("/api/v1/im.create", jsonEncode(roomNew.toMap())))
.thenAnswer((_) => Future(() => response));

Expand All @@ -44,39 +48,81 @@ void main() {

test('room messages', () async {
Room room = RoomData.getById("ByehQjC44FwMeiLbX");
RoomFilter filter = RoomFilter(room);

Response response =
Response(jsonEncode(RoomMessagesData.getMapById(1)), 200);
when(httpServiceMock.get("/api/v1/im.messages?roomId=${room.id}"))
http.Response response =
http.Response(jsonEncode(RoomMessagesData.getMapById(1)), 200);
when(httpServiceMock.getWithFilter("/api/v1/im.messages", filter))
.thenAnswer((_) => Future(() => response));

RoomMessages roomMessages = await roomService.messages(room);
expect(roomMessages.success, true);
});

test('channel counters without user', () async {
test('room markAsRead', () async {
Room room = RoomData.getById("ByehQjC44FwMeiLbX");
Map<String, String> body = {"rid": room.id};

Response response =
Response(jsonEncode(RoomCountersData.getMapById(1)), 200);
when(httpServiceMock.get("/api/v1/im.counters?roomId=${room.id}"))
http.Response response =
http.Response(jsonEncode(Response(success: true).toMap()), 200);
when(httpServiceMock.post("/api/v1/subscriptions.read", jsonEncode(body)))
.thenAnswer((_) => Future(() => response));

RoomCounters roomCounters = await roomService.counters(room);
bool success = await roomService.markAsRead(room);
expect(success, true);
});

test('room counters without user', () async {
Room room = RoomData.getById("ByehQjC44FwMeiLbX");
RoomCountersFilter filter = RoomCountersFilter(room);

http.Response response =
http.Response(jsonEncode(RoomCountersData.getMapById(1)), 200);
when(httpServiceMock.getWithFilter("/api/v1/im.counters", filter))
.thenAnswer((_) => Future(() => response));

RoomCounters roomCounters = await roomService.counters(filter);
expect(roomCounters.success, true);
});

test('channel counters with user', () async {
test('room counters with user', () async {
User user = UserData.getById("aobEdbYhXfu5hkeqG");
Room room = RoomData.getById("ByehQjC44FwMeiLbX");
RoomCountersFilter filter = RoomCountersFilter(room, user: user);

Response response =
Response(jsonEncode(RoomCountersData.getMapById(1)), 200);
when(httpServiceMock
.get("/api/v1/im.counters?roomId=${room.id}&userId=${user.id}"))
http.Response response =
http.Response(jsonEncode(RoomCountersData.getMapById(1)), 200);
when(httpServiceMock.getWithFilter("/api/v1/im.counters", filter))
.thenAnswer((_) => Future(() => response));

RoomCounters roomCounters = await roomService.counters(room, user);
RoomCounters roomCounters = await roomService.counters(filter);
expect(roomCounters.success, true);
});

test('room history', () async {
Room room = RoomData.getById("ByehQjC44FwMeiLbX");
RoomHistoryFilter filter = RoomHistoryFilter(room);

http.Response response =
http.Response(jsonEncode(RoomCountersData.getMapById(1)), 200);
when(httpServiceMock.getWithFilter("/api/v1/im.history", filter))
.thenAnswer((_) => Future(() => response));

RoomMessages roomMessages = await roomService.history(filter);
expect(roomMessages.success, true);
});

test('room history with date', () async {
Room room = RoomData.getById("ByehQjC44FwMeiLbX");
RoomHistoryFilter filter =
RoomHistoryFilter(room, latest: DateTime.now());

http.Response response =
http.Response(jsonEncode(RoomCountersData.getMapById(1)), 200);
when(httpServiceMock.getWithFilter("/api/v1/im.history", filter))
.thenAnswer((_) => Future(() => response));

RoomMessages roomMessages = await roomService.history(filter);
expect(roomMessages.success, true);
});
}

0 comments on commit ddf40fc

Please sign in to comment.