You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -117,10 +118,11 @@ A free, open source, complete, rapid development package for creating Social app
117
118
-[Chat](#chat)
118
119
-[Preview of chat functionality](#preview-of-chat-functionality)
119
120
-[Firestore structure of chat](#firestore-structure-of-chat)
120
-
-[Logic and Scenario of chat](#logic-and-scenario-of-chat)
121
+
-[Common Scenario of Chat](#common-scenario-of-chat)
122
+
-[Overview of chat functions](#overview-of-chat-functions)
121
123
-[Pitfalls of chat logic](#pitfalls-of-chat-logic)
122
124
-[Code of chat](#code-of-chat)
123
-
-[Preparation for chat](#preparation-for-chat)
125
+
-[Creating chat room list instace on global space](#creating-chat-room-list-instace-on-global-space)
124
126
-[Chat Room List](#chat-room-list)
125
127
-[Chat room](#chat-room)
126
128
-[Begin chat with a user](#begin-chat-with-a-user)
@@ -1162,6 +1164,11 @@ To create a creategory,
1162
1164
1163
1165
# Developer Coding Guidelines
1164
1166
1167
+
## Preview of Development
1168
+
1169
+
- As you may know, Firebase permission denied exception would cause a break as an `Unhandled Exception` on `VSCode` and it is the nature of fireflutter to work with permission denied exception. For instance, when the app meets permission denied error happens, the app would do something else. But the editor(like VScode) may produce `Uncaught Exception` and would stop the runtime pointing where it happens. This is normal. You can simply continue the runtime.
1170
+
- When you work with firebase, you should have something in mind that firebase works on offline and that causes reading twice.
1171
+
1165
1172
## General Setup
1166
1173
1167
1174
- Add latest version of [FireFlutter](https://pub.dev/packages/fireflutter) in pubspec.yaml
@@ -2094,23 +2101,24 @@ The settings are
2094
2101
2095
2102
# Chat
2096
2103
2104
+
(Revised on Dec 30, 2020)
2105
+
2097
2106
If you are looking for a package that support a complete chat functionality, fireflutter is for you. Unlike other sample (tutorial) code on the Internet, it has really complete features like
2098
2107
2099
-
- listing my room list (chat list or friend list).
2100
-
- group chat
2101
-
- creating chat room with one user or multip users.
2102
-
- adding user.
2103
-
- blocking user.
2104
-
- search user.
2105
-
- sending photo
2106
-
- changing settings of the room like room title update.
2107
-
- and much more.
2108
+
- Listing my room list (chat list or friend list).
2109
+
- Listening my room events globally ( or only in chat room list ).
2110
+
- Group chat (It can be one and one chat ).
2111
+
- Creating chat room with one user or multip users.
2112
+
- Adding a user(or multiple users) to existing room.
2113
+
- Blocking user.
2114
+
- Changing settings of the room like room title update.
2115
+
- And much more.
2108
2116
2109
2117
## Preview of chat functionality
2110
2118
2111
-
-All chat functionality works on user login. That means, to use chat, the user must logged in.
2119
+
-User must loginto use chat functionalities.
2112
2120
- Most of chat related methods throw permission error when a user tries something that is not permitted.
2113
-
- To use chat functionality, `openProfile` option of `ff.init()` must be set to access user's displayName and photoURL.
2121
+
-~~To use chat functionality, `openProfile` option of `ff.init()` must be set to access user's displayName and photoURL.~~ TODO: https://github.com/thruthesky/fireflutter/issues/24
2114
2122
2115
2123
## Firestore structure of chat
2116
2124
@@ -2119,24 +2127,24 @@ Before we begin, let's put an assumption.
2119
2127
- There are 4 users. User `A`, user `B`, user `C`, and user `D`. User A is the moderator which means he is the one who created the room.
2120
2128
- The room that moderator A created is `RoomA` and there are two users in the room. User A, B.
2121
2129
- The UIDs of user A, B, C, D is A, B, C, D respectively.
2122
-
- The room id of RoomA is RoomA.
2130
+
- The room id of `RoomA` is `RoomA`.
2123
2131
2124
2132
Firestore structure and its data are secured by Firestore security rules.
2125
2133
2126
-
-`/chat/info/room-list/{roomId}` is where each room information(setting) is stored. It's called global room list.
2127
-
- When a room is created, the `roomId` will be autogenrated by Firestore by adding a document under global room list collection - `/chat/info/room-list`.
2134
+
-`/chat/global/room-list/{roomId}` is where each room information(setting) is stored. It's called global room list.
2135
+
- When a room is created, the `roomId` will be autogenrated by Firestore by adding a document under global room list collection - `/chat/global/room-list`.
2128
2136
- Document properties
2129
2137
-`moderators` is an array of user's uid. Users in this array are the moderators.
2130
2138
-`users` is an array of participant users' uid.
2131
2139
-`blockedUsers` is an array of blocked users' uid.
2132
2140
-`createdAt` has the time when the chat room was created.
2133
2141
-`title` is the title of chat room.
2134
2142
- When `{users: [ ... ]}` is updated to add or remove user, other properties cannot be edited.
2135
-
-`/chat/my-room-list/{uid}/{roomId}` is where each user's room list are stored. The document has information about the room and last message. It's called private room.
2136
-
- If a user has unread messages, it has no of new messages.
2143
+
-`/chat/my-room-list/{uid}/{roomId}` is where each user's chat room information is stored. It's called private room. The document has the room information inlcuding last message of the room.
2144
+
- If a user has unread messages, it has the number of new messages.
2137
2145
- It has last message information of who sent what, time, and more.
2138
-
- It may have information about who added, blocked.
2139
-
When A chats in RoomA, the (last) message goes to `/chat/info/room-list/RoomA` and to all the users in the room which are
2146
+
- It may have information about who were added or blocked.
2147
+
When A chats in RoomA, the (last) message goes to `/chat/global/room-list/RoomA` and to all the users in the room which are
2140
2148
-`/chat/my-room-list/A/RoomA`
2141
2149
-`/chat/my-room-list/B/RoomA`
2142
2150
- Document properties
@@ -2153,55 +2161,71 @@ Firestore structure and its data are secured by Firestore security rules.
2153
2161
}
2154
2162
```
2155
2163
2156
-
-`/chat/messages/{roomId}/{message}` is where all the chat messages for the room are stored.
2164
+
-`/chat/messages/{roomId}/{message}` is where all the chat messages for each chat room are stored.
2165
+
2166
+
- By default, when a user begins to chat with another user, it will always create a new room. If `hatch: false` option is given, it may not create a new room for the same users, instead it will use the exising room.
2167
+
2168
+
## Common Scenario of Chat
2157
2169
2158
-
- By default, when a user begins to chat with another user, it will always create a new room. If `hatch` option is given, it may not create a new room for the same users.
2170
+
- User may enter(create) many chat rooms.
2171
+
- The app needs to get(or realtime update) all of login user's chat room in `/chat/my-room-list/{uid}`.
2172
+
- And the app needs to listen to all of login user's incomng chat room events.
2173
+
- And apply to the list.
2174
+
2175
+
- When the user touches on the room of the room list, the user enters the room
2176
+
- and gets some of last messages of the room
2177
+
- and listen to the events of the room and display
2178
+
2179
+
- The user can create a new chat room with other users.
2180
+
- The user can add another users.
2181
+
- If the user is one of the moderators, he can kickout/block other users.
2182
+
2183
+
2184
+
## Overview of chat functions
2159
2185
2160
-
## Logic and Scenario of chat
2161
2186
2162
2187
- User who begin(or create) to chat becomes the moderator.
2163
2188
- Moderator can add another moderator.
2164
-
- When a user enters `chat room list screen`, the app should display all of the user's chat room list. It is a recommended but costomisable.
2165
-
- User may search another user by openning a `user search screen` and select a user (or multiple users) to begin chat. Then the app should redirect the user to `chat room screen` when the use chosen other users to begin chat.
2189
+
- When a user enters `chat room list screen`, the app should display all of the user's chat room list. It is a recommended but customisable.
2190
+
- User may search another user by openning a `user search screen` and select a user (or multiple users) to begin chat. Then the app should redirect the user to `chat room screen` when the login user chosen other users to begin chat.
2166
2191
- User can enter chat room by selecting a chat room in his chat room list.
2167
2192
- User can add other users by selecting add user button in the chat room.
2168
2193
- User can create a chat room with the same user(s) over again. That means, A can begin chat with B by creating a room. And then, A can begin chat with B again by creating another room. `hatch` option can prevent creating new room upon creating a chat room with same users.
2169
2194
- When a room is created, `ChatProtocol.roomCreated` message will devlivered to all users.
2170
2195
- This protocol message can be useful to display that there is no more messages or this is the first message when user scrolls up to view previous messages.
2171
2196
- When a user is added, `ChatProtocol.enter` message (with user information) will devlivered to all users and property `users` has the names of the addedusers.
2172
2197
- When a user leaves a room, `ChatProtocol.leave` message (with user information) will devlivered to all users and property `userName` has the name of the left user.
2173
-
- When a user is blocked, `ChatProtocol.block` message will (with user information) devlivered to all users. Only moderator can blocks a user and the user's uid will be saved in `{ blockedUsers: [ ... ]}` array. And `users` will hold the names of bloked users.
2198
+
- When a user is blocked, `ChatProtocol.block` message will (with user information) devlivered to all users. Only moderator can blocks a user and the user's uid will be saved in `{ blockedUsers: [ ... ]}` array. And `users`property will hold the names of bloked users.
2174
2199
- When a room is created or a user is added, protocol message will be delivered to newly added users. And the room list should be appears on their room list.
2175
2200
- Blocked users will not be added to the room until moderator remove the user from `{ blockedUsers: [ ... ]}` array.
2176
2201
- When a user(or a moderator) leaves the room and there is no user left in the room, then that's it. The chat room is left as ghost chat room.
2177
2202
- When a user logs out or logs into another account while listening room list will causes permission error. Especially on testing, you would not open chat screen since testing uses several accounts at the same time.
2178
2203
- Logically, a user can search himself on search screen and begin chat with himself. You may add some logic to prevent it if you want.
2179
2204
- When a user is blocked by moderator, the user received no more messages except the `ChatProtocol.blocked` message.
2205
+
- Chat protocol messages should be translated into user's language by the translation functionality. You may customise to translate it by yourself.
2180
2206
2181
2207
- You would code like below to enter a chat room.
2182
2208
2183
2209
- if `id` (as chat room id) is given, it will enter the chat room and listens all the event of the room.
2184
2210
- Or if `id` is null, then a room will be created with the `users` of UIDs list.
2185
2211
- If both of `id` and `users` are null(or empty), then a room will be created without any users except the login user himself. He will be alone in the room.
2186
-
- If both of `id` and `users` have value, then, it enters the room if the room of the `id` exists. Or it will create a room with the `id` and with the users.
2187
-
- This will be a good option for 1:1 chat. If the app only allows 1:1 chat, or the user chats to admin for help, this will be a good option.
2188
-
- The `id` can be an md5 string of the login user's uid(A) and other user's uid(B).
2189
-
- When it creates the room, it will create a room for A and B, and next time A or B try to chat each other again, it will not create a new room. Instead, it will use previously created room.
2212
+
2190
2213
2191
2214
- If the app must inform new messages to the user when the user is not in room list screen,
2192
2215
2193
-
- The app can listen `my-room-list` collection on app screen (or homescreen)
2216
+
- The app can listen `my-room-list` collection on app screen (or homescreen, or in global space).
2194
2217
- And when a new message arrives, the app can show snackbar.
2195
2218
2196
2219
- You may put the logic of the app like below
2197
2220
- Declare `ChatMyRoomList` and `ChatRoom` instances as global variables.
2198
2221
- Listen to chat room update on home screen and display updates on chat icon.
2199
2222
- When somebody chats, the user will get push notification. and ignore push notifications if it's my chat or someone that I am talking to.
2200
2223
2224
+
2201
2225
## Pitfalls of chat logic
2202
2226
2203
2227
- User cannot remove(or block) another user. Only moderator can do it.
2204
-
- A add B.
2228
+
- A adds B.
2205
2229
- B goes offline.
2206
2230
- B add C. Since Firebase works offline, B can still add user C even if he has no connection.
2207
2231
- A add D.
@@ -2212,18 +2236,92 @@ Firestore structure and its data are secured by Firestore security rules.
2212
2236
2213
2237
## Code of chat
2214
2238
2215
-
### Preparation for chat
2239
+
### Creating chat room list instace on global space
2216
2240
2217
-
- By default, app can search users by name in `/meta/user/public/{uid}`. You may extend to search by gender and age.
2218
-
- And it requires `openProfile` option to be set in `fireflutter` initialization like below.
2219
-
- This option updates user's profile name and photo under `/meta/user/public/{uid}` and a user can search other users name and photo.
2241
+
- Depending on your app structure, you may declare login user's chat room list instance in global scope.
2242
+
- The declaration below could be a separate service(library) file.
2220
2243
2221
2244
```dart
2222
-
ff.init({
2223
-
'openProfile': true,
2224
-
})
2245
+
final FireFlutter ff = FireFlutter();
2246
+
2247
+
/// [myRoomList] is the instance of ChatMyRoomList. It will be instanciated in
2248
+
/// main.dart
2249
+
ChatMyRoomList myRoomList;
2250
+
2251
+
/// [myRoomListChanges] will be fired whenever/whatever events happens for my
0 commit comments