diff --git a/frontend/static/js/api/MyProfile.js b/frontend/static/js/api/MyProfile.js index 549f116..ef2e694 100644 --- a/frontend/static/js/api/MyProfile.js +++ b/frontend/static/js/api/MyProfile.js @@ -18,7 +18,7 @@ class MyProfile extends Profile /** * @type {[Profile]} */ - this.friends = []; + this.friendList = []; /** * @type {[Profile]} */ @@ -46,7 +46,7 @@ class MyProfile extends Profile async getFriends() { const response = await this.client._get('/api/profiles/friends'); const data = await response.json(); - data.forEach(profileData => this.friends.push(new Profile(this.client, profileData.username, profileData.user_id, profileData.avatar))); + data.forEach(profileData => this.friendList.push(new Profile(this.client, profileData.username, profileData.user_id, profileData.avatar))); } async getIncomingFriendRequests() { const response = await this.client._get('/api/profiles/incoming_friend_requests'); @@ -62,6 +62,52 @@ class MyProfile extends Profile new Profile(this.client, profileData.username, profileData.user_id, profileData.avatar) )); } + + /** + * @param {Profile} profile + * @returns {Boolean} + */ + _isFriend(profile) { + for (const user of this.friendList) { + if (user.id === profile.id) + return true; + } + return false; + } + /** + * @param {Profile} profile + * @returns {Boolean} + */ + _isBlocked(profile) { + for (const user of this.blockedUsers) { + if (user.id === profile.id) + return true; + } + return false; + } + /** + * @param {Profile} profile + * @returns {Boolean} + */ + _hasIncomingRequestFrom(profile) { + for (const user of this.incomingFriendRequests) { + if (user.id === profile.id) + return true; + } + return false; + } + /** + * @param {Profile} profile + * @returns {Boolean} + */ + _hasOutgoingRequestTo(profile) { + for (const user of this.outgoingFriendRequests) { + if (user.id === profile.id) + return true; + } + return false; + } + /** * * @param {File} selectedFile diff --git a/frontend/static/js/api/Profile.js b/frontend/static/js/api/Profile.js index bd38d9f..feb9570 100644 --- a/frontend/static/js/api/Profile.js +++ b/frontend/static/js/api/Profile.js @@ -33,8 +33,10 @@ export class Profile extends AExchangeable /** * @type {Boolean} */ - this.isBlocked = false; - this.isFriend = false; + this.isFriend; + this.isBlocked; + this.hasIncomingRequest; + this.hasOutgoingRequest; } /** @@ -57,6 +59,13 @@ export class Profile extends AExchangeable this.username = response_data.username; this.avatar = response_data.avatar; + if (!this.client.me || this.client.me.id === this.id) + return; + + this.isFriend = this.client.me._isFriend(this); + this.isBlocked = this.client.me._isBlocked(this); + this.hasIncomingRequest = this.client.me._hasIncomingRequestFrom(this); + this.hasOutgoingRequest = this.client.me._hasOutgoingRequestTo(this); } /** diff --git a/frontend/static/js/api/Profiles.js b/frontend/static/js/api/Profiles.js index 7c0de7e..796abc7 100644 --- a/frontend/static/js/api/Profiles.js +++ b/frontend/static/js/api/Profiles.js @@ -54,40 +54,6 @@ class Profiles return null; return profile; } - - /** - * Block a user - * @param {Number} user_id - * @returns {Promise} - */ - async block(user_id) { - - // blocker & blocked - let response = await this.client._post("/api/profiles/block", { - users_id:[this.client.me.id, user_id], - }); - - let data = await response.json(); - return data; - - } - - /** - * Unblock a user - * @param {Number} user_id - * @returns {Promise} - */ - async deblock(user_id) { - - // blocker & blocked - let response = await this.client._delete("/api/profiles/block", { - users_id:[this.client.me.id, user_id], - }); - - let data = await response.json(); - return data; - - } } export {Profiles}; diff --git a/frontend/static/js/views/ProfilePageView.js b/frontend/static/js/views/ProfilePageView.js index 930e206..8b27e81 100644 --- a/frontend/static/js/views/ProfilePageView.js +++ b/frontend/static/js/views/ProfilePageView.js @@ -8,7 +8,7 @@ export default class extends AbstractView { } setTitle() { - document.title = `${this.username} - Profile`; + document.title = this.titleKey; } async postInit() @@ -16,100 +16,32 @@ export default class extends AbstractView { if (!this.profile) return 404; - this.userId = this.profile.id; + const addFriendButton = document.getElementById('addFriendButton'), + removeFriendButton = document.getElementById('removeFriendButton'), + blockButton = document.getElementById('blockButton'), + unblockButton = document.getElementById('unblockButton'); - await this.blockButton(); - await this.friendButton(); + if (this.profile.hasIncomingRequest) { + addFriendButton.classList.remove('d-none'); + addFriendButton.innerHTML = 'Accept Request'; + } else if (this.profile.hasOutgoingRequest) { + removeFriendButton.classList.remove('d-none'); + removeFriendButton.classList.replace('btn-danger', 'btn-secondary'); + removeFriendButton.innerHTML = 'Cancel Request' + } else if (this.profile.isFriend) + removeFriendButton.classList.remove('d-none'); + else + addFriendButton.classList.remove('d-none'); - client.notice.rewrite_profile = async () => { - await this.profile.getFriend(); - await this.profile.getBlock(); - await this.friendButton(); - }; - } + if (this.profile.isBlocked) + unblockButton.classList.remove('d-none'); + else + blockButton.classList.remove('d-none'); - async blockButton() { - // Block option - if (await client.isAuthenticated() === false) - return; - - if (client.me.id != this.userId) { - let block = document.getElementById("block"); - if (block == undefined) { - block = document.createElement("p"); - // this.info.appendChild(block); - } - - block.id = "block"; - block.onclick = async () => { - if (!this.profile.isBlocked) - await client.profiles.block(this.userId); - else - await client.profiles.deblock(this.userId); - this.profile = await client.profiles.getProfile(this.username); - - this.blockButton(); - }; - if (this.profile.isBlocked) - block.textContent = lang.get('profileUnblock', 'Unblock'); - else - block.textContent = lang.get('profileBlock', 'Block'); - } - } - - async friendButton() { - if (await client.isAuthenticated() === false) - return; - - if (client.me.id != this.userId) { - let yes = document.getElementById("yes") || document.createElement("p"); - let no = document.getElementById("no") || document.createElement("p"); - let friend = document.getElementById("friend") || document.createElement("p"); - - if (client.notice.data.asker.includes(this.userId)) { - - if (friend) - friend.remove(); - - yes.id = "yes"; - yes.textContent = lang.get('profileAcceptRequest', 'Accept Friend'); - yes.onclick = async () => { - client.notice.accept_friend(this.userId); - }; - - no.id = "no"; - no.textContent = lang.get('profileDenyRequest', 'Decline Friend'); - no.onclick = async () => { - client.notice.refuse_friend(this.userId); - }; - - // this.info.appendChild(yes); - // this.info.appendChild(document.createTextNode(" ")); - // this.info.appendChild(no); - - } - else { - - if (yes && no) - yes.remove(); no.remove(); - - friend.id = "friend"; - friend.onclick = async () => { - if (this.profile.isFriend) - await client.notice.remove_friend(this.userId); - else - await client.notice.ask_friend(this.userId); - await client.profiles.getProfile(this.username); - this.friendButton(); - }; - if (this.profile.isFriend) - friend.textContent = lang.get('profileRemoveFriend', 'Remove Friend'); - else { - friend.textContent = lang.get('profileAddFriend', 'Ask Friend'); - } - // this.info.appendChild(friend); - } - } + addFriendButton.onclick = _ => this.addFriend(); + removeFriendButton.onclick = _ => this.removeFriend(); + unblockButton.onclick = _ => this.unblockUser(); + blockButton.onclick = _ => this.blockUser(); } async getHtml() { @@ -118,19 +50,94 @@ export default class extends AbstractView { if (!this.profile) return ''; - const logged = await client.isAuthenticated(); - return ` -
-

${this.username}

- - - -
-
- - -
- `; +
+
+

${this.username}

+ + + +
+
+ + + + +
+
+ `; + } + + async addFriend() { + const removeFriendButton = document.getElementById('removeFriendButton'); + + const response = await client._post(`/api/profiles/friends/${this.profile.id}`); + const body = await response.json(); + console.log(body); + + if (response.ok) { + removeFriendButton.classList.remove('d-none'); + document.getElementById('addFriendButton').classList.add('d-none'); + } + if (response.status === 200) { + removeFriendButton.innerHTML = 'Cancel Request'; + removeFriendButton.classList.replace('btn-danger', 'btn-secondary'); + client.me.outgoingFriendRequests.push(this.profile); + this.profile.hasOutgoingRequest = true; + } else if (response.status === 201) { + removeFriendButton.innerHTML = 'Remove Friend'; + removeFriendButton.classList.replace('btn-secondary', 'btn-danger'); + this.profile.friend = true; + this.profile.hasIncomingRequest = false; + client.me.incomingFriendRequests = client.me.incomingFriendRequests.filter(profile => profile.id !== this.profile.id); + client.me.friendList.push(this.profile); + } + } + + async removeFriend() { + const addFriendButton = document.getElementById('addFriendButton'); + + const response = await client._delete(`/api/profiles/friends/${this.profile.id}`); + const body = await response.json(); + console.log(body); + + if (response.ok) { + addFriendButton.innerHTML = 'Add Friend'; + addFriendButton.classList.remove('d-none'); + document.getElementById('removeFriendButton').classList.add('d-none'); + } + if (response.status === 200) { + this.profile.hasOutgoingRequest = false; + client.me.outgoingFriendRequests = client.me.outgoingFriendRequests.filter(profile => profile.id !== this.profile.id); + } else if (response.status === 201) { + this.profile.isFriend = false; + client.me.friendList = client.me.friendList.client.me.incomingFriendRequests = filter(friend => friend.id !== this.profile.id); + } + } + + async blockUser() { + const response = await client._post(`/api/profiles/block/${this.profile.id}`); + const body = await response.json(); + console.log(body); + + if (response.ok) { + document.getElementById('blockButton').classList.add('d-none'); + document.getElementById('unblockButton').classList.remove('d-none'); + client.me.blockedUsers.push(this.profile); + this.profile.isBlocked = true; + } + } + + async unblockUser() { + const response = await client._delete(`/api/profiles/block/${this.profile.id}`); + const body = await response.json(); + console.log(body); + + if (response.ok) { + document.getElementById('unblockButton').classList.add('d-none'); + document.getElementById('blockButton').classList.remove('d-none'); + client.me.blockedUsers = client.me.blockedUsers.filter(profile => profile.id !== this.profile.id); + this.profile.isBlocked = false; + } } } diff --git a/profiles/views/friends.py b/profiles/views/friends.py index 00d76b3..125b061 100644 --- a/profiles/views/friends.py +++ b/profiles/views/friends.py @@ -50,16 +50,16 @@ class EditFriendView(APIView): user_profile = self.get_object() friend_profile = get_object_or_404(ProfileModel, pk=pk) - if not user_profile.is_friend(friend_profile): - return Response(_('You are not friend with this user.'), status.HTTP_400_BAD_REQUEST) - outgoing_request = user_profile.get_outgoing_friend_request_to(friend_profile) if outgoing_request: outgoing_request.delete() return Response(_('Friend request cancelled.')) + if not user_profile.is_friend(friend_profile): + return Response(_('You are not friend with this user.'), status.HTTP_400_BAD_REQUEST) + user_profile.delete_friend(friend_profile) - return Response(_('Friendship succssfully deleted.')) + return Response(_('Friendship succssfully deleted.'), status.HTTP_201_CREATED) class GetIncomingFriendRequestView(APIView):