|
| 1 | +package postgres |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/georgysavva/scany/v2/pgxscan" |
| 8 | + "github.com/jackc/pgx/v5/pgxpool" |
| 9 | + |
| 10 | + commonpb "github.com/code-payments/flipchat-protobuf-api/generated/go/common/v1" |
| 11 | + profilepb "github.com/code-payments/flipchat-protobuf-api/generated/go/profile/v1" |
| 12 | + pg "github.com/code-payments/flipchat-server/database/postgres" |
| 13 | + |
| 14 | + "github.com/code-payments/code-server/pkg/pointer" |
| 15 | + "github.com/code-payments/flipchat-server/profile" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + usersTableName = "flipchat_users" |
| 20 | + allUserFields = `"id", "displayName", "isStaff", "isRegistered", "nextAirdropAt", "elibigibleForAirdropsUntil", "createdAt", "updatedAt"` |
| 21 | + |
| 22 | + xUsersTableName = "flipchat_x_users" |
| 23 | + allXUserFields = `"id", "username", "name", "description", "profilePicUrl", "followerCount", "verifiedType", "accessToken", "userId", "createdAt", "updatedAt"` |
| 24 | +) |
| 25 | + |
| 26 | +type xUserModel struct { |
| 27 | + ID string `db:"id"` |
| 28 | + Username string `db:"username"` |
| 29 | + Name *string `db:"name"` |
| 30 | + Description *string `db:"description"` |
| 31 | + ProfilePicUrl string `db:"profilePicUrl"` |
| 32 | + FollowerCount int `db:"followerCount"` |
| 33 | + VerifiedType int `db:"verifiedType"` |
| 34 | + AccessToken string `db:"accessToken"` |
| 35 | + UserID string `db:"userId"` |
| 36 | + CreatedAt time.Time `db:"createdAt"` |
| 37 | + UpdatedAt time.Time `db:"updatedAt"` |
| 38 | +} |
| 39 | + |
| 40 | +func toXUserModel(userID *commonpb.UserId, profile *profilepb.XProfile, accessToken string) (*xUserModel, error) { |
| 41 | + return &xUserModel{ |
| 42 | + ID: profile.Id, |
| 43 | + Username: profile.Username, |
| 44 | + Name: pointer.StringIfValid(len(profile.Name) > 0, profile.Name), |
| 45 | + Description: pointer.StringIfValid(len(profile.Description) > 0, profile.Description), |
| 46 | + ProfilePicUrl: profile.ProfilePicUrl, |
| 47 | + FollowerCount: int(profile.FollowerCount), |
| 48 | + VerifiedType: int(profile.VerifiedType), |
| 49 | + AccessToken: accessToken, |
| 50 | + UserID: pg.Encode(userID.Value), |
| 51 | + }, nil |
| 52 | +} |
| 53 | + |
| 54 | +func fromXUserModel(m *xUserModel) (*profilepb.XProfile, error) { |
| 55 | + return &profilepb.XProfile{ |
| 56 | + Id: m.ID, |
| 57 | + Username: m.Username, |
| 58 | + Name: *pointer.StringOrDefault(m.Name, ""), |
| 59 | + Description: *pointer.StringOrDefault(m.Description, ""), |
| 60 | + ProfilePicUrl: m.ProfilePicUrl, |
| 61 | + VerifiedType: profilepb.XProfile_VerifiedType(m.VerifiedType), |
| 62 | + FollowerCount: uint32(m.FollowerCount), |
| 63 | + }, nil |
| 64 | +} |
| 65 | + |
| 66 | +func dbGetDisplayName(ctx context.Context, pool *pgxpool.Pool, userID *commonpb.UserId) (*string, error) { |
| 67 | + var res *string |
| 68 | + query := `SELECT "displayName" FROM ` + usersTableName + ` WHERE "id" = $1` |
| 69 | + err := pgxscan.Get( |
| 70 | + ctx, |
| 71 | + pool, |
| 72 | + &res, |
| 73 | + query, |
| 74 | + pg.Encode(userID.Value), |
| 75 | + ) |
| 76 | + if err != nil { |
| 77 | + if pgxscan.NotFound(err) { |
| 78 | + return nil, profile.ErrNotFound |
| 79 | + } |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + return res, nil |
| 83 | +} |
| 84 | + |
| 85 | +func dbSetDisplayName(ctx context.Context, pool *pgxpool.Pool, userID *commonpb.UserId, displayName string) error { |
| 86 | + query := `INSERT INTO ` + usersTableName + ` (` + allUserFields + `) VALUES ($1, $2, false, false, NOW(), NOW(), NOW(), NOW()) ON CONFLICT ("id") DO UPDATE SET "displayName" = $2 WHERE ` + usersTableName + `."id" = $1` |
| 87 | + _, err := pool.Exec(ctx, query, pg.Encode(userID.Value), displayName) |
| 88 | + return err |
| 89 | +} |
| 90 | + |
| 91 | +func (m *xUserModel) dbUpsert(ctx context.Context, pool *pgxpool.Pool) error { |
| 92 | + query := `INSERT INTO ` + xUsersTableName + ` (` + allXUserFields + `) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, NOW(), NOW()) |
| 93 | + ON CONFLICT ("id") DO UPDATE |
| 94 | + SET "username" = $2, "name" = $3, "description" = $4, "profilePicUrl" = $5, "followerCount" = $6, "verifiedType" = $7, "accessToken" = $8, "userId" = $9, "updatedAt" = NOW() |
| 95 | + WHERE ` + xUsersTableName + `."id" = $1 |
| 96 | + RETURNING ` + allXUserFields |
| 97 | + err := pgxscan.Get( |
| 98 | + ctx, |
| 99 | + pool, |
| 100 | + m, |
| 101 | + query, |
| 102 | + m.ID, |
| 103 | + m.Username, |
| 104 | + m.Name, |
| 105 | + m.Description, |
| 106 | + m.ProfilePicUrl, |
| 107 | + m.FollowerCount, |
| 108 | + m.VerifiedType, |
| 109 | + m.AccessToken, |
| 110 | + m.UserID, |
| 111 | + ) |
| 112 | + return err |
| 113 | +} |
| 114 | + |
| 115 | +func dbUnlinkXAccount(ctx context.Context, pool *pgxpool.Pool, userID *commonpb.UserId, xUserID string) error { |
| 116 | + query := `DELETE FROM ` + xUsersTableName + ` WHERE "id" = $1 AND "userId" = $2` |
| 117 | + res, err := pool.Exec(ctx, query, xUserID, pg.Encode(userID.Value)) |
| 118 | + if err != nil { |
| 119 | + return err |
| 120 | + } |
| 121 | + if res.RowsAffected() == 0 { |
| 122 | + return profile.ErrNotFound |
| 123 | + } |
| 124 | + return nil |
| 125 | +} |
| 126 | + |
| 127 | +func dbGetXUser(ctx context.Context, pool *pgxpool.Pool, userID *commonpb.UserId) (*xUserModel, error) { |
| 128 | + res := &xUserModel{} |
| 129 | + query := `SELECT ` + allXUserFields + ` FROM ` + xUsersTableName + ` WHERE "userId" = $1` |
| 130 | + err := pgxscan.Get( |
| 131 | + ctx, |
| 132 | + pool, |
| 133 | + res, |
| 134 | + query, |
| 135 | + pg.Encode(userID.Value), |
| 136 | + ) |
| 137 | + if err != nil { |
| 138 | + if pgxscan.NotFound(err) { |
| 139 | + return nil, profile.ErrNotFound |
| 140 | + } |
| 141 | + return nil, err |
| 142 | + } |
| 143 | + return res, nil |
| 144 | +} |
| 145 | + |
| 146 | +func dbGetUserLinkedToXAccount(ctx context.Context, pool *pgxpool.Pool, xUserID string) (*commonpb.UserId, error) { |
| 147 | + var encoded string |
| 148 | + query := `SELECT "userId" FROM ` + xUsersTableName + ` WHERE "id" = $1` |
| 149 | + err := pgxscan.Get( |
| 150 | + ctx, |
| 151 | + pool, |
| 152 | + &encoded, |
| 153 | + query, |
| 154 | + xUserID, |
| 155 | + ) |
| 156 | + if err != nil { |
| 157 | + if pgxscan.NotFound(err) { |
| 158 | + return nil, profile.ErrNotFound |
| 159 | + } |
| 160 | + return nil, err |
| 161 | + } |
| 162 | + decoded, err := pg.Decode(encoded) |
| 163 | + if err != nil { |
| 164 | + return nil, err |
| 165 | + } |
| 166 | + return &commonpb.UserId{Value: decoded}, nil |
| 167 | +} |
0 commit comments