Skip to content

Commit

Permalink
Added fixes to support new security rules, updated support libraries …
Browse files Browse the repository at this point in the history
…and fixed bugs
  • Loading branch information
bensmiley committed Feb 7, 2018
1 parent 6d6e81a commit c7c7638
Show file tree
Hide file tree
Showing 20 changed files with 454 additions and 222 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ The Chat SDK has a number of additional modules that can easily be installed inc
- [Push Notifications](https://github.com/chat-sdk/chat-sdk-android#push-notifications)
- [File Storage](https://github.com/chat-sdk/chat-sdk-android/tree/master/chat-sdk-firebase-file-storage) (Included in basic setup instructions)

## Firebase Firestore

If you are interested in a version of the Chat SDK that supports Firebase's new database please vote on [this issue](https://github.com/chat-sdk/chat-sdk-android/issues/148).

## Get involved!
We're very excited about the project and we're looking for other people to get involved. Over time we would like to make the best messaging framework for mobile. Helping us could involve any of the following:

Expand Down
5 changes: 5 additions & 0 deletions chat-sdk-core/src/main/java/co/chatsdk/core/dao/Keys.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public static final class Exit {
public static final String Users = "users";
public static final String UID = "uid";

public static final String Owner = "owner";
public static final String Member = "member";

public static final String ImageUrl = "image-url";
public static final String CreatorEntityId = "creator-entity-id";
public static final String Deleted = "deleted";
Expand Down Expand Up @@ -96,6 +99,8 @@ public static final class Exit {
public static final String Status = "status";
public static final String PushToken = "pushToken";



// public static final class ThirdParty {
// public static final String Name = "name";
// public static final String ImageURL = "profile_image_url";
Expand Down
20 changes: 20 additions & 0 deletions chat-sdk-core/src/main/java/co/chatsdk/core/dao/Thread.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,26 @@ public void addMessage (Message message) {
resetMessages();
}

// public void removeMessage (Message message) {
// // If this is the last message
// if(message.getEntityID().equals(lastMessage().getEntityID())) {
// List<Message> messages = getMessagesWithOrder(DaoCore.ORDER_DESC);
// if (messages.size() > 1) {
// setLastMessage(messages.get(messages.size() - 1));
// }
// else {
// setLastMessage(null);
// }
// }
//
// setLastMessage(message);
// message.setThreadId(this.getId());
// message.update();
//
// update();
// resetMessages();
// }

public boolean hasUser(User user) {

UserThreadLink data =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public <T extends CoreEntity> T fetchOrCreateEntityWithEntityID(Class<T> c, Stri
entity = DaoCore.getEntityForClass(c);

if(entityId instanceof String) {
entity.setEntityID((String) entityId);
entity.setEntityID(entityId);
}
else {
entity.setEntityID(entityId.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,10 @@ public void userOff(final String entityID){

final User user = DaoCore.fetchEntityWithEntityID(User.class, entityID);

FirebaseReferenceManager.shared().removeListener(FirebasePaths.userThreadsRef(entityID));
FirebaseReferenceManager.shared().removeListener(FirebasePaths.publicThreadsRef());
FirebaseReferenceManager.shared().removeListener(FirebasePaths.userFollowersRef(entityID));
FirebaseReferenceManager.shared().removeListener(FirebasePaths.userFollowingRef(entityID));
FirebaseReferenceManager.shared().removeListeners(FirebasePaths.userThreadsRef(entityID));
FirebaseReferenceManager.shared().removeListeners(FirebasePaths.publicThreadsRef());
FirebaseReferenceManager.shared().removeListeners(FirebasePaths.userFollowersRef(entityID));
FirebaseReferenceManager.shared().removeListeners(FirebasePaths.userFollowingRef(entityID));

ThreadWrapper wrapper;
for (Thread thread : NM.thread().getThreads(ThreadType.All))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import co.chatsdk.core.dao.Keys;
import co.chatsdk.core.session.ChatSDK;

public class FirebasePaths{
Expand Down Expand Up @@ -117,21 +116,4 @@ public static DatabaseReference indexRef(){
return firebaseRef().child(IndexPath);
}


public static PathBuilder userThreadsPath (String userID, String threadID) {
return new PathBuilder(Keys.Users)
.append(userID)
.append(Keys.Threads)
.append(threadID);

}

public static PathBuilder threadUsersPath (String threadID, String userID) {
return new PathBuilder(Keys.Threads)
.append(threadID)
.append(Keys.Users)
.append(userID);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;

/**
* Created by benjaminsmiley-andrews on 18/05/2017.
Expand Down Expand Up @@ -64,7 +62,7 @@ public boolean isOn (DatabaseReference ref) {
return references.get(ref.toString()) != null;
}

public void removeListener (DatabaseReference ref) {
public void removeListeners (DatabaseReference ref) {
if(isOn(ref)) {
Value v = references.get(ref.toString());
v.removeListener();
Expand All @@ -79,4 +77,4 @@ public void removeAllListeners () {
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@

public class FirebaseThreadHandler extends AbstractThreadHandler {

public static int UserThreadLinkTypeAddUser = 1;
public static int UserThreadLinkTypeRemoveUser = 2;

public Single<List<Message>> loadMoreMessagesForThread(final Message fromMessage,final Thread thread) {
return super.loadMoreMessagesForThread(fromMessage, thread).flatMap(new Function<List<Message>, SingleSource<? extends List<Message>>>() {
@Override
Expand All @@ -61,7 +64,7 @@ public SingleSource<? extends List<Message>> apply(List<Message> messages) throw
* When all users are added the system will call the "onDone" method.
**/
public Completable addUsersToThread(final Thread thread, final List<User> users) {
return setUserThreadLinkValue(thread, users, Keys.Null);
return setUserThreadLinkValue(thread, users, UserThreadLinkTypeAddUser);
}

/**
Expand All @@ -72,36 +75,37 @@ public Completable addUsersToThread(final Thread thread, final List<User> users)
* path and the thread will be removed from the user/threads path
* @param thread
* @param users
* @param value
* @param userThreadLinkType - 1 => Add, 2 => Remove
* @return
*/
private Completable setUserThreadLinkValue(final Thread thread, final List<User> users, final String value) {
private Completable setUserThreadLinkValue(final Thread thread, final List<User> users, final int userThreadLinkType) {
return Completable.create(new CompletableOnSubscribe() {
@Override
public void subscribe(final CompletableEmitter e) throws Exception {

DatabaseReference ref = FirebasePaths.firebaseRef();
DatabaseReference ref = FirebasePaths.firebaseRawRef();
final HashMap<String, Object> data = new HashMap<>();

for (final User u : users) {
PathBuilder threadUsersPath = FirebasePaths.threadUsersPath(thread.getEntityID(), u.getEntityID());
PathBuilder userThreadsPath = FirebasePaths.userThreadsPath(u.getEntityID(), thread.getEntityID());

if (value != null) {
threadUsersPath.append(Keys.Null);
userThreadsPath.append(Keys.InvitedBy);
}
DatabaseReference threadUsersRef = FirebasePaths.threadUsersRef(thread.getEntityID()).child(u.getEntityID()).child(Keys.Status);
DatabaseReference userThreadsRef = FirebasePaths.userThreadsRef(u.getEntityID()).child(thread.getEntityID()).child(Keys.InvitedBy);

data.put(threadUsersPath.build(), value);
String threadUsersPath = threadUsersRef.toString().replace(threadUsersRef.getRoot().toString(), "");
String userThreadsPath = userThreadsRef.toString().replace(userThreadsRef.getRoot().toString(), "");

if (thread.typeIs(ThreadType.Private)) {
data.put(userThreadsPath.build(), value != null ? NM.currentUser().getEntityID() : value);
//
if(userThreadLinkType == UserThreadLinkTypeAddUser) {
data.put(threadUsersPath, u.getEntityID().equals(thread.getCreatorEntityId()) ? Keys.Owner : Keys.Member);
data.put(userThreadsPath, NM.currentUser().getEntityID());

if (thread.typeIs(ThreadType.Public)) {
threadUsersRef.onDisconnect().removeValue();
}
}
else if (value != null) {
// TODO: Check this
// If we add users to a public thread, make sure that they are removed if we
// log off
FirebasePaths.firebaseRef().child(threadUsersPath.build()).onDisconnect().removeValue();
else if (userThreadLinkType == UserThreadLinkTypeRemoveUser) {
data.put(threadUsersPath, null);
data.put(userThreadsPath, null);
}
}

Expand All @@ -124,7 +128,7 @@ public void onComplete(DatabaseError databaseError, DatabaseReference databaseRe
}

public Completable removeUsersFromThread(final Thread thread, List<User> users) {
return setUserThreadLinkValue(thread, users, null);
return setUserThreadLinkValue(thread, users, UserThreadLinkTypeRemoveUser);
}

public Completable pushThread(Thread thread) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ public void trigger(DataSnapshot snapshot, boolean hasValue) {
* Stop listening to thread details change
**/
public void off() {
FirebaseReferenceManager.shared().removeListener(FirebasePaths.threadDetailsRef(model.getEntityID()));
FirebaseReferenceManager.shared().removeListener(FirebasePaths.threadLastMessageRef(model.getEntityID()));
FirebaseReferenceManager.shared().removeListeners(FirebasePaths.threadDetailsRef(model.getEntityID()));
FirebaseReferenceManager.shared().removeListeners(FirebasePaths.threadLastMessageRef(model.getEntityID()));
if(NM.typingIndicator() != null) {
NM.typingIndicator().typingOff(model);
}
Expand All @@ -176,6 +176,19 @@ public void subscribe(final ObservableEmitter<Message> e) throws Exception {
return;
}

// Add the delete listener
ChildEventListener removedListener = ref.addChildEventListener(new FirebaseEventListener().onChildRemoved(new FirebaseEventListener.Removed() {
@Override
public void trigger(DataSnapshot snapshot, boolean hasValue) {
if(hasValue) {
MessageWrapper message = new MessageWrapper(snapshot);

}
}
}));

FirebaseReferenceManager.shared().addRef(ref, removedListener);

threadDeletedDate()
.subscribeOn(Schedulers.single())
.subscribe(new Consumer<Long>() {
Expand Down Expand Up @@ -248,7 +261,7 @@ public void trigger(DataSnapshot snapshot, String s, boolean hasValue) {
**/
public void messagesOff() {
DatabaseReference ref = FirebasePaths.threadMessagesRef(model.getEntityID());
FirebaseReferenceManager.shared().removeListener(ref);
FirebaseReferenceManager.shared().removeListeners(ref);
}

//Note the old listener that was used to process the thread bundle is still in use.
Expand Down Expand Up @@ -298,7 +311,7 @@ public void trigger(DataSnapshot snapshot, boolean hasValue) {
**/
public void usersOff(){
DatabaseReference ref = FirebasePaths.threadUsersRef(model.getEntityID());
FirebaseReferenceManager.shared().removeListener(ref);
FirebaseReferenceManager.shared().removeListeners(ref);
}

//Note - Maybe should reject when cant find value in the user deleted path.
Expand Down Expand Up @@ -463,15 +476,7 @@ private Map<String, Object> serialize() {
Map<String , Object> value = new HashMap<String, Object>();
Map<String , Object> nestedMap = new HashMap<String, Object>();

// If the creation date is null we assume that the thread is now being created so we push the server timestamp with it.
// Else we will push the saved creation date from the db.
// No treating this as so can cause problems with firebase security rules.
if (model.getCreationDate() == null) {
nestedMap.put(Keys.CreationDate, ServerValue.TIMESTAMP);
}
else {
nestedMap.put(Keys.CreationDate, model.getCreationDate().getTime());
}
nestedMap.put(Keys.CreationDate, ServerValue.TIMESTAMP);

nestedMap.put(Keys.Name, model.getName());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public void trigger(DataSnapshot snapshot, boolean hasValue) {

public void metaOff(){
DatabaseReference userMetaRef = FirebasePaths.userMetaRef(model.getEntityID());
FirebaseReferenceManager.shared().removeListener(userMetaRef);
FirebaseReferenceManager.shared().removeListeners(userMetaRef);
}


Expand Down Expand Up @@ -291,7 +291,7 @@ public void trigger(DataSnapshot snapshot, boolean hasValue) {

public void onlineOff () {
DatabaseReference ref = FirebasePaths.userOnlineRef(model.getEntityID());
FirebaseReferenceManager.shared().removeListener(ref);
FirebaseReferenceManager.shared().removeListeners(ref);
}

Map<String, Object> serialize(){
Expand Down
1 change: 0 additions & 1 deletion chat-sdk-ui/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ dependencies {
compile 'com.android.support:support-core-utils:' + project.ANDROID_SUPPORT_VERSION
compile 'com.android.support:design:' + project.ANDROID_SUPPORT_VERSION
compile 'com.android.support:recyclerview-v7:' + project.ANDROID_SUPPORT_VERSION
// compile "com.android.support:support-v4:" + project.ANDROID_SUPPORT_VERSION
compile "com.android.support:support-v13:" + project.ANDROID_SUPPORT_VERSION
compile "com.android.support:cardview-v7:" + project.ANDROID_SUPPORT_VERSION
compile "com.android.support:design:" + project.ANDROID_SUPPORT_VERSION
Expand Down
Loading

0 comments on commit c7c7638

Please sign in to comment.