SlideShare a Scribd company logo
Creating a Facebook Clone - Part XXII
UserService is a big class and it makes sense to cover in two parts
setProps(user, u);
if (u.getPassword() != null) {
u.setPassword(encoder.encode(user.getPassword()));
}
users.save(u);
}
public byte[] getAvatar(String userId) {
User u = users.findById(userId).get();
if (u.getAvatar() != null) {
return u.getAvatar().getData();
}
return null;
}
public void setAvatar(String authToken, String mediaId) {
Media m = medias.findById(mediaId).get();
User u = users.findByAuthtoken(authToken).get(0);
u.setAvatar(m);
users.save(u);
}
public void sendFriendRequest(String auth, String userId) {
User me = users.findByAuthtoken(auth).get(0);
User him = users.findById(userId).get();
UserService
Next on the agenda is getAvatar, it returns the avatar picture of the user as a byte array
setProps(user, u);
if (u.getPassword() != null) {
u.setPassword(encoder.encode(user.getPassword()));
}
users.save(u);
}
public byte[] getAvatar(String userId) {
User u = users.findById(userId).get();
if (u.getAvatar() != null) {
return u.getAvatar().getData();
}
return null;
}
public void setAvatar(String authToken, String mediaId) {
Media m = medias.findById(mediaId).get();
User u = users.findByAuthtoken(authToken).get(0);
u.setAvatar(m);
users.save(u);
}
public void sendFriendRequest(String auth, String userId) {
User me = users.findByAuthtoken(auth).get(0);
User him = users.findById(userId).get();
UserService
Notice we need an id and not a token to get the avatar as anyone who is familiar with the user ID should be able to see the avatar
setProps(user, u);
if (u.getPassword() != null) {
u.setPassword(encoder.encode(user.getPassword()));
}
users.save(u);
}
public byte[] getAvatar(String userId) {
User u = users.findById(userId).get();
if (u.getAvatar() != null) {
return u.getAvatar().getData();
}
return null;
}
public void setAvatar(String authToken, String mediaId) {
Media m = medias.findById(mediaId).get();
User u = users.findByAuthtoken(authToken).get(0);
u.setAvatar(m);
users.save(u);
}
public void sendFriendRequest(String auth, String userId) {
User me = users.findByAuthtoken(auth).get(0);
User him = users.findById(userId).get();
UserService
If the avatar reference is null we return null instantly
setProps(user, u);
if (u.getPassword() != null) {
u.setPassword(encoder.encode(user.getPassword()));
}
users.save(u);
}
public byte[] getAvatar(String userId) {
User u = users.findById(userId).get();
if (u.getAvatar() != null) {
return u.getAvatar().getData();
}
return null;
}
public void setAvatar(String authToken, String mediaId) {
Media m = medias.findById(mediaId).get();
User u = users.findByAuthtoken(authToken).get(0);
u.setAvatar(m);
users.save(u);
}
public void sendFriendRequest(String auth, String userId) {
User me = users.findByAuthtoken(auth).get(0);
User him = users.findById(userId).get();
UserService
The setAvatar method works in a similar way
setProps(user, u);
if (u.getPassword() != null) {
u.setPassword(encoder.encode(user.getPassword()));
}
users.save(u);
}
public byte[] getAvatar(String userId) {
User u = users.findById(userId).get();
if (u.getAvatar() != null) {
return u.getAvatar().getData();
}
return null;
}
public void setAvatar(String authToken, String mediaId) {
Media m = medias.findById(mediaId).get();
User u = users.findByAuthtoken(authToken).get(0);
u.setAvatar(m);
users.save(u);
}
public void sendFriendRequest(String auth, String userId) {
User me = users.findByAuthtoken(auth).get(0);
User him = users.findById(userId).get();
UserService
Notice that unlike getAvatar with setAvatar we need a token as this is a set operation. This prevents other users from changing our avatar.

When we map this to the webservice we need to first create a media entry and only then set the avatar value.
u.setAvatar(m);
users.save(u);
}
public void sendFriendRequest(String auth, String userId) {
User me = users.findByAuthtoken(auth).get(0);
User him = users.findById(userId).get();
him.getFriendRequests().add(me);
users.save(him);
notifications.sendNotification(him,
new NotificationDAO(null, me.getDAO(),
"wants to be friends",
"uE7FC", 0x587EBE,
0, true, null, null));
}
public void acceptFriendRequest(String auth, String userId) {
User me = users.findByAuthtoken(auth).get(0);
User him = users.findById(userId).get();
if (me.getFriendRequests().contains(him)) {
me.getFriendRequests().remove(him);
me.getFriends().add(him);
him.getFriendRequests().remove(me);
him.getFriends().add(me);
users.saveAll(Arrays.asList(me, him));
UserService
The friend request includes a few moving parts. First we need to send a request.
u.setAvatar(m);
users.save(u);
}
public void sendFriendRequest(String auth, String userId) {
User me = users.findByAuthtoken(auth).get(0);
User him = users.findById(userId).get();
him.getFriendRequests().add(me);
users.save(him);
notifications.sendNotification(him,
new NotificationDAO(null, me.getDAO(),
"wants to be friends",
"uE7FC", 0x587EBE,
0, true, null, null));
}
public void acceptFriendRequest(String auth, String userId) {
User me = users.findByAuthtoken(auth).get(0);
User him = users.findById(userId).get();
if (me.getFriendRequests().contains(him)) {
me.getFriendRequests().remove(him);
me.getFriends().add(him);
him.getFriendRequests().remove(me);
him.getFriends().add(me);
users.saveAll(Arrays.asList(me, him));
UserService
We need an auth token as this is a secure operation
u.setAvatar(m);
users.save(u);
}
public void sendFriendRequest(String auth, String userId) {
User me = users.findByAuthtoken(auth).get(0);
User him = users.findById(userId).get();
him.getFriendRequests().add(me);
users.save(him);
notifications.sendNotification(him,
new NotificationDAO(null, me.getDAO(),
"wants to be friends",
"uE7FC", 0x587EBE,
0, true, null, null));
}
public void acceptFriendRequest(String auth, String userId) {
User me = users.findByAuthtoken(auth).get(0);
User him = users.findById(userId).get();
if (me.getFriendRequests().contains(him)) {
me.getFriendRequests().remove(him);
me.getFriends().add(him);
him.getFriendRequests().remove(me);
him.getFriends().add(me);
users.saveAll(Arrays.asList(me, him));
UserService
We obviously don't have the auth token of the person we are "friending"
u.setAvatar(m);
users.save(u);
}
public void sendFriendRequest(String auth, String userId) {
User me = users.findByAuthtoken(auth).get(0);
User him = users.findById(userId).get();
him.getFriendRequests().add(me);
users.save(him);
notifications.sendNotification(him,
new NotificationDAO(null, me.getDAO(),
"wants to be friends",
"uE7FC", 0x587EBE,
0, true, null, null));
}
public void acceptFriendRequest(String auth, String userId) {
User me = users.findByAuthtoken(auth).get(0);
User him = users.findById(userId).get();
if (me.getFriendRequests().contains(him)) {
me.getFriendRequests().remove(him);
me.getFriends().add(him);
him.getFriendRequests().remove(me);
him.getFriends().add(me);
users.saveAll(Arrays.asList(me, him));
UserService
This is a special case where we modify his entity without an auth-token, that's a bit risky...
u.setAvatar(m);
users.save(u);
}
public void sendFriendRequest(String auth, String userId) {
User me = users.findByAuthtoken(auth).get(0);
User him = users.findById(userId).get();
him.getFriendRequests().add(me);
users.save(him);
notifications.sendNotification(him,
new NotificationDAO(null, me.getDAO(),
"wants to be friends",
"uE7FC", 0x587EBE,
0, true, null, null));
}
public void acceptFriendRequest(String auth, String userId) {
User me = users.findByAuthtoken(auth).get(0);
User him = users.findById(userId).get();
if (me.getFriendRequests().contains(him)) {
me.getFriendRequests().remove(him);
me.getFriends().add(him);
him.getFriendRequests().remove(me);
him.getFriends().add(me);
users.saveAll(Arrays.asList(me, him));
UserService
The notification service should notify the user that he got a new friend request. We'll dig into the notification service soon…
public void sendFriendRequest(String auth, String userId) {
User me = users.findByAuthtoken(auth).get(0);
User him = users.findById(userId).get();
him.getFriendRequests().add(me);
users.save(him);
notifications.sendNotification(him,
new NotificationDAO(null, me.getDAO(),
"wants to be friends",
"uE7FC", 0x587EBE,
0, true, null, null));
}
public void acceptFriendRequest(String auth, String userId) {
User me = users.findByAuthtoken(auth).get(0);
User him = users.findById(userId).get();
if (me.getFriendRequests().contains(him)) {
me.getFriendRequests().remove(him);
me.getFriends().add(him);
him.getFriendRequests().remove(me);
him.getFriends().add(me);
users.saveAll(Arrays.asList(me, him));
}
}
public void uploadContacts(String auth, List<ShadowUserDAO> contacts) {
Long c = shadows.countByUser(auth);
UserService
But first lets check out the other side of the equation. The acceptFriendRequest method.
public void sendFriendRequest(String auth, String userId) {
User me = users.findByAuthtoken(auth).get(0);
User him = users.findById(userId).get();
him.getFriendRequests().add(me);
users.save(him);
notifications.sendNotification(him,
new NotificationDAO(null, me.getDAO(),
"wants to be friends",
"uE7FC", 0x587EBE,
0, true, null, null));
}
public void acceptFriendRequest(String auth, String userId) {
User me = users.findByAuthtoken(auth).get(0);
User him = users.findById(userId).get();
if (me.getFriendRequests().contains(him)) {
me.getFriendRequests().remove(him);
me.getFriends().add(him);
him.getFriendRequests().remove(me);
him.getFriends().add(me);
users.saveAll(Arrays.asList(me, him));
}
}
public void uploadContacts(String auth, List<ShadowUserDAO> contacts) {
Long c = shadows.countByUser(auth);
UserService
This time me & him are flipped
public void sendFriendRequest(String auth, String userId) {
User me = users.findByAuthtoken(auth).get(0);
User him = users.findById(userId).get();
him.getFriendRequests().add(me);
users.save(him);
notifications.sendNotification(him,
new NotificationDAO(null, me.getDAO(),
"wants to be friends",
"uE7FC", 0x587EBE,
0, true, null, null));
}
public void acceptFriendRequest(String auth, String userId) {
User me = users.findByAuthtoken(auth).get(0);
User him = users.findById(userId).get();
if (me.getFriendRequests().contains(him)) {
me.getFriendRequests().remove(him);
me.getFriends().add(him);
him.getFriendRequests().remove(me);
him.getFriends().add(me);
users.saveAll(Arrays.asList(me, him));
}
}
public void uploadContacts(String auth, List<ShadowUserDAO> contacts) {
Long c = shadows.countByUser(auth);
UserService
This checks against a potential weakness where a hacker could potentially call accept friend request for a person that didn't request it
public void sendFriendRequest(String auth, String userId) {
User me = users.findByAuthtoken(auth).get(0);
User him = users.findById(userId).get();
him.getFriendRequests().add(me);
users.save(him);
notifications.sendNotification(him,
new NotificationDAO(null, me.getDAO(),
"wants to be friends",
"uE7FC", 0x587EBE,
0, true, null, null));
}
public void acceptFriendRequest(String auth, String userId) {
User me = users.findByAuthtoken(auth).get(0);
User him = users.findById(userId).get();
if (me.getFriendRequests().contains(him)) {
me.getFriendRequests().remove(him);
me.getFriends().add(him);
him.getFriendRequests().remove(me);
him.getFriends().add(me);
users.saveAll(Arrays.asList(me, him));
}
}
public void uploadContacts(String auth, List<ShadowUserDAO> contacts) {
Long c = shadows.countByUser(auth);
UserService
When a request is accepted we need to update the friend list on both sides. I didn't add a removeFriendRequest method which I probably should have added. I think it's
trivial to add so I'm leaving it as an exercise.
users.saveAll(Arrays.asList(me, him));
}
}
public void uploadContacts(String auth, List<ShadowUserDAO> contacts) {
Long c = shadows.countByUser(auth);
User u = users.findByAuthtoken(auth).get(0);
if (c == null || c == 0) {
List<ShadowUser> shadowList = new ArrayList<>();
for (ShadowUserDAO d : contacts) {
shadowList.add(new ShadowUser(d, u));
updatePeopleYouMayKnow(d, u);
}
shadows.saveAll(shadowList);
} else {
for (ShadowUserDAO d : contacts) {
List<ShadowUser> results = shadows.findByFullName(auth, d.
getFullName());
if (!results.isEmpty()) {
shadows.save(new ShadowUser(d, u));
} else {
ShadowUser current = results.get(0);
current.setEmail(d.getEmail());
current.setPhone(d.getPhone());
current.setSecondaryPhone(d.getSecondaryPhone());
shadows.save(current);
UserService
The last method for UserService is the `uploadContacts` method...

It's invoked when the user uploads the contacts from his phone.
users.saveAll(Arrays.asList(me, him));
}
}
public void uploadContacts(String auth, List<ShadowUserDAO> contacts) {
Long c = shadows.countByUser(auth);
User u = users.findByAuthtoken(auth).get(0);
if (c == null || c == 0) {
List<ShadowUser> shadowList = new ArrayList<>();
for (ShadowUserDAO d : contacts) {
shadowList.add(new ShadowUser(d, u));
updatePeopleYouMayKnow(d, u);
}
shadows.saveAll(shadowList);
} else {
for (ShadowUserDAO d : contacts) {
List<ShadowUser> results = shadows.findByFullName(auth, d.
getFullName());
if (!results.isEmpty()) {
shadows.save(new ShadowUser(d, u));
} else {
ShadowUser current = results.get(0);
current.setEmail(d.getEmail());
current.setPhone(d.getPhone());
current.setSecondaryPhone(d.getSecondaryPhone());
shadows.save(current);
UserService
If we uploaded contacts in the past its a good idea to try and merge with what we have. We can check that by running a count query
users.saveAll(Arrays.asList(me, him));
}
}
public void uploadContacts(String auth, List<ShadowUserDAO> contacts) {
Long c = shadows.countByUser(auth);
User u = users.findByAuthtoken(auth).get(0);
if (c == null || c == 0) {
List<ShadowUser> shadowList = new ArrayList<>();
for (ShadowUserDAO d : contacts) {
shadowList.add(new ShadowUser(d, u));
updatePeopleYouMayKnow(d, u);
}
shadows.saveAll(shadowList);
} else {
for (ShadowUserDAO d : contacts) {
List<ShadowUser> results = shadows.findByFullName(auth, d.
getFullName());
if (!results.isEmpty()) {
shadows.save(new ShadowUser(d, u));
} else {
ShadowUser current = results.get(0);
current.setEmail(d.getEmail());
current.setPhone(d.getPhone());
current.setSecondaryPhone(d.getSecondaryPhone());
shadows.save(current);
UserService
For every contact we find we check to see if there is a user in the system and if so we add that user to the people you may know list
users.saveAll(Arrays.asList(me, him));
}
}
public void uploadContacts(String auth, List<ShadowUserDAO> contacts) {
Long c = shadows.countByUser(auth);
User u = users.findByAuthtoken(auth).get(0);
if (c == null || c == 0) {
List<ShadowUser> shadowList = new ArrayList<>();
for (ShadowUserDAO d : contacts) {
shadowList.add(new ShadowUser(d, u));
updatePeopleYouMayKnow(d, u);
}
shadows.saveAll(shadowList);
} else {
for (ShadowUserDAO d : contacts) {
List<ShadowUser> results = shadows.findByFullName(auth, d.
getFullName());
if (!results.isEmpty()) {
shadows.save(new ShadowUser(d, u));
} else {
ShadowUser current = results.get(0);
current.setEmail(d.getEmail());
current.setPhone(d.getPhone());
current.setSecondaryPhone(d.getSecondaryPhone());
shadows.save(current);
UserService
When adding a lot of elements using saveAll is much faster than save
if (c == null || c == 0) {
List<ShadowUser> shadowList = new ArrayList<>();
for (ShadowUserDAO d : contacts) {
shadowList.add(new ShadowUser(d, u));
updatePeopleYouMayKnow(d, u);
}
shadows.saveAll(shadowList);
} else {
for (ShadowUserDAO d : contacts) {
List<ShadowUser> results = shadows.findByFullName(auth, d.
getFullName());
if (!results.isEmpty()) {
shadows.save(new ShadowUser(d, u));
} else {
ShadowUser current = results.get(0);
current.setEmail(d.getEmail());
current.setPhone(d.getPhone());
current.setSecondaryPhone(d.getSecondaryPhone());
shadows.save(current);
updatePeopleYouMayKnow(d, u);
}
}
}
}
private void updatePeopleYouMayKnow(ShadowUserDAO d, User u) {
UserService
For simplicity I chose to merge based on full name. In retrospect it might have been wiser to use the device unique ID for every contact but that might breed duplicates
too.

I oversimplified a very complex process of handling shadow users. If you take the time to build something like this then make sure to analyze the data more thoroughly
and create a more robust social graph.
current.setEmail(d.getEmail());
current.setPhone(d.getPhone());
current.setSecondaryPhone(d.getSecondaryPhone());
shadows.save(current);
updatePeopleYouMayKnow(d, u);
}
}
}
}
private void updatePeopleYouMayKnow(ShadowUserDAO d, User u) {
User friend = findHim(d);
if (friend != null) {
if (!u.getFriends().contains(friend)
&& !u.getPeopleYouMayKnow().contains(friend)) {
u.getPeopleYouMayKnow().add(friend);
users.save(u);
}
}
}
private User findHim(ShadowUserDAO d) {
if (d.getEmail() != null) {
List<User> l = users.findByEmailIgnoreCase(d.getEmail());
if (l != null && !l.isEmpty()) {
return l.get(0);
UserService
Notice that we still need to declare the updatePeopleYouMayKnow method we mentioned before
current.setEmail(d.getEmail());
current.setPhone(d.getPhone());
current.setSecondaryPhone(d.getSecondaryPhone());
shadows.save(current);
updatePeopleYouMayKnow(d, u);
}
}
}
}
private void updatePeopleYouMayKnow(ShadowUserDAO d, User u) {
User friend = findHim(d);
if (friend != null) {
if (!u.getFriends().contains(friend)
&& !u.getPeopleYouMayKnow().contains(friend)) {
u.getPeopleYouMayKnow().add(friend);
users.save(u);
}
}
}
private User findHim(ShadowUserDAO d) {
if (d.getEmail() != null) {
List<User> l = users.findByEmailIgnoreCase(d.getEmail());
if (l != null && !l.isEmpty()) {
return l.get(0);
UserService
We search for the shadow user in the table of users using the findHim method which we'll cover soon
current.setEmail(d.getEmail());
current.setPhone(d.getPhone());
current.setSecondaryPhone(d.getSecondaryPhone());
shadows.save(current);
updatePeopleYouMayKnow(d, u);
}
}
}
}
private void updatePeopleYouMayKnow(ShadowUserDAO d, User u) {
User friend = findHim(d);
if (friend != null) {
if (!u.getFriends().contains(friend)
&& !u.getPeopleYouMayKnow().contains(friend)) {
u.getPeopleYouMayKnow().add(friend);
users.save(u);
}
}
}
private User findHim(ShadowUserDAO d) {
if (d.getEmail() != null) {
List<User> l = users.findByEmailIgnoreCase(d.getEmail());
if (l != null && !l.isEmpty()) {
return l.get(0);
UserService
If a friend was found & isn't already a friend or in the "People You May Know" list...
current.setEmail(d.getEmail());
current.setPhone(d.getPhone());
current.setSecondaryPhone(d.getSecondaryPhone());
shadows.save(current);
updatePeopleYouMayKnow(d, u);
}
}
}
}
private void updatePeopleYouMayKnow(ShadowUserDAO d, User u) {
User friend = findHim(d);
if (friend != null) {
if (!u.getFriends().contains(friend)
&& !u.getPeopleYouMayKnow().contains(friend)) {
u.getPeopleYouMayKnow().add(friend);
users.save(u);
}
}
}
private User findHim(ShadowUserDAO d) {
if (d.getEmail() != null) {
List<User> l = users.findByEmailIgnoreCase(d.getEmail());
if (l != null && !l.isEmpty()) {
return l.get(0);
UserService
Then we can add that user to the people you may know
}
}
}
private User findHim(ShadowUserDAO d) {
if (d.getEmail() != null) {
List<User> l = users.findByEmailIgnoreCase(d.getEmail());
if (l != null && !l.isEmpty()) {
return l.get(0);
}
}
if (d.getPhone() != null) {
List<User> l = users.findByPhone(d.getPhone());
if (l != null && !l.isEmpty()) {
return l.get(0);
}
}
if (d.getSecondaryPhone() != null) {
List<User> l = users.findByPhone(d.getSecondaryPhone());
if (l != null && !l.isEmpty()) {
return l.get(0);
}
}
return null;
}
}
UserService
The findHim method is simple boilerplate that searches based on all the meta data in the shadow user. We use findByEmail/phone etc. to find the shadow user in our
regular user database.

With that we finished the UserService class which is the largest service class!

More Related Content

PDF
Creating a Facebook Clone - Part XXII.pdf
PDF
Creating a Whatsapp Clone - Part XIII.pdf
PDF
Creating a Facebook Clone - Part XXIII.pdf
PDF
Creating a Whatsapp Clone - Part XIV - Transcript.pdf
PDF
Creating a Facebook Clone - Part XXV.pdf
PDF
Creating a Facebook Clone - Part X - Transcript.pdf
PDF
Creating a Facebook Clone - Part XIII - Transcript.pdf
PDF
Creating a Facebook Clone - Part X.pdf
Creating a Facebook Clone - Part XXII.pdf
Creating a Whatsapp Clone - Part XIII.pdf
Creating a Facebook Clone - Part XXIII.pdf
Creating a Whatsapp Clone - Part XIV - Transcript.pdf
Creating a Facebook Clone - Part XXV.pdf
Creating a Facebook Clone - Part X - Transcript.pdf
Creating a Facebook Clone - Part XIII - Transcript.pdf
Creating a Facebook Clone - Part X.pdf

Similar to Creating a Facebook Clone - Part XXII - Transcript.pdf (8)

PDF
Creating a Facebook Clone - Part XI - Transcript.pdf
PDF
Creating a Facebook Clone - Part XXV - Transcript.pdf
PDF
Creating a Facebook Clone - Part XIII.pdf
PDF
Creating a Facebook Clone - Part XXI.pdf
PDF
Creating a Facebook Clone - Part XXIII - Transcript.pdf
PDF
Creating a Whatsapp Clone - Part XI - Transcript.pdf
PDF
Creating a Whatsapp Clone - Part XIII - Transcript.pdf
PDF
Creating a Facebook Clone - Part XXXVII.pdf
Creating a Facebook Clone - Part XI - Transcript.pdf
Creating a Facebook Clone - Part XXV - Transcript.pdf
Creating a Facebook Clone - Part XIII.pdf
Creating a Facebook Clone - Part XXI.pdf
Creating a Facebook Clone - Part XXIII - Transcript.pdf
Creating a Whatsapp Clone - Part XI - Transcript.pdf
Creating a Whatsapp Clone - Part XIII - Transcript.pdf
Creating a Facebook Clone - Part XXXVII.pdf

More from ShaiAlmog1 (20)

PDF
The Duck Teaches Learn to debug from the masters. Local to production- kill ...
PDF
create-netflix-clone-06-client-ui.pdf
PDF
create-netflix-clone-01-introduction_transcript.pdf
PDF
create-netflix-clone-02-server_transcript.pdf
PDF
create-netflix-clone-04-server-continued_transcript.pdf
PDF
create-netflix-clone-01-introduction.pdf
PDF
create-netflix-clone-06-client-ui_transcript.pdf
PDF
create-netflix-clone-03-server.pdf
PDF
create-netflix-clone-04-server-continued.pdf
PDF
create-netflix-clone-05-client-model_transcript.pdf
PDF
create-netflix-clone-03-server_transcript.pdf
PDF
create-netflix-clone-02-server.pdf
PDF
create-netflix-clone-05-client-model.pdf
PDF
Creating a Whatsapp Clone - Part II.pdf
PDF
Creating a Whatsapp Clone - Part IX - Transcript.pdf
PDF
Creating a Whatsapp Clone - Part II - Transcript.pdf
PDF
Creating a Whatsapp Clone - Part V - Transcript.pdf
PDF
Creating a Whatsapp Clone - Part IV - Transcript.pdf
PDF
Creating a Whatsapp Clone - Part IV.pdf
PDF
Creating a Whatsapp Clone - Part I - Transcript.pdf
The Duck Teaches Learn to debug from the masters. Local to production- kill ...
create-netflix-clone-06-client-ui.pdf
create-netflix-clone-01-introduction_transcript.pdf
create-netflix-clone-02-server_transcript.pdf
create-netflix-clone-04-server-continued_transcript.pdf
create-netflix-clone-01-introduction.pdf
create-netflix-clone-06-client-ui_transcript.pdf
create-netflix-clone-03-server.pdf
create-netflix-clone-04-server-continued.pdf
create-netflix-clone-05-client-model_transcript.pdf
create-netflix-clone-03-server_transcript.pdf
create-netflix-clone-02-server.pdf
create-netflix-clone-05-client-model.pdf
Creating a Whatsapp Clone - Part II.pdf
Creating a Whatsapp Clone - Part IX - Transcript.pdf
Creating a Whatsapp Clone - Part II - Transcript.pdf
Creating a Whatsapp Clone - Part V - Transcript.pdf
Creating a Whatsapp Clone - Part IV - Transcript.pdf
Creating a Whatsapp Clone - Part IV.pdf
Creating a Whatsapp Clone - Part I - Transcript.pdf

Recently uploaded (20)

PDF
Approach and Philosophy of On baking technology
PDF
Machine learning based COVID-19 study performance prediction
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Encapsulation theory and applications.pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPT
Teaching material agriculture food technology
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
KodekX | Application Modernization Development
PDF
NewMind AI Weekly Chronicles - August'25 Week I
Approach and Philosophy of On baking technology
Machine learning based COVID-19 study performance prediction
20250228 LYD VKU AI Blended-Learning.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
sap open course for s4hana steps from ECC to s4
Mobile App Security Testing_ A Comprehensive Guide.pdf
Encapsulation_ Review paper, used for researhc scholars
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Diabetes mellitus diagnosis method based random forest with bat algorithm
Encapsulation theory and applications.pdf
Empathic Computing: Creating Shared Understanding
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Understanding_Digital_Forensics_Presentation.pptx
Teaching material agriculture food technology
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Spectral efficient network and resource selection model in 5G networks
Dropbox Q2 2025 Financial Results & Investor Presentation
KodekX | Application Modernization Development
NewMind AI Weekly Chronicles - August'25 Week I

Creating a Facebook Clone - Part XXII - Transcript.pdf

  • 1. Creating a Facebook Clone - Part XXII UserService is a big class and it makes sense to cover in two parts
  • 2. setProps(user, u); if (u.getPassword() != null) { u.setPassword(encoder.encode(user.getPassword())); } users.save(u); } public byte[] getAvatar(String userId) { User u = users.findById(userId).get(); if (u.getAvatar() != null) { return u.getAvatar().getData(); } return null; } public void setAvatar(String authToken, String mediaId) { Media m = medias.findById(mediaId).get(); User u = users.findByAuthtoken(authToken).get(0); u.setAvatar(m); users.save(u); } public void sendFriendRequest(String auth, String userId) { User me = users.findByAuthtoken(auth).get(0); User him = users.findById(userId).get(); UserService Next on the agenda is getAvatar, it returns the avatar picture of the user as a byte array
  • 3. setProps(user, u); if (u.getPassword() != null) { u.setPassword(encoder.encode(user.getPassword())); } users.save(u); } public byte[] getAvatar(String userId) { User u = users.findById(userId).get(); if (u.getAvatar() != null) { return u.getAvatar().getData(); } return null; } public void setAvatar(String authToken, String mediaId) { Media m = medias.findById(mediaId).get(); User u = users.findByAuthtoken(authToken).get(0); u.setAvatar(m); users.save(u); } public void sendFriendRequest(String auth, String userId) { User me = users.findByAuthtoken(auth).get(0); User him = users.findById(userId).get(); UserService Notice we need an id and not a token to get the avatar as anyone who is familiar with the user ID should be able to see the avatar
  • 4. setProps(user, u); if (u.getPassword() != null) { u.setPassword(encoder.encode(user.getPassword())); } users.save(u); } public byte[] getAvatar(String userId) { User u = users.findById(userId).get(); if (u.getAvatar() != null) { return u.getAvatar().getData(); } return null; } public void setAvatar(String authToken, String mediaId) { Media m = medias.findById(mediaId).get(); User u = users.findByAuthtoken(authToken).get(0); u.setAvatar(m); users.save(u); } public void sendFriendRequest(String auth, String userId) { User me = users.findByAuthtoken(auth).get(0); User him = users.findById(userId).get(); UserService If the avatar reference is null we return null instantly
  • 5. setProps(user, u); if (u.getPassword() != null) { u.setPassword(encoder.encode(user.getPassword())); } users.save(u); } public byte[] getAvatar(String userId) { User u = users.findById(userId).get(); if (u.getAvatar() != null) { return u.getAvatar().getData(); } return null; } public void setAvatar(String authToken, String mediaId) { Media m = medias.findById(mediaId).get(); User u = users.findByAuthtoken(authToken).get(0); u.setAvatar(m); users.save(u); } public void sendFriendRequest(String auth, String userId) { User me = users.findByAuthtoken(auth).get(0); User him = users.findById(userId).get(); UserService The setAvatar method works in a similar way
  • 6. setProps(user, u); if (u.getPassword() != null) { u.setPassword(encoder.encode(user.getPassword())); } users.save(u); } public byte[] getAvatar(String userId) { User u = users.findById(userId).get(); if (u.getAvatar() != null) { return u.getAvatar().getData(); } return null; } public void setAvatar(String authToken, String mediaId) { Media m = medias.findById(mediaId).get(); User u = users.findByAuthtoken(authToken).get(0); u.setAvatar(m); users.save(u); } public void sendFriendRequest(String auth, String userId) { User me = users.findByAuthtoken(auth).get(0); User him = users.findById(userId).get(); UserService Notice that unlike getAvatar with setAvatar we need a token as this is a set operation. This prevents other users from changing our avatar. When we map this to the webservice we need to first create a media entry and only then set the avatar value.
  • 7. u.setAvatar(m); users.save(u); } public void sendFriendRequest(String auth, String userId) { User me = users.findByAuthtoken(auth).get(0); User him = users.findById(userId).get(); him.getFriendRequests().add(me); users.save(him); notifications.sendNotification(him, new NotificationDAO(null, me.getDAO(), "wants to be friends", "uE7FC", 0x587EBE, 0, true, null, null)); } public void acceptFriendRequest(String auth, String userId) { User me = users.findByAuthtoken(auth).get(0); User him = users.findById(userId).get(); if (me.getFriendRequests().contains(him)) { me.getFriendRequests().remove(him); me.getFriends().add(him); him.getFriendRequests().remove(me); him.getFriends().add(me); users.saveAll(Arrays.asList(me, him)); UserService The friend request includes a few moving parts. First we need to send a request.
  • 8. u.setAvatar(m); users.save(u); } public void sendFriendRequest(String auth, String userId) { User me = users.findByAuthtoken(auth).get(0); User him = users.findById(userId).get(); him.getFriendRequests().add(me); users.save(him); notifications.sendNotification(him, new NotificationDAO(null, me.getDAO(), "wants to be friends", "uE7FC", 0x587EBE, 0, true, null, null)); } public void acceptFriendRequest(String auth, String userId) { User me = users.findByAuthtoken(auth).get(0); User him = users.findById(userId).get(); if (me.getFriendRequests().contains(him)) { me.getFriendRequests().remove(him); me.getFriends().add(him); him.getFriendRequests().remove(me); him.getFriends().add(me); users.saveAll(Arrays.asList(me, him)); UserService We need an auth token as this is a secure operation
  • 9. u.setAvatar(m); users.save(u); } public void sendFriendRequest(String auth, String userId) { User me = users.findByAuthtoken(auth).get(0); User him = users.findById(userId).get(); him.getFriendRequests().add(me); users.save(him); notifications.sendNotification(him, new NotificationDAO(null, me.getDAO(), "wants to be friends", "uE7FC", 0x587EBE, 0, true, null, null)); } public void acceptFriendRequest(String auth, String userId) { User me = users.findByAuthtoken(auth).get(0); User him = users.findById(userId).get(); if (me.getFriendRequests().contains(him)) { me.getFriendRequests().remove(him); me.getFriends().add(him); him.getFriendRequests().remove(me); him.getFriends().add(me); users.saveAll(Arrays.asList(me, him)); UserService We obviously don't have the auth token of the person we are "friending"
  • 10. u.setAvatar(m); users.save(u); } public void sendFriendRequest(String auth, String userId) { User me = users.findByAuthtoken(auth).get(0); User him = users.findById(userId).get(); him.getFriendRequests().add(me); users.save(him); notifications.sendNotification(him, new NotificationDAO(null, me.getDAO(), "wants to be friends", "uE7FC", 0x587EBE, 0, true, null, null)); } public void acceptFriendRequest(String auth, String userId) { User me = users.findByAuthtoken(auth).get(0); User him = users.findById(userId).get(); if (me.getFriendRequests().contains(him)) { me.getFriendRequests().remove(him); me.getFriends().add(him); him.getFriendRequests().remove(me); him.getFriends().add(me); users.saveAll(Arrays.asList(me, him)); UserService This is a special case where we modify his entity without an auth-token, that's a bit risky...
  • 11. u.setAvatar(m); users.save(u); } public void sendFriendRequest(String auth, String userId) { User me = users.findByAuthtoken(auth).get(0); User him = users.findById(userId).get(); him.getFriendRequests().add(me); users.save(him); notifications.sendNotification(him, new NotificationDAO(null, me.getDAO(), "wants to be friends", "uE7FC", 0x587EBE, 0, true, null, null)); } public void acceptFriendRequest(String auth, String userId) { User me = users.findByAuthtoken(auth).get(0); User him = users.findById(userId).get(); if (me.getFriendRequests().contains(him)) { me.getFriendRequests().remove(him); me.getFriends().add(him); him.getFriendRequests().remove(me); him.getFriends().add(me); users.saveAll(Arrays.asList(me, him)); UserService The notification service should notify the user that he got a new friend request. We'll dig into the notification service soon…
  • 12. public void sendFriendRequest(String auth, String userId) { User me = users.findByAuthtoken(auth).get(0); User him = users.findById(userId).get(); him.getFriendRequests().add(me); users.save(him); notifications.sendNotification(him, new NotificationDAO(null, me.getDAO(), "wants to be friends", "uE7FC", 0x587EBE, 0, true, null, null)); } public void acceptFriendRequest(String auth, String userId) { User me = users.findByAuthtoken(auth).get(0); User him = users.findById(userId).get(); if (me.getFriendRequests().contains(him)) { me.getFriendRequests().remove(him); me.getFriends().add(him); him.getFriendRequests().remove(me); him.getFriends().add(me); users.saveAll(Arrays.asList(me, him)); } } public void uploadContacts(String auth, List<ShadowUserDAO> contacts) { Long c = shadows.countByUser(auth); UserService But first lets check out the other side of the equation. The acceptFriendRequest method.
  • 13. public void sendFriendRequest(String auth, String userId) { User me = users.findByAuthtoken(auth).get(0); User him = users.findById(userId).get(); him.getFriendRequests().add(me); users.save(him); notifications.sendNotification(him, new NotificationDAO(null, me.getDAO(), "wants to be friends", "uE7FC", 0x587EBE, 0, true, null, null)); } public void acceptFriendRequest(String auth, String userId) { User me = users.findByAuthtoken(auth).get(0); User him = users.findById(userId).get(); if (me.getFriendRequests().contains(him)) { me.getFriendRequests().remove(him); me.getFriends().add(him); him.getFriendRequests().remove(me); him.getFriends().add(me); users.saveAll(Arrays.asList(me, him)); } } public void uploadContacts(String auth, List<ShadowUserDAO> contacts) { Long c = shadows.countByUser(auth); UserService This time me & him are flipped
  • 14. public void sendFriendRequest(String auth, String userId) { User me = users.findByAuthtoken(auth).get(0); User him = users.findById(userId).get(); him.getFriendRequests().add(me); users.save(him); notifications.sendNotification(him, new NotificationDAO(null, me.getDAO(), "wants to be friends", "uE7FC", 0x587EBE, 0, true, null, null)); } public void acceptFriendRequest(String auth, String userId) { User me = users.findByAuthtoken(auth).get(0); User him = users.findById(userId).get(); if (me.getFriendRequests().contains(him)) { me.getFriendRequests().remove(him); me.getFriends().add(him); him.getFriendRequests().remove(me); him.getFriends().add(me); users.saveAll(Arrays.asList(me, him)); } } public void uploadContacts(String auth, List<ShadowUserDAO> contacts) { Long c = shadows.countByUser(auth); UserService This checks against a potential weakness where a hacker could potentially call accept friend request for a person that didn't request it
  • 15. public void sendFriendRequest(String auth, String userId) { User me = users.findByAuthtoken(auth).get(0); User him = users.findById(userId).get(); him.getFriendRequests().add(me); users.save(him); notifications.sendNotification(him, new NotificationDAO(null, me.getDAO(), "wants to be friends", "uE7FC", 0x587EBE, 0, true, null, null)); } public void acceptFriendRequest(String auth, String userId) { User me = users.findByAuthtoken(auth).get(0); User him = users.findById(userId).get(); if (me.getFriendRequests().contains(him)) { me.getFriendRequests().remove(him); me.getFriends().add(him); him.getFriendRequests().remove(me); him.getFriends().add(me); users.saveAll(Arrays.asList(me, him)); } } public void uploadContacts(String auth, List<ShadowUserDAO> contacts) { Long c = shadows.countByUser(auth); UserService When a request is accepted we need to update the friend list on both sides. I didn't add a removeFriendRequest method which I probably should have added. I think it's trivial to add so I'm leaving it as an exercise.
  • 16. users.saveAll(Arrays.asList(me, him)); } } public void uploadContacts(String auth, List<ShadowUserDAO> contacts) { Long c = shadows.countByUser(auth); User u = users.findByAuthtoken(auth).get(0); if (c == null || c == 0) { List<ShadowUser> shadowList = new ArrayList<>(); for (ShadowUserDAO d : contacts) { shadowList.add(new ShadowUser(d, u)); updatePeopleYouMayKnow(d, u); } shadows.saveAll(shadowList); } else { for (ShadowUserDAO d : contacts) { List<ShadowUser> results = shadows.findByFullName(auth, d. getFullName()); if (!results.isEmpty()) { shadows.save(new ShadowUser(d, u)); } else { ShadowUser current = results.get(0); current.setEmail(d.getEmail()); current.setPhone(d.getPhone()); current.setSecondaryPhone(d.getSecondaryPhone()); shadows.save(current); UserService The last method for UserService is the `uploadContacts` method... It's invoked when the user uploads the contacts from his phone.
  • 17. users.saveAll(Arrays.asList(me, him)); } } public void uploadContacts(String auth, List<ShadowUserDAO> contacts) { Long c = shadows.countByUser(auth); User u = users.findByAuthtoken(auth).get(0); if (c == null || c == 0) { List<ShadowUser> shadowList = new ArrayList<>(); for (ShadowUserDAO d : contacts) { shadowList.add(new ShadowUser(d, u)); updatePeopleYouMayKnow(d, u); } shadows.saveAll(shadowList); } else { for (ShadowUserDAO d : contacts) { List<ShadowUser> results = shadows.findByFullName(auth, d. getFullName()); if (!results.isEmpty()) { shadows.save(new ShadowUser(d, u)); } else { ShadowUser current = results.get(0); current.setEmail(d.getEmail()); current.setPhone(d.getPhone()); current.setSecondaryPhone(d.getSecondaryPhone()); shadows.save(current); UserService If we uploaded contacts in the past its a good idea to try and merge with what we have. We can check that by running a count query
  • 18. users.saveAll(Arrays.asList(me, him)); } } public void uploadContacts(String auth, List<ShadowUserDAO> contacts) { Long c = shadows.countByUser(auth); User u = users.findByAuthtoken(auth).get(0); if (c == null || c == 0) { List<ShadowUser> shadowList = new ArrayList<>(); for (ShadowUserDAO d : contacts) { shadowList.add(new ShadowUser(d, u)); updatePeopleYouMayKnow(d, u); } shadows.saveAll(shadowList); } else { for (ShadowUserDAO d : contacts) { List<ShadowUser> results = shadows.findByFullName(auth, d. getFullName()); if (!results.isEmpty()) { shadows.save(new ShadowUser(d, u)); } else { ShadowUser current = results.get(0); current.setEmail(d.getEmail()); current.setPhone(d.getPhone()); current.setSecondaryPhone(d.getSecondaryPhone()); shadows.save(current); UserService For every contact we find we check to see if there is a user in the system and if so we add that user to the people you may know list
  • 19. users.saveAll(Arrays.asList(me, him)); } } public void uploadContacts(String auth, List<ShadowUserDAO> contacts) { Long c = shadows.countByUser(auth); User u = users.findByAuthtoken(auth).get(0); if (c == null || c == 0) { List<ShadowUser> shadowList = new ArrayList<>(); for (ShadowUserDAO d : contacts) { shadowList.add(new ShadowUser(d, u)); updatePeopleYouMayKnow(d, u); } shadows.saveAll(shadowList); } else { for (ShadowUserDAO d : contacts) { List<ShadowUser> results = shadows.findByFullName(auth, d. getFullName()); if (!results.isEmpty()) { shadows.save(new ShadowUser(d, u)); } else { ShadowUser current = results.get(0); current.setEmail(d.getEmail()); current.setPhone(d.getPhone()); current.setSecondaryPhone(d.getSecondaryPhone()); shadows.save(current); UserService When adding a lot of elements using saveAll is much faster than save
  • 20. if (c == null || c == 0) { List<ShadowUser> shadowList = new ArrayList<>(); for (ShadowUserDAO d : contacts) { shadowList.add(new ShadowUser(d, u)); updatePeopleYouMayKnow(d, u); } shadows.saveAll(shadowList); } else { for (ShadowUserDAO d : contacts) { List<ShadowUser> results = shadows.findByFullName(auth, d. getFullName()); if (!results.isEmpty()) { shadows.save(new ShadowUser(d, u)); } else { ShadowUser current = results.get(0); current.setEmail(d.getEmail()); current.setPhone(d.getPhone()); current.setSecondaryPhone(d.getSecondaryPhone()); shadows.save(current); updatePeopleYouMayKnow(d, u); } } } } private void updatePeopleYouMayKnow(ShadowUserDAO d, User u) { UserService For simplicity I chose to merge based on full name. In retrospect it might have been wiser to use the device unique ID for every contact but that might breed duplicates too. I oversimplified a very complex process of handling shadow users. If you take the time to build something like this then make sure to analyze the data more thoroughly and create a more robust social graph.
  • 21. current.setEmail(d.getEmail()); current.setPhone(d.getPhone()); current.setSecondaryPhone(d.getSecondaryPhone()); shadows.save(current); updatePeopleYouMayKnow(d, u); } } } } private void updatePeopleYouMayKnow(ShadowUserDAO d, User u) { User friend = findHim(d); if (friend != null) { if (!u.getFriends().contains(friend) && !u.getPeopleYouMayKnow().contains(friend)) { u.getPeopleYouMayKnow().add(friend); users.save(u); } } } private User findHim(ShadowUserDAO d) { if (d.getEmail() != null) { List<User> l = users.findByEmailIgnoreCase(d.getEmail()); if (l != null && !l.isEmpty()) { return l.get(0); UserService Notice that we still need to declare the updatePeopleYouMayKnow method we mentioned before
  • 22. current.setEmail(d.getEmail()); current.setPhone(d.getPhone()); current.setSecondaryPhone(d.getSecondaryPhone()); shadows.save(current); updatePeopleYouMayKnow(d, u); } } } } private void updatePeopleYouMayKnow(ShadowUserDAO d, User u) { User friend = findHim(d); if (friend != null) { if (!u.getFriends().contains(friend) && !u.getPeopleYouMayKnow().contains(friend)) { u.getPeopleYouMayKnow().add(friend); users.save(u); } } } private User findHim(ShadowUserDAO d) { if (d.getEmail() != null) { List<User> l = users.findByEmailIgnoreCase(d.getEmail()); if (l != null && !l.isEmpty()) { return l.get(0); UserService We search for the shadow user in the table of users using the findHim method which we'll cover soon
  • 23. current.setEmail(d.getEmail()); current.setPhone(d.getPhone()); current.setSecondaryPhone(d.getSecondaryPhone()); shadows.save(current); updatePeopleYouMayKnow(d, u); } } } } private void updatePeopleYouMayKnow(ShadowUserDAO d, User u) { User friend = findHim(d); if (friend != null) { if (!u.getFriends().contains(friend) && !u.getPeopleYouMayKnow().contains(friend)) { u.getPeopleYouMayKnow().add(friend); users.save(u); } } } private User findHim(ShadowUserDAO d) { if (d.getEmail() != null) { List<User> l = users.findByEmailIgnoreCase(d.getEmail()); if (l != null && !l.isEmpty()) { return l.get(0); UserService If a friend was found & isn't already a friend or in the "People You May Know" list...
  • 24. current.setEmail(d.getEmail()); current.setPhone(d.getPhone()); current.setSecondaryPhone(d.getSecondaryPhone()); shadows.save(current); updatePeopleYouMayKnow(d, u); } } } } private void updatePeopleYouMayKnow(ShadowUserDAO d, User u) { User friend = findHim(d); if (friend != null) { if (!u.getFriends().contains(friend) && !u.getPeopleYouMayKnow().contains(friend)) { u.getPeopleYouMayKnow().add(friend); users.save(u); } } } private User findHim(ShadowUserDAO d) { if (d.getEmail() != null) { List<User> l = users.findByEmailIgnoreCase(d.getEmail()); if (l != null && !l.isEmpty()) { return l.get(0); UserService Then we can add that user to the people you may know
  • 25. } } } private User findHim(ShadowUserDAO d) { if (d.getEmail() != null) { List<User> l = users.findByEmailIgnoreCase(d.getEmail()); if (l != null && !l.isEmpty()) { return l.get(0); } } if (d.getPhone() != null) { List<User> l = users.findByPhone(d.getPhone()); if (l != null && !l.isEmpty()) { return l.get(0); } } if (d.getSecondaryPhone() != null) { List<User> l = users.findByPhone(d.getSecondaryPhone()); if (l != null && !l.isEmpty()) { return l.get(0); } } return null; } } UserService The findHim method is simple boilerplate that searches based on all the meta data in the shadow user. We use findByEmail/phone etc. to find the shadow user in our regular user database. With that we finished the UserService class which is the largest service class!