84 lines
2.6 KiB
JavaScript
84 lines
2.6 KiB
JavaScript
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';
|
|
|
|
export default class Notice {
|
|
|
|
/**
|
|
* @param {Client} client
|
|
*/
|
|
constructor(client) {
|
|
/**
|
|
* @type {Client}
|
|
*/
|
|
this.client = client;
|
|
this.url = location.origin.replace('http', 'ws') + '/ws/notice';
|
|
}
|
|
|
|
start() {
|
|
this._socket = new WebSocket(this.url);
|
|
|
|
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);
|
|
} else if (data.type === 'new_friend') {
|
|
this.new_friend(data.friend);
|
|
} else if (data.type === 'friend_removed') {
|
|
this.friend_removed(data.friend);
|
|
} else if (data.type === 'friend_request_canceled') {
|
|
this.friend_request_canceled(data.author);
|
|
}
|
|
};
|
|
}
|
|
|
|
stop() {
|
|
if (this._socket) {
|
|
this._socket.close();
|
|
this._socket = undefined;
|
|
}
|
|
}
|
|
|
|
friend_request(author) {
|
|
console.log('hey')
|
|
client.me.incomingFriendRequests.push(new Profile(author.username, author.id, author.avatar));
|
|
createNotification('Friend Request', `<strong>${author.username}</strong> sent you a friend request.`);
|
|
if (lastView instanceof ProfilePageView && lastView.profile.id === author.id) {
|
|
lastView.profile.hasIncomingRequest = true;
|
|
lastView.loadFriendshipStatus();
|
|
}
|
|
}
|
|
|
|
new_friend(friend) {
|
|
client.me.friendList.push(new Profile(friend.username, friend.id, friend.avatar));
|
|
createNotification('New Friend', `<strong>${friend.username}</strong> accepted your friend request.`);
|
|
if (lastView instanceof ProfilePageView && lastView.profile.id === friend.id) {
|
|
lastView.profile.isFriend = true;
|
|
lastView.profile.hasIncomingRequest = false;
|
|
lastView.profile.hasOutgoingRequest = false;
|
|
lastView.loadFriendshipStatus();
|
|
}
|
|
}
|
|
|
|
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.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();
|
|
}
|
|
}
|
|
}
|