-
Notifications
You must be signed in to change notification settings - Fork 184
/
data_model.dart
170 lines (131 loc) · 4.91 KB
/
data_model.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright (c) 2016, Google Inc. Please see the AUTHORS file for details.
// All rights reserved. Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/// Data model for the built_value chat example.
library data_model;
import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';
part 'data_model.g.dart';
/// Marker interface for classes sent from client to server.
abstract class Command {}
/// Sends a chat.
abstract class Chat implements Built<Chat, ChatBuilder>, Command {
static Serializer<Chat> get serializer => _$chatSerializer;
// Chat text.
String get text;
/// Set of usernames to send the chat to, or empty to send to everyone.
BuiltSet<String> get targets;
factory Chat([Function(ChatBuilder) updates]) = _$Chat;
Chat._();
}
/// Logs in.
abstract class Login implements Built<Login, LoginBuilder>, Command {
static Serializer<Login> get serializer => _$loginSerializer;
String get username;
String get password;
factory Login([Function(LoginBuilder) updates]) = _$Login;
Login._();
}
/// User status: online, away or offline, and a message.
///
/// Sent as a command, sets the current user status.
abstract class Status implements Built<Status, StatusBuilder>, Command {
static Serializer<Status> get serializer => _$statusSerializer;
String get message;
StatusType get type;
factory Status([Function(StatusBuilder) updates]) = _$Status;
Status._();
}
/// User status: online, away or offline.
class StatusType extends EnumClass {
static Serializer<StatusType> get serializer => _$statusTypeSerializer;
static const StatusType online = _$online;
static const StatusType away = _$away;
static const StatusType offline = _$offline;
const StatusType._(String name) : super(name);
static BuiltSet<StatusType> get values => _$stValues;
static StatusType valueOf(String name) => _$stValueOf(name);
}
/// Lists users, filtered by status.
abstract class ListUsers
implements Built<ListUsers, ListUsersBuilder>, Command {
static Serializer<ListUsers> get serializer => _$listUsersSerializer;
/// Set of statuses to filter by.
BuiltSet<StatusType> get statusTypes;
factory ListUsers([Function(ListUsersBuilder) updates]) = _$ListUsers;
ListUsers._();
}
/// Classes sent from server to client.
abstract class Response {
String render();
}
/// Response to a login attempt.
class LoginResponse extends EnumClass implements Response {
static Serializer<LoginResponse> get serializer => _$loginResponseSerializer;
static const LoginResponse success = _$success;
static const LoginResponse badPassword = _$badPassword;
static const LoginResponse reset = _$reset;
const LoginResponse._(String name) : super(name);
static BuiltSet<LoginResponse> get values => _$lrValues;
static LoginResponse valueOf(String name) => _$lrValueOf(name);
@override
String render() {
switch (this) {
case success:
return 'You have logged in successfully.';
case badPassword:
return 'Incorrect password.';
case reset:
return 'You have been logged out.';
default:
throw StateError(name);
}
}
}
/// Displays a chat message.
abstract class ShowChat implements Built<ShowChat, ShowChatBuilder>, Response {
static Serializer<ShowChat> get serializer => _$showChatSerializer;
/// Originator of the message.
String get username;
/// Whether the message is a /tell or a general message.
bool get private;
/// Message text.
String get text;
factory ShowChat([Function(ShowChatBuilder) updates]) = _$ShowChat;
ShowChat._();
@override
String render() => '$username${private ? " (private)" : ""}: $text';
}
abstract class Welcome implements Built<Welcome, WelcomeBuilder>, Response {
static Serializer<Welcome> get serializer => _$welcomeSerializer;
BuiltList<Response> get log;
String get message;
factory Welcome([Function(WelcomeBuilder) updates]) = _$Welcome;
Welcome._();
@override
String render() =>
log.map((response) => response.render()).join('\n') + '\n' + message;
}
/// Displays a list of users and their status messages.
abstract class ListUsersResponse
implements Built<ListUsersResponse, ListUsersResponseBuilder>, Response {
static Serializer<ListUsersResponse> get serializer =>
_$listUsersResponseSerializer;
/// Map from username to status.
BuiltMap<String, Status> get statuses;
factory ListUsersResponse([Function(ListUsersResponseBuilder) updates]) =
_$ListUsersResponse;
ListUsersResponse._();
@override
String render() {
final result = StringBuffer('The following users are online:\n\n');
for (final username in statuses.keys) {
final status = statuses[username]!;
result.write(
status.message.isEmpty ? username : '$username ${status.message}');
result.write('\n');
}
return result.toString();
}
}