Skip to content

Commit

Permalink
Merge branch 'friends' into sualeh/add-friend-ui
Browse files Browse the repository at this point in the history
  • Loading branch information
Sualeh Asif committed Jun 29, 2022
2 parents 5055691 + dd95b76 commit 6b4e2d2
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 97 deletions.
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

git_repository(
name = "asphr",
commit = "785fc95512b692444972318b79dbcb81daaf613d", # autoupdate anysphere/asphr
commit = "7cf56b781c72c6d3c5570031460db4209353cbab", # autoupdate anysphere/asphr
init_submodules = True,
remote = "https://github.com/anysphere/asphr.git",
)
Expand Down
2 changes: 1 addition & 1 deletion daemon/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("@cxx.rs//tools/bazel:rust_cxx_bridge.bzl", "rust_cxx_bridge")
load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_static_library", "rust_test")
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_static_library", "rust_test")

rust_binary(
name = "main",
Expand Down
2 changes: 0 additions & 2 deletions daemon/crypto/crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,6 @@ auto encrypt_async_friend_request(const string& self_id,
return absl::UnknownError("failed to pad message");
}

cout << "Friend Request Plaintext: " << plaintext << endl;

// encrypt it!
std::string ciphertext;
unsigned long long ciphertext_size = plaintext_size + crypto_box_MACBYTES;
Expand Down
130 changes: 85 additions & 45 deletions daemon/rpc/daemon_rpc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ Status DaemonRpc::GetFriendList(
new_friend->set_unique_name(std::string(s.unique_name));
new_friend->set_display_name(std::string(s.display_name));
new_friend->set_public_id(std::string(s.public_id));
new_friend->set_request_progress(
asphrdaemon::FriendRequestProgress::Complete);
new_friend->set_invitation_progress(
asphrdaemon::InvitationProgress::Complete);
}
} catch (const rust::Error& e) {
ASPHR_LOG_ERR("Database failed.", error, e.what(), rpc_call,
Expand Down Expand Up @@ -237,6 +237,12 @@ auto DaemonRpc::convertStructDBtoRPC(const db::Friend& db_friend,
friend_info.set_unique_name(std::string(db_friend.unique_name));
friend_info.set_display_name(std::string(db_friend.display_name));
friend_info.set_public_id(std::string(db_friend.public_id));

// WARNING: this is SkEtCHy!!
// TODO(sualeh): can we cast this with a proper function on either of the
// structs.
friend_info.set_invitation_progress(
static_cast<asphrdaemon::InvitationProgress>(db_friend.request_progress));
return std::pair(friend_info, std::string(db_address.friend_request_message));
}

Expand Down Expand Up @@ -318,18 +324,31 @@ grpc::Status DaemonRpc::AddAsyncFriend(
return Status::OK;
}

Status DaemonRpc::GetOutgoingFriendRequests(
grpc::Status DaemonRpc::GetOutgoingSyncInvitations(
grpc::ServerContext* context,
const asphrdaemon::GetOutgoingSyncInvitationsRequest*
getOutgoingSyncInvitationsRequest,
asphrdaemon::GetOutgoingSyncInvitationsResponse*
getOutgoingSyncInvitationsResponse) {
ASPHR_LOG_INFO("GetOutgoingSyncInvitations() called.", rpc_call,
"GetOutgoingSyncInvitations");

// unimplemented
return Status(grpc::StatusCode::UNIMPLEMENTED, "not implemented");
}

Status DaemonRpc::GetOutgoingAsyncInvitations(
grpc::ServerContext* context,
const asphrdaemon::GetOutgoingFriendRequestsRequest*
getOutgoingFriendRequestsRequest,
asphrdaemon::GetOutgoingFriendRequestsResponse*
getOutgoingFriendRequestsResponse) {
ASPHR_LOG_INFO("GetOutgoingFriendRequests() called.", rpc_call,
"GetOutgoingFriendRequests");
const asphrdaemon::GetOutgoingAsyncInvitationsRequest*
getOutgoingAsyncInvitationsRequest,
asphrdaemon::GetOutgoingAsyncInvitationsResponse*
getOutgoingAsyncInvitationsResponse) {
ASPHR_LOG_INFO("GetOutgoingAsyncInvitations() called.", rpc_call,
"GetOutgoingAsyncInvitations");

if (!G.db->has_registered()) {
ASPHR_LOG_INFO("Need to register first.", rpc_call,
"GetOutgoingFriendRequests");
"GetOutgoingAsyncInvitations");
return Status(grpc::StatusCode::UNAUTHENTICATED, "not registered");
}

Expand All @@ -343,15 +362,30 @@ Status DaemonRpc::GetOutgoingFriendRequests(
auto conversion_result = convertStructDBtoRPC(db_friend, address);
if (!conversion_result.ok()) {
ASPHR_LOG_ERR("Failed to convert DB to RPC.", rpc_call,
"GetOutgoingAsyncFriendRequests");
"GetOutgoingAsyncInvitations");
return Status(grpc::StatusCode::UNKNOWN, "failed to convert DB to RPC");
}
auto [friend_info, message] = conversion_result.value();
// add to response
auto friend_request =
getOutgoingFriendRequestsResponse->add_friend_requests();
friend_request->mutable_friend_info()->CopyFrom(friend_info);
friend_request->set_message(message);
auto invitation = getOutgoingAsyncInvitationsResponse->add_invitations();

/**
* message OutgoingAsyncInvitationInfo {
string unique_name = 1;
string display_name = 2;
string public_id = 3;
string message = 4;
google.protobuf.Timestamp sent_at = 5;
}
*/
invitation->set_unique_name(friend_info.unique_name());
invitation->set_display_name(friend_info.display_name());
invitation->set_public_id(friend_info.public_id());
invitation->set_message(message);
// set now
// invitation->set_sent_at(
// google::protobuf::util::TimeUtil::SecondsToTimestamp(
// std::time(nullptr)));
}
} catch (const rust::Error& e) {
ASPHR_LOG_ERR("Failed to get outgoing friend requests.", error, e.what(),
Expand All @@ -362,12 +396,12 @@ Status DaemonRpc::GetOutgoingFriendRequests(
return Status::OK;
}

Status DaemonRpc::GetIncomingAsyncFriendRequests(
Status DaemonRpc::GetIncomingAsyncInvitations(
grpc::ServerContext* context,
const asphrdaemon::GetIncomingAsyncFriendRequestsRequest*
getIncomingAsyncFriendRequestsRequest,
asphrdaemon::GetIncomingAsyncFriendRequestsResponse*
getIncomingAsyncFriendRequestsResponse) {
const asphrdaemon::GetIncomingAsyncInvitationsRequest*
getIncomingAsyncInvitationsRequest,
asphrdaemon::GetIncomingAsyncInvitationsResponse*
getIncomingAsyncInvitationsResponse) {
// clone of the above
try {
// call rust db to get all incoming friend requests
Expand All @@ -384,10 +418,22 @@ Status DaemonRpc::GetIncomingAsyncFriendRequests(
}
auto [friend_info, message] = conversion_result.value();
// add to response
auto friend_request =
getIncomingAsyncFriendRequestsResponse->add_friend_requests();
friend_request->mutable_friend_info()->CopyFrom(friend_info);
friend_request->set_message(message);
auto invitation = getIncomingAsyncInvitationsResponse->add_invitations();

/**
* message IncomingAsyncInvitationInfo {
string public_id = 1;
string message = 2;
google.protobuf.Timestamp received_at = 3;
}
*/

invitation->set_public_id("friend_info.public_id");
invitation->set_message(message);
// set now
// invitation->set_received_at(
// google::protobuf::util::TimeUtil::SecondsToTimestamp(
// std::time(nullptr)));
}
} catch (const rust::Error& e) {
ASPHR_LOG_ERR("Failed to get incoming friend requests.", error, e.what(),
Expand All @@ -397,28 +443,22 @@ Status DaemonRpc::GetIncomingAsyncFriendRequests(
return Status::OK;
}

Status DaemonRpc::DecideAsyncFriendRequest(
Status DaemonRpc::AcceptAsyncInvitation(
grpc::ServerContext* context,
const asphrdaemon::DecideAsyncFriendRequestRequest*
decideAsyncFriendRequestRequest,
asphrdaemon::DecideAsyncFriendRequestResponse*
decideAsyncFriendRequestResponse) {
try {
if (decideAsyncFriendRequestRequest->accept()) {
// call rust db to accept the friend request
G.db->approve_async_friend_request(
decideAsyncFriendRequestRequest->unique_name(), MAX_FRIENDS);
} else {
// call rust db to reject the friend request
G.db->deny_async_friend_request(
decideAsyncFriendRequestRequest->unique_name());
}
} catch (const rust::Error& e) {
ASPHR_LOG_ERR("Failed to decide async friend request.", error, e.what(),
rpc_call, "DecideAsyncFriendRequest");
return Status(grpc::StatusCode::UNKNOWN, e.what());
}
return Status::OK;
const asphrdaemon::AcceptAsyncInvitationRequest*
acceptAsyncInvitationRequest,
asphrdaemon::AcceptAsyncInvitationResponse* acceptAsyncInvitationResponse) {
// unimplemented
return Status(grpc::StatusCode::UNIMPLEMENTED, "not implemented");
}

Status DaemonRpc::RejectAsyncInvitation(
grpc::ServerContext* context,
const asphrdaemon::RejectAsyncInvitationRequest*
rejectAsyncInvitationRequest,
asphrdaemon::RejectAsyncInvitationResponse* rejectAsyncInvitationResponse) {
// unimplemented
return Status(grpc::StatusCode::UNIMPLEMENTED, "not implemented");
}

Status DaemonRpc::RemoveFriend(
Expand Down
60 changes: 39 additions & 21 deletions daemon/rpc/daemon_rpc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,22 @@ class DaemonRpc final : public asphrdaemon::Daemon::Service {
const asphrdaemon::RegisterUserRequest* registerUserRequest,
asphrdaemon::RegisterUserResponse* registerUserResponse) override;

grpc::Status GetMyPublicID(
grpc::ServerContext* context,
const asphrdaemon::GetMyPublicIDRequest* getMyPublicIDRequest,
asphrdaemon::GetMyPublicIDResponse* getMyPublicIDResponse) override;

grpc::Status GetFriendList(
grpc::ServerContext* context,
const asphrdaemon::GetFriendListRequest* getFriendListRequest,
asphrdaemon::GetFriendListResponse* getFriendListResponse) override;

grpc::Status GetMyPublicID(
grpc::Status RemoveFriend(
grpc::ServerContext* context,
const asphrdaemon::GetMyPublicIDRequest* getMyPublicIDRequest,
asphrdaemon::GetMyPublicIDResponse* getMyPublicIDResponse) override;
const asphrdaemon::RemoveFriendRequest* removeFriendRequest,
asphrdaemon::RemoveFriendResponse* removeFriendResponse) override;

// Invitations (async + sync) section. Sync \similarTo InPerson.
grpc::Status AddSyncFriend(
grpc::ServerContext* context,
const asphrdaemon::AddSyncFriendRequest* addSyncFriendRequest,
Expand All @@ -41,37 +47,47 @@ class DaemonRpc final : public asphrdaemon::Daemon::Service {
const asphrdaemon::AddAsyncFriendRequest* addAsyncFriendRequest,
asphrdaemon::AddAsyncFriendResponse* addAsyncFriendResponse) override;

grpc::Status GetOutgoingFriendRequests(
grpc::Status GetOutgoingSyncInvitations(
grpc::ServerContext* context,
const asphrdaemon::GetOutgoingFriendRequestsRequest*
getOutgoingFriendRequestsRequest,
asphrdaemon::GetOutgoingFriendRequestsResponse*
getOutgoingFriendRequestsResponse) override;
const asphrdaemon::GetOutgoingSyncInvitationsRequest*
getOutgoingSyncInvitationsRequest,
asphrdaemon::GetOutgoingSyncInvitationsResponse*
getOutgoingSyncInvitationsResponse) override;

grpc::Status GetIncomingAsyncFriendRequests(
grpc::Status GetOutgoingAsyncInvitations(
grpc::ServerContext* context,
const asphrdaemon::GetIncomingAsyncFriendRequestsRequest*
getIncomingAsyncFriendRequestsRequest,
asphrdaemon::GetIncomingAsyncFriendRequestsResponse*
getIncomingAsyncFriendRequestsResponse) override;
const asphrdaemon::GetOutgoingAsyncInvitationsRequest*
getOutgoingAsyncInvitationsRequest,
asphrdaemon::GetOutgoingAsyncInvitationsResponse*
getOutgoingAsyncInvitationsResponse) override;

grpc::Status DecideAsyncFriendRequest(
grpc::Status GetIncomingAsyncInvitations(
grpc::ServerContext* context,
const asphrdaemon::DecideAsyncFriendRequestRequest*
decideAsyncFriendRequestRequest,
asphrdaemon::DecideAsyncFriendRequestResponse*
decideAsyncFriendRequestResponse) override;
const asphrdaemon::GetIncomingAsyncInvitationsRequest*
getIncomingAsyncInvitationsRequest,
asphrdaemon::GetIncomingAsyncInvitationsResponse*
getIncomingAsyncInvitationsResponse) override;

grpc::Status RemoveFriend(
grpc::Status AcceptAsyncInvitation(
grpc::ServerContext* context,
const asphrdaemon::RemoveFriendRequest* removeFriendRequest,
asphrdaemon::RemoveFriendResponse* removeFriendResponse) override;
const asphrdaemon::AcceptAsyncInvitationRequest*
acceptAsyncInvitationRequest,
asphrdaemon::AcceptAsyncInvitationResponse* acceptAsyncInvitationResponse)
override;

grpc::Status RejectAsyncInvitation(
grpc::ServerContext* context,
const asphrdaemon::RejectAsyncInvitationRequest*
rejectAsyncInvitationRequest,
asphrdaemon::RejectAsyncInvitationResponse* rejectAsyncInvitationResponse)
override;

grpc::Status SendMessage(
grpc::ServerContext* context,
const asphrdaemon::SendMessageRequest* sendMessageRequest,
asphrdaemon::SendMessageResponse* sendMessageResponse) override;

// Get Messages section
grpc::Status GetMessages(
grpc::ServerContext* context,
const asphrdaemon::GetMessagesRequest* getMessagesRequest,
Expand All @@ -98,6 +114,8 @@ class DaemonRpc final : public asphrdaemon::Daemon::Service {
const asphrdaemon::MessageSeenRequest* messageSeenRequest,
asphrdaemon::MessageSeenResponse* messageSeenResponse) override;

// GetStatus returns the status of the daemon. Things like how long since the
// last send/receive cycle, etc
grpc::Status GetStatus(
grpc::ServerContext* context,
const asphrdaemon::GetStatusRequest* getStatusRequest,
Expand Down
25 changes: 16 additions & 9 deletions daemon/transmitter/transmitter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -343,17 +343,11 @@ auto Transmitter::retrieve() -> void {
// Crawl the async friend request database
// TODO: we could accelerate / deccelerate this as the client desires/
// by changing ASYNC_FRIEND_REQUEST_BATCH_SIZE
int start_index = next_async_friend_request_retrieve_index;
int end_index = std::min(next_async_friend_request_retrieve_index +
ASYNC_FRIEND_REQUEST_BATCH_SIZE,
CLIENT_DB_ROWS);
auto [start_index, end_index] = update_async_invitation_retrieve_index();

// call the server to retrieve the async friend requests
retrieve_async_friend_request(start_index, end_index);
if (end_index == CLIENT_DB_ROWS) {
next_async_friend_request_retrieve_index = 0;
} else {
next_async_friend_request_retrieve_index = end_index;
}

check_rep();
}

Expand Down Expand Up @@ -458,6 +452,7 @@ auto Transmitter::send() -> void {
server_status_code, status.error_code(),
server_status_message, status.error_message());
}

transmit_async_friend_request();
check_rep();
}
Expand Down Expand Up @@ -633,6 +628,10 @@ auto Transmitter::retrieve_async_friend_request(int start_index, int end_index)
return;
}
std::map<string, db::Friend> friends = {};

ASPHR_LOG_INFO("Retrieved async friend requests from server.",
"requests_size", reply.requests_size());

for (int i = 0; i < reply.requests_size(); i++) {
// Step 2.1: test if the friend request is meant for us
// For now, we attach the friend_public_key along with every request.
Expand Down Expand Up @@ -726,6 +725,7 @@ auto Transmitter::retrieve_async_friend_request(int start_index, int end_index)
// ASPHR_LOG_ERR("Could not find friend ID.", "Error", e.what());
continue;
}

// Step 2.4: if we get here, then we have a new friend request
// we need to create a new friend entry in the database
// Note: the current design decision is to not transmit the actual name of
Expand All @@ -748,13 +748,20 @@ auto Transmitter::retrieve_async_friend_request(int start_index, int end_index)
.read_key = string_to_rust_u8Vec(read_key),
.write_key = string_to_rust_u8Vec(write_key),
};

try {
G.db->add_incoming_async_friend_requests(new_friend, new_address);
} catch (const rust::Error& e) {
ASPHR_LOG_ERR("Could not add friend.", Error, e.what());
continue;
}
}

if (end_index == CLIENT_DB_ROWS) {
next_async_friend_request_retrieve_index = 0;
} else {
next_async_friend_request_retrieve_index = end_index;
}
}

auto Transmitter::check_rep() const noexcept -> void {
Expand Down
Loading

0 comments on commit 6b4e2d2

Please sign in to comment.