Skip to content

Commit

Permalink
Add discussions
Browse files Browse the repository at this point in the history
  • Loading branch information
risq committed Oct 24, 2015
1 parent a8b277d commit d38e513
Showing 7 changed files with 166 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -101,4 +101,19 @@ public String getSignature() {
public void setSignature(String signature) {
this.signature = signature;
}

@Override
public String toString() {
return "Discussion{" +
"uid='" + uid + '\'' +
", authorUid='" + authorUid + '\'' +
", guestUid='" + guestUid + '\'' +
", encryptedPassPhrase='" + encryptedPassPhrase + '\'' +
", signature='" + signature + '\'' +
", authorSignature='" + authorSignature + '\'' +
", guestSignature='" + guestSignature + '\'' +
", creationDate=" + creationDate +
", messagesUids=" + messagesUids +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
import fr.gobelins.crm14.workshop_android_crm14.R;
import fr.gobelins.crm14.workshop_android_crm14.discussion.fragments.ChatFragment;
import fr.gobelins.crm14.workshop_android_crm14.services.BusProvider;
import fr.gobelins.crm14.workshop_android_crm14.services.discussion.DiscussionService;
import fr.gobelins.crm14.workshop_android_crm14.user.User;

/**
@@ -31,6 +32,9 @@ protected void onCreate(Bundle savedInstanceState) {
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(contact.getUsername());

DiscussionService.getInstance()
.getDiscussionIdByContact(contact);

// Load login fragment
loadChatFragment();
}
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@
import butterknife.OnClick;
import fr.gobelins.crm14.workshop_android_crm14.R;
import fr.gobelins.crm14.workshop_android_crm14.services.BusProvider;
import fr.gobelins.crm14.workshop_android_crm14.services.discussion.DiscussionService;
import fr.gobelins.crm14.workshop_android_crm14.services.user.getUserData.GetUserDataEvent;
import fr.gobelins.crm14.workshop_android_crm14.user.adapter.ContactAdapter;

@@ -59,6 +60,8 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
ButterKnife.bind(this, view);
BusProvider.getInstance().register(this);



return view;
}

Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@
public class RSACryptoService {

private static final int PASSPHRASE_LENGTH = 512;
private static final int ID_LENGTH = 128;
private static final String RANDOM_ALGORITHM = "SHA1PRNG";
private static final String TAG = "RSACryptoService";

@@ -37,6 +38,12 @@ public static String generatePassphrase() throws GeneralSecurityException {
return new BigInteger(PASSPHRASE_LENGTH, random).toString(32);
}

@NonNull
public static String generateId() throws GeneralSecurityException {
SecureRandom random = SecureRandom.getInstance(RANDOM_ALGORITHM);
return new BigInteger(ID_LENGTH, random).toString(32);
}

@NonNull
public static KeyPair generateKeyPair() {
KeyPair keyPair = null;
Original file line number Diff line number Diff line change
@@ -7,13 +7,17 @@
import fr.gobelins.crm14.workshop_android_crm14.discussion.Discussion;
import fr.gobelins.crm14.workshop_android_crm14.services.BusProvider;
import fr.gobelins.crm14.workshop_android_crm14.services.DatabaseService;
import fr.gobelins.crm14.workshop_android_crm14.services.auth.AuthService;
import fr.gobelins.crm14.workshop_android_crm14.services.discussion.getDiscussion.GetDiscussionEvent;
import fr.gobelins.crm14.workshop_android_crm14.services.discussion.getDiscussionById.GetDiscussionByIdHandler;
import fr.gobelins.crm14.workshop_android_crm14.services.discussion.getDiscussionIdByContact.GetDiscussionIdByContactHandler;
import fr.gobelins.crm14.workshop_android_crm14.services.discussion.saveDiscussion.SaveDiscussionEvent;
import fr.gobelins.crm14.workshop_android_crm14.services.discussion.saveDiscussion.SaveDiscussionHandler;
import fr.gobelins.crm14.workshop_android_crm14.services.user.getUserData.GetUserDataEvent;
import fr.gobelins.crm14.workshop_android_crm14.services.user.getUserData.GetUserDataHandler;
import fr.gobelins.crm14.workshop_android_crm14.services.user.saveUserData.SaveUserDataEvent;
import fr.gobelins.crm14.workshop_android_crm14.services.user.saveUserData.SaveUserDataHandler;
import fr.gobelins.crm14.workshop_android_crm14.user.User;

/**
* Created by risq on 10/16/15.
@@ -31,19 +35,53 @@ private DiscussionService() {
}

public void saveDiscussion(Discussion discussion) {
// DatabaseService.getInstance()
// .getFirebase()
// .child("discussion")
// .child(discussion.getUid())
// .setValue(discussion, new SaveDiscussionHandler());
Log.d(TAG, "Saving discussion " + discussion.toString());
DatabaseService.getInstance()
.getFirebase()
.child("discussion")
.child(discussion.getUid())
.setValue(discussion, new SaveDiscussionHandler());
}

public void getDiscussion(Discussion discussion) {
// DatabaseService.getInstance()
// .getFirebase()
// .child("discussion")
// .child(discussion.getUid())
// .addValueEventListener(new GetUserDataHandler());
public void getDiscussionById(String discussionId) {
Log.d(TAG, "getDiscussionById " + discussionId);
DatabaseService.getInstance()
.getFirebase()
.child("discussion")
.child(discussionId)
.addListenerForSingleValueEvent(new GetDiscussionByIdHandler());
}

public void addDiscussionToUsers(Discussion discussion) {
Log.d(TAG, "addDiscussionToUsers " + discussion);
DatabaseService.getInstance()
.getFirebase()
.child("user")
.child(discussion.getAuthorUid())
.child("discussions")
.child(discussion.getGuestUid())
.setValue(discussion);

DatabaseService.getInstance()
.getFirebase()
.child("user")
.child(discussion.getGuestUid())
.child("discussions")
.child(discussion.getAuthorUid())
.setValue(discussion.getUid());
}

public void getDiscussionIdByContact(User contact) {
Log.d(TAG, "getDiscussionIdByContact " + contact.getUsername() + " " + contact.getUid());
User currentUser = AuthService.getInstance()
.getCurrentUser();
DatabaseService.getInstance()
.getFirebase()
.child("user")
.child(currentUser.getUid())
.child("discussions")
.child(contact.getUid())
.addListenerForSingleValueEvent(new GetDiscussionIdByContactHandler(currentUser, contact));
}

@Subscribe
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package fr.gobelins.crm14.workshop_android_crm14.services.discussion.getDiscussionById;

import com.firebase.client.DataSnapshot;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;

/**
* Created by risq on 10/24/15.
*/
public class GetDiscussionByIdHandler implements ValueEventListener {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

}

@Override
public void onCancelled(FirebaseError firebaseError) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package fr.gobelins.crm14.workshop_android_crm14.services.discussion.getDiscussionIdByContact;

import android.util.Log;

import com.firebase.client.DataSnapshot;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;

import java.security.GeneralSecurityException;

import fr.gobelins.crm14.workshop_android_crm14.discussion.Discussion;
import fr.gobelins.crm14.workshop_android_crm14.services.BusProvider;
import fr.gobelins.crm14.workshop_android_crm14.services.crypto.RSACryptoService;
import fr.gobelins.crm14.workshop_android_crm14.services.discussion.DiscussionService;
import fr.gobelins.crm14.workshop_android_crm14.services.user.findUserByUserName.FindUserByUsernameEvent;
import fr.gobelins.crm14.workshop_android_crm14.user.User;

/**
* Created by risq on 10/24/15.
*/
public class GetDiscussionIdByContactHandler implements ValueEventListener {
private final User guest;
private final User author;
private int requestId;

public GetDiscussionIdByContactHandler(User author, User guest) {
this.author = author;
this.guest = guest;
}

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getValue() != null) {
String discussionId = dataSnapshot.getValue().toString();
DiscussionService.getInstance()
.getDiscussionById(discussionId);
} else {
Log.d("discussionID", "null");
try {
String uid = RSACryptoService.generateId();
Discussion discussion = new Discussion(uid);
discussion.setAuthorUid(author.getUid());
discussion.setGuestUid(guest.getUid());
DiscussionService.getInstance()
.saveDiscussion(discussion);

DiscussionService.getInstance()
.addDiscussionToUsers(discussion);
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
}
// if (dataSnapshot.getChildren().iterator().hasNext()) {
// BusProvider.getInstance()
// .post(new FindUserByUsernameEvent(dataSnapshot.getChildren().iterator().next().getKey(), requestId));
// } else {
// BusProvider.getInstance()
// .post(new FindUserByUsernameEvent(requestId));
// }
//
}

@Override
public void onCancelled(FirebaseError firebaseError) {
Log.d("FindUserByUsername", "CANCELLED");
BusProvider.getInstance().post(new FindUserByUsernameEvent(firebaseError, requestId));
}
}

0 comments on commit d38e513

Please sign in to comment.