feat: add push notification support (#73)
* feat: add push notification support * misc: set localstorage on start
This commit is contained in:
1 parent
4f898915b2
commit
fc38560eda
6 files changed
+204
-24
No files matched your search
@@ -1,6 +1,10 @@
|
||||
import { getClient } from '$lib/lemmy.js'
|
||||
import type { Result } from '$lib/lemmy/item.js'
|
||||
import type {
|
||||
CommentReplyView,
|
||||
Person,
|
||||
PersonMentionView,
|
||||
PersonView,
|
||||
PrivateMessageView,
|
||||
} from 'lemmy-js-client'
|
||||
|
||||
@@ -38,3 +42,81 @@ export function isRead(
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
export const getInbox = async (
|
||||
jwt: string,
|
||||
until?: number
|
||||
): Promise<InboxItem[]> => {
|
||||
const client = getClient()
|
||||
|
||||
const params = {
|
||||
limit: 50,
|
||||
auth: jwt,
|
||||
unread_only: true,
|
||||
}
|
||||
|
||||
const [replies, mentions, privateMessages] = await Promise.all([
|
||||
client.getReplies({
|
||||
...params,
|
||||
sort: 'New',
|
||||
}),
|
||||
client.getPersonMentions({
|
||||
...params,
|
||||
sort: 'New',
|
||||
}),
|
||||
client.getPrivateMessages({ ...params }),
|
||||
])
|
||||
|
||||
let items = [
|
||||
...replies.replies,
|
||||
...mentions.mentions,
|
||||
...privateMessages.private_messages,
|
||||
]
|
||||
.map(toInboxItem)
|
||||
.sort((a, b) => b.created - a.created)
|
||||
|
||||
if (until) {
|
||||
items = items.filter((item) => item.created > until)
|
||||
}
|
||||
|
||||
return items
|
||||
}
|
||||
|
||||
const toInboxItem = (item: InboxItemView): InboxItem => {
|
||||
let type: 'private_message' | 'comment_reply' | 'person_mention'
|
||||
let body: string
|
||||
let personView: Person
|
||||
let created: number
|
||||
if ('comment_reply' in item) {
|
||||
type = 'comment_reply'
|
||||
body = item.comment.content
|
||||
personView = item.creator
|
||||
created = Date.parse(`${item.comment_reply.published}Z`)
|
||||
} else if ('person_mention' in item) {
|
||||
type = 'person_mention'
|
||||
body = item.comment.content
|
||||
personView = item.creator
|
||||
created = Date.parse(`${item.person_mention.published}Z`)
|
||||
} else if ('private_message' in item) {
|
||||
type = 'private_message'
|
||||
body = item.private_message.content
|
||||
personView = item.creator
|
||||
created = Date.parse(`${item.private_message.published}Z`)
|
||||
} else {
|
||||
throw new Error('Invalid item')
|
||||
}
|
||||
|
||||
return {
|
||||
type: type,
|
||||
body: body,
|
||||
person: personView,
|
||||
created,
|
||||
}
|
||||
}
|
||||
|
||||
interface InboxItem {
|
||||
type: 'private_message' | 'comment_reply' | 'person_mention'
|
||||
body: string
|
||||
person: Person
|
||||
created: number
|
||||
}
|
||||
@@ -10,7 +10,7 @@ import type {
|
||||
PrivateMessageView,
|
||||
} from 'lemmy-js-client'
|
||||
|
||||
type Result =
|
||||
export type Result =
|
||||
| PostView
|
||||
| CommentView
|
||||
| PersonView
|
||||
|
||||
Reference in new issue
Block a user