thedesk/app/js/tl/list.js

385 lines
9.9 KiB
JavaScript
Raw Normal View History

2019-05-19 17:39:30 +10:00
function listMenu() {
2020-04-22 00:56:04 +10:00
$('#left-menu a').removeClass('active')
2019-11-09 00:52:54 +11:00
$('#listMenu').addClass('active')
$('.menu-content').addClass('hide')
$('#list-box').removeClass('hide')
$('ul.tabs').tabs('select_tab', 'src-sta')
$('#src-contents').html('')
2018-07-28 07:25:12 +10:00
}
2019-05-19 17:39:30 +10:00
function list() {
2019-11-09 00:52:54 +11:00
$('#lists-user').html('')
var acct_id = $('#list-acct-sel').val()
var domain = localStorage.getItem('domain_' + acct_id)
var at = localStorage.getItem('acct_' + acct_id + '_at')
if (localStorage.getItem('mode_' + domain) == 'misskey') {
var start = 'https://' + domain + '/api/users/lists/list'
2019-04-08 01:14:06 +10:00
fetch(start, {
2019-11-09 00:52:54 +11:00
method: 'POST',
2019-04-08 01:14:06 +10:00
body: JSON.stringify({
2019-05-19 17:39:30 +10:00
i: at
2019-11-04 03:10:06 +11:00
})
})
.then(function(response) {
if (!response.ok) {
response.text().then(function(text) {
2019-11-09 00:52:54 +11:00
setLog(response.url, response.status, text)
})
2019-11-04 03:10:06 +11:00
}
2019-11-09 00:52:54 +11:00
return response.json()
2019-11-04 03:10:06 +11:00
})
.catch(function(error) {
2019-11-09 00:52:54 +11:00
todo(error)
setLog(start, 'JSON', error)
console.error(error)
2019-11-04 03:10:06 +11:00
})
.then(function(json) {
if (json) {
2019-11-09 00:52:54 +11:00
var lists = ''
2019-11-04 03:10:06 +11:00
Object.keys(json).forEach(function(key) {
2019-11-09 00:52:54 +11:00
var list = json[key]
lists =
lists +
escapeHTML(list.title) +
`:<a onclick="listShow('${list.id}','${escapeHTML(
list.title
)}','${acct_id}')" class="pointer">
${lang.lang_list_show}
</a><br>`
})
$('#lists').html(lists)
2019-11-04 03:10:06 +11:00
} else {
2019-11-09 00:52:54 +11:00
$('#lists').html(lang.lang_list_nodata)
2019-11-04 03:10:06 +11:00
}
2019-11-09 00:52:54 +11:00
})
2019-05-19 17:39:30 +10:00
} else {
2019-11-09 00:52:54 +11:00
var start = 'https://' + domain + '/api/v1/lists'
2019-04-08 01:14:06 +10:00
fetch(start, {
2019-11-09 00:52:54 +11:00
method: 'GET',
2019-04-08 01:14:06 +10:00
headers: {
2019-11-09 00:52:54 +11:00
'content-type': 'application/json',
Authorization: 'Bearer ' + at
2019-04-08 01:14:06 +10:00
}
2019-11-04 03:10:06 +11:00
})
.then(function(response) {
if (!response.ok) {
response.text().then(function(text) {
2019-11-09 00:52:54 +11:00
setLog(response.url, response.status, text)
})
2019-11-04 03:10:06 +11:00
}
2019-11-09 00:52:54 +11:00
return response.json()
2019-11-04 03:10:06 +11:00
})
.catch(function(error) {
2019-11-09 00:52:54 +11:00
todo(error)
setLog(start, 'JSON', error)
console.error(error)
2019-11-04 03:10:06 +11:00
})
.then(function(json) {
if (json) {
2019-11-09 00:52:54 +11:00
var lists = ''
2019-11-04 03:10:06 +11:00
Object.keys(json).forEach(function(key) {
2019-11-09 00:52:54 +11:00
var list = json[key]
lists =
lists +
escapeHTML(list.title) +
`:<a onclick="listShow('${list.id}','${escapeHTML(
list.title
)}','${acct_id}')" class="pointer">
${lang.lang_list_show}
</a>/
<a onclick="listUser('${list.id}','${acct_id}')" class="pointer">
${lang.lang_list_users}
2021-04-18 20:11:39 +10:00
</a><br>`
2019-11-09 00:52:54 +11:00
})
$('#lists').html(lists)
2019-11-04 03:10:06 +11:00
} else {
2019-11-09 00:52:54 +11:00
$('#lists').html(lang.lang_list_nodata)
2019-11-04 03:10:06 +11:00
}
2019-11-09 00:52:54 +11:00
})
2019-04-08 01:14:06 +10:00
}
2018-04-01 07:00:27 +10:00
}
2019-05-19 17:39:30 +10:00
function makeNewList() {
2019-11-09 00:52:54 +11:00
var acct_id = $('#list-acct-sel').val()
var text = $('#list-add').val()
var domain = localStorage.getItem('domain_' + acct_id)
var at = localStorage.getItem('acct_' + acct_id + '_at')
if (localStorage.getItem('mode_' + domain) != 'misskey') {
var start = 'https://' + domain + '/api/v1/lists'
var httpreq = new XMLHttpRequest()
httpreq.open('POST', start, true)
httpreq.setRequestHeader('Content-Type', 'application/json')
httpreq.setRequestHeader('Authorization', 'Bearer ' + at)
httpreq.responseType = 'json'
2019-11-04 03:10:06 +11:00
httpreq.send(
JSON.stringify({
title: text
})
2019-11-09 00:52:54 +11:00
)
2019-11-04 03:10:06 +11:00
httpreq.onreadystatechange = function() {
2019-05-19 17:39:30 +10:00
if (httpreq.readyState === 4) {
2019-11-09 00:52:54 +11:00
var json = httpreq.response
2019-11-04 03:10:06 +11:00
if (this.status !== 200) {
2019-11-09 00:52:54 +11:00
setLog(start, this.status, this.response)
2019-11-04 03:10:06 +11:00
}
2019-11-09 00:52:54 +11:00
list()
$('#list-add').val('')
2019-05-19 17:39:30 +10:00
}
2019-11-09 00:52:54 +11:00
}
2019-05-19 17:39:30 +10:00
} else {
2019-11-09 00:52:54 +11:00
var start = 'https://' + domain + '/api/users/lists/create'
var httpreq = new XMLHttpRequest()
httpreq.open('POST', start, true)
httpreq.setRequestHeader('Content-Type', 'application/json')
httpreq.responseType = 'json'
2019-11-04 03:10:06 +11:00
httpreq.send(
JSON.stringify({
i: at,
title: text
})
2019-11-09 00:52:54 +11:00
)
2019-11-04 03:10:06 +11:00
httpreq.onreadystatechange = function() {
2019-05-19 17:39:30 +10:00
if (httpreq.readyState === 4) {
2019-11-09 00:52:54 +11:00
var json = httpreq.response
2019-11-04 03:10:06 +11:00
if (this.status !== 200) {
2019-11-09 00:52:54 +11:00
setLog(start, this.status, this.response)
2019-11-04 03:10:06 +11:00
}
2019-11-09 00:52:54 +11:00
list()
$('#list-add').val('')
2019-05-19 17:39:30 +10:00
}
2019-11-09 00:52:54 +11:00
}
2019-04-08 01:14:06 +10:00
}
}
2019-05-19 17:39:30 +10:00
function listShow(id, title, acct_id) {
2019-11-09 00:52:54 +11:00
localStorage.setItem('list_' + id + '_' + acct_id, title)
tl('list', id, acct_id, 'add')
2018-04-01 07:00:27 +10:00
}
2019-05-19 17:39:30 +10:00
function listUser(id, acct_id) {
2019-11-09 00:52:54 +11:00
var domain = localStorage.getItem('domain_' + acct_id)
var at = localStorage.getItem('acct_' + acct_id + '_at')
var start = 'https://' + domain + '/api/v1/lists/' + id + '/accounts'
2018-04-01 07:00:27 +10:00
fetch(start, {
2019-11-09 00:52:54 +11:00
method: 'GET',
2018-04-01 07:00:27 +10:00
headers: {
2019-11-09 00:52:54 +11:00
'content-type': 'application/json',
Authorization: 'Bearer ' + at
2019-10-31 02:30:26 +11:00
}
2019-11-04 03:10:06 +11:00
})
.then(function(response) {
if (!response.ok) {
response.text().then(function(text) {
2019-11-09 00:52:54 +11:00
setLog(response.url, response.status, text)
})
2019-05-19 17:39:30 +10:00
}
2019-11-09 00:52:54 +11:00
return response.json()
2019-11-04 03:10:06 +11:00
})
.catch(function(error) {
2019-11-09 00:52:54 +11:00
todo(error)
setLog(start, 'JSON', error)
console.error(error)
2019-11-04 03:10:06 +11:00
})
.then(function(json) {
if (json) {
2019-11-09 00:52:54 +11:00
var lists = ''
var templete = userparse(json, '', acct_id)
2019-11-04 03:10:06 +11:00
if (!json[0]) {
2019-11-09 00:52:54 +11:00
templete = lang.lang_list_nouser
2019-11-04 03:10:06 +11:00
}
2019-11-09 00:52:54 +11:00
$('#lists-user').html(templete)
jQuery('time.timeago').timeago()
2019-11-04 03:10:06 +11:00
} else {
2019-11-09 00:52:54 +11:00
$('#lists-user').html(lang.lang_list_nouser)
2019-11-04 03:10:06 +11:00
}
2019-11-09 00:52:54 +11:00
})
2018-04-01 07:00:27 +10:00
}
2019-05-19 17:39:30 +10:00
function hisList(user, acct_id) {
2019-11-09 00:52:54 +11:00
var domain = localStorage.getItem('domain_' + acct_id)
var at = localStorage.getItem('acct_' + acct_id + '_at')
if (localStorage.getItem('mode_' + domain) != 'misskey') {
var start = 'https://' + domain + '/api/v1/lists'
2019-05-19 17:39:30 +10:00
fetch(start, {
2019-11-09 00:52:54 +11:00
method: 'GET',
2019-05-19 17:39:30 +10:00
headers: {
2019-11-09 00:52:54 +11:00
'content-type': 'application/json',
Authorization: 'Bearer ' + at
2019-05-19 17:39:30 +10:00
}
2019-11-04 03:10:06 +11:00
})
.then(function(response) {
if (!response.ok) {
response.text().then(function(text) {
2019-11-09 00:52:54 +11:00
setLog(response.url, response.status, text)
})
2019-11-04 03:10:06 +11:00
}
2019-11-09 00:52:54 +11:00
return response.json()
2019-11-04 03:10:06 +11:00
})
.catch(function(error) {
2019-11-09 00:52:54 +11:00
todo(error)
setLog(start, 'JSON', error)
console.error(error)
2019-11-04 03:10:06 +11:00
})
.then(function(json) {
if (json) {
2019-11-09 00:52:54 +11:00
var lists = lang.lang_list_add + '<br>'
2019-11-04 03:10:06 +11:00
Object.keys(json).forEach(function(key) {
2019-11-09 00:52:54 +11:00
var list = json[key]
lists =
lists +
`<a onclick="listAdd('${list.id}','${user}','${acct_id}')" class="pointer">
${escapeHTML(list.title)}
</a><br> `
})
$('#his-lists-a').html(lists)
2019-11-04 03:10:06 +11:00
} else {
2019-11-09 00:52:54 +11:00
$('#his-lists-a').html(lang.lang_list_nodata)
2019-11-04 03:10:06 +11:00
}
2019-11-09 00:52:54 +11:00
})
var start = 'https://' + domain + '/api/v1/accounts/' + user + '/lists'
2019-05-19 17:39:30 +10:00
fetch(start, {
2019-11-09 00:52:54 +11:00
method: 'GET',
2019-05-19 17:39:30 +10:00
headers: {
2019-11-09 00:52:54 +11:00
'content-type': 'application/json',
Authorization: 'Bearer ' + at
2019-05-19 17:39:30 +10:00
}
2019-11-04 03:10:06 +11:00
})
.then(function(response) {
if (!response.ok) {
response.text().then(function(text) {
2019-11-09 00:52:54 +11:00
setLog(response.url, response.status, text)
})
2019-11-04 03:10:06 +11:00
}
2019-11-09 00:52:54 +11:00
return response.json()
2019-11-04 03:10:06 +11:00
})
.catch(function(error) {
2019-11-09 00:52:54 +11:00
todo(error)
setLog(start, 'JSON', error)
console.error(error)
2019-11-04 03:10:06 +11:00
})
.then(function(json) {
if (json) {
2019-11-09 00:52:54 +11:00
var lists = lang.lang_list_remove + '<br>'
2019-11-04 03:10:06 +11:00
Object.keys(json).forEach(function(key) {
2019-11-09 00:52:54 +11:00
var list = json[key]
lists =
lists +
`<a onclick="listRemove('${list.id}','${user}','${acct_id}')" class="pointer">
${escapeHTML(list.title)}
</a><br> `
})
$('#his-lists-b').html(lists)
2019-11-04 03:10:06 +11:00
} else {
2019-11-09 00:52:54 +11:00
$('#his-lists-b').html(lang.lang_list_nodata)
2019-11-04 03:10:06 +11:00
}
2019-11-09 00:52:54 +11:00
})
2019-05-19 17:39:30 +10:00
} else {
2019-11-09 00:52:54 +11:00
var start = 'https://' + domain + '/api/users/lists/list'
2019-04-08 01:14:06 +10:00
fetch(start, {
2019-11-09 00:52:54 +11:00
method: 'POST',
2019-04-08 01:14:06 +10:00
body: JSON.stringify({
2019-05-19 17:39:30 +10:00
i: at
2019-11-04 03:10:06 +11:00
})
})
.then(function(response) {
if (!response.ok) {
response.text().then(function(text) {
2019-11-09 00:52:54 +11:00
setLog(response.url, response.status, text)
})
2019-11-04 03:10:06 +11:00
}
2019-11-09 00:52:54 +11:00
return response.json()
2019-11-04 03:10:06 +11:00
})
.catch(function(error) {
2019-11-09 00:52:54 +11:00
todo(error)
setLog(start, 'JSON', error)
console.error(error)
2019-11-04 03:10:06 +11:00
})
.then(function(json) {
if (json) {
2019-11-09 00:52:54 +11:00
var lists = ''
2019-11-04 03:10:06 +11:00
Object.keys(json).forEach(function(key) {
2019-11-09 00:52:54 +11:00
var list = json[key]
lists =
lists +
list.title +
`:<a onclick="listShow('${list.id}','${escapeHTML(
list.title
)}','${acct_id}')" class="pointer">
${lang.lang_list_show}
</a>/
<a onclick="listAdd('${list.id}','${user}','${acct_id}')" class="pointer">
${lang.lang_list_add}
${lang.lang_list_add_misskey}
'</a><br>`
})
$('#his-lists-a').html(lists)
2019-11-04 03:10:06 +11:00
} else {
2019-11-09 00:52:54 +11:00
$('#his-lists-a').html(lang.lang_list_nodata)
2019-11-04 03:10:06 +11:00
}
2019-11-09 00:52:54 +11:00
})
$('#his-lists-b').html('')
2019-05-19 17:39:30 +10:00
}
2019-04-08 01:14:06 +10:00
}
2019-05-19 17:39:30 +10:00
function listAdd(id, user, acct_id) {
2019-11-09 00:52:54 +11:00
var domain = localStorage.getItem('domain_' + acct_id)
var at = localStorage.getItem('acct_' + acct_id + '_at')
if (localStorage.getItem('mode_' + domain) == 'misskey') {
var start = 'https://' + domain + '/api/users/lists/push'
2019-05-19 17:39:30 +10:00
var i = {
i: at,
listId: id,
userId: user
2019-11-09 00:52:54 +11:00
}
2019-05-19 17:39:30 +10:00
} else {
2019-11-09 00:52:54 +11:00
var start = 'https://' + domain + '/api/v1/lists/' + id + '/accounts'
2019-05-19 17:39:30 +10:00
var i = {
2019-04-08 01:14:06 +10:00
account_ids: [user]
2019-11-09 00:52:54 +11:00
}
2019-04-08 01:14:06 +10:00
}
2019-11-09 00:52:54 +11:00
var httpreq = new XMLHttpRequest()
httpreq.open('POST', start, true)
httpreq.setRequestHeader('Content-Type', 'application/json')
httpreq.setRequestHeader('Authorization', 'Bearer ' + at)
httpreq.responseType = 'json'
httpreq.send(JSON.stringify(i))
2019-11-04 03:10:06 +11:00
httpreq.onreadystatechange = function() {
2019-03-08 05:19:26 +11:00
if (httpreq.readyState === 4) {
2019-11-09 00:52:54 +11:00
var json = httpreq.response
2019-11-04 03:10:06 +11:00
if (this.status !== 200) {
2019-11-09 00:52:54 +11:00
setLog(start, this.status, this.response)
2019-11-04 03:10:06 +11:00
}
2019-11-09 00:52:54 +11:00
hisList(user, acct_id)
2018-04-17 03:10:35 +10:00
}
2019-11-09 00:52:54 +11:00
}
2018-04-01 07:00:27 +10:00
}
2019-05-19 17:39:30 +10:00
function listRemove(id, user, acct_id) {
2019-11-09 00:52:54 +11:00
var domain = localStorage.getItem('domain_' + acct_id)
var at = localStorage.getItem('acct_' + acct_id + '_at')
if (localStorage.getItem('mode_' + domain) == 'misskey') {
var start = 'https://' + domain + '/api/users/lists/push'
var method = 'POST'
2019-05-19 17:39:30 +10:00
var i = {
i: at,
listId: id,
userId: user
2019-11-09 00:52:54 +11:00
}
2019-05-19 17:39:30 +10:00
} else {
2019-11-09 00:52:54 +11:00
var start = 'https://' + domain + '/api/v1/lists/' + id + '/accounts'
var method = 'DELETE'
2019-05-19 17:39:30 +10:00
var i = {
2019-04-08 01:14:06 +10:00
account_ids: [user]
2019-11-09 00:52:54 +11:00
}
2019-04-08 01:14:06 +10:00
}
2019-11-09 00:52:54 +11:00
var httpreq = new XMLHttpRequest()
httpreq.open(method, start, true)
httpreq.setRequestHeader('Content-Type', 'application/json')
httpreq.setRequestHeader('Authorization', 'Bearer ' + at)
httpreq.responseType = 'json'
httpreq.send(JSON.stringify(i))
2019-11-04 03:10:06 +11:00
httpreq.onreadystatechange = function() {
2019-03-08 05:19:26 +11:00
if (httpreq.readyState === 4) {
2019-11-09 00:52:54 +11:00
var json = httpreq.response
2019-11-04 03:10:06 +11:00
if (this.status !== 200) {
2019-11-09 00:52:54 +11:00
setLog(start, this.status, this.response)
2019-11-04 03:10:06 +11:00
}
2019-11-09 00:52:54 +11:00
hisList(user, acct_id)
2018-04-17 03:10:35 +10:00
}
2019-11-09 00:52:54 +11:00
}
2019-11-04 03:10:06 +11:00
}