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

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