core: use postinit status code
This commit is contained in:
parent
6f9903e309
commit
476ed0b833
@ -13,6 +13,4 @@ class LoggedView(APIView):
|
|||||||
authentication_classes = (SessionAuthentication,)
|
authentication_classes = (SessionAuthentication,)
|
||||||
|
|
||||||
def get(self, request: HttpRequest):
|
def get(self, request: HttpRequest):
|
||||||
if (request.user.is_authenticated):
|
return Response(status = (status.HTTP_200_OK if request.user.is_authenticated else status.HTTP_400_BAD_REQUEST))
|
||||||
return Response({'id': request.user.pk}, status=status.HTTP_200_OK)
|
|
||||||
return Response('false', status=status.HTTP_200_OK)
|
|
||||||
|
@ -2,6 +2,12 @@ import { Profile } from "./profile.js";
|
|||||||
|
|
||||||
class MyProfile extends Profile
|
class MyProfile extends Profile
|
||||||
{
|
{
|
||||||
|
|
||||||
|
constructor (client)
|
||||||
|
{
|
||||||
|
super(client, "me")
|
||||||
|
}
|
||||||
|
|
||||||
async change_avatar(form_data)
|
async change_avatar(form_data)
|
||||||
{
|
{
|
||||||
let response = await this.client._patch_file(`/api/profiles/me`, form_data);
|
let response = await this.client._patch_file(`/api/profiles/me`, form_data);
|
||||||
@ -10,10 +16,6 @@ class MyProfile extends Profile
|
|||||||
return response_data;
|
return response_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
async init()
|
|
||||||
{
|
|
||||||
super.init("me");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export {MyProfile}
|
export {MyProfile}
|
@ -21,8 +21,7 @@ class Channels {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let data = await response.json();
|
let data = await response.json();
|
||||||
let exit_code = await response.status;
|
if (data.status >= 300)
|
||||||
if (exit_code >= 300)
|
|
||||||
return undefined;
|
return undefined;
|
||||||
|
|
||||||
let messages = undefined;
|
let messages = undefined;
|
||||||
|
@ -100,14 +100,17 @@ class Client
|
|||||||
|
|
||||||
async _update_logged(state)
|
async _update_logged(state)
|
||||||
{
|
{
|
||||||
if (!this.logged && state)
|
if (this.logged == state)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (state)
|
||||||
{
|
{
|
||||||
this.me = new MyProfile(this);
|
this.me = new MyProfile(this);
|
||||||
await this.me.init();
|
await this.me.init();
|
||||||
}
|
}
|
||||||
if (this.logged && !state)
|
else
|
||||||
{
|
{
|
||||||
navigateTo("/login");
|
this.me = undefined;
|
||||||
}
|
}
|
||||||
this.logged = state;
|
this.logged = state;
|
||||||
}
|
}
|
||||||
@ -115,13 +118,13 @@ class Client
|
|||||||
async login(username, password)
|
async login(username, password)
|
||||||
{
|
{
|
||||||
let response = await this._post("/api/accounts/login", {username: username, password: password})
|
let response = await this._post("/api/accounts/login", {username: username, password: password})
|
||||||
let data = await response.json();
|
|
||||||
if (data.id != undefined)
|
if (response.status != 200)
|
||||||
{
|
return response.status;
|
||||||
await this._update_logged(true);
|
|
||||||
return null;
|
this._update_logged(true);
|
||||||
}
|
|
||||||
return data;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
async logout()
|
async logout()
|
||||||
@ -133,11 +136,9 @@ class Client
|
|||||||
async _test_logged()
|
async _test_logged()
|
||||||
{
|
{
|
||||||
let response = await this._get("/api/accounts/logged");
|
let response = await this._get("/api/accounts/logged");
|
||||||
let data = await response.json();
|
|
||||||
|
|
||||||
if (data.id !== undefined)
|
await this._update_logged(response.status === 200);
|
||||||
await this._update_logged(true);
|
return response.status === 200
|
||||||
return data.id !== undefined;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,35 +5,37 @@ class Profile
|
|||||||
/**
|
/**
|
||||||
* @param {Client} client
|
* @param {Client} client
|
||||||
*/
|
*/
|
||||||
constructor (client, username = undefined, avatar_url = undefined, user_id = undefined)
|
constructor (client, id, username = undefined, avatar_url = undefined)
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @type {Client} client
|
* @type {Client} client
|
||||||
*/
|
*/
|
||||||
this.client = client;
|
this.client = client;
|
||||||
|
this.id = id;
|
||||||
this.username = username;
|
this.username = username;
|
||||||
this.avatar_url = avatar_url;
|
this.avatar_url = avatar_url;
|
||||||
this.user_id = user_id;
|
|
||||||
this.isBlocked = false;
|
this.isBlocked = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
async init(user_id)
|
async init()
|
||||||
{
|
{
|
||||||
let response = await this.client._get(`/api/profiles/${user_id}`);
|
let response = await this.client._get(`/api/profiles/${this.id}`);
|
||||||
|
|
||||||
if (response.status === 404)
|
if (response.status !== 200)
|
||||||
return 1;
|
return response.status;
|
||||||
|
|
||||||
let response_data = await response.json();
|
let response_data = await response.json();
|
||||||
|
this.id = response_data.user_id;
|
||||||
this.user_id = response_data.user_id;
|
|
||||||
this.username = response_data.username;
|
this.username = response_data.username;
|
||||||
this.avatar_url = response_data.avatar_url;
|
this.avatar_url = response_data.avatar_url;
|
||||||
|
|
||||||
|
if (this.client.me == undefined)
|
||||||
|
return;
|
||||||
|
|
||||||
let block_response = await this.client._get("/api/profiles/block");
|
let block_response = await this.client._get("/api/profiles/block");
|
||||||
|
|
||||||
if (block_response.status == 404)
|
if (block_response.status == 404)
|
||||||
return
|
return;
|
||||||
|
|
||||||
let block_data = await block_response.json();
|
let block_data = await block_response.json();
|
||||||
let block_list = JSON.parse(block_data);
|
let block_list = JSON.parse(block_data);
|
||||||
|
@ -20,15 +20,15 @@ class Profiles
|
|||||||
|
|
||||||
let profiles = []
|
let profiles = []
|
||||||
response_data.forEach((profile) => {
|
response_data.forEach((profile) => {
|
||||||
profiles.push(new Profile(this.client, profile.username, profile.avatar_url, profile.user_id))
|
profiles.push(new Profile(this.client, profile.user_id, profile.username, profile.avatar_url))
|
||||||
});
|
});
|
||||||
return profiles;
|
return profiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getProfile(user_id)
|
async getProfile(user_id)
|
||||||
{
|
{
|
||||||
let profile = new Profile(this.client);
|
let profile = new Profile(this.client, user_id);
|
||||||
if (await profile.init(user_id))
|
if (await profile.init())
|
||||||
return null;
|
return null;
|
||||||
return profile;
|
return profile;
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ class Tourmanent
|
|||||||
/**
|
/**
|
||||||
* @param {Client} client
|
* @param {Client} client
|
||||||
*/
|
*/
|
||||||
constructor(client, name = undefined, nb_players = undefined, nb_players_by_game = undefined, level = undefined, started = undefined, finished = undefined, levels = undefined, id = undefined)
|
constructor(client, id, name = undefined, nb_players = undefined, nb_players_by_game = undefined, level = undefined, started = undefined, finished = undefined, levels = undefined, state = undefined)
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @type {Client}
|
* @type {Client}
|
||||||
@ -18,28 +18,18 @@ class Tourmanent
|
|||||||
this.started = started;
|
this.started = started;
|
||||||
this.finished = finished;
|
this.finished = finished;
|
||||||
this.levels = levels;
|
this.levels = levels;
|
||||||
this.state = this.get_state();
|
this.state = state;
|
||||||
this.id = id
|
this.id = id
|
||||||
|
|
||||||
this.connected = false;
|
this.connected = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
get_state()
|
async init()
|
||||||
{
|
|
||||||
if (this.finished)
|
|
||||||
return "finished";
|
|
||||||
if (this.started)
|
|
||||||
return "started";
|
|
||||||
else
|
|
||||||
return "waiting";
|
|
||||||
}
|
|
||||||
|
|
||||||
async init(id)
|
|
||||||
{
|
{
|
||||||
let response = await this.client._get(`/api/tournaments/${id}`);
|
let response = await this.client._get(`/api/tournaments/${id}`);
|
||||||
|
|
||||||
if (response.status === 404)
|
if (response.status !== 200)
|
||||||
return 1;
|
return response.status;
|
||||||
|
|
||||||
let response_data = await response.json();
|
let response_data = await response.json();
|
||||||
|
|
||||||
@ -50,8 +40,7 @@ class Tourmanent
|
|||||||
this.started = response_data.started;
|
this.started = response_data.started;
|
||||||
this.finished = response_data.finished;
|
this.finished = response_data.finished;
|
||||||
this.levels = response_data.levels;
|
this.levels = response_data.levels;
|
||||||
this.id = response_data.id
|
this.state = response_data.state;
|
||||||
this.state = this.get_state();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
leave(event)
|
leave(event)
|
||||||
|
@ -26,11 +26,8 @@ class Tourmanents
|
|||||||
{
|
{
|
||||||
let response = await this.client._post("/api/tournaments/", {nb_players: nb_players, nb_players_by_game: nb_players_by_game, name: name});
|
let response = await this.client._post("/api/tournaments/", {nb_players: nb_players, nb_players_by_game: nb_players_by_game, name: name});
|
||||||
|
|
||||||
if (response.status === 403)
|
if (response.status !== 200)
|
||||||
{
|
return response.status;
|
||||||
this.client._update_logged(false);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
let response_data = await response.json();
|
let response_data = await response.json();
|
||||||
return response_data;
|
return response_data;
|
||||||
|
@ -6,7 +6,8 @@ import Search from "./views/Search.js";
|
|||||||
import HomeView from "./views/HomeView.js";
|
import HomeView from "./views/HomeView.js";
|
||||||
import RegisterView from "./views/accounts/RegisterView.js";
|
import RegisterView from "./views/accounts/RegisterView.js";
|
||||||
import LogoutView from "./views/accounts/LogoutView.js";
|
import LogoutView from "./views/accounts/LogoutView.js";
|
||||||
import GameView from "./views/Game.js"
|
import GameOfflineView from "./views/GameOfflineView.js";
|
||||||
|
import GameView from "./views/GameView.js";
|
||||||
|
|
||||||
import PageNotFoundView from './views/PageNotFoundView.js'
|
import PageNotFoundView from './views/PageNotFoundView.js'
|
||||||
|
|
||||||
@ -47,11 +48,16 @@ async function renderView(view)
|
|||||||
view.setTitle();
|
view.setTitle();
|
||||||
document.querySelector("#app").innerHTML = content
|
document.querySelector("#app").innerHTML = content
|
||||||
|
|
||||||
if (await view.postInit())
|
let error_code = await view.postInit();
|
||||||
|
|
||||||
|
if (error_code === 404)
|
||||||
renderView(new PageNotFoundView());
|
renderView(new PageNotFoundView());
|
||||||
|
else if (error_code === 403)
|
||||||
|
this._client._update_logged(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
const router = async(uri) => {
|
const router = async(uri) => {
|
||||||
|
|
||||||
const routes = [
|
const routes = [
|
||||||
{ path: "/", view: Dashboard },
|
{ path: "/", view: Dashboard },
|
||||||
{ path: "/profiles/:id", view: ProfilePageView },
|
{ path: "/profiles/:id", view: ProfilePageView },
|
||||||
@ -65,7 +71,8 @@ const router = async (uri) => {
|
|||||||
{ path: "/home", view: HomeView },
|
{ path: "/home", view: HomeView },
|
||||||
{ path: "/me", view: MeView },
|
{ path: "/me", view: MeView },
|
||||||
{ path: "/matchmaking", view: MatchMakingView },
|
{ path: "/matchmaking", view: MatchMakingView },
|
||||||
{ path: "/game/offline", view: GameView },
|
{ path: "/games/offline", view: GameOfflineView },
|
||||||
|
{ path: "/games/:id", view: GameView },
|
||||||
];
|
];
|
||||||
|
|
||||||
// Test each route for potential match
|
// Test each route for potential match
|
||||||
@ -98,8 +105,9 @@ const router = async (uri) => {
|
|||||||
|
|
||||||
lastView = view;
|
lastView = view;
|
||||||
|
|
||||||
await client.isAuthentificate();
|
if (await renderView(view))
|
||||||
renderView(view);
|
return 1;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ export default class extends AbstractView {
|
|||||||
let profile = await client.profiles.getProfile(this.user_id);
|
let profile = await client.profiles.getProfile(this.user_id);
|
||||||
|
|
||||||
if (profile === null)
|
if (profile === null)
|
||||||
return 1;
|
return 404;
|
||||||
|
|
||||||
this.profile = await client.profiles.getProfile(this.user_id);
|
this.profile = await client.profiles.getProfile(this.user_id);
|
||||||
this.info = document.getElementById("info");
|
this.info = document.getElementById("info");
|
||||||
@ -36,6 +36,9 @@ export default class extends AbstractView {
|
|||||||
|
|
||||||
async blockButton() {
|
async blockButton() {
|
||||||
// Block option
|
// Block option
|
||||||
|
if (await client.isAuthentificate() === false)
|
||||||
|
return;
|
||||||
|
|
||||||
if (client.me.user_id != this.user_id) {
|
if (client.me.user_id != this.user_id) {
|
||||||
let block = document.getElementById("block") || document.createElement("a");
|
let block = document.getElementById("block") || document.createElement("a");
|
||||||
block.id = "block";
|
block.id = "block";
|
||||||
|
@ -16,24 +16,25 @@ export default class extends AbstractView {
|
|||||||
//chat_input.addEventListener("keydown", this.chat_manager)
|
//chat_input.addEventListener("keydown", this.chat_manager)
|
||||||
|
|
||||||
this.last_add_chat = undefined;
|
this.last_add_chat = undefined;
|
||||||
this.users();
|
|
||||||
this.chat();
|
let logged = await client.isAuthentificate();
|
||||||
|
let profiles = await client.profiles.all();
|
||||||
|
|
||||||
|
this.users(logged, profiles);
|
||||||
|
this.chat(logged, profiles);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async users() {
|
async users(logged, profiles) {
|
||||||
|
|
||||||
let search = document.getElementById("input_user").value.toLowerCase();
|
let search = document.getElementById("input_user").value.toLowerCase();
|
||||||
|
|
||||||
let logged = await client.isAuthentificate();
|
|
||||||
|
|
||||||
let users = await client.profiles.all();
|
|
||||||
let list_users = document.getElementById('list_users');
|
let list_users = document.getElementById('list_users');
|
||||||
list_users.innerHTML = "";
|
list_users.innerHTML = "";
|
||||||
|
|
||||||
users.filter(user => user.username.toLowerCase().startsWith(search) == true).forEach((user) => {
|
profiles.filter(user => user.username.toLowerCase().startsWith(search) == true).forEach((user) => {
|
||||||
|
|
||||||
if (user.user_id == null) {
|
if (user.id == null) {
|
||||||
console.log("list User one with id null;");
|
console.log("list User one with id null;");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -42,7 +43,7 @@ export default class extends AbstractView {
|
|||||||
|
|
||||||
// username
|
// username
|
||||||
let username = document.createElement("a");
|
let username = document.createElement("a");
|
||||||
username.href = `/profiles/${user.user_id}`;
|
username.href = `/profiles/${user.id}`;
|
||||||
username.appendChild(document.createTextNode(user.username));
|
username.appendChild(document.createTextNode(user.username));
|
||||||
new_user.appendChild(username);
|
new_user.appendChild(username);
|
||||||
|
|
||||||
@ -50,13 +51,13 @@ export default class extends AbstractView {
|
|||||||
new_user.appendChild(document.createTextNode(" "));
|
new_user.appendChild(document.createTextNode(" "));
|
||||||
|
|
||||||
// chat
|
// chat
|
||||||
if (logged && client.me.user_id != user.user_id) {
|
if (logged && client.me.id != user.id) {
|
||||||
let add_chat = document.createElement("a");
|
let add_chat = document.createElement("a");
|
||||||
add_chat.id = "add_chat_off";
|
add_chat.id = "add_chat_off";
|
||||||
add_chat.onclick = async () => {
|
add_chat.onclick = async () => {
|
||||||
if (client.channel != undefined) {
|
if (client.channel != undefined) {
|
||||||
client.channel.members_id.forEach((member_id) => {
|
client.channel.members_id.forEach((member_id) => {
|
||||||
if (member_id == user.user_id)
|
if (member_id == user.id)
|
||||||
client.channel = undefined;
|
client.channel = undefined;
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -69,7 +70,7 @@ export default class extends AbstractView {
|
|||||||
client.channel.disconnect();
|
client.channel.disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
client.channel = await client.channels.createChannel([client.me.user_id , user.user_id], this.chat);
|
client.channel = await client.channels.createChannel([client.me.id , user.id], this.chat);
|
||||||
this.chat();
|
this.chat();
|
||||||
if (this.last_add_chat != undefined)
|
if (this.last_add_chat != undefined)
|
||||||
this.last_add_chat.id = "add_chat_off";
|
this.last_add_chat.id = "add_chat_off";
|
||||||
@ -98,10 +99,8 @@ export default class extends AbstractView {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async chat() {
|
async chat(logged, profiles) {
|
||||||
|
|
||||||
let users = await client.profiles.all();
|
|
||||||
let logged = await client.isAuthentificate();
|
|
||||||
/*let reload = document.getElementById("messages");
|
/*let reload = document.getElementById("messages");
|
||||||
if (reload != null)
|
if (reload != null)
|
||||||
reload.remove();*/
|
reload.remove();*/
|
||||||
@ -139,7 +138,7 @@ export default class extends AbstractView {
|
|||||||
if (messages.children[i] == null || message.content != messages.children[i].innerText) {
|
if (messages.children[i] == null || message.content != messages.children[i].innerText) {
|
||||||
let text = document.createElement("p");
|
let text = document.createElement("p");
|
||||||
text.appendChild(document.createTextNode(message.content));
|
text.appendChild(document.createTextNode(message.content));
|
||||||
if (message.author_id == client.me.user_id)
|
if (message.author_id == client.me)
|
||||||
text.id = "you";
|
text.id = "you";
|
||||||
else
|
else
|
||||||
text.id = "other";
|
text.id = "other";
|
||||||
@ -166,8 +165,8 @@ export default class extends AbstractView {
|
|||||||
|
|
||||||
let receivers_id = [];
|
let receivers_id = [];
|
||||||
client.channel.members_id.forEach((member_id) => {
|
client.channel.members_id.forEach((member_id) => {
|
||||||
if (member_id != client.me.user_id)
|
if (member_id != client.me)
|
||||||
receivers_id.push(users.filter(user => user.user_id == member_id)[0].user_id);
|
receivers_id.push(profiles.filter(user => user.id == member_id)[0].user_id);
|
||||||
});
|
});
|
||||||
await client.channel.sendMessageChannel(chat_text, receivers_id)
|
await client.channel.sendMessageChannel(chat_text, receivers_id)
|
||||||
|
|
||||||
@ -182,10 +181,10 @@ export default class extends AbstractView {
|
|||||||
members.id = "members";
|
members.id = "members";
|
||||||
let usernames = "";
|
let usernames = "";
|
||||||
client.channel.members_id.forEach((member_id) => {
|
client.channel.members_id.forEach((member_id) => {
|
||||||
if (member_id != client.me.user_id) {
|
if (member_id != client.me) {
|
||||||
if (usernames.length > 0)
|
if (usernames.length > 0)
|
||||||
usernames += ", ";
|
usernames += ", ";
|
||||||
usernames += (users.filter(user => user.user_id == member_id)[0].username);
|
usernames += (profiles.filter(user => user.id == member_id)[0].username);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
members.appendChild(document.createTextNode(usernames));
|
members.appendChild(document.createTextNode(usernames));
|
||||||
|
@ -41,7 +41,7 @@ export default class extends AbstractAuthentifiedView
|
|||||||
this.tournament = await client.tournaments.getTournament(this.id);
|
this.tournament = await client.tournaments.getTournament(this.id);
|
||||||
|
|
||||||
if (this.tournament === null)
|
if (this.tournament === null)
|
||||||
return 1;
|
return 404;
|
||||||
|
|
||||||
this.tournament.join(this.receive.bind(this), this.ondisconnect.bind(this));
|
this.tournament.join(this.receive.bind(this), this.ondisconnect.bind(this));
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user