Skip to content

Commit

Permalink
[share dialog - share link] added 'share-link-api.js' back & used it
Browse files Browse the repository at this point in the history
  • Loading branch information
llj committed Jul 19, 2024
1 parent 48e1750 commit e4977cb
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Button } from 'reactstrap';
import { Utils } from '../../utils/utils';
import Loading from '../loading';
import toaster from '../toast';
import { seafileAPI } from '../../utils/seafile-api';
import { shareLinkAPI } from '../../utils/share-link-api';

class EmailItem extends React.Component {

Expand Down Expand Up @@ -94,7 +94,7 @@ class LinkAuthenticatedEmails extends React.Component {

getItems = () => {
const { linkToken, path } = this.props;
seafileAPI.listShareLinkAuthEmails(linkToken, path).then(res => {
shareLinkAPI.listShareLinkAuthEmails(linkToken, path).then(res => {
this.setState({authEmails: res.data.auth_list});

Check warning on line 98 in frontend/src/components/share-link-panel/link-authenticated-emails.js

View workflow job for this annotation

GitHub Actions / build (20.x)

A space is required after '{'

Check warning on line 98 in frontend/src/components/share-link-panel/link-authenticated-emails.js

View workflow job for this annotation

GitHub Actions / build (20.x)

A space is required before '}'
}).catch(error => {
let errMessage = Utils.getErrorMsg(error);
Expand All @@ -108,7 +108,7 @@ class LinkAuthenticatedEmails extends React.Component {
this.setState({
isSubmitting: true
});
seafileAPI.addShareLinkAuthEmails(linkToken, inputEmails, path).then(res => {
shareLinkAPI.addShareLinkAuthEmails(linkToken, inputEmails, path).then(res => {
const { success, failed } = res.data;
let newEmails = [];
if (success.length) {
Expand Down Expand Up @@ -139,7 +139,7 @@ class LinkAuthenticatedEmails extends React.Component {
deleteItem = (email) => {
const { linkToken, path } = this.props;
let emails = [email, ];

Check warning on line 141 in frontend/src/components/share-link-panel/link-authenticated-emails.js

View workflow job for this annotation

GitHub Actions / build (20.x)

There should be no space before ']'
seafileAPI.deleteShareLinkAuthEmails(linkToken, emails, path).then(res => {
shareLinkAPI.deleteShareLinkAuthEmails(linkToken, emails, path).then(res => {
let authEmails = this.state.authEmails.filter(e => {
return e !== email;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { Fragment } from 'react';
import PropTypes from 'prop-types';
import { gettext } from '../../utils/constants';
import { Button } from 'reactstrap';
import { seafileAPI } from '../../utils/seafile-api';
import { shareLinkAPI } from '../../utils/share-link-api';
import { Utils } from '../../utils/utils';
import UserSelect from '../user-select';
import toaster from '../toast';
Expand Down Expand Up @@ -99,7 +99,7 @@ class LinkAuthenticatedUsers extends React.Component {

listLinkAuthUsers = () => {
const { linkToken, path } = this.props;
seafileAPI.listShareLinkAuthUsers(linkToken, path).then(res => {
shareLinkAPI.listShareLinkAuthUsers(linkToken, path).then(res => {
this.setState({authUsers: res.data.auth_list});

Check warning on line 103 in frontend/src/components/share-link-panel/link-authenticated-users.js

View workflow job for this annotation

GitHub Actions / build (20.x)

A space is required after '{'

Check warning on line 103 in frontend/src/components/share-link-panel/link-authenticated-users.js

View workflow job for this annotation

GitHub Actions / build (20.x)

A space is required before '}'
}).catch(error => {
let errMessage = Utils.getErrorMsg(error);
Expand All @@ -114,7 +114,7 @@ class LinkAuthenticatedUsers extends React.Component {
return false;
}
const users = selectedOption.map((item, index) => item.email);
seafileAPI.addShareLinkAuthUsers(linkToken, users, path).then(res => {
shareLinkAPI.addShareLinkAuthUsers(linkToken, users, path).then(res => {
const { success, failed } = res.data;
if (success.length) {
let newNames = success.map(item => item.name);
Expand All @@ -141,7 +141,7 @@ class LinkAuthenticatedUsers extends React.Component {
deleteItem = (username) => {
const { linkToken, path } = this.props;
let users = [username, ];

Check warning on line 143 in frontend/src/components/share-link-panel/link-authenticated-users.js

View workflow job for this annotation

GitHub Actions / build (20.x)

There should be no space before ']'
seafileAPI.deleteShareLinkAuthUsers(linkToken, users, path).then(res => {
shareLinkAPI.deleteShareLinkAuthUsers(linkToken, users, path).then(res => {
let authUsers = this.state.authUsers.filter(user => {
return user.username !== username;
});
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/components/share-link-panel/link-creation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import moment from 'moment';
import { Button, Form, FormGroup, Label, Input, InputGroup, InputGroupAddon, Alert } from 'reactstrap';
import { gettext, shareLinkExpireDaysMin, shareLinkExpireDaysMax, shareLinkExpireDaysDefault, shareLinkForceUsePassword, shareLinkPasswordMinLength, shareLinkPasswordStrengthLevel, isEmailConfigured } from '../../utils/constants';
import { seafileAPI } from '../../utils/seafile-api';
import { shareLinkAPI } from '../../utils/share-link-api';
import { Utils } from '../../utils/utils';
import ShareLink from '../../models/share-link';
import toaster from '../toast';
Expand Down Expand Up @@ -133,7 +134,7 @@ class LinkCreation extends React.Component {
if (currentScope === 'specific_emails' && inputEmails) {
users = inputEmails;
}
request = seafileAPI.createMultiShareLink(repoID, itemPath, password, expirationTime, permissions, currentScope, users);
request = shareLinkAPI.createMultiShareLink(repoID, itemPath, password, expirationTime, permissions, currentScope, users);
}

request.then((res) => {
Expand Down
7 changes: 3 additions & 4 deletions frontend/src/components/share-link-panel/link-details.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Button, Input, InputGroup, InputGroupAddon } from 'reactstrap';
import { gettext, shareLinkExpireDaysMin, shareLinkExpireDaysMax, shareLinkExpireDaysDefault, canSendShareLinkEmail } from '../../utils/constants';
import CommonOperationConfirmationDialog from '../../components/dialog/common-operation-confirmation-dialog';
import { seafileAPI } from '../../utils/seafile-api';
import { shareLinkAPI } from '../../utils/share-link-api';
import { Utils } from '../../utils/utils';
import ShareLink from '../../models/share-link';
import toaster from '../toast';
Expand Down Expand Up @@ -40,9 +41,7 @@ class LinkDetails extends React.Component {
expireDays: this.props.defaultExpireDays,
expDate: null,
isLinkDeleteDialogOpen: false,
isSendLinkShown: false,

currentScope: this.props.sharedLinkInfo.user_scope, // all_users, specific_users, spcific_emails
isSendLinkShown: false
};
}

Expand Down Expand Up @@ -137,7 +136,7 @@ class LinkDetails extends React.Component {
changeScope = (scope) => {
const { sharedLinkInfo } = this.props;
const { token } = sharedLinkInfo;
seafileAPI.updateShareLink(token, '', '', scope).then((res) => {
shareLinkAPI.updateShareLink(token, '', '', scope).then((res) => {
this.props.updateLink(new ShareLink(res.data));
}).catch((error) => {
let errMessage = Utils.getErrorMsg(error);
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/utils/seafile-api.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import cookie from 'react-cookies';
import { SeafileAPI } from 'seafile-js';
//import { SeafileAPI } from 'seafile-js';
import { SeafileAPI } from './a';
import { siteRoot } from './constants';

let seafileAPI = new SeafileAPI();
Expand Down
130 changes: 130 additions & 0 deletions frontend/src/utils/share-link-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import cookie from 'react-cookies';
import {siteRoot} from './constants';
import axios from 'axios';

class ShareLinkAPI {
init({server, username, password, token}) {
this.server = server;
this.username = username;
this.password = password;
this.token = token; //none

Check failure on line 10 in frontend/src/utils/share-link-api.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Multiple spaces found before '//none'
if (this.token && this.server) {
this.req = axios.create({
baseURL: this.server,
headers: {'Authorization': 'Token ' + this.token},
});
}
return this;
}

initForSeahubUsage({siteRoot, xcsrfHeaders}) {
if (siteRoot && siteRoot.charAt(siteRoot.length - 1) === '/') {
let server = siteRoot.substring(0, siteRoot.length - 1);
this.server = server;
} else {
this.server = siteRoot;
}
this.req = axios.create({
headers: {
'X-CSRFToken': xcsrfHeaders,
}
});
return this;
}

_sendPostRequest(url, form) {
if (form.getHeaders) {
return this.req.post(url, form, {
headers: form.getHeaders()
});
} else {
return this.req.post(url, form);
}
}

listShareLinkAuthUsers(link_token, path) {
const url = this.server + '/api/v2.1/share-links/' + link_token + '/user-auth/?path=' + encodeURIComponent(path);
return this.req.get(url);
}

addShareLinkAuthUsers(link_token, emails, path) {
const url = this.server + '/api/v2.1/share-links/' + link_token + '/user-auth/?path=' + encodeURIComponent(path);
const data = {
emails: emails,
};
return this.req.post(url, data);

}

deleteShareLinkAuthUsers(link_token, emails, path) {
const url = this.server + '/api/v2.1/share-links/' + link_token + '/user-auth/?path=' + encodeURIComponent(path);
const params = {
emails: emails,
};
return this.req.delete(url, {data: params});
}

listShareLinkAuthEmails(link_token, path) {
const url = this.server + '/api/v2.1/share-links/' + link_token + '/email-auth/?path=' + encodeURIComponent(path);
return this.req.get(url);
}

addShareLinkAuthEmails(link_token, emails, path) {
const url = this.server + '/api/v2.1/share-links/' + link_token + '/email-auth/?path=' + encodeURIComponent(path);
const data = {
emails: emails,
};
return this.req.post(url, data);

}

deleteShareLinkAuthEmails(link_token, emails, path) {
const url = this.server + '/api/v2.1/share-links/' + link_token + '/email-auth/?path=' + encodeURIComponent(path);
const params = {
emails: emails,
};
return this.req.delete(url, {data: params});
}

updateShareLink(token, permissions, expirationTime='', userScope='') {

Check failure on line 89 in frontend/src/utils/share-link-api.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Operator '=' must be spaced

Check failure on line 89 in frontend/src/utils/share-link-api.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Operator '=' must be spaced
var url = this.server + '/api/v2.1/share-links/' + token + '/';
let form = new FormData();
if (permissions) {
form.append('permissions', permissions);
}
if (expirationTime) {
form.append('expiration_time', expirationTime);
}
if (userScope) {
form.append('user_scope', userScope);
}
return this.req.put(url, form);
}

createMultiShareLink(repoID, path, password, expirationTime, permissions, scope, users) {
const url = this.server + '/api/v2.1/multi-share-links/';
let form = {
'path': path,
'repo_id': repoID,
'user_scope': scope,
};
if (permissions) {
form['permissions'] = permissions;
}
if (password) {
form['password'] = password;
}
if (expirationTime) {
form['expiration_time'] = expirationTime;
}
if (users) {
form['emails'] = users;
}
return this._sendPostRequest(url, form);
}
}

let shareLinkAPI = new ShareLinkAPI();
let xcsrfHeaders = cookie.load('sfcsrftoken');
shareLinkAPI.initForSeahubUsage({siteRoot, xcsrfHeaders});
export {shareLinkAPI};

0 comments on commit e4977cb

Please sign in to comment.