Welcome to mirror list, hosted at ThFree Co, Russian Federation.

interactions.js « post « models « app « javascripts « assets « app - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 572375c7320b07ffbf07dc528207d67cf63fe53f (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
111
112
113
114
115
116
117
118
119
120
121
122
123
// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-v3-or-Later

//require ../post

app.models.Post.Interactions = Backbone.Model.extend({
  initialize : function(options){
    this.post = options.post;
    this.comments = new app.collections.Comments(this.get("comments"), {post : this.post});
    this.likes = new app.collections.Likes(this.get("likes"), {post : this.post});
    this.reshares = new app.collections.Reshares(this.get("reshares"), {post : this.post});
  },

  likesCount : function(){
    return this.get("likes_count");
  },

  resharesCount : function(){
    return this.get("reshares_count");
  },

  commentsCount : function(){
    return this.get("comments_count");
  },

  userLike : function(){
    return this.likes.select(function(like){
      return like.get("author") && like.get("author").guid === app.currentUser.get("guid");
    })[0];
  },

  userReshare : function(){
    return this.reshares.select(function(reshare){
      return reshare.get("author") && reshare.get("author").guid === app.currentUser.get("guid");
    })[0];
  },

  toggleLike : function() {
    if(this.userLike()) {
      this.unlike();
    } else {
      this.like();
    }
  },

  like : function() {
    var self = this;
    this.likes.create({}, {
      success: function() {
        self.post.set({participation: true});
        self.trigger("change");
        self.set({"likes_count" : self.get("likes_count") + 1});
        self.likes.trigger("change");
      },
      error: function(model, response) {
        app.flashMessages.handleAjaxError(response);
      }
    });
  },

  unlike : function() {
    var self = this;
    this.userLike().destroy({success : function() {
      self.post.set({participation: false});
      self.trigger('change');
      self.set({"likes_count" : self.get("likes_count") - 1});
      self.likes.trigger("change");
    },
      error: function(model, response) {
        app.flashMessages.handleAjaxError(response);
    }});
  },

  comment: function(text, options) {
    var self = this;
    options = options || {};

    this.comments.make(text).fail(function(response) {
      app.flashMessages.handleAjaxError(response);
      if (options.error) { options.error(); }
    }).done(function() {
      self.post.set({participation: true});
      self.set({"comments_count": self.get("comments_count") + 1});
      self.trigger('change'); //updates after sync
      if (options.success) { options.success(); }
    });
  },

  removedComment: function() {
    this.set({"comments_count": this.get("comments_count") - 1});
  },

  reshare : function(){
    var interactions = this;

    this.post.reshare().save()
      .done(function(reshare) {
        app.flashMessages.success(Diaspora.I18n.t("reshares.successful"));
        interactions.reshares.add(reshare);
        interactions.post.set({participation: true});
        if (app.stream && /^\/(?:stream|activity|aspects)/.test(app.stream.basePath())) {
          app.stream.addNow(reshare);
        }
        interactions.trigger("change");
        interactions.set({"reshares_count": interactions.get("reshares_count") + 1});
        interactions.reshares.trigger("change");
      })
      .fail(function(response) {
        app.flashMessages.handleAjaxError(response);
      });
  },

  userCanReshare : function(){
    var isReshare = this.post.get("post_type") === "Reshare"
      , rootExists = (isReshare ? this.post.get("root") : true)
      , publicPost = this.post.get("public")
      , userIsNotAuthor = this.post.get("author").diaspora_id !== app.currentUser.get("diaspora_id")
      , userIsNotRootAuthor = rootExists && (isReshare ? this.post.get("root").author.diaspora_id !== app.currentUser.get("diaspora_id") : true)
      , notReshared = !this.userReshare();

    return publicPost && app.currentUser.authenticated() && userIsNotAuthor && userIsNotRootAuthor && notReshared;
  }
});
// @license-end