Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/db/sqlite/friends.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface FriendPerson {
/** Null when their member row is gone. */
serverUserId: string | null;
nickname: string | null;
avatarFileId: string | null;
/** When the friendship, or the request, was made. */
at: string;
}
Expand All @@ -34,12 +35,13 @@ function cutoff(now = Date.now()): string {
return toIso(new Date(now - FRIEND_REQUEST_TTL_MS));
}

type PersonRow = { gryt_user_id: string; server_user_id: string | null; nickname: string | null; at: string };
type PersonRow = { gryt_user_id: string; server_user_id: string | null; nickname: string | null; avatar_file_id: string | null; at: string };

const toPerson = (r: PersonRow): FriendPerson => ({
grytUserId: r.gryt_user_id,
serverUserId: r.server_user_id,
nickname: r.nickname,
avatarFileId: r.avatar_file_id,
at: r.at,
});

Expand All @@ -59,7 +61,7 @@ export async function hasAnyFriend(grytUserId: string): Promise<boolean> {
export async function listFriends(grytUserId: string): Promise<FriendPerson[]> {
const rows = getSqliteDb()
.prepare(
`SELECT f.other AS gryt_user_id, u.server_user_id, u.nickname, f.created_at AS at
`SELECT f.other AS gryt_user_id, u.server_user_id, u.nickname, u.avatar_file_id, f.created_at AS at
FROM (SELECT CASE WHEN a_gryt_user_id = ? THEN b_gryt_user_id ELSE a_gryt_user_id END AS other, created_at
FROM friendships WHERE a_gryt_user_id = ? OR b_gryt_user_id = ?) f
LEFT JOIN users u ON u.gryt_user_id = f.other
Expand Down Expand Up @@ -110,15 +112,15 @@ export async function listFriendRequests(grytUserId: string): Promise<FriendRequ
const since = cutoff();
const incoming = db
.prepare(
`SELECT r.from_gryt_user_id AS gryt_user_id, u.server_user_id, u.nickname, r.created_at AS at
`SELECT r.from_gryt_user_id AS gryt_user_id, u.server_user_id, u.nickname, u.avatar_file_id, r.created_at AS at
FROM friend_requests r LEFT JOIN users u ON u.gryt_user_id = r.from_gryt_user_id
WHERE r.to_gryt_user_id = ? AND r.ignored = 0 AND r.created_at > ?
ORDER BY r.created_at`,
)
.all(grytUserId, since) as PersonRow[];
const outgoing = db
.prepare(
`SELECT r.to_gryt_user_id AS gryt_user_id, u.server_user_id, u.nickname, r.created_at AS at
`SELECT r.to_gryt_user_id AS gryt_user_id, u.server_user_id, u.nickname, u.avatar_file_id, r.created_at AS at
FROM friend_requests r LEFT JOIN users u ON u.gryt_user_id = r.to_gryt_user_id
WHERE r.from_gryt_user_id = ? AND r.created_at > ?
ORDER BY r.created_at`,
Expand Down
3 changes: 2 additions & 1 deletion src/socket/handlers/friends.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ const NOT_A_MEMBER = { error: "unknown_member", message: "That person is not a m
export interface FriendView {
serverUserId: string;
nickname: string | null;
avatarFileId: string | null;
at: string;
}

const view = (people: FriendPerson[]): FriendView[] =>
people
.filter((p): p is FriendPerson & { serverUserId: string } => !!p.serverUserId)
.map((p) => ({ serverUserId: p.serverUserId, nickname: p.nickname, at: p.at }));
.map((p) => ({ serverUserId: p.serverUserId, nickname: p.nickname, avatarFileId: p.avatarFileId, at: p.at }));

/** What one member is shown. A request from somebody either of them blocked isn't. */
export async function friendListFor(grytUserId: string): Promise<{ friends: FriendView[]; incoming: FriendView[]; outgoing: FriendView[] }> {
Expand Down
Loading