mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 02:14:05 +00:00
feat(journey): read and write comments and followers in the client hooks
The data side of journey comments and following a link by email (#1702, #1669), for the views to draw on, online only like the journey itself: - api/journeySocial and repo/journeySocialRepo, parsed against the shared contracts. - useJourneyComments: a journey's comments grouped by entry, add, hide and delete, read again on journey:entry:commented. - useJourneySubscribers: the owner's list of followers and their removal. - useJourneyPublicSocial: the comments a share link shows, a reader's comment and follow request, each answering what it came to. - useJourneyFollow: the confirm and unsubscribe pages a follower's mail links open, posting the token once. - useJourneyShareLink switches allow_comments and allow_subscriptions on their own, like the map extras.
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
import type { JourneyComment } from '@trek/shared';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { journeySocialApi } from '../api/journeySocial';
|
||||
import { journeySocialRepo } from './journeySocialRepo';
|
||||
import { OnlineOnlyError } from './onlineOnly';
|
||||
|
||||
// FE-REPO-JOURNEY-SOCIAL-001 to FE-REPO-JOURNEY-SOCIAL-004
|
||||
|
||||
const network = vi.hoisted(() => ({ offline: false }));
|
||||
vi.mock('../sync/networkMode', () => ({ isEffectivelyOffline: () => network.offline }));
|
||||
|
||||
const COMMENT: JourneyComment = {
|
||||
id: 1,
|
||||
entry_id: 2,
|
||||
user_id: 3,
|
||||
author_name: 'ana',
|
||||
avatar_url: null,
|
||||
is_guest: false,
|
||||
body: 'Lovely',
|
||||
hidden: false,
|
||||
created_at: '2026-11-30T10:00:00Z',
|
||||
own: true,
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
network.offline = false;
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
describe('journeySocialRepo', () => {
|
||||
it('FE-REPO-JOURNEY-SOCIAL-001: the journey comments and followers need a connection', async () => {
|
||||
network.offline = true;
|
||||
const comments = vi.spyOn(journeySocialApi, 'comments');
|
||||
await expect(journeySocialRepo.comments(7)).rejects.toBeInstanceOf(OnlineOnlyError);
|
||||
await expect(journeySocialRepo.addComment(7, 2, 'x')).rejects.toBeInstanceOf(OnlineOnlyError);
|
||||
await expect(journeySocialRepo.setCommentHidden(7, 1, true)).rejects.toBeInstanceOf(OnlineOnlyError);
|
||||
await expect(journeySocialRepo.deleteComment(7, 1)).rejects.toBeInstanceOf(OnlineOnlyError);
|
||||
await expect(journeySocialRepo.subscribers(7)).rejects.toBeInstanceOf(OnlineOnlyError);
|
||||
await expect(journeySocialRepo.removeSubscriber(7, 1)).rejects.toBeInstanceOf(OnlineOnlyError);
|
||||
expect(comments).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('FE-REPO-JOURNEY-SOCIAL-002: reads and writes the comments, parsed against the contract', async () => {
|
||||
vi.spyOn(journeySocialApi, 'comments').mockResolvedValue({ comments: [COMMENT], can_moderate: true });
|
||||
vi.spyOn(journeySocialApi, 'addComment').mockResolvedValue({ comment: COMMENT });
|
||||
const hide = vi.spyOn(journeySocialApi, 'setCommentHidden').mockResolvedValue({ success: true });
|
||||
const del = vi.spyOn(journeySocialApi, 'deleteComment').mockResolvedValue({ success: true });
|
||||
expect(await journeySocialRepo.comments(7)).toEqual({ comments: [COMMENT], can_moderate: true });
|
||||
expect(await journeySocialRepo.addComment(7, 2, 'Lovely')).toEqual(COMMENT);
|
||||
await journeySocialRepo.setCommentHidden(7, 1, true);
|
||||
await journeySocialRepo.deleteComment(7, 1);
|
||||
expect(hide).toHaveBeenCalledWith(7, 1, true);
|
||||
expect(del).toHaveBeenCalledWith(7, 1);
|
||||
|
||||
vi.spyOn(journeySocialApi, 'comments').mockResolvedValue({ comments: [{ id: 'x' }], can_moderate: true } as never);
|
||||
await expect(journeySocialRepo.comments(7)).rejects.toThrow();
|
||||
});
|
||||
|
||||
it('FE-REPO-JOURNEY-SOCIAL-003: the owner reads and removes the followers', async () => {
|
||||
const list = { count: 1, pending: 0, subscribers: [] };
|
||||
vi.spyOn(journeySocialApi, 'subscribers').mockResolvedValue(list);
|
||||
const remove = vi.spyOn(journeySocialApi, 'removeSubscriber').mockResolvedValue({ success: true });
|
||||
expect(await journeySocialRepo.subscribers(7)).toEqual(list);
|
||||
await journeySocialRepo.removeSubscriber(7, 4);
|
||||
expect(remove).toHaveBeenCalledWith(7, 4);
|
||||
});
|
||||
|
||||
it('FE-REPO-JOURNEY-SOCIAL-004: the reader of a link comments, follows, confirms and stops following', async () => {
|
||||
// The shared page keeps no offline copy: these go out and the answer says whether they landed.
|
||||
network.offline = true;
|
||||
const publicComment = { id: 1, entry_id: 2, author_name: 'Bo', is_guest: true, body: 'Hi', created_at: null };
|
||||
vi.spyOn(journeySocialApi, 'publicComments').mockResolvedValue({ comments: [publicComment] });
|
||||
const add = vi.spyOn(journeySocialApi, 'addPublicComment').mockResolvedValue({ success: true });
|
||||
const follow = vi.spyOn(journeySocialApi, 'subscribe').mockResolvedValue({ success: true });
|
||||
vi.spyOn(journeySocialApi, 'confirmSubscription').mockResolvedValue({ title: 'Azores', link: 'tok' });
|
||||
const stop = vi.spyOn(journeySocialApi, 'unsubscribe').mockResolvedValue({ success: true });
|
||||
|
||||
expect(await journeySocialRepo.publicComments('tok')).toEqual({ comments: [publicComment] });
|
||||
await journeySocialRepo.addPublicComment('tok', 2, { name: 'Bo', body: 'Hi' });
|
||||
await journeySocialRepo.subscribe('tok', { email: 'bo@example.com', locale: 'de' });
|
||||
expect(await journeySocialRepo.confirmSubscription('c'.repeat(32))).toEqual({ title: 'Azores', link: 'tok' });
|
||||
await journeySocialRepo.unsubscribe('u'.repeat(32));
|
||||
expect(add).toHaveBeenCalledWith('tok', 2, { name: 'Bo', body: 'Hi' });
|
||||
expect(follow).toHaveBeenCalledWith('tok', { email: 'bo@example.com', locale: 'de' });
|
||||
expect(stop).toHaveBeenCalledWith('u'.repeat(32));
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,75 @@
|
||||
import {
|
||||
journeyCommentCreateResponseSchema,
|
||||
journeyCommentsResponseSchema,
|
||||
journeyPublicCommentsResponseSchema,
|
||||
journeyShareSubscribersResponseSchema,
|
||||
journeySubscriptionConfirmResponseSchema,
|
||||
type JourneyComment,
|
||||
type JourneyCommentsResponse,
|
||||
type JourneyPublicCommentCreateRequest,
|
||||
type JourneyPublicCommentsResponse,
|
||||
type JourneyPublicSubscribeRequest,
|
||||
type JourneyShareSubscribersResponse,
|
||||
type JourneySubscriptionConfirmResponse,
|
||||
} from '@trek/shared';
|
||||
import { journeySocialApi } from '../api/journeySocial';
|
||||
import { requireOnline } from './onlineOnly';
|
||||
|
||||
/**
|
||||
* The social side of a journey (#1702, #1669), online only on purpose like the
|
||||
* journey itself: comments are a conversation, read when the journey is open
|
||||
* and written while the server can pass them on, and the share link is a page
|
||||
* on the network anyway. Answers are parsed against the shared contract.
|
||||
*/
|
||||
export const journeySocialRepo = {
|
||||
async comments(journeyId: number, signal?: AbortSignal): Promise<JourneyCommentsResponse> {
|
||||
requireOnline();
|
||||
return journeyCommentsResponseSchema.parse(await journeySocialApi.comments(journeyId, signal));
|
||||
},
|
||||
|
||||
async addComment(journeyId: number, entryId: number, body: string): Promise<JourneyComment> {
|
||||
requireOnline();
|
||||
return journeyCommentCreateResponseSchema.parse(await journeySocialApi.addComment(journeyId, entryId, body))
|
||||
.comment;
|
||||
},
|
||||
|
||||
async setCommentHidden(journeyId: number, commentId: number, hidden: boolean): Promise<void> {
|
||||
requireOnline();
|
||||
await journeySocialApi.setCommentHidden(journeyId, commentId, hidden);
|
||||
},
|
||||
|
||||
async deleteComment(journeyId: number, commentId: number): Promise<void> {
|
||||
requireOnline();
|
||||
await journeySocialApi.deleteComment(journeyId, commentId);
|
||||
},
|
||||
|
||||
async subscribers(journeyId: number, signal?: AbortSignal): Promise<JourneyShareSubscribersResponse> {
|
||||
requireOnline();
|
||||
return journeyShareSubscribersResponseSchema.parse(await journeySocialApi.subscribers(journeyId, signal));
|
||||
},
|
||||
|
||||
async removeSubscriber(journeyId: number, subscriberId: number): Promise<void> {
|
||||
requireOnline();
|
||||
await journeySocialApi.removeSubscriber(journeyId, subscriberId);
|
||||
},
|
||||
|
||||
async publicComments(token: string, signal?: AbortSignal): Promise<JourneyPublicCommentsResponse> {
|
||||
return journeyPublicCommentsResponseSchema.parse(await journeySocialApi.publicComments(token, signal));
|
||||
},
|
||||
|
||||
async addPublicComment(token: string, entryId: number, body: JourneyPublicCommentCreateRequest): Promise<void> {
|
||||
await journeySocialApi.addPublicComment(token, entryId, body);
|
||||
},
|
||||
|
||||
async subscribe(token: string, body: JourneyPublicSubscribeRequest): Promise<void> {
|
||||
await journeySocialApi.subscribe(token, body);
|
||||
},
|
||||
|
||||
async confirmSubscription(mailToken: string): Promise<JourneySubscriptionConfirmResponse> {
|
||||
return journeySubscriptionConfirmResponseSchema.parse(await journeySocialApi.confirmSubscription(mailToken));
|
||||
},
|
||||
|
||||
async unsubscribe(mailToken: string): Promise<void> {
|
||||
await journeySocialApi.unsubscribe(mailToken);
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user