2023-10-24 10:43:18 +00:00
package protocol
import (
"context"
"database/sql"
2024-01-25 13:29:29 +00:00
"errors"
2023-10-24 10:43:18 +00:00
2024-02-15 19:13:12 +00:00
"github.com/status-im/status-go/protocol/identity"
2023-10-24 10:43:18 +00:00
)
2024-02-26 13:53:40 +00:00
// Profile showcase preferences
2024-02-17 18:07:20 +00:00
const upsertProfileShowcasePreferencesQuery = "UPDATE profile_showcase_preferences SET clock=? WHERE NOT EXISTS (SELECT 1 FROM profile_showcase_preferences WHERE clock >= ?)"
const selectProfileShowcasePreferencesQuery = "SELECT clock FROM profile_showcase_preferences"
2024-01-25 16:48:27 +00:00
const upsertProfileShowcaseCommunityPreferenceQuery = "INSERT OR REPLACE INTO profile_showcase_communities_preferences(community_id, visibility, sort_order) VALUES (?, ?, ?)" // #nosec G101
const selectProfileShowcaseCommunityPreferenceQuery = "SELECT community_id, visibility, sort_order FROM profile_showcase_communities_preferences" // #nosec G101
2024-02-09 09:37:54 +00:00
const deleteProfileShowcaseCommunityPreferenceQuery = "DELETE FROM profile_showcase_communities_preferences WHERE community_id = ?" // #nosec G101
2023-11-09 18:59:01 +00:00
2024-01-25 16:48:27 +00:00
const upsertProfileShowcaseAccountPreferenceQuery = "INSERT OR REPLACE INTO profile_showcase_accounts_preferences(address, name, color_id, emoji, visibility, sort_order) VALUES (?, ?, ?, ?, ?, ?)" // #nosec G101
const selectProfileShowcaseAccountPreferenceQuery = "SELECT address, name, color_id, emoji, visibility, sort_order FROM profile_showcase_accounts_preferences" // #nosec G101
2024-02-07 09:30:56 +00:00
const selectSpecifiedShowcaseAccountPreferenceQuery = "SELECT address, name, color_id, emoji, visibility, sort_order FROM profile_showcase_accounts_preferences WHERE address = ?" // #nosec G101
const deleteProfileShowcaseAccountPreferenceQuery = "DELETE FROM profile_showcase_accounts_preferences WHERE address = ?" // #nosec G101
2023-11-09 18:59:01 +00:00
2024-01-25 16:48:27 +00:00
const upsertProfileShowcaseCollectiblePreferenceQuery = "INSERT OR REPLACE INTO profile_showcase_collectibles_preferences(contract_address, chain_id, token_id, community_id, account_address, visibility, sort_order) VALUES (?, ?, ?, ?, ?, ?, ?)" // #nosec G101
const selectProfileShowcaseCollectiblePreferenceQuery = "SELECT contract_address, chain_id, token_id, community_id, account_address, visibility, sort_order FROM profile_showcase_collectibles_preferences" // #nosec G101
2023-11-09 18:59:01 +00:00
2024-01-25 16:48:27 +00:00
const upsertProfileShowcaseVerifiedTokenPreferenceQuery = "INSERT OR REPLACE INTO profile_showcase_verified_tokens_preferences(symbol, visibility, sort_order) VALUES (?, ?, ?)" // #nosec G101
const selectProfileShowcaseVerifiedTokenPreferenceQuery = "SELECT symbol, visibility, sort_order FROM profile_showcase_verified_tokens_preferences" // #nosec G101
2023-11-09 18:59:01 +00:00
2024-01-25 16:48:27 +00:00
const upsertProfileShowcaseUnverifiedTokenPreferenceQuery = "INSERT OR REPLACE INTO profile_showcase_unverified_tokens_preferences(contract_address, chain_id, community_id, visibility, sort_order) VALUES (?, ?, ?, ?, ?)" // #nosec G101
const selectProfileShowcaseUnverifiedTokenPreferenceQuery = "SELECT contract_address, chain_id, community_id, visibility, sort_order FROM profile_showcase_unverified_tokens_preferences" // #nosec G101
2023-11-09 18:59:01 +00:00
2024-02-26 13:53:40 +00:00
const upsertProfileShowcaseSocialLinkPreferenceQuery = "INSERT OR REPLACE INTO profile_showcase_social_links_preferences(url, text, visibility, sort_order) VALUES (?, ?, ?, ?)" // #nosec G101
const selectProfileShowcaseSocialLinkPreferenceQuery = "SELECT url, text, visibility, sort_order FROM profile_showcase_social_links_preferences" // #nosec G101
// Profile showcase for a contact
2024-01-25 16:48:27 +00:00
const upsertContactProfileShowcaseCommunityQuery = "INSERT OR REPLACE INTO profile_showcase_communities_contacts(contact_id, community_id, sort_order) VALUES (?, ?, ?)" // #nosec G101
const selectContactProfileShowcaseCommunityQuery = "SELECT community_id, sort_order FROM profile_showcase_communities_contacts WHERE contact_id = ?" // #nosec G101
const removeContactProfileShowcaseCommunityQuery = "DELETE FROM profile_showcase_communities_contacts WHERE contact_id = ?" // #nosec G101
const upsertContactProfileShowcaseAccountQuery = "INSERT OR REPLACE INTO profile_showcase_accounts_contacts(contact_id, address, name, color_id, emoji, sort_order) VALUES (?, ?, ?, ?, ?, ?)" // #nosec G101
2024-01-25 13:29:29 +00:00
const selectContactProfileShowcaseAccountQuery = "SELECT * FROM profile_showcase_accounts_contacts WHERE contact_id = ?" // #nosec G101
const removeContactProfileShowcaseAccountQuery = "DELETE FROM profile_showcase_accounts_contacts WHERE contact_id = ?" // #nosec G101
2023-11-09 18:59:01 +00:00
2024-01-25 16:48:27 +00:00
const upsertContactProfileShowcaseCollectibleQuery = "INSERT OR REPLACE INTO profile_showcase_collectibles_contacts(contact_id, contract_address, chain_id, token_id, community_id, account_address, sort_order) VALUES (?, ?, ?, ?, ?, ?, ?)" // #nosec G101
const selectContactProfileShowcaseCollectibleQuery = "SELECT contract_address, chain_id, token_id, community_id, account_address, sort_order FROM profile_showcase_collectibles_contacts WHERE contact_id = ?" // #nosec G101
const removeContactProfileShowcaseCollectibleQuery = "DELETE FROM profile_showcase_collectibles_contacts WHERE contact_id = ?" // #nosec G101
const upsertContactProfileShowcaseVerifiedTokenQuery = "INSERT OR REPLACE INTO profile_showcase_verified_tokens_contacts(contact_id, symbol, sort_order) VALUES (?, ?, ?)" // #nosec G101
const selectContactProfileShowcaseVerifiedTokenQuery = "SELECT symbol, sort_order FROM profile_showcase_verified_tokens_contacts WHERE contact_id = ?" // #nosec G101
const removeContactProfileShowcaseVerifiedTokenQuery = "DELETE FROM profile_showcase_verified_tokens_contacts WHERE contact_id = ?" // #nosec G101
2023-11-09 18:59:01 +00:00
2024-01-25 16:48:27 +00:00
const upsertContactProfileShowcaseUnverifiedTokenQuery = "INSERT OR REPLACE INTO profile_showcase_unverified_tokens_contacts(contact_id, contract_address, chain_id, community_id, sort_order) VALUES (?, ?, ?, ?, ?)" // #nosec G101
const selectContactProfileShowcaseUnverifiedTokenQuery = "SELECT contract_address, chain_id, community_id, sort_order FROM profile_showcase_unverified_tokens_contacts WHERE contact_id = ?" // #nosec G101
const removeContactProfileShowcaseUnverifiedTokenQuery = "DELETE FROM profile_showcase_unverified_tokens_contacts WHERE contact_id = ?" // #nosec G101
2023-10-24 10:43:18 +00:00
2024-02-26 13:53:40 +00:00
const upsertContactProfileShowcaseSocialLinkQuery = "INSERT OR REPLACE INTO profile_showcase_social_links_contacts(contact_id, url, text, sort_order) VALUES (?, ?, ?, ?)" // #nosec G101
const selectContactProfileShowcaseSocialLinkQuery = "SELECT url, text, sort_order FROM profile_showcase_social_links_contacts WHERE contact_id = ?" // #nosec G101
const removeContactProfileShowcaseSocialLinkQuery = "DELETE FROM profile_showcase_social_links_contacts WHERE contact_id = ?" // #nosec G101
2024-01-25 13:29:29 +00:00
const selectProfileShowcaseAccountsWhichMatchTheAddress = `
SELECT psa . *
FROM
contacts c
LEFT JOIN
profile_showcase_accounts_contacts psa
ON
c . id = psa . contact_id
WHERE
psa . address = ?
`
2024-02-26 13:53:40 +00:00
// Queries for the profile showcase preferences
2024-02-17 18:07:20 +00:00
func ( db sqlitePersistence ) saveProfileShowcasePreferencesClock ( tx * sql . Tx , clock uint64 ) error {
_ , err := tx . Exec ( upsertProfileShowcasePreferencesQuery , clock , clock )
return err
}
func ( db sqlitePersistence ) getProfileShowcasePreferencesClock ( tx * sql . Tx ) ( uint64 , error ) {
var clock uint64
err := tx . QueryRow ( selectProfileShowcasePreferencesQuery ) . Scan ( & clock )
return clock , err
}
2024-02-15 19:13:12 +00:00
func ( db sqlitePersistence ) saveProfileShowcaseCommunityPreference ( tx * sql . Tx , community * identity . ProfileShowcaseCommunityPreference ) error {
2023-11-09 18:59:01 +00:00
_ , err := tx . Exec ( upsertProfileShowcaseCommunityPreferenceQuery ,
community . CommunityID ,
community . ShowcaseVisibility ,
community . Order ,
)
return err
}
2024-02-15 19:13:12 +00:00
func ( db sqlitePersistence ) getProfileShowcaseCommunitiesPreferences ( tx * sql . Tx ) ( [ ] * identity . ProfileShowcaseCommunityPreference , error ) {
2023-11-09 18:59:01 +00:00
rows , err := tx . Query ( selectProfileShowcaseCommunityPreferenceQuery )
if err != nil {
return nil , err
}
2024-02-15 19:13:12 +00:00
communities := [ ] * identity . ProfileShowcaseCommunityPreference { }
2023-11-09 18:59:01 +00:00
for rows . Next ( ) {
2024-02-15 19:13:12 +00:00
community := & identity . ProfileShowcaseCommunityPreference { }
2023-11-09 18:59:01 +00:00
err := rows . Scan (
& community . CommunityID ,
& community . ShowcaseVisibility ,
& community . Order ,
)
if err != nil {
return nil , err
}
communities = append ( communities , community )
}
return communities , nil
}
2024-02-15 19:13:12 +00:00
func ( db sqlitePersistence ) saveProfileShowcaseAccountPreference ( tx * sql . Tx , account * identity . ProfileShowcaseAccountPreference ) error {
2023-11-09 18:59:01 +00:00
_ , err := tx . Exec ( upsertProfileShowcaseAccountPreferenceQuery ,
account . Address ,
account . Name ,
account . ColorID ,
account . Emoji ,
account . ShowcaseVisibility ,
account . Order ,
2023-10-24 10:43:18 +00:00
)
2023-11-09 18:59:01 +00:00
return err
}
2024-02-15 19:13:12 +00:00
func ( db sqlitePersistence ) processProfileShowcaseAccountPreferences ( rows * sql . Rows ) ( result [ ] * identity . ProfileShowcaseAccountPreference , err error ) {
2024-02-07 09:30:56 +00:00
if rows == nil {
return nil , errors . New ( "rows is nil" )
2023-11-09 18:59:01 +00:00
}
for rows . Next ( ) {
2024-02-15 19:13:12 +00:00
account := & identity . ProfileShowcaseAccountPreference { }
2023-11-09 18:59:01 +00:00
err := rows . Scan (
& account . Address ,
& account . Name ,
& account . ColorID ,
& account . Emoji ,
& account . ShowcaseVisibility ,
& account . Order ,
)
if err != nil {
return nil , err
}
2024-02-07 09:30:56 +00:00
result = append ( result , account )
}
err = rows . Err ( )
return
}
2024-02-15 19:13:12 +00:00
func ( db sqlitePersistence ) getProfileShowcaseAccountsPreferences ( tx * sql . Tx ) ( [ ] * identity . ProfileShowcaseAccountPreference , error ) {
2024-02-07 09:30:56 +00:00
rows , err := tx . Query ( selectProfileShowcaseAccountPreferenceQuery )
if err != nil {
return nil , err
}
return db . processProfileShowcaseAccountPreferences ( rows )
}
2024-02-15 19:13:12 +00:00
func ( db sqlitePersistence ) GetProfileShowcaseAccountPreference ( accountAddress string ) ( * identity . ProfileShowcaseAccountPreference , error ) {
2024-02-07 09:30:56 +00:00
rows , err := db . db . Query ( selectSpecifiedShowcaseAccountPreferenceQuery , accountAddress )
if err != nil {
return nil , err
}
accounts , err := db . processProfileShowcaseAccountPreferences ( rows )
if len ( accounts ) > 0 {
return accounts [ 0 ] , err
2023-11-09 18:59:01 +00:00
}
2024-02-07 09:30:56 +00:00
return nil , err
}
2024-02-09 09:37:54 +00:00
func ( db sqlitePersistence ) DeleteProfileShowcaseAccountPreference ( accountAddress string ) ( bool , error ) {
result , err := db . db . Exec ( deleteProfileShowcaseAccountPreferenceQuery , accountAddress )
if err != nil {
return false , err
}
2024-02-07 09:30:56 +00:00
2024-02-09 09:37:54 +00:00
rows , err := result . RowsAffected ( )
return rows > 0 , err
}
func ( db sqlitePersistence ) DeleteProfileShowcaseCommunityPreference ( communityID string ) ( bool , error ) {
result , err := db . db . Exec ( deleteProfileShowcaseCommunityPreferenceQuery , communityID )
if err != nil {
return false , err
}
rows , err := result . RowsAffected ( )
return rows > 0 , err
2023-11-09 18:59:01 +00:00
}
2024-02-15 19:13:12 +00:00
func ( db sqlitePersistence ) saveProfileShowcaseCollectiblePreference ( tx * sql . Tx , collectible * identity . ProfileShowcaseCollectiblePreference ) error {
2023-11-09 18:59:01 +00:00
_ , err := tx . Exec ( upsertProfileShowcaseCollectiblePreferenceQuery ,
2024-01-25 16:48:27 +00:00
collectible . ContractAddress ,
collectible . ChainID ,
collectible . TokenID ,
collectible . CommunityID ,
collectible . AccountAddress ,
2023-11-09 18:59:01 +00:00
collectible . ShowcaseVisibility ,
collectible . Order ,
)
return err
}
2024-02-15 19:13:12 +00:00
func ( db sqlitePersistence ) getProfileShowcaseCollectiblesPreferences ( tx * sql . Tx ) ( [ ] * identity . ProfileShowcaseCollectiblePreference , error ) {
2023-11-09 18:59:01 +00:00
rows , err := tx . Query ( selectProfileShowcaseCollectiblePreferenceQuery )
if err != nil {
return nil , err
}
2024-02-15 19:13:12 +00:00
collectibles := [ ] * identity . ProfileShowcaseCollectiblePreference { }
2023-11-09 18:59:01 +00:00
for rows . Next ( ) {
2024-02-15 19:13:12 +00:00
collectible := & identity . ProfileShowcaseCollectiblePreference { }
2023-11-09 18:59:01 +00:00
err := rows . Scan (
2024-01-25 16:48:27 +00:00
& collectible . ContractAddress ,
& collectible . ChainID ,
& collectible . TokenID ,
& collectible . CommunityID ,
& collectible . AccountAddress ,
2023-11-09 18:59:01 +00:00
& collectible . ShowcaseVisibility ,
& collectible . Order ,
)
if err != nil {
return nil , err
}
collectibles = append ( collectibles , collectible )
}
return collectibles , nil
}
2024-02-15 19:13:12 +00:00
func ( db sqlitePersistence ) saveProfileShowcaseVerifiedTokenPreference ( tx * sql . Tx , token * identity . ProfileShowcaseVerifiedTokenPreference ) error {
2024-01-25 16:48:27 +00:00
_ , err := tx . Exec ( upsertProfileShowcaseVerifiedTokenPreferenceQuery ,
token . Symbol ,
token . ShowcaseVisibility ,
token . Order ,
2023-11-09 18:59:01 +00:00
)
return err
}
2024-02-15 19:13:12 +00:00
func ( db sqlitePersistence ) getProfileShowcaseVerifiedTokensPreferences ( tx * sql . Tx ) ( [ ] * identity . ProfileShowcaseVerifiedTokenPreference , error ) {
2024-01-25 16:48:27 +00:00
rows , err := tx . Query ( selectProfileShowcaseVerifiedTokenPreferenceQuery )
2023-11-09 18:59:01 +00:00
if err != nil {
return nil , err
}
2024-02-15 19:13:12 +00:00
tokens := [ ] * identity . ProfileShowcaseVerifiedTokenPreference { }
2023-11-09 18:59:01 +00:00
for rows . Next ( ) {
2024-02-15 19:13:12 +00:00
token := & identity . ProfileShowcaseVerifiedTokenPreference { }
2023-11-09 18:59:01 +00:00
err := rows . Scan (
2024-01-25 16:48:27 +00:00
& token . Symbol ,
& token . ShowcaseVisibility ,
& token . Order ,
2023-11-09 18:59:01 +00:00
)
if err != nil {
return nil , err
}
2024-01-25 16:48:27 +00:00
tokens = append ( tokens , token )
2023-11-09 18:59:01 +00:00
}
2024-01-25 16:48:27 +00:00
return tokens , nil
}
2024-02-26 13:53:40 +00:00
func ( db sqlitePersistence ) saveProfileShowcaseUnverifiedTokenPreference ( tx * sql . Tx , token * identity . ProfileShowcaseUnverifiedTokenPreference ) error {
_ , err := tx . Exec ( upsertProfileShowcaseUnverifiedTokenPreferenceQuery ,
token . ContractAddress ,
token . ChainID ,
token . CommunityID ,
token . ShowcaseVisibility ,
token . Order ,
)
return err
}
2024-02-15 19:13:12 +00:00
func ( db sqlitePersistence ) getProfileShowcaseUnverifiedTokensPreferences ( tx * sql . Tx ) ( [ ] * identity . ProfileShowcaseUnverifiedTokenPreference , error ) {
2024-01-25 16:48:27 +00:00
rows , err := tx . Query ( selectProfileShowcaseUnverifiedTokenPreferenceQuery )
if err != nil {
return nil , err
}
2024-02-15 19:13:12 +00:00
tokens := [ ] * identity . ProfileShowcaseUnverifiedTokenPreference { }
2024-01-25 16:48:27 +00:00
for rows . Next ( ) {
2024-02-15 19:13:12 +00:00
token := & identity . ProfileShowcaseUnverifiedTokenPreference { }
2024-01-25 16:48:27 +00:00
err := rows . Scan (
& token . ContractAddress ,
& token . ChainID ,
& token . CommunityID ,
& token . ShowcaseVisibility ,
& token . Order ,
)
if err != nil {
return nil , err
}
tokens = append ( tokens , token )
}
return tokens , nil
2023-11-09 18:59:01 +00:00
}
2024-02-26 13:53:40 +00:00
func ( db sqlitePersistence ) saveProfileShowcaseSocialLinkPreference ( tx * sql . Tx , link * identity . ProfileShowcaseSocialLinkPreference ) error {
_ , err := tx . Exec ( upsertProfileShowcaseSocialLinkPreferenceQuery ,
link . URL ,
link . Text ,
link . ShowcaseVisibility ,
link . Order ,
)
return err
}
func ( db sqlitePersistence ) getProfileShowcaseSocialLinkPreferences ( tx * sql . Tx ) ( [ ] * identity . ProfileShowcaseSocialLinkPreference , error ) {
rows , err := tx . Query ( selectProfileShowcaseSocialLinkPreferenceQuery )
if err != nil {
return nil , err
}
links := [ ] * identity . ProfileShowcaseSocialLinkPreference { }
for rows . Next ( ) {
link := & identity . ProfileShowcaseSocialLinkPreference { }
err := rows . Scan (
& link . URL ,
& link . Text ,
& link . ShowcaseVisibility ,
& link . Order ,
)
if err != nil {
return nil , err
}
links = append ( links , link )
}
return links , nil
}
// Queries for the profile showcase for a contact
2024-02-15 19:13:12 +00:00
func ( db sqlitePersistence ) saveProfileShowcaseCommunityContact ( tx * sql . Tx , contactID string , community * identity . ProfileShowcaseCommunity ) error {
2023-11-09 18:59:01 +00:00
_ , err := tx . Exec ( upsertContactProfileShowcaseCommunityQuery ,
contactID ,
community . CommunityID ,
community . Order ,
)
return err
}
2024-02-15 19:13:12 +00:00
func ( db sqlitePersistence ) getProfileShowcaseCommunitiesContact ( tx * sql . Tx , contactID string ) ( [ ] * identity . ProfileShowcaseCommunity , error ) {
2023-11-09 18:59:01 +00:00
rows , err := tx . Query ( selectContactProfileShowcaseCommunityQuery , contactID )
if err != nil {
return nil , err
}
2024-02-15 19:13:12 +00:00
communities := [ ] * identity . ProfileShowcaseCommunity { }
2023-11-09 18:59:01 +00:00
for rows . Next ( ) {
2024-02-15 19:13:12 +00:00
community := & identity . ProfileShowcaseCommunity {
MembershipStatus : identity . ProfileShowcaseMembershipStatusUnproven ,
}
2023-11-09 18:59:01 +00:00
err := rows . Scan ( & community . CommunityID , & community . Order )
if err != nil {
return nil , err
}
communities = append ( communities , community )
}
return communities , nil
}
func ( db sqlitePersistence ) clearProfileShowcaseCommunityContact ( tx * sql . Tx , contactID string ) error {
_ , err := tx . Exec ( removeContactProfileShowcaseCommunityQuery , contactID )
2023-10-24 10:43:18 +00:00
if err != nil {
return err
}
return nil
}
2024-02-15 19:13:12 +00:00
func ( db sqlitePersistence ) saveProfileShowcaseAccountContact ( tx * sql . Tx , contactID string , account * identity . ProfileShowcaseAccount ) error {
2023-11-09 18:59:01 +00:00
_ , err := tx . Exec ( upsertContactProfileShowcaseAccountQuery ,
contactID ,
account . Address ,
account . Name ,
account . ColorID ,
account . Emoji ,
account . Order ,
)
return err
}
2024-02-15 19:13:12 +00:00
func ( db sqlitePersistence ) processProfileShowcaseAccounts ( rows * sql . Rows ) ( result [ ] * identity . ProfileShowcaseAccount , err error ) {
2024-01-25 13:29:29 +00:00
if rows == nil {
return nil , errors . New ( "rows is nil" )
2023-11-09 18:59:01 +00:00
}
for rows . Next ( ) {
2024-02-15 19:13:12 +00:00
account := & identity . ProfileShowcaseAccount { }
2023-11-09 18:59:01 +00:00
2024-01-25 13:29:29 +00:00
err = rows . Scan ( & account . Address , & account . Name , & account . ColorID , & account . Emoji , & account . Order , & account . ContactID )
2023-11-09 18:59:01 +00:00
if err != nil {
2024-01-25 13:29:29 +00:00
return
2023-11-09 18:59:01 +00:00
}
2024-01-25 13:29:29 +00:00
result = append ( result , account )
2023-11-09 18:59:01 +00:00
}
2024-01-25 13:29:29 +00:00
err = rows . Err ( )
return
}
2024-02-15 19:13:12 +00:00
func ( db sqlitePersistence ) getProfileShowcaseAccountsContact ( tx * sql . Tx , contactID string ) ( [ ] * identity . ProfileShowcaseAccount , error ) {
2024-01-25 13:29:29 +00:00
rows , err := tx . Query ( selectContactProfileShowcaseAccountQuery , contactID )
if err != nil {
return nil , err
}
return db . processProfileShowcaseAccounts ( rows )
}
2024-02-15 19:13:12 +00:00
func ( db sqlitePersistence ) GetProfileShowcaseAccountsByAddress ( address string ) ( [ ] * identity . ProfileShowcaseAccount , error ) {
2024-01-25 13:29:29 +00:00
rows , err := db . db . Query ( selectProfileShowcaseAccountsWhichMatchTheAddress , address )
if err != nil {
return nil , err
}
return db . processProfileShowcaseAccounts ( rows )
2023-11-09 18:59:01 +00:00
}
func ( db sqlitePersistence ) clearProfileShowcaseAccountsContact ( tx * sql . Tx , contactID string ) error {
_ , err := tx . Exec ( removeContactProfileShowcaseAccountQuery , contactID )
return err
}
2024-02-15 19:13:12 +00:00
func ( db sqlitePersistence ) saveProfileShowcaseCollectibleContact ( tx * sql . Tx , contactID string , collectible * identity . ProfileShowcaseCollectible ) error {
2023-11-09 18:59:01 +00:00
_ , err := tx . Exec ( upsertContactProfileShowcaseCollectibleQuery ,
contactID ,
2024-01-25 16:48:27 +00:00
collectible . ContractAddress ,
collectible . ChainID ,
collectible . TokenID ,
collectible . CommunityID ,
collectible . AccountAddress ,
collectible . Order ,
2023-11-09 18:59:01 +00:00
)
return err
}
2024-02-15 19:13:12 +00:00
func ( db sqlitePersistence ) getProfileShowcaseCollectiblesContact ( tx * sql . Tx , contactID string ) ( [ ] * identity . ProfileShowcaseCollectible , error ) {
2023-11-09 18:59:01 +00:00
rows , err := tx . Query ( selectContactProfileShowcaseCollectibleQuery , contactID )
if err != nil {
return nil , err
}
2024-02-15 19:13:12 +00:00
collectibles := [ ] * identity . ProfileShowcaseCollectible { }
2023-11-09 18:59:01 +00:00
for rows . Next ( ) {
2024-02-15 19:13:12 +00:00
collectible := & identity . ProfileShowcaseCollectible { }
2023-11-09 18:59:01 +00:00
2024-01-25 16:48:27 +00:00
err := rows . Scan (
& collectible . ContractAddress ,
& collectible . ChainID ,
& collectible . TokenID ,
& collectible . CommunityID ,
& collectible . AccountAddress ,
& collectible . Order )
2023-11-09 18:59:01 +00:00
if err != nil {
return nil , err
}
collectibles = append ( collectibles , collectible )
}
return collectibles , nil
}
func ( db sqlitePersistence ) clearProfileShowcaseCollectiblesContact ( tx * sql . Tx , contactID string ) error {
_ , err := tx . Exec ( removeContactProfileShowcaseCollectibleQuery , contactID )
return err
}
2024-02-15 19:13:12 +00:00
func ( db sqlitePersistence ) saveProfileShowcaseVerifiedTokenContact ( tx * sql . Tx , contactID string , token * identity . ProfileShowcaseVerifiedToken ) error {
2024-01-25 16:48:27 +00:00
_ , err := tx . Exec ( upsertContactProfileShowcaseVerifiedTokenQuery ,
contactID ,
token . Symbol ,
token . Order ,
)
return err
}
2024-02-15 19:13:12 +00:00
func ( db sqlitePersistence ) getProfileShowcaseVerifiedTokensContact ( tx * sql . Tx , contactID string ) ( [ ] * identity . ProfileShowcaseVerifiedToken , error ) {
2024-01-25 16:48:27 +00:00
rows , err := tx . Query ( selectContactProfileShowcaseVerifiedTokenQuery , contactID )
2023-11-09 18:59:01 +00:00
if err != nil {
return nil , err
}
2024-02-15 19:13:12 +00:00
tokens := [ ] * identity . ProfileShowcaseVerifiedToken { }
2023-11-09 18:59:01 +00:00
for rows . Next ( ) {
2024-02-15 19:13:12 +00:00
token := & identity . ProfileShowcaseVerifiedToken { }
2023-11-09 18:59:01 +00:00
2024-01-25 16:48:27 +00:00
err := rows . Scan (
& token . Symbol ,
& token . Order )
2023-11-09 18:59:01 +00:00
if err != nil {
return nil , err
}
2024-01-25 16:48:27 +00:00
tokens = append ( tokens , token )
2023-11-09 18:59:01 +00:00
}
2024-01-25 16:48:27 +00:00
return tokens , nil
2023-11-09 18:59:01 +00:00
}
2024-02-26 13:53:40 +00:00
func ( db sqlitePersistence ) clearProfileShowcaseVerifiedTokensContact ( tx * sql . Tx , contactID string ) error {
_ , err := tx . Exec ( removeContactProfileShowcaseVerifiedTokenQuery , contactID )
return err
}
func ( db sqlitePersistence ) saveProfileShowcaseUnverifiedTokenContact ( tx * sql . Tx , contactID string , token * identity . ProfileShowcaseUnverifiedToken ) error {
_ , err := tx . Exec ( upsertContactProfileShowcaseUnverifiedTokenQuery ,
contactID ,
token . ContractAddress ,
token . ChainID ,
token . CommunityID ,
token . Order ,
)
return err
}
2024-02-15 19:13:12 +00:00
func ( db sqlitePersistence ) getProfileShowcaseUnverifiedTokensContact ( tx * sql . Tx , contactID string ) ( [ ] * identity . ProfileShowcaseUnverifiedToken , error ) {
2024-01-25 16:48:27 +00:00
rows , err := tx . Query ( selectContactProfileShowcaseUnverifiedTokenQuery , contactID )
if err != nil {
return nil , err
}
2024-02-15 19:13:12 +00:00
tokens := [ ] * identity . ProfileShowcaseUnverifiedToken { }
2024-01-25 16:48:27 +00:00
for rows . Next ( ) {
2024-02-15 19:13:12 +00:00
token := & identity . ProfileShowcaseUnverifiedToken { }
2024-01-25 16:48:27 +00:00
err := rows . Scan (
& token . ContractAddress ,
& token . ChainID ,
& token . CommunityID ,
& token . Order )
if err != nil {
return nil , err
}
tokens = append ( tokens , token )
}
return tokens , nil
}
2024-02-26 13:53:40 +00:00
func ( db sqlitePersistence ) clearProfileShowcaseUnverifiedTokensContact ( tx * sql . Tx , contactID string ) error {
_ , err := tx . Exec ( removeContactProfileShowcaseUnverifiedTokenQuery , contactID )
2024-01-25 16:48:27 +00:00
return err
}
2024-02-26 13:53:40 +00:00
func ( db sqlitePersistence ) saveProfileShowcaseSocialLinkContact ( tx * sql . Tx , contactID string , link * identity . ProfileShowcaseSocialLink ) error {
_ , err := tx . Exec ( upsertContactProfileShowcaseSocialLinkQuery ,
contactID ,
link . URL ,
link . Text ,
link . Order ,
)
return err
}
func ( db sqlitePersistence ) getProfileShowcaseSocialLinksContact ( tx * sql . Tx , contactID string ) ( [ ] * identity . ProfileShowcaseSocialLink , error ) {
rows , err := tx . Query ( selectContactProfileShowcaseSocialLinkQuery , contactID )
if err != nil {
return nil , err
}
links := [ ] * identity . ProfileShowcaseSocialLink { }
for rows . Next ( ) {
link := & identity . ProfileShowcaseSocialLink { }
err := rows . Scan (
& link . URL ,
& link . Text ,
& link . Order )
if err != nil {
return nil , err
}
links = append ( links , link )
}
err = rows . Err ( )
if err != nil {
return nil , err
}
return links , nil
}
func ( db sqlitePersistence ) clearProfileShowcaseSocialLinksContact ( tx * sql . Tx , contactID string ) error {
_ , err := tx . Exec ( removeContactProfileShowcaseSocialLinkQuery , contactID )
2023-11-09 18:59:01 +00:00
return err
}
// public functions
2024-02-15 19:13:12 +00:00
func ( db sqlitePersistence ) SaveProfileShowcasePreferences ( preferences * identity . ProfileShowcasePreferences ) error {
2023-10-24 10:43:18 +00:00
tx , err := db . db . BeginTx ( context . Background ( ) , & sql . TxOptions { } )
if err != nil {
return err
}
defer func ( ) {
if err == nil {
err = tx . Commit ( )
return
}
// don't shadow original error
_ = tx . Rollback ( )
} ( )
2024-02-17 18:07:20 +00:00
err = db . saveProfileShowcasePreferencesClock ( tx , preferences . Clock )
if err != nil {
return err
}
2023-11-09 18:59:01 +00:00
for _ , community := range preferences . Communities {
err = db . saveProfileShowcaseCommunityPreference ( tx , community )
if err != nil {
return err
}
}
2023-10-24 10:43:18 +00:00
2023-11-09 18:59:01 +00:00
for _ , account := range preferences . Accounts {
err = db . saveProfileShowcaseAccountPreference ( tx , account )
if err != nil {
return err
}
}
for _ , collectible := range preferences . Collectibles {
err = db . saveProfileShowcaseCollectiblePreference ( tx , collectible )
if err != nil {
return err
}
}
2024-01-25 16:48:27 +00:00
for _ , token := range preferences . VerifiedTokens {
err = db . saveProfileShowcaseVerifiedTokenPreference ( tx , token )
if err != nil {
return err
}
}
for _ , token := range preferences . UnverifiedTokens {
err = db . saveProfileShowcaseUnverifiedTokenPreference ( tx , token )
2023-10-24 10:43:18 +00:00
if err != nil {
return err
}
}
2024-02-26 13:53:40 +00:00
for _ , link := range preferences . SocialLinks {
err = db . saveProfileShowcaseSocialLinkPreference ( tx , link )
if err != nil {
return err
}
}
2023-10-24 10:43:18 +00:00
return nil
}
2024-02-15 19:13:12 +00:00
func ( db sqlitePersistence ) SaveProfileShowcaseAccountPreference ( account * identity . ProfileShowcaseAccountPreference ) error {
2024-02-07 09:30:56 +00:00
tx , err := db . db . BeginTx ( context . Background ( ) , & sql . TxOptions { } )
if err != nil {
return err
}
defer func ( ) {
if err == nil {
err = tx . Commit ( )
return
}
// don't shadow original error
_ = tx . Rollback ( )
} ( )
return db . saveProfileShowcaseAccountPreference ( tx , account )
}
2024-02-15 19:13:12 +00:00
func ( db sqlitePersistence ) GetProfileShowcasePreferences ( ) ( * identity . ProfileShowcasePreferences , error ) {
2023-11-09 18:59:01 +00:00
tx , err := db . db . BeginTx ( context . Background ( ) , & sql . TxOptions { } )
if err != nil {
return nil , err
}
defer func ( ) {
if err == nil {
err = tx . Commit ( )
return
}
// don't shadow original error
_ = tx . Rollback ( )
} ( )
2023-10-24 10:43:18 +00:00
2024-02-17 18:07:20 +00:00
clock , err := db . getProfileShowcasePreferencesClock ( tx )
if err != nil {
return nil , err
}
2023-11-09 18:59:01 +00:00
communities , err := db . getProfileShowcaseCommunitiesPreferences ( tx )
if err != nil {
return nil , err
}
2023-10-24 10:43:18 +00:00
2023-11-09 18:59:01 +00:00
accounts , err := db . getProfileShowcaseAccountsPreferences ( tx )
if err != nil {
return nil , err
}
collectibles , err := db . getProfileShowcaseCollectiblesPreferences ( tx )
if err != nil {
return nil , err
}
2024-01-25 16:48:27 +00:00
verifiedTokens , err := db . getProfileShowcaseVerifiedTokensPreferences ( tx )
if err != nil {
return nil , err
}
unverifiedTokens , err := db . getProfileShowcaseUnverifiedTokensPreferences ( tx )
2023-11-09 18:59:01 +00:00
if err != nil {
return nil , err
}
2024-02-26 13:53:40 +00:00
socialLinks , err := db . getProfileShowcaseSocialLinkPreferences ( tx )
if err != nil {
return nil , err
}
2024-02-15 19:13:12 +00:00
return & identity . ProfileShowcasePreferences {
2024-02-17 18:07:20 +00:00
Clock : clock ,
2024-01-25 16:48:27 +00:00
Communities : communities ,
Accounts : accounts ,
Collectibles : collectibles ,
VerifiedTokens : verifiedTokens ,
UnverifiedTokens : unverifiedTokens ,
2024-02-26 13:53:40 +00:00
SocialLinks : socialLinks ,
2023-11-09 18:59:01 +00:00
} , nil
}
2024-02-15 19:13:12 +00:00
func ( db sqlitePersistence ) SaveProfileShowcaseForContact ( showcase * identity . ProfileShowcase ) error {
2023-11-09 18:59:01 +00:00
tx , err := db . db . BeginTx ( context . Background ( ) , & sql . TxOptions { } )
if err != nil {
return err
}
defer func ( ) {
if err == nil {
err = tx . Commit ( )
return
}
// don't shadow original error
_ = tx . Rollback ( )
} ( )
2023-10-24 10:43:18 +00:00
2023-11-09 18:59:01 +00:00
for _ , community := range showcase . Communities {
err = db . saveProfileShowcaseCommunityContact ( tx , showcase . ContactID , community )
2023-10-24 10:43:18 +00:00
if err != nil {
2023-11-09 18:59:01 +00:00
return err
}
}
for _ , account := range showcase . Accounts {
err = db . saveProfileShowcaseAccountContact ( tx , showcase . ContactID , account )
if err != nil {
return err
2023-10-24 10:43:18 +00:00
}
2023-11-09 18:59:01 +00:00
}
2023-10-24 10:43:18 +00:00
2023-11-09 18:59:01 +00:00
for _ , collectible := range showcase . Collectibles {
err = db . saveProfileShowcaseCollectibleContact ( tx , showcase . ContactID , collectible )
if err != nil {
return err
}
2023-10-24 10:43:18 +00:00
}
2023-11-09 18:59:01 +00:00
2024-01-25 16:48:27 +00:00
for _ , token := range showcase . VerifiedTokens {
err = db . saveProfileShowcaseVerifiedTokenContact ( tx , showcase . ContactID , token )
if err != nil {
return err
}
}
for _ , token := range showcase . UnverifiedTokens {
err = db . saveProfileShowcaseUnverifiedTokenContact ( tx , showcase . ContactID , token )
2023-11-09 18:59:01 +00:00
if err != nil {
return err
}
}
2024-02-26 13:53:40 +00:00
for _ , link := range showcase . SocialLinks {
err = db . saveProfileShowcaseSocialLinkContact ( tx , showcase . ContactID , link )
if err != nil {
return err
}
}
2023-11-09 18:59:01 +00:00
return nil
2023-10-24 10:43:18 +00:00
}
2024-02-15 19:13:12 +00:00
func ( db sqlitePersistence ) GetProfileShowcaseForContact ( contactID string ) ( * identity . ProfileShowcase , error ) {
2023-11-09 18:59:01 +00:00
tx , err := db . db . BeginTx ( context . Background ( ) , & sql . TxOptions { } )
2023-10-24 10:43:18 +00:00
if err != nil {
return nil , err
}
2023-11-09 18:59:01 +00:00
defer func ( ) {
if err == nil {
err = tx . Commit ( )
return
}
// don't shadow original error
_ = tx . Rollback ( )
} ( )
2023-10-24 10:43:18 +00:00
2023-11-09 18:59:01 +00:00
communities , err := db . getProfileShowcaseCommunitiesContact ( tx , contactID )
if err != nil {
return nil , err
}
2023-10-24 10:43:18 +00:00
2023-11-09 18:59:01 +00:00
accounts , err := db . getProfileShowcaseAccountsContact ( tx , contactID )
2023-10-24 10:43:18 +00:00
if err != nil {
return nil , err
}
2023-11-09 18:59:01 +00:00
collectibles , err := db . getProfileShowcaseCollectiblesContact ( tx , contactID )
if err != nil {
return nil , err
}
2024-01-25 16:48:27 +00:00
verifiedTokens , err := db . getProfileShowcaseVerifiedTokensContact ( tx , contactID )
if err != nil {
return nil , err
}
unverifiedTokens , err := db . getProfileShowcaseUnverifiedTokensContact ( tx , contactID )
2023-11-09 18:59:01 +00:00
if err != nil {
return nil , err
}
2024-02-26 13:53:40 +00:00
socialLinks , err := db . getProfileShowcaseSocialLinksContact ( tx , contactID )
if err != nil {
return nil , err
}
2024-02-15 19:13:12 +00:00
return & identity . ProfileShowcase {
2024-01-25 16:48:27 +00:00
ContactID : contactID ,
Communities : communities ,
Accounts : accounts ,
Collectibles : collectibles ,
VerifiedTokens : verifiedTokens ,
UnverifiedTokens : unverifiedTokens ,
2024-02-26 13:53:40 +00:00
SocialLinks : socialLinks ,
2023-11-09 18:59:01 +00:00
} , nil
}
func ( db sqlitePersistence ) ClearProfileShowcaseForContact ( contactID string ) error {
tx , err := db . db . BeginTx ( context . Background ( ) , & sql . TxOptions { } )
if err != nil {
return err
}
defer func ( ) {
if err == nil {
err = tx . Commit ( )
return
}
// don't shadow original error
_ = tx . Rollback ( )
} ( )
err = db . clearProfileShowcaseCommunityContact ( tx , contactID )
if err != nil {
return err
}
err = db . clearProfileShowcaseAccountsContact ( tx , contactID )
if err != nil {
return err
}
err = db . clearProfileShowcaseCollectiblesContact ( tx , contactID )
if err != nil {
return err
}
2024-01-25 16:48:27 +00:00
err = db . clearProfileShowcaseVerifiedTokensContact ( tx , contactID )
if err != nil {
return err
}
err = db . clearProfileShowcaseUnverifiedTokensContact ( tx , contactID )
2023-11-09 18:59:01 +00:00
if err != nil {
return err
}
2024-02-26 13:53:40 +00:00
err = db . clearProfileShowcaseSocialLinksContact ( tx , contactID )
if err != nil {
return err
}
2023-11-09 18:59:01 +00:00
return nil
2023-10-24 10:43:18 +00:00
}