blob: 1c971a64c7794e8ce811d7c1253ba874a8214512 [file] [log] [blame]
Olivier Soldanod4311552017-11-20 15:09:53 -05001/*
2 * Copyright (C) 2017 Savoir-faire Linux Inc.
3 * Author: Anthony Léonard <anthony.leonard@savoirfairelinux.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#import <Foundation/Foundation.h>
Andreas Traczyk252a94a2018-04-20 16:36:20 -040021#import "NSString+Extensions.h"
Olivier Soldanod4311552017-11-20 15:09:53 -050022#import <api/conversation.h>
23#import <api/conversationmodel.h>
24#import <api/account.h>
25#import <api/contactmodel.h>
26#import <api/contact.h>
Anthony Léonard6f819752018-01-05 09:53:40 -050027#import <map>
Olivier Soldanod4311552017-11-20 15:09:53 -050028
29static inline NSString* bestIDForConversation(const lrc::api::conversation::Info& conv, const lrc::api::ConversationModel& model)
30{
31 auto contact = model.owner.contactModel->getContact(conv.participants[0]);
Andreas Traczyk252a94a2018-04-20 16:36:20 -040032 if (!contact.registeredName.empty()) {
33 contact.registeredName.erase(std::remove(contact.registeredName.begin(), contact.registeredName.end(), '\n'), contact.registeredName.end());
34 contact.registeredName.erase(std::remove(contact.registeredName.begin(), contact.registeredName.end(), '\r'), contact.registeredName.end());
35 return [@(contact.registeredName.c_str()) removeEmptyLinesAtBorders];
36 }
Olivier Soldanod4311552017-11-20 15:09:53 -050037 else
Andreas Traczyk252a94a2018-04-20 16:36:20 -040038 return [@(contact.profileInfo.uri.c_str()) removeEmptyLinesAtBorders];
Olivier Soldanod4311552017-11-20 15:09:53 -050039}
40
41static inline NSString* bestNameForConversation(const lrc::api::conversation::Info& conv, const lrc::api::ConversationModel& model)
42{
43 auto contact = model.owner.contactModel->getContact(conv.participants[0]);
Andreas Traczyk252a94a2018-04-20 16:36:20 -040044 if (contact.profileInfo.alias.empty()) {
Olivier Soldanod4311552017-11-20 15:09:53 -050045 return bestIDForConversation(conv, model);
Andreas Traczyk252a94a2018-04-20 16:36:20 -040046 }
47 auto alias = contact.profileInfo.alias;
48 alias.erase(std::remove(alias.begin(), alias.end(), '\n'), alias.end());
49 alias.erase(std::remove(alias.begin(), alias.end(), '\r'), alias.end());
50 if(alias.length() == 0) {
51 return bestIDForConversation(conv, model);
52 }
53 return @(alias.c_str());
54}
55
56static inline lrc::api::profile::Type profileType(const lrc::api::conversation::Info& conv, const lrc::api::ConversationModel& model)
57{
58 @try {
59 auto contact = model.owner.contactModel->getContact(conv.participants[0]);
60 return contact.profileInfo.type;
61 }
62 @catch (NSException *exception) {
63 lrc::api::profile::Type::INVALID;
64 }
Olivier Soldanod4311552017-11-20 15:09:53 -050065}
Anthony Léonard6f819752018-01-05 09:53:40 -050066
67/**
68 * This function return an iterator pointing to a Conversation::Info in ConversationModel given its uid. If not found
69 * the iterator is invalid thus it needs to be checked by caller.
70 * @param uid UID of conversation being searched
71 * @param model ConversationModel in which to do the lookup
72 * @return iterator pointing to corresponding Conversation if any. Points to past-the-end element otherwise.
73 */
74static inline lrc::api::ConversationModel::ConversationQueue::const_iterator getConversationFromUid(const std::string& uid, const lrc::api::ConversationModel& model) {
75 return std::find_if(model.allFilteredConversations().begin(), model.allFilteredConversations().end(),
76 [&] (const lrc::api::conversation::Info& conv) {
77 return uid == conv.uid;
78 });
79}