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

252 lines
6.3 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package base
  5. import (
  6. "bytes"
  7. "container/list"
  8. "encoding/json"
  9. "fmt"
  10. "html/template"
  11. "runtime"
  12. "strings"
  13. "time"
  14. "github.com/gogits/gogs/modules/setting"
  15. )
  16. func Str2html(raw string) template.HTML {
  17. return template.HTML(raw)
  18. }
  19. func Range(l int) []int {
  20. return make([]int, l)
  21. }
  22. func List(l *list.List) chan interface{} {
  23. e := l.Front()
  24. c := make(chan interface{})
  25. go func() {
  26. for e != nil {
  27. c <- e.Value
  28. e = e.Next()
  29. }
  30. close(c)
  31. }()
  32. return c
  33. }
  34. func ShortSha(sha1 string) string {
  35. if len(sha1) == 40 {
  36. return sha1[:10]
  37. }
  38. return sha1
  39. }
  40. var mailDomains = map[string]string{
  41. "gmail.com": "gmail.com",
  42. }
  43. var TemplateFuncs template.FuncMap = map[string]interface{}{
  44. "GoVer": func() string {
  45. return runtime.Version()
  46. },
  47. "AppName": func() string {
  48. return setting.AppName
  49. },
  50. "AppVer": func() string {
  51. return setting.AppVer
  52. },
  53. "AppDomain": func() string {
  54. return setting.Domain
  55. },
  56. "CdnMode": func() bool {
  57. return setting.ProdMode && !setting.OfflineMode
  58. },
  59. "LoadTimes": func(startTime time.Time) string {
  60. return fmt.Sprint(time.Since(startTime).Nanoseconds()/1e6) + "ms"
  61. },
  62. "AvatarLink": AvatarLink,
  63. "str2html": Str2html,
  64. "TimeSince": TimeSince,
  65. "FileSize": FileSize,
  66. "Subtract": Subtract,
  67. "Add": func(a, b int) int {
  68. return a + b
  69. },
  70. "ActionIcon": ActionIcon,
  71. "ActionDesc": ActionDesc,
  72. "DateFormat": DateFormat,
  73. "List": List,
  74. "Mail2Domain": func(mail string) string {
  75. if !strings.Contains(mail, "@") {
  76. return "try.gogits.org"
  77. }
  78. suffix := strings.SplitN(mail, "@", 2)[1]
  79. domain, ok := mailDomains[suffix]
  80. if !ok {
  81. return "mail." + suffix
  82. }
  83. return domain
  84. },
  85. "SubStr": func(str string, start, length int) string {
  86. return str[start : start+length]
  87. },
  88. "DiffTypeToStr": DiffTypeToStr,
  89. "DiffLineTypeToStr": DiffLineTypeToStr,
  90. "ShortSha": ShortSha,
  91. "Oauth2Icon": Oauth2Icon,
  92. "Oauth2Name": Oauth2Name,
  93. }
  94. type Actioner interface {
  95. GetOpType() int
  96. GetActUserName() string
  97. GetActEmail() string
  98. GetRepoUserName() string
  99. GetRepoName() string
  100. GetBranch() string
  101. GetContent() string
  102. }
  103. // ActionIcon accepts a int that represents action operation type
  104. // and returns a icon class name.
  105. func ActionIcon(opType int) string {
  106. switch opType {
  107. case 1: // Create repository.
  108. return "plus-circle"
  109. case 5, 9: // Commit repository.
  110. return "arrow-circle-o-right"
  111. case 6: // Create issue.
  112. return "exclamation-circle"
  113. case 8: // Transfer repository.
  114. return "share"
  115. case 10: // Comment issue.
  116. return "comment"
  117. default:
  118. return "invalid type"
  119. }
  120. }
  121. const (
  122. TPL_CREATE_REPO = `<a href="/user/%s">%s</a> created repository <a href="/%s">%s</a>`
  123. TPL_COMMIT_REPO = `<a href="/user/%s">%s</a> pushed to <a href="/%s/src/%s">%s</a> at <a href="/%s">%s</a>%s`
  124. TPL_COMMIT_REPO_LI = `<div><img src="%s?s=16" alt="user-avatar"/> <a href="/%s/commit/%s" rel="nofollow">%s</a> %s</div>`
  125. TPL_CREATE_ISSUE = `<a href="/user/%s">%s</a> opened issue <a href="/%s/issues/%s">%s#%s</a>
  126. <div><img src="%s?s=16" alt="user-avatar"/> %s</div>`
  127. TPL_TRANSFER_REPO = `<a href="/user/%s">%s</a> transfered repository <code>%s</code> to <a href="/%s">%s</a>`
  128. TPL_PUSH_TAG = `<a href="/user/%s">%s</a> pushed tag <a href="/%s/src/%s" rel="nofollow">%s</a> at <a href="/%s">%s</a>`
  129. TPL_COMMENT_ISSUE = `<a href="/user/%s">%s</a> commented on issue <a href="/%s/issues/%s">%s#%s</a>
  130. <div><img src="%s?s=16" alt="user-avatar"/> %s</div>`
  131. )
  132. type PushCommit struct {
  133. Sha1 string
  134. Message string
  135. AuthorEmail string
  136. AuthorName string
  137. }
  138. type PushCommits struct {
  139. Len int
  140. Commits []*PushCommit
  141. }
  142. // ActionDesc accepts int that represents action operation type
  143. // and returns the description.
  144. func ActionDesc(act Actioner) string {
  145. actUserName := act.GetActUserName()
  146. email := act.GetActEmail()
  147. repoUserName := act.GetRepoUserName()
  148. repoName := act.GetRepoName()
  149. repoLink := repoUserName + "/" + repoName
  150. branch := act.GetBranch()
  151. content := act.GetContent()
  152. switch act.GetOpType() {
  153. case 1: // Create repository.
  154. return fmt.Sprintf(TPL_CREATE_REPO, actUserName, actUserName, repoLink, repoName)
  155. case 5: // Commit repository.
  156. var push *PushCommits
  157. if err := json.Unmarshal([]byte(content), &push); err != nil {
  158. return err.Error()
  159. }
  160. buf := bytes.NewBuffer([]byte("\n"))
  161. for _, commit := range push.Commits {
  162. buf.WriteString(fmt.Sprintf(TPL_COMMIT_REPO_LI, AvatarLink(commit.AuthorEmail), repoLink, commit.Sha1, commit.Sha1[:7], commit.Message) + "\n")
  163. }
  164. if push.Len > 3 {
  165. buf.WriteString(fmt.Sprintf(`<div><a href="/%s/%s/commits/%s" rel="nofollow">%d other commits >></a></div>`, actUserName, repoName, branch, push.Len))
  166. }
  167. return fmt.Sprintf(TPL_COMMIT_REPO, actUserName, actUserName, repoLink, branch, branch, repoLink, repoLink,
  168. buf.String())
  169. case 6: // Create issue.
  170. infos := strings.SplitN(content, "|", 2)
  171. return fmt.Sprintf(TPL_CREATE_ISSUE, actUserName, actUserName, repoLink, infos[0], repoLink, infos[0],
  172. AvatarLink(email), infos[1])
  173. case 8: // Transfer repository.
  174. newRepoLink := content + "/" + repoName
  175. return fmt.Sprintf(TPL_TRANSFER_REPO, actUserName, actUserName, repoLink, newRepoLink, newRepoLink)
  176. case 9: // Push tag.
  177. return fmt.Sprintf(TPL_PUSH_TAG, actUserName, actUserName, repoLink, branch, branch, repoLink, repoLink)
  178. case 10: // Comment issue.
  179. infos := strings.SplitN(content, "|", 2)
  180. return fmt.Sprintf(TPL_COMMENT_ISSUE, actUserName, actUserName, repoLink, infos[0], repoLink, infos[0],
  181. AvatarLink(email), infos[1])
  182. default:
  183. return "invalid type"
  184. }
  185. }
  186. func DiffTypeToStr(diffType int) string {
  187. diffTypes := map[int]string{
  188. 1: "add", 2: "modify", 3: "del",
  189. }
  190. return diffTypes[diffType]
  191. }
  192. func DiffLineTypeToStr(diffType int) string {
  193. switch diffType {
  194. case 2:
  195. return "add"
  196. case 3:
  197. return "del"
  198. case 4:
  199. return "tag"
  200. }
  201. return "same"
  202. }
  203. func Oauth2Icon(t int) string {
  204. switch t {
  205. case 1:
  206. return "fa-github-square"
  207. case 2:
  208. return "fa-google-plus-square"
  209. case 3:
  210. return "fa-twitter-square"
  211. case 4:
  212. return "fa-linux"
  213. case 5:
  214. return "fa-weibo"
  215. }
  216. return ""
  217. }
  218. func Oauth2Name(t int) string {
  219. switch t {
  220. case 1:
  221. return "GitHub"
  222. case 2:
  223. return "Google"
  224. case 3:
  225. return "Twitter"
  226. case 4:
  227. return "Tencent QQ"
  228. case 5:
  229. return "Weibo"
  230. }
  231. return ""
  232. }