api: Give other user infos

This commit is contained in:
nemunaire 2023-11-24 03:46:18 +01:00
parent e47e99d4e3
commit 2b6e5eb098
6 changed files with 94 additions and 13 deletions

View File

@ -68,11 +68,15 @@ func declareUsersAuthRoutes(opts *config.Options, router *gin.RouterGroup) {
apiUserRoutes := router.Group("/users/:uid")
apiUserRoutes.Use(userHandler)
apiUserRoutes.Use(SameUserHandler)
apiUserRoutes.GET("", getUser)
apiUserRoutes.GET("/settings", getUserSettings)
apiUserRoutes.POST("/settings", changeUserSettings)
apiSameUserRoutes := router.Group("/users/:uid")
apiSameUserRoutes.Use(userHandler)
apiSameUserRoutes.Use(SameUserHandler)
apiSameUserRoutes.GET("/settings", getUserSettings)
apiSameUserRoutes.POST("/settings", changeUserSettings)
apiUserAuthRoutes := router.Group("/users/:uid")
apiUserAuthRoutes.Use(userAuthHandler)
@ -256,9 +260,17 @@ func SameUserHandler(c *gin.Context) {
}
func getUser(c *gin.Context) {
myuser := c.MustGet("LoggedUser").(*happydns.User)
user := c.MustGet("user").(*happydns.User)
c.JSON(http.StatusOK, user)
if bytes.Equal(user.Id, myuser.Id) {
c.JSON(http.StatusOK, user)
} else {
c.JSON(http.StatusOK, &happydns.User{
Id: user.Id,
Email: user.Email,
})
}
}
// getUserSettings gets the settings of the given user.

View File

@ -44,10 +44,10 @@ type User struct {
Email string
// CreatedAt is the time when the User logs in for the first time.
CreatedAt time.Time
CreatedAt time.Time `json:",omitempty"`
// LastSeen is the time when the User used happyDNS for the last time (in a 12h frame).
LastSeen time.Time
LastSeen time.Time `json:",omitempty"`
// Settings holds the settings for an account.
Settings UserSettings `json:"settings,omitempty"`

View File

@ -112,3 +112,9 @@ export function cleanUserSession(): void {
}
}
}
export async function getUser(id: string): Promise<User> {
id = encodeURIComponent(id);
const res = await fetch(`/api/users/${id}`, {headers: {'Accept': 'application/json'}});
return await handleApiResponse<User>(res);
}

View File

@ -0,0 +1,48 @@
import { get_store_value } from 'svelte/internal';
import { writable, type Writable } from 'svelte/store';
import { getUser as APIGetUser } from '$lib/api/user';
import type { User } from '$lib/model/user';
export const users: Writable<Record<string, User>> = writable({ });
function Mutex() {
let current = Promise.resolve();
this.lock = () => {
let _resolve;
const p = new Promise(resolve => {
_resolve = () => resolve();
});
const rv = current.then(() => _resolve);
current = p;
return rv;
};
}
const mutex = new Mutex();
const requests: Record<string, Promise<User>> = { };
export async function getUser(id: string, force: bool) {
let unlock = await mutex.lock();
if (requests[id]) {
unlock();
await requests[id];
return get_store_value(users)[id];
}
const user = get_store_value(users)[id];
if (user && !force) {
unlock();
return user;
}
requests[id] = APIGetUser(id);
unlock();
const data = await requests[id];
const obj = { };
obj[id] = data;
users.update((u) => Object.assign(u, obj));
delete requests[id];
return data;
}

View File

@ -6,6 +6,7 @@
} from 'sveltestrap';
import { getDomain } from '$lib/api/domains';
import { getUser } from '$lib/stores/users';
import { t } from '$lib/translations';
export let data: {domain: DomainInList; history: string; streamed: Object;};
@ -21,9 +22,14 @@
{:then domain}
{#each domain.zone_history as history}
<h3 class="mt-3">
{new Intl.DateTimeFormat(undefined, {dateStyle: "long", timeStyle: "long"}).format(new Date(history.last_modified))}
{new Intl.DateTimeFormat(undefined, {dateStyle: "long", timeStyle: "medium"}).format(new Date(history.last_modified))}
<small class="text-muted">
par {history.id_author}
par
{#await getUser(history.id_author)}
{history.id_author}
{:then user}
{user.Email}
{/await}
</small>
<Button
color="primary"
@ -37,20 +43,20 @@
{#if history.published}
<p class="mb-1">
<strong>Publiée le
{new Intl.DateTimeFormat(undefined, {dateStyle: "long", timeStyle: "long"}).format(new Date(history.published))}
{new Intl.DateTimeFormat(undefined, {dateStyle: "long", timeStyle: "medium"}).format(new Date(history.published))}
</strong>
</p>
{/if}
{#if history.commit_date}
<p class="mb-1">
Enregistrée le
{new Intl.DateTimeFormat(undefined, {dateStyle: "long", timeStyle: "long"}).format(new Date(history.commit_date))}
{new Intl.DateTimeFormat(undefined, {dateStyle: "long", timeStyle: "medium"}).format(new Date(history.commit_date))}
</p>
{/if}
{#if history.last_modified}
<p class="mb-1">
Dernière modification le
{new Intl.DateTimeFormat(undefined, {dateStyle: "long", timeStyle: "long"}).format(new Date(history.last_modified))}
{new Intl.DateTimeFormat(undefined, {dateStyle: "long", timeStyle: "medium"}).format(new Date(history.last_modified))}
</p>
{/if}
{#if history.commit_message}

View File

@ -7,6 +7,7 @@
} from 'sveltestrap';
import { getDomainLogs } from '$lib/api/domains';
import { getUser } from '$lib/stores/users';
import { t } from '$lib/translations';
export let data: {domain: DomainInList; history: string; streamed: Object;};
@ -33,10 +34,18 @@
{#if logs}
{#each logs as log}
<tr>
<td>{log.id_user}</td>
<td>
{#await getUser(log.id_user)}
{log.id_user}
{:then user}
<span title={user.Email}>
{user.Email}
</span>
{/await}
</td>
<td>{log.content}</td>
<td>
{new Intl.DateTimeFormat(undefined, {dateStyle: "short", timeStyle: "short"}).format(log.date)}
{new Intl.DateTimeFormat(undefined, {dateStyle: "short", timeStyle: "medium"}).format(new Date(log.date))}
</td>
<td>{log.level}</td>
</tr>