@@ -25,9 +25,13 @@ import kotlinx.coroutines.withContext
2525import kotlinx.serialization.encodeToString
2626import kotlinx.serialization.json.Json
2727import kotlinx.serialization.json.encodeToJsonElement
28+ import org.jetbrains.exposed.v1.core.LongColumnType
29+ import org.jetbrains.exposed.v1.core.SqlExpressionBuilder.case
2830import org.jetbrains.exposed.v1.core.SqlExpressionBuilder.eq
2931import org.jetbrains.exposed.v1.core.SqlExpressionBuilder.inList
3032import org.jetbrains.exposed.v1.core.and
33+ import org.jetbrains.exposed.v1.core.castTo
34+ import org.jetbrains.exposed.v1.core.longLiteral
3135import org.jetbrains.exposed.v1.jdbc.*
3236import org.jetbrains.exposed.v1.jdbc.transactions.transaction
3337import org.jsoup.Jsoup
@@ -47,9 +51,8 @@ class LinksRepoImpl : LinksRepo {
4751 Jsoup .connect(
4852 " http" + linkUrl.substringAfter(" http" ).substringBefore(" " ).trim()
4953 ).userAgent(userAgent).followRedirects(true ).header(" Accept" , " text/html" )
50- .header(" Accept-Encoding" , " gzip,deflate" ).header(" Accept-Language" , " en;q=1.0" )
51- .ignoreContentType(true ).maxBodySize(0 )
52- .ignoreHttpErrors(true ).get()
54+ .header(" Accept-Encoding" , " gzip,deflate" ).header(" Accept-Language" , " en;q=1.0" ).ignoreContentType(true )
55+ .maxBodySize(0 ).ignoreHttpErrors(true ).get()
5356 }.toString()
5457
5558 val document = Jsoup .parse(rawHTML)
@@ -100,26 +103,24 @@ class LinksRepoImpl : LinksRepo {
100103 HttpRequest .newBuilder(URI (" https://api.vxtwitter.com/${tweetURL.substringAfter(" .com/" )} " )).build()
101104
102105 val vxTwitterResponseBody = withContext(Dispatchers .IO ) {
103- HttpClient .newHttpClient().send(httpRequest, HttpResponse .BodyHandlers .ofString())
104- .body().run {
105- Json .decodeFromString<TwitterMetaDataDTO >(this )
106- }
106+ HttpClient .newHttpClient().send(httpRequest, HttpResponse .BodyHandlers .ofString()).body().run {
107+ Json .decodeFromString<TwitterMetaDataDTO >(this )
108+ }
107109 }
108110
109111 return ScrapedLinkInfo (
110112 title = vxTwitterResponseBody.text,
111113 imgUrl = vxTwitterResponseBody.media.takeIf { vxTwitterResponseBody.hasMedia && it.isNotEmpty() }
112114 ?.find { it.type in listOf (" image" , " video" , " gif" ) }
113- ?.let { if (it.type == " image" ) it.url else it.thumbnailUrl }
114- ? : vxTwitterResponseBody.userPfp,
115+ ?.let { if (it.type == " image" ) it.url else it.thumbnailUrl } ? : vxTwitterResponseBody.userPfp,
115116 mediaType = if (vxTwitterResponseBody.media.isNotEmpty() && vxTwitterResponseBody.media.first().type == " video" ) MediaType .VIDEO else MediaType .IMAGE )
116117 }
117118
118119 override suspend fun createANewLink (addLinkDTO : AddLinkDTO ): Result <NewItemResponseDTO > {
119120 return try {
120121 val eventTimestamp = getSystemEpochSeconds()
121122
122- val addLinkDTO = if (addLinkDTO.forceRetrieveOGMetaInfo) {
123+ val addLinkDTO = ( if (addLinkDTO.forceRetrieveOGMetaInfo) {
123124 try {
124125 val ogMetaInfo = if (addLinkDTO.url.isATwitterUrl()) {
125126 retrieveFromVxTwitterApi(addLinkDTO.url)
@@ -140,6 +141,8 @@ class LinksRepoImpl : LinksRepo {
140141 }
141142 } else {
142143 addLinkDTO
144+ }).run {
145+ copy(idOfLinkedFolder = linkType.defaultFolderId() ? : idOfLinkedFolder)
143146 }
144147
145148 val idOfNewlyAddedLink = transaction {
@@ -150,9 +153,9 @@ class LinksRepoImpl : LinksRepo {
150153 historyLinkMatch
151154 }.toList()
152155 TombstoneTable .batchInsert(historyLinkRecords) {
153- it [TombstoneTable .deletedAt] = eventTimestamp
154- it [TombstoneTable .operation] = Route .DELETE_A_LINK .name
155- it [TombstoneTable .payload] = Json .encodeToString(
156+ this [TombstoneTable .deletedAt] = eventTimestamp
157+ this [TombstoneTable .operation] = Route .DELETE_A_LINK .name
158+ this [TombstoneTable .payload] = Json .encodeToString(
156159 IDBasedDTO (
157160 id = it[LinksTable .id].value,
158161 correlation = addLinkDTO.correlation,
@@ -374,6 +377,7 @@ class LinksRepoImpl : LinksRepo {
374377 }) {
375378 it[lastModified] = eventTimestamp
376379 it[linkType] = LinkType .ARCHIVE_LINK .name
380+ it[idOfLinkedFolder] = Constants .ARCHIVE_ID
377381 }
378382 }
379383 Result .Success (
@@ -401,6 +405,7 @@ class LinksRepoImpl : LinksRepo {
401405 }) {
402406 it[lastModified] = eventTimestamp
403407 it[linkType] = LinkType .SAVED_LINK .name
408+ it[idOfLinkedFolder] = Constants .SAVED_LINKS_ID
404409 }
405410 }
406411 Result .Success (
@@ -489,7 +494,7 @@ class LinksRepoImpl : LinksRepo {
489494 it[baseURL] = linkDTO.baseURL
490495 it[imgURL] = linkDTO.imgURL
491496 it[note] = linkDTO.note
492- it[idOfLinkedFolder] = linkDTO.idOfLinkedFolder
497+ it[idOfLinkedFolder] = linkDTO.linkType.defaultFolderId() ? : linkDTO. idOfLinkedFolder
493498 it[userAgent] = linkDTO.userAgent
494499 it[mediaType] = linkDTO.mediaType.name
495500 it[markedAsImportant] = linkDTO.markedAsImportant
@@ -556,4 +561,45 @@ class LinksRepoImpl : LinksRepo {
556561 Result .Failure (e)
557562 }
558563 }
564+
565+ override suspend fun forceSetDefaultFolderToInternalIds (): Result <TimeStampBasedResponse > {
566+ val eventTimestamp = getSystemEpochSeconds()
567+ return try {
568+ val timeStampResponse = TimeStampBasedResponse (
569+ eventTimestamp = eventTimestamp, message = " Set Default folders to internal IDs."
570+ )
571+
572+ transaction {
573+ LinksTable .update(where = {
574+ LinksTable .linkType.inList(
575+ listOf (
576+ LinkType .SAVED_LINK .name,
577+ LinkType .IMPORTANT_LINK .name,
578+ LinkType .HISTORY_LINK .name,
579+ LinkType .ARCHIVE_LINK .name
580+ )
581+ )
582+ }) {
583+ it[idOfLinkedFolder] =
584+ case().When (linkType.eq(LinkType .SAVED_LINK .name), longLiteral(Constants .SAVED_LINKS_ID ))
585+ .When (linkType.eq(LinkType .IMPORTANT_LINK .name), longLiteral(Constants .IMPORTANT_LINKS_ID ))
586+ .When (linkType.eq(LinkType .HISTORY_LINK .name), longLiteral(Constants .HISTORY_ID ))
587+ .When (linkType.eq(LinkType .ARCHIVE_LINK .name), longLiteral(Constants .ARCHIVE_ID ))
588+ .Else (idOfLinkedFolder.castTo(LongColumnType ()))
589+
590+ it[lastModified] = eventTimestamp
591+ }
592+ }
593+
594+ Result .Success (
595+ response = timeStampResponse, webSocketEvent = WebSocketEvent (
596+ operation = Route .FORCE_SET_DEFAULT_FOLDER_TO_INTERNAL_IDS .name,
597+ payload = Json .encodeToJsonElement(timeStampResponse)
598+ )
599+ )
600+ } catch (e: Exception ) {
601+ Result .Failure (e)
602+ }
603+ }
604+
559605}
0 commit comments