Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Communicate with remote servers #160

Merged
merged 4 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 27 additions & 23 deletions web-api/Controllers/ContactsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ public class ContactsController : ControllerBase
{
private readonly IUsersService _usersService;
private readonly IChatsService _chatsService;
private readonly HttpClient _httpClient = new HttpClient();

private readonly JsonSerializerOptions _jsonSerializerOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};

public ContactsController(IUsersService usersService, IChatsService chatsService)
{
Expand Down Expand Up @@ -73,10 +79,10 @@ public IActionResult SignUp([FromBody] JsonElement body)
}

// Create new user
User newUser = new User(username, name, "localhost", password, profilePicture);
var newUser = new User(username, name, "localhost", password, profilePicture);
_usersService.Add(newUser);

return Ok();
return Created("", null);
}


Expand All @@ -91,8 +97,8 @@ public IActionResult Get()
return NotFound();
}

List<string> contacsIds = currentUser.Chats.Keys.ToList();
return Ok(_usersService.Get(contacsIds));
List<string> contactsIds = currentUser.Chats.Keys.ToList();
return Ok(_usersService.Get(contactsIds));
}

[HttpPost]
Expand Down Expand Up @@ -165,10 +171,14 @@ public IActionResult Post([FromBody] JsonElement body)
}
else
{
// TODO: add new chat to contact on a remote server
// Send an invitation to the contact on the remote server
var invitation = new Invitation(currentUser.Username, contact.Username, "localhost");
var json = JsonSerializer.Serialize(invitation, _jsonSerializerOptions);
var stringContent = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
_httpClient.PostAsync("https://" + contact.Server + "/api/invitations", stringContent).Wait();
}

return Ok();
return Created("", null);
}

[HttpGet("{id}")]
Expand Down Expand Up @@ -264,7 +274,7 @@ public IActionResult Put(string id, [FromBody] JsonElement body)
return NotFound();
}

return Ok();
return NoContent();
}

[HttpDelete("{id}")]
Expand Down Expand Up @@ -303,7 +313,7 @@ public IActionResult Delete(string id)
// Delete the currentUser from the contact
contact.Chats.Remove(currentUser.Username);
_usersService.Update(contact);
return Ok();
return NoContent();
}

[HttpGet("{id}/messages")]
Expand Down Expand Up @@ -383,11 +393,14 @@ public IActionResult PostNewMessage(string id, [FromBody] string content)
// Add the new message to the contact if on a remote server
if (contact.Server != "localhost")
{
// TODO: add new message to contact on a remote server
// Send transfer request
var transfer = new Transfer(currentUser.Username, contact.Username, content);
var json = JsonSerializer.Serialize(transfer, _jsonSerializerOptions);
var stringContent = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
_httpClient.PostAsync("https://" + contact.Server + "/api/transfer", stringContent).Wait();
}

return Ok();
return Created("", null);
}

[HttpGet("{id}/messages/{id2}")]
Expand Down Expand Up @@ -468,13 +481,9 @@ public IActionResult PutMessage(string id, int id2, [FromBody] string content)
// Update the chat
Chat chat = currentUser.Chats[id];
_chatsService.Update(chat);
// Update the contact if on a remote server
if (contact.Server != "localhost")
{
// TODO: update message on a remote server
}
// No API exists for updating the contact if on a remote server

return Ok();
return NoContent();
}

[HttpDelete("{id}/messages/{id2}")]
Expand Down Expand Up @@ -516,12 +525,7 @@ public IActionResult DeleteMessage(string id, int id2)
Chat chat = currentUser.Chats[id];
chat.Messages.Remove(message);
_chatsService.Update(chat);
// Delete the message from the contact if on a remote server
if (contact.Server != "localhost")
{
// TODO: delete message from contact on a remote server
}

return Ok();
// No API exists for deleting a message from the contact if on a remote server
return NoContent();
}
}
2 changes: 1 addition & 1 deletion web-api/Controllers/InvitationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ public IActionResult Post([FromBody] Invitation invitation)
_usersService.Update(remoteUser);
}

return Ok();
return Created("", null);
}
}
2 changes: 1 addition & 1 deletion web-api/Controllers/TransferController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ public IActionResult Post([FromBody] Transfer transfer)
chat.Messages.Add(message);
_chatsService.Update(chat);

return Ok();
return Created("", null);
}
}