Skip to content

Commit

Permalink
fix: duo picture dont have regular format (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
42atomys authored Jul 18, 2022
1 parent fa8d7c3 commit 9ff99e6
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 40 deletions.
30 changes: 22 additions & 8 deletions api/graphs/api.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type User {
email: String!
duoLogin: String!
duoID: Int!
duoAvatarURL: String
firstName: String!
usualFirstName: String
lastName: String!
Expand Down Expand Up @@ -115,23 +116,35 @@ type Query {
user(id: UUID!): User @authenticated
location(id: UUID!): Location @authenticated

locations(page: PageInput!, campusID: UUID!): LocationConnection! @authenticated
locationsByCampusName(page: PageInput!, campusName: String!): LocationConnection! @authenticated
locationsByCluster(page: PageInput!, campusName: String!, identifierPrefix: String): LocationConnection! @authenticated
locations(page: PageInput!, campusID: UUID!): LocationConnection!
@authenticated
locationsByCampusName(
page: PageInput!
campusName: String!
): LocationConnection! @authenticated
locationsByCluster(
page: PageInput!
campusName: String!
identifierPrefix: String
): LocationConnection! @authenticated

myFollowing: [User!]! @authenticated

internalGetUserByAccount(provider: PROVIDER!, uid: String!): User! @authzByPolicy(policy: SERVICE_TOKEN)
internalGetUserByEmail(email: String!): User! @authzByPolicy(policy: SERVICE_TOKEN)
internalGetUserByAccount(provider: PROVIDER!, uid: String!): User!
@authzByPolicy(policy: SERVICE_TOKEN)
internalGetUserByEmail(email: String!): User!
@authzByPolicy(policy: SERVICE_TOKEN)
internalGetUser(id: UUID!): User! @authzByPolicy(policy: SERVICE_TOKEN)
}

type Mutation {
createFriendship(userID: UUID!): Boolean! @authenticated
deleteFriendship(userID: UUID!): Boolean! @authenticated

internalCreateUser(input: CreateUserInput!): UUID! @authzByPolicy(policy: SERVICE_TOKEN)
internalLinkAccount(input: LinkAccountInput!): Account! @authzByPolicy(policy: SERVICE_TOKEN)
internalCreateUser(input: CreateUserInput!): UUID!
@authzByPolicy(policy: SERVICE_TOKEN)
internalLinkAccount(input: LinkAccountInput!): Account!
@authzByPolicy(policy: SERVICE_TOKEN)
inviteOnDiscord: Boolean! @authenticated
}

Expand All @@ -151,6 +164,7 @@ input CreateUserInput {
lastName: String!
poolYear: String
poolMonth: String
imageURL: String
phone: String
isStaff: Boolean!
}
Expand All @@ -176,4 +190,4 @@ enum PROVIDER {

enum ACCOUNT_TYPE {
OAUTH
}
}
1 change: 1 addition & 0 deletions internal/api/api.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (r *mutationResolver) InternalCreateUser(ctx context.Context, input typesge
SetNillablePoolYear(input.PoolYear).
SetNillablePoolMonth(input.PoolMonth).
SetNillablePhone(input.Phone).
SetNillableDuoAvatarURL(input.ImageURL).
SetIsStaff(input.IsStaff).
SetIsAUser(true).
OnConflictColumns(user.FieldDuoID).
Expand Down
1 change: 1 addition & 0 deletions internal/models/schema/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func (User) Fields() []ent.Field {
field.String("pool_year").Optional().Nillable(),
field.String("pool_month").Optional().Nillable(),
field.String("nickname").Optional().Nillable().Unique().MaxLen(255),
field.String("duo_avatar_url").Optional().Nillable().MaxLen(255),
field.String("avatar_url").Optional().Nillable().MaxLen(255),
field.String("cover_url").Optional().Nillable().MaxLen(255),
field.UUID("current_location_id", uuid.UUID{}).Nillable().Optional(),
Expand Down
26 changes: 14 additions & 12 deletions internal/models/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,25 @@ import (
func WithTx(ctx context.Context, client *modelgen.Client, fn func(tx *modelgen.Tx) error) error {
tx, err := client.Tx(ctx)
if err != nil {
return err
return err
}
defer func() {
if v := recover(); v != nil {
if err := tx.Rollback(); err != nil {
log.Error().Err(err).Msg("failed to rollback transaction")
sentry.CaptureException(err)
}
sentry.CaptureException(v.(error))
if v := recover(); v != nil {
if err := tx.Rollback(); err != nil {
log.Error().Err(err).Msg("failed to rollback transaction")
sentry.CaptureException(err)
}
sentry.CaptureException(v.(error))
}
}()
if err := fn(tx); err != nil {
if rerr := tx.Rollback(); rerr != nil {
err = fmt.Errorf("rolling back transaction: %w", rerr)
}
return err
if rerr := tx.Rollback(); rerr != nil {
err = fmt.Errorf("rolling back transaction: %w", rerr)
}
return err
}
if err := tx.Commit(); err != nil {
return fmt.Errorf("committing transaction: %w", err)
return fmt.Errorf("committing transaction: %w", err)
}
return nil
}
Expand All @@ -57,6 +57,7 @@ func UserFirstOrCreateFromComplexLocation(ctx context.Context, l *duoapi.Locatio
SetPhone(l.User.Phone).
SetPoolMonth(l.User.PoolMonth).
SetPoolYear(l.User.PoolYear).
SetDuoAvatarURL(l.User.ImageURL).
SetIsStaff(l.User.Staff).
SetIsAUser(false).
OnConflictColumns(user.FieldDuoID).
Expand Down Expand Up @@ -97,6 +98,7 @@ func UserFirstOrCreateFromLocation(ctx context.Context, l *duoapi.Location[duoap
SetPhone(duoUser.Phone).
SetPoolMonth(duoUser.PoolMonth).
SetPoolYear(duoUser.PoolYear).
SetDuoAvatarURL(duoUser.ImageURL).
SetIsStaff(duoUser.Staff).
SetIsAUser(false).
OnConflictColumns(user.FieldDuoID).
Expand Down
2 changes: 2 additions & 0 deletions internal/webhooks/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func (p *userProcessor) Create(u *duoapi.User, metadata *duoapi.WebhookMetadata)
SetDuoLogin(u.Login).
SetFirstName(u.FirstName).
SetLastName(u.LastName).
SetDuoAvatarURL(u.ImageURL).
SetEmail(u.Email).
SetIsStaff(u.Staff).
SetNillableUsualFirstName(&u.UsualFirstName).
Expand All @@ -36,6 +37,7 @@ func (p *userProcessor) Update(u *duoapi.User, metadata *duoapi.WebhookMetadata)
SetPhone(u.Phone).
SetPoolYear(u.PoolYear).
SetPoolMonth(u.PoolMonth).
SetDuoAvatarURL(u.ImageURL).
SetNillableUsualFirstName(&u.UsualFirstName).
Where(user.DuoID(u.ID)).
Exec(p.ctx)
Expand Down
26 changes: 14 additions & 12 deletions pkg/duoapi/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ type Language struct {
}

type Location[UserType ILocationUser] struct {
ID int `json:"id"`
BeginAt DuoTime `json:"begin_at"`
EndAt *DuoTime `json:"end_at"`
Primary bool `json:"primary"`
Host string `json:"host"`
CampusID int `json:"campus_id"`
ID int `json:"id"`
BeginAt DuoTime `json:"begin_at"`
EndAt *DuoTime `json:"end_at"`
Primary bool `json:"primary"`
Host string `json:"host"`
CampusID int `json:"campus_id"`
User UserType `json:"user"`
}

Expand All @@ -49,9 +49,9 @@ type LocationUser struct {
}

type ComplexLocationUser struct {
ID int `json:"id"`
Login string `json:"login"`
URL string `json:"url"`
ID int `json:"id"`
Login string `json:"login"`
URL string `json:"url"`
Email string `json:"email"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Expand Down Expand Up @@ -82,9 +82,11 @@ type User struct {
LastName string `json:"last_name"`
UsualFullName string `json:"usual_full_name"`
UsualFirstName string `json:"usual_first_name"`
ImageURL string `json:"image_url"`
NewImageURL string `json:"new_image_url"`
URL string `json:"url"`
Staff bool `json:"staff?"`
Phone string `json:"phone"`
PoolMonth string `json:"pool_month"`
PoolYear string `json:"pool_year"`
Phone string `json:"phone"`
PoolMonth string `json:"pool_month"`
PoolYear string `json:"pool_year"`
}
5 changes: 4 additions & 1 deletion web/ui/src/components/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ const sizeClasses = {

export const Avatar = ({
login,
duoAvatarURL,
size = 'sm',
rounded = false,
className,
}: AvatarProps) => {
return (
<div
style={{ backgroundImage: `url(${CDN_URL.replace('{}', login)})` }}
style={{
backgroundImage: `url(${duoAvatarURL || CDN_URL.replace('{}', login)})`,
}}
className={classNames(
className,
rounded ? 'rounded-full' : 'rounded',
Expand Down
1 change: 1 addition & 0 deletions web/ui/src/components/Avatar/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ type AvatarSize = 'xs' | 'sm' | 'md' | 'xl' | 'xxl' | 'xxxl';

export type AvatarProps = {
login: string;
duoAvatarURL?: string | null;
size?: AvatarSize;
rounded?: boolean;
className?: string;
Expand Down
7 changes: 6 additions & 1 deletion web/ui/src/components/ClusterMap/ClusterMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ export const ClusterWorkspaceWithUser = ({
onMouseLeave={(e) => onMouseLeave && onMouseLeave(e, location)}
>
<span className="mb-1">
<Avatar login={location.user.duoLogin} rounded={false} size="md" />
<Avatar
login={location.user.duoLogin}
duoAvatarURL={location.user.duoAvatarURL}
rounded={false}
size="md"
/>
</span>
<span className="text-xs">{displayText || location.identifier}</span>
</div>
Expand Down
1 change: 1 addition & 0 deletions web/ui/src/components/UserCard/UserCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const UserCard: UserCardComponent = ({
>
<Avatar
login={user.duoLogin}
duoAvatarURL={user.duoAvatarURL}
size="xxxl"
rounded
className="mb-4 bg-slate-800"
Expand Down
20 changes: 14 additions & 6 deletions web/ui/src/graphql/definitions.gql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
#
# !!!! WARNING: NEVER IMPORT THIS MODULE DIRECTLY !!!!
#
#
# This file is used to define the documents (queries and mutations) that are
# available in the GraphQL interface.
# To use this module, you must generate the schema using the `generate` script
Expand Down Expand Up @@ -138,8 +138,12 @@ query myFollowings {
}
}

query clusterView($campusName: String!, $identifierPrefix: String!) {
locationsByCluster(campusName: $campusName, identifierPrefix: $identifierPrefix, page:{ first: 1000 }) {
query clusterView($campusName: String!, $identifierPrefix: String!) {
locationsByCluster(
campusName: $campusName
identifierPrefix: $identifierPrefix
page: { first: 1000 }
) {
totalCount
pageInfo {
hasNextPage
Expand All @@ -157,6 +161,7 @@ query clusterView($campusName: String!, $identifierPrefix: String!) {
firstName
usualFirstName
lastName
duoAvatarURL
isFollowing
}
}
Expand All @@ -176,7 +181,10 @@ query internalGetUserByEmail($email: String!) {
}
}

query internalGetUserByAccount($provider: PROVIDER!, $providerAccountId: String!) {
query internalGetUserByAccount(
$provider: PROVIDER!
$providerAccountId: String!
) {
user: internalGetUserByAccount(provider: $provider, uid: $providerAccountId) {
...UserAccountsData
}
Expand All @@ -202,4 +210,4 @@ fragment LocationBasicData on Location {
identifier
beginAt
endAt
}
}

0 comments on commit 9ff99e6

Please sign in to comment.