2022-03-23 18:47:00 +00:00
|
|
|
package settings
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"reflect"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/protocol/common"
|
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
|
|
|
"github.com/status-im/status-go/sqlite"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrTypeAssertionFailed = errors.New("type assertion of interface value failed")
|
|
|
|
)
|
|
|
|
|
|
|
|
func buildRawSyncSettingMessage(msg *protobuf.SyncSetting, chatID string) (*common.RawMessage, error) {
|
|
|
|
encodedMessage, err := proto.Marshal(msg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &common.RawMessage{
|
|
|
|
LocalChatID: chatID,
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_SYNC_SETTING,
|
|
|
|
ResendAutomatically: true,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Currency
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func buildRawCurrencySyncMessage(v string, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
pb := &protobuf.SyncSetting{
|
|
|
|
Type: protobuf.SyncSetting_CURRENCY,
|
|
|
|
Value: &protobuf.SyncSetting_ValueString{ValueString: v},
|
|
|
|
Clock: clock,
|
|
|
|
}
|
2022-11-30 09:41:35 +00:00
|
|
|
rm, err := buildRawSyncSettingMessage(pb, chatID)
|
|
|
|
return rm, pb, err
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func currencyProtobufFactory(value interface{}, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
v, err := assertString(value)
|
|
|
|
if err != nil {
|
2022-11-30 09:41:35 +00:00
|
|
|
return nil, nil, err
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return buildRawCurrencySyncMessage(v, clock, chatID)
|
|
|
|
}
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func currencyProtobufFactoryStruct(s Settings, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
return buildRawCurrencySyncMessage(s.Currency, clock, chatID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GifFavorites
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func buildRawGifFavoritesSyncMessage(v []byte, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
pb := &protobuf.SyncSetting{
|
|
|
|
Type: protobuf.SyncSetting_GIF_FAVOURITES,
|
|
|
|
Value: &protobuf.SyncSetting_ValueBytes{ValueBytes: v},
|
|
|
|
Clock: clock,
|
|
|
|
}
|
2022-11-30 09:41:35 +00:00
|
|
|
rm, err := buildRawSyncSettingMessage(pb, chatID)
|
|
|
|
return rm, pb, err
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func gifFavouritesProtobufFactory(value interface{}, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
v, err := assertBytes(value)
|
|
|
|
if err != nil {
|
2022-11-30 09:41:35 +00:00
|
|
|
return nil, nil, err
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return buildRawGifFavoritesSyncMessage(v, clock, chatID)
|
|
|
|
}
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func gifFavouritesProtobufFactoryStruct(s Settings, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
gf := extractJSONRawMessage(s.GifFavorites)
|
|
|
|
return buildRawGifFavoritesSyncMessage(gf, clock, chatID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GifRecents
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func buildRawGifRecentsSyncMessage(v []byte, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
pb := &protobuf.SyncSetting{
|
|
|
|
Type: protobuf.SyncSetting_GIF_RECENTS,
|
|
|
|
Value: &protobuf.SyncSetting_ValueBytes{ValueBytes: v},
|
|
|
|
Clock: clock,
|
|
|
|
}
|
2022-11-30 09:41:35 +00:00
|
|
|
rm, err := buildRawSyncSettingMessage(pb, chatID)
|
|
|
|
return rm, pb, err
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func gifRecentsProtobufFactory(value interface{}, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
v, err := assertBytes(value)
|
|
|
|
if err != nil {
|
2022-11-30 09:41:35 +00:00
|
|
|
return nil, nil, err
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return buildRawGifRecentsSyncMessage(v, clock, chatID)
|
|
|
|
}
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func gifRecentsProtobufFactoryStruct(s Settings, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
gr := extractJSONRawMessage(s.GifRecents)
|
|
|
|
return buildRawGifRecentsSyncMessage(gr, clock, chatID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MessagesFromContactsOnly
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func buildRawMessagesFromContactsOnlySyncMessage(v bool, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
pb := &protobuf.SyncSetting{
|
|
|
|
Type: protobuf.SyncSetting_MESSAGES_FROM_CONTACTS_ONLY,
|
|
|
|
Value: &protobuf.SyncSetting_ValueBool{ValueBool: v},
|
|
|
|
Clock: clock,
|
|
|
|
}
|
2022-11-30 09:41:35 +00:00
|
|
|
rm, err := buildRawSyncSettingMessage(pb, chatID)
|
|
|
|
return rm, pb, err
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func messagesFromContactsOnlyProtobufFactory(value interface{}, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
v, err := assertBool(value)
|
|
|
|
if err != nil {
|
2022-11-30 09:41:35 +00:00
|
|
|
return nil, nil, err
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return buildRawMessagesFromContactsOnlySyncMessage(v, clock, chatID)
|
|
|
|
}
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func messagesFromContactsOnlyProtobufFactoryStruct(s Settings, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
return buildRawMessagesFromContactsOnlySyncMessage(s.MessagesFromContactsOnly, clock, chatID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PreferredName
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func buildRawPreferredNameSyncMessage(v string, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
pb := &protobuf.SyncSetting{
|
|
|
|
Type: protobuf.SyncSetting_PREFERRED_NAME,
|
|
|
|
Value: &protobuf.SyncSetting_ValueString{ValueString: v},
|
|
|
|
Clock: clock,
|
|
|
|
}
|
2022-11-30 09:41:35 +00:00
|
|
|
rm, err := buildRawSyncSettingMessage(pb, chatID)
|
|
|
|
return rm, pb, err
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func preferredNameProtobufFactory(value interface{}, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
v, err := assertString(value)
|
|
|
|
if err != nil {
|
2022-11-30 09:41:35 +00:00
|
|
|
return nil, nil, err
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return buildRawPreferredNameSyncMessage(v, clock, chatID)
|
|
|
|
}
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func preferredNameProtobufFactoryStruct(s Settings, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
var pn string
|
|
|
|
if s.PreferredName != nil {
|
|
|
|
pn = *s.PreferredName
|
|
|
|
}
|
|
|
|
|
|
|
|
return buildRawPreferredNameSyncMessage(pn, clock, chatID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PreviewPrivacy
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func buildRawPreviewPrivacySyncMessage(v bool, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
pb := &protobuf.SyncSetting{
|
|
|
|
Type: protobuf.SyncSetting_PREVIEW_PRIVACY,
|
|
|
|
Value: &protobuf.SyncSetting_ValueBool{ValueBool: v},
|
|
|
|
Clock: clock,
|
|
|
|
}
|
2022-11-30 09:41:35 +00:00
|
|
|
rm, err := buildRawSyncSettingMessage(pb, chatID)
|
|
|
|
return rm, pb, err
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func previewPrivacyProtobufFactory(value interface{}, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
v, err := assertBool(value)
|
|
|
|
if err != nil {
|
2022-11-30 09:41:35 +00:00
|
|
|
return nil, nil, err
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return buildRawPreviewPrivacySyncMessage(v, clock, chatID)
|
|
|
|
}
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func previewPrivacyProtobufFactoryStruct(s Settings, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
return buildRawPreviewPrivacySyncMessage(s.PreviewPrivacy, clock, chatID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProfilePicturesShowTo
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func buildRawProfilePicturesShowToSyncMessage(v int64, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
pb := &protobuf.SyncSetting{
|
|
|
|
Type: protobuf.SyncSetting_PROFILE_PICTURES_SHOW_TO,
|
|
|
|
Value: &protobuf.SyncSetting_ValueInt64{ValueInt64: v},
|
|
|
|
Clock: clock,
|
|
|
|
}
|
2022-11-30 09:41:35 +00:00
|
|
|
rm, err := buildRawSyncSettingMessage(pb, chatID)
|
|
|
|
return rm, pb, err
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func profilePicturesShowToProtobufFactory(value interface{}, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
v, err := parseNumberToInt64(value)
|
|
|
|
if err != nil {
|
2022-11-30 09:41:35 +00:00
|
|
|
return nil, nil, err
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return buildRawProfilePicturesShowToSyncMessage(v, clock, chatID)
|
|
|
|
}
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func profilePicturesShowToProtobufFactoryStruct(s Settings, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
return buildRawProfilePicturesShowToSyncMessage(int64(s.ProfilePicturesShowTo), clock, chatID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProfilePicturesVisibility
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func buildRawProfilePicturesVisibilitySyncMessage(v int64, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
pb := &protobuf.SyncSetting{
|
|
|
|
Type: protobuf.SyncSetting_PROFILE_PICTURES_VISIBILITY,
|
|
|
|
Value: &protobuf.SyncSetting_ValueInt64{ValueInt64: v},
|
|
|
|
Clock: clock,
|
|
|
|
}
|
2022-11-30 09:41:35 +00:00
|
|
|
rm, err := buildRawSyncSettingMessage(pb, chatID)
|
|
|
|
return rm, pb, err
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func profilePicturesVisibilityProtobufFactory(value interface{}, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
v, err := parseNumberToInt64(value)
|
|
|
|
if err != nil {
|
2022-11-30 09:41:35 +00:00
|
|
|
return nil, nil, err
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return buildRawProfilePicturesVisibilitySyncMessage(v, clock, chatID)
|
|
|
|
}
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func profilePicturesVisibilityProtobufFactoryStruct(s Settings, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
return buildRawProfilePicturesVisibilitySyncMessage(int64(s.ProfilePicturesVisibility), clock, chatID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendStatusUpdates
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func buildRawSendStatusUpdatesSyncMessage(v bool, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
pb := &protobuf.SyncSetting{
|
|
|
|
Type: protobuf.SyncSetting_SEND_STATUS_UPDATES,
|
|
|
|
Value: &protobuf.SyncSetting_ValueBool{ValueBool: v},
|
|
|
|
Clock: clock,
|
|
|
|
}
|
2022-11-30 09:41:35 +00:00
|
|
|
rm, err := buildRawSyncSettingMessage(pb, chatID)
|
|
|
|
return rm, pb, err
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func sendStatusUpdatesProtobufFactory(value interface{}, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
v, err := assertBool(value)
|
|
|
|
if err != nil {
|
2022-11-30 09:41:35 +00:00
|
|
|
return nil, nil, err
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return buildRawSendStatusUpdatesSyncMessage(v, clock, chatID)
|
|
|
|
}
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func sendStatusUpdatesProtobufFactoryStruct(s Settings, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
return buildRawSendStatusUpdatesSyncMessage(s.SendStatusUpdates, clock, chatID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StickerPacksInstalled
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func buildRawStickerPacksInstalledSyncMessage(v []byte, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
pb := &protobuf.SyncSetting{
|
|
|
|
Type: protobuf.SyncSetting_STICKERS_PACKS_INSTALLED,
|
|
|
|
Value: &protobuf.SyncSetting_ValueBytes{ValueBytes: v},
|
|
|
|
Clock: clock,
|
|
|
|
}
|
2022-11-30 09:41:35 +00:00
|
|
|
rm, err := buildRawSyncSettingMessage(pb, chatID)
|
|
|
|
return rm, pb, err
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func stickersPacksInstalledProtobufFactory(value interface{}, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
v, err := parseJSONBlobData(value)
|
|
|
|
if err != nil {
|
2022-11-30 09:41:35 +00:00
|
|
|
return nil, nil, err
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return buildRawStickerPacksInstalledSyncMessage(v, clock, chatID)
|
|
|
|
}
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func stickersPacksInstalledProtobufFactoryStruct(s Settings, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
spi := extractJSONRawMessage(s.StickerPacksInstalled)
|
|
|
|
return buildRawStickerPacksInstalledSyncMessage(spi, clock, chatID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StickerPacksPending
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func buildRawStickerPacksPendingSyncMessage(v []byte, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
pb := &protobuf.SyncSetting{
|
|
|
|
Type: protobuf.SyncSetting_STICKERS_PACKS_PENDING,
|
|
|
|
Value: &protobuf.SyncSetting_ValueBytes{ValueBytes: v},
|
|
|
|
Clock: clock,
|
|
|
|
}
|
2022-11-30 09:41:35 +00:00
|
|
|
rm, err := buildRawSyncSettingMessage(pb, chatID)
|
|
|
|
return rm, pb, err
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func stickersPacksPendingProtobufFactory(value interface{}, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
v, err := parseJSONBlobData(value)
|
|
|
|
if err != nil {
|
2022-11-30 09:41:35 +00:00
|
|
|
return nil, nil, err
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return buildRawStickerPacksPendingSyncMessage(v, clock, chatID)
|
|
|
|
}
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func stickersPacksPendingProtobufFactoryStruct(s Settings, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
spp := extractJSONRawMessage(s.StickerPacksPending)
|
|
|
|
return buildRawStickerPacksPendingSyncMessage(spp, clock, chatID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StickersRecentStickers
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func buildRawStickersRecentStickersSyncMessage(v []byte, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
pb := &protobuf.SyncSetting{
|
|
|
|
Type: protobuf.SyncSetting_STICKERS_RECENT_STICKERS,
|
|
|
|
Value: &protobuf.SyncSetting_ValueBytes{ValueBytes: v},
|
|
|
|
Clock: clock,
|
|
|
|
}
|
2022-11-30 09:41:35 +00:00
|
|
|
rm, err := buildRawSyncSettingMessage(pb, chatID)
|
|
|
|
return rm, pb, err
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func stickersRecentStickersProtobufFactory(value interface{}, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
v, err := parseJSONBlobData(value)
|
|
|
|
if err != nil {
|
2022-11-30 09:41:35 +00:00
|
|
|
return nil, nil, err
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return buildRawStickersRecentStickersSyncMessage(v, clock, chatID)
|
|
|
|
}
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func stickersRecentStickersProtobufFactoryStruct(s Settings, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
srs := extractJSONRawMessage(s.StickersRecentStickers)
|
|
|
|
return buildRawStickersRecentStickersSyncMessage(srs, clock, chatID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertBytes(value interface{}) ([]byte, error) {
|
|
|
|
v, ok := value.([]byte)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Wrapf(ErrTypeAssertionFailed, "expected '[]byte', received %T", value)
|
|
|
|
}
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertBool(value interface{}) (bool, error) {
|
|
|
|
v, ok := value.(bool)
|
|
|
|
if !ok {
|
|
|
|
return false, errors.Wrapf(ErrTypeAssertionFailed, "expected 'bool', received %T", value)
|
|
|
|
}
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertString(value interface{}) (string, error) {
|
|
|
|
rv := reflect.ValueOf(value)
|
|
|
|
if rv.Kind() == reflect.Ptr {
|
|
|
|
value = *value.(*string)
|
|
|
|
}
|
|
|
|
v, ok := value.(string)
|
|
|
|
if !ok {
|
|
|
|
return "", errors.Wrapf(ErrTypeAssertionFailed, "expected 'string', received %T", value)
|
|
|
|
}
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseJSONBlobData(value interface{}) ([]byte, error) {
|
|
|
|
switch v := value.(type) {
|
|
|
|
case []byte:
|
|
|
|
return v, nil
|
|
|
|
case *sqlite.JSONBlob:
|
|
|
|
return extractJSONBlob(v)
|
|
|
|
default:
|
|
|
|
return nil, errors.Wrapf(ErrTypeAssertionFailed, "expected []byte or *sqlite.JSONBlob, received %T", value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseNumberToInt64(value interface{}) (int64, error) {
|
|
|
|
switch v := value.(type) {
|
|
|
|
case float32:
|
|
|
|
return int64(v), nil
|
|
|
|
case float64:
|
|
|
|
return int64(v), nil
|
|
|
|
case int:
|
|
|
|
return int64(v), nil
|
|
|
|
case int8:
|
|
|
|
return int64(v), nil
|
|
|
|
case int16:
|
|
|
|
return int64(v), nil
|
|
|
|
case int32:
|
|
|
|
return int64(v), nil
|
|
|
|
case int64:
|
|
|
|
return v, nil
|
|
|
|
case uint:
|
|
|
|
return int64(v), nil
|
|
|
|
case uint8:
|
|
|
|
return int64(v), nil
|
|
|
|
case uint16:
|
|
|
|
return int64(v), nil
|
|
|
|
case uint32:
|
|
|
|
return int64(v), nil
|
|
|
|
case uint64:
|
|
|
|
return int64(v), nil
|
|
|
|
case ProfilePicturesShowToType:
|
|
|
|
return int64(v), nil
|
|
|
|
case ProfilePicturesVisibilityType:
|
|
|
|
return int64(v), nil
|
|
|
|
default:
|
|
|
|
return 0, errors.Wrapf(ErrTypeAssertionFailed, "expected a numeric type, received %T", value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractJSONBlob(jb *sqlite.JSONBlob) ([]byte, error) {
|
|
|
|
value, err := jb.Value()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-16 05:04:53 +00:00
|
|
|
if value == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
return value.([]byte), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractJSONRawMessage(jrm *json.RawMessage) []byte {
|
|
|
|
if jrm == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
out, _ := jrm.MarshalJSON() // Don't need to parse error because it is always nil
|
|
|
|
if len(out) == 0 || bytes.Equal(out, []byte("null")) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
2022-11-30 09:41:35 +00:00
|
|
|
|
|
|
|
// DisplayName
|
|
|
|
|
|
|
|
func buildRawDisplayNameSyncMessage(v string, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
|
|
|
pb := &protobuf.SyncSetting{
|
|
|
|
Type: protobuf.SyncSetting_DISPLAY_NAME,
|
|
|
|
Value: &protobuf.SyncSetting_ValueString{ValueString: v},
|
|
|
|
Clock: clock,
|
|
|
|
}
|
|
|
|
rm, err := buildRawSyncSettingMessage(pb, chatID)
|
|
|
|
return rm, pb, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func displayNameProtobufFactory(value interface{}, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
|
|
|
v, err := assertString(value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buildRawDisplayNameSyncMessage(v, clock, chatID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func displayNameProtobufFactoryStruct(s Settings, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
|
|
|
|
|
|
|
return buildRawDisplayNameSyncMessage(s.DisplayName, clock, chatID)
|
|
|
|
}
|
2023-04-13 00:45:25 +00:00
|
|
|
|
|
|
|
// Bio
|
|
|
|
|
|
|
|
func buildRawBioSyncMessage(v string, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
|
|
|
pb := &protobuf.SyncSetting{
|
|
|
|
Type: protobuf.SyncSetting_BIO,
|
|
|
|
Value: &protobuf.SyncSetting_ValueString{ValueString: v},
|
|
|
|
Clock: clock,
|
|
|
|
}
|
|
|
|
rm, err := buildRawSyncSettingMessage(pb, chatID)
|
|
|
|
return rm, pb, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func bioProtobufFactory(value interface{}, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
|
|
|
v, err := assertString(value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buildRawBioSyncMessage(v, clock, chatID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func bioProtobufFactoryStruct(s Settings, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
|
|
|
return buildRawBioSyncMessage(s.Bio, clock, chatID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MnemonicRemoved
|
|
|
|
|
|
|
|
func buildRawMnemonicRemovedSyncMessage(v bool, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
|
|
|
pb := &protobuf.SyncSetting{
|
|
|
|
Type: protobuf.SyncSetting_MNEMONIC_REMOVED,
|
|
|
|
Value: &protobuf.SyncSetting_ValueBool{ValueBool: v},
|
|
|
|
Clock: clock,
|
|
|
|
}
|
|
|
|
rm, err := buildRawSyncSettingMessage(pb, chatID)
|
|
|
|
return rm, pb, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func mnemonicRemovedProtobufFactory(value interface{}, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
|
|
|
v, err := assertBool(value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buildRawMnemonicRemovedSyncMessage(v, clock, chatID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func mnemonicRemovedProtobufFactoryStruct(s Settings, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
|
|
|
return buildRawMnemonicRemovedSyncMessage(s.MnemonicRemoved, clock, chatID)
|
|
|
|
}
|
2023-04-26 15:37:18 +00:00
|
|
|
|
|
|
|
// Usernames
|
|
|
|
|
|
|
|
func buildRawUsernamesSyncMessage(v []byte, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
|
|
|
pb := &protobuf.SyncSetting{
|
|
|
|
Type: protobuf.SyncSetting_ENS_USERNAMES,
|
|
|
|
Value: &protobuf.SyncSetting_ValueBytes{ValueBytes: v},
|
|
|
|
Clock: clock,
|
|
|
|
}
|
|
|
|
rm, err := buildRawSyncSettingMessage(pb, chatID)
|
|
|
|
return rm, pb, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func usernamesProtobufFactory(value interface{}, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
|
|
|
v, err := assertBytes(value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buildRawUsernamesSyncMessage(v, clock, chatID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func usernamesProtobufFactoryStruct(s Settings, clock uint64, chatID string) (*common.RawMessage, *protobuf.SyncSetting, error) {
|
|
|
|
us := extractJSONRawMessage(s.Usernames)
|
|
|
|
return buildRawUsernamesSyncMessage(us, clock, chatID)
|
|
|
|
}
|