mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-11-08 05:03:39 +08:00
update after rename friends to connections
This commit is contained in:
parent
76a0a19688
commit
4a408d8857
8 changed files with 62 additions and 64 deletions
|
|
@ -14,7 +14,7 @@ import {
|
|||
} from "../elements/test-activity-calendar";
|
||||
import { Preset } from "@monkeytype/schemas/presets";
|
||||
import { Language } from "@monkeytype/schemas/languages";
|
||||
import { FriendRequestStatus } from "@monkeytype/schemas/friends";
|
||||
import { ConnectionStatus } from "@monkeytype/schemas/connections";
|
||||
|
||||
export type SnapshotUserTag = UserTag & {
|
||||
active?: boolean;
|
||||
|
|
@ -85,7 +85,7 @@ export type Snapshot = Omit<
|
|||
xp: number;
|
||||
testActivity?: ModifiableTestActivityCalendar;
|
||||
testActivityByYear?: { [key: string]: TestActivityCalendar };
|
||||
friends: Record<string, FriendRequestStatus | "incoming">;
|
||||
connections: Record<string, ConnectionStatus | "incoming">;
|
||||
};
|
||||
|
||||
export type SnapshotPreset = Preset & {
|
||||
|
|
@ -133,7 +133,7 @@ const defaultSnap = {
|
|||
60: { english: { count: 0, rank: 0 } },
|
||||
},
|
||||
},
|
||||
friends: {},
|
||||
connections: {},
|
||||
} as Snapshot;
|
||||
|
||||
export function getDefaultSnapshot(): Snapshot {
|
||||
|
|
|
|||
|
|
@ -91,16 +91,16 @@ export async function initSnapshot(): Promise<Snapshot | false> {
|
|||
try {
|
||||
if (!isAuthenticated()) return false;
|
||||
|
||||
const friendsRequest = getServerConfiguration()?.friends.enabled
|
||||
? Ape.friends.getRequests()
|
||||
const connectionsRequest = getServerConfiguration()?.connections.enabled
|
||||
? Ape.connections.get()
|
||||
: { status: 200, body: { message: "", data: [] } };
|
||||
|
||||
const [userResponse, configResponse, presetsResponse, friendsResponse] =
|
||||
const [userResponse, configResponse, presetsResponse, connectionsResponse] =
|
||||
await Promise.all([
|
||||
Ape.users.get(),
|
||||
Ape.configs.get(),
|
||||
Ape.presets.get(),
|
||||
friendsRequest,
|
||||
connectionsRequest,
|
||||
]);
|
||||
|
||||
if (userResponse.status !== 200) {
|
||||
|
|
@ -121,17 +121,17 @@ export async function initSnapshot(): Promise<Snapshot | false> {
|
|||
presetsResponse.status
|
||||
);
|
||||
}
|
||||
if (friendsResponse.status !== 200) {
|
||||
if (connectionsResponse.status !== 200) {
|
||||
throw new SnapshotInitError(
|
||||
`${friendsResponse.body.message} (friendRequests)`,
|
||||
friendsResponse.status
|
||||
`${connectionsResponse.body.message} (connections)`,
|
||||
connectionsResponse.status
|
||||
);
|
||||
}
|
||||
|
||||
const userData = userResponse.body.data;
|
||||
const configData = configResponse.body.data;
|
||||
const presetsData = presetsResponse.body.data;
|
||||
const friendsData = friendsResponse.body.data;
|
||||
const connectionsData = connectionsResponse.body.data;
|
||||
|
||||
if (userData === null) {
|
||||
throw new SnapshotInitError(
|
||||
|
|
@ -268,16 +268,16 @@ export async function initSnapshot(): Promise<Snapshot | false> {
|
|||
);
|
||||
}
|
||||
|
||||
snap.friends = Object.fromEntries(
|
||||
friendsData.map((friend) => {
|
||||
// oxlint-disable-next-line no-non-null-assertion
|
||||
const isMyRequest = getAuthenticatedUser()!.uid === friend.initiatorUid;
|
||||
snap.connections = Object.fromEntries(
|
||||
connectionsData.map((connection) => {
|
||||
const isMyRequest =
|
||||
getAuthenticatedUser()?.uid === connection.initiatorUid;
|
||||
|
||||
return [
|
||||
isMyRequest ? friend.friendUid : friend.initiatorUid,
|
||||
friend.status === "pending" && !isMyRequest
|
||||
isMyRequest ? connection.friendUid : connection.initiatorUid,
|
||||
connection.status === "pending" && !isMyRequest
|
||||
? "incoming"
|
||||
: friend.status,
|
||||
: connection.status,
|
||||
];
|
||||
})
|
||||
);
|
||||
|
|
|
|||
|
|
@ -80,7 +80,7 @@ export function update(): void {
|
|||
}
|
||||
|
||||
export function updateFriendRequestsIndicator(): void {
|
||||
const friends = getSnapshot()?.friends;
|
||||
const friends = getSnapshot()?.connections;
|
||||
|
||||
if (friends !== undefined) {
|
||||
const pendingFriendRequests = Object.values(friends).filter(
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import * as Notifications from "../../elements/notifications";
|
||||
import { FriendRequest } from "@monkeytype/schemas/friends";
|
||||
import { Connection } from "@monkeytype/schemas/connections";
|
||||
import Ape from "../../ape";
|
||||
import { format } from "date-fns/format";
|
||||
import { isAuthenticated } from "../../firebase";
|
||||
|
|
@ -7,7 +7,7 @@ import { getFriendUid } from "../../pages/friends";
|
|||
import * as DB from "../../db";
|
||||
import { updateFriendRequestsIndicator } from "../account-button";
|
||||
|
||||
let blockedUsers: FriendRequest[] = [];
|
||||
let blockedUsers: Connection[] = [];
|
||||
const element = $("#pageAccountSettings .tab[data-tab='blockedUsers']");
|
||||
|
||||
async function getData(): Promise<boolean> {
|
||||
|
|
@ -18,7 +18,7 @@ async function getData(): Promise<boolean> {
|
|||
return false;
|
||||
}
|
||||
|
||||
const response = await Ape.friends.getRequests({
|
||||
const response = await Ape.connections.get({
|
||||
query: { status: "blocked", type: "incoming" },
|
||||
});
|
||||
|
||||
|
|
@ -83,7 +83,7 @@ element.on("click", "table button.delete", async (e) => {
|
|||
throw new Error("Cannot find id of target.");
|
||||
}
|
||||
|
||||
const response = await Ape.friends.deleteRequest({ params: { id } });
|
||||
const response = await Ape.connections.delete({ params: { id } });
|
||||
if (response.status !== 200) {
|
||||
Notifications.add(`Cannot unblock user: ${response.body.message}`, -1);
|
||||
} else {
|
||||
|
|
@ -99,7 +99,7 @@ element.on("click", "table button.delete", async (e) => {
|
|||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete, @typescript-eslint/no-unsafe-member-access
|
||||
delete snapshot.friends[uid];
|
||||
delete snapshot.connections[uid];
|
||||
updateFriendRequestsIndicator();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -436,7 +436,7 @@ export function updateNameFontSize(where: ProfileViewPaths): void {
|
|||
function isFriend(uid: string | undefined): boolean {
|
||||
if (uid === undefined || uid === getAuthenticatedUser()?.uid) return false;
|
||||
|
||||
return Object.entries(DB.getSnapshot()?.friends ?? []).some(
|
||||
return Object.entries(DB.getSnapshot()?.connections ?? []).some(
|
||||
([friendUid, status]) => friendUid === uid && status === "accepted"
|
||||
);
|
||||
}
|
||||
|
|
@ -449,8 +449,8 @@ export function updateFriendRequestButton(): void {
|
|||
const button = document.querySelector(".profile .addFriendButton");
|
||||
|
||||
const myProfile = myUid === profileUid;
|
||||
const hasRequest = DB.getSnapshot()?.friends[profileUid] !== undefined;
|
||||
const featureEnabled = getServerConfiguration()?.friends.enabled;
|
||||
const hasRequest = DB.getSnapshot()?.connections[profileUid] !== undefined;
|
||||
const featureEnabled = getServerConfiguration()?.connections.enabled;
|
||||
|
||||
if (!featureEnabled || myUid === undefined || myProfile || hasRequest) {
|
||||
button?.classList.add("disabled");
|
||||
|
|
|
|||
|
|
@ -16,7 +16,6 @@ import { secondsToString } from "../utils/date-and-time";
|
|||
import { PersonalBest } from "@monkeytype/schemas/shared";
|
||||
import Format from "../utils/format";
|
||||
import { getHtmlByUserFlags } from "../controllers/user-flag-controller";
|
||||
import { Friend, FriendRequest } from "@monkeytype/schemas/friends";
|
||||
import { SortedTable } from "../utils/sorted-table";
|
||||
import { getAvatarElement } from "../utils/discord-avatar";
|
||||
import { formatTypingStatsRatio } from "../utils/misc";
|
||||
|
|
@ -25,27 +24,29 @@ import * as DB from "../db";
|
|||
import { getAuthenticatedUser } from "../firebase";
|
||||
import * as ServerConfiguration from "../ape/server-configuration";
|
||||
import * as AuthEvent from "../observables/auth-event";
|
||||
import { Connection } from "@monkeytype/schemas/connections";
|
||||
import { Friend } from "@monkeytype/schemas/users";
|
||||
|
||||
const pageElement = $(".page.pageFriends");
|
||||
|
||||
let friendsTable: SortedTable<Friend> | undefined = undefined;
|
||||
|
||||
let pendingRequests: FriendRequest[] | undefined;
|
||||
let pendingRequests: Connection[] | undefined;
|
||||
let friendsList: Friend[] | undefined;
|
||||
|
||||
export function getFriendUid(
|
||||
friendRequest: Pick<FriendRequest, "initiatorUid" | "friendUid">
|
||||
connection: Pick<Connection, "initiatorUid" | "friendUid">
|
||||
): string {
|
||||
const me = getAuthenticatedUser();
|
||||
if (me === null)
|
||||
throw new Error("expected to be authenticated in getFriendUid");
|
||||
|
||||
if (me.uid === friendRequest.initiatorUid) return friendRequest.friendUid;
|
||||
return friendRequest.initiatorUid;
|
||||
if (me.uid === connection.initiatorUid) return connection.friendUid;
|
||||
return connection.initiatorUid;
|
||||
}
|
||||
|
||||
export async function addFriend(friendName: string): Promise<true | string> {
|
||||
const result = await Ape.friends.createRequest({ body: { friendName } });
|
||||
const result = await Ape.connections.create({ body: { friendName } });
|
||||
|
||||
if (result.status !== 200) {
|
||||
return `Friend request failed: ${result.body.message}`;
|
||||
|
|
@ -54,8 +55,8 @@ export async function addFriend(friendName: string): Promise<true | string> {
|
|||
if (snapshot !== undefined) {
|
||||
const friendUid = getFriendUid(result.body.data);
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
snapshot.friends[friendUid] = result.body.data.status;
|
||||
updatePendingRequests();
|
||||
snapshot.connections[friendUid] = result.body.data.status;
|
||||
updatePendingConnections();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
|
@ -90,15 +91,15 @@ const removeFriendModal = new SimpleModal({
|
|||
thisPopup.text = `Are you sure you want to remove ${thisPopup.parameters[1]} as a friend?`;
|
||||
},
|
||||
execFn: async (thisPopup) => {
|
||||
const friendRequestId = thisPopup.parameters[0] as string;
|
||||
const result = await Ape.friends.deleteRequest({
|
||||
params: { id: friendRequestId },
|
||||
const connectionId = thisPopup.parameters[0] as string;
|
||||
const result = await Ape.connections.delete({
|
||||
params: { id: connectionId },
|
||||
});
|
||||
if (result.status !== 200) {
|
||||
return { status: -1, message: result.body.message };
|
||||
} else {
|
||||
friendsList = friendsList?.filter(
|
||||
(it) => it.friendRequestId !== friendRequestId
|
||||
(it) => it.connectionId !== connectionId
|
||||
);
|
||||
friendsTable?.setData(friendsList ?? []);
|
||||
friendsTable?.updateBody();
|
||||
|
|
@ -107,23 +108,20 @@ const removeFriendModal = new SimpleModal({
|
|||
},
|
||||
});
|
||||
|
||||
async function fetchPendingRequests(): Promise<void> {
|
||||
const result = await Ape.friends.getRequests({
|
||||
async function fetchPendingConnections(): Promise<void> {
|
||||
const result = await Ape.connections.get({
|
||||
query: { status: "pending", type: "incoming" },
|
||||
});
|
||||
|
||||
if (result.status !== 200) {
|
||||
Notifications.add(
|
||||
"Error getting friend requests: " + result.body.message,
|
||||
-1
|
||||
);
|
||||
Notifications.add("Error getting connections: " + result.body.message, -1);
|
||||
pendingRequests = undefined;
|
||||
} else {
|
||||
pendingRequests = result.body.data;
|
||||
}
|
||||
}
|
||||
|
||||
function updatePendingRequests(): void {
|
||||
function updatePendingConnections(): void {
|
||||
$(".pageFriends .pendingRequests").addClass("hidden");
|
||||
|
||||
if (pendingRequests === undefined || pendingRequests.length === 0) {
|
||||
|
|
@ -160,7 +158,7 @@ function updatePendingRequests(): void {
|
|||
}
|
||||
|
||||
async function fetchFriends(): Promise<void> {
|
||||
const result = await Ape.friends.getFriends();
|
||||
const result = await Ape.users.getFriends();
|
||||
if (result.status !== 200) {
|
||||
Notifications.add("Error getting friends: " + result.body.message, -1);
|
||||
friendsList = undefined;
|
||||
|
|
@ -204,7 +202,7 @@ function buildFriendRow(entry: Friend): HTMLTableRowElement {
|
|||
const top60 = formatPb(entry.top60);
|
||||
|
||||
const element = document.createElement("tr");
|
||||
element.dataset["friendRequestId"] = entry.friendRequestId;
|
||||
element.dataset["connectionId"] = entry.connectionId;
|
||||
|
||||
const isMe = entry.uid === getAuthenticatedUser()?.uid;
|
||||
|
||||
|
|
@ -369,10 +367,10 @@ $(".pageFriends .pendingRequests table").on("click", async (e) => {
|
|||
|
||||
const result =
|
||||
action === "rejected"
|
||||
? await Ape.friends.deleteRequest({
|
||||
? await Ape.connections.delete({
|
||||
params: { id },
|
||||
})
|
||||
: await Ape.friends.updateRequest({
|
||||
: await Ape.connections.update({
|
||||
params: { id },
|
||||
body: { status: action as "accepted" | "blocked" },
|
||||
});
|
||||
|
|
@ -396,8 +394,8 @@ $(".pageFriends .pendingRequests table").on("click", async (e) => {
|
|||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete, @typescript-eslint/no-unsafe-member-access
|
||||
delete snapshot.friends[friendUid];
|
||||
updatePendingRequests();
|
||||
delete snapshot.connections[friendUid];
|
||||
updatePendingConnections();
|
||||
}
|
||||
if (count === 1) {
|
||||
$(".pageFriends .pendingRequests").addClass("hidden");
|
||||
|
|
@ -419,9 +417,9 @@ $(".pageFriends .friends table").on("click", async (e) => {
|
|||
if (action === undefined) return;
|
||||
|
||||
if (action === "remove") {
|
||||
const friendRequestId =
|
||||
e.target.parentElement?.parentElement?.dataset["friendRequestId"];
|
||||
if (friendRequestId === undefined) {
|
||||
const connectionId =
|
||||
e.target.parentElement?.parentElement?.dataset["connectionId"];
|
||||
if (connectionId === undefined) {
|
||||
throw new Error("Cannot find id of target.");
|
||||
}
|
||||
|
||||
|
|
@ -429,7 +427,7 @@ $(".pageFriends .friends table").on("click", async (e) => {
|
|||
e.target.parentElement?.parentElement?.querySelector("a.entryName")
|
||||
?.textContent ?? "";
|
||||
|
||||
removeFriendModal.show([friendRequestId, name], {});
|
||||
removeFriendModal.show([connectionId, name], {});
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -443,18 +441,18 @@ export const page = new Page<undefined>({
|
|||
waitFor: async () => {
|
||||
await ServerConfiguration.configurationPromise;
|
||||
const serverConfig = ServerConfiguration.get();
|
||||
if (!serverConfig?.friends.enabled) {
|
||||
throw new Error("Friends are disabled.");
|
||||
if (!serverConfig?.connections.enabled) {
|
||||
throw new Error("Connectins are disabled.");
|
||||
}
|
||||
|
||||
if (friendsList !== undefined && pendingRequests !== undefined) {
|
||||
setTimeout(async () => {
|
||||
await Promise.all([fetchPendingRequests(), fetchFriends()]);
|
||||
updatePendingRequests();
|
||||
await Promise.all([fetchPendingConnections(), fetchFriends()]);
|
||||
updatePendingConnections();
|
||||
updateFriends();
|
||||
}, 0);
|
||||
} else {
|
||||
await Promise.all([fetchPendingRequests(), fetchFriends()]);
|
||||
await Promise.all([fetchPendingConnections(), fetchFriends()]);
|
||||
}
|
||||
},
|
||||
style: "bar",
|
||||
|
|
@ -474,7 +472,7 @@ export const page = new Page<undefined>({
|
|||
beforeShow: async (): Promise<void> => {
|
||||
Skeleton.append("pageFriends", "main");
|
||||
|
||||
updatePendingRequests();
|
||||
updatePendingConnections();
|
||||
updateFriends();
|
||||
},
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1438,7 +1438,7 @@ export const page = new PageWithUrlParams({
|
|||
function isFriend(uid: string): boolean {
|
||||
if (uid === getAuthenticatedUser()?.uid) return false;
|
||||
|
||||
return Object.entries(DB.getSnapshot()?.friends ?? []).some(
|
||||
return Object.entries(DB.getSnapshot()?.connections ?? []).some(
|
||||
([friendUid, status]) => friendUid === uid && status === "accepted"
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ $(async (): Promise<void> => {
|
|||
$(".login").addClass("hidden");
|
||||
$(".disabledNotification").removeClass("hidden");
|
||||
}
|
||||
if (!ServerConfiguration.get()?.friends.enabled) {
|
||||
if (!ServerConfiguration.get()?.connections.enabled) {
|
||||
$(".accountButtonAndMenu .goToFriends").addClass("hidden");
|
||||
}
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue