diff --git a/frontend/static/js/api/Client.js b/frontend/static/js/api/Client.js index 3732b9f..6476084 100644 --- a/frontend/static/js/api/Client.js +++ b/frontend/static/js/api/Client.js @@ -4,8 +4,8 @@ import { Profiles } from "./Profiles.js"; import { Channels } from './chat/Channels.js'; import { MyProfile } from "./MyProfile.js"; import { Tourmanents } from "./tournament/Tournaments.js"; -import Notice from "./notice/Notice.js"; import { Channel } from "./chat/Channel.js"; +import Notice from "./Notice.js"; import LanguageManager from './LanguageManager.js'; function getCookie(name) diff --git a/frontend/static/js/api/MyProfile.js b/frontend/static/js/api/MyProfile.js index 17dd2df..88e20eb 100644 --- a/frontend/static/js/api/MyProfile.js +++ b/frontend/static/js/api/MyProfile.js @@ -15,26 +15,26 @@ class MyProfile extends Profile * @type {[Profile]} */ this.blockedUsers = []; - /** - * @type {[Profile]} - */ - this.friendList = []; - /** - * @type {[Profile]} - */ - this.incomingFriendRequests = []; - /** - * @type {[Profile]} - */ - this.outgoingFriendRequests = []; + // /** + // * @type {[Profile]} + // */ + // this.friendList = []; + // /** + // * @type {[Profile]} + // */ + // this.incomingFriendRequests = []; + // /** + // * @type {[Profile]} + // */ + // this.outgoingFriendRequests = []; } async init() { await super.init(); await this.getBlockedUsers(); - await this.getFriends(); - await this.getIncomingFriendRequests() - await this.getOutgoingFriendRequests() + // await this.getFriends(); + // await this.getIncomingFriendRequests() + // await this.getOutgoingFriendRequests() } async getBlockedUsers() { diff --git a/frontend/static/js/api/notice/Notice.js b/frontend/static/js/api/Notice.js similarity index 73% rename from frontend/static/js/api/notice/Notice.js rename to frontend/static/js/api/Notice.js index f0db7e1..67b4b55 100644 --- a/frontend/static/js/api/notice/Notice.js +++ b/frontend/static/js/api/Notice.js @@ -1,8 +1,7 @@ -import {Client} from '../Client.js'; -import {createNotification} from '../../utils/noticeUtils.js' -import { client, lastView } from '../../index.js'; -import { Profile } from '../Profile.js'; -import ProfilePageView from '../../views/ProfilePageView.js'; +import {Client} from './Client.js'; +import {createNotification} from '../utils/noticeUtils.js' +import { lastView } from '../index.js'; +import ProfilePageView from '../views/ProfilePageView.js'; export default class Notice { @@ -23,6 +22,7 @@ export default class Notice { this._socket.onclose = _ => this._socket = undefined; this._socket.onmessage = message => { const data = JSON.parse(message.data); + console.log(data) if (data.type === 'friend_request') { this.friend_request(data.author); @@ -32,6 +32,10 @@ export default class Notice { this.friend_removed(data.friend); } else if (data.type === 'friend_request_canceled') { this.friend_request_canceled(data.author); + } else if (data.type === 'online') { + this.online(data.user) + } else if (data.type === 'offline') { + this.offline(data.user) } }; } @@ -43,8 +47,22 @@ export default class Notice { } } + _setOnlineStatus(user, status) { + if (lastView instanceof ProfilePageView && lastView.profile.id === user.id) { + lastView.profile.online = status; + lastView.loadFriendshipStatus(); + } + } + + online(user) { + this._setOnlineStatus(user, true) + } + + offline(user) { + this._setOnlineStatus(user, false) + } + friend_request(author) { - client.me.incomingFriendRequests.push(new Profile(author.username, author.id, author.avatar)); createNotification('Friend Request', `${author.username} sent you a friend request.`); if (lastView instanceof ProfilePageView && lastView.profile.id === author.id) { lastView.profile.hasIncomingRequest = true; @@ -53,7 +71,6 @@ export default class Notice { } new_friend(friend) { - client.me.friendList.push(new Profile(friend.username, friend.id, friend.avatar)); createNotification('New Friend', `${friend.username} accepted your friend request.`); if (lastView instanceof ProfilePageView && lastView.profile.id === friend.id) { lastView.profile.isFriend = true; @@ -64,15 +81,14 @@ export default class Notice { } friend_removed(exFriend) { - client.me.friendList = client.me.friendList.filter(friend => friend.id !== exFriend.id); if (lastView instanceof ProfilePageView && lastView.profile.id === exFriend.id) { lastView.profile.isFriend = false; + lastView.profile.online = null; lastView.loadFriendshipStatus(); } } friend_request_canceled(author) { - client.me.incomingFriendRequests = client.me.incomingFriendRequests.filter(user => user.id !== author.id); if (lastView instanceof ProfilePageView && lastView.profile.id === author.id) { lastView.profile.hasIncomingRequest = false; lastView.loadFriendshipStatus(); diff --git a/frontend/static/js/api/Profile.js b/frontend/static/js/api/Profile.js index 0a1643f..faeef20 100644 --- a/frontend/static/js/api/Profile.js +++ b/frontend/static/js/api/Profile.js @@ -59,19 +59,19 @@ export class Profile extends AExchangeable if (response.status !== 200) return response.status; - let response_data = await response.json(); - this.id = response_data.id; - this.username = response_data.username; - this.avatar = response_data.avatar; - this.online = response_data.online + const responseData = await response.json(); + this.id = responseData.id; + this.username = responseData.username; + this.avatar = responseData.avatar; + this.online = responseData.online if (!this.client.me || this.client.me.id === this.id) return; + this.hasIncomingRequest = responseData.has_incoming_request; + this.hasOutgoingRequest = responseData.has_outgoing_request; + this.isFriend = responseData.is_friend; this.isBlocked = this.client.me._isBlocked(this); - this.hasIncomingRequest = this.client.me._hasIncomingRequestFrom(this); - this.hasOutgoingRequest = this.client.me._hasOutgoingRequestTo(this); - this.isFriend = this.client.me._isFriend(this); } /** diff --git a/frontend/static/js/views/ProfilePageView.js b/frontend/static/js/views/ProfilePageView.js index 63a2168..7ebc850 100644 --- a/frontend/static/js/views/ProfilePageView.js +++ b/frontend/static/js/views/ProfilePageView.js @@ -38,7 +38,8 @@ export default class extends AbstractView { loadFriendshipStatus() { const addFriendButton = document.getElementById('addFriendButton'), - removeFriendButton = document.getElementById('removeFriendButton'); + removeFriendButton = document.getElementById('removeFriendButton'), + statusIndicator = document.getElementById('statusIndicator'); if (this.profile.hasIncomingRequest) { removeFriendButton.classList.add('d-none'); @@ -59,6 +60,12 @@ export default class extends AbstractView { removeFriendButton.classList.add('d-none'); addFriendButton.classList.remove('d-none'); } + + statusIndicator.classList.remove('bg-success', 'bg-danger'); + if (this.profile.online === true) + statusIndicator.classList.add('bg-success'); + else if (this.profile.online === false) + statusIndicator.classList.add('bg-danger'); } async getHtml() { @@ -70,7 +77,7 @@ export default class extends AbstractView { return `