mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-12-24 18:51:50 +00:00
Separate User and PhotoProfile classes into their own files
Signed-off-by: David Mehren <dmehren1@gmail.com>
This commit is contained in:
parent
5852b45bdd
commit
20fbb39b3e
5 changed files with 105 additions and 104 deletions
|
@ -13,109 +13,8 @@ import {
|
|||
Table,
|
||||
Unique
|
||||
} from 'sequelize-typescript'
|
||||
import { generateAvatarURL } from '../letter-avatars'
|
||||
import { logger } from '../logger'
|
||||
import { PassportProfile, ProviderEnum } from '../web/auth/utils'
|
||||
import { Note } from './note'
|
||||
|
||||
export class PhotoProfile {
|
||||
name: string
|
||||
photo: string
|
||||
biggerphoto: string
|
||||
|
||||
static fromUser (user: User): PhotoProfile | null {
|
||||
if (!user) return null
|
||||
if (user.profile) return PhotoProfile.fromJSON(user.profile)
|
||||
if (user.email) return PhotoProfile.fromEmail(user.email)
|
||||
return null
|
||||
}
|
||||
|
||||
private static fromJSON (jsonProfile: string): PhotoProfile | null {
|
||||
try {
|
||||
const parsedProfile: PassportProfile = JSON.parse(jsonProfile)
|
||||
return {
|
||||
name: parsedProfile.displayName || parsedProfile.username,
|
||||
photo: PhotoProfile.generatePhotoURL(parsedProfile, false),
|
||||
biggerphoto: PhotoProfile.generatePhotoURL(parsedProfile, true)
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error(err)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
private static fromEmail (email: string): PhotoProfile {
|
||||
return {
|
||||
name: email.substring(0, email.lastIndexOf('@')),
|
||||
photo: generateAvatarURL('', email, false),
|
||||
biggerphoto: generateAvatarURL('', email, true)
|
||||
}
|
||||
}
|
||||
|
||||
private static generatePhotoURL (profile: PassportProfile, bigger: boolean): string {
|
||||
let photo: string
|
||||
switch (profile.provider) {
|
||||
case ProviderEnum.facebook:
|
||||
photo = 'https://graph.facebook.com/' + profile.id + '/picture'
|
||||
if (bigger) {
|
||||
photo += '?width=400'
|
||||
} else {
|
||||
photo += '?width=96'
|
||||
}
|
||||
break
|
||||
case ProviderEnum.twitter:
|
||||
photo = 'https://twitter.com/' + profile.username + '/profile_image'
|
||||
if (bigger) {
|
||||
photo += '?size=original'
|
||||
} else {
|
||||
photo += '?size=bigger'
|
||||
}
|
||||
break
|
||||
case ProviderEnum.github:
|
||||
photo = 'https://avatars.githubusercontent.com/u/' + profile.id
|
||||
if (bigger) {
|
||||
photo += '?s=400'
|
||||
} else {
|
||||
photo += '?s=96'
|
||||
}
|
||||
break
|
||||
case ProviderEnum.gitlab:
|
||||
photo = profile.avatarUrl
|
||||
if (photo) {
|
||||
if (bigger) {
|
||||
photo = photo.replace(/(\?s=)\d*$/i, '$1400')
|
||||
} else {
|
||||
photo = photo.replace(/(\?s=)\d*$/i, '$196')
|
||||
}
|
||||
} else {
|
||||
photo = generateAvatarURL(profile.username)
|
||||
}
|
||||
break
|
||||
case ProviderEnum.dropbox:
|
||||
photo = generateAvatarURL('', profile.emails[0], bigger)
|
||||
break
|
||||
case ProviderEnum.google:
|
||||
photo = profile.photos[0].value
|
||||
if (bigger) {
|
||||
photo = photo.replace(/(\?sz=)\d*$/i, '$1400')
|
||||
} else {
|
||||
photo = photo.replace(/(\?sz=)\d*$/i, '$196')
|
||||
}
|
||||
break
|
||||
case ProviderEnum.ldap:
|
||||
photo = generateAvatarURL(profile.username, profile.emails[0], bigger)
|
||||
break
|
||||
case ProviderEnum.saml:
|
||||
photo = generateAvatarURL(profile.username, profile.emails[0], bigger)
|
||||
break
|
||||
default:
|
||||
photo = generateAvatarURL(profile.username)
|
||||
break
|
||||
}
|
||||
return photo
|
||||
}
|
||||
}
|
||||
|
||||
@Table
|
||||
export class User extends Model<User> {
|
||||
@PrimaryKey
|
||||
|
|
|
@ -11,7 +11,7 @@ import { History } from './history'
|
|||
import { logger } from './logger'
|
||||
import { Author, Note, Revision, User } from './models'
|
||||
import { NoteAuthorship } from './models/note'
|
||||
import { PhotoProfile } from './models/user'
|
||||
import { PhotoProfile } from './utils/PhotoProfile'
|
||||
import { EditorSocketIOServer } from './ot/editor-socketio-server'
|
||||
import { mapToObject } from './utils'
|
||||
|
||||
|
|
102
src/lib/utils/PhotoProfile.ts
Normal file
102
src/lib/utils/PhotoProfile.ts
Normal file
|
@ -0,0 +1,102 @@
|
|||
import { generateAvatarURL } from '../letter-avatars'
|
||||
import { logger } from '../logger'
|
||||
import { PassportProfile, ProviderEnum } from '../web/auth/utils'
|
||||
import { User } from '../models'
|
||||
|
||||
export class PhotoProfile {
|
||||
name: string
|
||||
photo: string
|
||||
biggerphoto: string
|
||||
|
||||
static fromUser (user: User): PhotoProfile | null {
|
||||
if (!user) return null
|
||||
if (user.profile) return PhotoProfile.fromJSON(user.profile)
|
||||
if (user.email) return PhotoProfile.fromEmail(user.email)
|
||||
return null
|
||||
}
|
||||
|
||||
private static fromJSON (jsonProfile: string): PhotoProfile | null {
|
||||
try {
|
||||
const parsedProfile: PassportProfile = JSON.parse(jsonProfile)
|
||||
return {
|
||||
name: parsedProfile.displayName || parsedProfile.username,
|
||||
photo: PhotoProfile.generatePhotoURL(parsedProfile, false),
|
||||
biggerphoto: PhotoProfile.generatePhotoURL(parsedProfile, true)
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error(err)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
private static fromEmail (email: string): PhotoProfile {
|
||||
return {
|
||||
name: email.substring(0, email.lastIndexOf('@')),
|
||||
photo: generateAvatarURL('', email, false),
|
||||
biggerphoto: generateAvatarURL('', email, true)
|
||||
}
|
||||
}
|
||||
|
||||
private static generatePhotoURL (profile: PassportProfile, bigger: boolean): string {
|
||||
let photo: string
|
||||
switch (profile.provider) {
|
||||
case ProviderEnum.facebook:
|
||||
photo = 'https://graph.facebook.com/' + profile.id + '/picture'
|
||||
if (bigger) {
|
||||
photo += '?width=400'
|
||||
} else {
|
||||
photo += '?width=96'
|
||||
}
|
||||
break
|
||||
case ProviderEnum.twitter:
|
||||
photo = 'https://twitter.com/' + profile.username + '/profile_image'
|
||||
if (bigger) {
|
||||
photo += '?size=original'
|
||||
} else {
|
||||
photo += '?size=bigger'
|
||||
}
|
||||
break
|
||||
case ProviderEnum.github:
|
||||
photo = 'https://avatars.githubusercontent.com/u/' + profile.id
|
||||
if (bigger) {
|
||||
photo += '?s=400'
|
||||
} else {
|
||||
photo += '?s=96'
|
||||
}
|
||||
break
|
||||
case ProviderEnum.gitlab:
|
||||
photo = profile.avatarUrl
|
||||
if (photo) {
|
||||
if (bigger) {
|
||||
photo = photo.replace(/(\?s=)\d*$/i, '$1400')
|
||||
} else {
|
||||
photo = photo.replace(/(\?s=)\d*$/i, '$196')
|
||||
}
|
||||
} else {
|
||||
photo = generateAvatarURL(profile.username)
|
||||
}
|
||||
break
|
||||
case ProviderEnum.dropbox:
|
||||
photo = generateAvatarURL('', profile.emails[0], bigger)
|
||||
break
|
||||
case ProviderEnum.google:
|
||||
photo = profile.photos[0].value
|
||||
if (bigger) {
|
||||
photo = photo.replace(/(\?sz=)\d*$/i, '$1400')
|
||||
} else {
|
||||
photo = photo.replace(/(\?sz=)\d*$/i, '$196')
|
||||
}
|
||||
break
|
||||
case ProviderEnum.ldap:
|
||||
photo = generateAvatarURL(profile.username, profile.emails[0], bigger)
|
||||
break
|
||||
case ProviderEnum.saml:
|
||||
photo = generateAvatarURL(profile.username, profile.emails[0], bigger)
|
||||
break
|
||||
default:
|
||||
photo = generateAvatarURL(profile.username)
|
||||
break
|
||||
}
|
||||
return photo
|
||||
}
|
||||
}
|
|
@ -6,7 +6,7 @@ import { config } from '../../config'
|
|||
import { errors } from '../../errors'
|
||||
import { logger } from '../../logger'
|
||||
import { Note } from '../../models'
|
||||
import { PhotoProfile } from '../../models/user'
|
||||
import { PhotoProfile } from '../../utils/PhotoProfile'
|
||||
|
||||
export function newNote (req, res: Response, body: string | null): void {
|
||||
let owner = null
|
||||
|
|
|
@ -6,7 +6,7 @@ import { Note, User } from '../models'
|
|||
import { logger } from '../logger'
|
||||
import { generateAvatar } from '../letter-avatars'
|
||||
import { config } from '../config'
|
||||
import { PhotoProfile } from '../models/user'
|
||||
import { PhotoProfile } from '../utils/PhotoProfile'
|
||||
|
||||
const UserRouter = Router()
|
||||
|
||||
|
|
Loading…
Reference in a new issue