overleaf/services/web/public/coffee/main/affiliations/factories/UserAffiliationsDataService.coffee

138 lines
15 KiB
CoffeeScript
Raw Normal View History

define [
"base"
], (App) ->
countriesList = [{ code : "af", name: "Afghanistan" }, { code : "ax", name: "Åland Islands" }, { code : "al", name: "Albania" }, { code : "dz", name: "Algeria" }, { code : "as", name: "American Samoa" }, { code : "ad", name: "Andorra" }, { code : "ao", name: "Angola" }, { code : "ai", name: "Anguilla" }, { code : "aq", name: "Antarctica" }, { code : "ag", name: "Antigua and Barbuda" }, { code : "ar", name: "Argentina" }, { code : "am", name: "Armenia" }, { code : "aw", name: "Aruba" }, { code : "au", name: "Australia" }, { code : "at", name: "Austria" }, { code : "az", name: "Azerbaijan" }, { code : "bs", name: "Bahamas" }, { code : "bh", name: "Bahrain" }, { code : "bd", name: "Bangladesh" }, { code : "bb", name: "Barbados" }, { code : "by", name: "Belarus" }, { code : "be", name: "Belgium" }, { code : "bz", name: "Belize" }, { code : "bj", name: "Benin" }, { code : "bm", name: "Bermuda" }, { code : "bt", name: "Bhutan" }, { code : "bo", name: "Bolivia" }, { code : "bq", name: "Bonaire, Saint Eustatius and Saba" }, { code : "ba", name: "Bosnia and Herzegovina" }, { code : "bw", name: "Botswana" }, { code : "bv", name: "Bouvet Island" }, { code : "br", name: "Brazil" }, { code : "io", name: "British Indian Ocean Territory" }, { code : "vg", name: "British Virgin Islands" }, { code : "bn", name: "Brunei" }, { code : "bg", name: "Bulgaria" }, { code : "bf", name: "Burkina Faso" }, { code : "bi", name: "Burundi" }, { code : "kh", name: "Cambodia" }, { code : "cm", name: "Cameroon" }, { code : "ca", name: "Canada" }, { code : "cv", name: "Cabo Verde" }, { code : "ky", name: "Cayman Islands" }, { code : "cf", name: "Central African Republic" }, { code : "td", name: "Chad" }, { code : "cl", name: "Chile" }, { code : "cn", name: "China" }, { code : "cx", name: "Christmas Island" }, { code : "cc", name: "Cocos (Keeling) Islands" }, { code : "co", name: "Colombia" }, { code : "km", name: "Comoros" }, { code : "cg", name: "Congo" }, { code : "ck", name: "Cook Islands" }, { code : "cr", name: "Costa Rica" }, { code : "ci", name: "Côte d'Ivoire" }, { code : "hr", name: "Croatia" }, { code : "cu", name: "Cuba" }, { code : "cw", name: "Curaçao" }, { code : "cy", name: "Cyprus" }, { code : "cz", name: "Czech Republic" }, { code : "kp", name: "Democratic People's Republic of Korea" }, { code : "cd", name: "Democratic Republic of the Congo" }, { code : "dk", name: "Denmark" }, { code : "dj", name: "Djibouti" }, { code : "dm", name: "Dominica" }, { code : "do", name: "Dominican Republic" }, { code : "ec", name: "Ecuador" }, { code : "eg", name: "Egypt" }, { code : "sv", name: "El Salvador" }, { code : "gq", name: "Equatorial Guinea" }, { code : "er", name: "Eritrea" }, { code : "ee", name: "Estonia" }, { code : "et", name: "Ethiopia" }, { code : "fk", name: "Falkland Islands (Malvinas)" }, { code : "fo", name: "Faroe Islands" }, { code : "fj", name: "Fiji" }, { code : "fi", name: "Finland" }, { code : "fr", name: "France" }, { code : "gf", name: "French Guiana" }, { code : "pf", name: "French Polynesia" }, { code : "tf", name: "French Southern Territories" }, { code : "ga", name: "Gabon" }, { code : "gm", name: "Gambia" }, { code : "ge", name: "Georgia" }, { code : "de", name: "Germany" }, { code : "gh", name: "Ghana" }, { code : "gi", name: "Gibraltar" }, { code : "gr", name: "Greece" }, { code : "gl", name: "Greenland" }, { code : "gd", name: "Grenada" }, { code : "gp", name: "Guadeloupe" }, { code : "gu", name: "Guam" }, { code : "gt", name: "Guatemala" }, { code : "gg", name: "Guernsey" }, { code : "gn", name: "Guinea" }, { code : "gw", name: "Guinea-Bissau" }, { code : "gy", name: "Guyana" }, { code : "ht", name: "Haiti" }, { code : "hm", name: "Heard Island and McDonald Islands" }, { code : "va", name: "Holy See (Vatican City)" }, { code : "hn", name: "Honduras" }, { code : "hk", name: "Hong Kong" }, { code : "hu", name: "Hungary" }, { code : "is", name: "Iceland" }, { code : "in", name: "India" }, { code : "id", name: "Indonesia" }, { code : "ir", name: "Iran" }, { code : "iq", name: "Iraq" }, { code : "ie", name: "Ireland" },
universities = {}
universitiesByDomain = {}
defaultRoleHints = [ "Undergraduate Student", "Masters Student (MSc, MA, ...)", "Doctoral Student (PhD, EngD, ...)", "Postdoc", "Lecturer", "Senior Lecturer", "Reader", "Associate Professor ", "Assistant Professor ", "Professor", "Emeritus Professor" ]
defaultDepartmentHints = [ "Aeronautics & Astronautics", "Anesthesia", "Anthropology", "Applied Physics", "Art & Art History", "Biochemistry", "Bioengineering", "Biology", "Business School Library", "Business, Graduate School of", "Cardiothoracic Surgery", "Chemical and Systems Biology", "Chemical Engineering", "Chemistry", "Civil & Environmental Engineering", "Classics", "Communication", "Comparative Literature", "Comparative Medicine", "Computer Science", "Dermatology", "Developmental Biology", "Earth System Science", "East Asian Languages and Cultures", "Economics", "Education, School of", "Electrical Engineering", "Energy Resources Engineering", "English", "French and Italian", "Genetics", "Geological Sciences", "Geophysics", "German Studies", "Health Research & Policy", "History", "Iberian & Latin American Cultures", "Law Library", "Law School", "Linguistics", "Management Science & Engineering", "Materials Science & Engineering", "Mathematics", "Mechanical Engineering", "Medical Library", "Medicine", "Microbiology & Immunology", "Molecular & Cellular Physiology", "Music", "Neurobiology", "Neurology & Neurological Sciences", "Neurosurgery", "Obstetrics and Gynecology", "Ophthalmology", "Orthopaedic Surgery", "Otolaryngology (Head and Neck Surgery)", "Pathology", "Pediatrics", "Philosophy", "Physics", "Political Science", "Psychiatry and Behavioral Sciences", "Psychology", "Radiation Oncology", "Radiology", "Religious Studies", "Slavic Languages and Literature", "Sociology", "University Libraries", "Statistics", "Structural Biology", "Surgery", "Theater and Performance Studies", "Urology" ]
domainsBlackList = { "overleaf.com" : true }
commonTLDs = [ "br", "cn", "co", "co.jp", "co.uk", "com", "com.au", "de","fr", "in", "info", "io", "net", "no", "ru", "se", "us", "com.tw", "com.br", "pl", "it", "co.in", "com.mx" ]
commonDomains = [ "gmail", "googlemail", "icloud", "me", "yahoo", "ymail", "yahoomail", "hotmail", "live", "msn", "outlook", "gmx", "mail", "aol", "163", "mac", "qq", "o2", "libero", "126" ]
for domain in commonDomains
for tld in commonTLDs
domainsBlackList["#{domain}.#{tld}"] = true
App.factory "UserAffiliationsDataService", ["$http", "$q", "_", ($http, $q, _) ->
getCountries = () ->
$q.resolve countriesList
getDefaultRoleHints = () ->
$q.resolve defaultRoleHints
getDefaultDepartmentHints = () ->
$q.resolve defaultDepartmentHints
2018-07-05 06:36:02 -04:00
getUserEmails = () ->
$http.get "/user/emails"
.then (response) -> response.data
getUserDefaultEmail = () ->
getUserEmails().then (userEmails) ->
_.find userEmails, (userEmail) -> userEmail.default
getUniversitiesFromCountry = (country) ->
if universities[country.code]?
universitiesFromCountry = universities[country.code]
else
universitiesFromCountry = $http
.get "/institutions/list" , { params: { country_code: country.code } }
.then (response) -> universities[country.code] = response.data
$q.resolve(universitiesFromCountry)
getUniversityDomainFromPartialDomainInput = (partialDomainInput) ->
if universitiesByDomain[partialDomainInput]?
$q.resolve universitiesByDomain[partialDomainInput]
else
$http.get "/institutions/domains" , { params: { hostname: partialDomainInput, limit: 1 } }
.then (response) ->
university = response.data[0]
if university? and !isDomainBlacklisted university.hostname
universitiesByDomain[university.hostname] = university
$q.resolve university
else
$q.reject null
getUniversityDetails = (universityId) ->
$http.get "/institutions/list/#{ universityId }"
.then (response) -> response.data
addUserEmail = (email) ->
$http.post "/user/emails", {
2018-06-20 09:22:06 -04:00
email,
_csrf: window.csrfToken
}
addUserAffiliationWithUnknownUniversity = (email, unknownUniversityName, unknownUniversityCountryCode, role, department) ->
$http.post "/user/emails", {
email,
university:
name: unknownUniversityName
country_code: unknownUniversityCountryCode
role,
2018-06-20 09:22:06 -04:00
department,
_csrf: window.csrfToken
}
addUserAffiliation = (email, universityId, role, department) ->
$http.post "/user/emails", {
email,
university:
id: universityId
role,
2018-06-20 09:22:06 -04:00
department,
_csrf: window.csrfToken
}
addRoleAndDepartment = (email, role, department) ->
$http.post "/user/emails/endorse", {
email,
role,
department,
_csrf: window.csrfToken
}
setDefaultUserEmail = (email) ->
$http.post "/user/emails/default", {
email,
_csrf: window.csrfToken
}
removeUserEmail = (email) ->
2018-06-26 13:50:12 -04:00
$http.post "/user/emails/delete", {
email,
_csrf: window.csrfToken
}
resendConfirmationEmail = (email) ->
$http.post "/user/emails/resend_confirmation", {
email,
_csrf: window.csrfToken
}
isDomainBlacklisted = (domain) ->
domain.toLowerCase() of domainsBlackList
return {
getCountries
getDefaultRoleHints
getDefaultDepartmentHints
getUserEmails
getUserDefaultEmail
getUniversitiesFromCountry
getUniversityDomainFromPartialDomainInput
getUniversityDetails
addUserEmail
addUserAffiliationWithUnknownUniversity
addUserAffiliation
addRoleAndDepartment
setDefaultUserEmail
removeUserEmail
resendConfirmationEmail
isDomainBlacklisted
}
]