Skip to content

Commit

Permalink
shared: Add support for announcements
Browse files Browse the repository at this point in the history
Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
  • Loading branch information
theimpulson committed Oct 26, 2024
1 parent 3d37022 commit 41c85dd
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package app.opass.ccip.database

import app.opass.ccip.extensions.toAnnouncement
import app.opass.ccip.extensions.toAttendee
import app.opass.ccip.extensions.toEvent
import app.opass.ccip.extensions.toEventConfig
Expand All @@ -14,6 +15,7 @@ import app.opass.ccip.extensions.toSpeaker
import app.opass.ccip.network.models.common.LocalizedObject
import app.opass.ccip.network.models.event.Event
import app.opass.ccip.network.models.eventconfig.EventConfig
import app.opass.ccip.network.models.fastpass.Announcement
import app.opass.ccip.network.models.fastpass.Attendee
import app.opass.ccip.network.models.schedule.Schedule
import app.opass.ccip.network.models.schedule.Session
Expand Down Expand Up @@ -284,4 +286,35 @@ internal class OPassDatabaseHelper {
}
}
}

suspend fun getAllAnnouncements(eventId: String, token: String? = null): List<Announcement> {
return withContext(Dispatchers.IO) {
dbQuery.selectAllAnnouncements(eventId, token)
.executeAsList()
.map { it.toAnnouncement() }
}
}

suspend fun addAnnouncements(
eventId: String,
announcements: List<Announcement>,
token: String? = null
) {
withContext(Dispatchers.IO) {
dbQuery.transaction {
dbQuery.deleteAllAnnouncements(eventId, token)
announcements.forEach {
dbQuery.insertAnnouncement(
datetime = it.datetime,
url = it.url,
msg_en = it._msg_en,
msg_zh = it._msg_zh,
role = Json.encodeToString(it.role),
token = token,
eventId = eventId
)
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* SPDX-FileCopyrightText: 2024 OPass
* SPDX-License-Identifier: GPL-3.0-only
*/

package app.opass.ccip.extensions

import app.opass.ccip.database.AnnouncementTable
import app.opass.ccip.network.models.fastpass.Announcement
import kotlinx.serialization.json.Json

internal fun AnnouncementTable.toAnnouncement(): Announcement {
return Announcement(
datetime = this.datetime,
_msg_en = this.msg_en,
_msg_zh = this.msg_zh,
role = Json.decodeFromString<List<String>>(this.role),
url = this.url
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import app.opass.ccip.network.models.common.LocalizedObject
import app.opass.ccip.network.models.event.Event
import app.opass.ccip.network.models.eventconfig.EventConfig
import app.opass.ccip.network.models.eventconfig.FeatureType
import app.opass.ccip.network.models.fastpass.Announcement
import app.opass.ccip.network.models.fastpass.Attendee
import app.opass.ccip.network.models.schedule.Schedule
import app.opass.ccip.network.models.schedule.Session
Expand Down Expand Up @@ -79,6 +80,31 @@ class PortalHelper {
}
}

/**
* Fetches [Announcement] for specified event using given token from event's FastPass feature
* @param eventId ID of the event
* @param token Token to identify attendee
* @param forceReload Whether to ignore cache, false by default
* @return empty list if announcements haven't been cached yet or token is invalid; Announcements otherwise
*/
suspend fun getAnnouncements(
eventId: String,
token: String? = null,
forceReload: Boolean = false
): List<Announcement> {
val eventConfig = dbHelper.getEventConfig(eventId) ?: return emptyList()
val feat = eventConfig.features.find { f -> f.type == FeatureType.FAST_PASS } ?: return emptyList()

val cachedAnnouncements = dbHelper.getAllAnnouncements(eventId, token)
return if (cachedAnnouncements.isNotEmpty() && !forceReload) {
cachedAnnouncements
} else {
client.getAnnouncements(feat.url!!, token).also {
dbHelper.addAnnouncements(eventId, it, token)
}
}
}

/**
* Fetches [Attendee] for specified event using given token from event's FastPass feature
* @param eventId ID of the event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package app.opass.ccip.network

import app.opass.ccip.network.models.event.Event
import app.opass.ccip.network.models.eventconfig.EventConfig
import app.opass.ccip.network.models.fastpass.Announcement
import app.opass.ccip.network.models.fastpass.Attendee
import app.opass.ccip.network.models.schedule.Schedule
import io.ktor.client.HttpClient
Expand Down Expand Up @@ -54,4 +55,11 @@ internal class PortalClient {
parameter("token", token)
}.body()
}

suspend fun getAnnouncements(url: String, token: String? = null): List<Announcement> {
return universalClient.get {
url(url)
if (token != null) parameter("token", token)
}.body()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* SPDX-FileCopyrightText: 2024 OPass
* SPDX-License-Identifier: GPL-3.0-only
*/

package app.opass.ccip.network.models.fastpass

import app.opass.ccip.extensions.localized
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class Announcement(
val datetime: Long,

@SerialName("msg_en")
val _msg_en: String,

@SerialName("msg_zh")
val _msg_zh: String,

val role: List<String>,

@SerialName("uri")
val url: String
) {
val message: String
get() = localized(_msg_en, _msg_zh)
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ CREATE TABLE AttendeeTable(
FOREIGN KEY (eventId) REFERENCES EventConfigTable(id) ON DELETE CASCADE
);

-- Create Announcement table

CREATE TABLE AnnouncementTable(
primaryId INTEGER PRIMARY KEY AUTOINCREMENT,
datetime INTEGER NOT NULL,
role TEXT NOT NULL,
msg_en TEXT NOT NULL,
msg_zh TEXT NOT NULL,
token TEXT,
url TEXT NOT NULL,
eventId TEXT NOT NULL,
FOREIGN KEY (eventId) REFERENCES EventConfigTable(id) ON DELETE CASCADE
);

-- Named queries for event table

selectAllEvents:
Expand Down Expand Up @@ -228,3 +242,16 @@ DELETE FROM AttendeeTable WHERE eventId = :eventId AND token = :token;
insertAttendee:
INSERT INTO AttendeeTable (userId, attr, firstUse, role, scenarios, token, eventId)
VALUES (:userId, :attr, :firstUse, :role, :scenarios, :token, :eventId);

-- Named queries for announcement table

selectAllAnnouncements:
SELECT * FROM AnnouncementTable WHERE eventId = :eventId AND token = :token;

deleteAllAnnouncements:
DELETE FROM AnnouncementTable WHERE eventId = :eventId AND token = :token;

insertAnnouncement:
INSERT INTO AnnouncementTable (datetime, role, msg_en, msg_zh, token, url, eventId)
VALUES (:datetime, :role, :msg_en, :msg_zh, :token, :url, :eventId);

0 comments on commit 41c85dd

Please sign in to comment.