commit 1908eb0f1294f6fe926e9b74e3d751387d3b3275
parent b444a1349e20bdb6e5d70c0a07d1b3151563ed44
Author: kyle <kyle@getaddrinfo.net>
Date: Sun, 29 Nov 2015 12:41:37 -0700
sl: implement add_friend message
- algorithm for checking mutual friendship is straighforward
- first check if added friend has a device id
- if they do, then check if you are in their list of friends
- if you are, then it's a mutual friendship, and add a row to mutual
friendship table
- if no to any of above, just add friend to friend_map and continue
- added new test
Diffstat:
4 files changed, 99 insertions(+), 5 deletions(-)
diff --git a/sl b/sl
@@ -1,4 +1,5 @@
#!/usr/bin/perl
+$| = 1;
use warnings;
use strict;
@@ -304,21 +305,39 @@ sub msg_leave_list
# update friend map
sub msg_add_friend
{
- my ($dbh, $new_sock, $addr, $msg) = @_;
+ my ($dbh, $sth_ref, $new_sock, $addr, $msg) = @_;
+ my %sth = %$sth_ref;
# device id followed by 1 or more friends numbers
my ($device_id, $friend) = split("\0", $msg);
- return if (device_id_invalid($dbh, $device_id, $addr));
- print "$addr: '$device_id' adding '$friend'\n";
+ return if (device_id_invalid($dbh, $sth_ref, $device_id, $addr));
+ my $devid_fp = fingerprint($device_id);
+ print "$addr: '$devid_fp' adding '$friend'\n";
unless (looks_like_number($friend)) {
print "$addr: bad friends number $friend\n";
return;
}
- # $friends_map_sth->execute($device_id, $_);
- # print "$addr: added friend $_\n";
+ # XXX: check they're not already a friend before doing this
+ $sth{friends_map}->execute($device_id, $friend);
+
+ # check if this added friend is a member already
+ my ($fr_devid) = $dbh->selectrow_array($sth{ph_num_exists}, undef, $friend);
+ if ($fr_devid) {
+ print "$addr: added friend is a member\n";
+ print "$addr: friends device id is '$fr_devid'\n";
+
+ my $phnum = get_phone_number($dbh, $sth_ref, $device_id);
+
+ # check if my phone number is in their friends list
+ if ($dbh->selectrow_array($sth{friends_map_select}, undef, $fr_devid, $phnum)) {
+ print "$addr: found mutual friendship\n";
+ $sth{mutual_friend_insert}->execute($device_id, $fr_devid);
+ $sth{mutual_friend_insert}->execute($fr_devid, $device_id);
+ }
+ }
my $out = "$friend";
print $new_sock pack("nn", $msg_num{add_friend}, length($out));
diff --git a/tests/mutual_friends_basic/Makefile b/tests/mutual_friends_basic/Makefile
@@ -0,0 +1 @@
+include ../test.mk
diff --git a/tests/mutual_friends_basic/server.log.good b/tests/mutual_friends_basic/server.log.good
@@ -0,0 +1,19 @@
+new connection (pid = <digits>)
+added new device <digits> <base64>
+new connection (pid = <digits>)
+added new device <digits> <base64>
+<base64> adding <digits>
+added friend is a member
+friends device id is <base64>
+<base64> adding <digits>
+added friend is a member
+friends device id is <base64>
+found mutual friendship
+<string>
+adding first list member devid = <base64>
+list fingerprint = <base64>
+gathering lists for <base64>
+found mutual friend <base64>
+found mutual friends list <string>
+disconnected!
+disconnected!
diff --git a/tests/mutual_friends_basic/test.pl b/tests/mutual_friends_basic/test.pl
@@ -0,0 +1,55 @@
+#!/usr/bin/perl -I../
+use strict;
+use warnings;
+use test;
+
+# this test:
+# - gets two new device id's
+# - the two devices add each other mutually
+# - device 1 creates a new list
+# - then verify that device 2 can see it
+
+my $sock_1 = new_socket();
+my $phnum_1 ="4038675309";
+send_msg($sock_1, 'new_device', $phnum_1);
+my (undef, $device_id1, undef) = recv_msg($sock_1);
+
+my $sock_2 = new_socket();
+my $phnum_2 = "4037082094";
+send_msg($sock_2, 'new_device', $phnum_2);
+my (undef, $device_id2, undef) = recv_msg($sock_2);
+
+# the mutual friend relationship, computer style
+send_msg($sock_1, 'add_friend', "$device_id1\0$phnum_2");
+recv_msg($sock_1);
+send_msg($sock_2, 'add_friend', "$device_id2\0$phnum_1");
+recv_msg($sock_2);
+
+my $list_name = "this is a new list";
+send_msg($sock_1, 'new_list', "$device_id1\0$list_name");
+my (undef, $list_data, undef) = recv_msg($sock_1);
+my ($list_id) = split("\0", $list_data);
+
+# make sure socket 2 can see socket 1's list
+send_msg($sock_2, 'list_request', $device_id2);
+my (undef, $request_data, $request_len) = recv_msg($sock_2);
+my (undef, $other) = split("\0\0", $request_data);
+
+my $num_lists = 0;
+for my $l (split("\0", $other)) {
+ my ($name, $id, @members) = split(":", $l);
+ unless ($name && $id && @members) {
+ fail "response didn't send at least 3 fields";
+ }
+ if ($list_id ne $id) {
+ fail "recevied list id '$id' different than sent '$list_id'!";
+ }
+ if (@members != 1) {
+ fail "expected 1 list member, got " . scalar @members . "\n";
+ }
+ if ($members[0] ne $phnum_1) {
+ fail "unexpected list member '$members[0]'";
+ }
+ $num_lists++;
+}
+fail "expected 1 indirect list, got $num_lists\n" if ($num_lists != 1);