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 17 kB

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