Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Github Workflows #1

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
29 changes: 29 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Jest Coverage Comment
on:
pull_request:
jobs:
build:
permissions:
checks: write
pull-requests: write
contents: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
node: 20

- uses: actions/setup-node@v4
with:
node-version: "20"

- name: Install dependencies
run: yarn install

- name: Jest Coverage Comment
uses: artiomtr/[email protected]
with:
package-manager: yarn
test-script: yarn run test --ci --json --coverage --testLocationInResults --outputFile=report.json
github-token: ${{ secrets.GITHUB_TOKEN }}

4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ module.exports = {
collectCoverageFrom: [
'./src/**/*.ts'
],
coverageReporters: [
'html-spa',
'json-summary',
],
moduleNameMapper,
preset: 'ts-jest',
testEnvironment: 'node',
Expand Down
18 changes: 18 additions & 0 deletions src/auth/cookies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import store, { FRENFIT_COOKE_KEY, JSESSIONID_KEY } from '@support/store';

export interface AuthCookies {
jsessionId: string;
frenfitCookie: string;
}

export const clearAuthCookies = () => setAuthCookies(null, null);

export const getAuthCookies = (): AuthCookies => ({
jsessionId: store.get(JSESSIONID_KEY) as string | null,
frenfitCookie: store.get(FRENFIT_COOKE_KEY) as string | null,
});

export const setAuthCookies = (jsessionId?: string, frenfitCookie?: string) => {
store.set(JSESSIONID_KEY, jsessionId);
store.set(FRENFIT_COOKE_KEY, frenfitCookie);
};
10 changes: 8 additions & 2 deletions src/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import { clearData, ffetch } from '@support/client';
import { InvalidCredentialsException, UnexpectedResponseException } from '@support/exceptions';
import store, { ME_KEY } from '@support/store';

import { clearAuthCookies, getAuthCookies } from './cookies';
import endpoints from './endpoints';
import { LoginRequest, MeResponse } from './types';

export * from './cookies';
export default './endpoints';
export * from './types';

export const authEndpoints = endpoints;

Expand Down Expand Up @@ -37,13 +40,16 @@ export const login = async (request: LoginRequest) => {
if (location.includes('/login/authfail?login_error')) {
throw new InvalidCredentialsException();
}

return getAuthCookies();
};

export const logout = async () => {
await ffetch(endpoints.logout);
clearAuthCookies();
};

export const getMe = async (fetch = false) => {
export const getMe = async (fetch = false): Promise<MeResponse> => {
let me = store.get(ME_KEY) as MeResponse | undefined;

if (!me || fetch) {
Expand All @@ -55,10 +61,10 @@ export const getMe = async (fetch = false) => {

return {
avatarUrl: me.avatarUrl,
class: 'it.senape.ff.dto.UserDto',
fullName: me.fullName,
id: me.id,
locked: me.locked,
username: me.username,
type: 'User',
};
};
33 changes: 1 addition & 32 deletions src/comment/reactions.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,8 @@
import { MyPreference, type Preferences } from '@frenfit-types';
import { ffetch } from '@support/client';
import { evaluateReactionResponse } from '@support/reactions';

import endpoints from './endpoints';

const evaluateReactionResponse = (text: string): Preferences => {
let myPreference: MyPreference = MyPreference.none;

let pattern = /<i class="icon icon-dislike(?<disliked>-active)">/g;
let match = pattern.exec(text);

if (match?.groups?.disliked) {
myPreference = MyPreference.dislike;
} else {
pattern = /<i class="icon icon-like(?<liked>-active)">/g;
match = pattern.exec(text);
if (match?.groups?.liked) {
myPreference = MyPreference.like;
}
}

pattern = /data-retrieve="like">(?<likes>\d+)<\/a>/;
match = pattern.exec(text);
const likes = parseInt(match?.groups?.likes, 10) || 0;

pattern = /data-retrieve="dislike">(?<dislikes>\d+)<\/a>/;
match = pattern.exec(text);
const dislikes = parseInt(match?.groups?.dislikes, 10) || 0;

return {
dislikes,
likes,
myPreference,
};
};

export const toggleCommentDislike = async (commentId: number) => {
const dislikeResponse = await ffetch(endpoints.dislikeComment(commentId));
const text = await dislikeResponse.text();
Expand Down
4 changes: 4 additions & 0 deletions src/comment/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { decodeHTML } from 'entities';

import type { Comment } from '@frenfit-types';
import { decodeUser } from '@sdk/friend';

export const decodeComment = (comment: Comment): Comment =>
Object.assign({}, comment, {
dislikers: (comment.dislikers || []).map(decodeUser),
likers: (comment.likers || []).map(decodeUser),
origin: decodeHTML(comment.origin),
sender: decodeUser(comment.sender),
text: decodeHTML(comment.text),
});
5 changes: 4 additions & 1 deletion src/feed/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import { apiURL, frontendURL } from '@support/client';
export default {
bookmark: (entryId: number) => frontendURL('bookmark/bookmark/?id={entryId}', { entryId }),
deleteEntry: (entryId: number) => frontendURL('feed/remove?id={entryId}', { entryId }),
dislikeEntry: (id: number) => frontendURL('feed/dislike?id={id}', { id }),
editEntry: frontendURL('message/editEntry'),
emptyRoom: frontendURL('empty'),
entry: (id: number) => apiURL('entry/{id}', { id }),
hideEntry: frontendURL('feed/hideEntry'),
likeEntry: (id: number) => frontendURL('feed/like?id={id}', { id }),
postEntry: frontendURL('message/postToFrenfi'),
restoreEntry: (entryId: number) => frontendURL('feed/restore/{entryId}', { entryId }),
unhideEntry: (id: number) => frontendURL('feed/restoreHiddenEntry?id={id}&restore=true', { id }),
};
23 changes: 23 additions & 0 deletions src/feed/entries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ffetch } from '@support/client';

import { ListEntriesResponse } from './types';

export const listEntryIds = async (url: URL): Promise<ListEntriesResponse> => {
const response = await ffetch(url);

const text = await response.text();
const pattern = /"entry"\s+id="(\d+)"/gm;
const match = text.matchAll(pattern);

const ids = [...match].map(([, id]) => parseInt(id, 10));

const [, [pagination]] = text.split('<div class="pagination">').map(p => p.split('</ul>'));
const pieces = pagination.split('<li ');
const nextPage = !pieces[2].includes('disabled');

return {
ids,
page: parseInt(url.searchParams.get('page') || '1'),
nextPage,
};
};
26 changes: 16 additions & 10 deletions src/feed/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { ffetch } from '@support/client';
import { ffetch, frontendURL } from '@support/client';
import { FrenfitException, UnexpectedResponseException } from '@support/exceptions';

import { toggleBookmark } from './bookmark';
import endpoints from './endpoints';
import { AddEntryRequest, EditEntryRequest, EntryResponse } from './types';
import { listEntryIds } from './entries';
import { AddEntryRequest, EditEntryRequest, EntryResponse, ListEntriesRequest } from './types';
import { decodeEntry } from './utils';

export * from './bookmark';
export default './endpoints';
export * from './entries';
export * from './reactions';
export * from './recipients';
export * from './types';

const ALLOWED_FILE_TYPES = ['image/png', 'image/x-png', 'image/gif', 'image/jpeg'];

Expand Down Expand Up @@ -69,6 +73,16 @@ export const getEntry = async (
return decodeEntry(entry);
};

export const listEntries = async (request: ListEntriesRequest) => {
const url = new URL(frontendURL(request.feed));
request.page && url.searchParams.set('page', String(request.page));

const response = await listEntryIds(url);
const entries = await Promise.all(response.ids.map(id => getEntry(id)));

return Object.assign({ entries }, response);
};

export const postEntry = async (request: AddEntryRequest) => {
const body = new FormData();

Expand Down Expand Up @@ -105,11 +119,3 @@ export const postEntry = async (request: AddEntryRequest) => {
const entryId = parseInt(match.groups.entryId, 10);
return await getEntry(entryId, { checkIfBookmarked: false });
};

export const restoreEntry = async (id: number) => {
await ffetch(endpoints.restoreEntry(id), {
headers: {
'X-Requested-With': 'XMLHttpRequest',
},
});
};
63 changes: 63 additions & 0 deletions src/feed/reactions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { ffetch } from '@support/client';
import { UnexpectedResponseException } from '@support/exceptions';
import { evaluateReactionResponse } from '@support/reactions';

import endpoints from './endpoints';

import { getEntry } from '.';

export const hideEntry = async (entryId: number) => {
const body = new URLSearchParams();
body.set('id', String(entryId));

const hideResponse = await ffetch(endpoints.hideEntry, {
method: 'POST',
headers: {
'X-Requested-With': 'XMLHttpRequest',
},
body,
});

const text = await hideResponse.text();

return text.includes(`restoreEntry('${entryId}')`);
};

export const toggleEntryDislike = async (entryId: number) => {
const dislikeResponse = await ffetch(endpoints.dislikeEntry(entryId), {
headers: {
'X-Requested-With': 'XMLHttpRequest',
},
});
const text = await dislikeResponse.text();

return evaluateReactionResponse(text);
};

export const toggleEntryLike = async (entryId: number) => {
const likeResponse = await ffetch(endpoints.likeEntry(entryId), {
headers: {
'X-Requested-With': 'XMLHttpRequest',
},
});
const text = await likeResponse.text();

return evaluateReactionResponse(text);
};

export const unhideEntry = async (entryId: number) => {
const unhideResponse = await ffetch(endpoints.unhideEntry(entryId), {
headers: {
'X-Requested-With': 'XMLHttpRequest',
},
});

const text = await unhideResponse.text();
if (!text.includes(`id="${entryId}"`)) {
throw new UnexpectedResponseException(unhideResponse, {
missingEntry: 'The requested entry was not returned after un-hide',
});
}

return await getEntry(entryId);
};
14 changes: 13 additions & 1 deletion src/feed/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Message } from '@frenfit-types';
import type { FeedPath, Message } from '@frenfit-types';

export type AddEntryRequest = {
recipientIds?: number[];
Expand Down Expand Up @@ -27,6 +27,18 @@ export type FeedInfoResponse<T> = {
email?: string;
};

export type ListEntriesRequest = {
feed: FeedPath;
page?: number;
};

export type ListEntriesResponse = {
entries?: Message[];
ids: number[];
page: number;
nextPage: boolean;
};

export type TargetFeed = {
fullname: string;
name: string;
Expand Down
4 changes: 4 additions & 0 deletions src/feed/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ import { decodeHTML } from 'entities';

import type { Message, Recipient } from '@frenfit-types';
import { decodeComment } from '@sdk/comment';
import { decodeUser } from '@sdk/friend';

export const decodeEntry = (entry: Message): Message =>
Object.assign({}, entry, {
comments: (entry.comments || []).map(decodeComment),
dislikers: (entry.dislikers || []).map(decodeUser),
firstComment: entry.firstComment ? decodeComment(entry.firstComment) : undefined,
lastComment: entry.lastComment ? decodeComment(entry.lastComment) : undefined,
likers: (entry.likers || []).map(decodeUser),
origin: entry.origin ? decodeHTML(entry.origin) : undefined,
providers: entry.providers.map(decodeRecipient),
sender: decodeUser(entry.sender),
text: decodeHTML(entry.text),
});

Expand Down
Loading
Loading