profiles: friend and block through profile page :))
This commit is contained in:
parent
2a63edf739
commit
8c8847cdd8
@ -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
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -54,40 +54,6 @@ class Profiles
|
||||
return null;
|
||||
return profile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Block a user
|
||||
* @param {Number} user_id
|
||||
* @returns {Promise<Object>}
|
||||
*/
|
||||
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<Object>}
|
||||
*/
|
||||
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};
|
||||
|
@ -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();
|
||||
|
||||
client.notice.rewrite_profile = async () => {
|
||||
await this.profile.getFriend();
|
||||
await this.profile.getBlock();
|
||||
await this.friendButton();
|
||||
};
|
||||
}
|
||||
|
||||
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);
|
||||
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
|
||||
await client.profiles.deblock(this.userId);
|
||||
this.profile = await client.profiles.getProfile(this.username);
|
||||
addFriendButton.classList.remove('d-none');
|
||||
|
||||
this.blockButton();
|
||||
};
|
||||
if (this.profile.isBlocked)
|
||||
block.textContent = lang.get('profileUnblock', 'Unblock');
|
||||
unblockButton.classList.remove('d-none');
|
||||
else
|
||||
block.textContent = lang.get('profileBlock', 'Block');
|
||||
}
|
||||
}
|
||||
blockButton.classList.remove('d-none');
|
||||
|
||||
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,9 +50,8 @@ export default class extends AbstractView {
|
||||
if (!this.profile)
|
||||
return '';
|
||||
|
||||
const logged = await client.isAuthenticated();
|
||||
|
||||
return `
|
||||
<div>
|
||||
<div class='mb-3' id='profileInfo'>
|
||||
<h1>${this.username}</h1>
|
||||
<a href=${this.profile.avatar} target='_blank'>
|
||||
@ -128,9 +59,85 @@ export default class extends AbstractView {
|
||||
</a>
|
||||
</div>
|
||||
<div>
|
||||
<button class='btn btn-sm btn-success ${logged ? '' : 'd-none'}' id='addFriendButton'>Add Friend</button>
|
||||
<button class='btn btn-sm btn-danger ${logged ? '' : 'd-none'}' id='removeFriendButton'>Remove Friend</button>
|
||||
<button class='btn btn-sm btn-success d-none' id='addFriendButton'>Add Friend</button>
|
||||
<button class='btn btn-sm btn-danger d-none' id='removeFriendButton'>Remove Friend</button>
|
||||
<button class='btn btn-sm btn-danger d-none' id='blockButton'>Block</button>
|
||||
<button class='btn btn-sm btn-secondary d-none' id='unblockButton'>Unblock</button>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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):
|
||||
|
Loading…
Reference in New Issue
Block a user