Skip to content

Commit

Permalink
chat: share with community, teams and enterprises (fixes #3679) (#3680)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <dogi@users.noreply.github.com>
  • Loading branch information
Okuro3499 and dogi committed Jun 24, 2024
1 parent 69f0935 commit f302d3b
Show file tree
Hide file tree
Showing 26 changed files with 652 additions and 81 deletions.
5 changes: 2 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ android {
defaultConfig {
applicationId "org.ole.planet.myplanet"
minSdkVersion 21
targetSdkVersion 34
versionCode 1613
versionName "0.16.13"
versionCode 1615
versionName "0.16.15"
ndkVersion '21.3.6528147'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ open class RealmChatHistory : RealmObject() {
var _id: String? = null
var _rev: String? = null
var user: String? = null
var aiProvider: String? = null
var title: String? = null
var updatedTime: String? = null
var createdDate: String? = null
var conversations: RealmList<Conversation>? = null
companion object {
@JvmStatic
Expand All @@ -31,8 +32,9 @@ open class RealmChatHistory : RealmObject() {
chatHistory._rev = JsonUtils.getString("_rev", act)
chatHistory._id = JsonUtils.getString("_id", act)
chatHistory.title = JsonUtils.getString("title", act)
chatHistory.updatedTime = JsonUtils.getString("updatedTime", act)
chatHistory.createdDate = JsonUtils.getString("createdDate", act)
chatHistory.user = JsonUtils.getString("user", act)
chatHistory.aiProvider = JsonUtils.getString("aiProvider", act)
chatHistory.conversations = parseConversations(mRealm, JsonUtils.getJsonArray("conversations", act))
mRealm.commitTransaction()
}
Expand Down
72 changes: 56 additions & 16 deletions app/src/main/java/org/ole/planet/myplanet/model/RealmNews.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.text.TextUtils
import com.google.gson.Gson
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import com.google.gson.JsonSyntaxException
import io.realm.Realm
import io.realm.RealmList
import io.realm.RealmObject
Expand Down Expand Up @@ -70,6 +71,7 @@ open class RealmNews : RealmObject() {

val imagesArray: JsonArray
get() = if (images == null) JsonArray() else Gson().fromJson(images, JsonArray::class.java)

val labelsArray: JsonArray
get() {
val array = JsonArray()
Expand Down Expand Up @@ -100,17 +102,14 @@ open class RealmNews : RealmObject() {
}
return ms
}

val isCommunityNews: Boolean
get() {
val array = Gson().fromJson(viewIn, JsonArray::class.java)
var isCommunity = false
for (e in array) {
val `object` = e.asJsonObject
if (`object`.has("section") && `object`["section"].asString.equals(
"community",
ignoreCase = true
)
) {
if (`object`.has("section") && `object`["section"].asString.equals("community", ignoreCase = true)) {
isCommunity = true
break
}
Expand All @@ -121,9 +120,7 @@ open class RealmNews : RealmObject() {
companion object {
@JvmStatic
fun insert(mRealm: Realm, doc: JsonObject?) {
if (!mRealm.isInTransaction) {
mRealm.beginTransaction()
}
if (!mRealm.isInTransaction) mRealm.beginTransaction()
var news = mRealm.where(RealmNews::class.java)
.equalTo("_id", JsonUtils.getString("_id", doc))
.findFirst()
Expand Down Expand Up @@ -214,8 +211,11 @@ open class RealmNews : RealmObject() {

@JvmStatic
fun createNews(map: HashMap<String?, String>, mRealm: Realm, user: RealmUserModel?, imageUrls: RealmList<String>?): RealmNews {
if (!mRealm.isInTransaction) mRealm.beginTransaction()
val news = mRealm.createObject(RealmNews::class.java, UUID.randomUUID().toString())
if (!mRealm.isInTransaction) {
mRealm.beginTransaction()
}

val news = mRealm.createObject(RealmNews::class.java, "${UUID.randomUUID()}")
news.message = map["message"]
news.time = Date().time
news.createdOn = user?.planetCode
Expand All @@ -226,19 +226,59 @@ open class RealmNews : RealmObject() {
news.messagePlanetCode = map["messagePlanetCode"]
news.messageType = map["messageType"]
news.viewIn = getViewInJson(map)
news.chat = map["chat"]?.toBoolean() ?: false

try {
news.updatedDate = map["updatedDate"]?.toLong()!!
news.updatedDate = map["updatedDate"]?.toLong() ?: 0
} catch (e: Exception) {
e.printStackTrace()
}

news.userId = user?.id
news.replyTo = if (map.containsKey("replyTo")) {
map["replyTo"]
} else {
""
}
news.replyTo = map["replyTo"] ?: ""
news.user = Gson().toJson(user?.serialize())
news.imageUrls = imageUrls

if (map.containsKey("news")) {
val newsObj = map["news"]
val gson = Gson()
try {
val newsJsonString = newsObj?.replace("=", ":")
val newsJson = gson.fromJson(newsJsonString, JsonObject::class.java)
news.newsId = JsonUtils.getString("_id", newsJson)
news.newsRev = JsonUtils.getString("_rev", newsJson)
news.newsUser = JsonUtils.getString("user", newsJson)
news.aiProvider = JsonUtils.getString("aiProvider", newsJson)
news.newsTitle = JsonUtils.getString("title", newsJson)
if (newsJson.has("conversations")) {
val conversationsElement = newsJson.get("conversations")
if (conversationsElement.isJsonPrimitive && conversationsElement.asJsonPrimitive.isString) {
val conversationsString = conversationsElement.asString
try {
val conversationsArray = gson.fromJson(conversationsString, JsonArray::class.java)
if (conversationsArray.size() > 0) {
val conversationsList = ArrayList<HashMap<String, String>>()
conversationsArray.forEach { conversationElement ->
val conversationObj = conversationElement.asJsonObject
val conversationMap = HashMap<String, String>()
conversationMap["query"] = conversationObj.get("query").asString
conversationMap["response"] = conversationObj.get("response").asString
conversationsList.add(conversationMap)
}
news.conversations = Gson().toJson(conversationsList)
}
} catch (e: JsonSyntaxException) {
e.printStackTrace()
}
}
}
news.newsCreatedDate = JsonUtils.getLong("createdDate", newsJson)
news.newsUpdatedDate = JsonUtils.getLong("updatedDate", newsJson)
} catch (e: JsonSyntaxException) {
e.printStackTrace()
}
}

mRealm.commitTransaction()
return news
}
Expand Down
Loading

0 comments on commit f302d3b

Please sign in to comment.