Skip to content

Commit

Permalink
Merge pull request #386 from NDLANO/arena-notifications-support
Browse files Browse the repository at this point in the history
Added notification endpoint, resolver functions and types
  • Loading branch information
MaPoKen authored Nov 23, 2023
2 parents 16c3ba4 + 7dd9b0f commit 906a772
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 12 deletions.
50 changes: 44 additions & 6 deletions src/api/arenaApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@

import {
GQLArenaCategory,
GQLArenaNotification,
GQLArenaNotificationUser,
GQLArenaPost,
GQLArenaTopic,
GQLArenaUser,
GQLBaseUser,
GQLMutationNewArenaTopicArgs,
GQLMutationReplyToTopicArgs,
GQLQueryArenaCategoryArgs,
Expand All @@ -20,22 +23,30 @@ import {
} from '../types/schema';
import { fetch, resolveJson } from '../utils/apiHelpers';

const toUser = (user: any): GQLArenaUser => ({
const toBaseUser = (user: any): Omit<GQLBaseUser, '__typename'> => ({
id: user.uid,
displayName: user.displayname,
username: user.username,
profilePicture: user.picture,
slug: user.userslug,
});

const toArenaUser = (user: any): GQLArenaUser => ({
...toBaseUser(user),
groupTitleArray: user.groupTitleArray,
});

const toArenaNotificationUser = (user: any): GQLArenaNotificationUser => ({
...toBaseUser(user),
});

const toArenaPost = (post: any, mainPid?: any): GQLArenaPost => ({
id: post.pid,
topicId: post.tid,
content: post.content,
timestamp: post.timestampISO,
isMainPost: post.isMainPost ?? post.pid === mainPid,
user: toUser(post.user),
user: toArenaUser(post.user),
});

const toTopic = (topic: any): GQLArenaTopic => {
Expand Down Expand Up @@ -74,6 +85,23 @@ const toCategory = (category: any): GQLArenaCategory => {
};
};

const toNotification = (notification: any): GQLArenaNotification => ({
bodyShort: notification.bodyShort,
datetimeISO: notification.datetimeISO,
from: notification.from,
importance: notification.importance,
path: notification.path,
read: notification.read,
user: toArenaNotificationUser(notification.user),
readClass: notification.readClass,
image: notification.image,
topicTitle: notification.topicTitle,
type: notification.type,
subject: notification.subject,
topicId: notification.tid,
postId: notification.pid,
notificationId: notification.nid,
});
export const fetchCsrfTokenForSession = async (
context: Context,
): Promise<{ cookie: string; 'x-csrf-token': string }> => {
Expand Down Expand Up @@ -108,7 +136,7 @@ export const fetchArenaUser = async (
context,
);
const resolved: any = await resolveJson(response);
return toUser(resolved);
return toArenaUser(resolved);
};

export const fetchArenaCategories = async (
Expand All @@ -127,19 +155,21 @@ export const fetchArenaCategory = async (
`/groups/api/category/${categoryId}?page=${page}`,
context,
);
const resolved: any = await resolveJson(response);
const resolved = await resolveJson(response);
return toCategory(resolved);
};

export const fetchArenaTopic = async (
{ topicId, page }: GQLQueryArenaTopicArgs,
context: Context,
): Promise<GQLArenaTopic> => {
const csrfHeaders = await fetchCsrfTokenForSession(context);
const response = await fetch(
`/groups/api/topic/${topicId}?page=${page}`,
`/groups/api/topic/${topicId}?page=${page ?? 1}`,
context,
{ headers: csrfHeaders },
);
const resolved: any = await resolveJson(response);
const resolved = await resolveJson(response);
return toTopic(resolved);
};

Expand All @@ -160,6 +190,14 @@ export const fetchArenaTopicsByUser = async (
return resolved.topics.map(toTopic);
};

export const fetchArenaNotifications = async (
context: Context,
): Promise<GQLArenaNotification[]> => {
const response = await fetch('/groups/api/notifications', context);
const resolved = await resolveJson(response);
return resolved.notifications.map(toNotification);
};

export const newTopic = async (
{ title, content, categoryId }: GQLMutationNewArenaTopicArgs,
context: Context,
Expand Down
23 changes: 22 additions & 1 deletion src/resolvers/arenaResolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
fetchArenaUser,
fetchArenaTopic,
fetchArenaTopicsByUser,
fetchArenaNotifications,
fetchCsrfTokenForSession,
newTopic,
replyToTopic,
Expand All @@ -26,7 +27,9 @@ import {
GQLQueryArenaTopicArgs,
GQLQueryArenaTopicsByUserArgs,
GQLQueryResolvers,
GQLArenaNotification,
GQLMutationResolvers,
GQLMutationMarkNotificationAsReadArgs,
GQLArenaPost,
} from '../types/schema';

Expand All @@ -38,6 +41,7 @@ export const Query: Pick<
| 'arenaTopic'
| 'arenaRecentTopics'
| 'arenaTopicsByUser'
| 'arenaNotifications'
> = {
async arenaUser(
_: any,
Expand Down Expand Up @@ -81,11 +85,18 @@ export const Query: Pick<
): Promise<GQLArenaTopic[]> {
return fetchArenaTopicsByUser(params, context);
},
async arenaNotifications(
_: any,
__: any,
context: ContextWithLoaders,
): Promise<GQLArenaNotification[]> {
return fetchArenaNotifications(context);
},
};

export const Mutations: Pick<
GQLMutationResolvers,
'newArenaTopic' | 'replyToTopic'
'newArenaTopic' | 'replyToTopic' | 'markNotificationAsRead'
> = {
async newArenaTopic(
_: any,
Expand All @@ -101,4 +112,14 @@ export const Mutations: Pick<
): Promise<GQLArenaPost> {
return replyToTopic(params, context);
},
async markNotificationAsRead(
_: any,
params: GQLMutationMarkNotificationAsReadArgs,
context: Context,
) {
await Promise.all(
params.topicIds.map(topicId => fetchArenaTopic({ topicId }, context)),
);
return params.topicIds;
},
};
40 changes: 38 additions & 2 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,15 @@ export const typeDefs = gql`
topics: [ArenaTopic!]
}
type ArenaUser {
interface BaseUser {
id: Int!
displayName: String!
username: String!
profilePicture: String
slug: String!
}
type ArenaUser implements BaseUser {
id: Int!
displayName: String!
username: String!
Expand All @@ -1199,6 +1207,14 @@ export const typeDefs = gql`
groupTitleArray: [String!]!
}
type ArenaNotificationUser implements BaseUser {
id: Int!
displayName: String!
username: String!
profilePicture: String
slug: String!
}
type ArenaPost {
id: Int!
topicId: Int!
Expand Down Expand Up @@ -1226,6 +1242,24 @@ export const typeDefs = gql`
breadcrumbs: [ArenaBreadcrumb!]!
}
type ArenaNotification {
bodyShort: String!
path: String!
from: Int!
importance: Int!
datetimeISO: String!
read: Boolean!
user: ArenaNotificationUser!
image: String
readClass: String!
postId: Int!
topicId: Int!
notificationId: String!
topicTitle: String!
type: String!
subject: String!
}
type Query {
resource(id: String!, subjectId: String, topicId: String): Resource
articleResource(articleId: String, taxonomyId: String): Resource
Expand Down Expand Up @@ -1357,9 +1391,10 @@ export const typeDefs = gql`
arenaCategories: [ArenaCategory!]!
arenaCategory(categoryId: Int!, page: Int!): ArenaCategory
arenaUser(username: String!): ArenaUser
arenaTopic(topicId: Int!, page: Int!): ArenaTopic
arenaTopic(topicId: Int!, page: Int): ArenaTopic
arenaRecentTopics: [ArenaTopic!]!
arenaTopicsByUser(userSlug: String!): [ArenaTopic!]!
arenaNotifications: [ArenaNotification!]!
}
type Mutation {
Expand Down Expand Up @@ -1402,6 +1437,7 @@ export const typeDefs = gql`
draftConcept: Boolean
absoluteUrl: Boolean
): String!
markNotificationAsRead(topicIds: [Int!]!): [Int!]!
newArenaTopic(
categoryId: Int!
title: String!
Expand Down
Loading

0 comments on commit 906a772

Please sign in to comment.