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

Hook bridget app discussion #10858

Closed
wants to merge 10 commits into from
2 changes: 1 addition & 1 deletion dotcom-rendering/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"@emotion/server": "11.11.0",
"@guardian/ab-core": "7.0.1",
"@guardian/braze-components": "18.1.0",
"@guardian/bridget": "2.6.0",
"@guardian/bridget": "0.0.0-2024-02-28-SNAPSHOT-DISC",
"@guardian/browserslist-config": "6.1.0",
"@guardian/cdk": "50.13.0",
"@guardian/commercial": "17.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ export const AbuseReportForm = ({
}

reportAbuse({
categoryId,
categoryId: categoryId.toString(),
reason,
email,
commentId,
commentId: commentId.toString(),
})
.then((response) => {
if (response.kind === 'error') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,11 @@ export const CommentForm = ({

if (body) {
const response = commentBeingRepliedTo
? await user.onReply(shortUrl, body, commentBeingRepliedTo.id)
? await user.onReply(
shortUrl,
body,
commentBeingRepliedTo.id.toString(),
)
: await user.onComment(shortUrl, body);
// Check response message for error states
if (response.kind === 'error') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const RecommendationCount = ({
setCount(newCount);
setRecommended(true);

user.onRecommend(commentId)
user.onRecommend(commentId.toString())
.then((accepted) => {
if (!accepted) {
setCount(newCount - 1);
Expand Down
164 changes: 127 additions & 37 deletions dotcom-rendering/src/components/DiscussionApps.importable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,148 @@ import { getdiscussionClient } from '../lib/bridgetApi';
import type { Reader, UserProfile } from '../lib/discussion';
import type { CommentResponse } from '../lib/discussionApi';
import { reportAbuse as reportAbuseWeb } from '../lib/discussionApi';
import type { Result } from '../lib/result';
import { error, ok, type Result } from '../lib/result';
import { Discussion, type Props as DiscussionProps } from './Discussion';

type Props = Omit<DiscussionProps, 'user' | 'reportAbuseUnauthenticated'>;

const onComment = async (): Promise<CommentResponse> => {
console.log('onComment');
return { kind: 'error', error: 'ApiError' };
const onComment = async (
shortUrl: string,
body: string,
): Promise<CommentResponse> => {
return getdiscussionClient()
.comment(shortUrl, body)
.then((response) => {
if (response.__type == 'error') {
return {
kind: 'error',
error: 'NativeError',
};
} else {
if (response.response.errorCode) {
return {
kind: 'error',
error: 'ApiError',
};
}

return ok(Number(response.response.message));
}
});
};

const onReply = async (): Promise<CommentResponse> => {
console.log('onReply');
return { kind: 'error', error: 'ApiError' };
const onReply = async (
shortUrl: string,
body: string,
parentCommentId: string,
): Promise<CommentResponse> => {
return getdiscussionClient()
.reply(shortUrl, body, parentCommentId)
.then((response) => {
if (response.__type == 'error') {
return {
kind: 'error',
error: 'NativeError',
};
} else {
if (response.response.errorCode) {
return {
kind: 'error',
error: 'ApiError',
};
}

return ok(Number(response.response.message));
}
});
};

const onRecommend = async (commentId: number): Promise<boolean> => {
return getdiscussionClient().recommend(commentId);
const onRecommend = async (commentId: string): Promise<boolean> => {
return getdiscussionClient()
.recommend(commentId)
.then((response) => {
if (response.__type === 'error') {
return false;
} else {
if (response.response.statusCode === 200) return true;
return false;
}
});
};
const addUsername = async (): Promise<Result<string, true>> => {
console.log('addUsername');
return { kind: 'error', error: 'ApiError' };
const addUsername = async (username: string): Promise<Result<string, true>> => {
return getdiscussionClient()
.addUsername(username)
.then((response) => {
if (response.__type === 'error') {
return error('NativeError');
} else {
if (response.response.errorCode) {
return error('ApiError');
}
return ok(true);
}
});
};

/***
* Currently we are using the web handler for both authenticated and unauthenticated users.
* Once we have knowlege of if the user is authenticated from bridget, we can implement an apps-specific function below, to allow us to send user data to analytics.
*/
// const reportAbuse = async (): Promise<Result<string, true>> => {
// console.log('reportAbuse');
// return { kind: 'error', error: 'ApiError' };
// };
const reportAbuse = async ({
commentId,
categoryId,
reason,
email,
}: {
commentId: string;
categoryId: string;
reason?: string;
email?: string;
}): Promise<Result<string, true>> => {
return getdiscussionClient()
.reportAbuse({ commentId, categoryId, reason, email })
.then((response) => {
if (response.__type == 'error') {
return error('NativeError');
} else {
if (response.response.errorCode) {
return error('ApiError');
}

const mockedProfile: UserProfile = {
userId: 'userId',
displayName: 'displayName',
webUrl: 'webUrl',
apiUrl: 'apiUrl',
avatar: 'avatar',
secureAvatarUrl: 'secureAvatarUrl',
badge: [],
return ok(true);
}
});
};

const getUser = async (): Promise<Reader> => {
return {
kind: 'Reader',
onComment,
onReply,
onRecommend,
addUsername,
reportAbuse: reportAbuseWeb(undefined),
profile: mockedProfile,
};
return getdiscussionClient()
.getUserProfile()
.then((response) => {
// TODO: I don't think we're handling a discussion API
// error for this request in the Bridget API

if (response.__type === 'profile') {
const profile = response.profile;
const userProfile = {
userId: profile.userId,
displayName: profile.displayName,
webUrl: profile.webUrl,
apiUrl: profile.apiUrl,
avatar: profile.avatar,
secureAvatarUrl: profile.secureAvatarUrl,
badge: [],
} as UserProfile;

return {
kind: 'Reader',
onComment,
onReply,
onRecommend,
addUsername,
reportAbuse,
profile: userProfile,
};
} else {
// TODO: Handle the error properly
throw error('NativeError');
}
});
};

export const DiscussionApps = (props: Props) => {
Expand Down
31 changes: 12 additions & 19 deletions dotcom-rendering/src/lib/discussionApi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ export const preview = async (
export type CommentResponse = Result<
| 'NetworkError'
| 'ApiError'
| 'NativeError'
| (ReturnType<typeof parseCommentResponse> & { kind: 'error' })['error'],
number
>;
Expand Down Expand Up @@ -198,15 +199,15 @@ export const reply =
async (
shortUrl: string,
body: string,
parentCommentId: number,
parentCommentId: string,
): Promise<CommentResponse> => {
const url =
joinUrl(
options.baseUrl,
'discussion',
shortUrl,
'comment',
parentCommentId.toString(),
parentCommentId,
'reply',
) + objAsParams(defaultParams);
const data = new URLSearchParams();
Expand Down Expand Up @@ -264,22 +265,18 @@ export const reportAbuse =
email,
reason,
}: {
commentId: number;
categoryId: number;
commentId: string;
categoryId: string;
reason?: string;
email?: string;
}): Promise<Result<string, true>> => {
const url =
joinUrl(
options.baseUrl,
'comment',
commentId.toString(),
'reportAbuse',
) + objAsParams(defaultParams);
joinUrl(options.baseUrl, 'comment', commentId, 'reportAbuse') +
objAsParams(defaultParams);

const data = new URLSearchParams();
data.append('categoryId', categoryId.toString());
email && data.append('email', email.toString());
data.append('categoryId', categoryId);
email && data.append('email', email);
reason && data.append('reason', reason);

const authOptions = authStatus
Expand All @@ -306,14 +303,10 @@ export const reportAbuse =

export const recommend =
(authStatus: SignedInWithCookies | SignedInWithOkta) =>
async (commentId: number): Promise<boolean> => {
async (commentId: string): Promise<boolean> => {
const url =
joinUrl(
options.baseUrl,
'comment',
commentId.toString(),
'recommend',
) + objAsParams(defaultParams);
joinUrl(options.baseUrl, 'comment', commentId, 'recommend') +
objAsParams(defaultParams);

const authOptions = getOptionsHeadersWithOkta(authStatus);

Expand Down
18 changes: 11 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading