1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "notification_conversational_content.h"
17 #include "ans_log_wrapper.h"
18
19 namespace OHOS {
20 namespace Notification {
NotificationConversationalContent(const MessageUser & messageUser)21 NotificationConversationalContent::NotificationConversationalContent(const MessageUser &messageUser)
22 : messageUser_(messageUser)
23 {}
24
GetMessageUser() const25 MessageUser NotificationConversationalContent::GetMessageUser() const
26 {
27 return messageUser_;
28 }
29
SetConversationTitle(const std::string & conversationTitle)30 void NotificationConversationalContent::SetConversationTitle(const std::string &conversationTitle)
31 {
32 conversationTitle_ = conversationTitle;
33 }
34
GetConversationTitle() const35 std::string NotificationConversationalContent::GetConversationTitle() const
36 {
37 return conversationTitle_;
38 }
39
IsConversationGroup() const40 bool NotificationConversationalContent::IsConversationGroup() const
41 {
42 return isGroup_;
43 }
44
SetConversationGroup(bool isGroup)45 void NotificationConversationalContent::SetConversationGroup(bool isGroup)
46 {
47 isGroup_ = isGroup;
48 }
49
AddConversationalMessage(const std::string & text,int64_t timestamp,const MessageUser & sender)50 void NotificationConversationalContent::AddConversationalMessage(
51 const std::string &text, int64_t timestamp, const MessageUser &sender)
52 {
53 auto message = std::make_shared<NotificationConversationalMessage>(text, timestamp, sender);
54 if (!message) {
55 ANS_LOGE("Failed to create message");
56 return;
57 }
58
59 messages_.emplace_back(message);
60 }
61
AddConversationalMessage(const MessagePtr & message)62 void NotificationConversationalContent::AddConversationalMessage(const MessagePtr &message)
63 {
64 if (!message) {
65 ANS_LOGE("Message can not be null");
66 return;
67 }
68
69 messages_.emplace_back(message);
70 }
71
GetAllConversationalMessages() const72 NotificationConversationalContent::MessageVector NotificationConversationalContent::GetAllConversationalMessages() const
73 {
74 return messages_;
75 }
76
Dump()77 std::string NotificationConversationalContent::Dump()
78 {
79 std::string messages = "";
80 for (auto &message : messages_) {
81 if (!message) {
82 messages += "nullptr, ";
83 continue;
84 }
85 messages += message->Dump();
86 messages += ", ";
87 }
88 return "NotificationConversationalContent{ " + NotificationBasicContent::Dump() +
89 ", conversationTitle = " + conversationTitle_ +
90 ", isGroup = " + (isGroup_ ? "true" : "false") +
91 ", messageUser = " + messageUser_.Dump() +
92 ", messages = " + messages +
93 " }";
94 }
95
ToJson(nlohmann::json & jsonObject) const96 bool NotificationConversationalContent::ToJson(nlohmann::json &jsonObject) const
97 {
98 if (!NotificationBasicContent::ToJson(jsonObject)) {
99 ANS_LOGE("Cannot convert basicContent to JSON");
100 return false;
101 }
102
103 nlohmann::json userObj;
104 if (!NotificationJsonConverter::ConvertToJson(&messageUser_, userObj)) {
105 ANS_LOGE("Cannot convert messageUser to JSON");
106 return false;
107 }
108 jsonObject["messageUser"] = userObj;
109
110 jsonObject["conversationTitle"] = conversationTitle_;
111 jsonObject["isGroup"] = isGroup_;
112
113 nlohmann::json msgsArr = nlohmann::json::array();
114 for (auto &msg : messages_) {
115 if (!msg) {
116 continue;
117 }
118
119 nlohmann::json msgObj;
120 if (!NotificationJsonConverter::ConvertToJson(msg.get(), msgObj)) {
121 ANS_LOGE("Cannot convert conversationalMessage to JSON");
122 return false;
123 }
124 msgsArr.emplace_back(msgObj);
125 }
126 jsonObject["messages"] = msgsArr;
127
128 return true;
129 }
130
FromJson(const nlohmann::json & jsonObject)131 NotificationConversationalContent *NotificationConversationalContent::FromJson(const nlohmann::json &jsonObject)
132 {
133 if (jsonObject.is_null() or !jsonObject.is_object()) {
134 ANS_LOGE("Invalid JSON object");
135 return nullptr;
136 }
137
138 auto pContent = new (std::nothrow) NotificationConversationalContent();
139 if (pContent == nullptr) {
140 ANS_LOGE("Failed to create conversationalContent instance");
141 return nullptr;
142 }
143
144 pContent->ReadFromJson(jsonObject);
145
146 const auto &jsonEnd = jsonObject.cend();
147 if (jsonObject.find("messageUser") != jsonEnd) {
148 auto userObj = jsonObject.at("messageUser");
149 auto pUser = NotificationJsonConverter::ConvertFromJson<MessageUser>(userObj);
150 if (pUser != nullptr) {
151 pContent->messageUser_ = *pUser;
152
153 delete pUser;
154 pUser = nullptr;
155 }
156 }
157
158 if (jsonObject.find("messages") != jsonEnd) {
159 nlohmann::json msgsArr = jsonObject.at("messages");
160 for (auto &msgObj : msgsArr) {
161 auto pMsg = NotificationJsonConverter::ConvertFromJson<NotificationConversationalMessage>(msgObj);
162 if (pMsg == nullptr) {
163 ANS_LOGE("Failed to parse message ");
164
165 delete pContent;
166 pContent = nullptr;
167 return nullptr;
168 }
169
170 pContent->messages_.emplace_back(pMsg);
171 }
172 }
173
174 return pContent;
175 }
176
Marshalling(Parcel & parcel) const177 bool NotificationConversationalContent::Marshalling(Parcel &parcel) const
178 {
179 if (!NotificationBasicContent::Marshalling(parcel)) {
180 ANS_LOGE("Failed to write basic");
181 return false;
182 }
183
184 if (!parcel.WriteString(conversationTitle_)) {
185 ANS_LOGE("Failed to write conversation title");
186 return false;
187 }
188
189 if (!parcel.WriteBool(isGroup_)) {
190 ANS_LOGE("Failed to write flag group");
191 return false;
192 }
193
194 if (!parcel.WriteParcelable(&messageUser_)) {
195 ANS_LOGE("Failed to write messageUser");
196 return false;
197 }
198
199 if (!parcel.WriteUint64(messages_.size())) {
200 ANS_LOGE("Failed to write the size of messages");
201 return false;
202 }
203
204 for (auto it = messages_.begin(); it != messages_.end(); ++it) {
205 auto valid = (*it) ? true : false;
206 if (!parcel.WriteBool(valid)) {
207 ANS_LOGE("Failed to write the flag which indicate whether message is null");
208 return false;
209 }
210
211 if (!valid) {
212 ANS_LOGE("Invalid message, write to parcel failed");
213 return false;
214 }
215
216 if (!parcel.WriteParcelable(it->get())) {
217 ANS_LOGE("Failed to write message");
218 return false;
219 }
220 }
221
222 return true;
223 }
224
Unmarshalling(Parcel & parcel)225 NotificationConversationalContent *NotificationConversationalContent::Unmarshalling(Parcel &parcel)
226 {
227 auto pContent = new (std::nothrow) NotificationConversationalContent();
228 if ((pContent != nullptr) && !pContent->ReadFromParcel(parcel)) {
229 delete pContent;
230 pContent = nullptr;
231 }
232
233 return pContent;
234 }
235
ReadFromParcel(Parcel & parcel)236 bool NotificationConversationalContent::ReadFromParcel(Parcel &parcel)
237 {
238 if (!NotificationBasicContent::ReadFromParcel(parcel)) {
239 ANS_LOGE("Failed to read basic");
240 return false;
241 }
242
243 if (!parcel.ReadString(conversationTitle_)) {
244 ANS_LOGE("Failed to read conversation title");
245 return false;
246 }
247
248 isGroup_ = parcel.ReadBool();
249
250 auto pUser = parcel.ReadParcelable<MessageUser>();
251 if (pUser == nullptr) {
252 ANS_LOGE("Failed to read messageUser");
253 return false;
254 }
255 messageUser_ = *pUser;
256
257 auto vsize = parcel.ReadUint64();
258 for (uint64_t it = 0; it < vsize; ++it) {
259 auto valid = parcel.ReadBool();
260 if (!valid) {
261 ANS_LOGE("Invalid message, read from parcel failed");
262 return false;
263 }
264
265 auto member = parcel.ReadParcelable<NotificationConversationalMessage>();
266 if (member == nullptr) {
267 ANS_LOGE("Failed to read message");
268 return false;
269 }
270
271 messages_.emplace_back(member);
272 }
273
274 return true;
275 }
276 } // namespace Notification
277 } // namespace OHOS
278