summaryrefslogtreecommitdiff
path: root/public/assets/js/lib/views/profile/profile.js
blob: 14511f9835613ff04d9b172e142ab358e184ec6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
var ProfileView = View.extend({

  el: "#profile",

  events: {
  },

  action: "/api/profile/",

  initialize: function(opt){
    this.template = this.$(".template").html()
    this.comments = new CommentsView ({ parent: this })
    this.files = new FilesView ({ parent: this })
    this.gallery = new GalleryView ({ parent: this })
    this.threadbox = new ThreadBox ({ parent: this })
  },

  load: function(username){
    $.get(this.action + username, this.loadProfile.bind(this))
  },

  loadProfile: function(data){
    const { user, threads, topThreads, files, comments } = data
    this.populate(user)
    // this.populateComments(topComments)
    // this.comments.load(data.comments, data.thread)
    this.gallery.load(files)
    this.files.load(files)
    this.files.sortByDate().reverse()
    this.threadbox.load({ threads, user })
  },

  populate: function(user){
    $("body").removeClass('loading')
    var $table = this.$("table.profile_meta")
    var username = sanitizeHTML(user.username)
    var is_own_profile = (username === auth.user.username)
    if (is_own_profile) {
      $(".edit_profile a").attr("href", "/profile/" + username + "/edit")
    } else {
      $(".edit_profile").hide()
    }
    this.$("img").attr("src", profile_image(username))
    var fields = [
      ["username", "Username"],
      ["realname", "Real Name"],
      ["location", "Location"],
      ["phone", "Phone"],
    ].map(pair => {
      var key = pair[0], label = pair[1]
      if (! user[key]) return;
      return [label, sanitizeHTML(user[key])]
    })

    if (user.email) {
      fields.push([
        'Email',
        '<a href="mailto:' + sanitizeHTML(user.email) + '">' + sanitizeHTML(user.email) + '</a>'
      ])
    }
    if (user.twitter) {
      if (user.twitter.match(/^http/)) {
        var partz = user.twitter.split('/')
        if (partz.length > 2) {
          var handle = sanitizeHTML(partz[3])
          fields.push([
            'Twitter',
            '<a href="https://twitter.com/' + handle + '">@' + handle + '</a>'
          ])
        }
      } else {
        var handle = sanitizeHTML(user.twitter)
        fields.push([
          'Twitter',
          '<a href="https://twitter.com/' + handle + '">@' + handle + '</a>'
        ])
      }
    }
    if (user.website) {
      var website = sanitizeHTML(user.website)
      fields.push([
        'Website',
        '<a href="' + sanitizeHTML(website) + '">' + sanitizeHTML(website) + '</a>'
      ])
    }

    fields = fields.concat([
      ["firstseen", "First Seen"],
      ["lastseen", "Last Seen"],
    ].map((pair) => {
      var key = pair[0], label = pair[1]
      if (! user[key]) return;
      var date = verbose_date(user[key])
      return [label, date[0] + ' <small>' + date[1] + '</small>']
    }))

    fields.push([
      '&nbsp;',
      '<a href="/mail/compose/' + username + '">send ' + username + ' a message</a>'
    ])

    var rows = fields.filter(pair => !!pair).map(pair => {
      var t = this.template.replace(/{{label}}/, pair[0])
                           .replace(/{{value}}/, pair[1])
      return t
    })
    $table.append(rows.join(""))
  },

})