Skip to content

Commit

Permalink
SK-273 remove platform field from pushEvent object
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksandr1414 committed Oct 30, 2024
1 parent 67f6067 commit 6056d8a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 57 deletions.
14 changes: 7 additions & 7 deletions app/lib/push_queue/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ import CreateChatAlertEventOptions from "./models/CreateChatAlertEventOptions.js
import CreatePushEventOptions from "./models/CreatePushEventOptions.js"

export default class BasePushQueue {
async buildPushEvents(createPushEventOptions) {
const pushEvents = await pushNotificationsRepository.createPushEvents(
async buildPushEvent(createPushEventOptions) {
const pushEvent = await pushNotificationsRepository.createPushEvent(
createPushEventOptions.user_id,
createPushEventOptions.user_ids,
createPushEventOptions.payload,
{}
)

return pushEvents
return pushEvent
}

async getSubscriptionsByPlatform(platform, user_id) {
return await pushNotificationsRepository.getSubscriptionsByPlatform(platform, user_id)
async getSubscriptionsByUid(user_id) {
return await pushNotificationsRepository.getSubscriptionsByUid(user_id)
}

async createPush(pushQueueMessage) {
if (pushQueueMessage instanceof CreateChatAlertEventOptions) {
return await this.createChatAlert(pushQueueMessage)
} else if (pushQueueMessage instanceof CreatePushEventOptions) {
return await this.createPushEvents(pushQueueMessage)
return await this.createPushEvent(pushQueueMessage)
}

throw new Error("Unknown push message type")
Expand All @@ -33,7 +33,7 @@ export default class BasePushQueue {
throw new Error("Not implemented")
}

async createPushEvents() {
async createPushEvent() {
throw new Error("Not implemented")
}
}
31 changes: 14 additions & 17 deletions app/lib/push_queue/sama_native_queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,31 @@ export default class SamaNativePushQueue extends BasePushQueue {
{ user_ids: createChatAlertEventOptions.offlineUsersIDs }
)

const pushEvents = await this.buildPushEvents(createPushEventOptions)
const pushEvent = await this.buildPushEvent(createPushEventOptions)

await this.#addToQueue(pushEvents)
await this.#addToQueue(pushEvent)
}

async createPushEvents(createPushEventOptions) {
const pushEvents = await this.buildPushEvents(createPushEventOptions)
async createPushEvent(createPushEventOptions) {
const pushEvents = await this.buildPushEvent(createPushEventOptions)

await this.#addToQueue(pushEvents)

return pushEvents
}

async #addToQueue(pushEvents) {
for (const pushEvent of pushEvents) {
let devices = []
const platform = pushEvent.params.platform
async #addToQueue(pushEvent) {
let devices = []

for (const uid of pushEvent.params.user_ids) {
const userDevices = await this.getSubscriptionsByPlatform(platform, uid)
if (!userDevices.length) continue
devices = devices.concat(userDevices)
}
for (const uid of pushEvent.params.user_ids) {
const userDevices = await this.getSubscriptionsByUid(uid)
if (!userDevices.length) continue
devices = devices.concat(userDevices)
}

if (!Object.keys(devices).length) continue
if (!Object.keys(devices).length) return

const data = { devices, message: pushEvent.params.message, platform }
await this.queue.add(data)
}
const data = { devices, message: pushEvent.params.message }
await this.queue.add(data)
}
}
40 changes: 7 additions & 33 deletions app/repositories/push_notifications_repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,29 @@ class PushNotificationsRepository extends BaseRepository {
this.PushSubscriptionModel = PushSubscriptionModel
}

async #usersPlatforms(users_ids) {
let notificationChannelIds = new Set()

for (const user_id of users_ids) {
let userSubscriptions = await this.PushSubscriptionModel.findAll({ user_id: user_id })

userSubscriptions = userSubscriptions ?? []

for (const subscription of userSubscriptions) {
notificationChannelIds.add(subscription.platform)
}
}

return [...notificationChannelIds]
}

async getSubscriptionsByPlatform(platform, user_id) {
return await this.PushSubscriptionModel.findAll({ user_id, platform }, [
async getSubscriptionsByUid(user_id) {
return await this.PushSubscriptionModel.findAll({ user_id }, [
"platform",
"web_endpoint",
"web_key_auth",
"web_key_p256dh",
"device_token",
])
}

async createPushEvents(userId, userIds, payload, options) {
const platforms = await this.#usersPlatforms(userIds)

async createPushEvent(userId, userIds, payload, options) {
const base64Payload = Buffer.from(JSON.stringify(payload)).toString("base64")

const pushEvents = []

const pushEventParams = {
user_id: userId,
user_ids: userIds,

message: base64Payload,
}
const pushEvent = new this.Model(pushEventParams)
await pushEvent.save()

for (const platform of platforms) {
pushEventParams.platform = platform

const pushEvent = new this.Model(pushEventParams)
await pushEvent.save()

pushEvents.push(pushEvent)
}

return pushEvents
return pushEvent
}
}

Expand Down

0 comments on commit 6056d8a

Please sign in to comment.