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

feat(indexeddb): Added dexie to support indexeddb storage AP-366 #814

Merged
merged 18 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 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
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ module.exports = {
'import/named': 0,
'standard/no-callback-literal': 0,
'@typescript-eslint/no-unused-vars': 0,
'@typescript-eslint/require-await': 0,
'require-await': 0,
'no-empty-pattern': 0,
'no-else-return': [
'error',
{
Expand Down
19 changes: 10 additions & 9 deletions libraries/Enums/types/prop-common-events.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
export enum PropCommonEnum {
UPDATE = "Update",
NORMAL = "Normal",
DEFAULT = "default",
WAIT = "wait",
FULFILLED = "fulfilled",
CREATED_AT = "created_at",
TYPING = "TYPING",
NOT_TYPING = "NOT_TYPING",
UPDATE = 'Update',
NORMAL = 'Normal',
DEFAULT = 'default',
WAIT = 'wait',
FULFILLED = 'fulfilled',
CREATED_AT = 'created_at',
MOD = '_mod',
TYPING = 'TYPING',
NOT_TYPING = 'NOT_TYPING',
}

export type PropCommon = keyof typeof PropCommonEnum
export type PropCommon = keyof typeof PropCommonEnum
72 changes: 54 additions & 18 deletions libraries/Textile/MailboxManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import {
Message,
} from '~/types/textile/mailbox'
import { TextileInitializationData } from '~/types/textile/manager'
import {PropCommonEnum} from "~/libraries/Enums/types/prop-common-events";
import {MessagingTypesEnum} from "~/libraries/Enums/types/messaging-types";
import {EncodingTypesEnum} from "~/libraries/Enums/types/encoding-types";
import { PropCommonEnum } from '~/libraries/Enums/types/prop-common-events'
import { MessagingTypesEnum } from '~/libraries/Enums/types/messaging-types'
import { EncodingTypesEnum } from '~/libraries/Enums/types/encoding-types'

export class MailboxManager {
senderAddress: string
Expand Down Expand Up @@ -75,16 +75,32 @@ export class MailboxManager {
* Retrieve a conversation with a specific user, filtered by the given query parameters
* @param friendIdentifier friend mailboxId
* @param query parameters for filtering
* @param lastInbound timestamp of last received message
* @returns an array of messages
*/
async getConversation(
friendIdentifier: string,
query: ConversationQuery,
): Promise<Message[]> {
async getConversation({
friendIdentifier,
query,
lastInbound,
}: {
friendIdentifier: string
query: ConversationQuery
lastInbound?: number
}): Promise<Message[]> {
const thread = await this.textile.users.getThread('hubmail')
const threadID = ThreadID.fromString(thread.id)

const inboxQuery = Query.where('from').eq(friendIdentifier).orderByIDDesc()
let inboxQuery = Query.where('from').eq(friendIdentifier).orderByIDDesc()

// if messages are stored in indexeddb, only fetch new messages from textile
if (lastInbound) {
lastInbound = lastInbound * 1000000 // textile has a more specific unix timestamp, matching theirs
inboxQuery = Query.where('from')
.eq(friendIdentifier)
.and(PropCommonEnum.MOD)
.ge(lastInbound)
.orderByIDDesc()
}

if (query?.limit) {
inboxQuery.limitTo(query.limit)
Expand Down Expand Up @@ -113,11 +129,16 @@ export class MailboxManager {
sentboxQuery.and(PropCommonEnum.CREATED_AT).lt(lastMessageTime)
}

const encryptedSentbox = await this.textile.client.find<MessageFromThread>(
threadID,
MailboxSubscriptionType.sentbox,
sentboxQuery,
)
let encryptedSentbox: MessageFromThread[] = []

// only fetch sent messages from textile if indexeddb is empty. after that, fetch sent messages from indexeddb
if (lastInbound === undefined) {
encryptedSentbox = await this.textile.client.find<MessageFromThread>(
threadID,
MailboxSubscriptionType.sentbox,
sentboxQuery,
)
}

const messages = [...encryptedInbox, ...encryptedSentbox].sort(
(a, b) => a.created_at - b.created_at,
Expand Down Expand Up @@ -208,9 +229,18 @@ export class MailboxManager {
at: Date.now(),
type: message.type,
payload: message.payload,
reactedTo: message.type === MessagingTypesEnum.REACTION ? message.reactedTo : undefined,
repliedTo: message.type === MessagingTypesEnum.REPLY ? message.repliedTo : undefined,
replyType: message.type === MessagingTypesEnum.REPLY ? message.replyType : undefined,
reactedTo:
message.type === MessagingTypesEnum.REACTION
? message.reactedTo
: undefined,
repliedTo:
message.type === MessagingTypesEnum.REPLY
? message.repliedTo
: undefined,
replyType:
message.type === MessagingTypesEnum.REPLY
? message.replyType
: undefined,
pack: message.pack,
}),
)
Expand Down Expand Up @@ -245,8 +275,14 @@ export class MailboxManager {
editedAt: Date.now(),
type: message.type,
payload: message.payload,
reactedTo: message.type === MessagingTypesEnum.REACTION ? message.reactedTo : undefined,
repliedTo: message.type === MessagingTypesEnum.REPLY ? message.repliedTo : undefined,
reactedTo:
message.type === MessagingTypesEnum.REACTION
? message.reactedTo
: undefined,
repliedTo:
message.type === MessagingTypesEnum.REPLY
? message.repliedTo
: undefined,
}),
)

Expand Down
2 changes: 1 addition & 1 deletion locales
Submodule locales updated 1 files
+8 βˆ’1 en-US.js
1 change: 1 addition & 0 deletions nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export default defineNuxtConfig({
{ src: '~/plugins/thirdparty/videoplayer.ts' },
{ src: '~/plugins/thirdparty/vuetify.ts' },
{ src: '~/plugins/thirdparty/swiper.ts' },
{ src: '~/plugins/thirdparty/dexie.ts' },
// Local
{ src: '~/plugins/local/classLoader.ts' },
{ src: '~/plugins/local/notifications.ts', mode: 'client' },
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"curve25519-js": "^0.0.4",
"dayjs": "^1.10.4",
"detectrtc": "^1.4.1",
"dexie": "^3.2.0",
"ed2curve": "^0.3.0",
"emoji-mart-vue-fast": "^10.0.1",
"events": "^3.3.0",
Expand Down
20 changes: 20 additions & 0 deletions plugins/thirdparty/dexie.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Dexie from 'dexie'
import { Message } from '~/types/textile/mailbox'

export type DexieMessage = {
key: string
conversation: Message[]
}
class SatelliteDB extends Dexie {
public conversations: Dexie.Table<DexieMessage, string>

public constructor() {
super('SatelliteDB')
this.version(1).stores({
conversations: 'key',
})
this.conversations = this.table('conversations')
}
}

export const db = new SatelliteDB()
Loading