diff --git a/src/api/__tests__/chatbot/chatbot.test.ts b/src/api/__tests__/chatbot/chatbot.test.ts index 098addf..6b21ef8 100644 --- a/src/api/__tests__/chatbot/chatbot.test.ts +++ b/src/api/__tests__/chatbot/chatbot.test.ts @@ -1,5 +1,5 @@ import { afterEach, describe, expect, it, vi } from "vitest"; -import { deleteChatbotThreadById, getChatbotMessageById, getChatbotThreadMessages, getChatbotThreads, patchChatbotThreadById, postChatbotMessage, postChatbotThread } from "../../chatbot"; +import { chatbotStreamUrl, deleteChatbotThreadById, getChatbotMessageById, getChatbotThreadMessages, getChatbotThreads, patchChatbotThreadById, postChatbotMessage, postChatbotThread } from "../../chatbot"; import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch"; describe("chatbot API", () => { @@ -38,4 +38,7 @@ describe("chatbot API", () => { expect(url).toBe("/api/chatbot/threads/thread-1/messages/answer-1"); expect(init?.method).toBe("GET"); }); + it("chatbotStreamUrl builds the SSE path for an EventSource", () => { + expect(chatbotStreamUrl("t 1", "m1")).toBe("/api/chatbot/threads/t%201/messages/m1/stream"); + }); }); diff --git a/src/api/__tests__/users/users.test.ts b/src/api/__tests__/users/users.test.ts index a847091..bc1ba6a 100644 --- a/src/api/__tests__/users/users.test.ts +++ b/src/api/__tests__/users/users.test.ts @@ -14,6 +14,7 @@ import { postMyAvatar } from "../../users"; import { deleteMyAvatar } from "../../users"; import { deleteUserAvatar } from "../../users"; import { getUserAvatarUrl } from "../../users"; +import { postUser } from "../../users"; import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch"; describe("users API", () => { @@ -214,4 +215,17 @@ describe("users API", () => { expect(getUserAvatarUrl("u1", 3)).toBe("/api/users/u1/avatar?v=3"); expect(getUserAvatarUrl("u/1")).toBe("/api/users/u%2F1/avatar"); }); + it("postUser posts the new account to /users", async () => { + const mockUser = { id: "u9", username: "ayse", role: "teacher" }; + mockFetchSuccess(mockUser); + + const body = { username: "ayse", password: "secret123", role: "teacher" as const }; + const result = await postUser(body); + expect(result).toEqual(mockUser); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/users"); + expect(init?.method).toBe("POST"); + expect(JSON.parse(init?.body as string)).toEqual(body); + }); }); diff --git a/src/api/chatbot/chatbotStreamUrl.ts b/src/api/chatbot/chatbotStreamUrl.ts new file mode 100644 index 0000000..9c73078 --- /dev/null +++ b/src/api/chatbot/chatbotStreamUrl.ts @@ -0,0 +1,7 @@ +/** + * The SSE URL for one pending turn: `delta` chunks, then one `done` or + * `error`. Not a request — hand it to an `EventSource` (`withCredentials: true`). + */ +export function chatbotStreamUrl(threadId: string, messageId: string): string { + return `/api/chatbot/threads/${encodeURIComponent(threadId)}/messages/${encodeURIComponent(messageId)}/stream`; +} diff --git a/src/api/chatbot/index.ts b/src/api/chatbot/index.ts index 33c87a2..233620f 100644 --- a/src/api/chatbot/index.ts +++ b/src/api/chatbot/index.ts @@ -5,4 +5,5 @@ export * from "./patchChatbotThreadById"; export * from "./deleteChatbotThreadById"; export * from "./postChatbotMessage"; export * from "./postChatbotThread"; +export * from "./chatbotStreamUrl"; export * from "./types"; diff --git a/src/api/client/types.ts b/src/api/client/types.ts index 426427d..242f56e 100644 --- a/src/api/client/types.ts +++ b/src/api/client/types.ts @@ -38,6 +38,17 @@ export type ProfileUpdate = { birth_date?: string | null; display_name?: string | null; bio?: string | null; + // The teacher's branş, by name from settings.branches. + branch?: string | null; +}; + +// POST /users (admin): the school office opens an account directly. `role` +// defaults to student on the server; an existing person joins this school only +// when the password matches their credential (409 otherwise). +export type CreateUserInput = { + username: string; + password: string; + role?: Role; }; export type PersonRef = { @@ -105,6 +116,8 @@ export type Profile = { display_name: string | null; role: Role; bio: string | null; + // The teacher's branş, one of settings.branches; null when unset. + branch: string | null; avatar: AvatarMeta | null; // Capped at limits.user.max_profile_classes; stats.classes holds the true total. classes: ProfileClassRef[]; @@ -395,6 +408,12 @@ export type ClassMember = { class: string; user: PersonRef; added_by: PersonRef; + // One stint: a leave-and-rejoin is a fresh row with a fresh id. + joined_at: number; + // Null while the stint is live; the roster routes only list live rows. + left_at: number | null; + // The şube a year rollover copied this member out of; null when placed by hand. + source_class_group: string | null; }; // The instance as the class routes return it: an Instance plus who attached @@ -833,6 +852,9 @@ export type Limits = { max_class_grade_len: number; max_class_members: number; max_class_courses: number; + max_academic_year_name_len: number; + min_ders_saati: number; + max_ders_saati: number; }; exam: { max_title_len: number; @@ -934,7 +956,7 @@ export type Limits = { required_attendance_statuses: string[]; }; request: { max_page_limit: number; max_request_id_len: number; schedule_past_grace_ms: number; request_timeout_secs: number }; - rate: { window_secs: number; auth_per_minute: number; api_per_minute: number; chatbot_per_minute: number }; + rate: { window_secs: number; auth_per_minute: number; api_per_minute: number; chatbot_per_minute: number; rag_per_minute: number }; // The RAG nest. Message length, thread titles, history depth and the thread // cap are the chatbot's own knobs, republished so a RAG client need not read // the chatbot group to bound its input. diff --git a/src/api/users/index.ts b/src/api/users/index.ts index ac8f0c4..feff523 100644 --- a/src/api/users/index.ts +++ b/src/api/users/index.ts @@ -13,3 +13,4 @@ export * from "./postMyAvatar"; export * from "./deleteMyAvatar"; export * from "./deleteUserAvatar"; export * from "./getUserAvatarUrl"; +export * from "./postUser"; diff --git a/src/api/users/postUser.ts b/src/api/users/postUser.ts new file mode 100644 index 0000000..f61707f --- /dev/null +++ b/src/api/users/postUser.ts @@ -0,0 +1,6 @@ +import { client } from "../client"; +import type { CreateUserInput, User } from "../client"; + +export function postUser(body: CreateUserInput): Promise { + return client("/users", { method: "POST", body }); +} diff --git a/src/components/layout/celebi-panel.tsx b/src/components/layout/celebi-panel.tsx index 652a68d..f421f49 100644 --- a/src/components/layout/celebi-panel.tsx +++ b/src/components/layout/celebi-panel.tsx @@ -1,6 +1,6 @@ import { For, Match, Show, Switch, createEffect, createSignal, on, onCleanup, untrack } from "solid-js"; import { useNavigate } from "@tanstack/solid-router"; -import { deleteChatbotThreadById, getChatbotMessageById, getChatbotThreadMessages, getChatbotThreads, patchChatbotThreadById, postChatbotMessage, postChatbotThread, type ChatbotMessage, type ChatbotThread } from "@/api/chatbot"; +import { chatbotStreamUrl, deleteChatbotThreadById, getChatbotMessageById, getChatbotThreadMessages, getChatbotThreads, patchChatbotThreadById, postChatbotMessage, postChatbotThread, type ChatbotMessage, type ChatbotThread } from "@/api/chatbot"; import { formatApiError } from "@/api/client"; import { CelebiComposer } from "@/components/layout/celebi-composer"; import { CelebiMarkdown } from "@/components/layout/celebi-markdown"; @@ -217,7 +217,7 @@ export function CelebiPanel(props: { open: boolean; onOpenChange: (open: boolean const streamAnswer = (activeThreadId: string, messageId: string) => { stopStream(); - const source = new EventSource(`/api/chatbot/threads/${encodeURIComponent(activeThreadId)}/messages/${encodeURIComponent(messageId)}/stream`, { withCredentials: true }); + const source = new EventSource(chatbotStreamUrl(activeThreadId, messageId), { withCredentials: true }); stream = source; source.addEventListener("delta", (event) => { try { const data = JSON.parse((event as MessageEvent).data) as { text?: unknown }; if (typeof data.text === "string") setMessages((items) => items.map((item) => item.id === messageId ? { ...item, content: item.content + data.text } : item)); } catch { /* ignore malformed chunk */ } }); source.addEventListener("done", (event) => { try { updateAssistant((JSON.parse((event as MessageEvent).data) as { message: ChatbotMessage }).message); } catch { poll(activeThreadId, messageId); } finally { stopStream(); void loadThreads(); } }); diff --git a/src/components/users/profile-form.tsx b/src/components/users/profile-form.tsx index b50d14d..ad78511 100644 --- a/src/components/users/profile-form.tsx +++ b/src/components/users/profile-form.tsx @@ -1,4 +1,4 @@ -import { Show, createSignal } from "solid-js"; +import { For, Show, createSignal } from "solid-js"; import { patchMe } from "@/api/users"; import type { ProfileUpdate, User } from "@/api/client"; import { formatApiError } from "@/api/client"; @@ -7,8 +7,10 @@ import { Button } from "@/components/ui/button"; import { DatePicker } from "@/components/ui/date-picker"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; +import { Select } from "@/components/ui/select"; import { Textarea } from "@/components/ui/textarea"; import { createFlash } from "@/lib/flash"; +import { hasMinRole } from "@/lib/roles"; import { useT } from "@/stores/preferences-context"; const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; @@ -47,7 +49,9 @@ export function ProfileForm(props: { * them where a profile is in hand; omit them and the fields stay hidden * rather than silently offering to clear values they cannot see. */ - profile?: { display_name: string | null; bio: string | null }; + profile?: { display_name: string | null; bio: string | null; branch?: string | null }; + /** The school's branş vocabulary (settings.branches); the picker hides when empty. */ + branches?: string[]; maxDisplayNameLen?: number; maxBioLen?: number; onSaved: () => void; @@ -61,6 +65,15 @@ export function ProfileForm(props: { const [birthDate, setBirthDate] = createSignal(dateInputFromIso(props.user.birth_date)); const [displayName, setDisplayName] = createSignal(props.profile?.display_name ?? ""); const [bio, setBio] = createSignal(props.profile?.bio ?? ""); + const [branch, setBranch] = createSignal(props.profile?.branch ?? ""); + // Branş is a teacher's field; a stored value outside the list still shows so + // saving the form never silently drops it. + const branchOptions = () => { + const list = props.branches ?? []; + const stored = props.profile?.branch; + return stored && !list.includes(stored) ? [stored, ...list] : list; + }; + const showBranch = () => !!props.profile && hasMinRole(props.user.role, "teacher") && branchOptions().length > 0; const [error, setError] = createSignal(""); const [flash, setFlash] = createFlash(); const [pending, setPending] = createSignal(false); @@ -93,6 +106,7 @@ export function ProfileForm(props: { const bi = bio().trim(); if (dn !== (props.profile.display_name ?? "")) b.display_name = dn; if (bi !== (props.profile.bio ?? "")) b.bio = bi; + if (showBranch() && branch() !== (props.profile.branch ?? "")) b.branch = branch(); } return b; }; @@ -160,6 +174,15 @@ export function ProfileForm(props: {

+ +
+ + +
+
setEmail(e.currentTarget.value)} /> diff --git a/src/i18n/messages.ts b/src/i18n/messages.ts index 6da4ed1..d67cc51 100644 --- a/src/i18n/messages.ts +++ b/src/i18n/messages.ts @@ -126,6 +126,8 @@ export type MessageKey = | "profile.displayNameHint" | "profile.bio" | "profile.bioHint" + | "profile.branch" + | "profile.branchNone" | "profile.bioEmpty" | "profile.classes" | "profile.noClasses" @@ -2121,6 +2123,8 @@ const en: Dict = { "profile.displayNameHint": "Shown to other people instead of your username.", "profile.bio": "About", "profile.bioHint": "A short line about yourself.", + "profile.branch": "Teaching subject (branş)", + "profile.branchNone": "Not set", "profile.bioEmpty": "No bio yet.", "profile.classes": "Classes", "profile.noClasses": "Not in any class.", @@ -4124,6 +4128,8 @@ const tr: Dict = { "profile.displayNameHint": "Diğer kişilere kullanıcı adın yerine bu gösterilir.", "profile.bio": "Hakkında", "profile.bioHint": "Kendinle ilgili kısa bir not.", + "profile.branch": "Branş", + "profile.branchNone": "Belirtilmedi", "profile.bioEmpty": "Henüz bir not yok.", "profile.classes": "Şubeler", "profile.noClasses": "Hiçbir şubede değil.", diff --git a/src/pages/profile-page.tsx b/src/pages/profile-page.tsx index cf85f1a..25d99a9 100644 --- a/src/pages/profile-page.tsx +++ b/src/pages/profile-page.tsx @@ -3,6 +3,7 @@ import { createResource } from "@/lib/create-resource"; import { useLocation, useParams } from "@tanstack/solid-router"; import { getClassesByUserId, getMyClasses } from "@/api/classes"; import { getLimits } from "@/api/limits"; +import { getSettings } from "@/api/settings"; import { getMyProfile, getUserProfile } from "@/api/users"; import { ApiError, formatApiError, type Profile } from "@/api/client"; import { RouteGuard } from "@/components/layout/route-guard"; @@ -50,6 +51,7 @@ function ProfileContent() { id === "me" ? getMyProfile() : getUserProfile(id), ); const [limits] = createResource(() => getLimits()); + const [settings] = createResource(() => getSettings().catch(() => null)); const [editing, setEditing] = createSignal(false); // The five counters that fit the profile owner's role: a student's own @@ -160,6 +162,9 @@ function ProfileContent() {
@{p().username} + + {(b) => {b()}} +

{p().bio || t("profile.bioEmpty")} @@ -220,7 +225,8 @@ function ProfileContent() { {(u) => ( {