Skip to content

Commit

Permalink
Merge branch 'master' into work-be2-ons-federation-dataExchange-imple…
Browse files Browse the repository at this point in the history
…mentation
  • Loading branch information
onsriahi14 committed Jun 30, 2024
2 parents c552a98 + 58523c7 commit da70de8
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 8 deletions.
7 changes: 4 additions & 3 deletions be1-go/internal/database/sqlite/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ func (s *SQLite) StoreRumor(rumorID int, sender string, timestamp mrumor.RumorTi
if err != nil {
return poperrors.NewDatabaseTransactionBeginErrorMsg(err.Error())
}
defer tx.Rollback()

timestampBuf, err := json.Marshal(timestamp)
if err != nil {
Expand Down Expand Up @@ -283,12 +284,12 @@ func (s *SQLite) AddMessageToMyRumor(messageID string) (int, error) {
}
defer tx.Rollback()

_, err = s.database.Exec(insertMessageToMyRumor, messageID, serverKeysPath)
_, err = tx.Exec(insertMessageToMyRumor, messageID, serverKeysPath)
if err != nil {
return -1, poperrors.NewDatabaseInsertErrorMsg("message to the current rumor: %v", err)
}
var count int
err = s.database.QueryRow(selectCountMyRumor, serverKeysPath).Scan(&count)
err = tx.QueryRow(selectCountMyRumor, serverKeysPath).Scan(&count)
if err != nil {
return -1, poperrors.NewDatabaseSelectErrorMsg("number of messages in the current rumor: %v", err)
}
Expand All @@ -310,7 +311,7 @@ func (s *SQLite) GetAndIncrementMyRumor() (bool, mrumor.ParamsRumor, error) {
}
defer tx.Rollback()

rows, err := s.database.Query(selectMyRumorMessages, true, serverKeysPath, serverKeysPath)
rows, err := tx.Query(selectMyRumorMessages, true, serverKeysPath, serverKeysPath)
if err != nil {
return false, mrumor.ParamsRumor{}, poperrors.NewDatabaseSelectErrorMsg("current rumor params: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion be1-go/internal/database/sqlite/sqlite_const.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ const (
insertKeys = `INSERT INTO key (channelPath, publicKey, secretKey) VALUES (?, ?, ?)`
insertPublicKey = `INSERT INTO key (channelPath, publicKey) VALUES (?, ?)`
insertRumor = `INSERT INTO rumor (ID, sender, timestamp) VALUES (?, ?, ?)`
insertUnprocessedMessage = `INSERT INTO unprocessedMessage (messageID, channelPath, message) VALUES (?, ?, ?)`
insertUnprocessedMessage = `INSERT OR IGNORE INTO unprocessedMessage (messageID, channelPath, message) VALUES (?, ?, ?)`
insertUnprocessedMessageRumor = `INSERT INTO unprocessedMessageRumor (messageID, rumorID, sender) VALUES (?, ?, ?)`
insertMessageRumor = `INSERT INTO messageRumor (messageID, rumorID, sender) VALUES (?, ?, ?)`
tranferUnprocessedMessageRumor = `INSERT INTO messageRumor (messageID, rumorID, sender) SELECT messageID, rumorID, sender FROM unprocessedMessageRumor WHERE messageID = ?`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.DrawableRes
Expand Down Expand Up @@ -89,6 +90,7 @@ class ChirpListAdapter(
val itemUsername = view.findViewById<TextView>(R.id.social_media_username)
val itemTime = view.findViewById<TextView>(R.id.social_media_time)
val itemText = view.findViewById<TextView>(R.id.social_media_text)
val itemProfile = view.findViewById<ImageView>(R.id.social_media_profile)
val deleteChirp = view.findViewById<ImageButton>(R.id.delete_chirp_button)
val upvoteChirp = view.findViewById<ImageButton>(R.id.upvote_button)
val downvoteChirp = view.findViewById<ImageButton>(R.id.downvote_button)
Expand Down Expand Up @@ -192,6 +194,13 @@ class ChirpListAdapter(
itemTime.text = DateUtils.getRelativeTimeSpanString(timestamp * 1000)
itemText.text = text

// Changes the color of the profile if the chirps comes from another LAO
if (chirp.laoId == laoViewModel.laoId) {
itemProfile.imageTintList = context.getColorStateList(R.color.colorAccent)
} else {
itemProfile.imageTintList = context.getColorStateList(R.color.gray)
}

return view
}

Expand Down
1 change: 1 addition & 0 deletions fe2-android/app/src/main/res/layout/chirp_card.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
android:orientation="horizontal">

<ImageView
android:id="@+id/social_media_profile"
android:layout_width="@dimen/bottom_navigation_view_height"
android:layout_height="@dimen/bottom_navigation_view_height"
android:contentDescription="@string/profile"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.github.dedis.popstellar.ui.lao.socialmedia

import android.content.Context
import android.text.format.DateUtils
import android.view.View
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.fragment.app.FragmentActivity
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.github.dedis.popstellar.R
Expand Down Expand Up @@ -37,6 +40,7 @@ import com.github.dedis.popstellar.utility.security.KeyManager
import dagger.hilt.android.testing.BindValue
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import io.reactivex.Observable
import org.junit.Assert
import org.junit.Rule
import org.junit.Test
Expand Down Expand Up @@ -72,9 +76,9 @@ class ChirpListAdapterTest {
private val REACTION2 =
Reaction(
Base64DataUtils.generateMessageID(),
SENDER_1,
SENDER_2,
Reaction.ReactionEmoji.DOWNVOTE.code,
CHIRP_1.id,
CHIRP_3.id,
TIMESTAMP,
)
private val REACTION3 =
Expand Down Expand Up @@ -301,24 +305,75 @@ class ChirpListAdapterTest {
}
}

@Test
fun profileDisplayTest() {
val mockSocialMediaViewModel = Mockito.mock(SocialMediaViewModel::class.java)
Mockito.`when`(mockSocialMediaViewModel.chirps)
.thenReturn(Observable.just(ArrayList(listOf(CHIRP_1, CHIRP_3))))
Mockito.`when`(mockSocialMediaViewModel.getReactions(LAO_ID, MESSAGE_ID_1))
.thenReturn(Observable.just(setOf(REACTION)))
Mockito.`when`(mockSocialMediaViewModel.getReactions(LAO_ID_2, MESSAGE_ID_3))
.thenReturn(Observable.just(setOf(REACTION2)))

val chirps = ArrayList(listOf(CHIRP_1, CHIRP_3))
activityScenarioRule.scenario.onActivity { activity: LaoActivity ->
val socialMediaViewModel = mockSocialMediaViewModel
val viewModel = obtainViewModel(activity)
val chirpListAdapter =
createChirpListAdapter(activity, viewModel, socialMediaViewModel, chirps)
val context = ApplicationProvider.getApplicationContext<Context>()

// Use a non null ViewGroup to inflate the card
val layout = LinearLayout(activity.applicationContext)
val parent = TextView(activity.applicationContext)
parent.text = "Mock Title"
layout.addView(parent)

// Get the view for the first chirp in the list.
val view1 = chirpListAdapter.getView(0, null, layout)
Assert.assertNotNull(view1)

// The chirp of our LAO should be blue
val profile1 = view1.findViewById<ImageView>(R.id.social_media_profile)
Assert.assertNotNull(profile1)
Assert.assertEquals(context.getColor(R.color.colorAccent), profile1.imageTintList?.defaultColor)

// Get the view for the second chirp in the list.
val view2 = chirpListAdapter.getView(1, null, layout)
Assert.assertNotNull(view2)

// The chirp of the other LAO should be gray
val profile2 = view2.findViewById<ImageView>(R.id.social_media_profile)
Assert.assertNotNull(profile2)
Assert.assertEquals(context.getColor(R.color.gray), profile2.imageTintList?.defaultColor)
}
}

companion object {
private const val CREATION_TIME: Long = 1631280815
private const val LAO_NAME = "laoName"
private const val LAO_NAME_1 = "laoName1"
private const val LAO_NAME_2 = "laoName2"
private var SENDER_KEY_1: KeyPair = Base64DataUtils.generatePoPToken()
private val SENDER_KEY_2: KeyPair = Base64DataUtils.generatePoPToken()
private val SENDER_1 = SENDER_KEY_1.publicKey
private val SENDER_2 = SENDER_KEY_2.publicKey
private val LAO_ID = generateLaoId(SENDER_1, CREATION_TIME, LAO_NAME)
private val LAO_ID = generateLaoId(SENDER_1, CREATION_TIME, LAO_NAME_1)
private val LAO_ID_2 = generateLaoId(SENDER_2, CREATION_TIME, LAO_NAME_2)
private val MESSAGE_ID_1 = Base64DataUtils.generateMessageID()
private val MESSAGE_ID_2 = Base64DataUtils.generateMessageID()
private val MESSAGE_ID_3 = Base64DataUtils.generateMessageID()
private const val TEXT_1 = "text1"
private const val TEXT_2 = "text2"
private const val TEXT_3 = "text3"
private const val TIMESTAMP_1: Long = 1632204910
private const val TIMESTAMP_2: Long = 1632204900
private const val TIMESTAMP_3: Long = 1632204905
private val CHIRP_1 =
Chirp(MESSAGE_ID_1, SENDER_1, TEXT_1, TIMESTAMP_1, MessageID(""), LAO_ID)
private val CHIRP_2 =
Chirp(MESSAGE_ID_2, SENDER_2, TEXT_2, TIMESTAMP_2, MessageID(""), LAO_ID).deleted()
private val CHIRP_3 =
Chirp(MESSAGE_ID_3, SENDER_2, TEXT_3, TIMESTAMP_3, MessageID(""), LAO_ID_2)
private val TIMESTAMP = Instant.now().epochSecond

private fun createChirpList(): List<Chirp> {
Expand Down

0 comments on commit da70de8

Please sign in to comment.