1 require 'databasedotcom/chatter/record'
2
3 module Databasedotcom
4 module Chatter
5 # A thread of private messages. When calling +Conversation.find+ or +Conversation.all+, you must pass +:user_id => <my_user_id>+ in the _parameters_
6 #
7 # Conversation.all(@client, :user_id => "me")
8 # Conversation.find(@client, "conversationId", :user_id => "f80ad89f9d98d89dfd89")
9 class Conversation < Record
10
11 # Creates a new Conversation and sets its +id+ and +url+ to values obtained from the server response.
12 def initialize(client, response)
13 super
14 @id ||= @raw_hash["conversationId"]
15 @url ||= @raw_hash["conversationUrl"]
16 end
17
18 # Find the Conversation identified by _cid_ and archive it. Returns the updated Conversation.
19 #
20 # Conversation.archive(@client, "fakeid")
21 def self.archive(client, cid)
22 url = "/services/data/v#{client.version}/chatter/users/me/conversations/#{cid}"
23 response = client.http_patch(url, nil, :archived => "true")
24 Conversation.new(client, response.body)
25 end
26
27 # Find the Conversation identified by _cid_ and unarchive it. Returns the updated Conversation.
28 #
29 # Conversation.unarchive(@client, "fakeid")
30 def self.unarchive(client, cid)
31 url = "/services/data/v#{client.version}/chatter/users/me/conversations/#{cid}"
32 response = client.http_patch(url, nil, :archived => "false")
33 Conversation.new(client, response.body)
34 end
35
36 # Find the Conversation identified by _cid_ and mark it as read. Returns the updated Conversation.
37 #
38 # Conversation.mark_read(@client, "fakeid")
39 def self.mark_read(client, cid)
40 url = "/services/data/v#{client.version}/chatter/users/me/conversations/#{cid}"
41 response = client.http_patch(url, nil, :read => "true")
42 Conversation.new(client, response.body)
43 end
44
45 # Find the Conversation identified by _cid_ and mark it as unread. Returns the updated Conversation.
46 #
47 # Conversation.mark_unread(@client, "fakeid")
48 def self.mark_unread(client, cid)
49 url = "/services/data/v#{client.version}/chatter/users/me/conversations/#{cid}"
50 response = client.http_patch(url, nil, :read => "false")
51 Conversation.new(client, response.body)
52 end
53
54 # Gets all messages for the Conversation specified by _cid_ and the User specified by _uid_. Returns a Collection of Message objects.
55 def self.messages(client, uid, cid)
56 conversation = self.find(client, cid, :user_id => uid)
57 collection = Databasedotcom::Collection.new(client, nil, conversation.raw_hash["messages"]["nextPageUrl"], conversation.raw_hash["messages"]["previousPageUrl"], conversation.raw_hash["messages"]["currentPageUrl"])
58 conversation.raw_hash["messages"]["messages"].each do |item|
59 collection << Message.new(client, item)
60 end
61 collection
62 end
63
64 # Archive this Conversation.
65 def archive
66 self.class.archive(self.client, self.id)
67 end
68
69 # Unarchive this Conversation.
70 def unarchive
71 self.class.unarchive(self.client, self.id)
72 end
73
74 # Mark this Conversation as read.
75 def mark_read
76 self.class.mark_read(self.client, self.id)
77 end
78
79 # Mark this Conversation as unread.
80 def mark_unread
81 self.class.mark_unread(self.client, self.id)
82 end
83
84 # Return a Collection of messages from this Conversation.
85 def messages
86 collection = Databasedotcom::Collection.new(client, nil, self.raw_hash["messages"]["nextPageUrl"], self.raw_hash["messages"]["previousPageUrl"], self.raw_hash["messages"]["currentPageUrl"])
87 self.raw_hash["messages"]["messages"].each do |item|
88 collection << Message.new(client, item)
89 end
90 collection
91 end
92
93 protected
94
95 def self.search_parameter_name
96 :Q
97 end
98 end
99 end
100 en