From 8d563c97571e178057b217b72434d3c4d554de3f Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Thu, 8 Aug 2019 12:51:07 +0200 Subject: [PATCH] [RoomState] Rename State to RoomState --- lib/famedlysdk.dart | 2 +- lib/src/Event.dart | 4 ++-- lib/src/Room.dart | 16 ++++++++-------- lib/src/RoomList.dart | 4 ++-- lib/src/{State.dart => RoomState.dart} | 8 ++++---- lib/src/Store.dart | 9 +++++---- lib/src/User.dart | 4 ++-- test/Event_test.dart | 4 ++-- test/Room_test.dart | 14 +++++++------- test/User_test.dart | 4 ++-- 10 files changed, 35 insertions(+), 34 deletions(-) rename lib/src/{State.dart => RoomState.dart} (95%) diff --git a/lib/famedlysdk.dart b/lib/famedlysdk.dart index ceda834..1be5324 100644 --- a/lib/famedlysdk.dart +++ b/lib/famedlysdk.dart @@ -36,7 +36,7 @@ export 'package:famedlysdk/src/Event.dart'; export 'package:famedlysdk/src/RawEvent.dart'; export 'package:famedlysdk/src/Room.dart'; export 'package:famedlysdk/src/RoomList.dart'; -export 'package:famedlysdk/src/State.dart'; +export 'package:famedlysdk/src/RoomState.dart'; export 'package:famedlysdk/src/Store.dart'; export 'package:famedlysdk/src/Timeline.dart'; export 'package:famedlysdk/src/User.dart'; diff --git a/lib/src/Event.dart b/lib/src/Event.dart index 8f0feb9..5252693 100644 --- a/lib/src/Event.dart +++ b/lib/src/Event.dart @@ -21,7 +21,7 @@ * along with famedlysdk. If not, see . */ -import 'package:famedlysdk/src/State.dart'; +import 'package:famedlysdk/src/RoomState.dart'; import 'package:famedlysdk/src/sync/EventUpdate.dart'; import 'package:famedlysdk/src/utils/ChatTime.dart'; @@ -29,7 +29,7 @@ import './Room.dart'; import './RawEvent.dart'; /// Defines a timeline event for a room. -class Event extends State { +class Event extends RoomState { /// The status of this event. /// -1=ERROR /// 0=SENDING diff --git a/lib/src/Room.dart b/lib/src/Room.dart index 07b0f0d..f27c29d 100644 --- a/lib/src/Room.dart +++ b/lib/src/Room.dart @@ -24,7 +24,7 @@ import 'package:famedlysdk/src/Client.dart'; import 'package:famedlysdk/src/Event.dart'; import 'package:famedlysdk/src/RoomAccountData.dart'; -import 'package:famedlysdk/src/State.dart'; +import 'package:famedlysdk/src/RoomState.dart'; import 'package:famedlysdk/src/responses/ErrorResponse.dart'; import 'package:famedlysdk/src/sync/EventUpdate.dart'; import 'package:famedlysdk/src/utils/ChatTime.dart'; @@ -54,7 +54,7 @@ class Room { int mJoinedMemberCount; int mInvitedMemberCount; - Map states = {}; + Map states = {}; Map roomAccountData = {}; @@ -431,11 +431,11 @@ class Room { roomAccountData: {}, ); - Map newStates = {}; + Map newStates = {}; if (states != null) { List> rawStates = await states; for (int i = 0; i < rawStates.length; i++) { - State newState = State.fromJson(rawStates[i], newRoom); + RoomState newState = RoomState.fromJson(rawStates[i], newRoom); newStates[newState.key] = newState; } newRoom.states = newStates; @@ -492,7 +492,7 @@ class Room { return participants; for (num i = 0; i < res["chunk"].length; i++) { - User newUser = State.fromJson(res["chunk"][i], this).asUser; + User newUser = RoomState.fromJson(res["chunk"][i], this).asUser; if (newUser.membership != Membership.leave) participants.add(newUser); } @@ -507,7 +507,7 @@ class Room { if (resp is ErrorResponse) return null; // Somehow we miss the mxid in the response and only get the content of the event. resp["matrix_id"] = mxID; - return State.fromJson(resp, this).asUser; + return RoomState.fromJson(resp, this).asUser; } /// Searches for the event in the store. If it isn't found, try to request it @@ -526,7 +526,7 @@ class Room { /// Returns the user's own power level. int getPowerLevelByUserId(String userId) { int powerLevel = 0; - State powerLevelState = states["m.room.power_levels"]; + RoomState powerLevelState = states["m.room.power_levels"]; if (powerLevelState == null) return powerLevel; if (powerLevelState.content["users_default"] is int) powerLevel = powerLevelState.content["users_default"]; @@ -541,7 +541,7 @@ class Room { /// Returns the power levels from all users for this room or null if not given. Map get powerLevels { - State powerLevelState = states["m.room.power_levels"]; + RoomState powerLevelState = states["m.room.power_levels"]; if (powerLevelState.content["users"] is Map) return powerLevelState.content["users"]; return null; diff --git a/lib/src/RoomList.dart b/lib/src/RoomList.dart index 6096dd4..d24a424 100644 --- a/lib/src/RoomList.dart +++ b/lib/src/RoomList.dart @@ -24,7 +24,7 @@ import 'dart:async'; import 'dart:core'; -import 'package:famedlysdk/src/State.dart'; +import 'package:famedlysdk/src/RoomState.dart'; import 'Client.dart'; import 'Room.dart'; @@ -141,7 +141,7 @@ class RoomList { final bool found = (j < rooms.length && rooms[j].id == eventUpdate.roomID); if (!found) return; - State stateEvent = State.fromJson(eventUpdate.content, rooms[j]); + RoomState stateEvent = RoomState.fromJson(eventUpdate.content, rooms[j]); if (rooms[j].states[stateEvent.key] != null && rooms[j].states[stateEvent.key].time > stateEvent.time) return; rooms[j].states[stateEvent.key] = stateEvent; diff --git a/lib/src/State.dart b/lib/src/RoomState.dart similarity index 95% rename from lib/src/State.dart rename to lib/src/RoomState.dart index bf083fc..3e13e37 100644 --- a/lib/src/State.dart +++ b/lib/src/RoomState.dart @@ -26,7 +26,7 @@ import 'package:famedlysdk/src/utils/ChatTime.dart'; import './Room.dart'; import './RawEvent.dart'; -class State extends RawEvent { +class RoomState extends RawEvent { /// Optional. The previous content for this state. /// This will be present only for state events appearing in the timeline. /// If this is not a state event, or there is no previous content, this key will be null. @@ -38,7 +38,7 @@ class State extends RawEvent { User get stateKeyUser => room.states[stateKey] ?? User(stateKey); - State( + RoomState( {this.prevContent, this.stateKey, dynamic content, @@ -60,14 +60,14 @@ class State extends RawEvent { room: room); /// Get a State event from a table row or from the event stream. - factory State.fromJson(Map jsonPayload, Room room) { + factory RoomState.fromJson(Map jsonPayload, Room room) { final Map content = RawEvent.getMapFromPayload(jsonPayload['content']); final Map unsigned = RawEvent.getMapFromPayload(jsonPayload['unsigned']); final Map prevContent = RawEvent.getMapFromPayload(jsonPayload['prev_content']); - return State( + return RoomState( stateKey: jsonPayload['state_key'], prevContent: prevContent, content: content, diff --git a/lib/src/Store.dart b/lib/src/Store.dart index 592bafa..a2520f8 100644 --- a/lib/src/Store.dart +++ b/lib/src/Store.dart @@ -27,7 +27,7 @@ import 'dart:core'; import 'package:famedlysdk/src/AccountData.dart'; import 'package:famedlysdk/src/Presence.dart'; -import 'package:famedlysdk/src/State.dart'; +import 'package:famedlysdk/src/RoomState.dart'; import 'package:path/path.dart' as p; import 'package:sqflite/sqflite.dart'; @@ -303,7 +303,7 @@ class Store { "SELECT * FROM States WHERE state_key=? AND room_id=?", [matrixID, room.id]); if (res.length != 1) return null; - return State.fromJson(res[0], room).asUser; + return RoomState.fromJson(res[0], room).asUser; } /// Loads all Users in the database to provide a contact list @@ -314,7 +314,8 @@ class Store { [client.userID, exceptRoomID]); List userList = []; for (int i = 0; i < res.length; i++) - userList.add(State.fromJson(res[i], Room(id: "", client: client)).asUser); + userList + .add(RoomState.fromJson(res[i], Room(id: "", client: client)).asUser); return userList; } @@ -330,7 +331,7 @@ class Store { List participants = []; for (num i = 0; i < res.length; i++) { - participants.add(State.fromJson(res[i], room).asUser); + participants.add(RoomState.fromJson(res[i], room).asUser); } return participants; diff --git a/lib/src/User.dart b/lib/src/User.dart index 11b5206..311c5fe 100644 --- a/lib/src/User.dart +++ b/lib/src/User.dart @@ -22,7 +22,7 @@ */ import 'package:famedlysdk/src/Room.dart'; -import 'package:famedlysdk/src/State.dart'; +import 'package:famedlysdk/src/RoomState.dart'; import 'package:famedlysdk/src/responses/ErrorResponse.dart'; import 'package:famedlysdk/src/utils/ChatTime.dart'; import 'package:famedlysdk/src/utils/MxContent.dart'; @@ -32,7 +32,7 @@ import 'Connection.dart'; enum Membership { join, invite, leave, ban } /// Represents a Matrix User which may be a participant in a Matrix Room. -class User extends State { +class User extends RoomState { factory User( String id, { String membership, diff --git a/test/Event_test.dart b/test/Event_test.dart index 8209116..980b589 100644 --- a/test/Event_test.dart +++ b/test/Event_test.dart @@ -25,7 +25,7 @@ import 'dart:convert'; import 'package:famedlysdk/famedlysdk.dart'; import 'package:famedlysdk/src/RawEvent.dart'; -import 'package:famedlysdk/src/State.dart'; +import 'package:famedlysdk/src/RoomState.dart'; import 'package:flutter_test/flutter_test.dart'; import 'FakeMatrixApi.dart'; @@ -64,7 +64,7 @@ void main() { expect(event.getBody(), body); expect(event.type, EventTypes.Text); jsonObj["state_key"] = ""; - State state = State.fromJson(jsonObj, null); + RoomState state = RoomState.fromJson(jsonObj, null); expect(state.eventId, id); expect(state.stateKey, ""); expect(state.key, "m.room.message"); diff --git a/test/Room_test.dart b/test/Room_test.dart index 0b251a1..14c2fc9 100644 --- a/test/Room_test.dart +++ b/test/Room_test.dart @@ -24,7 +24,7 @@ import 'package:famedlysdk/src/Client.dart'; import 'package:famedlysdk/src/Event.dart'; import 'package:famedlysdk/src/Room.dart'; -import 'package:famedlysdk/src/State.dart'; +import 'package:famedlysdk/src/RoomState.dart'; import 'package:famedlysdk/src/Timeline.dart'; import 'package:famedlysdk/src/User.dart'; import 'package:famedlysdk/src/utils/ChatTime.dart'; @@ -85,7 +85,7 @@ void main() { expect(room.mHeroes, heroes); expect(room.displayname, "alice, bob, charley"); - room.states["m.room.canonical_alias"] = State( + room.states["m.room.canonical_alias"] = RoomState( senderId: "@test:example.com", typeKey: "m.room.canonical_alias", roomId: room.id, @@ -96,7 +96,7 @@ void main() { expect(room.displayname, "testalias"); expect(room.canonicalAlias, "#testalias:example.com"); - room.states["m.room.name"] = State( + room.states["m.room.name"] = RoomState( senderId: "@test:example.com", typeKey: "m.room.name", roomId: room.id, @@ -107,7 +107,7 @@ void main() { expect(room.displayname, "testname"); expect(room.topic, ""); - room.states["m.room.topic"] = State( + room.states["m.room.topic"] = RoomState( senderId: "@test:example.com", typeKey: "m.room.topic", roomId: room.id, @@ -118,7 +118,7 @@ void main() { expect(room.topic, "testtopic"); expect(room.avatar.mxc, ""); - room.states["m.room.avatar"] = State( + room.states["m.room.avatar"] = RoomState( senderId: "@test:example.com", typeKey: "m.room.avatar", roomId: room.id, @@ -129,7 +129,7 @@ void main() { expect(room.avatar.mxc, "mxc://testurl"); expect(room.lastEvent, null); - room.states["m.room.message"] = State( + room.states["m.room.message"] = RoomState( senderId: "@test:example.com", typeKey: "m.room.message", roomId: room.id, @@ -191,7 +191,7 @@ void main() { }); test("PowerLevels", () async { - room.states["m.room.power_levels"] = State( + room.states["m.room.power_levels"] = RoomState( senderId: "@test:example.com", typeKey: "m.room.power_levels", roomId: room.id, diff --git a/test/User_test.dart b/test/User_test.dart index 7e192c2..dcb6c22 100644 --- a/test/User_test.dart +++ b/test/User_test.dart @@ -21,7 +21,7 @@ * along with famedlysdk. If not, see . */ -import 'package:famedlysdk/src/State.dart'; +import 'package:famedlysdk/src/RoomState.dart'; import 'package:famedlysdk/src/User.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -49,7 +49,7 @@ void main() { "state_key": id }; - User user = State.fromJson(jsonObj, null).asUser; + User user = RoomState.fromJson(jsonObj, null).asUser; expect(user.id, id); expect(user.membership, membership);