[Event] Make Event subclass of State

This commit is contained in:
Christian Pauly 2019-08-08 12:29:09 +02:00
parent 1e46af6b67
commit 35b9e0db40
2 changed files with 25 additions and 11 deletions

View file

@ -21,6 +21,7 @@
* along with famedlysdk. If not, see <http://www.gnu.org/licenses/>. * along with famedlysdk. If not, see <http://www.gnu.org/licenses/>.
*/ */
import 'package:famedlysdk/src/State.dart';
import 'package:famedlysdk/src/sync/EventUpdate.dart'; import 'package:famedlysdk/src/sync/EventUpdate.dart';
import 'package:famedlysdk/src/utils/ChatTime.dart'; import 'package:famedlysdk/src/utils/ChatTime.dart';
@ -28,7 +29,7 @@ import './Room.dart';
import './RawEvent.dart'; import './RawEvent.dart';
/// Defines a timeline event for a room. /// Defines a timeline event for a room.
class Event extends RawEvent { class Event extends State {
/// The status of this event. /// The status of this event.
/// -1=ERROR /// -1=ERROR
/// 0=SENDING /// 0=SENDING
@ -45,6 +46,8 @@ class Event extends RawEvent {
String senderId, String senderId,
ChatTime time, ChatTime time,
dynamic unsigned, dynamic unsigned,
dynamic prevContent,
String stateKey,
Room room}) Room room})
: super( : super(
content: content, content: content,
@ -54,6 +57,8 @@ class Event extends RawEvent {
senderId: senderId, senderId: senderId,
time: time, time: time,
unsigned: unsigned, unsigned: unsigned,
prevContent: prevContent,
stateKey: stateKey,
room: room); room: room);
/// Get a State event from a table row or from the event stream. /// Get a State event from a table row or from the event stream.
@ -62,6 +67,8 @@ class Event extends RawEvent {
RawEvent.getMapFromPayload(jsonPayload['content']); RawEvent.getMapFromPayload(jsonPayload['content']);
final Map<String, dynamic> unsigned = final Map<String, dynamic> unsigned =
RawEvent.getMapFromPayload(jsonPayload['unsigned']); RawEvent.getMapFromPayload(jsonPayload['unsigned']);
final Map<String, dynamic> prevContent =
RawEvent.getMapFromPayload(jsonPayload['prev_content']);
return Event( return Event(
status: jsonPayload['status'] ?? 1, status: jsonPayload['status'] ?? 1,
content: content, content: content,
@ -71,6 +78,8 @@ class Event extends RawEvent {
senderId: jsonPayload['sender'], senderId: jsonPayload['sender'],
time: ChatTime(jsonPayload['origin_server_ts']), time: ChatTime(jsonPayload['origin_server_ts']),
unsigned: unsigned, unsigned: unsigned,
prevContent: prevContent,
stateKey: jsonPayload['state_key'],
room: room); room: room);
} }

View file

@ -250,16 +250,19 @@ class Store {
]); ]);
else else
txn.rawInsert( txn.rawInsert(
"INSERT OR REPLACE INTO Events VALUES(?, ?, ?, ?, ?, ?, ?, ?)", [ "INSERT OR REPLACE INTO Events VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
eventContent["event_id"], [
chat_id, eventContent["event_id"],
eventContent["origin_server_ts"], chat_id,
eventContent["sender"], eventContent["origin_server_ts"],
eventContent["type"], eventContent["sender"],
json.encode(eventContent["unsigned"] ?? ""), eventContent["type"],
json.encode(eventContent["content"]), json.encode(eventContent["unsigned"] ?? ""),
status json.encode(eventContent["content"]),
]); json.encode(eventContent["prevContent"]),
eventContent["state_key"],
status
]);
// Is there a transaction id? Then delete the event with this id. // Is there a transaction id? Then delete the event with this id.
if (status != -1 && if (status != -1 &&
@ -474,6 +477,8 @@ class Store {
'type TEXT, ' + 'type TEXT, ' +
'unsigned TEXT, ' + 'unsigned TEXT, ' +
'content TEXT, ' + 'content TEXT, ' +
'prev_content TEXT, ' +
'state_key TEXT, ' +
"status INTEGER, " + "status INTEGER, " +
'UNIQUE(id))', 'UNIQUE(id))',