You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

home.js 15 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. var token;
  2. if(isEmpty(token)){
  3. var meta = $("meta[name=_uid]");
  4. if(!isEmpty(meta)){
  5. token = meta.attr("content");
  6. console.log("token is uid:" + token);
  7. }
  8. }
  9. var output = document.getElementById("newmessage");
  10. var socket = new WebSocket("ws://" + document.location.host + "/action/notification");
  11. socket.onopen = function () {
  12. console.log("message has connected.");
  13. };
  14. var messageQueue = [];
  15. var maxSize = 10;
  16. var html =document.documentElement;
  17. var lang = html.attributes["lang"]
  18. var isZh = true;
  19. if(lang != null && lang =="en-US" ){
  20. isZh=false;
  21. }
  22. console.log("the language is " + lang);
  23. socket.onmessage = function (e) {
  24. var data =JSON.parse(e.data)
  25. console.log("recevie data=" + e.data)
  26. var html = "";
  27. if (data != null){
  28. console.log("queue length=" + messageQueue.length);
  29. if(messageQueue.length > maxSize){
  30. delete messageQueue[0];
  31. }else{
  32. messageQueue.push(data);
  33. }
  34. var currentTime = new Date().getTime();
  35. for(var i = 0; i < messageQueue.length;i++){
  36. var record = messageQueue[i];
  37. html += "<div class=\"swiper-slide item\">";
  38. html += " <img class=\"ui avatar image\" src=\"/user/avatar/" + record.ActUser.Name + "/-1\" alt=\"\">"
  39. html += " <div class=\"middle aligned content\">"
  40. html += " <a href=\"/" + record.ActUser.Name + "\" title=\"\">" + record.ActUser.Name + "</a>"
  41. var actionName = getAction(record.OpType,isZh);
  42. if(record.OpType == "6" || record.OpType == "10" || record.OpType == "12" || record.OpType == "13"){
  43. html += actionName;
  44. html += " <a href=\"" + getIssueLink(record) + "\" rel=\"nofollow\">" + getIssueText(record) + "</a>"
  45. }
  46. else if(record.OpType == "7" || record.OpType == "11" || record.OpType == "14" || record.OpType == "15" || record.OpType == "22"
  47. || record.OpType == "23"){
  48. html += actionName;
  49. html += " <a href=\"" + getPRLink(record) + "\" rel=\"nofollow\">" + getPRText(record) + "</a>"
  50. }
  51. else if(record.OpType == "1"){
  52. html += actionName;
  53. html += " <a href=\"" + getRepoLink(record) + "\" rel=\"nofollow\">" + getRepoLink(record) + "</a>"
  54. }
  55. else if(record.OpType == "9"){
  56. actionName = actionName.replace("{branch}",record.RefName);
  57. html += actionName;
  58. html += " <a href=\"" + getRepoLink(record) + "\" rel=\"nofollow\">" + getRepoLink(record) + "</a>"
  59. }else if(record.OpType == "17"){
  60. actionName = actionName.replace("{deleteBranchName}",record.RefName);
  61. var repoLink = "<a href=\"" + getRepoLink(record) + "\" rel=\"nofollow\">" + getRepoLink(record) + "</a>"
  62. actionName = actionName.replace("{repoName}",repoLink);
  63. html += actionName;
  64. }
  65. else if(record.OpType == "2"){
  66. actionName = actionName.replace("{oldRepoName}",record.Content);
  67. html += actionName;
  68. html += " <a href=\"" + getRepoLink(record) + "\" rel=\"nofollow\">" + getRepoLink(record) + "</a>"
  69. }
  70. else{
  71. continue;
  72. }
  73. if(record.Repo != null){
  74. var time = getTime(record.Repo.UpdatedUnix,currentTime);
  75. html += time;
  76. }
  77. html += "</div>";
  78. html += "</div>";
  79. }
  80. /*
  81. <div class="swiper-slide item">
  82. <img class="ui avatar image" src="/user/avatar/zhoupzh/-1" alt="">
  83. <div class="middle aligned content">
  84. <a href="/zhoupzh" title="">zhoupzh</a> 合并了合并请求 <a href="/OpenI/aiforge/pulls/1168" rel="nofollow">OpenI/aiforge#1168</a><span class="time-since">22 分钟前</span>
  85. </div>
  86. </div>
  87. */
  88. }
  89. console.log("html=" + html)
  90. output.innerHTML = html;
  91. };
  92. function getRepoLink(record){
  93. return "/" + record.Repo.OwnerName + "/" + record.Repo.Name;
  94. }
  95. function getRepoLink(record){
  96. return record.Repo.OwnerName + "/" + record.Repo.Name;
  97. }
  98. function getTime(UpdatedUnix,currentTime){
  99. UpdatedUnix = UpdatedUnix;
  100. currentTime = currentTime / 1000;
  101. var timeEscSecond = currentTime - UpdatedUnix;
  102. console.log("currentTime=" + currentTime + " updateUnix=" + UpdatedUnix);
  103. var hours= Math.floor(timeEscSecond / 3600);
  104. //计算相差分钟数
  105. var leave2 = Math.floor(timeEscSecond % (3600)); //计算小时数后剩余的秒数
  106. var minutes= Math.floor(leave2 / 60);//计算相差分钟数
  107. var leave3=Math.floor(leave2 % 60); //计算分钟数后剩余的秒数
  108. var seconds= leave3;
  109. if(hours == 0 && minutes == 0){
  110. return seconds + "秒前"
  111. }else{
  112. if(hours > 0){
  113. return hours + "小时前";
  114. }else{
  115. return minutes + "分钟前";
  116. }
  117. }
  118. }
  119. function getPRLink(record){
  120. return "/" + record.Repo.OwnerName + "/" + record.Repo.Name + "/pulls/" + record.ID
  121. }
  122. function getPRText(record){
  123. return record.Repo.OwnerName + "/" + record.Repo.Name + "#" + record.ID
  124. }
  125. function getIssueLink(record){
  126. return "/" + record.Repo.OwnerName + "/" + record.Repo.Name + "/issues/" + record.ID
  127. }
  128. function getIssueText(record){
  129. return record.Repo.OwnerName + "/" + record.Repo.Name + "#" + record.ID
  130. }
  131. /*
  132. ActionCreateRepo ActionType = iota + 1 // 1
  133. ActionRenameRepo // 2
  134. ActionStarRepo // 3
  135. ActionWatchRepo // 4
  136. ActionCommitRepo // 5
  137. ActionCreateIssue // 6
  138. ActionCreatePullRequest // 7
  139. ActionTransferRepo // 8
  140. ActionPushTag // 9
  141. ActionCommentIssue // 10
  142. ActionMergePullRequest // 11
  143. ActionCloseIssue // 12
  144. ActionReopenIssue // 13
  145. ActionClosePullRequest // 14
  146. ActionReopenPullRequest // 15
  147. ActionDeleteTag // 16
  148. ActionDeleteBranch // 17
  149. ActionMirrorSyncPush // 18
  150. ActionMirrorSyncCreate // 19
  151. ActionMirrorSyncDelete // 20
  152. ActionApprovePullRequest // 21
  153. ActionRejectPullRequest // 22
  154. ActionCommentPull // 23
  155. */
  156. var actionNameZH={
  157. "1":"创建了项目",
  158. "2":"重命名项目 {oldRepoName} 为",
  159. "6":"创建了任务",
  160. "7":"创建了合并请求",
  161. "9":"推送了 {branch} 分支的代码到",
  162. "10":"评论了任务",
  163. "11":"合并了合并请求",
  164. "12":"关闭了任务",
  165. "13":"重新开启了任务",
  166. "14":"关闭了合并请求",
  167. "15":"重新开启了合并请求",
  168. "17":"从 {repoName} 删除分支 {deleteBranchName}",
  169. "22":"拒绝了合并请求",
  170. "23":"评论了合并请求"
  171. };
  172. var actionNameEN={
  173. "1":"created repository",
  174. "2":"renamed repository from {oldRepoName} to ",
  175. "6":"opened issue",
  176. "7":"created pull request",
  177. "9":"pushed to {branch} at",
  178. "10":"commented on issue",
  179. "11":"merged pull request",
  180. "12":"closed issue",
  181. "13":"reopened issue",
  182. "14":"closed pull request",
  183. "15":"reopened pull request",
  184. "17":"deleted branch {deleteBranchName} from {repoName}",
  185. "22":"rejected pull request",
  186. "23":"commented on pull request"
  187. };
  188. var repoAndOrgZH={
  189. "1":"项目",
  190. "2":"成员",
  191. "3":"团队"
  192. };
  193. var repoAndOrgEN={
  194. "1":"repository",
  195. "2":"Members ",
  196. "3":"Teams"
  197. };
  198. function getAction(opType,isZh){
  199. if(isZh){
  200. return actionNameZH[opType]
  201. }else{
  202. return actionNameEN[opType]
  203. }
  204. }
  205. queryRecommendData();
  206. function queryRecommendData(){
  207. $.ajax({
  208. type:"GET",
  209. url:"/recommend/org",
  210. headers: {
  211. authorization:token,
  212. },
  213. dataType:"json",
  214. async:false,
  215. success:function(json){
  216. console.log(json);
  217. displayOrg(json);
  218. },
  219. error:function(response) {
  220. console.log(response);
  221. }
  222. });
  223. $.ajax({
  224. type:"GET",
  225. url:"/recommend/repo",
  226. headers: {
  227. authorization:token,
  228. },
  229. dataType:"json",
  230. async:false,
  231. success:function(json){
  232. console.log(json);
  233. displayRepo(json);
  234. },
  235. error:function(response) {
  236. console.log(response);
  237. }
  238. });
  239. }
  240. /*
  241. <div class="swiper-slide">
  242. <div class="ui fluid card">
  243. <div class="content">
  244. <span class="right floated meta">
  245. <i class="star icon"></i>276 <i class="star icon"></i>32
  246. </span>
  247. <img class="left floated mini ui image" src="/repo-avatars/278-a9f45e21b92b86dbf969c9f70dff1efc">
  248. <a class="header nowrap" href="/OpenI/aiforge">aiforge </a>
  249. <div class="description nowrap-2">
  250. 本项目是群体化方法与技术的开源实现案例,在基于Gitea的基础上,进一步支持社交化的协同开发、协同学习、协同研究等群体创新实践服务,特别是针对新一代人工智能技术特点,重点支持项目管理、git代码管理、大数据集存储管理与智能计算平台接入。
  251. </div>
  252. <div class="ui tags nowrap am-mt-10">
  253. <a class="ui small label topic" href="/explore/repos?q=ai%e5%bc%80%e5%8f%91%e5%b7%a5%e5%85%b7&amp;topic=">ai开发工具</a>
  254. <a class="ui small label topic" href="/explore/repos?q=openi&amp;topic=">openi</a>
  255. <a class="ui small label topic" href="/explore/repos?q=golang&amp;topic=">golang</a>
  256. <a class="ui small label topic" href="/explore/repos?q=git&amp;topic=">git</a>
  257. <a class="ui small label topic" href="/explore/repos?q=pcl&amp;topic=">pcl</a>
  258. </div>
  259. </div>
  260. </div>
  261. </div>
  262. */
  263. function displayRepo(json){
  264. var orgRepo = document.getElementById("recommendrepo");
  265. var html = "";
  266. if (json != null && json.length > 0){
  267. for(var i = 0; i < json.length;i++){
  268. var record = json[i]
  269. html += "<div class=\"swiper-slide\">";
  270. html += " <div class=\"ui fluid card\">";
  271. html += " <div class=\"content\">";
  272. html += " <span class=\"right floated meta\">";
  273. html += " <i class=\"star icon\"></i>" + record["NumStars"];
  274. html += " </span>";
  275. html += " <img class=\"left floated mini ui image\" src=\"" + record["Avatar"] + "\">";
  276. html += " <a class=\"header nowrap\" href=\"/" + record["OwnerName"] + "/" + record["Name"] + "\">" + record["Name"] +"</a>";
  277. html += " <div class=\"description nowrap-2\">" + record["Description"] + " </div>";
  278. html += " <div class=\"ui tags nowrap am-mt-10\">"
  279. if(record["Topics"] != null){
  280. for(var j = 0; j < record["Topics"].length; j++){
  281. topic = record["Topics"][j];
  282. url = "/explore/repos?q=" + topic + "&amp;topic="
  283. url = escape(url);
  284. html += "<a class=\"ui small label topic\" href=\"" + url + "\">" + topic + "</a>";
  285. }
  286. }
  287. html += " <div>";
  288. html += " <div>";
  289. html += " <div>";
  290. html += "<div>";
  291. }
  292. }
  293. orgRepo.innerHTML = html;
  294. }
  295. /**
  296. *
  297. * <div class="column">
  298. <div class="ui fluid card">
  299. <div class="content">
  300. <div class="ui small header">
  301. <img class="ui image" src="/user/avatar/OpenI/-1">
  302. <div class="content nowrap">
  303. <a href="/OpenI">OpenI</a> 启智社区
  304. <div class="sub header">39 项目 ・ 60 成员 ・ 23 团队</div>
  305. </div>
  306. </div>
  307. </div>
  308. </div>
  309. </div>
  310. */
  311. //var repoAndOrgZH = new Map([['1', "项目"], ['2', "成员"], ['3', "团队"]]);
  312. //var repoAndOrgEN = new Map([['1', "Repository"], ['2', "Members"], ['3', "Teams"]]);
  313. function getRepoOrOrg(key,isZhLang){
  314. if(isZhLang){
  315. return repoAndOrgZH[key];
  316. }else{
  317. return repoAndOrgEN[key];
  318. }
  319. }
  320. function displayOrg(json){
  321. var orgDiv = document.getElementById("recommendorg");
  322. var html = "";
  323. if (json != null && json.length > 0){
  324. for(var i = 0; i < json.length;i++){
  325. var record = json[i]
  326. html += "<div class=\"column\">";
  327. html += " <div class=\"ui fluid card\">";
  328. html += " <div class=\"content\">";
  329. html += " <div class=\"ui small header\">";
  330. html += " <img class=\"ui image\" src=\"" + record["Avatar"] + "\">";
  331. html += " <div class=\"content nowrap\">";
  332. html += " <a href=\"/" + record["Name"] + "\">" + record["Name"] + "</a> " + record["FullName"];
  333. html += " <div class=\"sub header\">" + record["NumRepos"] +" " + getRepoOrOrg(1,isZh) + " ・ " + record["NumMembers"] +" " + getRepoOrOrg(2,isZh) + " ・ " + record["NumTeams"] + " " + getRepoOrOrg(3,isZh) + "</div>";
  334. html += " </div>";
  335. html += " </div>";
  336. html += " </div>";
  337. html += " </div>";
  338. html += "</div>";
  339. }
  340. }
  341. orgDiv.innerHTML = html;
  342. }