2019-01-10 15:39:37 +03:00
|
|
|
define(['globalize', 'apphost', 'loading', 'alert', 'emby-linkbutton'], function (globalize, appHost, loading, alert) {
|
|
|
|
'use strict';
|
2018-10-23 01:05:09 +03:00
|
|
|
|
|
|
|
function resolvePromise() {
|
2019-01-10 15:39:37 +03:00
|
|
|
return Promise.resolve();
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function rejectPromise() {
|
2019-01-10 15:39:37 +03:00
|
|
|
return Promise.reject();
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function showNewUserInviteMessage(result) {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (!result.IsNewUserInvitation && !result.IsPending) {
|
|
|
|
|
|
|
|
// It was immediately approved
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
var message = result.IsNewUserInvitation ?
|
|
|
|
globalize.translate('sharedcomponents#MessageInvitationSentToNewUser', result.GuestDisplayName) :
|
|
|
|
globalize.translate('sharedcomponents#MessageInvitationSentToUser', result.GuestDisplayName);
|
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
return alert({
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
text: message,
|
2019-01-10 15:39:37 +03:00
|
|
|
title: globalize.translate('sharedcomponents#HeaderInvitationSent')
|
|
|
|
|
|
|
|
}).then(resolvePromise, resolvePromise);
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function inviteGuest(options) {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
var apiClient = options.apiClient;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
loading.show();
|
|
|
|
|
|
|
|
// Add/Update connect info
|
|
|
|
return apiClient.ajax({
|
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
type: "POST",
|
2019-01-10 15:39:37 +03:00
|
|
|
url: apiClient.getUrl('Connect/Invite'),
|
|
|
|
dataType: 'json',
|
2018-10-23 01:05:09 +03:00
|
|
|
data: options.guestOptions || {}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
}).then(function (result) {
|
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
loading.hide();
|
2019-01-10 15:39:37 +03:00
|
|
|
return showNewUserInviteMessage(result);
|
|
|
|
|
|
|
|
}, function (response) {
|
|
|
|
|
|
|
|
loading.hide();
|
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
var statusCode = response ? response.status : 0;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (statusCode === 502) {
|
|
|
|
return showConnectServerUnreachableErrorMessage().then(rejectPromise, rejectPromise);
|
|
|
|
}
|
|
|
|
else if (statusCode === 404) {
|
|
|
|
// User doesn't exist
|
|
|
|
return alert({
|
|
|
|
text: globalize.translate('sharedcomponents#GuestUserNotFound')
|
|
|
|
}).then(rejectPromise, rejectPromise);
|
|
|
|
|
|
|
|
} else if ((statusCode || 0) >= 500) {
|
|
|
|
|
|
|
|
// Unable to reach connect server ?
|
|
|
|
return alert({
|
|
|
|
text: globalize.translate('sharedcomponents#ErrorReachingEmbyConnect')
|
|
|
|
}).then(rejectPromise, rejectPromise);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// status 400 = account not activated
|
|
|
|
|
|
|
|
// General error
|
|
|
|
return showGuestGeneralErrorMessage().then(rejectPromise, rejectPromise);
|
|
|
|
}
|
|
|
|
});
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function showGuestGeneralErrorMessage() {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
var html;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (appHost.supports('externallinks')) {
|
|
|
|
html = globalize.translate('sharedcomponents#ErrorAddingGuestAccount1', '<a is="emby-linkbutton" class="button-link" href="https://emby.media/connect" target="_blank">https://emby.media/connect</a>');
|
|
|
|
html += '<br/><br/>' + globalize.translate('sharedcomponents#ErrorAddingGuestAccount2', 'apps@emby.media');
|
|
|
|
}
|
|
|
|
|
|
|
|
var text = globalize.translate('sharedcomponents#ErrorAddingGuestAccount1', 'https://emby.media/connect');
|
|
|
|
text += '\n\n' + globalize.translate('sharedcomponents#ErrorAddingGuestAccount2', 'apps@emby.media');
|
|
|
|
|
|
|
|
return alert({
|
2018-10-23 01:05:09 +03:00
|
|
|
text: text,
|
|
|
|
html: html
|
2019-01-10 15:39:37 +03:00
|
|
|
});
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function showConnectServerUnreachableErrorMessage() {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
var text = globalize.translate('sharedcomponents#ErrorConnectServerUnreachable', 'https://connect.emby.media');
|
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
return alert({
|
|
|
|
text: text
|
2019-01-10 15:39:37 +03:00
|
|
|
});
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function showLinkUserErrorMessage(username, statusCode) {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
var html;
|
|
|
|
var text;
|
|
|
|
|
|
|
|
if (statusCode === 502) {
|
|
|
|
return showConnectServerUnreachableErrorMessage();
|
|
|
|
}
|
|
|
|
else if (username) {
|
|
|
|
|
|
|
|
if (appHost.supports('externallinks')) {
|
|
|
|
html = globalize.translate('sharedcomponents#ErrorAddingEmbyConnectAccount1', '<a is="emby-linkbutton" class="button-link" href="https://emby.media/connect" target="_blank">https://emby.media/connect</a>');
|
|
|
|
html += '<br/><br/>' + globalize.translate('sharedcomponents#ErrorAddingEmbyConnectAccount2', 'apps@emby.media');
|
|
|
|
}
|
|
|
|
|
|
|
|
text = globalize.translate('sharedcomponents#ErrorAddingEmbyConnectAccount1', 'https://emby.media/connect');
|
|
|
|
text += '\n\n' + globalize.translate('sharedcomponents#ErrorAddingEmbyConnectAccount2', 'apps@emby.media');
|
|
|
|
|
|
|
|
} else {
|
|
|
|
html = text = globalize.translate('sharedcomponents#DefaultErrorMessage');
|
|
|
|
}
|
|
|
|
|
|
|
|
return alert({
|
2018-10-23 01:05:09 +03:00
|
|
|
text: text,
|
|
|
|
html: html
|
2019-01-10 15:39:37 +03:00
|
|
|
});
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateUserLink(apiClient, user, newConnectUsername) {
|
2019-01-10 15:39:37 +03:00
|
|
|
var currentConnectUsername = user.ConnectUserName || '';
|
|
|
|
var enteredConnectUsername = newConnectUsername;
|
|
|
|
|
|
|
|
var linkUrl = apiClient.getUrl('Users/' + user.Id + '/Connect/Link');
|
|
|
|
|
|
|
|
if (currentConnectUsername && !enteredConnectUsername) {
|
|
|
|
|
|
|
|
// Remove connect info
|
|
|
|
// Add/Update connect info
|
|
|
|
return apiClient.ajax({
|
|
|
|
|
|
|
|
type: "DELETE",
|
|
|
|
url: linkUrl
|
|
|
|
|
|
|
|
}).then(function () {
|
|
|
|
|
|
|
|
return alert({
|
|
|
|
text: globalize.translate('sharedcomponents#MessageEmbyAccontRemoved'),
|
|
|
|
title: globalize.translate('sharedcomponents#HeaderEmbyAccountRemoved'),
|
|
|
|
|
|
|
|
}).catch(resolvePromise);
|
|
|
|
|
|
|
|
}, function (response) {
|
|
|
|
|
|
|
|
var statusCode = response ? response.status : 0;
|
|
|
|
|
|
|
|
if (statusCode === 502) {
|
|
|
|
return showConnectServerUnreachableErrorMessage().then(rejectPromise);
|
|
|
|
}
|
|
|
|
|
|
|
|
return alert({
|
|
|
|
text: globalize.translate('sharedcomponents#ErrorRemovingEmbyConnectAccount')
|
|
|
|
|
|
|
|
}).then(rejectPromise);
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (currentConnectUsername !== enteredConnectUsername) {
|
|
|
|
|
|
|
|
// Add/Update connect info
|
|
|
|
return apiClient.ajax({
|
|
|
|
type: "POST",
|
|
|
|
url: linkUrl,
|
|
|
|
data: {
|
|
|
|
ConnectUsername: enteredConnectUsername
|
|
|
|
},
|
|
|
|
dataType: 'json'
|
|
|
|
|
|
|
|
}).then(function (result) {
|
|
|
|
|
|
|
|
var msgKey = result.IsPending ? 'sharedcomponents#MessagePendingEmbyAccountAdded' : 'sharedcomponents#MessageEmbyAccountAdded';
|
|
|
|
|
|
|
|
return alert({
|
|
|
|
text: globalize.translate(msgKey),
|
|
|
|
title: globalize.translate('sharedcomponents#HeaderEmbyAccountAdded'),
|
|
|
|
|
|
|
|
}).catch(resolvePromise);
|
|
|
|
|
|
|
|
}, function (response) {
|
|
|
|
|
|
|
|
var statusCode = response ? response.status : 0;
|
|
|
|
|
|
|
|
if (statusCode === 502) {
|
|
|
|
return showConnectServerUnreachableErrorMessage().then(rejectPromise);
|
|
|
|
}
|
|
|
|
|
|
|
|
return showLinkUserErrorMessage('.', statusCode).then(rejectPromise);
|
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return Promise.reject();
|
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
return {
|
|
|
|
inviteGuest: inviteGuest,
|
|
|
|
updateUserLink: updateUserLink,
|
|
|
|
showLinkUserErrorMessage: showLinkUserErrorMessage,
|
|
|
|
showConnectServerUnreachableErrorMessage: showConnectServerUnreachableErrorMessage
|
2019-01-10 15:39:37 +03:00
|
|
|
};
|
2018-10-23 01:05:09 +03:00
|
|
|
});
|