thedesk/app/js/platform/nano.js

137 lines
3.6 KiB
JavaScript
Raw Normal View History

2018-03-11 01:22:59 +11:00
//TL取得
function tl(data) {
2019-05-19 17:39:30 +10:00
var tlid = 0;
var acct_id = $("#post-acct-sel").val();
var type = $("#type-sel").val();
2018-03-11 01:22:59 +11:00
var domain = localStorage.getItem("domain_" + acct_id);
//タグの場合はカラム追加して描画
2019-05-19 17:39:30 +10:00
if (!type) {
//デフォルト
var type = "local";
}
var at = localStorage.getItem("acct_" + acct_id + "_at");
2018-03-11 01:22:59 +11:00
$("#notice_nano").text(cap(type, data) + " TL(" + localStorage.getItem(
"user_" + acct_id) + "@" + domain + ")");
var start = "https://" + domain + "/api/v1/timelines/" + com(type, data);
fetch(start, {
method: 'GET',
headers: {
'content-type': 'application/json',
'Authorization': 'Bearer ' + at
},
2019-05-19 17:39:30 +10:00
}).then(function (response) {
2018-03-11 01:22:59 +11:00
return response.json();
2019-05-19 17:39:30 +10:00
}).catch(function (error) {
2018-03-11 01:22:59 +11:00
console.error(error);
2019-05-19 17:39:30 +10:00
}).then(function (json) {
2018-03-11 01:22:59 +11:00
var templete = parse([json[0]], '', acct_id, tlid);
$("#timeline_nano").html(templete);
jQuery("time.timeago").timeago();
});
2019-05-19 17:39:30 +10:00
//Streaming接続
var websocket = [];
var tlid = 0;
2018-03-11 01:22:59 +11:00
if (type == "home") {
var start = "wss://" + domain +
"/api/v1/streaming/?stream=user&access_token=" + at;
} else if (type == "pub") {
var start = "wss://" + domain +
"/api/v1/streaming/?stream=public&access_token=" + at;
} else if (type == "local") {
var start = "wss://" + domain +
"/api/v1/streaming/?stream=public:local&access_token=" + at;
} else if (type == "tag") {
var start = "wss://" + domain +
2019-05-19 17:39:30 +10:00
"/api/v1/streaming/?stream=hashtag&tag=" + data + "&access_token=" + at;
}
2018-03-11 01:22:59 +11:00
var wsid = websocket.length;
websocket[wsid] = new WebSocket(start);
2019-05-19 17:39:30 +10:00
websocket[wsid].onopen = function (mess) {
2018-03-11 01:22:59 +11:00
$("#notice_icon_" + tlid).removeClass("red-text");
}
2019-05-19 17:39:30 +10:00
websocket[wsid].onmessage = function (mess) {
2018-03-11 01:22:59 +11:00
var typeA = JSON.parse(mess.data).event;
if (typeA == "delete") {
var obj = JSON.parse(mess.data).payload;
$("[toot-id=" + JSON.parse(mess.data).payload + "]").hide();
$("[toot-id=" + JSON.parse(mess.data).payload + "]").remove();
} else if (typeA == "update") {
var obj = JSON.parse(JSON.parse(mess.data).payload);
2019-05-19 17:39:30 +10:00
var templete = parse([obj], '', acct_id, tlid);
$("#timeline_nano").html(templete);
2018-03-11 01:22:59 +11:00
}
2019-05-19 17:39:30 +10:00
websocket[wsid].onclose = function (mess) {
2019-05-19 16:17:05 +10:00
console.error("Close Streaming API:" + type);
2018-03-11 01:22:59 +11:00
}
}
2019-05-19 17:39:30 +10:00
websocket[wsid].onerror = function (error) {
2018-03-11 01:22:59 +11:00
console.error('WebSocket Error ' + error);
};
}
//TLのタイトル
function cap(type, data) {
if (type == "home") {
return "Home"
} else if (type == "local") {
return "Local"
} else if (type == "pub") {
return "Public"
} else if (type == "tag") {
return "#" + data
} else if (type == "list") {
return "List(id:" + data + ")"
} else if (type == "notf") {
return "Notification"
}
}
//TLのURL
function com(type, data) {
if (type == "home") {
return "home?"
} else if (type == "local") {
return "public?local=true&"
} else if (type == "pub") {
return "public?"
} else if (type == "tag") {
return "tag/" + data + "?"
}
if (type == "list") {
return "list/" + data + "?"
}
}
//TLのアイコン
function icon(type) {
if (type == "home") {
return "home"
} else if (type == "local") {
return "people_outline"
} else if (type == "pub") {
return "language"
} else if (type == "tag") {
return "search"
}
if (type == "list") {
return "subject"
}
}
2019-05-19 17:39:30 +10:00
function todo() { }
function todc() { }
function hide() { }
$(function ($) {
2018-03-13 04:41:38 +11:00
//キーボードショートカット
2019-05-19 17:39:30 +10:00
$(window).keydown(function (e) {
2018-03-13 04:41:38 +11:00
var hasFocus = $('input').is(':focus');
var hasFocus2 = $('textarea').is(':focus');
//Ctrl+Enter:投稿
if (event.ctrlKey) {
if (e.keyCode === 13) {
post();
return false;
}
}
});
});