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