forked from gitify-app/gitify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform.ts
More file actions
103 lines (94 loc) · 2.61 KB
/
transform.ts
File metadata and controls
103 lines (94 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import type {
Account,
GitifyNotification,
GitifyOwner,
GitifyReason,
GitifyRepository,
GitifySubject,
Link,
Reason,
SubjectType,
UserType,
} from '../../types';
import type { RawGitHubNotification } from './types';
import { getReasonDetails } from '../reason';
/**
* Transform all raw notifications from Atlassian types to Atlassify types.
*
* @param rawNotifications - The Atlassian notifications.
* @param account - The account.
* @returns Transformed Atlassify notifications.
*/
export function transformNotifications(
rawNotifications: RawGitHubNotification[],
account: Account,
): GitifyNotification[] {
return rawNotifications.map((raw) => {
return transformNotification(raw, account);
});
}
/**
* Transform a raw GitHub notification to GitifyNotification.
* Called immediately after REST API response is received.
*
* This is the ONLY place where raw GitHub types should be converted
* to Gitify's internal notification type.
*
* @param raw - The GitHub notification.
* @param account - The account.
* @returns A transformed Gitify notification.
*/
function transformNotification(
raw: RawGitHubNotification,
account: Account,
): GitifyNotification {
return {
id: raw.id,
unread: raw.unread,
updatedAt: raw.updated_at,
reason: transformReason(raw.reason),
subject: transformSubject(raw.subject),
repository: transformRepository(raw.repository),
account: account,
order: 0, // Will be set later in stabilizeNotificationsOrder
display: undefined, // Display fields start as undefined, populated by formatNotification post-enrichment
};
}
function transformReason(raw: RawGitHubNotification['reason']): GitifyReason {
const reasonDetails = getReasonDetails(raw as Reason);
return {
code: raw as Reason,
title: reasonDetails.title,
description: reasonDetails.description,
};
}
function transformSubject(
raw: RawGitHubNotification['subject'],
): GitifySubject {
return {
title: raw.title,
type: raw.type as SubjectType,
url: raw.url as Link | null,
latestCommentUrl: raw.latest_comment_url as Link | null,
// Enriched fields start as undefined, populated by handlers
};
}
function transformRepository(
raw: RawGitHubNotification['repository'],
): GitifyRepository {
return {
name: raw.name,
fullName: raw.full_name,
htmlUrl: raw.html_url as Link,
owner: transformOwner(raw.owner),
};
}
function transformOwner(
raw: NonNullable<RawGitHubNotification['repository']['owner']>,
): GitifyOwner {
return {
login: raw.login,
avatarUrl: raw.avatar_url as Link,
type: raw.type as UserType,
};
}