feat(desktop): report current user online clients
This commit is contained in:
@@ -63,6 +63,7 @@ type DesktopClientRepository interface {
|
||||
Heartbeat(ctx context.Context, params HeartbeatDesktopClientParams) (*DesktopClient, error)
|
||||
Revoke(ctx context.Context, id uuid.UUID, workspaceID int64) (*DesktopClient, error)
|
||||
ListByWorkspace(ctx context.Context, workspaceID int64) ([]DesktopClient, error)
|
||||
ListByUser(ctx context.Context, tenantID, workspaceID, userID int64) ([]DesktopClient, error)
|
||||
}
|
||||
|
||||
type desktopClientRepository struct {
|
||||
@@ -156,6 +157,23 @@ func (r *desktopClientRepository) ListByWorkspace(ctx context.Context, workspace
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return desktopClientsFromGenerated(rows), nil
|
||||
}
|
||||
|
||||
func (r *desktopClientRepository) ListByUser(ctx context.Context, tenantID, workspaceID, userID int64) ([]DesktopClient, error) {
|
||||
rows, err := r.q.ListDesktopClientsByUser(ctx, generated.ListDesktopClientsByUserParams{
|
||||
TenantID: tenantID,
|
||||
WorkspaceID: workspaceID,
|
||||
UserID: userID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return desktopClientsFromGenerated(rows), nil
|
||||
}
|
||||
|
||||
func desktopClientsFromGenerated(rows []generated.DesktopClient) []DesktopClient {
|
||||
items := make([]DesktopClient, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
item := desktopClientFromGenerated(row)
|
||||
@@ -163,7 +181,7 @@ func (r *desktopClientRepository) ListByWorkspace(ctx context.Context, workspace
|
||||
items = append(items, *item)
|
||||
}
|
||||
}
|
||||
return items, nil
|
||||
return items
|
||||
}
|
||||
|
||||
func desktopClientFromGenerated(row generated.DesktopClient) *DesktopClient {
|
||||
|
||||
@@ -131,6 +131,57 @@ func (q *Queries) HeartbeatDesktopClient(ctx context.Context, arg HeartbeatDeskt
|
||||
return i, err
|
||||
}
|
||||
|
||||
const listDesktopClientsByUser = `-- name: ListDesktopClientsByUser :many
|
||||
SELECT id, tenant_id, workspace_id, user_id, token_hash, device_name, os, cpu_arch, client_version, channel, created_at, last_seen_at, last_rotated_at, revoked_at
|
||||
FROM desktop_clients
|
||||
WHERE tenant_id = $1
|
||||
AND workspace_id = $2
|
||||
AND user_id = $3
|
||||
AND revoked_at IS NULL
|
||||
ORDER BY last_seen_at DESC NULLS LAST, created_at DESC
|
||||
`
|
||||
|
||||
type ListDesktopClientsByUserParams struct {
|
||||
TenantID int64 `json:"tenant_id"`
|
||||
WorkspaceID int64 `json:"workspace_id"`
|
||||
UserID int64 `json:"user_id"`
|
||||
}
|
||||
|
||||
func (q *Queries) ListDesktopClientsByUser(ctx context.Context, arg ListDesktopClientsByUserParams) ([]DesktopClient, error) {
|
||||
rows, err := q.db.Query(ctx, listDesktopClientsByUser, arg.TenantID, arg.WorkspaceID, arg.UserID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []DesktopClient
|
||||
for rows.Next() {
|
||||
var i DesktopClient
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.TenantID,
|
||||
&i.WorkspaceID,
|
||||
&i.UserID,
|
||||
&i.TokenHash,
|
||||
&i.DeviceName,
|
||||
&i.Os,
|
||||
&i.CpuArch,
|
||||
&i.ClientVersion,
|
||||
&i.Channel,
|
||||
&i.CreatedAt,
|
||||
&i.LastSeenAt,
|
||||
&i.LastRotatedAt,
|
||||
&i.RevokedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listDesktopClientsByWorkspace = `-- name: ListDesktopClientsByWorkspace :many
|
||||
SELECT id, tenant_id, workspace_id, user_id, token_hash, device_name, os, cpu_arch, client_version, channel, created_at, last_seen_at, last_rotated_at, revoked_at
|
||||
FROM desktop_clients
|
||||
|
||||
@@ -117,6 +117,7 @@ type Querier interface {
|
||||
ListCompetitors(ctx context.Context, arg ListCompetitorsParams) ([]ListCompetitorsRow, error)
|
||||
ListDesktopAccountsByClient(ctx context.Context, arg ListDesktopAccountsByClientParams) ([]ListDesktopAccountsByClientRow, error)
|
||||
ListDesktopAccountsByUser(ctx context.Context, arg ListDesktopAccountsByUserParams) ([]ListDesktopAccountsByUserRow, error)
|
||||
ListDesktopClientsByUser(ctx context.Context, arg ListDesktopClientsByUserParams) ([]DesktopClient, error)
|
||||
ListDesktopClientsByWorkspace(ctx context.Context, workspaceID int64) ([]DesktopClient, error)
|
||||
ListImageAssets(ctx context.Context, arg ListImageAssetsParams) ([]ListImageAssetsRow, error)
|
||||
ListImageFolders(ctx context.Context, tenantID int64) ([]ListImageFoldersRow, error)
|
||||
|
||||
@@ -89,3 +89,12 @@ FROM desktop_clients
|
||||
WHERE workspace_id = sqlc.arg(workspace_id)
|
||||
AND revoked_at IS NULL
|
||||
ORDER BY last_seen_at DESC NULLS LAST, created_at DESC;
|
||||
|
||||
-- name: ListDesktopClientsByUser :many
|
||||
SELECT *
|
||||
FROM desktop_clients
|
||||
WHERE tenant_id = sqlc.arg(tenant_id)
|
||||
AND workspace_id = sqlc.arg(workspace_id)
|
||||
AND user_id = sqlc.arg(user_id)
|
||||
AND revoked_at IS NULL
|
||||
ORDER BY last_seen_at DESC NULLS LAST, created_at DESC;
|
||||
|
||||
Reference in New Issue
Block a user