summaryrefslogtreecommitdiff
path: root/fake-service/lib/pixelated_service/fake.rb
blob: f87bb711764b81332933d6abceb81d2af1158291 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
require 'open-uri'
require 'archive/tar/minitar'
require 'fileutils'

module PixelatedService
  class << self
    def mail_service
      @mail_service ||= MailService.new
    end
  end

  module Fake
    PERSONAS = [
      Persona.new(1, "Yago Macedo", nil, "sirineu@souza.org")
    ]

    def personas
      PERSONAS.map(&:ident)
    end

    def persona(i)
      PERSONAS.select { |x| x.ident.to_s == i}.first
    end

    def mails(query, page_number, window_size)
      with_timing do
        stats, mails = PixelatedService.mail_service.mails(query, page_number, window_size)
        { stats: stats, mails: mails.to_a }
      end
    end

    def contacts(query, page_number, window_size)
      with_timing do
        contacts = PixelatedService.mail_service.contacts(query, page_number, window_size)
        { contacts: contacts.to_a }
      end
    end

    def contact(ix)
      PixelatedService.mail_service.contact(ix)
    end

    def delete_mails(query, page_number, window_size, mails_idents)
      idents = mails_idents.gsub(/[\[\]]/, '').split(',').collect {|x| x.to_i}
      PixelatedService.mail_service.delete_mails(query, page_number, window_size, idents)
      []
    end

    def mail(i)
      PixelatedService.mail_service.mail(i)
    end

    def send_mail(data)
      PixelatedService.mail_service.send_mail(data)
    end

    def update_mail(data)
      PixelatedService.mail_service.update_mail(data)
    end

    def delete_mail(i)
      PixelatedService.mail_service.delete_mail(i)
    end

    def draft_reply_for(i)
      PixelatedService.mail_service.draft_reply_for(i)
    end

    def tags(i)
      PixelatedService.mail_service.mail(i).tag_names
    end

    def create_tag(tag_json)
      PixelatedService.mail_service.create_tag tag_json
    end

    def all_tags(q)
      PixelatedService.mail_service.tags(q)
    end

    def settags(i, body)
      m = PixelatedService.mail_service.mail(i)
      m.tag_names = body["newtags"]
      m.tag_names
    end

    def starmail(i, val)
      m = PixelatedService.mail_service.mail(i)
      m.starred = val if m
      ""
    end

    def repliedmail(i, val)
      m = PixelatedService.mail_service.mail(i)
      m.replied = val if m
      ""
    end

    def readmail(i, val)
      m = PixelatedService.mail_service.mail(i)
      m.read = val if m
      ""
    end

    def readmails(mail_idents, val)
      idents = mail_idents.gsub(/[\[\]]/, '').split(',').collect {|x| x.to_i}
      PixelatedService.mail_service.each { |k,v| readmail(k.ident, val) if idents.include?(k.ident) }
      []
    end

    def control_create_mail
      PixelatedService.mail_service.create
      ""
    end

    def control_delete_mails
      PixelatedService.mail_service.clean
      ""
    end

    def control_mailset_load(name)
      mbox_root = "#{ENV['HOME']}/mailsets"
      if (Dir["#{mbox_root}/mediumtagged/mbox*"].empty?)

        FileUtils.mkdir_p(mbox_root)
        unless (File.exists?("#{mbox_root}/mediumtagged.tar.gz"))
          medium_tagged = File.new("#{mbox_root}/mediumtagged.tar.gz", 'w')
          web_medium_tagged = open('https://example.wazokazi.is:8154/go/static/mediumtagged.tar.gz', :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE)
          medium_tagged.write(web_medium_tagged.read)
          medium_tagged.close
        end
        Archive::Tar::Minitar.unpack("#{mbox_root}/mediumtagged.tar.gz", mbox_root)
      end

      with_timing do
        {
          stats: PixelatedService.mail_service.load_mailset(name),
          loaded: name
        }
      end
    end

    def stats
      PixelatedService.mail_service.stats_report
    end

    def with_timing
      before = Time.now
      result = yield
      after = Time.now
      res = case result
            when Hash
              result.dup
            when nil
              {}
            else
              { result: result }
            end
      res[:timing] = { duration: after - before }
      res
    end
  end
end